| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import java.awt.geom.Path2D; |
| 25 |
import java.lang.invoke.MethodHandle; |
| 26 |
import java.lang.invoke.MethodHandles; |
| 27 |
import java.lang.invoke.MethodType; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.attributes.FillRule; |
| 33 |
import com.github.weisj.jsvg.attributes.value.ConstantValue; |
| 34 |
import com.github.weisj.jsvg.geometry.FillRuleAwareAWTSVGShape; |
| 35 |
import com.github.weisj.jsvg.geometry.SVGShape; |
| 36 |
import com.github.weisj.jsvg.geometry.path.BuildHistory; |
| 37 |
import com.github.weisj.jsvg.geometry.path.PathCommand; |
| 38 |
import com.github.weisj.jsvg.geometry.path.PathParser; |
| 39 |
|
| 40 |
public final class PathUtil { |
| 41 |
|
| 42 |
private static final @Nullable MethodHandle trimPathHandle = lookupTrimPathMethod(); |
| 43 |
|
| 44 |
|
| 45 |
private static @Nullable MethodHandle lookupTrimPathMethod() { |
| 46 |
try { |
| 47 |
MethodType methodType = MethodType.methodType(void.class); |
| 48 |
return MethodHandles.lookup().findVirtual(Path2D.class, "trimToSize", methodType); |
| 49 |
} catch (NoSuchMethodException | IllegalAccessException e) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
private PathUtil() {} |
| 55 |
|
| 56 |
public static @NotNull SVGShape parseFromPathData(@NotNull String data, FillRule fillRule) { |
| 57 |
PathCommand[] pathCommands = new PathParser(data).parsePathCommand(); |
| 58 |
|
| 59 |
int nodeCount = 2; |
| 60 |
for (PathCommand pathCommand : pathCommands) { |
| 61 |
nodeCount += pathCommand.nodeCount() - 1; |
| 62 |
} |
| 63 |
|
| 64 |
Path2D path = new Path2D.Float(fillRule.awtWindingRule, nodeCount); |
| 65 |
BuildHistory hist = new BuildHistory(); |
| 66 |
|
| 67 |
for (PathCommand pathCommand : pathCommands) { |
| 68 |
pathCommand.appendPath(path, hist); |
| 69 |
} |
| 70 |
|
| 71 |
trimPathToSize(path); |
| 72 |
|
| 73 |
return new FillRuleAwareAWTSVGShape(new ConstantValue<>(path)); |
| 74 |
} |
| 75 |
|
| 76 |
public static @NotNull Path2D setPolyLine(@Nullable Path2D path, float @NotNull [] points, boolean closed) { |
| 77 |
Path2D p; |
| 78 |
if (path == null) { |
| 79 |
p = new Path2D.Float(Path2D.WIND_EVEN_ODD, points.length / 2); |
| 80 |
} else { |
| 81 |
p = path; |
| 82 |
p.reset(); |
| 83 |
} |
| 84 |
|
| 85 |
p.moveTo(points[0], points[1]); |
| 86 |
for (int i = 2; i < points.length - 1; i += 2) { |
| 87 |
p.lineTo(points[i], points[i + 1]); |
| 88 |
} |
| 89 |
if (closed) p.closePath(); |
| 90 |
return p; |
| 91 |
} |
| 92 |
|
| 93 |
public static void trimPathToSize(@NotNull Path2D path) { |
| 94 |
if (trimPathHandle != null) { |
| 95 |
try { |
| 96 |
trimPathHandle.invokeExact(path); |
| 97 |
} catch (Throwable e) { |
| 98 |
throw new RuntimeException(e); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |