Debian Codegraph @

src/com/github/weisj/jsvg/util/PathUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // Only available in Java 10 or later
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 }