Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/SVGBuilder.java

raw symbols ◀ ▶
Source reconstructed at patch state 2 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 package org.stathissideris.ascii2image.graphics;
2
3 import org.stathissideris.ascii2image.core.RenderingOptions;
4 import org.stathissideris.ascii2image.core.ShapeAreaComparator;
5 import org.stathissideris.ascii2image.core.Shape3DOrderingComparator;
6
7 import java.awt.*;
8 import java.awt.geom.GeneralPath;
9 import java.awt.geom.PathIterator;
10 import java.util.ArrayList;
11 import java.util.Locale;
12
13 /**
14 * Created by Jean Lazarou.
15 */
16 public class SVGBuilder {
17
18 SVGBuilder(Diagram diagram, RenderingOptions options) {
19
20 this.diagram = diagram;
21 this.options = options;
22
23 float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2;
24 float strokeWeight = diagram.getMinimumOfCellDimension() / 10;
25
26 normalStroke = String.format(Locale.ROOT, "stroke-width='%f' stroke-linecap='round' stroke-linejoin='round' ",
27 strokeWeight);
28
29 dashStroke = String.format(Locale.ROOT,
30 "stroke-width='%f' stroke-dasharray='%f,%f' stroke-miterlimit='0' " +
31 "stroke-linecap='butt' stroke-linejoin='round' ",
32 strokeWeight, dashInterval, dashInterval);
33
34 }
35
36 public String build() {
37
38 return openSVGTag() + definitions() + render() + "</svg>";
39
40 }
41
42 private String definitions() {
43
44 String DEFS =
45 " <defs>\n%s" +
46 " <filter id='f2' x='0' y='0' width='200%%' height='200%%'>\n" +
47 " <feOffset result='offOut' in='SourceGraphic' dx='5' dy='5' />\n" +
48 " <feGaussianBlur result='blurOut' in='offOut' stdDeviation='3' />\n" +
49 " <feBlend in='SourceGraphic' in2='blurOut' mode='normal' />\n" +
50 " </filter>\n" +
51 " </defs>\n";
52
53 if (options.getFontURL() == null) {
54 return String.format(Locale.ROOT, DEFS, "");
55 }
56
57 String fontStyle =
58 " <style type='text/css'>\n" +
59 " @font-face {\n" +
60 " font-family: %s;\n" +
61 " src: url('%s');\n" +
62 " }\n" +
63 " </style>\n";
64
65 return String.format(Locale.ROOT, DEFS, String.format(Locale.ROOT, fontStyle, options.getFontFamily(), options.getFontURL()));
66
67 }
68
69 private String openSVGTag() {
70
71 String HEADER =
72 "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n" +
73 "<svg \n" +
74 " xmlns='http://www.w3.org/2000/svg'\n" +
75 " width='%d'\n" +
76 " height='%d'\n" +
77 " %s\n" +
78 " version='1.0'>\n";
79
80 return String.format(
81 Locale.ROOT,
82 HEADER,
83 diagram.getWidth(),
84 diagram.getHeight(),
85 antialiasing()
86 );
87
88 }
89
90 private String render() {
91
92 backgroundLayer();
93
94 renderStorageShapes();
95 renderRestOfShapes();
96 renderTexts();
97
98 return " <g stroke-width='1' stroke-linecap='square' stroke-linejoin='round'>\n" +
99 layer0.toString() +
100 layer1.toString() +
101 layer2.toString() +
102 layer3.toString() +
103 " </g>\n";
104
105 }
106
107 private void renderStorageShapes() {
108
109 ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes();
110
111 ArrayList<DiagramShape> storageShapes = findSorageShapes(shapes);
112
113 storageShapes.sort(new Shape3DOrderingComparator());
114
115 for (DiagramShape shape : storageShapes) {
116
117 GeneralPath path = shape.makeIntoRenderPath(diagram, options);
118
119 SVGCommands commands = new SVGCommands(path);
120
121 String fill = "none";
122 String color = "white";
123
124 if(!shape.isStrokeDashed()) {
125
126 renderShadow(commands);
127
128 if(shape.getFillColor() != null)
129 fill = colorToHex(shape.getFillColor());
130 else
131 fill = colorToHex(Color.white);
132
133 }
134
135 renderPath(shape, commands, color, fill);
136
137 renderPath(shape, commands, colorToHex(shape.getStrokeColor()), "none");
138
139 }
140
141 }
142
143 private ArrayList<DiagramShape> findSorageShapes(ArrayList<DiagramShape> shapes) {
144
145 ArrayList<DiagramShape> storageShapes = new ArrayList<>();
146
147 for (DiagramShape shape : shapes) {
148
149 if(shape.getType() == DiagramShape.TYPE_STORAGE) {
150 storageShapes.add(shape);
151 }
152
153 }
154
155 return storageShapes;
156
157 }
158
159 private void renderRestOfShapes() {
160
161 ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes();
162 ArrayList<DiagramShape> pointMarkers = new ArrayList<>();
163
164 shapes.sort(new ShapeAreaComparator());
165
166 for (DiagramShape shape : shapes) {
167
168 if (shape.getType() == DiagramShape.TYPE_POINT_MARKER) {
169 pointMarkers.add(shape);
170 continue;
171 }
172 if (shape.getType() == DiagramShape.TYPE_STORAGE) {
173 continue;
174 }
175 if (shape.getType() == DiagramShape.TYPE_CUSTOM) {
176 //renderCustomShape(shape, g2);
177 //continue;
178 throw new RuntimeException("Not yet implemented");
179 }
180
181 if (shape.getPoints().isEmpty()) continue;
182
183 GeneralPath path = shape.makeIntoRenderPath(diagram, options);
184
185 SVGCommands commands = new SVGCommands(path);
186
187 renderPath(shape, commands);
188
189 }
190
191 renderPointMarkers(pointMarkers);
192
193 }
194
195 private void renderPath(DiagramShape shape, SVGCommands commands) {
196
197 String fill = "none";
198
199 if (shape.isClosed() && !shape.isStrokeDashed()) {
200
201 if(shape.getFillColor() != null)
202 fill = colorToHex(shape.getFillColor());
203 else
204 fill = "white";
205
206 if (shape.getType() == DiagramShape.TYPE_ARROWHEAD) {
207 renderPath(shape, commands, "none", fill);
208 }
209
210 } else if (shape.isStrokeDashed()) {
211 fill = "white";
212 }
213
214 if (shape.getType() != DiagramShape.TYPE_ARROWHEAD) {
215
216 if (commands.isClosed && !shape.isStrokeDashed()) {
217 renderShadow(commands);
218 }
219
220 renderPath(shape, commands, colorToHex(shape.getStrokeColor()), fill);
221
222 }
223
224 }
225
226 private void renderPath(DiagramShape shape, SVGCommands commands, String stroke, String fill) {
227
228 String path = " <path stroke='" + stroke + "' ";
229
230 if (shape.isStrokeDashed())
231 path += dashStroke;
232 else
233 path += normalStroke;
234
235 path += "fill='" + fill + "' d='" + commands.svgPath + "' />\n";
236
237 layer2.append(path);
238
239 }
240
241 private void renderShadow(SVGCommands commands) {
242
243 if (!options.dropShadows()) return;
244
245 String path = " <path stroke='gray' fill='gray' filter='url(#f2)' d='" + commands.svgPath + "' />\n";
246
247 layer1.append(path);
248
249 }
250
251 private void renderPointMarkers(ArrayList<DiagramShape> pointMarkers) {
252
253 for (DiagramShape shape : pointMarkers) {
254
255 GeneralPath path = shape.makeIntoRenderPath(diagram, options);
256
257 String fill = "white";
258
259 if(shape.getFillColor() != null)
260 fill = colorToHex(shape.getFillColor());
261
262 renderPath(shape, new SVGCommands(path), colorToHex(shape.getStrokeColor()), fill);
263
264 }
265
266 }
267
268 private String antialiasing() {
269 String rendering = options.performAntialias() ? "geometricPrecision" : "optimizeSpeed";
270 return String.format(Locale.ROOT, "shape-rendering='%s'", rendering);
271 }
272
273 private void backgroundLayer() {
274
275 Color color = options.getBackgroundColor();
276
277 if (color.getAlpha() == 0) return;
278
279 layer0.append (
280 String.format(
281 Locale.ROOT,
282 " <rect x='0' y='0' width='%d' height='%d' style='fill: %s'/>\n",
283 diagram.getWidth(),
284 diagram.getHeight(),
285 colorToHex(color)
286 )
287 );
288
289 }
290
291 private void renderTexts() {
292
293 for (DiagramText diagramText : diagram.getTextObjects()) {
294
295 Font font = diagramText.getFont();
296 String text = diagramText.getText();
297
298 int xPos = diagramText.getXPos();
299 int yPos = diagramText.getYPos();
300
301 renderText(text, xPos, yPos, font, diagramText.getColor());
302
303 if (diagramText.hasOutline()) {
304
305 Color outlineColor = diagramText.getOutlineColor();
306
307 renderText(text, xPos + 1, yPos, font, outlineColor);
308 renderText(text, xPos - 1, yPos, font, outlineColor);
309 renderText(text, xPos, yPos + 1, font, outlineColor);
310 renderText(text, xPos, yPos - 1, font, outlineColor);
311
312 }
313 }
314
315 }
316
317 private void renderText(String text, int xPos, int yPos, Font font, Color color) {
318
319 String TEXT_ELEMENT = " <text x='%d' y='%d' font-family='%s' font-size='%d' stroke='none' fill='%s' >" +
320 "<![CDATA[%s]]></text>\n";
321
322 /* Prefer normal font weight
323 if (font.isBold()) {
324 style = " font-weight='bold'";
325 }
326 */
327
328 layer3.append(
329 String.format(
330 Locale.ROOT,
331 TEXT_ELEMENT,
332 xPos,
333 yPos,
334 options.getFontFamily(),
335 font.getSize(),
336 colorToHex(color),
337 text
338 )
339 );
340
341 }
342
343 private static String colorToHex(Color color) {
344 return String.format(
345 Locale.ROOT,
346 "#%s%s%s",
347 toHex(color.getRed()),
348 toHex(color.getGreen()),
349 toHex(color.getBlue())
350 );
351 }
352
353 private static String toHex(int n) {
354 String hex = Integer.toHexString(n);
355
356 return n > 15 ? hex : "0" + hex;
357 }
358
359 private final Diagram diagram;
360 private final RenderingOptions options;
361
362 private final StringBuilder layer0 = new StringBuilder();
363 private final StringBuilder layer1 = new StringBuilder();
364 private final StringBuilder layer2 = new StringBuilder();
365 private final StringBuilder layer3 = new StringBuilder();
366
367 private final String normalStroke;
368 private final String dashStroke;
369
370 class SVGCommands {
371
372 final String svgPath;
373 final boolean isClosed;
374
375 SVGCommands(GeneralPath path) {
376
377 boolean closed = false;
378
379 float[] coords = new float[6];
380
381 StringBuilder builder = new StringBuilder();
382
383 PathIterator pathIterator = path.getPathIterator(null);
384
385 while (!pathIterator.isDone()) {
386
387 String commands;
388
389 switch(pathIterator.currentSegment(coords)) {
390 case PathIterator.SEG_MOVETO:
391 commands = "M" + coords[0] + " " + coords[1] + " ";
392 break;
393 case PathIterator.SEG_LINETO:
394 commands = "L" + coords[0] + " " + coords[1] + " ";
395 break;
396 case PathIterator.SEG_QUADTO:
397 commands = "Q" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " ";
398 break;
399 case PathIterator.SEG_CUBICTO:
400 commands = "C" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " " + coords[4] + " " + coords[5] + " ";
401 break;
402 case PathIterator.SEG_CLOSE:
403 commands = "z";
404 closed = true;
405 break;
406 default:
407 commands = "";
408 break;
409 }
410
411 builder.append(commands);
412
413 pathIterator.next();
414
415 }
416
417 isClosed = closed;
418 svgPath = builder.toString();
419
420 }
421
422 }
423
424 }