| •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 |
|
| 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 |
|
| 177 |
|
| 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 |
} |
| 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(Locale.ROOT, "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( |
| 279 |
Locale.ROOT, |
| 280 |
" <rect x='0' y='0' width='%d' height='%d' style='fill: %s'/>\n", |
| 281 |
diagram.getWidth(), |
| 282 |
diagram.getHeight(), |
| 283 |
colorToHex(color) |
| 284 |
) |
| 285 |
); |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
private void renderTexts() { |
| 290 |
|
| 291 |
for (DiagramText diagramText : diagram.getTextObjects()) { |
| 292 |
|
| 293 |
Font font = diagramText.getFont(); |
| 294 |
String text = diagramText.getText(); |
| 295 |
|
| 296 |
int xPos = diagramText.getXPos(); |
| 297 |
int yPos = diagramText.getYPos(); |
| 298 |
|
| 299 |
renderText(text, xPos, yPos, font, diagramText.getColor()); |
| 300 |
|
| 301 |
if (diagramText.hasOutline()) { |
| 302 |
|
| 303 |
Color outlineColor = diagramText.getOutlineColor(); |
| 304 |
|
| 305 |
renderText(text, xPos + 1, yPos, font, outlineColor); |
| 306 |
renderText(text, xPos - 1, yPos, font, outlineColor); |
| 307 |
renderText(text, xPos, yPos + 1, font, outlineColor); |
| 308 |
renderText(text, xPos, yPos - 1, font, outlineColor); |
| 309 |
|
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
private void renderText(String text, int xPos, int yPos, Font font, Color color) { |
| 316 |
|
| 317 |
String TEXT_ELEMENT = " <text x='%d' y='%d' font-family='%s' font-size='%d' stroke='none' fill='%s' >" + |
| 318 |
"<![CDATA[%s]]></text>\n"; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
layer3.append( |
| •327 |
String.format( |
| 328 |
Locale.ROOT, |
| 329 |
TEXT_ELEMENT, |
| 330 |
xPos, |
| 331 |
yPos, |
| 332 |
options.getFontFamily(), |
| 333 |
font.getSize(), |
| 334 |
colorToHex(color), |
| 335 |
text |
| 336 |
) |
| 337 |
); |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
private static String colorToHex(Color color) { |
| •342 |
return String.format( |
| 343 |
Locale.ROOT, |
| 344 |
"#%s%s%s", |
| 345 |
toHex(color.getRed()), |
| 346 |
toHex(color.getGreen()), |
| 347 |
toHex(color.getBlue()) |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
private static String toHex(int n) { |
| 352 |
String hex = Integer.toHexString(n); |
| 353 |
|
| 354 |
return n > 15 ? hex : "0" + hex; |
| 355 |
} |
| 356 |
|
| 357 |
private final Diagram diagram; |
| 358 |
private final RenderingOptions options; |
| 359 |
|
| 360 |
private final StringBuilder layer0 = new StringBuilder(); |
| 361 |
private final StringBuilder layer1 = new StringBuilder(); |
| 362 |
private final StringBuilder layer2 = new StringBuilder(); |
| 363 |
private final StringBuilder layer3 = new StringBuilder(); |
| 364 |
|
| 365 |
private final String normalStroke; |
| 366 |
private final String dashStroke; |
| 367 |
|
| 368 |
class SVGCommands { |
| 369 |
|
| 370 |
final String svgPath; |
| 371 |
final boolean isClosed; |
| 372 |
|
| 373 |
SVGCommands(GeneralPath path) { |
| 374 |
|
| 375 |
boolean closed = false; |
| 376 |
|
| 377 |
float[] coords = new float[6]; |
| 378 |
|
| 379 |
StringBuilder builder = new StringBuilder(); |
| 380 |
|
| 381 |
PathIterator pathIterator = path.getPathIterator(null); |
| 382 |
|
| 383 |
while (!pathIterator.isDone()) { |
| 384 |
|
| 385 |
String commands; |
| 386 |
|
| 387 |
switch(pathIterator.currentSegment(coords)) { |
| 388 |
case PathIterator.SEG_MOVETO: |
| 389 |
commands = "M" + coords[0] + " " + coords[1] + " "; |
| 390 |
break; |
| 391 |
case PathIterator.SEG_LINETO: |
| 392 |
commands = "L" + coords[0] + " " + coords[1] + " "; |
| 393 |
break; |
| 394 |
case PathIterator.SEG_QUADTO: |
| 395 |
commands = "Q" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " "; |
| 396 |
break; |
| 397 |
case PathIterator.SEG_CUBICTO: |
| 398 |
commands = "C" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " " + coords[4] + " " + coords[5] + " "; |
| 399 |
break; |
| 400 |
case PathIterator.SEG_CLOSE: |
| 401 |
commands = "z"; |
| 402 |
closed = true; |
| 403 |
break; |
| 404 |
default: |
| 405 |
commands = ""; |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
builder.append(commands); |
| 410 |
|
| 411 |
pathIterator.next(); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
isClosed = closed; |
| 416 |
svgPath = builder.toString(); |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
} |