Debian Codegraph @

src/com/github/weisj/jsvg/paint/impl/DefaultPaintParser.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 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.paint.impl;
23
24 import java.awt.*;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32
33 import com.github.weisj.jsvg.logging.Logger;
34 import com.github.weisj.jsvg.logging.impl.LogFactory;
35 import com.github.weisj.jsvg.paint.SVGPaint;
36 import com.github.weisj.jsvg.parser.PaintParser;
37 import com.github.weisj.jsvg.parser.impl.ParserUtil;
38 import com.github.weisj.jsvg.parser.impl.SeparatorMode;
39
40
41 public final class DefaultPaintParser implements PaintParser {
42 private static final Logger LOGGER = LogFactory.createLogger(DefaultPaintParser.class);
43
44 // Todo: Handle hsl(), hsla() per the SVG 2.0 spec requirement
45 @Override
46 public @Nullable Color parseColor(@NotNull String value) {
47 if (value.isEmpty()) return null;
48 try {
49 if (value.charAt(0) == '#') {
50 int rgba = 0xff000000;
51 switch (value.length()) {
52 case 4:
53 // Short rgb
54 rgba = parseHex(new char[] {
55 value.charAt(1), value.charAt(1),
56 value.charAt(2), value.charAt(2),
57 value.charAt(3), value.charAt(3),
58 'F', 'F'});
59 break;
60 case 5:
61 // Short rgba
62 rgba = parseHex(new char[] {
63 value.charAt(1), value.charAt(1),
64 value.charAt(2), value.charAt(2),
65 value.charAt(3), value.charAt(3),
66 value.charAt(4), value.charAt(4)});
67 break;
68 case 7:
69 // Long rgb
70 rgba = parseHex(new char[] {
71 value.charAt(1), value.charAt(2),
72 value.charAt(3), value.charAt(4),
73 value.charAt(5), value.charAt(6),
74 'F', 'F'});
75 break;
76 case 9:
77 // Long rgba
78 rgba = parseHex(value.substring(1).toCharArray());
79 break;
80 default:
81 break;
82 }
83 return new Color(rgba, true);
84 } else if (value.length() > 3 && value.substring(0, 3).equalsIgnoreCase("rgb")) {
85 boolean isRgba = value.length() > 4 && (value.charAt(3) == 'a' || value.charAt(3) == 'A');
86 int startIndex = isRgba ? 5 : 4;
87 String[] values = ParserUtil.parseStringList(
88 value.substring(startIndex, value.length() - 1), SeparatorMode.COMMA_AND_WHITESPACE);
89 isRgba = isRgba && values.length >= 4;
90 return new Color(
91 parseColorComponent(values[0], false),
92 parseColorComponent(values[1], false),
93 parseColorComponent(values[2], false),
94 isRgba ? parseColorComponent(values[3], true) : 255);
95 }
96 return ColorLookup.colorMap().get(value.toLowerCase(Locale.ENGLISH));
97 } catch (Exception e) {
98 LOGGER.log(Logger.Level.INFO, e.getMessage(), e);
99 return null;
100 }
101 }
102
103 @Override
104 public @Nullable SVGPaint parsePaint(@Nullable String value) {
105 if (value == null) return null;
106 String lower = value.toLowerCase(Locale.ENGLISH);
107 if ("none".equals(lower) || "transparent".equals(lower)) return PredefinedPaints.NONE;
108 if ("currentcolor".equals(lower)) return PredefinedPaints.CURRENT_COLOR;
109 if ("context-fill".equals(lower)) return PredefinedPaints.CONTEXT_FILL;
110 if ("context-stroke".equals(lower)) return PredefinedPaints.CONTEXT_STROKE;
111 Color color = parseColor(lower);
112 if (color == null) return null;
113 return new AwtSVGPaint(color);
114 }
115
116 private int parseColorComponent(String value, boolean percentage) {
117 float parsed;
118 if (value.endsWith("%")) {
119 parsed = ParserUtil.parseFloat(value.substring(0, value.length() - 1), 0);
120 parsed /= 100;
121 parsed *= 255;
122 } else {
123 parsed = ParserUtil.parseFloat(value, 0);
124 if (percentage) parsed *= 255;
125 }
126 return Math.min(255, Math.max(0, (int) parsed));
127 }
128
129 private int parseHex(char[] chars) {
130 int r = charToColorInt(chars[0]) << 4 | charToColorInt(chars[1]);
131 int g = charToColorInt(chars[2]) << 4 | charToColorInt(chars[3]);
132 int b = charToColorInt(chars[4]) << 4 | charToColorInt(chars[5]);
133 int a = charToColorInt(chars[6]) << 4 | charToColorInt(chars[7]);
134 return ((a & 0xFF) << 24) |
135 ((r & 0xFF) << 16) |
136 ((g & 0xFF) << 8) |
137 (b & 0xFF);
138 }
139
140 private int charToColorInt(char c) {
141 if (c >= '0' && c <= '9') {
142 return c - '0';
143 } else if (c >= 'a' && c <= 'z') {
144 return c - 'a' + 10;
145 } else if (c >= 'A' && c <= 'Z') {
146 return c - 'A' + 10;
147 } else {
148 return 0;
149 }
150 }
151
152 private static final class ColorLookup {
153 @SuppressWarnings("ImmutableEnumChecker")
154 private enum Cache {
155 INSTANCE;
156
157 private final Map<String, Color> colorMap;
158
159 Cache() {
160 Map<String, Color> map = new HashMap<>(144);
161 map.put("aliceblue", new Color(0xf0f8ff));
162 map.put("antiquewhite", new Color(0xfaebd7));
163 map.put("aqua", new Color(0x00ffff));
164 map.put("aquamarine", new Color(0x7fffd4));
165 map.put("azure", new Color(0xf0ffff));
166 map.put("beige", new Color(0xf5f5dc));
167 map.put("bisque", new Color(0xffe4c4));
168 map.put("black", new Color(0x000000));
169 map.put("blanchedalmond", new Color(0xffebcd));
170 map.put("blue", new Color(0x0000ff));
171 map.put("blueviolet", new Color(0x8a2be2));
172 map.put("brown", new Color(0xa52a2a));
173 map.put("burlywood", new Color(0xdeb887));
174 map.put("cadetblue", new Color(0x5f9ea0));
175 map.put("chartreuse", new Color(0x7fff00));
176 map.put("chocolate", new Color(0xd2691e));
177 map.put("coral", new Color(0xff7f50));
178 map.put("cornflowerblue", new Color(0x6495ed));
179 map.put("cornsilk", new Color(0xfff8dc));
180 map.put("crimson", new Color(0xdc143c));
181 map.put("cyan", new Color(0x00ffff));
182 map.put("darkblue", new Color(0x00008b));
183 map.put("darkcyan", new Color(0x008b8b));
184 map.put("darkgoldenrod", new Color(0xb8860b));
185 map.put("darkgray", new Color(0xa9a9a9));
186 map.put("darkgrey", new Color(0xa9a9a9));
187 map.put("darkgreen", new Color(0x006400));
188 map.put("darkkhaki", new Color(0xbdb76b));
189 map.put("darkmagenta", new Color(0x8b008b));
190 map.put("darkolivegreen", new Color(0x556b2f));
191 map.put("darkorange", new Color(0xff8c00));
192 map.put("darkorchid", new Color(0x9932cc));
193 map.put("darkred", new Color(0x8b0000));
194 map.put("darksalmon", new Color(0xe9967a));
195 map.put("darkseagreen", new Color(0x8fbc8f));
196 map.put("darkslateblue", new Color(0x483d8b));
197 map.put("darkslategray", new Color(0x2f4f4f));
198 map.put("darkslategrey", new Color(0x2f4f4f));
199 map.put("darkturquoise", new Color(0x00ced1));
200 map.put("darkviolet", new Color(0x9400d3));
201 map.put("deeppink", new Color(0xff1493));
202 map.put("deepskyblue", new Color(0x00bfff));
203 map.put("dimgray", new Color(0x696969));
204 map.put("dimgrey", new Color(0x696969));
205 map.put("dodgerblue", new Color(0x1e90ff));
206 map.put("feldspar", new Color(0xd19275));
207 map.put("firebrick", new Color(0xb22222));
208 map.put("floralwhite", new Color(0xfffaf0));
209 map.put("forestgreen", new Color(0x228b22));
210 map.put("fuchsia", new Color(0xff00ff));
211 map.put("gainsboro", new Color(0xdcdcdc));
212 map.put("ghostwhite", new Color(0xf8f8ff));
213 map.put("gold", new Color(0xffd700));
214 map.put("goldenrod", new Color(0xdaa520));
215 map.put("gray", new Color(0x808080));
216 map.put("grey", new Color(0x808080));
217 map.put("green", new Color(0x008000));
218 map.put("greenyellow", new Color(0xadff2f));
219 map.put("honeydew", new Color(0xf0fff0));
220 map.put("hotpink", new Color(0xff69b4));
221 map.put("indianred", new Color(0xcd5c5c));
222 map.put("indigo", new Color(0x4b0082));
223 map.put("ivory", new Color(0xfffff0));
224 map.put("khaki", new Color(0xf0e68c));
225 map.put("lavender", new Color(0xe6e6fa));
226 map.put("lavenderblush", new Color(0xfff0f5));
227 map.put("lawngreen", new Color(0x7cfc00));
228 map.put("lemonchiffon", new Color(0xfffacd));
229 map.put("lightblue", new Color(0xadd8e6));
230 map.put("lightcoral", new Color(0xf08080));
231 map.put("lightcyan", new Color(0xe0ffff));
232 map.put("lightgoldenrodyellow", new Color(0xfafad2));
233 map.put("lightgray", new Color(0xd3d3d3));
234 map.put("lightgrey", new Color(0xd3d3d3));
235 map.put("lightgreen", new Color(0x90ee90));
236 map.put("lightpink", new Color(0xffb6c1));
237 map.put("lightsalmon", new Color(0xffa07a));
238 map.put("lightseagreen", new Color(0x20b2aa));
239 map.put("lightskyblue", new Color(0x87cefa));
240 map.put("lightslateblue", new Color(0x8470ff));
241 map.put("lightslategray", new Color(0x778899));
242 map.put("lightslategrey", new Color(0x778899));
243 map.put("lightsteelblue", new Color(0xb0c4de));
244 map.put("lightyellow", new Color(0xffffe0));
245 map.put("lime", new Color(0x00ff00));
246 map.put("limegreen", new Color(0x32cd32));
247 map.put("linen", new Color(0xfaf0e6));
248 map.put("magenta", new Color(0xff00ff));
249 map.put("maroon", new Color(0x800000));
250 map.put("mediumaquamarine", new Color(0x66cdaa));
251 map.put("mediumblue", new Color(0x0000cd));
252 map.put("mediumorchid", new Color(0xba55d3));
253 map.put("mediumpurple", new Color(0x9370d8));
254 map.put("mediumseagreen", new Color(0x3cb371));
255 map.put("mediumslateblue", new Color(0x7b68ee));
256 map.put("mediumspringgreen", new Color(0x00fa9a));
257 map.put("mediumturquoise", new Color(0x48d1cc));
258 map.put("mediumvioletred", new Color(0xc71585));
259 map.put("midnightblue", new Color(0x191970));
260 map.put("mintcream", new Color(0xf5fffa));
261 map.put("mistyrose", new Color(0xffe4e1));
262 map.put("moccasin", new Color(0xffe4b5));
263 map.put("navajowhite", new Color(0xffdead));
264 map.put("navy", new Color(0x000080));
265 map.put("oldlace", new Color(0xfdf5e6));
266 map.put("olive", new Color(0x808000));
267 map.put("olivedrab", new Color(0x6b8e23));
268 map.put("orange", new Color(0xffa500));
269 map.put("orangered", new Color(0xff4500));
270 map.put("orchid", new Color(0xda70d6));
271 map.put("palegoldenrod", new Color(0xeee8aa));
272 map.put("palegreen", new Color(0x98fb98));
273 map.put("paleturquoise", new Color(0xafeeee));
274 map.put("palevioletred", new Color(0xd87093));
275 map.put("papayawhip", new Color(0xffefd5));
276 map.put("peachpuff", new Color(0xffdab9));
277 map.put("peru", new Color(0xcd853f));
278 map.put("pink", new Color(0xffc0cb));
279 map.put("plum", new Color(0xdda0dd));
280 map.put("powderblue", new Color(0xb0e0e6));
281 map.put("purple", new Color(0x800080));
282 map.put("red", new Color(0xff0000));
283 map.put("rosybrown", new Color(0xbc8f8f));
284 map.put("royalblue", new Color(0x4169e1));
285 map.put("saddlebrown", new Color(0x8b4513));
286 map.put("salmon", new Color(0xfa8072));
287 map.put("sandybrown", new Color(0xf4a460));
288 map.put("seagreen", new Color(0x2e8b57));
289 map.put("seashell", new Color(0xfff5ee));
290 map.put("sienna", new Color(0xa0522d));
291 map.put("silver", new Color(0xc0c0c0));
292 map.put("skyblue", new Color(0x87ceeb));
293 map.put("slateblue", new Color(0x6a5acd));
294 map.put("slategray", new Color(0x708090));
295 map.put("slategrey", new Color(0x708090));
296 map.put("snow", new Color(0xfffafa));
297 map.put("springgreen", new Color(0x00ff7f));
298 map.put("steelblue", new Color(0x4682b4));
299 map.put("tan", new Color(0xd2b48c));
300 map.put("teal", new Color(0x008080));
301 map.put("thistle", new Color(0xd8bfd8));
302 map.put("tomato", new Color(0xff6347));
303 map.put("turquoise", new Color(0x40e0d0));
304 map.put("violet", new Color(0xee82ee));
305 map.put("violetred", new Color(0xd02090));
306 map.put("wheat", new Color(0xf5deb3));
307 map.put("white", new Color(0xffffff));
308 map.put("whitesmoke", new Color(0xf5f5f5));
309 map.put("yellow", new Color(0xffff00));
310 map.put("yellowgreen", new Color(0x9acd32));
311 colorMap = Collections.unmodifiableMap(map);
312 }
313 }
314
315 private static Map<String, Color> colorMap() {
316 return Cache.INSTANCE.colorMap;
317 }
318 }
319 }