| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| 22 |
import java.awt.Color; |
| 23 |
import java.awt.Font; |
| •24 |
import java.awt.geom.Rectangle2D; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Iterator; |
| 27 |
|
| 28 |
import org.stathissideris.ascii2image.core.ConversionOptions; |
| 29 |
import org.stathissideris.ascii2image.core.Pair; |
| 30 |
import org.stathissideris.ascii2image.text.AbstractionGrid; |
| 31 |
import org.stathissideris.ascii2image.text.CellSet; |
| 32 |
import org.stathissideris.ascii2image.text.TextGrid; |
| •33 |
import org.stathissideris.ascii2image.text.TextGrid.Cell; |
| 34 |
import org.stathissideris.ascii2image.text.TextGrid.CellColorPair; |
| 35 |
import org.stathissideris.ascii2image.text.TextGrid.CellStringPair; |
| 36 |
import org.stathissideris.ascii2image.text.TextGrid.CellTagPair; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Diagram { |
| 43 |
|
| 44 |
private static final boolean DEBUG = false; |
| •45 |
private static final boolean DEBUG_VERBOSE = false; |
| 46 |
private static final boolean DEBUG_MAKE_SHAPES = false; |
| 47 |
|
| •48 |
private ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>(); |
| 49 |
private ArrayList<CompositeDiagramShape> compositeShapes = new ArrayList<CompositeDiagramShape>(); |
| 50 |
private ArrayList<DiagramText> textObjects = new ArrayList<DiagramText>(); |
| 51 |
|
| 52 |
private int width, height; |
| 53 |
private int cellWidth, cellHeight; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| •74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| •88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| •102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| •113 |
public Diagram(TextGrid grid, ConversionOptions options) { |
| 114 |
|
| 115 |
this.cellWidth = options.renderingOptions.getCellWidth(); |
| 116 |
this.cellHeight = options.renderingOptions.getCellHeight(); |
| 117 |
|
| 118 |
width = grid.getWidth() * cellWidth; |
| 119 |
height = grid.getHeight() * cellHeight; |
| 120 |
|
| 121 |
TextGrid workGrid = new TextGrid(grid); |
| 122 |
workGrid.replaceTypeOnLine(); |
| 123 |
workGrid.replacePointMarkersOnLine(); |
| 124 |
if(DEBUG) workGrid.printDebug(); |
| 125 |
|
| 126 |
int width = grid.getWidth(); |
| 127 |
int height = grid.getHeight(); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
AbstractionGrid temp = new AbstractionGrid(workGrid, workGrid.getAllBoundaries()); |
| •132 |
ArrayList<CellSet> boundarySetsStep1 = temp.getDistinctShapes(); |
| 133 |
|
| 134 |
if(DEBUG){ |
| 135 |
System.out.println("******* Distinct shapes found using AbstractionGrid *******"); |
| •136 |
Iterator<CellSet> dit = boundarySetsStep1.iterator(); |
| 137 |
while (dit.hasNext()) { |
| •138 |
CellSet set = dit.next(); |
| 139 |
set.printAsGrid(); |
| 140 |
} |
| 141 |
System.out.println("******* Same set of shapes after processing them by filling *******"); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| •147 |
ArrayList<CellSet> boundarySetsStep2 = new ArrayList<CellSet>(); |
| 148 |
for(CellSet set : boundarySetsStep1) { |
| 149 |
|
| 150 |
|
| 151 |
TextGrid fillBuffer = new TextGrid(width * 3, height * 3); |
| 152 |
|
| 153 |
for(int yi = 0; yi < height * 3; yi++){ |
| 154 |
for(int xi = 0; xi < width * 3; xi++){ |
| 155 |
if(fillBuffer.isBlank(xi, yi)){ |
| 156 |
|
| 157 |
TextGrid copyGrid = new AbstractionGrid(workGrid, set).getCopyOfInternalBuffer(); |
| 158 |
|
| 159 |
CellSet boundaries = |
| 160 |
copyGrid |
| 161 |
.findBoundariesExpandingFrom(copyGrid.new Cell(xi, yi)); |
| 162 |
if(boundaries.size() == 0) continue; |
| 163 |
boundarySetsStep2.add(boundaries.makeScaledOneThirdEquivalent()); |
| 164 |
|
| 165 |
copyGrid = new AbstractionGrid(workGrid, set).getCopyOfInternalBuffer(); |
| 166 |
CellSet filled = |
| 167 |
copyGrid |
| 168 |
.fillContinuousArea(copyGrid.new Cell(xi, yi), '*'); |
| 169 |
fillBuffer.fillCellsWith(filled, '*'); |
| 170 |
fillBuffer.fillCellsWith(boundaries, '-'); |
| 171 |
|
| 172 |
if(DEBUG){ |
| 173 |
|
| 174 |
|
| 175 |
boundaries.makeScaledOneThirdEquivalent().printAsGrid(); |
| 176 |
System.out.println("-----------------------------------"); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (DEBUG) |
| 185 |
System.out.println("******* Removed duplicates *******"); |
| 186 |
|
| 187 |
boundarySetsStep2 = CellSet.removeDuplicateSets(boundarySetsStep2); |
| 188 |
|
| 189 |
if(DEBUG){ |
| •190 |
Iterator<CellSet> dit = boundarySetsStep2.iterator(); |
| 191 |
while (dit.hasNext()) { |
| •192 |
CellSet set = dit.next(); |
| 193 |
set.printAsGrid(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
int originalSize = boundarySetsStep2.size(); |
| 198 |
boundarySetsStep2 = CellSet.removeDuplicateSets(boundarySetsStep2); |
| 199 |
if(DEBUG) { |
| 200 |
System.out.println( |
| 201 |
"******* Removed duplicates: there were " |
| 202 |
+originalSize |
| 203 |
+" shapes and now there are " |
| 204 |
+boundarySetsStep2.size()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if (DEBUG) |
| 211 |
System.out.println("******* First evaluation of openess *******"); |
| 212 |
|
| •213 |
ArrayList<CellSet> open = new ArrayList<CellSet>(); |
| 214 |
ArrayList<CellSet> closed = new ArrayList<CellSet>(); |
| 215 |
ArrayList<CellSet> mixed = new ArrayList<CellSet>(); |
| 216 |
|
| •217 |
Iterator<CellSet> sets = boundarySetsStep2.iterator(); |
| 218 |
while(sets.hasNext()){ |
| 219 |
CellSet set = (CellSet) sets.next(); |
| 220 |
int type = set.getType(workGrid); |
| 221 |
if(type == CellSet.TYPE_CLOSED) closed.add(set); |
| 222 |
else if(type == CellSet.TYPE_OPEN) open.add(set); |
| 223 |
else if(type == CellSet.TYPE_MIXED) mixed.add(set); |
| 224 |
if(DEBUG){ |
| 225 |
if(type == CellSet.TYPE_CLOSED) System.out.println("Closed boundaries:"); |
| 226 |
else if(type == CellSet.TYPE_OPEN) System.out.println("Open boundaries:"); |
| 227 |
else if(type == CellSet.TYPE_MIXED) System.out.println("Mixed boundaries:"); |
| 228 |
set.printAsGrid(); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
boolean hadToEliminateMixed = false; |
| 233 |
|
| 234 |
if(mixed.size() > 0 && closed.size() > 0) { |
| 235 |
|
| 236 |
|
| 237 |
if (DEBUG) |
| 238 |
System.out.println("******* Eliminating mixed shapes (basic algorithm) *******"); |
| 239 |
|
| 240 |
hadToEliminateMixed = true; |
| 241 |
|
| 242 |
|
| 243 |
sets = mixed.iterator(); |
| 244 |
while(sets.hasNext()){ |
| 245 |
CellSet set = (CellSet) sets.next(); |
| •246 |
Iterator<CellSet> closedSets = closed.iterator(); |
| 247 |
while(closedSets.hasNext()){ |
| •248 |
CellSet closedSet = closedSets.next(); |
| 249 |
set.subtractSet(closedSet); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
if(set.getType(workGrid) == CellSet.TYPE_OPEN) { |
| 255 |
boundarySetsStep2.remove(set); |
| 256 |
boundarySetsStep2.addAll(set.breakIntoDistinctBoundaries(workGrid)); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
} else if(mixed.size() > 0 && closed.size() == 0) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
hadToEliminateMixed = true; |
| 271 |
|
| 272 |
if (DEBUG) |
| 273 |
System.out.println("******* Eliminating mixed shapes (advanced algorithm for truly mixed shapes) *******"); |
| 274 |
|
| 275 |
sets = mixed.iterator(); |
| 276 |
while(sets.hasNext()){ |
| 277 |
CellSet set = (CellSet) sets.next(); |
| 278 |
boundarySetsStep2.remove(set); |
| 279 |
boundarySetsStep2.addAll(set.breakTrulyMixedBoundaries(workGrid)); |
| 280 |
} |
| 281 |
|
| 282 |
} else { |
| 283 |
if (DEBUG) |
| 284 |
System.out.println("No mixed shapes found. Skipped mixed shape elimination step"); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
if(hadToEliminateMixed){ |
| 289 |
if (DEBUG) |
| 290 |
System.out.println("******* Second evaluation of openess *******"); |
| 291 |
|
| 292 |
|
| •293 |
open = new ArrayList<CellSet>(); |
| 294 |
closed = new ArrayList<CellSet>(); |
| 295 |
mixed = new ArrayList<CellSet>(); |
| 296 |
|
| 297 |
sets = boundarySetsStep2.iterator(); |
| 298 |
while(sets.hasNext()){ |
| 299 |
CellSet set = (CellSet) sets.next(); |
| 300 |
int type = set.getType(workGrid); |
| 301 |
if(type == CellSet.TYPE_CLOSED) closed.add(set); |
| 302 |
else if(type == CellSet.TYPE_OPEN) open.add(set); |
| 303 |
else if(type == CellSet.TYPE_MIXED) mixed.add(set); |
| 304 |
if(DEBUG){ |
| 305 |
if(type == CellSet.TYPE_CLOSED) System.out.println("Closed boundaries:"); |
| 306 |
else if(type == CellSet.TYPE_OPEN) System.out.println("Open boundaries:"); |
| 307 |
else if(type == CellSet.TYPE_MIXED) System.out.println("Mixed boundaries:"); |
| 308 |
set.printAsGrid(); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
boolean removedAnyObsolete = removeObsoleteShapes(workGrid, closed); |
| 314 |
|
| 315 |
boolean allCornersRound = false; |
| •316 |
if(options.processingOptions.areAllCornersRound()) allCornersRound = true; |
| 317 |
|
| 318 |
|
| 319 |
|
| •320 |
if(DEBUG_MAKE_SHAPES) { |
| 321 |
System.out.println("***** MAKING SHAPES FROM BOUNDARY SETS *****"); |
| 322 |
System.out.println("***** CLOSED: *****"); |
| 323 |
} |
| 324 |
|
| 325 |
ArrayList<DiagramComponent> closedShapes = new ArrayList<DiagramComponent>(); |
| 326 |
sets = closed.iterator(); |
| 327 |
while(sets.hasNext()){ |
| 328 |
CellSet set = (CellSet) sets.next(); |
| •329 |
|
| 330 |
if(DEBUG_MAKE_SHAPES) { |
| 331 |
set.printAsGrid(); |
| 332 |
} |
| 333 |
|
| 334 |
DiagramComponent shape = DiagramComponent.createClosedFromBoundaryCells(workGrid, set, cellWidth, cellHeight, allCornersRound); |
| 335 |
if(shape != null){ |
| 336 |
if(shape instanceof DiagramShape){ |
| 337 |
addToShapes((DiagramShape) shape); |
| 338 |
closedShapes.add(shape); |
| 339 |
} else if(shape instanceof CompositeDiagramShape) |
| 340 |
addToCompositeShapes((CompositeDiagramShape) shape); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| •344 |
if(options.processingOptions.performSeparationOfCommonEdges()) |
| 345 |
separateCommonEdges(closedShapes); |
| 346 |
|
| 347 |
|
| 348 |
sets = open.iterator(); |
| 349 |
while(sets.hasNext()){ |
| 350 |
CellSet set = (CellSet) sets.next(); |
| 351 |
if(set.size() == 1){ |
| 352 |
TextGrid.Cell cell = (TextGrid.Cell) set.getFirst(); |
| 353 |
if(!grid.cellContainsDashedLineChar(cell)) { |
| 354 |
DiagramShape shape = DiagramShape.createSmallLine(workGrid, cell, cellWidth, cellHeight); |
| 355 |
if(shape != null) { |
| 356 |
addToShapes(shape); |
| 357 |
shape.connectEndsToAnchors(workGrid, this); |
| 358 |
} |
| 359 |
} |
| 360 |
} else { |
| •361 |
if (DEBUG) |
| 362 |
System.out.println(set.getCellsAsString()); |
| 363 |
|
| 364 |
DiagramComponent shape = |
| 365 |
CompositeDiagramShape |
| 366 |
.createOpenFromBoundaryCells( |
| 367 |
workGrid, set, cellWidth, cellHeight, allCornersRound); |
| 368 |
|
| 369 |
if(shape != null){ |
| 370 |
if(shape instanceof CompositeDiagramShape){ |
| 371 |
addToCompositeShapes((CompositeDiagramShape) shape); |
| 372 |
((CompositeDiagramShape) shape).connectEndsToAnchors(workGrid, this); |
| 373 |
} else if(shape instanceof DiagramShape) { |
| 374 |
addToShapes((DiagramShape) shape); |
| 375 |
((DiagramShape) shape).connectEndsToAnchors(workGrid, this); |
| 376 |
((DiagramShape) shape).moveEndsToCellEdges(grid, this); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| •386 |
Iterator<CellColorPair> cellColorPairs = grid.findColorCodes().iterator(); |
| 387 |
while(cellColorPairs.hasNext()){ |
| 388 |
TextGrid.CellColorPair pair = |
| 389 |
(TextGrid.CellColorPair) cellColorPairs.next(); |
| •390 |
|
| 391 |
ShapePoint point = |
| 392 |
new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell)); |
| •393 |
DiagramShape containingShape = findSmallestShapeContaining(point); |
| 394 |
|
| 395 |
if(containingShape != null) |
| 396 |
containingShape.setFillColor(pair.color); |
| 397 |
} |
| 398 |
|
| 399 |
|
| •400 |
Iterator<CellTagPair> cellTagPairs = grid.findMarkupTags().iterator(); |
| 401 |
while(cellTagPairs.hasNext()){ |
| 402 |
TextGrid.CellTagPair pair = |
| 403 |
(TextGrid.CellTagPair) cellTagPairs.next(); |
| •404 |
|
| 405 |
ShapePoint point = |
| 406 |
new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell)); |
| •407 |
|
| 408 |
DiagramShape containingShape = findSmallestShapeContaining(point); |
| 409 |
|
| 410 |
|
| 411 |
if(containingShape == null) continue; |
| 412 |
|
| 413 |
|
| 414 |
if(pair.tag.equals("d")){ |
| 415 |
CustomShapeDefinition def = |
| •416 |
options.processingOptions.getFromCustomShapes("d"); |
| 417 |
if(def == null) |
| 418 |
containingShape.setType(DiagramShape.TYPE_DOCUMENT); |
| 419 |
else { |
| 420 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 421 |
containingShape.setDefinition(def); |
| 422 |
} |
| 423 |
} else if(pair.tag.equals("s")){ |
| 424 |
CustomShapeDefinition def = |
| •425 |
options.processingOptions.getFromCustomShapes("s"); |
| 426 |
if(def == null) |
| 427 |
containingShape.setType(DiagramShape.TYPE_STORAGE); |
| 428 |
else { |
| 429 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 430 |
containingShape.setDefinition(def); |
| 431 |
} |
| 432 |
} else if(pair.tag.equals("io")){ |
| 433 |
CustomShapeDefinition def = |
| •434 |
options.processingOptions.getFromCustomShapes("io"); |
| 435 |
if(def == null) |
| 436 |
containingShape.setType(DiagramShape.TYPE_IO); |
| 437 |
else { |
| 438 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 439 |
containingShape.setDefinition(def); |
| 440 |
} |
| 441 |
} else if(pair.tag.equals("c")){ |
| 442 |
CustomShapeDefinition def = |
| •443 |
options.processingOptions.getFromCustomShapes("c"); |
| 444 |
if(def == null) |
| 445 |
containingShape.setType(DiagramShape.TYPE_DECISION); |
| 446 |
else { |
| 447 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 448 |
containingShape.setDefinition(def); |
| 449 |
} |
| 450 |
} else if(pair.tag.equals("mo")){ |
| 451 |
CustomShapeDefinition def = |
| •452 |
options.processingOptions.getFromCustomShapes("mo"); |
| 453 |
if(def == null) |
| 454 |
containingShape.setType(DiagramShape.TYPE_MANUAL_OPERATION); |
| 455 |
else { |
| 456 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 457 |
containingShape.setDefinition(def); |
| 458 |
} |
| 459 |
} else if(pair.tag.equals("tr")){ |
| 460 |
CustomShapeDefinition def = |
| •461 |
options.processingOptions.getFromCustomShapes("tr"); |
| 462 |
if(def == null) |
| 463 |
containingShape.setType(DiagramShape.TYPE_TRAPEZOID); |
| 464 |
else { |
| 465 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 466 |
containingShape.setDefinition(def); |
| 467 |
} |
| 468 |
} else if(pair.tag.equals("o")){ |
| 469 |
CustomShapeDefinition def = |
| •470 |
options.processingOptions.getFromCustomShapes("o"); |
| 471 |
if(def == null) |
| 472 |
containingShape.setType(DiagramShape.TYPE_ELLIPSE); |
| 473 |
else { |
| 474 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 475 |
containingShape.setDefinition(def); |
| 476 |
} |
| 477 |
} else { |
| 478 |
CustomShapeDefinition def = |
| •479 |
options.processingOptions.getFromCustomShapes(pair.tag); |
| 480 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 481 |
containingShape.setDefinition(def); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| •486 |
Iterator<Cell> arrowheadCells = workGrid.findArrowheads().iterator(); |
| 487 |
while(arrowheadCells.hasNext()){ |
| •488 |
TextGrid.Cell cell = arrowheadCells.next(); |
| 489 |
DiagramShape arrowhead = DiagramShape.createArrowhead(workGrid, cell, cellWidth, cellHeight); |
| 490 |
if(arrowhead != null) addToShapes(arrowhead); |
| 491 |
else System.err.println("Could not create arrowhead shape. Unexpected error."); |
| 492 |
} |
| 493 |
|
| 494 |
|
| •495 |
Iterator<TextGrid.Cell> markersIt = grid.getPointMarkersOnLine().iterator(); |
| 496 |
while (markersIt.hasNext()) { |
| •497 |
TextGrid.Cell cell = markersIt.next(); |
| 498 |
|
| 499 |
DiagramShape mark = new DiagramShape(); |
| 500 |
mark.addToPoints(new ShapePoint( |
| 501 |
getCellMidX(cell), |
| 502 |
getCellMidY(cell) |
| 503 |
)); |
| 504 |
mark.setType(DiagramShape.TYPE_POINT_MARKER); |
| 505 |
mark.setFillColor(Color.white); |
| 506 |
shapes.add(mark); |
| 507 |
} |
| 508 |
|
| 509 |
removeDuplicateShapes(); |
| 510 |
|
| 511 |
if(DEBUG) System.out.println("Shape count: "+shapes.size()); |
| 512 |
if(DEBUG) System.out.println("Composite shape count: "+compositeShapes.size()); |
| 513 |
|
| 514 |
|
| 515 |
workGrid = new TextGrid(grid); |
| 516 |
workGrid.removeNonText(); |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
TextGrid textGroupGrid = new TextGrid(workGrid); |
| 522 |
CellSet gaps = textGroupGrid.getAllBlanksBetweenCharacters(); |
| 523 |
|
| 524 |
textGroupGrid.fillCellsWith(gaps, '|'); |
| 525 |
CellSet nonBlank = textGroupGrid.getAllNonBlank(); |
| •526 |
ArrayList<CellSet> textGroups = nonBlank.breakIntoDistinctBoundaries(); |
| 527 |
if(DEBUG) System.out.println(textGroups.size()+" text groups found"); |
| 528 |
|
| 529 |
Font font = FontMeasurer.instance().getFontFor(cellHeight); |
| 530 |
|
| •531 |
Iterator<CellSet> textGroupIt = textGroups.iterator(); |
| 532 |
while(textGroupIt.hasNext()){ |
| 533 |
CellSet textGroupCellSet = (CellSet) textGroupIt.next(); |
| 534 |
|
| 535 |
TextGrid isolationGrid = new TextGrid(width, height); |
| 536 |
workGrid.copyCellsTo(textGroupCellSet, isolationGrid); |
| 537 |
|
| •538 |
ArrayList<CellStringPair> strings = isolationGrid.findStrings(); |
| 539 |
Iterator<CellStringPair> it = strings.iterator(); |
| 540 |
while(it.hasNext()){ |
| •541 |
TextGrid.CellStringPair pair = it.next(); |
| 542 |
TextGrid.Cell cell = pair.cell; |
| 543 |
String string = pair.string; |
| 544 |
if (DEBUG) |
| 545 |
System.out.println("Found string "+string); |
| 546 |
TextGrid.Cell lastCell = isolationGrid.new Cell(cell.x + string.length() - 1, cell.y); |
| 547 |
|
| 548 |
int minX = getCellMinX(cell); |
| 549 |
int y = getCellMaxY(cell); |
| 550 |
int maxX = getCellMaxX(lastCell); |
| 551 |
|
| 552 |
DiagramText textObject; |
| 553 |
if(FontMeasurer.instance().getWidthFor(string, font) > maxX - minX){ |
| 554 |
Font lessWideFont = FontMeasurer.instance().getFontFor(maxX - minX, string); |
| 555 |
textObject = new DiagramText(minX, y, string, lessWideFont); |
| 556 |
} else textObject = new DiagramText(minX, y, string, font); |
| 557 |
|
| 558 |
textObject.centerVerticallyBetween(getCellMinY(cell), getCellMaxY(cell)); |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
int otherStart = isolationGrid.otherStringsStartInTheSameColumn(cell); |
| 564 |
int otherEnd = isolationGrid.otherStringsEndInTheSameColumn(lastCell); |
| 565 |
if(0 == otherStart && 0 == otherEnd) { |
| 566 |
textObject.centerHorizontallyBetween(minX, maxX); |
| 567 |
} else if(otherEnd > 0 && otherStart == 0) { |
| 568 |
textObject.alignRightEdgeTo(maxX); |
| 569 |
} else if(otherEnd > 0 && otherStart > 0){ |
| 570 |
if(otherEnd > otherStart){ |
| 571 |
textObject.alignRightEdgeTo(maxX); |
| 572 |
} else if(otherEnd == otherStart){ |
| 573 |
textObject.centerHorizontallyBetween(minX, maxX); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
addToTextObjects(textObject); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
if (DEBUG) |
| 582 |
System.out.println("Positioned text"); |
| 583 |
|
| 584 |
|
| 585 |
|
| •586 |
for(DiagramText textObject : getTextObjects()) { |
| 587 |
DiagramShape shape = findSmallestShapeIntersecting(textObject.getBounds()); |
| 588 |
if(shape != null |
| 589 |
&& shape.getFillColor() != null |
| 590 |
&& BitmapRenderer.isColorDark(shape.getFillColor())) { |
| 591 |
textObject.setColor(Color.white); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
|
| •596 |
Iterator<DiagramShape> shapes = this.getAllDiagramShapes().iterator(); |
| 597 |
while(shapes.hasNext()){ |
| 598 |
DiagramShape shape = (DiagramShape) shapes.next(); |
| 599 |
if(shape.getType() == DiagramShape.TYPE_CUSTOM){ |
| •600 |
Iterator<DiagramText> textObjects = getTextObjects().iterator(); |
| 601 |
while(textObjects.hasNext()){ |
| 602 |
DiagramText textObject = (DiagramText) textObjects.next(); |
| 603 |
textObject.setHasOutline(true); |
| 604 |
textObject.setColor(DiagramText.DEFAULT_COLOR); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
if (DEBUG) |
| 610 |
System.out.println("Corrected color of text according to underlying color"); |
| 611 |
|
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| •620 |
public ArrayList<DiagramShape> getAllDiagramShapes(){ |
| 621 |
ArrayList<DiagramShape> shapes = new ArrayList<DiagramShape>(); |
| 622 |
shapes.addAll(this.getShapes()); |
| 623 |
|
| •624 |
for(CompositeDiagramShape compShape : getCompositeShapes()) { |
| 625 |
shapes.addAll(compShape.getShapes()); |
| 626 |
} |
| 627 |
return shapes; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
private boolean removeObsoleteShapes(TextGrid grid, ArrayList<CellSet> sets){ |
| 638 |
if (DEBUG) |
| 639 |
System.out.println("******* Removing obsolete shapes *******"); |
| 640 |
|
| 641 |
boolean removedAny = false; |
| 642 |
|
| 643 |
ArrayList<CellSet> filledSets = new ArrayList<CellSet>(); |
| 644 |
|
| 645 |
Iterator it; |
| 646 |
|
| •647 |
if(DEBUG_VERBOSE) { |
| 648 |
System.out.println("******* Sets before *******"); |
| 649 |
it = sets.iterator(); |
| 650 |
while(it.hasNext()){ |
| 651 |
CellSet set = (CellSet) it.next(); |
| 652 |
set.printAsGrid(); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
it = sets.iterator(); |
| 658 |
while(it.hasNext()){ |
| 659 |
CellSet set = (CellSet) it.next(); |
| 660 |
set = set.getFilledEquivalent(grid); |
| 661 |
if(set == null){ |
| 662 |
return false; |
| 663 |
} else filledSets.add(set); |
| 664 |
} |
| 665 |
|
| 666 |
ArrayList<Integer> toBeRemovedIndices = new ArrayList<Integer>(); |
| 667 |
it = filledSets.iterator(); |
| 668 |
while(it.hasNext()){ |
| 669 |
CellSet set = (CellSet) it.next(); |
| 670 |
|
| •671 |
if(DEBUG_VERBOSE){ |
| 672 |
System.out.println("*** Deciding if the following should be removed:"); |
| 673 |
set.printAsGrid(); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
ArrayList<CellSet> common = new ArrayList<CellSet>(); |
| 678 |
common.add(set); |
| 679 |
Iterator it2 = filledSets.iterator(); |
| 680 |
while(it2.hasNext()){ |
| 681 |
CellSet set2 = (CellSet) it2.next(); |
| 682 |
if(set != set2 && set.hasCommonCells(set2)){ |
| 683 |
common.add(set2); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
if(common.size() == 2) continue; |
| 688 |
|
| 689 |
|
| 690 |
CellSet largest = set; |
| 691 |
it2 = common.iterator(); |
| 692 |
while(it2.hasNext()){ |
| 693 |
CellSet set2 = (CellSet) it2.next(); |
| 694 |
if(set2.size() > largest.size()){ |
| 695 |
largest = set2; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| •699 |
if(DEBUG_VERBOSE){ |
| 700 |
System.out.println("Largest:"); |
| 701 |
largest.printAsGrid(); |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
common.remove(largest); |
| 706 |
|
| 707 |
|
| 708 |
TextGrid gridOfSmalls = new TextGrid(largest.getMaxX() + 2, largest.getMaxY() + 2); |
| 709 |
CellSet sumOfSmall = new CellSet(); |
| 710 |
it2 = common.iterator(); |
| 711 |
while(it2.hasNext()){ |
| 712 |
CellSet set2 = (CellSet) it2.next(); |
| •713 |
if(DEBUG_VERBOSE){ |
| 714 |
System.out.println("One of smalls:"); |
| 715 |
set2.printAsGrid(); |
| 716 |
} |
| 717 |
gridOfSmalls.fillCellsWith(set2, '*'); |
| 718 |
} |
| •719 |
if(DEBUG_VERBOSE){ |
| 720 |
System.out.println("Sum of smalls:"); |
| 721 |
gridOfSmalls.printDebug(); |
| 722 |
} |
| 723 |
TextGrid gridLargest = new TextGrid(largest.getMaxX() + 2, largest.getMaxY() + 2); |
| 724 |
gridLargest.fillCellsWith(largest, '*'); |
| 725 |
|
| 726 |
int index = filledSets.indexOf(largest); |
| 727 |
if(gridLargest.equals(gridOfSmalls) |
| 728 |
&& !toBeRemovedIndices.contains(new Integer(index))) { |
| 729 |
toBeRemovedIndices.add(new Integer(index)); |
| 730 |
if (DEBUG){ |
| 731 |
System.out.println("Decided to remove set:"); |
| 732 |
largest.printAsGrid(); |
| 733 |
} |
| •734 |
} |
| 735 |
|
| 736 |
|
| •737 |
|
| 738 |
|
| 739 |
} |
| 740 |
|
| 741 |
ArrayList<CellSet> setsToBeRemoved = new ArrayList<CellSet>(); |
| 742 |
it = toBeRemovedIndices.iterator(); |
| 743 |
while(it.hasNext()){ |
| 744 |
int i = ((Integer) it.next()).intValue(); |
| 745 |
setsToBeRemoved.add(sets.get(i)); |
| 746 |
} |
| 747 |
|
| 748 |
it = setsToBeRemoved.iterator(); |
| 749 |
while(it.hasNext()){ |
| 750 |
CellSet set = (CellSet) it.next(); |
| 751 |
removedAny = true; |
| 752 |
sets.remove(set); |
| 753 |
} |
| 754 |
|
| •755 |
if(DEBUG_VERBOSE) { |
| 756 |
System.out.println("******* Sets after *******"); |
| 757 |
it = sets.iterator(); |
| 758 |
while(it.hasNext()){ |
| 759 |
CellSet set = (CellSet) it.next(); |
| 760 |
set.printAsGrid(); |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
return removedAny; |
| 765 |
} |
| 766 |
|
| 767 |
public float getMinimumOfCellDimension(){ |
| 768 |
return Math.min(getCellWidth(), getCellHeight()); |
| 769 |
} |
| 770 |
|
| 771 |
private void separateCommonEdges(ArrayList shapes){ |
| 772 |
|
| 773 |
float offset = getMinimumOfCellDimension() / 5; |
| 774 |
|
| 775 |
ArrayList<ShapeEdge> edges = new ArrayList<ShapeEdge>(); |
| 776 |
|
| 777 |
|
| 778 |
Iterator it = shapes.iterator(); |
| 779 |
while (it.hasNext()) { |
| 780 |
DiagramShape shape = (DiagramShape) it.next(); |
| 781 |
edges.addAll(shape.getEdges()); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
ArrayList<Pair<ShapeEdge, ShapeEdge>> listOfPairs = new ArrayList<Pair<ShapeEdge, ShapeEdge>>(); |
| 786 |
it = edges.iterator(); |
| 787 |
|
| 788 |
|
| 789 |
int startIndex = 1; |
| 790 |
|
| 791 |
while(it.hasNext()){ |
| 792 |
ShapeEdge edge1 = (ShapeEdge) it.next(); |
| 793 |
|
| 794 |
for(int k = startIndex; k < edges.size(); k++) { |
| 795 |
ShapeEdge edge2 = edges.get(k); |
| 796 |
|
| 797 |
if(edge1.touchesWith(edge2)) { |
| 798 |
listOfPairs.add(new Pair<ShapeEdge, ShapeEdge>(edge1, edge2)); |
| 799 |
} |
| 800 |
} |
| 801 |
startIndex++; |
| 802 |
} |
| 803 |
|
| 804 |
ArrayList<ShapeEdge> movedEdges = new ArrayList<ShapeEdge>(); |
| 805 |
|
| 806 |
|
| 807 |
it = listOfPairs.iterator(); |
| 808 |
while(it.hasNext()){ |
| 809 |
Pair<ShapeEdge, ShapeEdge> pair = (Pair<ShapeEdge, ShapeEdge>) it.next(); |
| 810 |
if(!movedEdges.contains(pair.first)) { |
| 811 |
pair.first.moveInwardsBy(offset); |
| 812 |
movedEdges.add(pair.first); |
| 813 |
} |
| 814 |
if(!movedEdges.contains(pair.second)) { |
| 815 |
pair.second.moveInwardsBy(offset); |
| 816 |
movedEdges.add(pair.second); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
private void removeDuplicateShapes() { |
| 825 |
ArrayList originalShapes = new ArrayList(); |
| 826 |
|
| 827 |
Iterator shapesIt = getShapesIterator(); |
| 828 |
while(shapesIt.hasNext()){ |
| 829 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 830 |
boolean isOriginal = true; |
| 831 |
Iterator originals = originalShapes.iterator(); |
| 832 |
while(originals.hasNext()){ |
| 833 |
DiagramShape originalShape = (DiagramShape) originals.next(); |
| 834 |
if(shape.equals(originalShape)){ |
| 835 |
isOriginal = false; |
| 836 |
} |
| 837 |
} |
| 838 |
if(isOriginal) originalShapes.add(shape); |
| 839 |
} |
| 840 |
|
| 841 |
shapes.clear(); |
| 842 |
shapes.addAll(originalShapes); |
| 843 |
} |
| 844 |
|
| •845 |
private DiagramShape findSmallestShapeContaining(ShapePoint point) { |
| 846 |
DiagramShape containingShape = null; |
| 847 |
Iterator<DiagramShape> shapes = getShapes().iterator(); |
| 848 |
while(shapes.hasNext()){ |
| 849 |
DiagramShape shape = shapes.next(); |
| 850 |
if(shape.contains(point)){ |
| 851 |
if(containingShape == null){ |
| 852 |
containingShape = shape; |
| 853 |
} else { |
| 854 |
if(shape.isSmallerThan(containingShape)){ |
| 855 |
containingShape = shape; |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
return containingShape; |
| 861 |
} |
| 862 |
|
| 863 |
private DiagramShape findSmallestShapeIntersecting(Rectangle2D rect) { |
| 864 |
DiagramShape intersectingShape = null; |
| 865 |
Iterator<DiagramShape> shapes = getShapes().iterator(); |
| 866 |
while(shapes.hasNext()){ |
| 867 |
DiagramShape shape = shapes.next(); |
| 868 |
if(shape.intersects(rect)){ |
| 869 |
if(intersectingShape == null){ |
| 870 |
intersectingShape = shape; |
| 871 |
} else { |
| 872 |
if(shape.isSmallerThan(intersectingShape)){ |
| 873 |
intersectingShape = shape; |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
} |
| 878 |
return intersectingShape; |
| 879 |
} |
| 880 |
|
| 881 |
private void addToTextObjects(DiagramText shape){ |
| 882 |
textObjects.add(shape); |
| 883 |
} |
| 884 |
|
| 885 |
private void addToCompositeShapes(CompositeDiagramShape shape){ |
| 886 |
compositeShapes.add(shape); |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
private void addToShapes(DiagramShape shape){ |
| 891 |
shapes.add(shape); |
| 892 |
} |
| 893 |
|
| 894 |
public Iterator getShapesIterator(){ |
| 895 |
return shapes.iterator(); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
public int getHeight() { |
| 902 |
return height; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
public int getWidth() { |
| 909 |
return width; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
public int getCellWidth() { |
| 916 |
return cellWidth; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public int getCellHeight() { |
| 923 |
return cellHeight; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| •929 |
public ArrayList<CompositeDiagramShape> getCompositeShapes() { |
| 930 |
return compositeShapes; |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| •936 |
public ArrayList<DiagramShape> getShapes() { |
| 937 |
return shapes; |
| 938 |
} |
| 939 |
|
| 940 |
public int getCellMinX(TextGrid.Cell cell){ |
| 941 |
return getCellMinX(cell, cellWidth); |
| 942 |
} |
| 943 |
public static int getCellMinX(TextGrid.Cell cell, int cellXSize){ |
| 944 |
return cell.x * cellXSize; |
| 945 |
} |
| 946 |
|
| 947 |
public int getCellMidX(TextGrid.Cell cell){ |
| 948 |
return getCellMidX(cell, cellWidth); |
| 949 |
} |
| 950 |
public static int getCellMidX(TextGrid.Cell cell, int cellXSize){ |
| 951 |
return cell.x * cellXSize + cellXSize / 2; |
| 952 |
} |
| 953 |
|
| 954 |
public int getCellMaxX(TextGrid.Cell cell){ |
| 955 |
return getCellMaxX(cell, cellWidth); |
| 956 |
} |
| 957 |
public static int getCellMaxX(TextGrid.Cell cell, int cellXSize){ |
| 958 |
return cell.x * cellXSize + cellXSize; |
| 959 |
} |
| 960 |
|
| 961 |
public int getCellMinY(TextGrid.Cell cell){ |
| 962 |
return getCellMinY(cell, cellHeight); |
| 963 |
} |
| 964 |
public static int getCellMinY(TextGrid.Cell cell, int cellYSize){ |
| 965 |
return cell.y * cellYSize; |
| 966 |
} |
| 967 |
|
| 968 |
public int getCellMidY(TextGrid.Cell cell){ |
| 969 |
return getCellMidY(cell, cellHeight); |
| 970 |
} |
| 971 |
public static int getCellMidY(TextGrid.Cell cell, int cellYSize){ |
| 972 |
return cell.y * cellYSize + cellYSize / 2; |
| 973 |
} |
| 974 |
|
| 975 |
public int getCellMaxY(TextGrid.Cell cell){ |
| 976 |
return getCellMaxY(cell, cellHeight); |
| 977 |
} |
| 978 |
public static int getCellMaxY(TextGrid.Cell cell, int cellYSize){ |
| 979 |
return cell.y * cellYSize + cellYSize; |
| 980 |
} |
| 981 |
|
| 982 |
public TextGrid.Cell getCellFor(ShapePoint point){ |
| 983 |
if(point == null) throw new IllegalArgumentException("ShapePoint cannot be null"); |
| 984 |
|
| 985 |
TextGrid g = new TextGrid(); |
| 986 |
return g.new Cell((int) point.x / cellWidth, |
| 987 |
(int) point.y / cellHeight); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| •994 |
public ArrayList<DiagramText> getTextObjects() { |
| 995 |
return textObjects; |
| 996 |
} |
| 997 |
|
| 998 |
} |