Debian Codegraph @

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

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