Debian Codegraph @

debian/patches/ditaa/0001-Update-ditaa.patch

raw symbols SCIP
1 From: Tomas Janousek <[email protected]>
2 Date: Wed, 20 Nov 2019 23:37:14 +0100
3 Subject: Update ditaa
4
5 ---
6 build.xml | 7 +-
7 .../sourceforge/plantuml/ditaa/PSystemDitaa.java | 27 +-
8 .../ascii2image/core/ConfigurationParser.java | 340 +++++++++--------
9 .../ascii2image/core/ConversionOptions.java | 80 +++-
10 .../ascii2image/core/DebugUtils.java | 19 +
11 .../ascii2image/core/DocBookConverter.java | 70 ----
12 .../stathissideris/ascii2image/core/FileUtils.java | 87 +++--
13 src/org/stathissideris/ascii2image/core/Pair.java | 19 +
14 .../ascii2image/core/PerformanceTester.java | 64 ++++
15 .../ascii2image/core/ProcessingOptions.java | 29 +-
16 .../ascii2image/core/RenderingOptions.java | 100 ++---
17 .../core/Shape3DOrderingComparator.java | 34 +-
18 .../ascii2image/core/ShapeAreaComparator.java | 46 +++
19 .../ascii2image/graphics/BitmapRenderer.java | 160 ++++----
20 .../graphics/CompositeDiagramShape.java | 35 +-
21 .../graphics/CustomShapeDefinition.java | 23 +-
22 .../ascii2image/graphics/Diagram.java | 259 +++++++------
23 .../ascii2image/graphics/DiagramComponent.java | 30 +-
24 .../ascii2image/graphics/DiagramShape.java | 81 ++--
25 .../ascii2image/graphics/DiagramText.java | 24 +-
26 .../ascii2image/graphics/FontMeasurer.java | 33 +-
27 .../ascii2image/graphics/ImageHandler.java | 111 ++++--
28 .../ascii2image/graphics/OffScreenSVGRenderer.java | 138 +++++++
29 .../ascii2image/graphics/SVGBuilder.java | 416 +++++++++++++++++++++
30 .../ascii2image/graphics/SVGRenderer.java | 18 +
31 .../ascii2image/graphics/ShapeEdge.java | 29 +-
32 .../ascii2image/graphics/ShapePoint.java | 24 +-
33 .../ascii2image/text/AbstractCell.java | 23 +-
34 .../ascii2image/text/AbstractionGrid.java | 33 +-
35 .../stathissideris/ascii2image/text/CellSet.java | 103 +++--
36 .../ascii2image/text/GridPattern.java | 29 +-
37 .../ascii2image/text/GridPatternGroup.java | 23 +-
38 .../ascii2image/text/StringUtils.java | 25 +-
39 .../stathissideris/ascii2image/text/TextGrid.java | 138 ++++---
40 34 files changed, 1760 insertions(+), 917 deletions(-)
41 delete mode 100644 src/org/stathissideris/ascii2image/core/DocBookConverter.java
42 create mode 100644 src/org/stathissideris/ascii2image/core/PerformanceTester.java
43 create mode 100644 src/org/stathissideris/ascii2image/core/ShapeAreaComparator.java
44 create mode 100644 src/org/stathissideris/ascii2image/graphics/OffScreenSVGRenderer.java
45 create mode 100644 src/org/stathissideris/ascii2image/graphics/SVGBuilder.java
46 create mode 100644 src/org/stathissideris/ascii2image/graphics/SVGRenderer.java
47
48 diff --git a/build.xml b/build.xml
49 index ccda508..f976b78 100644
50 --- a/build.xml
51 +++ b/build.xml
52 @@ -20,7 +20,12 @@
53 <target name="compile">
54 <delete dir="build" />
55 <mkdir dir="build" />
56 - <javac target="7" srcdir="src" destdir="build" debug="on" />
57 + <javac target="7" srcdir="src" destdir="build" debug="on">
58 + <classpath>
59 + <pathelement location="/usr/share/java/batik-all.jar" />
60 + <pathelement location="/usr/share/java/xml-apis-ext.jar" />
61 + </classpath>
62 + </javac>
63 <copy file="src/net/sourceforge/plantuml/version/logo.png"
64 todir="build/net/sourceforge/plantuml/version" />
65 <copy file="src/net/sourceforge/plantuml/version/favicon.png"
66 diff --git a/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java b/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java
67 index 9f13178..f8c7e1c 100644
68 --- a/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java
69 +++ b/src/net/sourceforge/plantuml/ditaa/PSystemDitaa.java
70 @@ -53,7 +53,6 @@ import net.sourceforge.plantuml.core.ImageData;
71 public class PSystemDitaa extends AbstractPSystem {
72
73 // private ProcessingOptions processingOptions;
74 - private Object processingOptions;
75 private final boolean dropShadows;
76 private final String data;
77 private final float scale;
78 @@ -63,16 +62,6 @@ public class PSystemDitaa extends AbstractPSystem {
79 this.data = data;
80 this.dropShadows = dropShadows;
81 this.performSeparationOfCommonEdges = performSeparationOfCommonEdges;
82 - try {
83 - this.processingOptions = Class.forName("org.stathissideris.ascii2image.core.ProcessingOptions")
84 - .newInstance();
85 - // this.processingOptions.setPerformSeparationOfCommonEdges(performSeparationOfCommonEdges);
86 - this.processingOptions.getClass().getMethod("setPerformSeparationOfCommonEdges", boolean.class)
87 - .invoke(this.processingOptions, performSeparationOfCommonEdges);
88 - } catch (Exception e) {
89 - e.printStackTrace();
90 - this.processingOptions = null;
91 - }
92 this.scale = scale;
93 }
94
95 @@ -94,7 +83,6 @@ public class PSystemDitaa extends AbstractPSystem {
96
97 // ditaa can only export png so file format is mostly ignored
98 try {
99 - // ditaa0_9.jar
100 // final ConversionOptions options = new ConversionOptions();
101 final Object options = Class.forName("org.stathissideris.ascii2image.core.ConversionOptions").newInstance();
102
103 @@ -106,6 +94,14 @@ public class PSystemDitaa extends AbstractPSystem {
104 final Method setScale = renderingOptions.getClass().getMethod("setScale", float.class);
105 setScale.invoke(renderingOptions, scale);
106
107 + // final ProcessingOptions = options.processingOptions;
108 + final Field f_processingOptions = options.getClass().getField("processingOptions");
109 + final Object processingOptions = f_processingOptions.get(options);
110 +
111 + // processingOptions.setPerformSeparationOfCommonEdges(performSeparationOfCommonEdges);
112 + final Method setPerformSeparationOfCommonEdges = processingOptions.getClass().getMethod("setPerformSeparationOfCommonEdges", boolean.class);
113 + setPerformSeparationOfCommonEdges.invoke(processingOptions, performSeparationOfCommonEdges);
114 +
115 // options.setDropShadows(dropShadows);
116 final Method setDropShadows = options.getClass().getMethod("setDropShadows", boolean.class);
117 setDropShadows.invoke(options, dropShadows);
118 @@ -118,12 +114,9 @@ public class PSystemDitaa extends AbstractPSystem {
119 Class.forName("org.stathissideris.ascii2image.core.ProcessingOptions"));
120 initialiseWithText.invoke(grid, data, null);
121
122 - // final Diagram diagram = new Diagram(grid, options, processingOptions);
123 + // final Diagram diagram = new Diagram(grid, options);
124 final Class<?> clDiagram = Class.forName("org.stathissideris.ascii2image.graphics.Diagram");
125 - clDiagram.getConstructor(grid.getClass(), options.getClass(), processingOptions.getClass()).newInstance(
126 - grid, options, processingOptions);
127 - final Object diagram = clDiagram.getConstructor(grid.getClass(), options.getClass(),
128 - processingOptions.getClass()).newInstance(grid, options, processingOptions);
129 + final Object diagram = clDiagram.getConstructor(grid.getClass(), options.getClass()).newInstance(grid, options);
130
131 // final BitmapRenderer bitmapRenderer = new BitmapRenderer();
132 final Object bitmapRenderer = Class.forName("org.stathissideris.ascii2image.graphics.BitmapRenderer")
133 diff --git a/src/org/stathissideris/ascii2image/core/ConfigurationParser.java b/src/org/stathissideris/ascii2image/core/ConfigurationParser.java
134 index ee27881..96800b6 100644
135 --- a/src/org/stathissideris/ascii2image/core/ConfigurationParser.java
136 +++ b/src/org/stathissideris/ascii2image/core/ConfigurationParser.java
137 @@ -1,21 +1,20 @@
138 -/*
139 - * DiTAA - Diagrams Through Ascii Art
140 +/**
141 + * ditaa - Diagrams Through Ascii Art
142 *
143 - * Copyright (C) 2004 Efstathios Sideris
144 + * Copyright (C) 2004-2011 Efstathios Sideris
145 *
146 - * This program is free software; you can redistribute it and/or
147 - * modify it under the terms of the GNU General Public License
148 - * as published by the Free Software Foundation; either version 2
149 - * of the License, or (at your option) any later version.
150 + * ditaa is free software: you can redistribute it and/or modify
151 + * it under the terms of the GNU Lesser General Public License as
152 + * published by the Free Software Foundation, either version 3 of
153 + * the License, or (at your option) any later version.
154 *
155 - * This program is distributed in the hope that it will be useful,
156 + * ditaa is distributed in the hope that it will be useful,
157 * but WITHOUT ANY WARRANTY; without even the implied warranty of
158 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
159 - * GNU General Public License for more details.
160 + * GNU Lesser General Public License for more details.
161 *
162 - * You should have received a copy of the GNU General Public License
163 - * along with this program; if not, write to the Free Software
164 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
165 + * You should have received a copy of the GNU Lesser General Public
166 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
167 *
168 */
169 package org.stathissideris.ascii2image.core;
170 @@ -36,155 +35,170 @@ import org.xml.sax.helpers.DefaultHandler;
171
172 public class ConfigurationParser {
173
174 - private static final boolean DEBUG = false;
175 -
176 - private static final String INLCUDE_TAG_NAME = "include";
177 - private static final String SHAPE_TAG_NAME = "shape";
178 - private static final String SHAPE_GROUP_TAG_NAME = "shapes";
179 -
180 - private String currentDir = "";
181 - private File configFile;
182 -
183 - private HashMap<String, CustomShapeDefinition> shapeDefinitions =
184 - new HashMap<String, CustomShapeDefinition>();
185 -
186 - public Collection<CustomShapeDefinition> getShapeDefinitions() {
187 - return shapeDefinitions.values();
188 - }
189 -
190 - public HashMap<String, CustomShapeDefinition> getShapeDefinitionsHash() {
191 - return shapeDefinitions;
192 - }
193 -
194 - public void parseFile(File file)
195 - throws ParserConfigurationException, SAXException, IOException
196 - {
197 - configFile = file;
198 -
199 - DefaultHandler handler = new XMLHandler();
200 -
201 - // Use the default (non-validating) parser
202 - SAXParserFactory factory = SAXParserFactory.newInstance();
203 -
204 - SAXParser saxParser = factory.newSAXParser();
205 - saxParser.parse(file, handler);
206 - }
207 -
208 - private class XMLHandler extends DefaultHandler{
209 - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
210 - if(qName.equals(SHAPE_GROUP_TAG_NAME)){
211 - if(attributes.getLength() == 1){
212 - currentDir = attributes.getValue(0).trim();
213 - if(currentDir.equals("")) currentDir = configFile.getParentFile().getAbsolutePath();
214 - } else {
215 - //the dir that contains the config file:
216 - currentDir = configFile.getParentFile().getAbsolutePath();
217 - }
218 - }
219 - if(qName.equals(SHAPE_TAG_NAME)){
220 - CustomShapeDefinition definition = new CustomShapeDefinition();
221 -
222 - int len = attributes.getLength();
223 - for(int i = 0; i < len; i++){
224 - String name = attributes.getQName(i);
225 - String value = attributes.getValue(i);
226 -
227 - if(name.equals("tag")){
228 - definition.setTag(value);
229 - } else if(name.equals("stretch")){
230 - definition.setStretches(getBooleanFromAttributeValue(value));
231 - } else if(name.equals("border")){
232 - definition.setHasBorder(getBooleanFromAttributeValue(value));
233 - } else if(name.equals("shadow")){
234 - definition.setDropsShadow(getBooleanFromAttributeValue(value));
235 - } else if(name.equals("comment")){
236 - definition.setComment(value);
237 - } else if(name.equals("filename")){
238 - File file = new File(value);
239 - if(file.isAbsolute()){
240 - definition.setFilename(value);
241 - } else { //relative to the location of the config file or to the group's base dir
242 - definition.setFilename(createFilename(currentDir, value));
243 - }
244 - }
245 - }
246 -
247 - if(shapeDefinitions.containsKey(definition.getTag())){
248 - CustomShapeDefinition oldDef = shapeDefinitions.get(definition.getTag());
249 - System.err.println(
250 - "*** Warning: shape \""+oldDef.getTag()+"\" (file: "
251 - +oldDef.getFilename()+") has been redefined as file: "
252 - +definition.getFilename()
253 - );
254 - }
255 -
256 - File file = new File(definition.getFilename());
257 - if(file.exists()){
258 - shapeDefinitions.put(definition.getTag(), definition);
259 - if(DEBUG) System.out.println(definition);
260 - } else {
261 - System.err.println("File "+file+" does not exist, skipping tag "+definition.getTag());
262 - }
263 -
264 - }
265 - if(qName.equals(INLCUDE_TAG_NAME)){
266 - if(attributes.getLength() == 1){
267 - File includedFile = new File(attributes.getValue(0).trim());
268 -
269 - if(!includedFile.isAbsolute()){
270 - includedFile = new File(
271 - createFilename(
272 - configFile.getParentFile().getAbsolutePath(),
273 - includedFile.getPath()));
274 - }
275 -
276 - if(!includedFile.exists()){
277 - System.err.println("Included file "+includedFile+" does not exist, skipping");
278 - return;
279 - }
280 -
281 - ConfigurationParser configParser = new ConfigurationParser();
282 - try {
283 - configParser.parseFile(includedFile);
284 - } catch (ParserConfigurationException e) {
285 - e.printStackTrace();
286 - } catch (SAXException e) {
287 - e.printStackTrace();
288 - } catch (IOException e) {
289 - e.printStackTrace();
290 - }
291 - HashMap<String, CustomShapeDefinition> shapes = configParser.getShapeDefinitionsHash();
292 - shapeDefinitions.putAll(shapes);
293 - }
294 - }
295 - }
296 - }
297 -
298 - private String createFilename(String baseDir, String filename){
299 - if(baseDir == null || baseDir.trim().equals("")){
300 - return filename;
301 - }
302 - if(baseDir.endsWith(File.separator)){
303 - return baseDir + filename;
304 - }
305 - return baseDir + File.separator + filename;
306 - }
307 -
308 - private boolean getBooleanFromAttributeValue(String value){
309 - value = value.toLowerCase();
310 - if("no".equals(value)) return false;
311 - if("false".equals(value)) return false;
312 - if("yes".equals(value)) return true;
313 - if("true".equals(value)) return true;
314 - throw new IllegalArgumentException("value "+value+" cannot be interpreted as a boolean");
315 - }
316 -
317 - public static void main(String argv[]) throws ParserConfigurationException, SAXException, IOException {
318 - ConfigurationParser parser = new ConfigurationParser();
319 - parser.parseFile(new File("config.xml"));
320 - parser.getShapeDefinitions();
321 - }
322 -
323 -
324 -
325 + private static final boolean DEBUG = false;
326 +
327 + private static final String INLCUDE_TAG_NAME = "include";
328 + private static final String SHAPE_TAG_NAME = "shape";
329 + private static final String SHAPE_GROUP_TAG_NAME = "shapes";
330 +
331 + private String currentDir = "";
332 + private File configFile;
333 +
334 + private HashMap<String, CustomShapeDefinition> shapeDefinitions = new HashMap<String, CustomShapeDefinition>();
335 +
336 + public Collection<CustomShapeDefinition> getShapeDefinitions() {
337 + return shapeDefinitions.values();
338 + }
339 +
340 + public HashMap<String, CustomShapeDefinition> getShapeDefinitionsHash() {
341 + return shapeDefinitions;
342 + }
343 +
344 + public void parseFile(File file) throws ParserConfigurationException,
345 + SAXException, IOException {
346 + configFile = file;
347 +
348 + DefaultHandler handler = new XMLHandler();
349 +
350 + // Use the default (non-validating) parser
351 + SAXParserFactory factory = SAXParserFactory.newInstance();
352 +
353 + SAXParser saxParser = factory.newSAXParser();
354 + saxParser.parse(file, handler);
355 + }
356 +
357 + private class XMLHandler extends DefaultHandler {
358 + public void startElement(String uri, String localName, String qName,
359 + Attributes attributes) throws SAXException {
360 + if (qName.equals(SHAPE_GROUP_TAG_NAME)) {
361 + if (attributes.getLength() == 1) {
362 + currentDir = attributes.getValue(0).trim();
363 + if (currentDir.equals(""))
364 + currentDir = configFile.getParentFile()
365 + .getAbsolutePath();
366 + } else {
367 + // the dir that contains the config file:
368 + currentDir = configFile.getParentFile().getAbsolutePath();
369 + }
370 + }
371 + if (qName.equals(SHAPE_TAG_NAME)) {
372 + CustomShapeDefinition definition = new CustomShapeDefinition();
373 +
374 + int len = attributes.getLength();
375 + for (int i = 0; i < len; i++) {
376 + String name = attributes.getQName(i);
377 + String value = attributes.getValue(i);
378 +
379 + if (name.equals("tag")) {
380 + definition.setTag(value);
381 + } else if (name.equals("stretch")) {
382 + definition
383 + .setStretches(getBooleanFromAttributeValue(value));
384 + } else if (name.equals("border")) {
385 + definition
386 + .setHasBorder(getBooleanFromAttributeValue(value));
387 + } else if (name.equals("shadow")) {
388 + definition
389 + .setDropsShadow(getBooleanFromAttributeValue(value));
390 + } else if (name.equals("comment")) {
391 + definition.setComment(value);
392 + } else if (name.equals("filename")) {
393 + File file = new File(value);
394 + if (file.isAbsolute()) {
395 + definition.setFilename(value);
396 + } else { // relative to the location of the config file
397 + // or to the group's base dir
398 + definition.setFilename(createFilename(currentDir,
399 + value));
400 + }
401 + }
402 + }
403 +
404 + if (shapeDefinitions.containsKey(definition.getTag())) {
405 + CustomShapeDefinition oldDef = shapeDefinitions
406 + .get(definition.getTag());
407 + System.err.println("*** Warning: shape \""
408 + + oldDef.getTag() + "\" (file: "
409 + + oldDef.getFilename()
410 + + ") has been redefined as file: "
411 + + definition.getFilename());
412 + }
413 +
414 + File file = new File(definition.getFilename());
415 + if (file.exists()) {
416 + shapeDefinitions.put(definition.getTag(), definition);
417 + if (DEBUG)
418 + System.out.println(definition);
419 + } else {
420 + System.err.println("File " + file
421 + + " does not exist, skipping tag "
422 + + definition.getTag());
423 + }
424 +
425 + }
426 + if (qName.equals(INLCUDE_TAG_NAME)) {
427 + if (attributes.getLength() == 1) {
428 + File includedFile = new File(attributes.getValue(0).trim());
429 +
430 + if (!includedFile.isAbsolute()) {
431 + includedFile = new File(createFilename(configFile
432 + .getParentFile().getAbsolutePath(),
433 + includedFile.getPath()));
434 + }
435 +
436 + if (!includedFile.exists()) {
437 + System.err.println("Included file " + includedFile
438 + + " does not exist, skipping");
439 + return;
440 + }
441 +
442 + ConfigurationParser configParser = new ConfigurationParser();
443 + try {
444 + configParser.parseFile(includedFile);
445 + } catch (ParserConfigurationException e) {
446 + e.printStackTrace();
447 + } catch (SAXException e) {
448 + e.printStackTrace();
449 + } catch (IOException e) {
450 + e.printStackTrace();
451 + }
452 + HashMap<String, CustomShapeDefinition> shapes = configParser
453 + .getShapeDefinitionsHash();
454 + shapeDefinitions.putAll(shapes);
455 + }
456 + }
457 + }
458 + }
459 +
460 + private String createFilename(String baseDir, String filename) {
461 + if (baseDir == null || baseDir.trim().equals("")) {
462 + return filename;
463 + }
464 + if (baseDir.endsWith(File.separator)) {
465 + return baseDir + filename;
466 + }
467 + return baseDir + File.separator + filename;
468 + }
469 +
470 + private boolean getBooleanFromAttributeValue(String value) {
471 + value = value.toLowerCase();
472 + if ("no".equals(value))
473 + return false;
474 + if ("false".equals(value))
475 + return false;
476 + if ("yes".equals(value))
477 + return true;
478 + if ("true".equals(value))
479 + return true;
480 + throw new IllegalArgumentException("value " + value
481 + + " cannot be interpreted as a boolean");
482 + }
483 +
484 + public static void main(String argv[]) throws ParserConfigurationException,
485 + SAXException, IOException {
486 + ConfigurationParser parser = new ConfigurationParser();
487 + parser.parseFile(new File("config.xml"));
488 + parser.getShapeDefinitions();
489 + }
490 +
491 }
492 diff --git a/src/org/stathissideris/ascii2image/core/ConversionOptions.java b/src/org/stathissideris/ascii2image/core/ConversionOptions.java
493 index 17df7a9..958f179 100644
494 --- a/src/org/stathissideris/ascii2image/core/ConversionOptions.java
495 +++ b/src/org/stathissideris/ascii2image/core/ConversionOptions.java
496 @@ -1,25 +1,35 @@
497 -/*
498 - * DiTAA - Diagrams Through Ascii Art
499 +/**
500 + * ditaa - Diagrams Through Ascii Art
501 *
502 - * Copyright (C) 2004 Efstathios Sideris
503 + * Copyright (C) 2004-2011 Efstathios Sideris
504 *
505 - * This program is free software; you can redistribute it and/or
506 - * modify it under the terms of the GNU General Public License
507 - * as published by the Free Software Foundation; either version 2
508 - * of the License, or (at your option) any later version.
509 + * ditaa is free software: you can redistribute it and/or modify
510 + * it under the terms of the GNU Lesser General Public License as
511 + * published by the Free Software Foundation, either version 3 of
512 + * the License, or (at your option) any later version.
513 *
514 - * This program is distributed in the hope that it will be useful,
515 + * ditaa is distributed in the hope that it will be useful,
516 * but WITHOUT ANY WARRANTY; without even the implied warranty of
517 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
518 - * GNU General Public License for more details.
519 + * GNU Lesser General Public License for more details.
520 *
521 - * You should have received a copy of the GNU General Public License
522 - * along with this program; if not, write to the Free Software
523 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
524 + * You should have received a copy of the GNU Lesser General Public
525 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
526 *
527 */
528 package org.stathissideris.ascii2image.core;
529
530 +//import org.apache.commons.cli.CommandLine;
531 +//import org.apache.commons.cli.Option;
532 +//import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
533 +//import org.xml.sax.SAXException;
534 +
535 +//import javax.xml.parsers.ParserConfigurationException;
536 +//import java.awt.*;
537 +//import java.io.File;
538 +//import java.io.IOException;
539 +//import java.io.UnsupportedEncodingException;
540 +//import java.util.HashMap;
541
542 /**
543 *
544 @@ -27,13 +37,13 @@ package org.stathissideris.ascii2image.core;
545 */
546 public class ConversionOptions {
547
548 -// public ProcessingOptions processingOptions =
549 -// new ProcessingOptions();
550 + public ProcessingOptions processingOptions =
551 + new ProcessingOptions();
552 public RenderingOptions renderingOptions =
553 new RenderingOptions();
554
555 public void setDebug(boolean value){
556 - // processingOptions.setPrintDebugOutput(value);
557 + processingOptions.setPrintDebugOutput(value);
558 renderingOptions.setRenderDebugLines(value);
559 }
560
561 @@ -42,7 +52,24 @@ public class ConversionOptions {
562 public void setDropShadows(boolean dropShadows) {
563 renderingOptions.setDropShadows(dropShadows);
564 }
565 -
566 +
567 +// /** Parse a color from a 6- or 8-digit hex string. For example, FF0000 is red.
568 +// * If eight digits, last two digits are alpha. */
569 +// public static Color parseColor(String hexString) {
570 +// if(hexString.length() == 6) {
571 +// return new Color(Integer.parseInt(hexString, 16));
572 +// } else if(hexString.length() == 8) {
573 +// return new Color(
574 +// Integer.parseInt(hexString.substring(0,2), 16),
575 +// Integer.parseInt(hexString.substring(2,4), 16),
576 +// Integer.parseInt(hexString.substring(4,6), 16),
577 +// Integer.parseInt(hexString.substring(6,8), 16)
578 +// );
579 +// } else {
580 +// throw new IllegalArgumentException("Cannot interpret \""+hexString+"\" as background colour. It needs to be a 6- or 8-digit hex number, depending on whether you have transparency or not (same as HTML).");
581 +// }
582 +// }
583 +//
584 // public ConversionOptions(CommandLine cmdLine) throws UnsupportedEncodingException{
585 //
586 // processingOptions.setVerbose(cmdLine.hasOption("verbose"));
587 @@ -58,8 +85,17 @@ public class ConversionOptions {
588 // processingOptions.setAllCornersAreRound(cmdLine.hasOption("round-corners"));
589 // processingOptions.setPerformSeparationOfCommonEdges(!cmdLine.hasOption("no-separation"));
590 // renderingOptions.setAntialias(!cmdLine.hasOption("no-antialias"));
591 +// renderingOptions.setFixedSlope(cmdLine.hasOption("fixed-slope"));
592 //
593 -//
594 +// if(cmdLine.hasOption("background")) {
595 +// String b = cmdLine.getOptionValue("background");
596 +// Color background = parseColor(b);
597 +// renderingOptions.setBackgroundColor(background);
598 +// }
599 +//
600 +// if(cmdLine.hasOption("transparent")) {
601 +// renderingOptions.setBackgroundColor(new Color(0,0,0,0));
602 +// }
603 //
604 // if(cmdLine.hasOption("tabs")){
605 // Integer tabSize = Integer.parseInt(cmdLine.getOptionValue("tabs"));
606 @@ -74,6 +110,14 @@ public class ConversionOptions {
607 // processingOptions.setCharacterEncoding(encoding);
608 // }
609 //
610 +// if (cmdLine.hasOption("svg")){
611 +// renderingOptions.setImageType(RenderingOptions.ImageType.SVG);
612 +// }
613 +//
614 +// if (cmdLine.hasOption("svg-font-url")){
615 +// renderingOptions.setFontURL(cmdLine.getOptionValue("svg-font-url"));
616 +// }
617 +//
618 // ConfigurationParser configParser = new ConfigurationParser();
619 // try {
620 // for (Option curOption : cmdLine.getOptions()) {
621 @@ -150,4 +194,4 @@ public class ConversionOptions {
622 // processingOptions.setColorCodesProcessingMode(ProcessingOptions.RENDER_COLOR_CODES);
623 // processingOptions.setTagProcessingMode(ProcessingOptions.RENDER_TAGS);
624 // }
625 -//}
626 \ No newline at end of file
627 +//}
628 diff --git a/src/org/stathissideris/ascii2image/core/DebugUtils.java b/src/org/stathissideris/ascii2image/core/DebugUtils.java
629 index f0e4376..62c9276 100644
630 --- a/src/org/stathissideris/ascii2image/core/DebugUtils.java
631 +++ b/src/org/stathissideris/ascii2image/core/DebugUtils.java
632 @@ -1,3 +1,22 @@
633 +/**
634 + * ditaa - Diagrams Through Ascii Art
635 + *
636 + * Copyright (C) 2004-2011 Efstathios Sideris
637 + *
638 + * ditaa is free software: you can redistribute it and/or modify
639 + * it under the terms of the GNU Lesser General Public License as
640 + * published by the Free Software Foundation, either version 3 of
641 + * the License, or (at your option) any later version.
642 + *
643 + * ditaa is distributed in the hope that it will be useful,
644 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
645 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
646 + * GNU Lesser General Public License for more details.
647 + *
648 + * You should have received a copy of the GNU Lesser General Public
649 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
650 + *
651 + */
652 package org.stathissideris.ascii2image.core;
653
654 public class DebugUtils {
655 diff --git a/src/org/stathissideris/ascii2image/core/DocBookConverter.java b/src/org/stathissideris/ascii2image/core/DocBookConverter.java
656 deleted file mode 100644
657 index dfbf796..0000000
658 --- a/src/org/stathissideris/ascii2image/core/DocBookConverter.java
659 +++ /dev/null
660 @@ -1,70 +0,0 @@
661 -/*
662 - * DiTAA - Diagrams Through Ascii Art
663 - *
664 - * Copyright (C) 2004 Efstathios Sideris
665 - *
666 - * This program is free software; you can redistribute it and/or
667 - * modify it under the terms of the GNU General Public License
668 - * as published by the Free Software Foundation; either version 2
669 - * of the License, or (at your option) any later version.
670 - *
671 - * This program is distributed in the hope that it will be useful,
672 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
673 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
674 - * GNU General Public License for more details.
675 - *
676 - * You should have received a copy of the GNU General Public License
677 - * along with this program; if not, write to the Free Software
678 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
679 - *
680 - */
681 -package org.stathissideris.ascii2image.core;
682 -
683 -import org.xml.sax.Attributes;
684 -import org.xml.sax.SAXException;
685 -import org.xml.sax.XMLReader;
686 -import org.xml.sax.helpers.DefaultHandler;
687 -import org.xml.sax.helpers.XMLReaderFactory;
688 -
689 -// using SAX
690 -public class DocBookConverter {
691 -
692 - class HowToHandler extends DefaultHandler {
693 - boolean title = false;
694 - boolean url = false;
695 -
696 - public void startElement(
697 - String nsURI,
698 - String strippedName,
699 - String tagName,
700 - Attributes attributes)
701 - throws SAXException {
702 - if (tagName.equalsIgnoreCase("title"))
703 - title = true;
704 - if (tagName.equalsIgnoreCase("url"))
705 - url = true;
706 - }
707 -
708 - public void characters(char[] ch, int start, int length) {
709 - if (title) {
710 - System.out.println("Title: " + new String(ch, start, length));
711 - title = false;
712 - } else if (url) {
713 - System.out.println("Url: " + new String(ch, start,length));
714 - url = false;
715 - }
716 - }
717 - }
718 -
719 - public void list( ) throws Exception {
720 - XMLReader parser =
721 - XMLReaderFactory.createXMLReader
722 - ("org.apache.crimson.parser.XMLReaderImpl");
723 - parser.setContentHandler(new HowToHandler( ));
724 - parser.parse("howto.xml");
725 - }
726 -
727 - public static void main(String[] args) throws Exception {
728 - new DocBookConverter().list( );
729 - }
730 -}
731 diff --git a/src/org/stathissideris/ascii2image/core/FileUtils.java b/src/org/stathissideris/ascii2image/core/FileUtils.java
732 index 9763453..4570a53 100644
733 --- a/src/org/stathissideris/ascii2image/core/FileUtils.java
734 +++ b/src/org/stathissideris/ascii2image/core/FileUtils.java
735 @@ -1,29 +1,31 @@
736 -/*
737 - * DiTAA - Diagrams Through Ascii Art
738 +/**
739 + * ditaa - Diagrams Through Ascii Art
740 *
741 - * Copyright (C) 2004 Efstathios Sideris
742 + * Copyright (C) 2004-2011 Efstathios Sideris
743 *
744 - * This program is free software; you can redistribute it and/or
745 - * modify it under the terms of the GNU General Public License
746 - * as published by the Free Software Foundation; either version 2
747 - * of the License, or (at your option) any later version.
748 + * ditaa is free software: you can redistribute it and/or modify
749 + * it under the terms of the GNU Lesser General Public License as
750 + * published by the Free Software Foundation, either version 3 of
751 + * the License, or (at your option) any later version.
752 *
753 - * This program is distributed in the hope that it will be useful,
754 + * ditaa is distributed in the hope that it will be useful,
755 * but WITHOUT ANY WARRANTY; without even the implied warranty of
756 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
757 - * GNU General Public License for more details.
758 + * GNU Lesser General Public License for more details.
759 *
760 - * You should have received a copy of the GNU General Public License
761 - * along with this program; if not, write to the Free Software
762 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
763 + * You should have received a copy of the GNU Lesser General Public
764 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
765 *
766 */
767 package org.stathissideris.ascii2image.core;
768
769 import java.io.File;
770 import java.io.FileInputStream;
771 +import java.io.FileNotFoundException;
772 import java.io.IOException;
773 import java.io.InputStream;
774 +import java.io.InputStreamReader;
775 +import java.io.LineNumberReader;
776
777 /**
778 *
779 @@ -81,37 +83,58 @@ public class FileUtils {
780 }
781
782 public static String readFile(File file, String encoding) throws IOException {
783 - InputStream is = new FileInputStream(file);
784 long length = file.length();
785
786 if (length > Integer.MAX_VALUE) {
787 // File is too large
788 // TODO: we need some feedback for the case of the file being too large
789 }
790 +
791 + return readFile(new FileInputStream(file), file.getName(), encoding, length);
792 + }
793 +
794 + public static String readFile(InputStream is, String name, String encoding) throws IOException {
795 + return readFile(is, name, encoding, -1);
796 + }
797 +
798 + public static String readFile(InputStream is, String name, String encoding, long length) throws IOException {
799 +
800 + if (length < 0) {
801 + LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
802 + StringBuilder builder = new StringBuilder();
803 + while (true) {
804 + String line = reader.readLine();
805 + if (line == null) break;
806 + else builder.append(line).append("\n");
807 + }
808 + return builder.toString();
809 + }
810 +
811 + else {
812 + // Create the byte array to hold the data
813 + byte[] bytes = new byte[(int)length];
814
815 - // Create the byte array to hold the data
816 - byte[] bytes = new byte[(int)length];
817 -
818 - // Read in the bytes
819 - int offset = 0;
820 - int numRead = 0;
821 - while (offset < bytes.length
822 - && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
823 - offset += numRead;
824 - }
825 + // Read in the bytes
826 + int offset = 0;
827 + int numRead = 0;
828 + while (offset < bytes.length
829 + && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
830 + offset += numRead;
831 + }
832
833 - // Ensure all the bytes have been read in
834 - if (offset < bytes.length) {
835 - throw new IOException("Could not completely read file "+file.getName());
836 - }
837 + // Ensure all the bytes have been read in
838 + if (offset < bytes.length) {
839 + throw new IOException("Could not completely read file "+name);
840 + }
841
842 - // Close the input stream and return bytes
843 - is.close();
844 - if(encoding == null){
845 + // Close the input stream and return bytes
846 + is.close();
847 + if(encoding == null){
848 return new String(bytes);
849 - } else {
850 + } else {
851 return new String(bytes, encoding);
852 - }
853 + }
854 + }
855 }
856
857 public static void main(String[] args){
858 diff --git a/src/org/stathissideris/ascii2image/core/Pair.java b/src/org/stathissideris/ascii2image/core/Pair.java
859 index 3d58163..0468633 100644
860 --- a/src/org/stathissideris/ascii2image/core/Pair.java
861 +++ b/src/org/stathissideris/ascii2image/core/Pair.java
862 @@ -1,3 +1,22 @@
863 +/**
864 + * ditaa - Diagrams Through Ascii Art
865 + *
866 + * Copyright (C) 2004-2011 Efstathios Sideris
867 + *
868 + * ditaa is free software: you can redistribute it and/or modify
869 + * it under the terms of the GNU Lesser General Public License as
870 + * published by the Free Software Foundation, either version 3 of
871 + * the License, or (at your option) any later version.
872 + *
873 + * ditaa is distributed in the hope that it will be useful,
874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
876 + * GNU Lesser General Public License for more details.
877 + *
878 + * You should have received a copy of the GNU Lesser General Public
879 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
880 + *
881 + */
882 package org.stathissideris.ascii2image.core;
883
884 public class Pair<T,K> {
885 diff --git a/src/org/stathissideris/ascii2image/core/PerformanceTester.java b/src/org/stathissideris/ascii2image/core/PerformanceTester.java
886 new file mode 100644
887 index 0000000..fbeb8ad
888 --- /dev/null
889 +++ b/src/org/stathissideris/ascii2image/core/PerformanceTester.java
890 @@ -0,0 +1,64 @@
891 +/**
892 + * ditaa - Diagrams Through Ascii Art
893 + *
894 + * Copyright (C) 2004-2011 Efstathios Sideris
895 + *
896 + * ditaa is free software: you can redistribute it and/or modify
897 + * it under the terms of the GNU Lesser General Public License as
898 + * published by the Free Software Foundation, either version 3 of
899 + * the License, or (at your option) any later version.
900 + *
901 + * ditaa is distributed in the hope that it will be useful,
902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
904 + * GNU Lesser General Public License for more details.
905 + *
906 + * You should have received a copy of the GNU Lesser General Public
907 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
908 + *
909 + */
910 +package org.stathissideris.ascii2image.core;
911 +
912 +import java.io.FileNotFoundException;
913 +import java.io.IOException;
914 +
915 +import org.stathissideris.ascii2image.graphics.Diagram;
916 +import org.stathissideris.ascii2image.text.TextGrid;
917 +
918 +/**
919 + *
920 + * @author Efstathios Sideris
921 + */
922 +public class PerformanceTester {
923 +
924 + public static void main(String[] args){
925 +
926 + String inputFilename = "tests/text/ditaa_bug.txt";
927 + ConversionOptions options = new ConversionOptions();
928 +
929 + int iterations = 30;
930 +
931 + try {
932 + long a = java.lang.System.currentTimeMillis();
933 +
934 + for(int i = 0; i < iterations; i++) {
935 + System.out.println("iteration "+i);
936 +
937 + TextGrid grid = new TextGrid();
938 + grid.loadFrom(inputFilename);
939 + new Diagram(grid, options);
940 + }
941 +
942 + long b = java.lang.System.currentTimeMillis();
943 +
944 + System.out.println((b-a) + "msec for " + iterations + " iterations on "+inputFilename);
945 +
946 + } catch (FileNotFoundException e) {
947 + e.printStackTrace();
948 + } catch (IOException e) {
949 + e.printStackTrace();
950 + }
951 +
952 + System.out.println("Tests completed");
953 + }
954 +}
955 diff --git a/src/org/stathissideris/ascii2image/core/ProcessingOptions.java b/src/org/stathissideris/ascii2image/core/ProcessingOptions.java
956 index c0f4219..1833e9f 100644
957 --- a/src/org/stathissideris/ascii2image/core/ProcessingOptions.java
958 +++ b/src/org/stathissideris/ascii2image/core/ProcessingOptions.java
959 @@ -1,21 +1,20 @@
960 -/*
961 - * DiTAA - Diagrams Through Ascii Art
962 +/**
963 + * ditaa - Diagrams Through Ascii Art
964 *
965 - * Copyright (C) 2004 Efstathios Sideris
966 + * Copyright (C) 2004-2011 Efstathios Sideris
967 *
968 - * This program is free software; you can redistribute it and/or
969 - * modify it under the terms of the GNU General Public License
970 - * as published by the Free Software Foundation; either version 2
971 - * of the License, or (at your option) any later version.
972 + * ditaa is free software: you can redistribute it and/or modify
973 + * it under the terms of the GNU Lesser General Public License as
974 + * published by the Free Software Foundation, either version 3 of
975 + * the License, or (at your option) any later version.
976 *
977 - * This program is distributed in the hope that it will be useful,
978 + * ditaa is distributed in the hope that it will be useful,
979 * but WITHOUT ANY WARRANTY; without even the implied warranty of
980 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
981 - * GNU General Public License for more details.
982 + * GNU Lesser General Public License for more details.
983 *
984 - * You should have received a copy of the GNU General Public License
985 - * along with this program; if not, write to the Free Software
986 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
987 + * You should have received a copy of the GNU Lesser General Public
988 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
989 *
990 */
991 package org.stathissideris.ascii2image.core;
992 @@ -37,7 +36,7 @@ public class ProcessingOptions {
993 private boolean overwriteFiles = false;
994 private boolean performSeparationOfCommonEdges = true;
995 private boolean allCornersAreRound = false;
996 -
997 +
998 public static final int USE_TAGS = 0;
999 public static final int RENDER_TAGS = 1;
1000 public static final int IGNORE_TAGS = 2;
1001 @@ -89,7 +88,7 @@ public class ProcessingOptions {
1002 return performSeparationOfCommonEdges;
1003 }
1004
1005 - /**
1006 + /**
1007 * @return
1008 */
1009 public int getTagProcessingMode() {
1010 @@ -124,7 +123,7 @@ public class ProcessingOptions {
1011 performSeparationOfCommonEdges = b;
1012 }
1013
1014 - /**
1015 + /**
1016 * @param i
1017 */
1018 public void setTagProcessingMode(int i) {
1019 diff --git a/src/org/stathissideris/ascii2image/core/RenderingOptions.java b/src/org/stathissideris/ascii2image/core/RenderingOptions.java
1020 index 8515143..02af946 100644
1021 --- a/src/org/stathissideris/ascii2image/core/RenderingOptions.java
1022 +++ b/src/org/stathissideris/ascii2image/core/RenderingOptions.java
1023 @@ -1,25 +1,25 @@
1024 -/*
1025 - * DiTAA - Diagrams Through Ascii Art
1026 +/**
1027 + * ditaa - Diagrams Through Ascii Art
1028 *
1029 - * Copyright (C) 2004 Efstathios Sideris
1030 + * Copyright (C) 2004-2011 Efstathios Sideris
1031 *
1032 - * This program is free software; you can redistribute it and/or
1033 - * modify it under the terms of the GNU General Public License
1034 - * as published by the Free Software Foundation; either version 2
1035 - * of the License, or (at your option) any later version.
1036 + * ditaa is free software: you can redistribute it and/or modify
1037 + * it under the terms of the GNU Lesser General Public License as
1038 + * published by the Free Software Foundation, either version 3 of
1039 + * the License, or (at your option) any later version.
1040 *
1041 - * This program is distributed in the hope that it will be useful,
1042 + * ditaa is distributed in the hope that it will be useful,
1043 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1044 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1045 - * GNU General Public License for more details.
1046 + * GNU Lesser General Public License for more details.
1047 *
1048 - * You should have received a copy of the GNU General Public License
1049 - * along with this program; if not, write to the Free Software
1050 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1051 + * You should have received a copy of the GNU Lesser General Public
1052 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1053 *
1054 */
1055 package org.stathissideris.ascii2image.core;
1056
1057 +import java.awt.Color;
1058 import java.util.HashMap;
1059
1060 import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
1061 @@ -35,82 +35,98 @@ public class RenderingOptions {
1062 private boolean dropShadows = true;
1063 private boolean renderDebugLines = false;
1064 private boolean antialias = true;
1065 + private boolean fixedSlope = false;
1066
1067 private int cellWidth = 10;
1068 private int cellHeight = 14;
1069
1070 private float scale = 1;
1071 +
1072 + private Color backgroundColor = Color.white;
1073 +
1074 + public enum ImageType { PNG, SVG };
1075 +
1076 + private ImageType imageType = ImageType.PNG;
1077 +
1078 + public ImageType getImageType() { return imageType; }
1079 + public void setImageType(ImageType type) { imageType = type; }
1080 +
1081 + private String fontFamily = "Courier";
1082 + private String fontURL = null;
1083 +
1084 + public String getFontFamily() { return fontFamily; }
1085 + public String getFontURL() { return fontURL; }
1086 + public void setFontURL(String url) { fontFamily = "Custom"; fontURL = url; }
1087
1088 - /**
1089 - * @return
1090 - */
1091 public int getCellHeight() {
1092 return cellHeight;
1093 }
1094
1095 - /**
1096 - * @return
1097 - */
1098 public int getCellWidth() {
1099 return cellWidth;
1100 }
1101
1102 - /**
1103 - * @return
1104 - */
1105 public boolean dropShadows() {
1106 return dropShadows;
1107 }
1108
1109 - /**
1110 - * @return
1111 - */
1112 public boolean renderDebugLines() {
1113 return renderDebugLines;
1114 }
1115
1116 - /**
1117 - * @return
1118 - */
1119 public float getScale() {
1120 return scale;
1121 }
1122
1123 - /**
1124 - * @param b
1125 - */
1126 public void setDropShadows(boolean b) {
1127 dropShadows = b;
1128 }
1129
1130 - /**
1131 - * @param b
1132 - */
1133 public void setRenderDebugLines(boolean b) {
1134 renderDebugLines = b;
1135 }
1136
1137 - /**
1138 - * @param f
1139 - */
1140 public void setScale(float f) {
1141 scale = f;
1142 cellWidth *= scale;
1143 cellHeight *= scale;
1144 }
1145
1146 - /**
1147 - * @return
1148 - */
1149 public boolean performAntialias() {
1150 return antialias;
1151 }
1152
1153 - /**
1154 - * @param b
1155 - */
1156 public void setAntialias(boolean b) {
1157 antialias = b;
1158 }
1159
1160 + public Color getBackgroundColor() {
1161 + return backgroundColor;
1162 + }
1163 +
1164 + public void setBackgroundColor(Color backgroundColor) {
1165 + this.backgroundColor = backgroundColor;
1166 + }
1167 +
1168 + public boolean needsTransparency() {
1169 + return backgroundColor.getAlpha() < 255;
1170 + }
1171 +
1172 + /**
1173 + * Should the sides of trapezoids and parallelograms have fixed width (false, default)
1174 + * or fixed slope (true)?
1175 + * @return true for fixed slope, false for fixed width
1176 + */
1177 + public boolean isFixedSlope() {
1178 + return fixedSlope;
1179 + }
1180 +
1181 + /**
1182 + * Should the sides of trapezoids and parallelograms have fixed width (false, default)
1183 + * or fixed slope (true)?
1184 + * @param b true for fixed slope, false for fixed width
1185 + */
1186 + public void setFixedSlope(boolean b) {
1187 + this.fixedSlope = b;
1188 + }
1189 }
1190 diff --git a/src/org/stathissideris/ascii2image/core/Shape3DOrderingComparator.java b/src/org/stathissideris/ascii2image/core/Shape3DOrderingComparator.java
1191 index 937ea07..f78e9c2 100644
1192 --- a/src/org/stathissideris/ascii2image/core/Shape3DOrderingComparator.java
1193 +++ b/src/org/stathissideris/ascii2image/core/Shape3DOrderingComparator.java
1194 @@ -1,21 +1,20 @@
1195 -/*
1196 - * DiTAA - Diagrams Through Ascii Art
1197 +/**
1198 + * ditaa - Diagrams Through Ascii Art
1199 *
1200 - * Copyright (C) 2004 Efstathios Sideris
1201 + * Copyright (C) 2004-2011 Efstathios Sideris
1202 *
1203 - * This program is free software; you can redistribute it and/or
1204 - * modify it under the terms of the GNU General Public License
1205 - * as published by the Free Software Foundation; either version 2
1206 - * of the License, or (at your option) any later version.
1207 + * ditaa is free software: you can redistribute it and/or modify
1208 + * it under the terms of the GNU Lesser General Public License as
1209 + * published by the Free Software Foundation, either version 3 of
1210 + * the License, or (at your option) any later version.
1211 *
1212 - * This program is distributed in the hope that it will be useful,
1213 + * ditaa is distributed in the hope that it will be useful,
1214 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1215 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1216 - * GNU General Public License for more details.
1217 + * GNU Lesser General Public License for more details.
1218 *
1219 - * You should have received a copy of the GNU General Public License
1220 - * along with this program; if not, write to the Free Software
1221 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1222 + * You should have received a copy of the GNU Lesser General Public
1223 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1224 *
1225 */
1226 package org.stathissideris.ascii2image.core;
1227 @@ -28,20 +27,13 @@ import org.stathissideris.ascii2image.graphics.DiagramShape;
1228 *
1229 * @author Efstathios Sideris
1230 */
1231 -public class Shape3DOrderingComparator implements Comparator {
1232 +public class Shape3DOrderingComparator implements Comparator<DiagramShape> {
1233
1234 /**
1235 * Puts diagram shapes in pseudo-3d order starting from back to front
1236 *
1237 */
1238 - public int compare(Object object1, Object object2) {
1239 - if(!(object1 instanceof DiagramShape)
1240 - || !(object2 instanceof DiagramShape))
1241 - throw new RuntimeException("This comparator can only compare DiagramShapeS");
1242 -
1243 - DiagramShape shape1 = (DiagramShape) object1;
1244 - DiagramShape shape2 = (DiagramShape) object2;
1245 -
1246 + public int compare(DiagramShape shape1, DiagramShape shape2) {
1247 double y1 = shape1.makeIntoPath().getBounds().getCenterY();
1248 double y2 = shape2.makeIntoPath().getBounds().getCenterY();
1249
1250 diff --git a/src/org/stathissideris/ascii2image/core/ShapeAreaComparator.java b/src/org/stathissideris/ascii2image/core/ShapeAreaComparator.java
1251 new file mode 100644
1252 index 0000000..162ca59
1253 --- /dev/null
1254 +++ b/src/org/stathissideris/ascii2image/core/ShapeAreaComparator.java
1255 @@ -0,0 +1,46 @@
1256 +/**
1257 + * ditaa - Diagrams Through Ascii Art
1258 + *
1259 + * Copyright (C) 2004-2011 Efstathios Sideris
1260 + *
1261 + * ditaa is free software: you can redistribute it and/or modify
1262 + * it under the terms of the GNU Lesser General Public License as
1263 + * published by the Free Software Foundation, either version 3 of
1264 + * the License, or (at your option) any later version.
1265 + *
1266 + * ditaa is distributed in the hope that it will be useful,
1267 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1268 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1269 + * GNU Lesser General Public License for more details.
1270 + *
1271 + * You should have received a copy of the GNU Lesser General Public
1272 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1273 + *
1274 + */
1275 +package org.stathissideris.ascii2image.core;
1276 +
1277 +import java.util.Comparator;
1278 +
1279 +import org.stathissideris.ascii2image.graphics.DiagramShape;
1280 +
1281 +/**
1282 + *
1283 + * @author Efstathios Sideris
1284 + */
1285 +public class ShapeAreaComparator implements Comparator<DiagramShape> {
1286 +
1287 + /**
1288 + * Puts diagram shapes in order or area starting from largest to smallest
1289 + *
1290 + */
1291 + public int compare(DiagramShape shape1, DiagramShape shape2) {
1292 + double y1 = shape1.calculateArea();
1293 + double y2 = shape2.calculateArea();
1294 +
1295 + if(y1 > y2) return -1;
1296 + if(y1 < y2) return 1;
1297 +
1298 + return 0;
1299 + }
1300 +
1301 +}
1302 diff --git a/src/org/stathissideris/ascii2image/graphics/BitmapRenderer.java b/src/org/stathissideris/ascii2image/graphics/BitmapRenderer.java
1303 index 5ac634a..40d1bd3 100644
1304 --- a/src/org/stathissideris/ascii2image/graphics/BitmapRenderer.java
1305 +++ b/src/org/stathissideris/ascii2image/graphics/BitmapRenderer.java
1306 @@ -1,21 +1,20 @@
1307 -/*
1308 - * DiTAA - Diagrams Through Ascii Art
1309 +/**
1310 + * ditaa - Diagrams Through Ascii Art
1311 *
1312 - * Copyright (C) 2004 Efstathios Sideris
1313 + * Copyright (C) 2004-2011 Efstathios Sideris
1314 *
1315 - * This program is free software; you can redistribute it and/or
1316 - * modify it under the terms of the GNU General Public License
1317 - * as published by the Free Software Foundation; either version 2
1318 - * of the License, or (at your option) any later version.
1319 + * ditaa is free software: you can redistribute it and/or modify
1320 + * it under the terms of the GNU Lesser General Public License as
1321 + * published by the Free Software Foundation, either version 3 of
1322 + * the License, or (at your option) any later version.
1323 *
1324 - * This program is distributed in the hope that it will be useful,
1325 + * ditaa is distributed in the hope that it will be useful,
1326 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1327 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1328 - * GNU General Public License for more details.
1329 + * GNU Lesser General Public License for more details.
1330 *
1331 - * You should have received a copy of the GNU General Public License
1332 - * along with this program; if not, write to the Free Software
1333 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1334 + * You should have received a copy of the GNU Lesser General Public
1335 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1336 *
1337 */
1338 package org.stathissideris.ascii2image.graphics;
1339 @@ -43,8 +42,11 @@ import java.util.Iterator;
1340
1341 import javax.imageio.ImageIO;
1342
1343 +import org.stathissideris.ascii2image.core.ConversionOptions;
1344 import org.stathissideris.ascii2image.core.RenderingOptions;
1345 import org.stathissideris.ascii2image.core.Shape3DOrderingComparator;
1346 +import org.stathissideris.ascii2image.core.ShapeAreaComparator;
1347 +import org.stathissideris.ascii2image.text.TextGrid;
1348
1349 /**
1350 *
1351 @@ -53,34 +55,35 @@ import org.stathissideris.ascii2image.core.Shape3DOrderingComparator;
1352 public class BitmapRenderer {
1353
1354 private static final boolean DEBUG = false;
1355 + private static final boolean DEBUG_LINES = false;
1356
1357 private static final String IDREGEX = "^.+_vfill$";
1358
1359 Stroke normalStroke;
1360 Stroke dashStroke;
1361
1362 -// public static void main(String[] args) throws Exception {
1363 -//
1364 -//
1365 -// long startTime = System.currentTimeMillis();
1366 -//
1367 -// ConversionOptions options = new ConversionOptions();
1368 -//
1369 -// TextGrid grid = new TextGrid();
1370 -//
1371 -// String filename = "dak_orgstruktur_vs_be.ditaa.OutOfMemoryError.edit.txt";
1372 -//
1373 -// grid.loadFrom("tests/text/"+filename);
1374 -//
1375 -// Diagram diagram = new Diagram(grid, options);
1376 -// new BitmapRenderer().renderToPNG(diagram, "tests/images/"+filename+".png", options.renderingOptions);
1377 -// long endTime = System.currentTimeMillis();
1378 -// long totalTime = (endTime - startTime) / 1000;
1379 -// System.out.println("Done in "+totalTime+"sec");
1380 -//
1381 -// File workDir = new File("tests/images");
1382 -// //Process p = Runtime.getRuntime().exec("display "+filename+".png", null, workDir);
1383 -// }
1384 + public static void main(String[] args) throws Exception {
1385 +
1386 +
1387 + long startTime = System.currentTimeMillis();
1388 +
1389 + ConversionOptions options = new ConversionOptions();
1390 +
1391 + TextGrid grid = new TextGrid();
1392 +
1393 + String filename = "bug18.txt";
1394 +
1395 + grid.loadFrom("tests/text/"+filename);
1396 +
1397 + Diagram diagram = new Diagram(grid, options);
1398 + new BitmapRenderer().renderToPNG(diagram, "tests/images/"+filename+".png", options.renderingOptions);
1399 + long endTime = System.currentTimeMillis();
1400 + long totalTime = (endTime - startTime) / 1000;
1401 + System.out.println("Done in "+totalTime+"sec");
1402 +
1403 + File workDir = new File("tests/images");
1404 + //Process p = Runtime.getRuntime().exec("display "+filename+".png", null, workDir);
1405 + }
1406
1407 private boolean renderToPNG(Diagram diagram, String filename, RenderingOptions options){
1408 RenderedImage image = renderToImage(diagram, options);
1409 @@ -96,11 +99,19 @@ public class BitmapRenderer {
1410 return true;
1411 }
1412
1413 - public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){
1414 - BufferedImage image = new BufferedImage(
1415 + public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){
1416 + BufferedImage image;
1417 + if(options.needsTransparency()) {
1418 + image = new BufferedImage(
1419 + diagram.getWidth(),
1420 + diagram.getHeight(),
1421 + BufferedImage.TYPE_INT_ARGB);
1422 + } else {
1423 + image = new BufferedImage(
1424 diagram.getWidth(),
1425 diagram.getHeight(),
1426 BufferedImage.TYPE_INT_RGB);
1427 + }
1428
1429 return render(diagram, image, options);
1430 }
1431 @@ -115,7 +126,7 @@ public class BitmapRenderer {
1432
1433 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
1434
1435 - g2.setColor(Color.white);
1436 + g2.setColor(options.getBackgroundColor());
1437 //TODO: find out why the next line does not work
1438 g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10);
1439 /*for(int y = 0; y < diagram.getHeight(); y ++)
1440 @@ -123,22 +134,22 @@ public class BitmapRenderer {
1441
1442 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
1443
1444 - ArrayList shapes = diagram.getAllDiagramShapes();
1445 + ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes();
1446
1447 if(DEBUG) System.out.println("Rendering "+shapes.size()+" shapes (groups flattened)");
1448
1449 - Iterator shapesIt;
1450 + Iterator<DiagramShape> shapesIt;
1451 if(options.dropShadows()){
1452 //render shadows
1453 shapesIt = shapes.iterator();
1454 while(shapesIt.hasNext()){
1455 - DiagramShape shape = (DiagramShape) shapesIt.next();
1456 + DiagramShape shape = shapesIt.next();
1457
1458 if(shape.getPoints().isEmpty()) continue;
1459
1460 //GeneralPath path = shape.makeIntoPath();
1461 GeneralPath path;
1462 - path = shape.makeIntoRenderPath(diagram);
1463 + path = shape.makeIntoRenderPath(diagram, options);
1464
1465 float offset = diagram.getMinimumOfCellDimension() / 3.333f;
1466
1467 @@ -217,7 +228,7 @@ public class BitmapRenderer {
1468
1469
1470 //find storage shapes
1471 - ArrayList storageShapes = new ArrayList();
1472 + ArrayList<DiagramShape> storageShapes = new ArrayList<DiagramShape>();
1473 shapesIt = shapes.iterator();
1474 while(shapesIt.hasNext()){
1475 DiagramShape shape = (DiagramShape) shapesIt.next();
1476 @@ -242,7 +253,7 @@ public class BitmapRenderer {
1477 DiagramShape shape = (DiagramShape) shapesIt.next();
1478
1479 GeneralPath path;
1480 - path = shape.makeIntoRenderPath(diagram);
1481 + path = shape.makeIntoRenderPath(diagram, options);
1482
1483 if(!shape.isStrokeDashed()) {
1484 if(shape.getFillColor() != null)
1485 @@ -260,9 +271,11 @@ public class BitmapRenderer {
1486 g2.draw(path);
1487 }
1488
1489 -
1490 + //sort so that the largest shapes are rendered first
1491 + Collections.sort(shapes, new ShapeAreaComparator());
1492 +
1493 //render the rest of the shapes
1494 - ArrayList pointMarkers = new ArrayList();
1495 + ArrayList<DiagramShape> pointMarkers = new ArrayList<DiagramShape>();
1496 shapesIt = shapes.iterator();
1497 while(shapesIt.hasNext()){
1498 DiagramShape shape = (DiagramShape) shapesIt.next();
1499 @@ -283,7 +296,7 @@ public class BitmapRenderer {
1500 int size = shape.getPoints().size();
1501
1502 GeneralPath path;
1503 - path = shape.makeIntoRenderPath(diagram);
1504 + path = shape.makeIntoRenderPath(diagram, options);
1505
1506 //fill
1507 if(path != null && shape.isClosed() && !shape.isStrokeDashed()){
1508 @@ -314,7 +327,7 @@ public class BitmapRenderer {
1509 //if(shape.getType() != DiagramShape.TYPE_POINT_MARKER) continue;
1510
1511 GeneralPath path;
1512 - path = shape.makeIntoRenderPath(diagram);
1513 + path = shape.makeIntoRenderPath(diagram, options);
1514
1515 g2.setColor(Color.white);
1516 g2.fill(path);
1517 @@ -326,9 +339,9 @@ public class BitmapRenderer {
1518 //g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
1519 //renderTextLayer(diagram.getTextObjects().iterator());
1520
1521 - Iterator textIt = diagram.getTextObjects().iterator();
1522 + Iterator<DiagramText> textIt = diagram.getTextObjects().iterator();
1523 while(textIt.hasNext()){
1524 - DiagramText text = (DiagramText) textIt.next();
1525 + DiagramText text = textIt.next();
1526 g2.setFont(text.getFont());
1527 if(text.hasOutline()){
1528 g2.setColor(text.getOutlineColor());
1529 @@ -341,7 +354,7 @@ public class BitmapRenderer {
1530 g2.drawString(text.getText(), text.getXPos(), text.getYPos());
1531 }
1532
1533 - if(options.renderDebugLines() || DEBUG){
1534 + if(options.renderDebugLines() || DEBUG_LINES){
1535 Stroke debugStroke =
1536 new BasicStroke(
1537 1,
1538 @@ -363,7 +376,7 @@ public class BitmapRenderer {
1539 return renderedImage;
1540 }
1541
1542 - private RenderedImage renderTextLayer(ArrayList textObjects, int width, int height){
1543 + private RenderedImage renderTextLayer(ArrayList<DiagramText> textObjects, int width, int height){
1544 TextCanvas canvas = new TextCanvas(textObjects);
1545 Image image = canvas.createImage(width, height);
1546 Graphics g = image.getGraphics();
1547 @@ -372,15 +385,15 @@ public class BitmapRenderer {
1548 }
1549
1550 private class TextCanvas extends Canvas {
1551 - ArrayList textObjects;
1552 + ArrayList<DiagramText> textObjects;
1553
1554 - public TextCanvas(ArrayList textObjects){
1555 + public TextCanvas(ArrayList<DiagramText> textObjects){
1556 this.textObjects = textObjects;
1557 }
1558
1559 public void paint(Graphics g){
1560 Graphics g2 = (Graphics2D) g;
1561 - Iterator textIt = textObjects.iterator();
1562 + Iterator<DiagramText> textIt = textObjects.iterator();
1563 while(textIt.hasNext()){
1564 DiagramText text = (DiagramText) textIt.next();
1565 g2.setFont(text.getFont());
1566 @@ -416,32 +429,31 @@ public class BitmapRenderer {
1567 // g2.drawRect(bounds.x, bounds.y, bounds.width, bounds.height); //looks different!
1568 }
1569
1570 - //TODO: custom shape distintion relies on filename extension. Make this more intelligent
1571 + //TODO: custom shape distinction relies on filename extension. Make this more intelligent
1572 if(definition.getFilename().endsWith(".png")){
1573 renderCustomPNGShape(shape, g2);
1574 } else if(definition.getFilename().endsWith(".svg")){
1575 - // renderCustomSVGShape(shape, g2);
1576 - throw new UnsupportedOperationException();
1577 + renderCustomSVGShape(shape, g2);
1578 }
1579 }
1580
1581 -// private void renderCustomSVGShape(DiagramShape shape, Graphics2D g2){
1582 -// CustomShapeDefinition definition = shape.getDefinition();
1583 -// Rectangle bounds = shape.getBounds();
1584 -// Image graphic;
1585 -// try {
1586 -// if(shape.getFillColor() == null) {
1587 -// graphic = ImageHandler.instance().renderSVG(
1588 -// definition.getFilename(), bounds.width, bounds.height, definition.stretches());
1589 -// } else {
1590 -// graphic = ImageHandler.instance().renderSVG(
1591 -// definition.getFilename(), bounds.width, bounds.height, definition.stretches(), IDREGEX, shape.getFillColor());
1592 -// }
1593 -// g2.drawImage(graphic, bounds.x, bounds.y, null);
1594 -// } catch (IOException e) {
1595 -// e.printStackTrace();
1596 -// }
1597 -// }
1598 + private void renderCustomSVGShape(DiagramShape shape, Graphics2D g2){
1599 + CustomShapeDefinition definition = shape.getDefinition();
1600 + Rectangle bounds = shape.getBounds();
1601 + Image graphic;
1602 + try {
1603 + if(shape.getFillColor() == null) {
1604 + graphic = ImageHandler.instance().renderSVG(
1605 + definition.getFilename(), bounds.width, bounds.height, definition.stretches());
1606 + } else {
1607 + graphic = ImageHandler.instance().renderSVG(
1608 + definition.getFilename(), bounds.width, bounds.height, definition.stretches(), IDREGEX, shape.getFillColor());
1609 + }
1610 + g2.drawImage(graphic, bounds.x, bounds.y, null);
1611 + } catch (IOException e) {
1612 + e.printStackTrace();
1613 + }
1614 + }
1615
1616 private void renderCustomPNGShape(DiagramShape shape, Graphics2D g2){
1617 CustomShapeDefinition definition = shape.getDefinition();
1618 diff --git a/src/org/stathissideris/ascii2image/graphics/CompositeDiagramShape.java b/src/org/stathissideris/ascii2image/graphics/CompositeDiagramShape.java
1619 index 108f01e..0677966 100644
1620 --- a/src/org/stathissideris/ascii2image/graphics/CompositeDiagramShape.java
1621 +++ b/src/org/stathissideris/ascii2image/graphics/CompositeDiagramShape.java
1622 @@ -1,32 +1,31 @@
1623 -/*
1624 - * DiTAA - Diagrams Through Ascii Art
1625 +/**
1626 + * ditaa - Diagrams Through Ascii Art
1627 *
1628 - * Copyright (C) 2004 Efstathios Sideris
1629 + * Copyright (C) 2004-2011 Efstathios Sideris
1630 *
1631 - * This program is free software; you can redistribute it and/or
1632 - * modify it under the terms of the GNU General Public License
1633 - * as published by the Free Software Foundation; either version 2
1634 - * of the License, or (at your option) any later version.
1635 + * ditaa is free software: you can redistribute it and/or modify
1636 + * it under the terms of the GNU Lesser General Public License as
1637 + * published by the Free Software Foundation, either version 3 of
1638 + * the License, or (at your option) any later version.
1639 *
1640 - * This program is distributed in the hope that it will be useful,
1641 + * ditaa is distributed in the hope that it will be useful,
1642 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1643 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1644 - * GNU General Public License for more details.
1645 + * GNU Lesser General Public License for more details.
1646 *
1647 - * You should have received a copy of the GNU General Public License
1648 - * along with this program; if not, write to the Free Software
1649 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1650 + * You should have received a copy of the GNU Lesser General Public
1651 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1652 *
1653 */
1654 package org.stathissideris.ascii2image.graphics;
1655
1656 import java.util.ArrayList;
1657 -import java.util.Iterator;
1658 import java.util.List;
1659 +import java.util.Iterator;
1660 +
1661
1662 import org.stathissideris.ascii2image.core.DebugUtils;
1663 -import org.stathissideris.ascii2image.text.CellSet;
1664 -import org.stathissideris.ascii2image.text.TextGrid;
1665 +import org.stathissideris.ascii2image.text.*;
1666
1667 /**
1668 *
1669 @@ -140,9 +139,9 @@ public class CompositeDiagramShape extends DiagramComponent {
1670 if(nextCells.size() == 1) {
1671 previousCell = cell;
1672 cell = (TextGrid.Cell) nextCells.getFirst();
1673 - } else if(nextCells.size() > 1) {//3- or 4- way intersection
1674 + if(DEBUG) System.out.println("tracing at "+cell+" (call from line: "+DebugUtils.getLineNumber()+")");
1675 + } else if(nextCells.size() > 1 || nextCells.size() == 0) {//3- or 4- way intersection
1676 finished = true;
1677 - if(DEBUG) System.out.println("finished shape");
1678 for(TextGrid.Cell nextCell : nextCells)
1679 result.addAll(growEdgesFromCell(workGrid, cellWidth, cellHeight, allRound, nextCell, cell, visitedCells));
1680 }
1681 @@ -305,7 +304,7 @@ public class CompositeDiagramShape extends DiagramComponent {
1682 /**
1683 * @return
1684 */
1685 - public ArrayList getShapes() {
1686 + public ArrayList<DiagramShape> getShapes() {
1687 return shapes;
1688 }
1689
1690 diff --git a/src/org/stathissideris/ascii2image/graphics/CustomShapeDefinition.java b/src/org/stathissideris/ascii2image/graphics/CustomShapeDefinition.java
1691 index c3bce8a..db513d9 100644
1692 --- a/src/org/stathissideris/ascii2image/graphics/CustomShapeDefinition.java
1693 +++ b/src/org/stathissideris/ascii2image/graphics/CustomShapeDefinition.java
1694 @@ -1,21 +1,20 @@
1695 -/*
1696 - * DiTAA - Diagrams Through Ascii Art
1697 +/**
1698 + * ditaa - Diagrams Through Ascii Art
1699 *
1700 - * Copyright (C) 2004 Efstathios Sideris
1701 + * Copyright (C) 2004-2011 Efstathios Sideris
1702 *
1703 - * This program is free software; you can redistribute it and/or
1704 - * modify it under the terms of the GNU General Public License
1705 - * as published by the Free Software Foundation; either version 2
1706 - * of the License, or (at your option) any later version.
1707 + * ditaa is free software: you can redistribute it and/or modify
1708 + * it under the terms of the GNU Lesser General Public License as
1709 + * published by the Free Software Foundation, either version 3 of
1710 + * the License, or (at your option) any later version.
1711 *
1712 - * This program is distributed in the hope that it will be useful,
1713 + * ditaa is distributed in the hope that it will be useful,
1714 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1715 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1716 - * GNU General Public License for more details.
1717 + * GNU Lesser General Public License for more details.
1718 *
1719 - * You should have received a copy of the GNU General Public License
1720 - * along with this program; if not, write to the Free Software
1721 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1722 + * You should have received a copy of the GNU Lesser General Public
1723 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1724 *
1725 */
1726 package org.stathissideris.ascii2image.graphics;
1727 diff --git a/src/org/stathissideris/ascii2image/graphics/Diagram.java b/src/org/stathissideris/ascii2image/graphics/Diagram.java
1728 index c41af73..ef3d66b 100644
1729 --- a/src/org/stathissideris/ascii2image/graphics/Diagram.java
1730 +++ b/src/org/stathissideris/ascii2image/graphics/Diagram.java
1731 @@ -1,36 +1,39 @@
1732 -/*
1733 - * DiTAA - Diagrams Through Ascii Art
1734 +/**
1735 + * ditaa - Diagrams Through Ascii Art
1736 *
1737 - * Copyright (C) 2004 Efstathios Sideris
1738 + * Copyright (C) 2004-2011 Efstathios Sideris
1739 *
1740 - * This program is free software; you can redistribute it and/or
1741 - * modify it under the terms of the GNU General Public License
1742 - * as published by the Free Software Foundation; either version 2
1743 - * of the License, or (at your option) any later version.
1744 + * ditaa is free software: you can redistribute it and/or modify
1745 + * it under the terms of the GNU Lesser General Public License as
1746 + * published by the Free Software Foundation, either version 3 of
1747 + * the License, or (at your option) any later version.
1748 *
1749 - * This program is distributed in the hope that it will be useful,
1750 + * ditaa is distributed in the hope that it will be useful,
1751 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1752 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1753 - * GNU General Public License for more details.
1754 + * GNU Lesser General Public License for more details.
1755 *
1756 - * You should have received a copy of the GNU General Public License
1757 - * along with this program; if not, write to the Free Software
1758 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1759 + * You should have received a copy of the GNU Lesser General Public
1760 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
1761 *
1762 */
1763 package org.stathissideris.ascii2image.graphics;
1764
1765 import java.awt.Color;
1766 import java.awt.Font;
1767 +import java.awt.geom.Rectangle2D;
1768 import java.util.ArrayList;
1769 import java.util.Iterator;
1770
1771 import org.stathissideris.ascii2image.core.ConversionOptions;
1772 import org.stathissideris.ascii2image.core.Pair;
1773 -import org.stathissideris.ascii2image.core.ProcessingOptions;
1774 import org.stathissideris.ascii2image.text.AbstractionGrid;
1775 import org.stathissideris.ascii2image.text.CellSet;
1776 import org.stathissideris.ascii2image.text.TextGrid;
1777 +import org.stathissideris.ascii2image.text.TextGrid.Cell;
1778 +import org.stathissideris.ascii2image.text.TextGrid.CellColorPair;
1779 +import org.stathissideris.ascii2image.text.TextGrid.CellStringPair;
1780 +import org.stathissideris.ascii2image.text.TextGrid.CellTagPair;
1781
1782 /**
1783 *
1784 @@ -39,11 +42,12 @@ import org.stathissideris.ascii2image.text.TextGrid;
1785 public class Diagram {
1786
1787 private static final boolean DEBUG = false;
1788 - private static final boolean VERBOSE_DEBUG = false;
1789 + private static final boolean DEBUG_VERBOSE = false;
1790 + private static final boolean DEBUG_MAKE_SHAPES = false;
1791
1792 - private ArrayList shapes = new ArrayList();
1793 - private ArrayList compositeShapes = new ArrayList();
1794 - private ArrayList textObjects = new ArrayList();
1795 + private ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>();
1796 + private ArrayList<CompositeDiagramShape> compositeShapes = new ArrayList<CompositeDiagramShape>();
1797 + private ArrayList<DiagramText> textObjects = new ArrayList<DiagramText>();
1798
1799 private int width, height;
1800 private int cellWidth, cellHeight;
1801 @@ -67,7 +71,7 @@ public class Diagram {
1802 * <li>Remove obsolete boundaries. Obsolete boundaries are the ones that are
1803 * the sum of their parts when plotted as filled shapes. (see method
1804 * removeObsoleteShapes())</li>
1805 - * <li>Seperate the found boundary sets to open, closed or mixed
1806 + * <li>Separate the found boundary sets to open, closed or mixed
1807 * (See CellSet class on how its done).</li>
1808 * <li>Are there any closed boundaries?
1809 * <ul>
1810 @@ -81,7 +85,7 @@ public class Diagram {
1811 * an intersection.</li>
1812 * </ul>
1813 * </li>
1814 - * <li>If we had to eliminate any mixed shapes, we seperate the found
1815 + * <li>If we had to eliminate any mixed shapes, we separate the found
1816 * boundary sets again to open, closed or mixed.</li>
1817 * </ol>
1818 *
1819 @@ -95,7 +99,7 @@ public class Diagram {
1820 * see methods connectEndsToAnchors() and moveEndsToCellEdges() of
1821 * DiagramShape.</li>
1822 * <li>Assign color codes to closed shapes.</li>
1823 - * <li>Assing extended markup tags to closed shapes.</p>
1824 + * <li>Assign extended markup tags to closed shapes.</p>
1825 * <li>Create arrowheads.</p>
1826 * <li>Create point markers.</p>
1827 * </ol>
1828 @@ -106,7 +110,7 @@ public class Diagram {
1829 * @param cellWidth
1830 * @param cellHeight
1831 */
1832 - public Diagram(TextGrid grid, ConversionOptions options, ProcessingOptions processingOptions) {
1833 + public Diagram(TextGrid grid, ConversionOptions options) {
1834
1835 this.cellWidth = options.renderingOptions.getCellWidth();
1836 this.cellHeight = options.renderingOptions.getCellHeight();
1837 @@ -125,13 +129,13 @@ public class Diagram {
1838
1839 //split distinct shapes using AbstractionGrid
1840 AbstractionGrid temp = new AbstractionGrid(workGrid, workGrid.getAllBoundaries());
1841 - ArrayList boundarySetsStep1 = temp.getDistinctShapes();
1842 + ArrayList<CellSet> boundarySetsStep1 = temp.getDistinctShapes();
1843
1844 if(DEBUG){
1845 System.out.println("******* Distinct shapes found using AbstractionGrid *******");
1846 - Iterator dit = boundarySetsStep1.iterator();
1847 + Iterator<CellSet> dit = boundarySetsStep1.iterator();
1848 while (dit.hasNext()) {
1849 - CellSet set = (CellSet) dit.next();
1850 + CellSet set = dit.next();
1851 set.printAsGrid();
1852 }
1853 System.out.println("******* Same set of shapes after processing them by filling *******");
1854 @@ -140,11 +144,8 @@ public class Diagram {
1855
1856 //Find all the boundaries by using the special version of the filling method
1857 //(fills in a different buffer than the buffer it reads from)
1858 - ArrayList boundarySetsStep2 = new ArrayList();
1859 - Iterator boundarySetIt = boundarySetsStep1.iterator();
1860 - while (boundarySetIt.hasNext()) {
1861 - CellSet set = (CellSet) boundarySetIt.next();
1862 -
1863 + ArrayList<CellSet> boundarySetsStep2 = new ArrayList<CellSet>();
1864 + for(CellSet set : boundarySetsStep1) {
1865 //the fill buffer keeps track of which cells have been
1866 //filled already
1867 TextGrid fillBuffer = new TextGrid(width * 3, height * 3);
1868 @@ -186,9 +187,9 @@ public class Diagram {
1869 boundarySetsStep2 = CellSet.removeDuplicateSets(boundarySetsStep2);
1870
1871 if(DEBUG){
1872 - Iterator dit = boundarySetsStep2.iterator();
1873 + Iterator<CellSet> dit = boundarySetsStep2.iterator();
1874 while (dit.hasNext()) {
1875 - CellSet set = (CellSet) dit.next();
1876 + CellSet set = dit.next();
1877 set.printAsGrid();
1878 }
1879 }
1880 @@ -209,11 +210,11 @@ public class Diagram {
1881 if (DEBUG)
1882 System.out.println("******* First evaluation of openess *******");
1883
1884 - ArrayList open = new ArrayList();
1885 - ArrayList closed = new ArrayList();
1886 - ArrayList mixed = new ArrayList();
1887 + ArrayList<CellSet> open = new ArrayList<CellSet>();
1888 + ArrayList<CellSet> closed = new ArrayList<CellSet>();
1889 + ArrayList<CellSet> mixed = new ArrayList<CellSet>();
1890
1891 - Iterator sets = boundarySetsStep2.iterator();
1892 + Iterator<CellSet> sets = boundarySetsStep2.iterator();
1893 while(sets.hasNext()){
1894 CellSet set = (CellSet) sets.next();
1895 int type = set.getType(workGrid);
1896 @@ -242,9 +243,9 @@ public class Diagram {
1897 sets = mixed.iterator();
1898 while(sets.hasNext()){
1899 CellSet set = (CellSet) sets.next();
1900 - Iterator closedSets = closed.iterator();
1901 + Iterator<CellSet> closedSets = closed.iterator();
1902 while(closedSets.hasNext()){
1903 - CellSet closedSet = (CellSet) closedSets.next();
1904 + CellSet closedSet = closedSets.next();
1905 set.subtractSet(closedSet);
1906 }
1907 // this is necessary because some mixed sets produce
1908 @@ -289,9 +290,9 @@ public class Diagram {
1909 System.out.println("******* Second evaluation of openess *******");
1910
1911 //split boundaries again to open, closed and mixed
1912 - open = new ArrayList();
1913 - closed = new ArrayList();
1914 - mixed = new ArrayList();
1915 + open = new ArrayList<CellSet>();
1916 + closed = new ArrayList<CellSet>();
1917 + mixed = new ArrayList<CellSet>();
1918
1919 sets = boundarySetsStep2.iterator();
1920 while(sets.hasNext()){
1921 @@ -312,14 +313,24 @@ public class Diagram {
1922 boolean removedAnyObsolete = removeObsoleteShapes(workGrid, closed);
1923
1924 boolean allCornersRound = false;
1925 - if(processingOptions.areAllCornersRound()) allCornersRound = true;
1926 + if(options.processingOptions.areAllCornersRound()) allCornersRound = true;
1927
1928 //make shapes from the boundary sets
1929 //make closed shapes
1930 - ArrayList closedShapes = new ArrayList();
1931 + if(DEBUG_MAKE_SHAPES) {
1932 + System.out.println("***** MAKING SHAPES FROM BOUNDARY SETS *****");
1933 + System.out.println("***** CLOSED: *****");
1934 + }
1935 +
1936 + ArrayList<DiagramComponent> closedShapes = new ArrayList<DiagramComponent>();
1937 sets = closed.iterator();
1938 while(sets.hasNext()){
1939 CellSet set = (CellSet) sets.next();
1940 +
1941 + if(DEBUG_MAKE_SHAPES) {
1942 + set.printAsGrid();
1943 + }
1944 +
1945 DiagramComponent shape = DiagramComponent.createClosedFromBoundaryCells(workGrid, set, cellWidth, cellHeight, allCornersRound);
1946 if(shape != null){
1947 if(shape instanceof DiagramShape){
1948 @@ -330,7 +341,7 @@ public class Diagram {
1949 }
1950 }
1951
1952 - if(processingOptions.performSeparationOfCommonEdges())
1953 + if(options.processingOptions.performSeparationOfCommonEdges())
1954 separateCommonEdges(closedShapes);
1955
1956 //make open shapes
1957 @@ -347,6 +358,9 @@ public class Diagram {
1958 }
1959 }
1960 } else { //normal shape
1961 + if (DEBUG)
1962 + System.out.println(set.getCellsAsString());
1963 +
1964 DiagramComponent shape =
1965 CompositeDiagramShape
1966 .createOpenFromBoundaryCells(
1967 @@ -368,44 +382,30 @@ public class Diagram {
1968
1969 //assign color codes to shapes
1970 //TODO: text on line should not change its color
1971 - //TODO: each color tag should be assigned to the smallest containing shape (like shape tags)
1972
1973 - Iterator cellColorPairs = grid.findColorCodes().iterator();
1974 + Iterator<CellColorPair> cellColorPairs = grid.findColorCodes().iterator();
1975 while(cellColorPairs.hasNext()){
1976 TextGrid.CellColorPair pair =
1977 (TextGrid.CellColorPair) cellColorPairs.next();
1978 +
1979 ShapePoint point =
1980 new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell));
1981 - Iterator shapes = getShapes().iterator();
1982 - while(shapes.hasNext()){
1983 - DiagramShape shape = (DiagramShape) shapes.next();
1984 - if(shape.contains(point)) shape.setFillColor(pair.color);
1985 - }
1986 + DiagramShape containingShape = findSmallestShapeContaining(point);
1987 +
1988 + if(containingShape != null)
1989 + containingShape.setFillColor(pair.color);
1990 }
1991
1992 //assign markup to shapes
1993 - Iterator cellTagPairs = grid.findMarkupTags().iterator();
1994 + Iterator<CellTagPair> cellTagPairs = grid.findMarkupTags().iterator();
1995 while(cellTagPairs.hasNext()){
1996 TextGrid.CellTagPair pair =
1997 (TextGrid.CellTagPair) cellTagPairs.next();
1998 +
1999 ShapePoint point =
2000 new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell));
2001 -
2002 - //find the smallest shape that contains the tag
2003 - DiagramShape containingShape = null;
2004 - Iterator shapes = getShapes().iterator();
2005 - while(shapes.hasNext()){
2006 - DiagramShape shape = (DiagramShape) shapes.next();
2007 - if(shape.contains(point)){
2008 - if(containingShape == null){
2009 - containingShape = shape;
2010 - } else {
2011 - if(shape.isSmallerThan(containingShape)){
2012 - containingShape = shape;
2013 - }
2014 - }
2015 - }
2016 - }
2017 +
2018 + DiagramShape containingShape = findSmallestShapeContaining(point);
2019
2020 //this tag is not within a shape, skip
2021 if(containingShape == null) continue;
2022 @@ -413,7 +413,7 @@ public class Diagram {
2023 //TODO: the code below could be a lot more concise
2024 if(pair.tag.equals("d")){
2025 CustomShapeDefinition def =
2026 - processingOptions.getFromCustomShapes("d");
2027 + options.processingOptions.getFromCustomShapes("d");
2028 if(def == null)
2029 containingShape.setType(DiagramShape.TYPE_DOCUMENT);
2030 else {
2031 @@ -422,7 +422,7 @@ public class Diagram {
2032 }
2033 } else if(pair.tag.equals("s")){
2034 CustomShapeDefinition def =
2035 - processingOptions.getFromCustomShapes("s");
2036 + options.processingOptions.getFromCustomShapes("s");
2037 if(def == null)
2038 containingShape.setType(DiagramShape.TYPE_STORAGE);
2039 else {
2040 @@ -431,7 +431,7 @@ public class Diagram {
2041 }
2042 } else if(pair.tag.equals("io")){
2043 CustomShapeDefinition def =
2044 - processingOptions.getFromCustomShapes("io");
2045 + options.processingOptions.getFromCustomShapes("io");
2046 if(def == null)
2047 containingShape.setType(DiagramShape.TYPE_IO);
2048 else {
2049 @@ -440,7 +440,7 @@ public class Diagram {
2050 }
2051 } else if(pair.tag.equals("c")){
2052 CustomShapeDefinition def =
2053 - processingOptions.getFromCustomShapes("c");
2054 + options.processingOptions.getFromCustomShapes("c");
2055 if(def == null)
2056 containingShape.setType(DiagramShape.TYPE_DECISION);
2057 else {
2058 @@ -449,7 +449,7 @@ public class Diagram {
2059 }
2060 } else if(pair.tag.equals("mo")){
2061 CustomShapeDefinition def =
2062 - processingOptions.getFromCustomShapes("mo");
2063 + options.processingOptions.getFromCustomShapes("mo");
2064 if(def == null)
2065 containingShape.setType(DiagramShape.TYPE_MANUAL_OPERATION);
2066 else {
2067 @@ -458,7 +458,7 @@ public class Diagram {
2068 }
2069 } else if(pair.tag.equals("tr")){
2070 CustomShapeDefinition def =
2071 - processingOptions.getFromCustomShapes("tr");
2072 + options.processingOptions.getFromCustomShapes("tr");
2073 if(def == null)
2074 containingShape.setType(DiagramShape.TYPE_TRAPEZOID);
2075 else {
2076 @@ -467,7 +467,7 @@ public class Diagram {
2077 }
2078 } else if(pair.tag.equals("o")){
2079 CustomShapeDefinition def =
2080 - processingOptions.getFromCustomShapes("o");
2081 + options.processingOptions.getFromCustomShapes("o");
2082 if(def == null)
2083 containingShape.setType(DiagramShape.TYPE_ELLIPSE);
2084 else {
2085 @@ -476,25 +476,25 @@ public class Diagram {
2086 }
2087 } else {
2088 CustomShapeDefinition def =
2089 - processingOptions.getFromCustomShapes(pair.tag);
2090 + options.processingOptions.getFromCustomShapes(pair.tag);
2091 containingShape.setType(DiagramShape.TYPE_CUSTOM);
2092 containingShape.setDefinition(def);
2093 }
2094 }
2095
2096 //make arrowheads
2097 - Iterator arrowheadCells = workGrid.findArrowheads().iterator();
2098 + Iterator<Cell> arrowheadCells = workGrid.findArrowheads().iterator();
2099 while(arrowheadCells.hasNext()){
2100 - TextGrid.Cell cell = (TextGrid.Cell) arrowheadCells.next();
2101 + TextGrid.Cell cell = arrowheadCells.next();
2102 DiagramShape arrowhead = DiagramShape.createArrowhead(workGrid, cell, cellWidth, cellHeight);
2103 if(arrowhead != null) addToShapes(arrowhead);
2104 else System.err.println("Could not create arrowhead shape. Unexpected error.");
2105 }
2106
2107 //make point markers
2108 - Iterator markersIt = grid.getPointMarkersOnLine().iterator();
2109 + Iterator<TextGrid.Cell> markersIt = grid.getPointMarkersOnLine().iterator();
2110 while (markersIt.hasNext()) {
2111 - TextGrid.Cell cell = (TextGrid.Cell) markersIt.next();
2112 + TextGrid.Cell cell = markersIt.next();
2113
2114 DiagramShape mark = new DiagramShape();
2115 mark.addToPoints(new ShapePoint(
2116 @@ -523,22 +523,22 @@ public class Diagram {
2117 //kludge
2118 textGroupGrid.fillCellsWith(gaps, '|');
2119 CellSet nonBlank = textGroupGrid.getAllNonBlank();
2120 - ArrayList textGroups = nonBlank.breakIntoDistinctBoundaries();
2121 + ArrayList<CellSet> textGroups = nonBlank.breakIntoDistinctBoundaries();
2122 if(DEBUG) System.out.println(textGroups.size()+" text groups found");
2123
2124 Font font = FontMeasurer.instance().getFontFor(cellHeight);
2125
2126 - Iterator textGroupIt = textGroups.iterator();
2127 + Iterator<CellSet> textGroupIt = textGroups.iterator();
2128 while(textGroupIt.hasNext()){
2129 CellSet textGroupCellSet = (CellSet) textGroupIt.next();
2130
2131 TextGrid isolationGrid = new TextGrid(width, height);
2132 workGrid.copyCellsTo(textGroupCellSet, isolationGrid);
2133
2134 - ArrayList strings = isolationGrid.findStrings();
2135 - Iterator it = strings.iterator();
2136 + ArrayList<CellStringPair> strings = isolationGrid.findStrings();
2137 + Iterator<CellStringPair> it = strings.iterator();
2138 while(it.hasNext()){
2139 - TextGrid.CellStringPair pair = (TextGrid.CellStringPair) it.next();
2140 + TextGrid.CellStringPair pair = it.next();
2141 TextGrid.Cell cell = pair.cell;
2142 String string = pair.string;
2143 if (DEBUG)
2144 @@ -583,30 +583,21 @@ public class Diagram {
2145
2146 //correct the color of the text objects according
2147 //to the underlying color
2148 - Iterator shapes = this.getAllDiagramShapes().iterator();
2149 - while(shapes.hasNext()){
2150 - DiagramShape shape = (DiagramShape) shapes.next();
2151 - Color fillColor = shape.getFillColor();
2152 - if(shape.isClosed()
2153 - && shape.getType() != DiagramShape.TYPE_ARROWHEAD
2154 - && fillColor != null
2155 - && BitmapRenderer.isColorDark(fillColor)){
2156 - Iterator textObjects = getTextObjects().iterator();
2157 - while(textObjects.hasNext()){
2158 - DiagramText textObject = (DiagramText) textObjects.next();
2159 - if(shape.intersects(textObject.getBounds())){
2160 - textObject.setColor(Color.white);
2161 - }
2162 - }
2163 + for(DiagramText textObject : getTextObjects()) {
2164 + DiagramShape shape = findSmallestShapeIntersecting(textObject.getBounds());
2165 + if(shape != null
2166 + && shape.getFillColor() != null
2167 + && BitmapRenderer.isColorDark(shape.getFillColor())) {
2168 + textObject.setColor(Color.white);
2169 }
2170 }
2171
2172 //set outline to true for test within custom shapes
2173 - shapes = this.getAllDiagramShapes().iterator();
2174 + Iterator<DiagramShape> shapes = this.getAllDiagramShapes().iterator();
2175 while(shapes.hasNext()){
2176 DiagramShape shape = (DiagramShape) shapes.next();
2177 if(shape.getType() == DiagramShape.TYPE_CUSTOM){
2178 - Iterator textObjects = getTextObjects().iterator();
2179 + Iterator<DiagramText> textObjects = getTextObjects().iterator();
2180 while(textObjects.hasNext()){
2181 DiagramText textObject = (DiagramText) textObjects.next();
2182 textObject.setHasOutline(true);
2183 @@ -626,13 +617,11 @@ public class Diagram {
2184 *
2185 * @return
2186 */
2187 - public ArrayList getAllDiagramShapes(){
2188 - ArrayList shapes = new ArrayList();
2189 + public ArrayList<DiagramShape> getAllDiagramShapes(){
2190 + ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>();
2191 shapes.addAll(this.getShapes());
2192
2193 - Iterator shapesIt = this.getCompositeShapes().iterator();
2194 - while(shapesIt.hasNext()){
2195 - CompositeDiagramShape compShape = (CompositeDiagramShape) shapesIt.next();
2196 + for(CompositeDiagramShape compShape : getCompositeShapes()) {
2197 shapes.addAll(compShape.getShapes());
2198 }
2199 return shapes;
2200 @@ -655,7 +644,7 @@ public class Diagram {
2201
2202 Iterator it;
2203
2204 - if(VERBOSE_DEBUG) {
2205 + if(DEBUG_VERBOSE) {
2206 System.out.println("******* Sets before *******");
2207 it = sets.iterator();
2208 while(it.hasNext()){
2209 @@ -679,7 +668,7 @@ public class Diagram {
2210 while(it.hasNext()){
2211 CellSet set = (CellSet) it.next();
2212
2213 - if(VERBOSE_DEBUG){
2214 + if(DEBUG_VERBOSE){
2215 System.out.println("*** Deciding if the following should be removed:");
2216 set.printAsGrid();
2217 }
2218 @@ -707,7 +696,7 @@ public class Diagram {
2219 }
2220 }
2221
2222 - if(VERBOSE_DEBUG){
2223 + if(DEBUG_VERBOSE){
2224 System.out.println("Largest:");
2225 largest.printAsGrid();
2226 }
2227 @@ -721,13 +710,13 @@ public class Diagram {
2228 it2 = common.iterator();
2229 while(it2.hasNext()){
2230 CellSet set2 = (CellSet) it2.next();
2231 - if(VERBOSE_DEBUG){
2232 + if(DEBUG_VERBOSE){
2233 System.out.println("One of smalls:");
2234 set2.printAsGrid();
2235 }
2236 gridOfSmalls.fillCellsWith(set2, '*');
2237 }
2238 - if(VERBOSE_DEBUG){
2239 + if(DEBUG_VERBOSE){
2240 System.out.println("Sum of smalls:");
2241 gridOfSmalls.printDebug();
2242 }
2243 @@ -742,10 +731,10 @@ public class Diagram {
2244 System.out.println("Decided to remove set:");
2245 largest.printAsGrid();
2246 }
2247 - } else if (DEBUG){
2248 + } /*else if (DEBUG){
2249 System.out.println("This set WILL NOT be removed:");
2250 largest.printAsGrid();
2251 - }
2252 + }*/
2253 //if(gridLargest.equals(gridOfSmalls)) toBeRemovedIndices.add(new Integer(index));
2254 }
2255
2256 @@ -763,7 +752,7 @@ public class Diagram {
2257 sets.remove(set);
2258 }
2259
2260 - if(VERBOSE_DEBUG) {
2261 + if(DEBUG_VERBOSE) {
2262 System.out.println("******* Sets after *******");
2263 it = sets.iterator();
2264 while(it.hasNext()){
2265 @@ -853,6 +842,42 @@ public class Diagram {
2266 shapes.addAll(originalShapes);
2267 }
2268
2269 + private DiagramShape findSmallestShapeContaining(ShapePoint point) {
2270 + DiagramShape containingShape = null;
2271 + Iterator<DiagramShape> shapes = getShapes().iterator();
2272 + while(shapes.hasNext()){
2273 + DiagramShape shape = shapes.next();
2274 + if(shape.contains(point)){
2275 + if(containingShape == null){
2276 + containingShape = shape;
2277 + } else {
2278 + if(shape.isSmallerThan(containingShape)){
2279 + containingShape = shape;
2280 + }
2281 + }
2282 + }
2283 + }
2284 + return containingShape;
2285 + }
2286 +
2287 + private DiagramShape findSmallestShapeIntersecting(Rectangle2D rect) {
2288 + DiagramShape intersectingShape = null;
2289 + Iterator<DiagramShape> shapes = getShapes().iterator();
2290 + while(shapes.hasNext()){
2291 + DiagramShape shape = shapes.next();
2292 + if(shape.intersects(rect)){
2293 + if(intersectingShape == null){
2294 + intersectingShape = shape;
2295 + } else {
2296 + if(shape.isSmallerThan(intersectingShape)){
2297 + intersectingShape = shape;
2298 + }
2299 + }
2300 + }
2301 + }
2302 + return intersectingShape;
2303 + }
2304 +
2305 private void addToTextObjects(DiagramText shape){
2306 textObjects.add(shape);
2307 }
2308 @@ -901,14 +926,14 @@ public class Diagram {
2309 /**
2310 * @return
2311 */
2312 - public ArrayList getCompositeShapes() {
2313 + public ArrayList<CompositeDiagramShape> getCompositeShapes() {
2314 return compositeShapes;
2315 }
2316
2317 /**
2318 * @return
2319 */
2320 - public ArrayList getShapes() {
2321 + public ArrayList<DiagramShape> getShapes() {
2322 return shapes;
2323 }
2324
2325 @@ -966,7 +991,7 @@ public class Diagram {
2326 /**
2327 * @return
2328 */
2329 - public ArrayList getTextObjects() {
2330 + public ArrayList<DiagramText> getTextObjects() {
2331 return textObjects;
2332 }
2333
2334 diff --git a/src/org/stathissideris/ascii2image/graphics/DiagramComponent.java b/src/org/stathissideris/ascii2image/graphics/DiagramComponent.java
2335 index a0699d2..efaee3a 100644
2336 --- a/src/org/stathissideris/ascii2image/graphics/DiagramComponent.java
2337 +++ b/src/org/stathissideris/ascii2image/graphics/DiagramComponent.java
2338 @@ -1,25 +1,27 @@
2339 -/*
2340 - * DiTAA - Diagrams Through Ascii Art
2341 +/**
2342 + * ditaa - Diagrams Through Ascii Art
2343 *
2344 - * Copyright (C) 2004 Efstathios Sideris
2345 + * Copyright (C) 2004-2011 Efstathios Sideris
2346 *
2347 - * This program is free software; you can redistribute it and/or
2348 - * modify it under the terms of the GNU General Public License
2349 - * as published by the Free Software Foundation; either version 2
2350 - * of the License, or (at your option) any later version.
2351 + * ditaa is free software: you can redistribute it and/or modify
2352 + * it under the terms of the GNU Lesser General Public License as
2353 + * published by the Free Software Foundation, either version 3 of
2354 + * the License, or (at your option) any later version.
2355 *
2356 - * This program is distributed in the hope that it will be useful,
2357 + * ditaa is distributed in the hope that it will be useful,
2358 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2359 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2360 - * GNU General Public License for more details.
2361 + * GNU Lesser General Public License for more details.
2362 *
2363 - * You should have received a copy of the GNU General Public License
2364 - * along with this program; if not, write to the Free Software
2365 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2366 + * You should have received a copy of the GNU Lesser General Public
2367 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2368 *
2369 */
2370 package org.stathissideris.ascii2image.graphics;
2371
2372 +import java.util.ArrayList;
2373 +import java.util.Iterator;
2374 +
2375 import org.stathissideris.ascii2image.text.CellSet;
2376 import org.stathissideris.ascii2image.text.TextGrid;
2377
2378 @@ -108,8 +110,8 @@ public abstract class DiagramComponent {
2379 return null;
2380 } else {
2381 throw new RuntimeException("cannot create closed shape from boundary cells, nowhere to go from "
2382 - + cell + " coming from " + previous + " in grid:\n" + grid
2383 - +"\nmaybe you have an edge pointing nowhere?");
2384 + + cell + " coming from " + previous + " in grid:\n" + grid
2385 + +"\nmaybe you have an edge pointing nowhere?");
2386 }
2387 }
2388
2389 diff --git a/src/org/stathissideris/ascii2image/graphics/DiagramShape.java b/src/org/stathissideris/ascii2image/graphics/DiagramShape.java
2390 index 7a15133..a96f166 100644
2391 --- a/src/org/stathissideris/ascii2image/graphics/DiagramShape.java
2392 +++ b/src/org/stathissideris/ascii2image/graphics/DiagramShape.java
2393 @@ -1,35 +1,35 @@
2394 -/*
2395 - * DiTAA - Diagrams Through Ascii Art
2396 +/**
2397 + * ditaa - Diagrams Through Ascii Art
2398 *
2399 - * Copyright (C) 2004 Efstathios Sideris
2400 + * Copyright (C) 2004-2011 Efstathios Sideris
2401 *
2402 - * This program is free software; you can redistribute it and/or
2403 - * modify it under the terms of the GNU General Public License
2404 - * as published by the Free Software Foundation; either version 2
2405 - * of the License, or (at your option) any later version.
2406 + * ditaa is free software: you can redistribute it and/or modify
2407 + * it under the terms of the GNU Lesser General Public License as
2408 + * published by the Free Software Foundation, either version 3 of
2409 + * the License, or (at your option) any later version.
2410 *
2411 - * This program is distributed in the hope that it will be useful,
2412 + * ditaa is distributed in the hope that it will be useful,
2413 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2414 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2415 - * GNU General Public License for more details.
2416 + * GNU Lesser General Public License for more details.
2417 *
2418 - * You should have received a copy of the GNU General Public License
2419 - * along with this program; if not, write to the Free Software
2420 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2421 + * You should have received a copy of the GNU Lesser General Public
2422 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2423 *
2424 */
2425 package org.stathissideris.ascii2image.graphics;
2426
2427 import java.awt.Color;
2428 import java.awt.Rectangle;
2429 -import java.awt.geom.Ellipse2D;
2430 import java.awt.geom.GeneralPath;
2431 import java.awt.geom.Rectangle2D;
2432 +import java.awt.geom.Ellipse2D;
2433 import java.util.ArrayList;
2434 import java.util.HashMap;
2435 import java.util.Iterator;
2436
2437 -import org.stathissideris.ascii2image.text.TextGrid;
2438 +import org.stathissideris.ascii2image.core.RenderingOptions;
2439 +import org.stathissideris.ascii2image.text.*;
2440
2441 /**
2442 *
2443 @@ -51,6 +51,9 @@ public class DiagramShape extends DiagramComponent {
2444 public static final int TYPE_ELLIPSE = 9;
2445 public static final int TYPE_CUSTOM = 9999;
2446
2447 + /** The slope of side lines on trapezoids (mo, tr) and parallelograms (io). */
2448 + public static final float SHAPE_SLOPE = 8;
2449 +
2450 protected int type = TYPE_SIMPLE;
2451
2452 private Color fillColor = null;
2453 @@ -59,7 +62,7 @@ public class DiagramShape extends DiagramComponent {
2454 private boolean isClosed = false;
2455 private boolean isStrokeDashed = false;
2456
2457 - protected ArrayList points = new ArrayList();
2458 + protected ArrayList<ShapePoint> points = new ArrayList<ShapePoint>();
2459
2460 CustomShapeDefinition definition = null;
2461
2462 @@ -392,7 +395,7 @@ public class DiagramShape extends DiagramComponent {
2463 return bounds;
2464 }
2465
2466 - public GeneralPath makeIntoRenderPath(Diagram diagram) {
2467 + public GeneralPath makeIntoRenderPath(Diagram diagram, RenderingOptions options) {
2468 int size = getPoints().size();
2469
2470 if(getType() == TYPE_POINT_MARKER){
2471 @@ -408,7 +411,7 @@ public class DiagramShape extends DiagramComponent {
2472 }
2473
2474 if(getType() == TYPE_IO && points.size() == 4){
2475 - return makeIOPath(diagram);
2476 + return makeIOPath(diagram, options);
2477 }
2478
2479 if(getType() == TYPE_DECISION && points.size() == 4){
2480 @@ -416,11 +419,11 @@ public class DiagramShape extends DiagramComponent {
2481 }
2482
2483 if(getType() == TYPE_MANUAL_OPERATION && points.size() == 4){
2484 - return makeTrapezoidPath(diagram, true);
2485 + return makeTrapezoidPath(diagram, options, true);
2486 }
2487
2488 if(getType() == TYPE_TRAPEZOID && points.size() == 4){
2489 - return makeTrapezoidPath(diagram, false);
2490 + return makeTrapezoidPath(diagram, options, false);
2491 }
2492
2493 if(getType() == TYPE_ELLIPSE && points.size() == 4){
2494 @@ -596,7 +599,7 @@ public class DiagramShape extends DiagramComponent {
2495 TextGrid.Cell cell = diagram.getCellFor(pointInCell);
2496
2497 if(cell == null)
2498 - throw new RuntimeException("Upexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram);
2499 + throw new RuntimeException("Unexpected error, cannot find cell corresponding to point "+pointInCell+" for diagram "+diagram);
2500
2501 if(otherPoint.isNorthOf(pointInCell))
2502 result = new ShapePoint(pointInCell.x,
2503 @@ -612,7 +615,7 @@ public class DiagramShape extends DiagramComponent {
2504 pointInCell.y);
2505
2506 if(result == null)
2507 - throw new RuntimeException("Upexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram);
2508 + throw new RuntimeException("Unexpected error, cannot find cell edge point for points "+pointInCell+" and "+otherPoint+" for diagram "+diagram);
2509
2510
2511 return result;
2512 @@ -878,11 +881,11 @@ public class DiagramShape extends DiagramComponent {
2513 return path;
2514 }
2515
2516 - private GeneralPath makeTrapezoidPath(Diagram diagram, boolean inverted) {
2517 + private GeneralPath makeTrapezoidPath(Diagram diagram, RenderingOptions options, boolean inverted) {
2518 if(points.size() != 4) return null;
2519 Rectangle bounds = makeIntoPath().getBounds();
2520 - float offset = 0.7f * diagram.getCellWidth(); // fixed slope
2521 - if (inverted) offset = -offset;
2522 + float offset = options.isFixedSlope() ? bounds.height / SHAPE_SLOPE : diagram.getCellWidth() * 0.5f;
2523 + if (inverted) offset = -offset;
2524 ShapePoint ul = new ShapePoint((float)bounds.getMinX() + offset, (float)bounds.getMinY());
2525 ShapePoint ur = new ShapePoint((float)bounds.getMaxX() - offset, (float)bounds.getMinY());
2526 ShapePoint br = new ShapePoint((float)bounds.getMaxX() + offset, (float)bounds.getMaxY());
2527 @@ -903,7 +906,7 @@ public class DiagramShape extends DiagramComponent {
2528 private GeneralPath makeDecisionPath(Diagram diagram) {
2529 if(points.size() != 4) return null;
2530 Rectangle bounds = makeIntoPath().getBounds();
2531 - ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY());
2532 + ShapePoint pointMid = new ShapePoint((float)bounds.getCenterX(), (float)bounds.getCenterY());
2533 ShapePoint left = new ShapePoint((float)bounds.getMinX(), (float)pointMid.getY());
2534 ShapePoint right = new ShapePoint((float)bounds.getMaxX(), (float)pointMid.getY());
2535 ShapePoint top = new ShapePoint((float)pointMid.getX(), (float)bounds.getMinY());
2536 @@ -920,7 +923,7 @@ public class DiagramShape extends DiagramComponent {
2537 return path;
2538 }
2539
2540 - private GeneralPath makeIOPath(Diagram diagram) {
2541 + private GeneralPath makeIOPath(Diagram diagram, RenderingOptions options) {
2542 if(points.size() != 4) return null;
2543 Rectangle bounds = makeIntoPath().getBounds();
2544 ShapePoint point1 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMinY());
2545 @@ -928,7 +931,7 @@ public class DiagramShape extends DiagramComponent {
2546 ShapePoint point3 = new ShapePoint((float)bounds.getMaxX(), (float)bounds.getMaxY());
2547 ShapePoint point4 = new ShapePoint((float)bounds.getMinX(), (float)bounds.getMaxY());
2548
2549 - float offset = diagram.getCellWidth() / 2;
2550 + float offset = options.isFixedSlope() ? bounds.height / SHAPE_SLOPE : diagram.getCellWidth() * 0.5f;
2551
2552 GeneralPath path = new GeneralPath();
2553 path.moveTo(point1.x + offset, point1.y);
2554 @@ -948,4 +951,28 @@ public class DiagramShape extends DiagramComponent {
2555 this.definition = definition;
2556 }
2557
2558 + /**
2559 + * See http://mathworld.wolfram.com/PolygonArea.html
2560 + *
2561 + * @return the overall area of the shape
2562 + */
2563 + public double calculateArea() {
2564 + if(points.size() == 0) return 0;
2565 +
2566 + double area = 0;
2567 +
2568 + for(int i = 0; i < points.size() - 1; i++){
2569 + ShapePoint point1 = points.get(i);
2570 + ShapePoint point2 = points.get(i + 1);
2571 + area += point1.x * point2.y;
2572 + area -= point2.x * point1.y;
2573 + }
2574 + ShapePoint point1 = points.get(points.size() - 1);
2575 + ShapePoint point2 = points.get(0);
2576 + area += point1.x * point2.y;
2577 + area -= point2.x * point1.y;
2578 +
2579 + return Math.abs(area / 2);
2580 + }
2581 +
2582 }
2583 diff --git a/src/org/stathissideris/ascii2image/graphics/DiagramText.java b/src/org/stathissideris/ascii2image/graphics/DiagramText.java
2584 index 58c984d..ef411ca 100644
2585 --- a/src/org/stathissideris/ascii2image/graphics/DiagramText.java
2586 +++ b/src/org/stathissideris/ascii2image/graphics/DiagramText.java
2587 @@ -1,27 +1,27 @@
2588 -/*
2589 - * DiTAA - Diagrams Through Ascii Art
2590 +/**
2591 + * ditaa - Diagrams Through Ascii Art
2592 *
2593 - * Copyright (C) 2004 Efstathios Sideris
2594 + * Copyright (C) 2004-2011 Efstathios Sideris
2595 *
2596 - * This program is free software; you can redistribute it and/or
2597 - * modify it under the terms of the GNU General Public License
2598 - * as published by the Free Software Foundation; either version 2
2599 - * of the License, or (at your option) any later version.
2600 + * ditaa is free software: you can redistribute it and/or modify
2601 + * it under the terms of the GNU Lesser General Public License as
2602 + * published by the Free Software Foundation, either version 3 of
2603 + * the License, or (at your option) any later version.
2604 *
2605 - * This program is distributed in the hope that it will be useful,
2606 + * ditaa is distributed in the hope that it will be useful,
2607 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2608 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2609 - * GNU General Public License for more details.
2610 + * GNU Lesser General Public License for more details.
2611 *
2612 - * You should have received a copy of the GNU General Public License
2613 - * along with this program; if not, write to the Free Software
2614 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2615 + * You should have received a copy of the GNU Lesser General Public
2616 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2617 *
2618 */
2619 package org.stathissideris.ascii2image.graphics;
2620
2621 import java.awt.Color;
2622 import java.awt.Font;
2623 +import java.awt.Rectangle;
2624 import java.awt.geom.Rectangle2D;
2625
2626 /**
2627 diff --git a/src/org/stathissideris/ascii2image/graphics/FontMeasurer.java b/src/org/stathissideris/ascii2image/graphics/FontMeasurer.java
2628 index 665c0f3..6cdac3e 100644
2629 --- a/src/org/stathissideris/ascii2image/graphics/FontMeasurer.java
2630 +++ b/src/org/stathissideris/ascii2image/graphics/FontMeasurer.java
2631 @@ -1,21 +1,20 @@
2632 -/*
2633 - * DiTAA - Diagrams Through Ascii Art
2634 +/**
2635 + * ditaa - Diagrams Through Ascii Art
2636 *
2637 - * Copyright (C) 2004 Efstathios Sideris
2638 + * Copyright (C) 2004-2011 Efstathios Sideris
2639 *
2640 - * This program is free software; you can redistribute it and/or
2641 - * modify it under the terms of the GNU General Public License
2642 - * as published by the Free Software Foundation; either version 2
2643 - * of the License, or (at your option) any later version.
2644 + * ditaa is free software: you can redistribute it and/or modify
2645 + * it under the terms of the GNU Lesser General Public License as
2646 + * published by the Free Software Foundation, either version 3 of
2647 + * the License, or (at your option) any later version.
2648 *
2649 - * This program is distributed in the hope that it will be useful,
2650 + * ditaa is distributed in the hope that it will be useful,
2651 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2652 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2653 - * GNU General Public License for more details.
2654 + * GNU Lesser General Public License for more details.
2655 *
2656 - * You should have received a copy of the GNU General Public License
2657 - * along with this program; if not, write to the Free Software
2658 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2659 + * You should have received a copy of the GNU Lesser General Public
2660 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2661 *
2662 */
2663 package org.stathissideris.ascii2image.graphics;
2664 @@ -23,11 +22,15 @@ package org.stathissideris.ascii2image.graphics;
2665 import java.awt.Font;
2666 import java.awt.FontMetrics;
2667 import java.awt.Graphics2D;
2668 +import java.awt.GraphicsEnvironment;
2669 +import java.awt.Rectangle;
2670 import java.awt.font.FontRenderContext;
2671 import java.awt.geom.Rectangle2D;
2672 import java.awt.image.BufferedImage;
2673 import java.util.Locale;
2674
2675 +import javax.swing.JOptionPane;
2676 +
2677 /**
2678 *
2679 * @author Efstathios Sideris
2680 @@ -47,7 +50,7 @@ public class FontMeasurer {
2681 BufferedImage image = new BufferedImage(1,1, BufferedImage.TYPE_INT_RGB);
2682 fakeGraphics = image.createGraphics();
2683
2684 - if (DEBUG) System.out.println("Locale: "+Locale.getDefault());
2685 + if(DEBUG) System.out.println("Locale: "+Locale.getDefault());
2686
2687 fakeRenderContext = fakeGraphics.getFontRenderContext();
2688 }
2689 @@ -139,7 +142,7 @@ public class FontMeasurer {
2690 float size = 12;
2691 Font currentFont = new Font(fontFamilyName, Font.BOLD, (int) size);
2692 // Font currentFont = new Font("Times", Font.BOLD, (int) size);
2693 - if (DEBUG) System.out.println(currentFont.getFontName());
2694 + if(DEBUG) System.out.println(currentFont.getFontName());
2695 //ascent is the distance between the baseline and the tallest character
2696 int ascent = getAscent(currentFont);
2697
2698 @@ -183,7 +186,7 @@ public class FontMeasurer {
2699 //FontMeasurer.instance().getFontFor(7);
2700 float size = 12;
2701 Font currentFont = new Font("Sans", Font.BOLD, (int) size);
2702 - System.out.println(currentFont.getSize());
2703 + if(DEBUG) System.out.println(currentFont.getSize());
2704 currentFont = currentFont.deriveFont(--size);
2705 System.out.println(currentFont.getSize());
2706 currentFont = currentFont.deriveFont(--size);
2707 diff --git a/src/org/stathissideris/ascii2image/graphics/ImageHandler.java b/src/org/stathissideris/ascii2image/graphics/ImageHandler.java
2708 index 4c89799..74f8d98 100644
2709 --- a/src/org/stathissideris/ascii2image/graphics/ImageHandler.java
2710 +++ b/src/org/stathissideris/ascii2image/graphics/ImageHandler.java
2711 @@ -1,36 +1,64 @@
2712 -/*
2713 - * DiTAA - Diagrams Through Ascii Art
2714 +/**
2715 + * ditaa - Diagrams Through Ascii Art
2716 *
2717 - * Copyright (C) 2004 Efstathios Sideris
2718 + * Copyright (C) 2004-2011 Efstathios Sideris
2719 *
2720 - * This program is free software; you can redistribute it and/or
2721 - * modify it under the terms of the GNU General Public License
2722 - * as published by the Free Software Foundation; either version 2
2723 - * of the License, or (at your option) any later version.
2724 + * ditaa is free software: you can redistribute it and/or modify
2725 + * it under the terms of the GNU Lesser General Public License as
2726 + * published by the Free Software Foundation, either version 3 of
2727 + * the License, or (at your option) any later version.
2728 *
2729 - * This program is distributed in the hope that it will be useful,
2730 + * ditaa is distributed in the hope that it will be useful,
2731 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2732 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2733 - * GNU General Public License for more details.
2734 + * GNU Lesser General Public License for more details.
2735 *
2736 - * You should have received a copy of the GNU General Public License
2737 - * along with this program; if not, write to the Free Software
2738 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2739 + * You should have received a copy of the GNU Lesser General Public
2740 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2741 *
2742 */
2743 package org.stathissideris.ascii2image.graphics;
2744
2745 +import java.awt.Color;
2746 import java.awt.Image;
2747 import java.awt.MediaTracker;
2748 +import java.awt.Rectangle;
2749 import java.awt.Toolkit;
2750 +import java.awt.geom.AffineTransform;
2751 +import java.awt.image.BufferedImage;
2752 +import java.io.File;
2753 +import java.io.FileOutputStream;
2754 +import java.io.IOException;
2755 +import java.io.OutputStream;
2756 +import java.net.URI;
2757 import java.net.URL;
2758
2759 +import javax.imageio.ImageIO;
2760 +import javax.swing.ImageIcon;
2761 +import javax.swing.JButton;
2762 +import javax.swing.JFrame;
2763 import javax.swing.JLabel;
2764
2765 +import org.apache.batik.bridge.BridgeContext;
2766 +import org.apache.batik.bridge.GVTBuilder;
2767 +import org.apache.batik.bridge.UserAgentAdapter;
2768 +import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
2769 +import org.apache.batik.dom.util.DocumentFactory;
2770 +import org.apache.batik.ext.awt.image.codec.png.PNGEncodeParam;
2771 +import org.apache.batik.ext.awt.image.codec.png.PNGImageEncoder;
2772 +import org.apache.batik.gvt.GraphicsNode;
2773 +import org.apache.batik.gvt.renderer.ConcreteImageRendererFactory;
2774 +import org.apache.batik.gvt.renderer.ImageRenderer;
2775 +import org.apache.batik.gvt.renderer.ImageRendererFactory;
2776 +import org.apache.batik.gvt.renderer.StaticRenderer;
2777 +import org.stathissideris.ascii2image.core.FileUtils;
2778 +import org.w3c.dom.Document;
2779 +import org.w3c.dom.svg.SVGDocument;
2780 +
2781 public class ImageHandler {
2782
2783 -// private static OffScreenSVGRenderer svgRenderer =
2784 -// new OffScreenSVGRenderer();
2785 + private static OffScreenSVGRenderer svgRenderer =
2786 + new OffScreenSVGRenderer();
2787
2788 private static ImageHandler instance = new ImageHandler();
2789
2790 @@ -40,6 +68,10 @@ public class ImageHandler {
2791
2792 private static final MediaTracker tracker = new MediaTracker(new JLabel());
2793
2794 + public BufferedImage loadBufferedImage(File file) throws IOException {
2795 + return ImageIO.read(file);
2796 + }
2797 +
2798 public Image loadImage(String filename){
2799 URL url = ClassLoader.getSystemResource(filename);
2800 Image result = null;
2801 @@ -62,17 +94,46 @@ public class ImageHandler {
2802 return result;
2803 }
2804
2805 -// public BufferedImage renderSVG(String filename, int width, int height, boolean stretch) throws IOException {
2806 -// File file = new File(filename);
2807 -// URI uri = file.toURI();
2808 -// return svgRenderer.renderToImage(uri.toString(), width, height, stretch, null, null);
2809 -// }
2810 -//
2811 -// public BufferedImage renderSVG(String filename, int width, int height, boolean stretch, String idRegex, Color color) throws IOException {
2812 -// File file = new File(filename);
2813 -// URI uri = file.toURI();
2814 -// return svgRenderer.renderToImage(uri.toString(), width, height, stretch, idRegex, color);
2815 -// }
2816 + public BufferedImage renderSVG(String filename, int width, int height, boolean stretch) throws IOException {
2817 + File file = new File(filename);
2818 + URI uri = file.toURI();
2819 + return svgRenderer.renderToImage(uri.toString(), width, height, stretch, null, null);
2820 + }
2821 +
2822 + public BufferedImage renderSVG(String filename, int width, int height, boolean stretch, String idRegex, Color color) throws IOException {
2823 + File file = new File(filename);
2824 + URI uri = file.toURI();
2825 + return svgRenderer.renderToImage(uri.toString(), width, height, stretch, idRegex, color);
2826 + }
2827
2828
2829 + public static void main(String[] args) throws IOException{
2830 +
2831 + OffScreenSVGRenderer renderer = new OffScreenSVGRenderer();
2832 +
2833 + //BufferedImage image = instance.renderSVG("sphere.svg", 200, 200, false);
2834 +
2835 + //BufferedImage image = renderer.renderToImage("file:///Users/sideris/Documents/workspace/ditaa/joystick.svg", FileUtils.readFile(new File("joystick.svg")), 400, 200, false);
2836 +// BufferedImage image = renderer.renderToImage(
2837 +// null, FileUtils.readFile(new File("sphere.svg")).replaceFirst("#187637", "#3333FF"), 200, 200, false);
2838 +
2839 + String content = FileUtils.readFile(new File("sphere.svg")).replaceAll("#187637", "#1133FF");
2840 +
2841 + System.out.println(content);
2842 +
2843 +// BufferedImage image = renderer.renderToImage(
2844 +// "file:/K:/devel/ditaa/sphere.svg", content, 200, 200, false);
2845 +
2846 + BufferedImage image = renderer.renderXMLToImage(content, 200, 200, false, null, null);
2847 +
2848 +
2849 + try {
2850 + File file = new File("testing.png");
2851 + ImageIO.write(image, "png", file);
2852 + } catch (IOException e) {
2853 + //e.printStackTrace();
2854 + System.err.println("Error: Cannot write to file");
2855 + }
2856 +
2857 + }
2858 }
2859 diff --git a/src/org/stathissideris/ascii2image/graphics/OffScreenSVGRenderer.java b/src/org/stathissideris/ascii2image/graphics/OffScreenSVGRenderer.java
2860 new file mode 100644
2861 index 0000000..21c88bc
2862 --- /dev/null
2863 +++ b/src/org/stathissideris/ascii2image/graphics/OffScreenSVGRenderer.java
2864 @@ -0,0 +1,138 @@
2865 +/**
2866 + * ditaa - Diagrams Through Ascii Art
2867 + *
2868 + * Copyright (C) 2004-2011 Efstathios Sideris
2869 + *
2870 + * ditaa is free software: you can redistribute it and/or modify
2871 + * it under the terms of the GNU Lesser General Public License as
2872 + * published by the Free Software Foundation, either version 3 of
2873 + * the License, or (at your option) any later version.
2874 + *
2875 + * ditaa is distributed in the hope that it will be useful,
2876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2878 + * GNU Lesser General Public License for more details.
2879 + *
2880 + * You should have received a copy of the GNU Lesser General Public
2881 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
2882 + *
2883 + */
2884 +package org.stathissideris.ascii2image.graphics;
2885 +
2886 +import java.awt.Color;
2887 +import java.awt.Rectangle;
2888 +import java.awt.geom.AffineTransform;
2889 +import java.awt.image.BufferedImage;
2890 +import java.io.IOException;
2891 +import java.io.StringReader;
2892 +
2893 +import org.apache.batik.bridge.BridgeContext;
2894 +import org.apache.batik.bridge.GVTBuilder;
2895 +import org.apache.batik.bridge.UserAgentAdapter;
2896 +import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
2897 +import org.apache.batik.gvt.GraphicsNode;
2898 +import org.apache.batik.gvt.renderer.ConcreteImageRendererFactory;
2899 +import org.apache.batik.gvt.renderer.ImageRenderer;
2900 +import org.apache.batik.gvt.renderer.ImageRendererFactory;
2901 +import org.w3c.dom.NodeList;
2902 +import org.w3c.dom.svg.SVGDocument;
2903 +import org.w3c.dom.svg.SVGElement;
2904 +
2905 +public class OffScreenSVGRenderer {
2906 +
2907 + private static final boolean DEBUG = false;
2908 +
2909 + public BufferedImage renderXMLToImage(String xmlContent, int width, int height) throws IOException {
2910 + return renderXMLToImage(xmlContent, width, height, false, null, null);
2911 + }
2912 +
2913 + public BufferedImage renderXMLToImage(String xmlContent, int width, int height, boolean stretch, String idRegex, Color replacementColor) throws IOException {
2914 + // the following is necessary so that batik knows how to resolve URI fragments
2915 + // (#myLinearGradient). Otherwise the resolution fails and you cannot render.
2916 +
2917 + String uri = "file:/fake.svg";
2918 +
2919 + SAXSVGDocumentFactory df = new SAXSVGDocumentFactory("org.apache.xerces.parsers.SAXParser");
2920 + SVGDocument document = df.createSVGDocument(uri, new StringReader(xmlContent));
2921 + if(idRegex != null && replacementColor != null)
2922 + replaceFill(document, idRegex, replacementColor);
2923 + return renderToImage(document, width, height, stretch);
2924 + }
2925 +
2926 + public BufferedImage renderToImage(String uri, int width, int height) throws IOException {
2927 + return renderToImage(uri, width, height, false, null, null);
2928 + }
2929 +
2930 + public BufferedImage renderToImage(String uri, int width, int height, boolean stretch, String idRegex, Color replacementColor) throws IOException {
2931 + SAXSVGDocumentFactory df = new SAXSVGDocumentFactory("org.apache.xerces.parsers.SAXParser");
2932 + SVGDocument document = df.createSVGDocument(uri);
2933 + if(idRegex != null && replacementColor != null)
2934 + replaceFill(document, idRegex, replacementColor);
2935 + return renderToImage(document, width, height, stretch);
2936 + }
2937 +
2938 + public BufferedImage renderToImage(SVGDocument document, int width, int height){
2939 + return renderToImage(document, width, height, false);
2940 + }
2941 +
2942 + public void replaceFill(SVGDocument document, String idRegex, Color color){
2943 + String colorCode = String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
2944 +
2945 + if(DEBUG) System.out.println("color code: "+colorCode);
2946 +
2947 + NodeList children = document.getElementsByTagName("*");
2948 + for(int i = 0; i < children.getLength(); i++){
2949 + if(children.item(i) instanceof SVGElement){
2950 + SVGElement element = (SVGElement) children.item(i);
2951 + if(element.getId().matches(idRegex)){
2952 + if(DEBUG) System.out.println("child>>> "+element+", "+element.getId());
2953 + String style = element.getAttributeNS(null, "style");
2954 + style = style.replaceFirst("fill:#[a-zA-z0-9]+", "fill:"+colorCode);
2955 + if(DEBUG) System.out.println(style);
2956 + element.setAttributeNS(null, "style", style);
2957 + }
2958 + }
2959 + }
2960 + }
2961 +
2962 + public BufferedImage renderToImage(SVGDocument document, int width, int height, boolean stretch){
2963 +
2964 + ImageRendererFactory rendererFactory;
2965 + rendererFactory = new ConcreteImageRendererFactory();
2966 + ImageRenderer renderer = rendererFactory.createStaticImageRenderer();
2967 +
2968 + GVTBuilder builder = new GVTBuilder();
2969 + BridgeContext ctx = new BridgeContext(new UserAgentAdapter());
2970 + ctx.setDynamicState(BridgeContext.STATIC);
2971 + GraphicsNode rootNode = builder.build(ctx, document);
2972 +
2973 + renderer.setTree(rootNode);
2974 +
2975 + float docWidth = (float) ctx.getDocumentSize().getWidth();
2976 + float docHeight = (float) ctx.getDocumentSize().getHeight();
2977 +
2978 + float xscale = width/docWidth;
2979 + float yscale = height/docHeight;
2980 + if(!stretch){
2981 + float scale = Math.min(xscale, yscale);
2982 + xscale = scale;
2983 + yscale = scale;
2984 + }
2985 +
2986 + AffineTransform px = AffineTransform.getScaleInstance(xscale, yscale);
2987 +
2988 + double tx = -0 + (width/xscale - docWidth)/2;
2989 + double ty = -0 + (height/yscale - docHeight)/2;
2990 + px.translate(tx, ty);
2991 + //cgn.setViewingTransform(px);
2992 +
2993 + renderer.updateOffScreen(width, height);
2994 + renderer.setTree(rootNode);
2995 + renderer.setTransform(px);
2996 + //renderer.clearOffScreen();
2997 + renderer.repaint(new Rectangle(0, 0, width, height));
2998 +
2999 + return renderer.getOffScreen();
3000 +
3001 + }
3002 +}
3003 diff --git a/src/org/stathissideris/ascii2image/graphics/SVGBuilder.java b/src/org/stathissideris/ascii2image/graphics/SVGBuilder.java
3004 new file mode 100644
3005 index 0000000..7be7cd5
3006 --- /dev/null
3007 +++ b/src/org/stathissideris/ascii2image/graphics/SVGBuilder.java
3008 @@ -0,0 +1,416 @@
3009 +package org.stathissideris.ascii2image.graphics;
3010 +
3011 +import org.stathissideris.ascii2image.core.RenderingOptions;
3012 +import org.stathissideris.ascii2image.core.ShapeAreaComparator;
3013 +import org.stathissideris.ascii2image.core.Shape3DOrderingComparator;
3014 +
3015 +import java.awt.*;
3016 +import java.awt.geom.GeneralPath;
3017 +import java.awt.geom.PathIterator;
3018 +import java.util.ArrayList;
3019 +
3020 +/**
3021 + * Created by Jean Lazarou.
3022 + */
3023 +public class SVGBuilder {
3024 +
3025 + SVGBuilder(Diagram diagram, RenderingOptions options) {
3026 +
3027 + this.diagram = diagram;
3028 + this.options = options;
3029 +
3030 + float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2;
3031 + float strokeWeight = diagram.getMinimumOfCellDimension() / 10;
3032 +
3033 + normalStroke = String.format("stroke-width='%f' stroke-linecap='round' stroke-linejoin='round' ",
3034 + strokeWeight);
3035 +
3036 + dashStroke = String.format(
3037 + "stroke-width='%f' stroke-dasharray='%f,%f' stroke-miterlimit='0' " +
3038 + "stroke-linecap='butt' stroke-linejoin='round' ",
3039 + strokeWeight, dashInterval, dashInterval);
3040 +
3041 + }
3042 +
3043 + public String build() {
3044 +
3045 + return openSVGTag() + definitions() + render() + "</svg>";
3046 +
3047 + }
3048 +
3049 + private String definitions() {
3050 +
3051 + String DEFS =
3052 + " <defs>\n%s" +
3053 + " <filter id='f2' x='0' y='0' width='200%%' height='200%%'>\n" +
3054 + " <feOffset result='offOut' in='SourceGraphic' dx='5' dy='5' />\n" +
3055 + " <feGaussianBlur result='blurOut' in='offOut' stdDeviation='3' />\n" +
3056 + " <feBlend in='SourceGraphic' in2='blurOut' mode='normal' />\n" +
3057 + " </filter>\n" +
3058 + " </defs>\n";
3059 +
3060 + if (options.getFontURL() == null) {
3061 + return String.format(DEFS, "");
3062 + }
3063 +
3064 + String fontStyle =
3065 + " <style type='text/css'>\n" +
3066 + " @font-face {\n" +
3067 + " font-family: %s;\n" +
3068 + " src: url('%s');\n" +
3069 + " }\n" +
3070 + " </style>\n";
3071 +
3072 + return String.format(DEFS, String.format(fontStyle, options.getFontFamily(), options.getFontURL()));
3073 +
3074 + }
3075 +
3076 + private String openSVGTag() {
3077 +
3078 + String HEADER =
3079 + "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n" +
3080 + "<svg \n" +
3081 + " xmlns='http://www.w3.org/2000/svg'\n" +
3082 + " width='%d'\n" +
3083 + " height='%d'\n" +
3084 + " %s\n" +
3085 + " version='1.0'>\n";
3086 +
3087 + return String.format(
3088 + HEADER,
3089 + diagram.getWidth(),
3090 + diagram.getHeight(),
3091 + antialiasing()
3092 + );
3093 +
3094 + }
3095 +
3096 + private String render() {
3097 +
3098 + backgroundLayer();
3099 +
3100 + renderStorageShapes();
3101 + renderRestOfShapes();
3102 + renderTexts();
3103 +
3104 + return " <g stroke-width='1' stroke-linecap='square' stroke-linejoin='round'>\n" +
3105 + layer0.toString() +
3106 + layer1.toString() +
3107 + layer2.toString() +
3108 + layer3.toString() +
3109 + " </g>\n";
3110 +
3111 + }
3112 +
3113 + private void renderStorageShapes() {
3114 +
3115 + ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes();
3116 +
3117 + ArrayList<DiagramShape> storageShapes = findSorageShapes(shapes);
3118 +
3119 + storageShapes.sort(new Shape3DOrderingComparator());
3120 +
3121 + for (DiagramShape shape : storageShapes) {
3122 +
3123 + GeneralPath path = shape.makeIntoRenderPath(diagram, options);
3124 +
3125 + SVGCommands commands = new SVGCommands(path);
3126 +
3127 + String fill = "none";
3128 + String color = "white";
3129 +
3130 + if(!shape.isStrokeDashed()) {
3131 +
3132 + renderShadow(commands);
3133 +
3134 + if(shape.getFillColor() != null)
3135 + fill = colorToHex(shape.getFillColor());
3136 + else
3137 + fill = colorToHex(Color.white);
3138 +
3139 + }
3140 +
3141 + renderPath(shape, commands, color, fill);
3142 +
3143 + renderPath(shape, commands, colorToHex(shape.getStrokeColor()), "none");
3144 +
3145 + }
3146 +
3147 + }
3148 +
3149 + private ArrayList<DiagramShape> findSorageShapes(ArrayList<DiagramShape> shapes) {
3150 +
3151 + ArrayList<DiagramShape> storageShapes = new ArrayList<>();
3152 +
3153 + for (DiagramShape shape : shapes) {
3154 +
3155 + if(shape.getType() == DiagramShape.TYPE_STORAGE) {
3156 + storageShapes.add(shape);
3157 + }
3158 +
3159 + }
3160 +
3161 + return storageShapes;
3162 +
3163 + }
3164 +
3165 + private void renderRestOfShapes() {
3166 +
3167 + ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes();
3168 + ArrayList<DiagramShape> pointMarkers = new ArrayList<>();
3169 +
3170 + shapes.sort(new ShapeAreaComparator());
3171 +
3172 + for (DiagramShape shape : shapes) {
3173 +
3174 + if (shape.getType() == DiagramShape.TYPE_POINT_MARKER) {
3175 + pointMarkers.add(shape);
3176 + continue;
3177 + }
3178 + if (shape.getType() == DiagramShape.TYPE_STORAGE) {
3179 + continue;
3180 + }
3181 + if (shape.getType() == DiagramShape.TYPE_CUSTOM) {
3182 + //renderCustomShape(shape, g2);
3183 + //continue;
3184 + throw new RuntimeException("Not yet implemented");
3185 + }
3186 +
3187 + if (shape.getPoints().isEmpty()) continue;
3188 +
3189 + GeneralPath path = shape.makeIntoRenderPath(diagram, options);
3190 +
3191 + SVGCommands commands = new SVGCommands(path);
3192 +
3193 + renderPath(shape, commands);
3194 +
3195 + }
3196 +
3197 + renderPointMarkers(pointMarkers);
3198 +
3199 + }
3200 +
3201 + private void renderPath(DiagramShape shape, SVGCommands commands) {
3202 +
3203 + String fill = "none";
3204 +
3205 + if (shape.isClosed() && !shape.isStrokeDashed()) {
3206 +
3207 + if(shape.getFillColor() != null)
3208 + fill = colorToHex(shape.getFillColor());
3209 + else
3210 + fill = "white";
3211 +
3212 + if (shape.getType() == DiagramShape.TYPE_ARROWHEAD) {
3213 + renderPath(shape, commands, "none", fill);
3214 + }
3215 +
3216 + } else if (shape.isStrokeDashed()) {
3217 + fill = "white";
3218 + }
3219 +
3220 + if (shape.getType() != DiagramShape.TYPE_ARROWHEAD) {
3221 +
3222 + if (commands.isClosed && !shape.isStrokeDashed()) {
3223 + renderShadow(commands);
3224 + }
3225 +
3226 + renderPath(shape, commands, colorToHex(shape.getStrokeColor()), fill);
3227 +
3228 + }
3229 +
3230 + }
3231 +
3232 + private void renderPath(DiagramShape shape, SVGCommands commands, String stroke, String fill) {
3233 +
3234 + String path = " <path stroke='" + stroke + "' ";
3235 +
3236 + if (shape.isStrokeDashed())
3237 + path += dashStroke;
3238 + else
3239 + path += normalStroke;
3240 +
3241 + path += "fill='" + fill + "' d='" + commands.svgPath + "' />\n";
3242 +
3243 + layer2.append(path);
3244 +
3245 + }
3246 +
3247 + private void renderShadow(SVGCommands commands) {
3248 +
3249 + if (!options.dropShadows()) return;
3250 +
3251 + String path = " <path stroke='gray' fill='gray' filter='url(#f2)' d='" + commands.svgPath + "' />\n";
3252 +
3253 + layer1.append(path);
3254 +
3255 + }
3256 +
3257 + private void renderPointMarkers(ArrayList<DiagramShape> pointMarkers) {
3258 +
3259 + for (DiagramShape shape : pointMarkers) {
3260 +
3261 + GeneralPath path = shape.makeIntoRenderPath(diagram, options);
3262 +
3263 + String fill = "white";
3264 +
3265 + if(shape.getFillColor() != null)
3266 + fill = colorToHex(shape.getFillColor());
3267 +
3268 + renderPath(shape, new SVGCommands(path), colorToHex(shape.getStrokeColor()), fill);
3269 +
3270 + }
3271 +
3272 + }
3273 +
3274 + private String antialiasing() {
3275 + String rendering = options.performAntialias() ? "geometricPrecision" : "optimizeSpeed";
3276 + return String.format("shape-rendering='%s'", rendering);
3277 + }
3278 +
3279 + private void backgroundLayer() {
3280 +
3281 + Color color = options.getBackgroundColor();
3282 +
3283 + if (color.getAlpha() == 0) return;
3284 +
3285 + layer0.append (
3286 + String.format(" <rect x='0' y='0' width='%d' height='%d' style='fill: %s'/>\n",
3287 + diagram.getWidth(),
3288 + diagram.getHeight(),
3289 + colorToHex(color)
3290 + )
3291 + );
3292 +
3293 + }
3294 +
3295 + private void renderTexts() {
3296 +
3297 + for (DiagramText diagramText : diagram.getTextObjects()) {
3298 +
3299 + Font font = diagramText.getFont();
3300 + String text = diagramText.getText();
3301 +
3302 + int xPos = diagramText.getXPos();
3303 + int yPos = diagramText.getYPos();
3304 +
3305 + renderText(text, xPos, yPos, font, diagramText.getColor());
3306 +
3307 + if (diagramText.hasOutline()) {
3308 +
3309 + Color outlineColor = diagramText.getOutlineColor();
3310 +
3311 + renderText(text, xPos + 1, yPos, font, outlineColor);
3312 + renderText(text, xPos - 1, yPos, font, outlineColor);
3313 + renderText(text, xPos, yPos + 1, font, outlineColor);
3314 + renderText(text, xPos, yPos - 1, font, outlineColor);
3315 +
3316 + }
3317 + }
3318 +
3319 + }
3320 +
3321 + private void renderText(String text, int xPos, int yPos, Font font, Color color) {
3322 +
3323 + String TEXT_ELEMENT = " <text x='%d' y='%d' font-family='%s' font-size='%d' stroke='none' fill='%s' >" +
3324 + "<![CDATA[%s]]></text>\n";
3325 +
3326 + /* Prefer normal font weight
3327 + if (font.isBold()) {
3328 + style = " font-weight='bold'";
3329 + }
3330 + */
3331 +
3332 + layer3.append(
3333 + String.format(TEXT_ELEMENT,
3334 + xPos,
3335 + yPos,
3336 + options.getFontFamily(),
3337 + font.getSize(),
3338 + colorToHex(color),
3339 + text
3340 + )
3341 + );
3342 +
3343 + }
3344 +
3345 + private static String colorToHex(Color color) {
3346 + return String.format("#%s%s%s",
3347 + toHex(color.getRed()),
3348 + toHex(color.getGreen()),
3349 + toHex(color.getBlue())
3350 + );
3351 + }
3352 +
3353 + private static String toHex(int n) {
3354 + String hex = Integer.toHexString(n);
3355 +
3356 + return n > 15 ? hex : "0" + hex;
3357 + }
3358 +
3359 + private final Diagram diagram;
3360 + private final RenderingOptions options;
3361 +
3362 + private final StringBuilder layer0 = new StringBuilder();
3363 + private final StringBuilder layer1 = new StringBuilder();
3364 + private final StringBuilder layer2 = new StringBuilder();
3365 + private final StringBuilder layer3 = new StringBuilder();
3366 +
3367 + private final String normalStroke;
3368 + private final String dashStroke;
3369 +
3370 + class SVGCommands {
3371 +
3372 + final String svgPath;
3373 + final boolean isClosed;
3374 +
3375 + SVGCommands(GeneralPath path) {
3376 +
3377 + boolean closed = false;
3378 +
3379 + float[] coords = new float[6];
3380 +
3381 + StringBuilder builder = new StringBuilder();
3382 +
3383 + PathIterator pathIterator = path.getPathIterator(null);
3384 +
3385 + while (!pathIterator.isDone()) {
3386 +
3387 + String commands;
3388 +
3389 + switch(pathIterator.currentSegment(coords)) {
3390 + case PathIterator.SEG_MOVETO:
3391 + commands = "M" + coords[0] + " " + coords[1] + " ";
3392 + break;
3393 + case PathIterator.SEG_LINETO:
3394 + commands = "L" + coords[0] + " " + coords[1] + " ";
3395 + break;
3396 + case PathIterator.SEG_QUADTO:
3397 + commands = "Q" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " ";
3398 + break;
3399 + case PathIterator.SEG_CUBICTO:
3400 + commands = "C" + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3] + " " + coords[4] + " " + coords[5] + " ";
3401 + break;
3402 + case PathIterator.SEG_CLOSE:
3403 + commands = "z";
3404 + closed = true;
3405 + break;
3406 + default:
3407 + commands = "";
3408 + break;
3409 + }
3410 +
3411 + builder.append(commands);
3412 +
3413 + pathIterator.next();
3414 +
3415 + }
3416 +
3417 + isClosed = closed;
3418 + svgPath = builder.toString();
3419 +
3420 + }
3421 +
3422 + }
3423 +
3424 +}
3425 diff --git a/src/org/stathissideris/ascii2image/graphics/SVGRenderer.java b/src/org/stathissideris/ascii2image/graphics/SVGRenderer.java
3426 new file mode 100644
3427 index 0000000..edf8947
3428 --- /dev/null
3429 +++ b/src/org/stathissideris/ascii2image/graphics/SVGRenderer.java
3430 @@ -0,0 +1,18 @@
3431 +package org.stathissideris.ascii2image.graphics;
3432 +
3433 +import org.stathissideris.ascii2image.core.RenderingOptions;
3434 +
3435 +/**
3436 + * Created by Jean Lazarou.
3437 + */
3438 +public class SVGRenderer {
3439 +
3440 + public String renderToImage(Diagram diagram, RenderingOptions options) {
3441 +
3442 + SVGBuilder builder = new SVGBuilder(diagram, options);
3443 +
3444 + return builder.build();
3445 +
3446 + }
3447 +
3448 +}
3449 diff --git a/src/org/stathissideris/ascii2image/graphics/ShapeEdge.java b/src/org/stathissideris/ascii2image/graphics/ShapeEdge.java
3450 index c02b353..7ae5231 100644
3451 --- a/src/org/stathissideris/ascii2image/graphics/ShapeEdge.java
3452 +++ b/src/org/stathissideris/ascii2image/graphics/ShapeEdge.java
3453 @@ -1,21 +1,20 @@
3454 -/*
3455 - * DiTAA - Diagrams Through Ascii Art
3456 +/**
3457 + * ditaa - Diagrams Through Ascii Art
3458 *
3459 - * Copyright (C) 2004 Efstathios Sideris
3460 + * Copyright (C) 2004-2011 Efstathios Sideris
3461 *
3462 - * This program is free software; you can redistribute it and/or
3463 - * modify it under the terms of the GNU General Public License
3464 - * as published by the Free Software Foundation; either version 2
3465 - * of the License, or (at your option) any later version.
3466 + * ditaa is free software: you can redistribute it and/or modify
3467 + * it under the terms of the GNU Lesser General Public License as
3468 + * published by the Free Software Foundation, either version 3 of
3469 + * the License, or (at your option) any later version.
3470 *
3471 - * This program is distributed in the hope that it will be useful,
3472 + * ditaa is distributed in the hope that it will be useful,
3473 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3474 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3475 - * GNU General Public License for more details.
3476 + * GNU Lesser General Public License for more details.
3477 *
3478 - * You should have received a copy of the GNU General Public License
3479 - * along with this program; if not, write to the Free Software
3480 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3481 + * You should have received a copy of the GNU Lesser General Public
3482 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3483 *
3484 */
3485 package org.stathissideris.ascii2image.graphics;
3486 @@ -66,7 +65,7 @@ public class ShapeEdge {
3487 public void moveInwardsBy(float offset){
3488 int type = this.getType();
3489 if(type == TYPE_SLOPED)
3490 - throw new RuntimeException("Cannot move a sloped egde inwards: "+this);
3491 + throw new RuntimeException("Cannot move a sloped edge inwards: "+this);
3492
3493 float xOffset = 0;
3494 float yOffset = 0;
3495 @@ -222,7 +221,7 @@ public class ShapeEdge {
3496
3497 /**
3498 * if horizontal flips start and end points so that start is left of end
3499 - * if verical flips start and end points so that start is over of end
3500 + * if vertical flips start and end points so that start is over of end
3501 *
3502 */
3503 private void fixDirection(){
3504 @@ -231,7 +230,7 @@ public class ShapeEdge {
3505 } else if(isVertical()) {
3506 if(startPoint.y > endPoint.y) flipDirection();
3507 } else {
3508 - throw new RuntimeException("Cannot fix direction of sloped egde");
3509 + throw new RuntimeException("Cannot fix direction of sloped edge");
3510 }
3511 }
3512
3513 diff --git a/src/org/stathissideris/ascii2image/graphics/ShapePoint.java b/src/org/stathissideris/ascii2image/graphics/ShapePoint.java
3514 index 8ea9c17..e752ce2 100644
3515 --- a/src/org/stathissideris/ascii2image/graphics/ShapePoint.java
3516 +++ b/src/org/stathissideris/ascii2image/graphics/ShapePoint.java
3517 @@ -1,25 +1,25 @@
3518 -/*
3519 - * DiTAA - Diagrams Through Ascii Art
3520 +/**
3521 + * ditaa - Diagrams Through Ascii Art
3522 *
3523 - * Copyright (C) 2004 Efstathios Sideris
3524 + * Copyright (C) 2004-2011 Efstathios Sideris
3525 *
3526 - * This program is free software; you can redistribute it and/or
3527 - * modify it under the terms of the GNU General Public License
3528 - * as published by the Free Software Foundation; either version 2
3529 - * of the License, or (at your option) any later version.
3530 + * ditaa is free software: you can redistribute it and/or modify
3531 + * it under the terms of the GNU Lesser General Public License as
3532 + * published by the Free Software Foundation, either version 3 of
3533 + * the License, or (at your option) any later version.
3534 *
3535 - * This program is distributed in the hope that it will be useful,
3536 + * ditaa is distributed in the hope that it will be useful,
3537 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3538 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3539 - * GNU General Public License for more details.
3540 + * GNU Lesser General Public License for more details.
3541 *
3542 - * You should have received a copy of the GNU General Public License
3543 - * along with this program; if not, write to the Free Software
3544 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3545 + * You should have received a copy of the GNU Lesser General Public
3546 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3547 *
3548 */
3549 package org.stathissideris.ascii2image.graphics;
3550
3551 +import java.awt.geom.Point2D.Float;
3552
3553 /**
3554 *
3555 diff --git a/src/org/stathissideris/ascii2image/text/AbstractCell.java b/src/org/stathissideris/ascii2image/text/AbstractCell.java
3556 index c3ae969..276bd72 100644
3557 --- a/src/org/stathissideris/ascii2image/text/AbstractCell.java
3558 +++ b/src/org/stathissideris/ascii2image/text/AbstractCell.java
3559 @@ -1,21 +1,20 @@
3560 -/*
3561 - * DiTAA - Diagrams Through Ascii Art
3562 +/**
3563 + * ditaa - Diagrams Through Ascii Art
3564 *
3565 - * Copyright (C) 2004 Efstathios Sideris
3566 + * Copyright (C) 2004-2011 Efstathios Sideris
3567 *
3568 - * This program is free software; you can redistribute it and/or
3569 - * modify it under the terms of the GNU General Public License
3570 - * as published by the Free Software Foundation; either version 2
3571 - * of the License, or (at your option) any later version.
3572 + * ditaa is free software: you can redistribute it and/or modify
3573 + * it under the terms of the GNU Lesser General Public License as
3574 + * published by the Free Software Foundation, either version 3 of
3575 + * the License, or (at your option) any later version.
3576 *
3577 - * This program is distributed in the hope that it will be useful,
3578 + * ditaa is distributed in the hope that it will be useful,
3579 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3580 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3581 - * GNU General Public License for more details.
3582 + * GNU Lesser General Public License for more details.
3583 *
3584 - * You should have received a copy of the GNU General Public License
3585 - * along with this program; if not, write to the Free Software
3586 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3587 + * You should have received a copy of the GNU Lesser General Public
3588 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3589 *
3590 */
3591 package org.stathissideris.ascii2image.text;
3592 diff --git a/src/org/stathissideris/ascii2image/text/AbstractionGrid.java b/src/org/stathissideris/ascii2image/text/AbstractionGrid.java
3593 index c5bb56b..f226269 100644
3594 --- a/src/org/stathissideris/ascii2image/text/AbstractionGrid.java
3595 +++ b/src/org/stathissideris/ascii2image/text/AbstractionGrid.java
3596 @@ -1,21 +1,20 @@
3597 -/*
3598 - * DiTAA - Diagrams Through Ascii Art
3599 +/**
3600 + * ditaa - Diagrams Through Ascii Art
3601 *
3602 - * Copyright (C) 2004 Efstathios Sideris
3603 + * Copyright (C) 2004-2011 Efstathios Sideris
3604 *
3605 - * This program is free software; you can redistribute it and/or
3606 - * modify it under the terms of the GNU General Public License
3607 - * as published by the Free Software Foundation; either version 2
3608 - * of the License, or (at your option) any later version.
3609 + * ditaa is free software: you can redistribute it and/or modify
3610 + * it under the terms of the GNU Lesser General Public License as
3611 + * published by the Free Software Foundation, either version 3 of
3612 + * the License, or (at your option) any later version.
3613 *
3614 - * This program is distributed in the hope that it will be useful,
3615 + * ditaa is distributed in the hope that it will be useful,
3616 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3617 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3618 - * GNU General Public License for more details.
3619 + * GNU Lesser General Public License for more details.
3620 *
3621 - * You should have received a copy of the GNU General Public License
3622 - * along with this program; if not, write to the Free Software
3623 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3624 + * You should have received a copy of the GNU Lesser General Public
3625 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3626 *
3627 */
3628 package org.stathissideris.ascii2image.text;
3629 @@ -147,15 +146,15 @@ public class AbstractionGrid {
3630 return result;
3631 }
3632
3633 - public ArrayList getDistinctShapes(){
3634 - ArrayList result = new ArrayList();
3635 + public ArrayList<CellSet> getDistinctShapes(){
3636 + ArrayList<CellSet> result = new ArrayList<CellSet>();
3637
3638 CellSet nonBlank = grid.getAllNonBlank();
3639 - ArrayList distinct = nonBlank.breakIntoDistinctBoundaries();
3640 + ArrayList<CellSet> distinct = nonBlank.breakIntoDistinctBoundaries();
3641
3642 - Iterator it = distinct.iterator();
3643 + Iterator<CellSet> it = distinct.iterator();
3644 while (it.hasNext()) {
3645 - CellSet set = (CellSet) it.next();
3646 + CellSet set = it.next();
3647 AbstractionGrid temp = new AbstractionGrid(this.getWidth(), this.getHeight());
3648 temp.fillCells(set);
3649 result.add(temp.getAsTextGrid().getAllNonBlank());
3650 diff --git a/src/org/stathissideris/ascii2image/text/CellSet.java b/src/org/stathissideris/ascii2image/text/CellSet.java
3651 index fca9edf..0a40b8c 100644
3652 --- a/src/org/stathissideris/ascii2image/text/CellSet.java
3653 +++ b/src/org/stathissideris/ascii2image/text/CellSet.java
3654 @@ -1,27 +1,28 @@
3655 -/*
3656 - * DiTAA - Diagrams Through Ascii Art
3657 +/**
3658 + * ditaa - Diagrams Through Ascii Art
3659 *
3660 - * Copyright (C) 2004 Efstathios Sideris
3661 + * Copyright (C) 2004-2011 Efstathios Sideris
3662 *
3663 - * This program is free software; you can redistribute it and/or
3664 - * modify it under the terms of the GNU General Public License
3665 - * as published by the Free Software Foundation; either version 2
3666 - * of the License, or (at your option) any later version.
3667 + * ditaa is free software: you can redistribute it and/or modify
3668 + * it under the terms of the GNU Lesser General Public License as
3669 + * published by the Free Software Foundation, either version 3 of
3670 + * the License, or (at your option) any later version.
3671 *
3672 - * This program is distributed in the hope that it will be useful,
3673 + * ditaa is distributed in the hope that it will be useful,
3674 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3675 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3676 - * GNU General Public License for more details.
3677 + * GNU Lesser General Public License for more details.
3678 *
3679 - * You should have received a copy of the GNU General Public License
3680 - * along with this program; if not, write to the Free Software
3681 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3682 + * You should have received a copy of the GNU Lesser General Public
3683 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3684 *
3685 */
3686 package org.stathissideris.ascii2image.text;
3687
3688 import java.util.ArrayList;
3689 +import java.util.HashMap;
3690 import java.util.HashSet;
3691 +import java.util.Hashtable;
3692 import java.util.Iterator;
3693 import java.util.Set;
3694
3695 @@ -121,7 +122,8 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3696 public static CellSet copyCellSet(CellSet set) {
3697 TextGrid grid = new TextGrid();
3698 CellSet newSet = new CellSet();
3699 - Iterator it = set.iterator();
3700 +
3701 + Iterator<TextGrid.Cell> it = set.iterator();
3702 while (it.hasNext()) {
3703 TextGrid.Cell cell = (TextGrid.Cell) it.next();
3704 TextGrid.Cell newCell = grid.new Cell(cell);
3705 @@ -259,9 +261,6 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3706 AbstractionGrid abstraction = new AbstractionGrid(subGrid, tempSet);
3707 TextGrid temp = abstraction.getCopyOfInternalBuffer();
3708
3709 - TextGrid.Cell cell1 = null;
3710 - TextGrid.Cell cell2 = null;
3711 -
3712 int width = temp.getWidth();
3713 int height = temp.getHeight();
3714
3715 @@ -290,9 +289,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3716
3717 public void translate(int dx, int dy){
3718 typeIsValid = false;
3719 - Iterator it = iterator();
3720 + Iterator<TextGrid.Cell> it = iterator();
3721 while(it.hasNext()){
3722 - TextGrid.Cell cCell = (TextGrid.Cell) it.next();
3723 + TextGrid.Cell cCell = it.next();
3724 cCell.x += dx;
3725 cCell.y += dy;
3726 }
3727 @@ -336,9 +335,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3728 }
3729
3730 public TextGrid.Cell find(int x, int y){
3731 - Iterator it = iterator();
3732 + Iterator<TextGrid.Cell> it = iterator();
3733 while(it.hasNext()){
3734 - TextGrid.Cell cCell = (TextGrid.Cell) it.next();
3735 + TextGrid.Cell cCell = it.next();
3736 if(cCell.x == x && cCell.y == y) return cCell;
3737 }
3738 return null;
3739 @@ -379,9 +378,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3740 * @return
3741 */
3742 public TextGrid.Cell findCellNextTo(TextGrid.Cell cell){
3743 - Iterator it = iterator();
3744 + Iterator<TextGrid.Cell> it = iterator();
3745 while(it.hasNext()){
3746 - TextGrid.Cell cCell = (TextGrid.Cell) it.next();
3747 + TextGrid.Cell cCell = it.next();
3748 if(cCell.isNextTo(cell)) return cCell;
3749 }
3750 return null;
3751 @@ -396,9 +395,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3752 public CellSet findCellsNextTo(TextGrid.Cell cell){
3753 if(cell == null) throw new IllegalArgumentException("cell cannot be null");
3754 CellSet set = new CellSet();
3755 - Iterator it = iterator();
3756 + Iterator<TextGrid.Cell> it = iterator();
3757 while(it.hasNext()){
3758 - TextGrid.Cell cCell = (TextGrid.Cell) it.next();
3759 + TextGrid.Cell cCell = it.next();
3760 if(cCell.isNextTo(cell)) set.add(cCell);
3761 }
3762 return set;
3763 @@ -406,18 +405,18 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3764
3765 public void appendSet(CellSet set){
3766 typeIsValid = false;
3767 - Iterator it = set.iterator();
3768 + Iterator<TextGrid.Cell> it = set.iterator();
3769 while(it.hasNext()){
3770 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3771 + TextGrid.Cell cell = it.next();
3772 if(find(cell) == null) add(cell);
3773 }
3774 }
3775
3776 public void subtractSet(CellSet set){
3777 typeIsValid = false;
3778 - Iterator it = set.iterator();
3779 + Iterator<TextGrid.Cell> it = set.iterator();
3780 while(it.hasNext()){
3781 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3782 + TextGrid.Cell cell = it.next();
3783 TextGrid.Cell thisCell = find(cell);
3784 if(thisCell != null) remove(thisCell);
3785 }
3786 @@ -434,9 +433,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3787
3788 public int getMaxX(){
3789 int result = 0;
3790 - Iterator it = iterator();
3791 + Iterator<TextGrid.Cell> it = iterator();
3792 while(it.hasNext()){
3793 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3794 + TextGrid.Cell cell = it.next();
3795 if(cell.x > result) result = cell.x;
3796 }
3797 return result;
3798 @@ -444,9 +443,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3799
3800 public int getMinX(){
3801 int result = Integer.MAX_VALUE;
3802 - Iterator it = iterator();
3803 + Iterator<TextGrid.Cell> it = iterator();
3804 while(it.hasNext()){
3805 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3806 + TextGrid.Cell cell = it.next();
3807 if(cell.x < result) result = cell.x;
3808 }
3809 return result;
3810 @@ -455,9 +454,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3811
3812 public int getMaxY(){
3813 int result = 0;
3814 - Iterator it = iterator();
3815 + Iterator<TextGrid.Cell> it = iterator();
3816 while(it.hasNext()){
3817 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3818 + TextGrid.Cell cell = it.next();
3819 if(cell.y > result) result = cell.y;
3820 }
3821 return result;
3822 @@ -465,9 +464,9 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3823
3824 public int getMinY(){
3825 int result = Integer.MAX_VALUE;
3826 - Iterator it = iterator();
3827 + Iterator<TextGrid.Cell> it = iterator();
3828 while(it.hasNext()){
3829 - TextGrid.Cell cell = (TextGrid.Cell) it.next();
3830 + TextGrid.Cell cell = it.next();
3831 if(cell.y < result) result = cell.y;
3832 }
3833 return result;
3834 @@ -487,16 +486,16 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3835 }
3836
3837
3838 - public static ArrayList removeDuplicateSets(ArrayList list) {
3839 - ArrayList uniqueSets = new ArrayList();
3840 + public static ArrayList<CellSet> removeDuplicateSets(ArrayList<CellSet> list) {
3841 + ArrayList<CellSet> uniqueSets = new ArrayList<CellSet>();
3842
3843 - Iterator it = list.iterator();
3844 + Iterator<CellSet> it = list.iterator();
3845 while(it.hasNext()){
3846 - CellSet set = (CellSet) it.next();
3847 + CellSet set = it.next();
3848 boolean isOriginal = true;
3849 - Iterator uniquesIt = uniqueSets.iterator();
3850 + Iterator<CellSet> uniquesIt = uniqueSets.iterator();
3851 while(uniquesIt.hasNext()){
3852 - CellSet uniqueSet = (CellSet) uniquesIt.next();
3853 + CellSet uniqueSet = uniquesIt.next();
3854 if(set.equals(uniqueSet)){
3855 isOriginal = false;
3856 }
3857 @@ -512,8 +511,8 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3858 *
3859 * @return ArrayList of distinct BoundarySetS
3860 */
3861 - public ArrayList breakIntoDistinctBoundaries(TextGrid grid){
3862 - ArrayList result;
3863 + public ArrayList<CellSet> breakIntoDistinctBoundaries(TextGrid grid){
3864 + ArrayList<CellSet> result;
3865
3866 AbstractionGrid temp = new AbstractionGrid(grid, this);
3867 result = temp.getDistinctShapes();
3868 @@ -526,8 +525,8 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3869 *
3870 * @return ArrayList of distinct BoundarySetS
3871 */
3872 - public ArrayList breakIntoDistinctBoundaries(){
3873 - ArrayList result = new ArrayList();
3874 + public ArrayList<CellSet> breakIntoDistinctBoundaries(){
3875 + ArrayList<CellSet> result = new ArrayList<CellSet>();
3876
3877 //CellSet tempSet = copyCellSet(this);
3878 //tempSet.translate( - this.getMinX() + 1, - this.getMinY() + 1);
3879 @@ -539,7 +538,7 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3880 boundaryGrid.fillCellsWith(this, '*');
3881
3882
3883 - Iterator it = iterator();
3884 + Iterator<TextGrid.Cell> it = iterator();
3885 while(it.hasNext()){
3886 TextGrid.Cell cell = (TextGrid.Cell) it.next();
3887 if(boundaryGrid.isBlank(cell.x, cell.y)) continue;
3888 @@ -579,8 +578,8 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3889 * @return a list of boundaries that are either open or closed but not mixed
3890 * and they are equivalent to the <code>this</code>
3891 */
3892 - public ArrayList breakTrulyMixedBoundaries(TextGrid grid){
3893 - ArrayList result = new ArrayList();
3894 + public ArrayList<CellSet> breakTrulyMixedBoundaries(TextGrid grid){
3895 + ArrayList<CellSet> result = new ArrayList<CellSet>();
3896 CellSet visitedEnds = new CellSet();
3897
3898 TextGrid workGrid = TextGrid.makeSameSizeAs(grid);
3899 @@ -591,7 +590,7 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3900 workGrid.printDebug();
3901 }
3902
3903 - Iterator it = iterator();
3904 + Iterator<TextGrid.Cell> it = iterator();
3905 while(it.hasNext()){
3906 TextGrid.Cell start = (TextGrid.Cell) it.next();
3907 if(workGrid.isLinesEnd(start) && !visitedEnds.contains(start)){
3908 @@ -641,9 +640,7 @@ public class CellSet implements Iterable<TextGrid.Cell> {
3909
3910 //substract all boundary sets from this CellSet
3911 CellSet whatsLeft = new CellSet(this);
3912 - it = result.iterator();
3913 - while (it.hasNext()) {
3914 - CellSet set = (CellSet) it.next();
3915 + for(CellSet set : result) {
3916 whatsLeft.subtractSet(set);
3917 if(DEBUG) set.printAsGrid();
3918 }
3919 diff --git a/src/org/stathissideris/ascii2image/text/GridPattern.java b/src/org/stathissideris/ascii2image/text/GridPattern.java
3920 index 0a53a39..bb96452 100644
3921 --- a/src/org/stathissideris/ascii2image/text/GridPattern.java
3922 +++ b/src/org/stathissideris/ascii2image/text/GridPattern.java
3923 @@ -1,21 +1,20 @@
3924 -/*
3925 - * DiTAA - Diagrams Through Ascii Art
3926 +/**
3927 + * ditaa - Diagrams Through Ascii Art
3928 *
3929 - * Copyright (C) 2004 Efstathios Sideris
3930 + * Copyright (C) 2004-2011 Efstathios Sideris
3931 *
3932 - * This program is free software; you can redistribute it and/or
3933 - * modify it under the terms of the GNU General Public License
3934 - * as published by the Free Software Foundation; either version 2
3935 - * of the License, or (at your option) any later version.
3936 + * ditaa is free software: you can redistribute it and/or modify
3937 + * it under the terms of the GNU Lesser General Public License as
3938 + * published by the Free Software Foundation, either version 3 of
3939 + * the License, or (at your option) any later version.
3940 *
3941 - * This program is distributed in the hope that it will be useful,
3942 + * ditaa is distributed in the hope that it will be useful,
3943 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3944 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3945 - * GNU General Public License for more details.
3946 + * GNU Lesser General Public License for more details.
3947 *
3948 - * You should have received a copy of the GNU General Public License
3949 - * along with this program; if not, write to the Free Software
3950 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3951 + * You should have received a copy of the GNU Lesser General Public
3952 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
3953 *
3954 */
3955 package org.stathissideris.ascii2image.text;
3956 @@ -126,7 +125,7 @@ public class GridPattern extends TextGrid {
3957 if (DEBUG)
3958 System.out.println("Trying to match:");
3959 if(!usesStandardSyntax){
3960 - Iterator<StringBuffer> it = getRows().iterator();
3961 + Iterator<StringBuilder> it = getRows().iterator();
3962 while (it.hasNext()) {
3963 String row = it.next().toString();
3964 regExps.add(Pattern.compile(makeRegExp(row)));
3965 @@ -134,7 +133,7 @@ public class GridPattern extends TextGrid {
3966 System.out.println(row+" becomes "+makeRegExp(row));
3967 }
3968 } else {
3969 - Iterator<StringBuffer> it = getRows().iterator();
3970 + Iterator<StringBuilder> it = getRows().iterator();
3971 while (it.hasNext()) {
3972 String row = it.next().toString();
3973 regExps.add(Pattern.compile(row));
3974 @@ -152,7 +151,7 @@ public class GridPattern extends TextGrid {
3975 } else if(c == '|') {
3976 result.append("[|:]");
3977 } else if(c == '-') {
3978 - result.append("-");
3979 + result.append("[-=]");
3980 } else if(c == '!') {
3981 result.append("[^-=\\/\\\\+|:]");
3982 } else if(c == 'b') {
3983 diff --git a/src/org/stathissideris/ascii2image/text/GridPatternGroup.java b/src/org/stathissideris/ascii2image/text/GridPatternGroup.java
3984 index 96b8613..0d8e184 100644
3985 --- a/src/org/stathissideris/ascii2image/text/GridPatternGroup.java
3986 +++ b/src/org/stathissideris/ascii2image/text/GridPatternGroup.java
3987 @@ -1,21 +1,20 @@
3988 -/*
3989 - * DiTAA - Diagrams Through Ascii Art
3990 +/**
3991 + * ditaa - Diagrams Through Ascii Art
3992 *
3993 - * Copyright (C) 2004 Efstathios Sideris
3994 + * Copyright (C) 2004-2011 Efstathios Sideris
3995 *
3996 - * This program is free software; you can redistribute it and/or
3997 - * modify it under the terms of the GNU General Public License
3998 - * as published by the Free Software Foundation; either version 2
3999 - * of the License, or (at your option) any later version.
4000 + * ditaa is free software: you can redistribute it and/or modify
4001 + * it under the terms of the GNU Lesser General Public License as
4002 + * published by the Free Software Foundation, either version 3 of
4003 + * the License, or (at your option) any later version.
4004 *
4005 - * This program is distributed in the hope that it will be useful,
4006 + * ditaa is distributed in the hope that it will be useful,
4007 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4008 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4009 - * GNU General Public License for more details.
4010 + * GNU Lesser General Public License for more details.
4011 *
4012 - * You should have received a copy of the GNU General Public License
4013 - * along with this program; if not, write to the Free Software
4014 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4015 + * You should have received a copy of the GNU Lesser General Public
4016 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
4017 *
4018 */
4019 package org.stathissideris.ascii2image.text;
4020 diff --git a/src/org/stathissideris/ascii2image/text/StringUtils.java b/src/org/stathissideris/ascii2image/text/StringUtils.java
4021 index 4a32be7..e753245 100644
4022 --- a/src/org/stathissideris/ascii2image/text/StringUtils.java
4023 +++ b/src/org/stathissideris/ascii2image/text/StringUtils.java
4024 @@ -1,21 +1,20 @@
4025 -/*
4026 - * DiTAA - Diagrams Through Ascii Art
4027 +/**
4028 + * ditaa - Diagrams Through Ascii Art
4029 *
4030 - * Copyright (C) 2004 Efstathios Sideris
4031 + * Copyright (C) 2004-2011 Efstathios Sideris
4032 *
4033 - * This program is free software; you can redistribute it and/or
4034 - * modify it under the terms of the GNU General Public License
4035 - * as published by the Free Software Foundation; either version 2
4036 - * of the License, or (at your option) any later version.
4037 + * ditaa is free software: you can redistribute it and/or modify
4038 + * it under the terms of the GNU Lesser General Public License as
4039 + * published by the Free Software Foundation, either version 3 of
4040 + * the License, or (at your option) any later version.
4041 *
4042 - * This program is distributed in the hope that it will be useful,
4043 + * ditaa is distributed in the hope that it will be useful,
4044 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4045 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4046 - * GNU General Public License for more details.
4047 + * GNU Lesser General Public License for more details.
4048 *
4049 - * You should have received a copy of the GNU General Public License
4050 - * along with this program; if not, write to the Free Software
4051 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4052 + * You should have received a copy of the GNU Lesser General Public
4053 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
4054 *
4055 */
4056 package org.stathissideris.ascii2image.text;
4057 @@ -91,7 +90,7 @@ public class StringUtils {
4058 indexes[0] = 0;
4059 int k = 1;
4060
4061 - //then we find the indexes (we have ckecked the first char already)
4062 + //then we find the indexes (we have checked the first char already)
4063 for(int j =1; j < string.length(); j++){
4064 if(Character.isUpperCase(string.charAt(j))) indexes[k++] = j;
4065 }
4066 diff --git a/src/org/stathissideris/ascii2image/text/TextGrid.java b/src/org/stathissideris/ascii2image/text/TextGrid.java
4067 index 40a635f..1ead278 100644
4068 --- a/src/org/stathissideris/ascii2image/text/TextGrid.java
4069 +++ b/src/org/stathissideris/ascii2image/text/TextGrid.java
4070 @@ -1,41 +1,33 @@
4071 -/*
4072 - * DiTAA - Diagrams Through Ascii Art
4073 +/**
4074 + * ditaa - Diagrams Through Ascii Art
4075 *
4076 - * Copyright (C) 2004 Efstathios Sideris
4077 + * Copyright (C) 2004-2011 Efstathios Sideris
4078 *
4079 - * This program is free software; you can redistribute it and/or
4080 - * modify it under the terms of the GNU General Public License
4081 - * as published by the Free Software Foundation; either version 2
4082 - * of the License, or (at your option) any later version.
4083 + * ditaa is free software: you can redistribute it and/or modify
4084 + * it under the terms of the GNU Lesser General Public License as
4085 + * published by the Free Software Foundation, either version 3 of
4086 + * the License, or (at your option) any later version.
4087 *
4088 - * This program is distributed in the hope that it will be useful,
4089 + * ditaa is distributed in the hope that it will be useful,
4090 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4091 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4092 - * GNU General Public License for more details.
4093 + * GNU Lesser General Public License for more details.
4094 *
4095 - * You should have received a copy of the GNU General Public License
4096 - * along with this program; if not, write to the Free Software
4097 - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4098 + * You should have received a copy of the GNU Lesser General Public
4099 + * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
4100 *
4101 */
4102 package org.stathissideris.ascii2image.text;
4103
4104 import java.awt.Color;
4105 -import java.io.File;
4106 -import java.io.FileNotFoundException;
4107 -import java.io.IOException;
4108 -import java.io.UnsupportedEncodingException;
4109 -import java.util.ArrayList;
4110 -import java.util.Collection;
4111 -import java.util.HashMap;
4112 -import java.util.HashSet;
4113 -import java.util.Iterator;
4114 -import java.util.Stack;
4115 +import java.io.*;
4116 +import java.util.*;
4117 import java.util.regex.Matcher;
4118 import java.util.regex.Pattern;
4119
4120 import org.stathissideris.ascii2image.core.FileUtils;
4121 import org.stathissideris.ascii2image.core.ProcessingOptions;
4122 +import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
4123
4124
4125 /**
4126 @@ -46,7 +38,7 @@ public class TextGrid {
4127
4128 private static final boolean DEBUG = false;
4129
4130 - private ArrayList<StringBuffer> rows;
4131 + private ArrayList<StringBuilder> rows;
4132
4133 private static char[] boundaries = {'/', '\\', '|', '-', '*', '=', ':'};
4134 private static char[] undisputableBoundaries = {'|', '-', '*', '=', ':'};
4135 @@ -113,14 +105,14 @@ public class TextGrid {
4136
4137
4138 public TextGrid(){
4139 - rows = new ArrayList<StringBuffer>();
4140 + rows = new ArrayList<StringBuilder>();
4141 }
4142
4143 public TextGrid(int width, int height){
4144 String space = StringUtils.repeatString(" ", width);
4145 - rows = new ArrayList<StringBuffer>();
4146 + rows = new ArrayList<StringBuilder>();
4147 for(int i = 0; i < height; i++)
4148 - rows.add(new StringBuffer(space));
4149 + rows.add(new StringBuilder(space));
4150 }
4151
4152 public static TextGrid makeSameSizeAs(TextGrid grid){
4153 @@ -129,9 +121,9 @@ public class TextGrid {
4154
4155
4156 public TextGrid(TextGrid otherGrid){
4157 - rows = new ArrayList<StringBuffer>();
4158 - for(StringBuffer row : otherGrid.getRows()) {
4159 - rows.add(new StringBuffer(row));
4160 + rows = new ArrayList<StringBuilder>();
4161 + for(StringBuilder row : otherGrid.getRows()) {
4162 + rows.add(new StringBuilder(row));
4163 }
4164 }
4165
4166 @@ -140,7 +132,7 @@ public class TextGrid {
4167 int height = getHeight();
4168 rows.clear();
4169 for(int i = 0; i < height; i++)
4170 - rows.add(new StringBuffer(blank));
4171 + rows.add(new StringBuilder(blank));
4172 }
4173
4174 // duplicated code due to lots of hits to this function
4175 @@ -161,14 +153,14 @@ public class TextGrid {
4176 return rows.get(cell.y).charAt(cell.x);
4177 }
4178
4179 - public StringBuffer getRow(int y){
4180 + public StringBuilder getRow(int y){
4181 return rows.get(y);
4182 }
4183
4184 public TextGrid getSubGrid(int x, int y, int width, int height){
4185 TextGrid grid = new TextGrid(width, height);
4186 for(int i = 0; i < height; i++){
4187 - grid.setRow(i, new StringBuffer(getRow(y + i).subSequence(x, x + width)));
4188 + grid.setRow(i, new StringBuilder(getRow(y + i).subSequence(x, x + width)));
4189 }
4190 return grid;
4191 }
4192 @@ -217,17 +209,17 @@ public class TextGrid {
4193
4194 public void set(int x, int y, char c){
4195 if(x > getWidth() - 1 || y > getHeight() - 1) return;
4196 - StringBuffer row = rows.get(y);
4197 + StringBuilder row = rows.get(y);
4198 row.setCharAt(x, c);
4199 }
4200
4201 public void setRow(int y, String row){
4202 if(y > getHeight() || row.length() != getWidth())
4203 throw new IllegalArgumentException("setRow out of bounds or string wrong size");
4204 - rows.set(y, new StringBuffer(row));
4205 + rows.set(y, new StringBuilder(row));
4206 }
4207
4208 - public void setRow(int y, StringBuffer row){
4209 + public void setRow(int y, StringBuilder row){
4210 if(y > getHeight() || row.length() != getWidth())
4211 throw new IllegalArgumentException("setRow out of bounds or string wrong size");
4212 rows.set(y, row);
4213 @@ -243,7 +235,7 @@ public class TextGrid {
4214 }
4215
4216 public void printDebug(){
4217 - Iterator<StringBuffer> it = rows.iterator();
4218 + Iterator<StringBuilder> it = rows.iterator();
4219 int i = 0;
4220 System.out.println(
4221 " "
4222 @@ -258,8 +250,8 @@ public class TextGrid {
4223 }
4224
4225 public String getDebugString(){
4226 - StringBuffer buffer = new StringBuffer();
4227 - Iterator<StringBuffer> it = rows.iterator();
4228 + StringBuilder buffer = new StringBuilder();
4229 + Iterator<StringBuilder> it = rows.iterator();
4230 int i = 0;
4231 buffer.append(
4232 " "
4233 @@ -397,7 +389,7 @@ public class TextGrid {
4234 humanCode = "c" + humanCode;
4235 hexCode = "c" + hexCode;
4236 row = row.replaceAll(humanCode, hexCode);
4237 - rows.set(y, new StringBuffer(row)); //TODO: this is not the most efficient way to do this
4238 + rows.set(y, new StringBuilder(row)); //TODO: this is not the most efficient way to do this
4239 row = rows.get(y).toString();
4240 }
4241 }
4242 @@ -406,7 +398,7 @@ public class TextGrid {
4243
4244
4245 /**
4246 - * Replace all occurences of c1 with c2
4247 + * Replace all occurrences of c1 with c2
4248 *
4249 * @param c1
4250 * @param c2
4251 @@ -612,7 +604,7 @@ public class TextGrid {
4252 }
4253
4254 //remove in two stages, because decision of
4255 - //isBoundary depends on contants of surrounding
4256 + //isBoundary depends on content of surrounding
4257 //cells
4258 Iterator it = toBeRemoved.iterator();
4259 while(it.hasNext()){
4260 @@ -621,8 +613,8 @@ public class TextGrid {
4261 }
4262 }
4263
4264 - public ArrayList findArrowheads(){
4265 - ArrayList result = new ArrayList();
4266 + public ArrayList<Cell> findArrowheads(){
4267 + ArrayList<Cell> result = new ArrayList<Cell>();
4268 int width = getWidth();
4269 int height = getHeight();
4270 for(int yi = 0; yi < height; yi++){
4271 @@ -738,7 +730,7 @@ public class TextGrid {
4272 char c = get(cell.x, cell.y);
4273 if(0 == c) return false;
4274 if('+' == c || '\\' == c || '/' == c){
4275 - // System.out.print("");
4276 + System.out.print("");
4277 if(
4278 isIntersection(cell)
4279 || isCorner(cell)
4280 @@ -1132,7 +1124,7 @@ public class TextGrid {
4281 if(isLine(cell)) return followLine(cell, blocked);
4282 if(isStub(cell)) return followStub(cell, blocked);
4283 if(isCrossOnLine(cell)) return followCrossOnLine(cell, blocked);
4284 - System.err.println("Umbiguous input at position "+cell+":");
4285 + System.err.println("Ambiguous input at position "+cell+":");
4286 TextGrid subGrid = getTestingSubGrid(cell);
4287 subGrid.printDebug();
4288 throw new RuntimeException("Cannot follow cell "+cell+": cannot determine cell type");
4289 @@ -1287,15 +1279,6 @@ public class TextGrid {
4290 return true;
4291 }
4292
4293 -// @Override
4294 -// public int hashCode() {
4295 -// int h = 0;
4296 -// for(StringBuffer row : rows) {
4297 -// h += row.hashCode();
4298 -// }
4299 -// return h;
4300 -// }
4301 -
4302 /**
4303 * Fills all the cells in <code>cells</code> with <code>c</code>
4304 *
4305 @@ -1522,34 +1505,39 @@ public class TextGrid {
4306 {
4307
4308 String encoding = (options == null) ? null : options.getCharacterEncoding();
4309 - ArrayList<StringBuffer> lines = new ArrayList<StringBuffer>();
4310 - String[] linesArray = FileUtils.readFile(new File(filename), encoding).split("(\r)?\n");
4311 + ArrayList<StringBuilder> lines = new ArrayList<StringBuilder>();
4312 + InputStream is;
4313 + if ("-".equals(filename))
4314 + is = System.in;
4315 + else
4316 + is = new FileInputStream(filename);
4317 + String[] linesArray = FileUtils.readFile(is, filename, encoding).split("(\r)?\n");
4318 for(int i = 0; i < linesArray.length; i++)
4319 - lines.add(new StringBuffer(linesArray[i]));
4320 + lines.add(new StringBuilder(linesArray[i]));
4321
4322 return initialiseWithLines(lines, options);
4323 }
4324
4325 public boolean initialiseWithText(String text, ProcessingOptions options) throws UnsupportedEncodingException {
4326
4327 - ArrayList<StringBuffer> lines = new ArrayList<StringBuffer>();
4328 + ArrayList<StringBuilder> lines = new ArrayList<StringBuilder>();
4329 String[] linesArray = text.split("(\r)?\n");
4330 for(int i = 0; i < linesArray.length; i++)
4331 - lines.add(new StringBuffer(linesArray[i]));
4332 + lines.add(new StringBuilder(linesArray[i]));
4333
4334 return initialiseWithLines(lines, options);
4335 }
4336
4337 - public boolean initialiseWithLines(ArrayList<StringBuffer> lines, ProcessingOptions options) throws UnsupportedEncodingException {
4338 + public boolean initialiseWithLines(ArrayList<StringBuilder> lines, ProcessingOptions options) throws UnsupportedEncodingException {
4339
4340 //remove blank rows at the bottom
4341 boolean done = false;
4342 int i;
4343 - for(i = lines.size() - 1; !done; i--){
4344 - StringBuffer row = lines.get(i);
4345 + for(i = lines.size() - 1; i >= 0 && !done; i--){
4346 + StringBuilder row = lines.get(i);
4347 if(!StringUtils.isBlank(row.toString())) done = true;
4348 }
4349 - rows = new ArrayList<StringBuffer>(lines.subList(0, i + 2));
4350 + rows = new ArrayList<StringBuilder>(lines.subList(0, i + 2));
4351
4352 if(options != null) fixTabs(options.getTabSize());
4353 else fixTabs(options.DEFAULT_TAB_SIZE);
4354 @@ -1565,9 +1553,9 @@ public class TextGrid {
4355 int index = 0;
4356
4357 String encoding = null;
4358 - //if(options != null) encoding = options.getCharacterEncoding();
4359 + if(options != null) encoding = options.getCharacterEncoding();
4360
4361 - Iterator<StringBuffer> it = rows.iterator();
4362 + Iterator<StringBuilder> it = rows.iterator();
4363 while(it.hasNext()){
4364 String row = it.next().toString();
4365 if(encoding != null){
4366 @@ -1575,25 +1563,25 @@ public class TextGrid {
4367 row = new String(bytes, encoding);
4368 }
4369 if(row.length() > maxLength) maxLength = row.length();
4370 - rows.set(index, new StringBuffer(row));
4371 + rows.set(index, new StringBuilder(row));
4372 index++;
4373 }
4374
4375 it = rows.iterator();
4376 - ArrayList<StringBuffer> newRows = new ArrayList<StringBuffer>();
4377 + ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>();
4378 //TODO: make the following depend on blankBorderSize
4379
4380 - StringBuffer topBottomRow =
4381 - new StringBuffer(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2));
4382 + StringBuilder topBottomRow =
4383 + new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2));
4384
4385 newRows.add(topBottomRow);
4386 newRows.add(topBottomRow);
4387 while(it.hasNext()){
4388 - StringBuffer row = it.next();
4389 + StringBuilder row = it.next();
4390
4391 if(row.length() < maxLength) {
4392 String borderString = StringUtils.repeatString(" ", blankBorderSize);
4393 - StringBuffer newRow = new StringBuffer();
4394 + StringBuilder newRow = new StringBuilder();
4395
4396 newRow.append(borderString);
4397 newRow.append(row);
4398 @@ -1602,7 +1590,7 @@ public class TextGrid {
4399
4400 newRows.add(newRow);
4401 } else { //TODO: why is the following line like that?
4402 - newRows.add(new StringBuffer(" ").append(row).append(" "));
4403 + newRows.add(new StringBuilder(" ").append(row).append(" "));
4404 }
4405 }
4406 //TODO: make the following depend on blankBorderSize
4407 @@ -1619,11 +1607,11 @@ public class TextGrid {
4408 private void fixTabs(int tabSize){
4409
4410 int rowIndex = 0;
4411 - Iterator<StringBuffer> it = rows.iterator();
4412 + Iterator<StringBuilder> it = rows.iterator();
4413
4414 while(it.hasNext()){
4415 String row = it.next().toString();
4416 - StringBuffer newRow = new StringBuffer();
4417 + StringBuilder newRow = new StringBuilder();
4418
4419 char[] chars = row.toCharArray();
4420 for(int i = 0; i < chars.length; i++){
4421 @@ -1647,7 +1635,7 @@ public class TextGrid {
4422 /**
4423 * @return
4424 */
4425 - protected ArrayList<StringBuffer> getRows() {
4426 + protected ArrayList<StringBuilder> getRows() {
4427 return rows;
4428 }
4429