| 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 |
|
| 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 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 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 |
|
| 123 |
+ // final Diagram diagram = new Diagram(grid, options); |
| 124 |
final Class<?> clDiagram = Class.forName("org.stathissideris.ascii2image.graphics.Diagram"); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 140 |
+/** |
| 141 |
+ * ditaa - Diagrams Through Ascii Art |
| 142 |
* |
| 143 |
|
| 144 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 145 |
* |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 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 |
|
| 160 |
+ * GNU Lesser General Public License for more details. |
| 161 |
* |
| 162 |
|
| 163 |
|
| 164 |
|
| 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 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 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 |
|
| 499 |
+/** |
| 500 |
+ * ditaa - Diagrams Through Ascii Art |
| 501 |
* |
| 502 |
|
| 503 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 504 |
* |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 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 |
|
| 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 |
|
| 519 |
+ * GNU Lesser General Public License for more details. |
| 520 |
* |
| 521 |
|
| 522 |
|
| 523 |
|
| 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 |
|
| 549 |
|
| 550 |
+ public ProcessingOptions processingOptions = |
| 551 |
+ new ProcessingOptions(); |
| 552 |
public RenderingOptions renderingOptions = |
| 553 |
new RenderingOptions(); |
| 554 |
|
| 555 |
public void setDebug(boolean value){ |
| 556 |
|
| 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 |
|
| 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 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 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 |
|
| 738 |
+/** |
| 739 |
+ * ditaa - Diagrams Through Ascii Art |
| 740 |
* |
| 741 |
|
| 742 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 743 |
* |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 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 |
|
| 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 |
|
| 758 |
+ * GNU Lesser General Public License for more details. |
| 759 |
* |
| 760 |
|
| 761 |
|
| 762 |
|
| 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 |
|
| 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 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 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 |
|
| 834 |
|
| 835 |
|
| 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 |
|
| 843 |
|
| 844 |
|
| 845 |
+ // Close the input stream and return bytes |
| 846 |
+ is.close(); |
| 847 |
+ if(encoding == null){ |
| 848 |
return new String(bytes); |
| 849 |
|
| 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 |
|
| 962 |
+/** |
| 963 |
+ * ditaa - Diagrams Through Ascii Art |
| 964 |
* |
| 965 |
|
| 966 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 967 |
* |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 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 |
|
| 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 |
|
| 982 |
+ * GNU Lesser General Public License for more details. |
| 983 |
* |
| 984 |
|
| 985 |
|
| 986 |
|
| 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 |
|
| 1026 |
+/** |
| 1027 |
+ * ditaa - Diagrams Through Ascii Art |
| 1028 |
* |
| 1029 |
|
| 1030 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1031 |
* |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 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 |
|
| 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 |
|
| 1046 |
+ * GNU Lesser General Public License for more details. |
| 1047 |
* |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 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 |
|
| 1090 |
|
| 1091 |
public int getCellHeight() { |
| 1092 |
return cellHeight; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
public int getCellWidth() { |
| 1099 |
return cellWidth; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
public boolean dropShadows() { |
| 1106 |
return dropShadows; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
public boolean renderDebugLines() { |
| 1113 |
return renderDebugLines; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
public float getScale() { |
| 1120 |
return scale; |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
public void setDropShadows(boolean b) { |
| 1127 |
dropShadows = b; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
public void setRenderDebugLines(boolean b) { |
| 1134 |
renderDebugLines = b; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
public void setScale(float f) { |
| 1141 |
scale = f; |
| 1142 |
cellWidth *= scale; |
| 1143 |
cellHeight *= scale; |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
public boolean performAntialias() { |
| 1150 |
return antialias; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 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 |
|
| 1197 |
+/** |
| 1198 |
+ * ditaa - Diagrams Through Ascii Art |
| 1199 |
* |
| 1200 |
|
| 1201 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1202 |
* |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 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 |
|
| 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 |
|
| 1217 |
+ * GNU Lesser General Public License for more details. |
| 1218 |
* |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 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 |
|
| 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 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 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 |
|
| 1309 |
+/** |
| 1310 |
+ * ditaa - Diagrams Through Ascii Art |
| 1311 |
* |
| 1312 |
|
| 1313 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1314 |
* |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 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 |
|
| 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 |
|
| 1329 |
+ * GNU Lesser General Public License for more details. |
| 1330 |
* |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 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 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 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 |
|
| 1414 |
|
| 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 |
|
| 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 |
|
| 1445 |
+ ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes(); |
| 1446 |
|
| 1447 |
if(DEBUG) System.out.println("Rendering "+shapes.size()+" shapes (groups flattened)"); |
| 1448 |
|
| 1449 |
|
| 1450 |
+ Iterator<DiagramShape> shapesIt; |
| 1451 |
if(options.dropShadows()){ |
| 1452 |
//render shadows |
| 1453 |
shapesIt = shapes.iterator(); |
| 1454 |
while(shapesIt.hasNext()){ |
| 1455 |
|
| 1456 |
+ DiagramShape shape = shapesIt.next(); |
| 1457 |
|
| 1458 |
if(shape.getPoints().isEmpty()) continue; |
| 1459 |
|
| 1460 |
//GeneralPath path = shape.makeIntoPath(); |
| 1461 |
GeneralPath path; |
| 1462 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1522 |
+ Iterator<DiagramText> textIt = diagram.getTextObjects().iterator(); |
| 1523 |
while(textIt.hasNext()){ |
| 1524 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1552 |
+ ArrayList<DiagramText> textObjects; |
| 1553 |
|
| 1554 |
|
| 1555 |
+ public TextCanvas(ArrayList<DiagramText> textObjects){ |
| 1556 |
this.textObjects = textObjects; |
| 1557 |
} |
| 1558 |
|
| 1559 |
public void paint(Graphics g){ |
| 1560 |
Graphics g2 = (Graphics2D) g; |
| 1561 |
|
| 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 |
|
| 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 |
|
| 1576 |
|
| 1577 |
+ renderCustomSVGShape(shape, g2); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 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 |
|
| 1625 |
+/** |
| 1626 |
+ * ditaa - Diagrams Through Ascii Art |
| 1627 |
* |
| 1628 |
|
| 1629 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1630 |
* |
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 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 |
|
| 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 |
|
| 1645 |
+ * GNU Lesser General Public License for more details. |
| 1646 |
* |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 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 |
|
| 1658 |
import java.util.List; |
| 1659 |
+import java.util.Iterator; |
| 1660 |
+ |
| 1661 |
|
| 1662 |
import org.stathissideris.ascii2image.core.DebugUtils; |
| 1663 |
|
| 1664 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1697 |
+/** |
| 1698 |
+ * ditaa - Diagrams Through Ascii Art |
| 1699 |
* |
| 1700 |
|
| 1701 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1702 |
* |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 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 |
|
| 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 |
|
| 1717 |
+ * GNU Lesser General Public License for more details. |
| 1718 |
* |
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 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 |
|
| 1734 |
+/** |
| 1735 |
+ * ditaa - Diagrams Through Ascii Art |
| 1736 |
* |
| 1737 |
|
| 1738 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 1739 |
* |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 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 |
|
| 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 |
|
| 1754 |
+ * GNU Lesser General Public License for more details. |
| 1755 |
* |
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 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 |
|
| 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 |
|
| 1789 |
+ private static final boolean DEBUG_VERBOSE = false; |
| 1790 |
+ private static final boolean DEBUG_MAKE_SHAPES = false; |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1842 |
+ ArrayList<CellSet> boundarySetsStep1 = temp.getDistinctShapes(); |
| 1843 |
|
| 1844 |
if(DEBUG){ |
| 1845 |
System.out.println("******* Distinct shapes found using AbstractionGrid *******"); |
| 1846 |
|
| 1847 |
+ Iterator<CellSet> dit = boundarySetsStep1.iterator(); |
| 1848 |
while (dit.hasNext()) { |
| 1849 |
|
| 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 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 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 |
|
| 1873 |
+ Iterator<CellSet> dit = boundarySetsStep2.iterator(); |
| 1874 |
while (dit.hasNext()) { |
| 1875 |
|
| 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 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
+ ArrayList<CellSet> open = new ArrayList<CellSet>(); |
| 1888 |
+ ArrayList<CellSet> closed = new ArrayList<CellSet>(); |
| 1889 |
+ ArrayList<CellSet> mixed = new ArrayList<CellSet>(); |
| 1890 |
|
| 1891 |
|
| 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 |
|
| 1901 |
+ Iterator<CellSet> closedSets = closed.iterator(); |
| 1902 |
while(closedSets.hasNext()){ |
| 1903 |
|
| 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 |
|
| 1913 |
|
| 1914 |
|
| 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 |
|
| 1926 |
+ if(options.processingOptions.areAllCornersRound()) allCornersRound = true; |
| 1927 |
|
| 1928 |
//make shapes from the boundary sets |
| 1929 |
//make closed shapes |
| 1930 |
|
| 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 |
|
| 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 |
|
| 1972 |
|
| 1973 |
|
| 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 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
+ DiagramShape containingShape = findSmallestShapeContaining(point); |
| 1987 |
+ |
| 1988 |
+ if(containingShape != null) |
| 1989 |
+ containingShape.setFillColor(pair.color); |
| 1990 |
} |
| 1991 |
|
| 1992 |
//assign markup to shapes |
| 1993 |
|
| 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 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2090 |
+ options.processingOptions.getFromCustomShapes(pair.tag); |
| 2091 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 2092 |
containingShape.setDefinition(def); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
//make arrowheads |
| 2097 |
|
| 2098 |
+ Iterator<Cell> arrowheadCells = workGrid.findArrowheads().iterator(); |
| 2099 |
while(arrowheadCells.hasNext()){ |
| 2100 |
|
| 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 |
|
| 2109 |
+ Iterator<TextGrid.Cell> markersIt = grid.getPointMarkersOnLine().iterator(); |
| 2110 |
while (markersIt.hasNext()) { |
| 2111 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2135 |
|
| 2136 |
+ ArrayList<CellStringPair> strings = isolationGrid.findStrings(); |
| 2137 |
+ Iterator<CellStringPair> it = strings.iterator(); |
| 2138 |
while(it.hasNext()){ |
| 2139 |
|
| 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 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2188 |
|
| 2189 |
+ public ArrayList<DiagramShape> getAllDiagramShapes(){ |
| 2190 |
+ ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>(); |
| 2191 |
shapes.addAll(this.getShapes()); |
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2232 |
+ if(DEBUG_VERBOSE){ |
| 2233 |
System.out.println("One of smalls:"); |
| 2234 |
set2.printAsGrid(); |
| 2235 |
} |
| 2236 |
gridOfSmalls.fillCellsWith(set2, '*'); |
| 2237 |
} |
| 2238 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2313 |
+ public ArrayList<CompositeDiagramShape> getCompositeShapes() { |
| 2314 |
return compositeShapes; |
| 2315 |
} |
| 2316 |
|
| 2317 |
/** |
| 2318 |
* @return |
| 2319 |
*/ |
| 2320 |
|
| 2321 |
+ public ArrayList<DiagramShape> getShapes() { |
| 2322 |
return shapes; |
| 2323 |
} |
| 2324 |
|
| 2325 |
@@ -966,7 +991,7 @@ public class Diagram { |
| 2326 |
/** |
| 2327 |
* @return |
| 2328 |
*/ |
| 2329 |
|
| 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 |
|
| 2341 |
+/** |
| 2342 |
+ * ditaa - Diagrams Through Ascii Art |
| 2343 |
* |
| 2344 |
|
| 2345 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 2346 |
* |
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 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 |
|
| 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 |
|
| 2361 |
+ * GNU Lesser General Public License for more details. |
| 2362 |
* |
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 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 |
|
| 2383 |
|
| 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 |
|
| 2396 |
+/** |
| 2397 |
+ * ditaa - Diagrams Through Ascii Art |
| 2398 |
* |
| 2399 |
|
| 2400 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 2401 |
* |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 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 |
|
| 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 |
|
| 2416 |
+ * GNU Lesser General Public License for more details. |
| 2417 |
* |
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2485 |
+ return makeTrapezoidPath(diagram, options, true); |
| 2486 |
} |
| 2487 |
|
| 2488 |
if(getType() == TYPE_TRAPEZOID && points.size() == 4){ |
| 2489 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2517 |
+ private GeneralPath makeTrapezoidPath(Diagram diagram, RenderingOptions options, boolean inverted) { |
| 2518 |
if(points.size() != 4) return null; |
| 2519 |
Rectangle bounds = makeIntoPath().getBounds(); |
| 2520 |
|
| 2521 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2590 |
+/** |
| 2591 |
+ * ditaa - Diagrams Through Ascii Art |
| 2592 |
* |
| 2593 |
|
| 2594 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 2595 |
* |
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 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 |
|
| 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 |
|
| 2610 |
+ * GNU Lesser General Public License for more details. |
| 2611 |
* |
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 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 |
|
| 2634 |
+/** |
| 2635 |
+ * ditaa - Diagrams Through Ascii Art |
| 2636 |
* |
| 2637 |
|
| 2638 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 2639 |
* |
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 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 |
|
| 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 |
|
| 2654 |
+ * GNU Lesser General Public License for more details. |
| 2655 |
* |
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 2714 |
+/** |
| 2715 |
+ * ditaa - Diagrams Through Ascii Art |
| 2716 |
* |
| 2717 |
|
| 2718 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 2719 |
* |
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 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 |
|
| 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 |
|
| 2734 |
+ * GNU Lesser General Public License for more details. |
| 2735 |
* |
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 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 |
|
| 2784 |
|
| 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 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 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 |
|
| 3456 |
+/** |
| 3457 |
+ * ditaa - Diagrams Through Ascii Art |
| 3458 |
* |
| 3459 |
|
| 3460 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3461 |
* |
| 3462 |
|
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 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 |
|
| 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 |
|
| 3476 |
+ * GNU Lesser General Public License for more details. |
| 3477 |
* |
| 3478 |
|
| 3479 |
|
| 3480 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 3520 |
+/** |
| 3521 |
+ * ditaa - Diagrams Through Ascii Art |
| 3522 |
* |
| 3523 |
|
| 3524 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3525 |
* |
| 3526 |
|
| 3527 |
|
| 3528 |
|
| 3529 |
|
| 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 |
|
| 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 |
|
| 3540 |
+ * GNU Lesser General Public License for more details. |
| 3541 |
* |
| 3542 |
|
| 3543 |
|
| 3544 |
|
| 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 |
|
| 3562 |
+/** |
| 3563 |
+ * ditaa - Diagrams Through Ascii Art |
| 3564 |
* |
| 3565 |
|
| 3566 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3567 |
* |
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 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 |
|
| 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 |
|
| 3582 |
+ * GNU Lesser General Public License for more details. |
| 3583 |
* |
| 3584 |
|
| 3585 |
|
| 3586 |
|
| 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 |
|
| 3599 |
+/** |
| 3600 |
+ * ditaa - Diagrams Through Ascii Art |
| 3601 |
* |
| 3602 |
|
| 3603 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3604 |
* |
| 3605 |
|
| 3606 |
|
| 3607 |
|
| 3608 |
|
| 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 |
|
| 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 |
|
| 3619 |
+ * GNU Lesser General Public License for more details. |
| 3620 |
* |
| 3621 |
|
| 3622 |
|
| 3623 |
|
| 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 |
|
| 3634 |
|
| 3635 |
+ public ArrayList<CellSet> getDistinctShapes(){ |
| 3636 |
+ ArrayList<CellSet> result = new ArrayList<CellSet>(); |
| 3637 |
|
| 3638 |
CellSet nonBlank = grid.getAllNonBlank(); |
| 3639 |
|
| 3640 |
+ ArrayList<CellSet> distinct = nonBlank.breakIntoDistinctBoundaries(); |
| 3641 |
|
| 3642 |
|
| 3643 |
+ Iterator<CellSet> it = distinct.iterator(); |
| 3644 |
while (it.hasNext()) { |
| 3645 |
|
| 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 |
|
| 3657 |
+/** |
| 3658 |
+ * ditaa - Diagrams Through Ascii Art |
| 3659 |
* |
| 3660 |
|
| 3661 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3662 |
* |
| 3663 |
|
| 3664 |
|
| 3665 |
|
| 3666 |
|
| 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 |
|
| 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 |
|
| 3677 |
+ * GNU Lesser General Public License for more details. |
| 3678 |
* |
| 3679 |
|
| 3680 |
|
| 3681 |
|
| 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 |
|
| 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 |
|
| 3710 |
|
| 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 |
|
| 3720 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3721 |
while(it.hasNext()){ |
| 3722 |
|
| 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 |
|
| 3732 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3733 |
while(it.hasNext()){ |
| 3734 |
|
| 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 |
|
| 3744 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3745 |
while(it.hasNext()){ |
| 3746 |
|
| 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 |
|
| 3756 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3757 |
while(it.hasNext()){ |
| 3758 |
|
| 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 |
|
| 3768 |
+ Iterator<TextGrid.Cell> it = set.iterator(); |
| 3769 |
while(it.hasNext()){ |
| 3770 |
|
| 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 |
|
| 3779 |
+ Iterator<TextGrid.Cell> it = set.iterator(); |
| 3780 |
while(it.hasNext()){ |
| 3781 |
|
| 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 |
|
| 3791 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3792 |
while(it.hasNext()){ |
| 3793 |
|
| 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 |
|
| 3803 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3804 |
while(it.hasNext()){ |
| 3805 |
|
| 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 |
|
| 3815 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3816 |
while(it.hasNext()){ |
| 3817 |
|
| 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 |
|
| 3827 |
+ Iterator<TextGrid.Cell> it = iterator(); |
| 3828 |
while(it.hasNext()){ |
| 3829 |
|
| 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 |
|
| 3839 |
|
| 3840 |
+ public static ArrayList<CellSet> removeDuplicateSets(ArrayList<CellSet> list) { |
| 3841 |
+ ArrayList<CellSet> uniqueSets = new ArrayList<CellSet>(); |
| 3842 |
|
| 3843 |
|
| 3844 |
+ Iterator<CellSet> it = list.iterator(); |
| 3845 |
while(it.hasNext()){ |
| 3846 |
|
| 3847 |
+ CellSet set = it.next(); |
| 3848 |
boolean isOriginal = true; |
| 3849 |
|
| 3850 |
+ Iterator<CellSet> uniquesIt = uniqueSets.iterator(); |
| 3851 |
while(uniquesIt.hasNext()){ |
| 3852 |
|
| 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 |
|
| 3862 |
|
| 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 |
|
| 3873 |
|
| 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 |
|
| 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 |
|
| 3893 |
|
| 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 |
|
| 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 |
|
| 3913 |
|
| 3914 |
|
| 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 |
|
| 3926 |
+/** |
| 3927 |
+ * ditaa - Diagrams Through Ascii Art |
| 3928 |
* |
| 3929 |
|
| 3930 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3931 |
* |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 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 |
|
| 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 |
|
| 3946 |
+ * GNU Lesser General Public License for more details. |
| 3947 |
* |
| 3948 |
|
| 3949 |
|
| 3950 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 3990 |
+/** |
| 3991 |
+ * ditaa - Diagrams Through Ascii Art |
| 3992 |
* |
| 3993 |
|
| 3994 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 3995 |
* |
| 3996 |
|
| 3997 |
|
| 3998 |
|
| 3999 |
|
| 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 |
|
| 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 |
|
| 4010 |
+ * GNU Lesser General Public License for more details. |
| 4011 |
* |
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 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 |
|
| 4027 |
+/** |
| 4028 |
+ * ditaa - Diagrams Through Ascii Art |
| 4029 |
* |
| 4030 |
|
| 4031 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 4032 |
* |
| 4033 |
|
| 4034 |
|
| 4035 |
|
| 4036 |
|
| 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 |
|
| 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 |
|
| 4047 |
+ * GNU Lesser General Public License for more details. |
| 4048 |
* |
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 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 |
|
| 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 |
|
| 4073 |
+/** |
| 4074 |
+ * ditaa - Diagrams Through Ascii Art |
| 4075 |
* |
| 4076 |
|
| 4077 |
+ * Copyright (C) 2004-2011 Efstathios Sideris |
| 4078 |
* |
| 4079 |
|
| 4080 |
|
| 4081 |
|
| 4082 |
|
| 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 |
|
| 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 |
|
| 4093 |
+ * GNU Lesser General Public License for more details. |
| 4094 |
* |
| 4095 |
|
| 4096 |
|
| 4097 |
|
| 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 |
|
| 4106 |
|
| 4107 |
|
| 4108 |
|
| 4109 |
|
| 4110 |
|
| 4111 |
|
| 4112 |
|
| 4113 |
|
| 4114 |
|
| 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 |
|
| 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 |
|
| 4140 |
+ rows = new ArrayList<StringBuilder>(); |
| 4141 |
} |
| 4142 |
|
| 4143 |
public TextGrid(int width, int height){ |
| 4144 |
String space = StringUtils.repeatString(" ", width); |
| 4145 |
|
| 4146 |
+ rows = new ArrayList<StringBuilder>(); |
| 4147 |
for(int i = 0; i < height; i++) |
| 4148 |
|
| 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 |
|
| 4158 |
|
| 4159 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 4205 |
+ rows.set(y, new StringBuilder(row)); |
| 4206 |
} |
| 4207 |
|
| 4208 |
|
| 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 |
|
| 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 |
|
| 4227 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 4265 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
|
| 4299 |
|
| 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 |
|
| 4310 |
|
| 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 |
|
| 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 |
|
| 4328 |
+ ArrayList<StringBuilder> lines = new ArrayList<StringBuilder>(); |
| 4329 |
String[] linesArray = text.split("(\r)?\n"); |
| 4330 |
for(int i = 0; i < linesArray.length; i++) |
| 4331 |
|
| 4332 |
+ lines.add(new StringBuilder(linesArray[i])); |
| 4333 |
|
| 4334 |
return initialiseWithLines(lines, options); |
| 4335 |
} |
| 4336 |
|
| 4337 |
|
| 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 |
|
| 4344 |
|
| 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 |
|
| 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 |
|
| 4359 |
+ if(options != null) encoding = options.getCharacterEncoding(); |
| 4360 |
|
| 4361 |
|
| 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 |
|
| 4371 |
+ rows.set(index, new StringBuilder(row)); |
| 4372 |
index++; |
| 4373 |
} |
| 4374 |
|
| 4375 |
it = rows.iterator(); |
| 4376 |
|
| 4377 |
+ ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>(); |
| 4378 |
//TODO: make the following depend on blankBorderSize |
| 4379 |
|
| 4380 |
|
| 4381 |
|
| 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 |
|
| 4389 |
+ StringBuilder row = it.next(); |
| 4390 |
|
| 4391 |
if(row.length() < maxLength) { |
| 4392 |
String borderString = StringUtils.repeatString(" ", blankBorderSize); |
| 4393 |
|
| 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 |
|
| 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 |
|
| 4412 |
+ Iterator<StringBuilder> it = rows.iterator(); |
| 4413 |
|
| 4414 |
while(it.hasNext()){ |
| 4415 |
String row = it.next().toString(); |
| 4416 |
|
| 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 |
|
| 4426 |
+ protected ArrayList<StringBuilder> getRows() { |
| 4427 |
return rows; |
| 4428 |
} |
| 4429 |
|