Debian Codegraph @

src/com/github/weisj/jsvg/nodes/mesh/MeshBuilder.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2022 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.nodes.mesh;
23
24 import java.awt.geom.Point2D;
25 import java.util.List;
26 import java.util.Objects;
27
28 import org.jetbrains.annotations.NotNull;
29
30 import com.github.weisj.jsvg.geometry.mesh.Bezier;
31 import com.github.weisj.jsvg.geometry.mesh.CoonPatch;
32 import com.github.weisj.jsvg.geometry.size.Length;
33 import com.github.weisj.jsvg.nodes.SVGNode;
34 import com.github.weisj.jsvg.nodes.Stop;
35
36 final class MeshBuilder {
37 private MeshBuilder() {}
38
39 static void buildMesh(@NotNull MeshGradient meshGradient, @NotNull Point2D.Float origin) {
40 Point2D.Float start = origin;
41
42 int patchCount = -1;
43 List<? extends @NotNull SVGNode> rows = meshGradient.children();
44
45 for (int rowIndex = 0, rowCount = rows.size(); rowIndex < rowCount; rowIndex++) {
46 SVGNode child = rows.get(rowIndex);
47 MeshRow row = (MeshRow) child;
48 int rowPatchCount = row.children().size();
49 if (patchCount == -1) patchCount = rowPatchCount;
50 if (rowPatchCount != patchCount) {
51 throw new IllegalStateException("Every mesh row needs to specify the same amount of patched");
52 }
53 List<? extends @NotNull SVGNode> patchesInRow = row.children();
54 for (int patchIndex = 0; patchIndex < rowPatchCount; patchIndex++) {
55 SVGNode node = patchesInRow.get(patchIndex);
56 MeshPatch patch = (MeshPatch) node;
57
58 List<? extends @NotNull SVGNode> stops = patch.children();
59 int stopCount = stops.size();
60 int requiredStops = stopsForPatchPosition(rowIndex, patchIndex);
61 if (stopCount < requiredStops) {
62 throw new IllegalStateException("Not enough stops specified");
63 }
64 int offset = offsetForPatchPosition(rowIndex);
65 stopCount = requiredStops;
66
67 MeshPatch patchAbove = null;
68 if (offset == 1) {
69 patchAbove = (MeshPatch) ((MeshRow) rows.get(rowIndex - 1)).children().get(patchIndex);
70 patch.coonPatch.north = patchAbove.coonPatch.south.inverse();
71 patch.north = patchAbove.west;
72 }
73
74 MeshPatch patchLeft = null;
75 if (offset + stopCount < 4) {
76 patchLeft = (MeshPatch) patchesInRow.get(patchIndex - 1);
77 patch.coonPatch.west = patchLeft.coonPatch.east.inverse();
78 patch.west = patchLeft.south;
79 } else if (patchIndex > 0) {
80 patchLeft = (MeshPatch) patchesInRow.get(patchIndex - 1);
81 }
82
83 for (int stopIndex = 0; stopIndex < stopCount; stopIndex++) {
84 Stop stop = (Stop) stops.get(stopIndex);
85 switch (stopIndex + offset) {
86 case 0:
87 patch.coonPatch.north = Objects.requireNonNull(stop.bezierCommand())
88 .createBezier(start);
89 start = patch.coonPatch.north.d;
90 patch.north = patchIndex > 0 ? Objects.requireNonNull(patchLeft).east : stop.color();
91 break;
92 case 1:
93 patch.coonPatch.east = Objects.requireNonNull(stop.bezierCommand())
94 .createBezier(patch.coonPatch.north.d);
95 patch.east = offset == 1 ? Objects.requireNonNull(patchAbove).south : stop.color();
96 break;
97 case 2:
98 patch.coonPatch.south = Objects.requireNonNull(stop.bezierCommand())
99 .createBezier(patch.coonPatch.east.d);
100 patch.south = stop.color();
101 break;
102 case 3:
103 patch.coonPatch.west = Objects.requireNonNull(stop.bezierCommand())
104 .createBezier(patch.coonPatch.south.d);
105 patch.coonPatch.west.d.setLocation(patch.coonPatch.north.a);
106 patch.west = stop.color();
107 break;
108 default:
109 assert false;
110 break;
111 }
112 }
113 if (offset + stopCount < 4) {
114 assert patchLeft != null;
115 patch.coonPatch.south.d.setLocation(patchLeft.coonPatch.east.d);
116 }
117
118 try {
119 assertPatchDefined(patch.coonPatch);
120 } catch (IllegalStateException e) {
121 throw new IllegalStateException(rowIndex + "," + patchIndex + " " + patch.coonPatch, e);
122 }
123 }
124 }
125 }
126
127 private static int stopsForPatchPosition(int rowIndex, int patchIndex) {
128 if (rowIndex > 0) {
129 if (patchIndex > 0) {
130 return 2;
131 } else {
132 return 3;
133 }
134 } else {
135 if (patchIndex > 0) {
136 return 3;
137 } else {
138 return 4;
139 }
140 }
141 }
142
143 private static int offsetForPatchPosition(int rowIndex) {
144 if (rowIndex > 0) {
145 return 1;
146 } else {
147 return 0;
148 }
149 }
150
151 private static void assertPatchDefined(@NotNull CoonPatch coonPatch) {
152 if (coonPatch.north == null) throw new IllegalStateException("North path is null");
153 if (coonPatch.east == null) throw new IllegalStateException("East path is null");
154 if (coonPatch.south == null) throw new IllegalStateException("South path is null");
155 if (coonPatch.west == null) throw new IllegalStateException("West path is null");
156 if (hasUnspecifiedPoint(coonPatch.north))
157 throw new IllegalStateException("North path has unspecified point");
158 if (hasUnspecifiedPoint(coonPatch.east))
159 throw new IllegalStateException("East path has unspecified point");
160 if (hasUnspecifiedPoint(coonPatch.south))
161 throw new IllegalStateException("South path has unspecified point");
162 if (hasUnspecifiedPoint(coonPatch.west))
163 throw new IllegalStateException("West path has unspecified point");
164 }
165
166 private static boolean hasUnspecifiedPoint(Bezier bezier) {
167 if (isUnspecified(bezier.a)) return true;
168 if (isUnspecified(bezier.b)) return true;
169 if (isUnspecified(bezier.c)) return true;
170 return isUnspecified(bezier.d);
171 }
172
173 private static boolean isUnspecified(@NotNull Point2D.Float p) {
174 return !Length.isSpecified(p.x) || !Length.isSpecified(p.y);
175 }
176 }