| 1 |
/* |
| 2 |
* DiTAA - Diagrams Through Ascii Art |
| 3 |
* |
| 4 |
* Copyright (C) 2004 Efstathios Sideris |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.stathissideris.ascii2image.graphics; |
| 22 |
|
| 23 |
import java.awt.Color; |
| 24 |
import java.awt.Font; |
| 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.core.ProcessingOptions; |
| 31 |
import org.stathissideris.ascii2image.text.AbstractionGrid; |
| 32 |
import org.stathissideris.ascii2image.text.CellSet; |
| 33 |
import org.stathissideris.ascii2image.text.TextGrid; |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
* @author Efstathios Sideris |
| 38 |
*/ |
| 39 |
public class Diagram { |
| 40 |
|
| 41 |
private static final boolean DEBUG = false; |
| 42 |
private static final boolean VERBOSE_DEBUG = false; |
| 43 |
|
| 44 |
private ArrayList shapes = new ArrayList(); |
| 45 |
private ArrayList compositeShapes = new ArrayList(); |
| 46 |
private ArrayList textObjects = new ArrayList(); |
| 47 |
|
| 48 |
private int width, height; |
| 49 |
private int cellWidth, cellHeight; |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* |
| 54 |
* <p>An outline of the inner workings of this very important (and monstrous) |
| 55 |
* constructor is presented here. Boundary processing is the first step |
| 56 |
* of the process:</p> |
| 57 |
* |
| 58 |
* <ol> |
| 59 |
* <li>Copy the grid into a work grid and remove all type-on-line |
| 60 |
* and point markers from the work grid</li> |
| 61 |
* <li>Split grid into distinct shapes by plotting the grid |
| 62 |
* onto an AbstractionGrid and its getDistinctShapes() method.</li> |
| 63 |
* <li>Find all the possible boundary sets of each of the |
| 64 |
* distinct shapes. This can produce duplicate shapes (if the boundaries |
| 65 |
* are the same when filling from the inside and the outside).</li> |
| 66 |
* <li>Remove duplicate boundaries.</li> |
| 67 |
* <li>Remove obsolete boundaries. Obsolete boundaries are the ones that are |
| 68 |
* the sum of their parts when plotted as filled shapes. (see method |
| 69 |
* removeObsoleteShapes())</li> |
| 70 |
* <li>Seperate the found boundary sets to open, closed or mixed |
| 71 |
* (See CellSet class on how its done).</li> |
| 72 |
* <li>Are there any closed boundaries? |
| 73 |
* <ul> |
| 74 |
* <li>YES. Subtract all the closed boundaries from each of the |
| 75 |
* open ones. That should convert the mixed shapes into open.</li> |
| 76 |
* <li>NO. In this (harder) case, we use the method |
| 77 |
* breakTrulyMixedBoundaries() of CellSet to break boundaries |
| 78 |
* into open and closed shapes (would work in any case, but it's |
| 79 |
* probably slower than the other method). This method is based |
| 80 |
* on tracing from the lines' ends and splitting when we get to |
| 81 |
* an intersection.</li> |
| 82 |
* </ul> |
| 83 |
* </li> |
| 84 |
* <li>If we had to eliminate any mixed shapes, we seperate the found |
| 85 |
* boundary sets again to open, closed or mixed.</li> |
| 86 |
* </ol> |
| 87 |
* |
| 88 |
* <p>At this stage, the boundary processing is all complete and we |
| 89 |
* proceed with using those boundaries to create the shapes:</p> |
| 90 |
* |
| 91 |
* <ol> |
| 92 |
* <li>Create closed shapes.</li> |
| 93 |
* <li>Create open shapes. That's when the line end corrections are |
| 94 |
* also applied, concerning the positioning of the ends of lines |
| 95 |
* see methods connectEndsToAnchors() and moveEndsToCellEdges() of |
| 96 |
* DiagramShape.</li> |
| 97 |
* <li>Assign color codes to closed shapes.</li> |
| 98 |
* <li>Assing extended markup tags to closed shapes.</p> |
| 99 |
* <li>Create arrowheads.</p> |
| 100 |
* <li>Create point markers.</p> |
| 101 |
* </ol> |
| 102 |
* |
| 103 |
* <p>Finally, the text processing occurs: [pending]</p> |
| 104 |
* |
| 105 |
* @param grid |
| 106 |
* @param cellWidth |
| 107 |
* @param cellHeight |
| 108 |
*/ |
| 109 |
public Diagram(TextGrid grid, ConversionOptions options, ProcessingOptions processingOptions) { |
| 110 |
|
| 111 |
this.cellWidth = options.renderingOptions.getCellWidth(); |
| 112 |
this.cellHeight = options.renderingOptions.getCellHeight(); |
| 113 |
|
| 114 |
width = grid.getWidth() * cellWidth; |
| 115 |
height = grid.getHeight() * cellHeight; |
| 116 |
|
| 117 |
TextGrid workGrid = new TextGrid(grid); |
| 118 |
workGrid.replaceTypeOnLine(); |
| 119 |
workGrid.replacePointMarkersOnLine(); |
| 120 |
if(DEBUG) workGrid.printDebug(); |
| 121 |
|
| 122 |
int width = grid.getWidth(); |
| 123 |
int height = grid.getHeight(); |
| 124 |
|
| 125 |
|
| 126 |
//split distinct shapes using AbstractionGrid |
| 127 |
AbstractionGrid temp = new AbstractionGrid(workGrid, workGrid.getAllBoundaries()); |
| 128 |
ArrayList boundarySetsStep1 = temp.getDistinctShapes(); |
| 129 |
|
| 130 |
if(DEBUG){ |
| 131 |
System.out.println("******* Distinct shapes found using AbstractionGrid *******"); |
| 132 |
Iterator dit = boundarySetsStep1.iterator(); |
| 133 |
while (dit.hasNext()) { |
| 134 |
CellSet set = (CellSet) dit.next(); |
| 135 |
set.printAsGrid(); |
| 136 |
} |
| 137 |
System.out.println("******* Same set of shapes after processing them by filling *******"); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
//Find all the boundaries by using the special version of the filling method |
| 142 |
//(fills in a different buffer than the buffer it reads from) |
| 143 |
ArrayList boundarySetsStep2 = new ArrayList(); |
| 144 |
Iterator boundarySetIt = boundarySetsStep1.iterator(); |
| 145 |
while (boundarySetIt.hasNext()) { |
| 146 |
CellSet set = (CellSet) boundarySetIt.next(); |
| 147 |
|
| 148 |
//the fill buffer keeps track of which cells have been |
| 149 |
//filled already |
| 150 |
TextGrid fillBuffer = new TextGrid(width * 3, height * 3); |
| 151 |
|
| 152 |
for(int yi = 0; yi < height * 3; yi++){ |
| 153 |
for(int xi = 0; xi < width * 3; xi++){ |
| 154 |
if(fillBuffer.isBlank(xi, yi)){ |
| 155 |
|
| 156 |
TextGrid copyGrid = new AbstractionGrid(workGrid, set).getCopyOfInternalBuffer(); |
| 157 |
|
| 158 |
CellSet boundaries = |
| 159 |
copyGrid |
| 160 |
.findBoundariesExpandingFrom(copyGrid.new Cell(xi, yi)); |
| 161 |
if(boundaries.size() == 0) continue; //i'm not sure why these occur |
| 162 |
boundarySetsStep2.add(boundaries.makeScaledOneThirdEquivalent()); |
| 163 |
|
| 164 |
copyGrid = new AbstractionGrid(workGrid, set).getCopyOfInternalBuffer(); |
| 165 |
CellSet filled = |
| 166 |
copyGrid |
| 167 |
.fillContinuousArea(copyGrid.new Cell(xi, yi), '*'); |
| 168 |
fillBuffer.fillCellsWith(filled, '*'); |
| 169 |
fillBuffer.fillCellsWith(boundaries, '-'); |
| 170 |
|
| 171 |
if(DEBUG){ |
| 172 |
//System.out.println("Fill buffer:"); |
| 173 |
//fillBuffer.printDebug(); |
| 174 |
boundaries.makeScaledOneThirdEquivalent().printAsGrid(); |
| 175 |
System.out.println("-----------------------------------"); |
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (DEBUG) |
| 184 |
System.out.println("******* Removed duplicates *******"); |
| 185 |
|
| 186 |
boundarySetsStep2 = CellSet.removeDuplicateSets(boundarySetsStep2); |
| 187 |
|
| 188 |
if(DEBUG){ |
| 189 |
Iterator dit = boundarySetsStep2.iterator(); |
| 190 |
while (dit.hasNext()) { |
| 191 |
CellSet set = (CellSet) dit.next(); |
| 192 |
set.printAsGrid(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
int originalSize = boundarySetsStep2.size(); |
| 197 |
boundarySetsStep2 = CellSet.removeDuplicateSets(boundarySetsStep2); |
| 198 |
if(DEBUG) { |
| 199 |
System.out.println( |
| 200 |
"******* Removed duplicates: there were " |
| 201 |
+originalSize |
| 202 |
+" shapes and now there are " |
| 203 |
+boundarySetsStep2.size()); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
//split boundaries to open, closed and mixed |
| 208 |
|
| 209 |
if (DEBUG) |
| 210 |
System.out.println("******* First evaluation of openess *******"); |
| 211 |
|
| 212 |
ArrayList open = new ArrayList(); |
| 213 |
ArrayList closed = new ArrayList(); |
| 214 |
ArrayList mixed = new ArrayList(); |
| 215 |
|
| 216 |
Iterator sets = boundarySetsStep2.iterator(); |
| 217 |
while(sets.hasNext()){ |
| 218 |
CellSet set = (CellSet) sets.next(); |
| 219 |
int type = set.getType(workGrid); |
| 220 |
if(type == CellSet.TYPE_CLOSED) closed.add(set); |
| 221 |
else if(type == CellSet.TYPE_OPEN) open.add(set); |
| 222 |
else if(type == CellSet.TYPE_MIXED) mixed.add(set); |
| 223 |
if(DEBUG){ |
| 224 |
if(type == CellSet.TYPE_CLOSED) System.out.println("Closed boundaries:"); |
| 225 |
else if(type == CellSet.TYPE_OPEN) System.out.println("Open boundaries:"); |
| 226 |
else if(type == CellSet.TYPE_MIXED) System.out.println("Mixed boundaries:"); |
| 227 |
set.printAsGrid(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
boolean hadToEliminateMixed = false; |
| 232 |
|
| 233 |
if(mixed.size() > 0 && closed.size() > 0) { |
| 234 |
// mixed shapes can be eliminated by |
| 235 |
// subtracting all the closed shapes from them |
| 236 |
if (DEBUG) |
| 237 |
System.out.println("******* Eliminating mixed shapes (basic algorithm) *******"); |
| 238 |
|
| 239 |
hadToEliminateMixed = true; |
| 240 |
|
| 241 |
//subtract from each of the mixed sets all the closed sets |
| 242 |
sets = mixed.iterator(); |
| 243 |
while(sets.hasNext()){ |
| 244 |
CellSet set = (CellSet) sets.next(); |
| 245 |
Iterator closedSets = closed.iterator(); |
| 246 |
while(closedSets.hasNext()){ |
| 247 |
CellSet closedSet = (CellSet) closedSets.next(); |
| 248 |
set.subtractSet(closedSet); |
| 249 |
} |
| 250 |
// this is necessary because some mixed sets produce |
| 251 |
// several distinct open sets after you subtract the |
| 252 |
// closed sets from them |
| 253 |
if(set.getType(workGrid) == CellSet.TYPE_OPEN) { |
| 254 |
boundarySetsStep2.remove(set); |
| 255 |
boundarySetsStep2.addAll(set.breakIntoDistinctBoundaries(workGrid)); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
} else if(mixed.size() > 0 && closed.size() == 0) { |
| 260 |
// no closed shape exists, will have to |
| 261 |
// handle mixed shape on its own |
| 262 |
// an example of this case is the following: |
| 263 |
// +-----+ |
| 264 |
// | A |C B |
| 265 |
// + ---+------------------- |
| 266 |
// | | |
| 267 |
// +-----+ |
| 268 |
|
| 269 |
hadToEliminateMixed = true; |
| 270 |
|
| 271 |
if (DEBUG) |
| 272 |
System.out.println("******* Eliminating mixed shapes (advanced algorithm for truly mixed shapes) *******"); |
| 273 |
|
| 274 |
sets = mixed.iterator(); |
| 275 |
while(sets.hasNext()){ |
| 276 |
CellSet set = (CellSet) sets.next(); |
| 277 |
boundarySetsStep2.remove(set); |
| 278 |
boundarySetsStep2.addAll(set.breakTrulyMixedBoundaries(workGrid)); |
| 279 |
} |
| 280 |
|
| 281 |
} else { |
| 282 |
if (DEBUG) |
| 283 |
System.out.println("No mixed shapes found. Skipped mixed shape elimination step"); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
if(hadToEliminateMixed){ |
| 288 |
if (DEBUG) |
| 289 |
System.out.println("******* Second evaluation of openess *******"); |
| 290 |
|
| 291 |
//split boundaries again to open, closed and mixed |
| 292 |
open = new ArrayList(); |
| 293 |
closed = new ArrayList(); |
| 294 |
mixed = new ArrayList(); |
| 295 |
|
| 296 |
sets = boundarySetsStep2.iterator(); |
| 297 |
while(sets.hasNext()){ |
| 298 |
CellSet set = (CellSet) sets.next(); |
| 299 |
int type = set.getType(workGrid); |
| 300 |
if(type == CellSet.TYPE_CLOSED) closed.add(set); |
| 301 |
else if(type == CellSet.TYPE_OPEN) open.add(set); |
| 302 |
else if(type == CellSet.TYPE_MIXED) mixed.add(set); |
| 303 |
if(DEBUG){ |
| 304 |
if(type == CellSet.TYPE_CLOSED) System.out.println("Closed boundaries:"); |
| 305 |
else if(type == CellSet.TYPE_OPEN) System.out.println("Open boundaries:"); |
| 306 |
else if(type == CellSet.TYPE_MIXED) System.out.println("Mixed boundaries:"); |
| 307 |
set.printAsGrid(); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
boolean removedAnyObsolete = removeObsoleteShapes(workGrid, closed); |
| 313 |
|
| 314 |
boolean allCornersRound = false; |
| 315 |
if(processingOptions.areAllCornersRound()) allCornersRound = true; |
| 316 |
|
| 317 |
//make shapes from the boundary sets |
| 318 |
//make closed shapes |
| 319 |
ArrayList closedShapes = new ArrayList(); |
| 320 |
sets = closed.iterator(); |
| 321 |
while(sets.hasNext()){ |
| 322 |
CellSet set = (CellSet) sets.next(); |
| 323 |
DiagramComponent shape = DiagramComponent.createClosedFromBoundaryCells(workGrid, set, cellWidth, cellHeight, allCornersRound); |
| 324 |
if(shape != null){ |
| 325 |
if(shape instanceof DiagramShape){ |
| 326 |
addToShapes((DiagramShape) shape); |
| 327 |
closedShapes.add(shape); |
| 328 |
} else if(shape instanceof CompositeDiagramShape) |
| 329 |
addToCompositeShapes((CompositeDiagramShape) shape); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if(processingOptions.performSeparationOfCommonEdges()) |
| 334 |
separateCommonEdges(closedShapes); |
| 335 |
|
| 336 |
//make open shapes |
| 337 |
sets = open.iterator(); |
| 338 |
while(sets.hasNext()){ |
| 339 |
CellSet set = (CellSet) sets.next(); |
| 340 |
if(set.size() == 1){ //single cell "shape" |
| 341 |
TextGrid.Cell cell = (TextGrid.Cell) set.getFirst(); |
| 342 |
if(!grid.cellContainsDashedLineChar(cell)) { |
| 343 |
DiagramShape shape = DiagramShape.createSmallLine(workGrid, cell, cellWidth, cellHeight); |
| 344 |
if(shape != null) { |
| 345 |
addToShapes(shape); |
| 346 |
shape.connectEndsToAnchors(workGrid, this); |
| 347 |
} |
| 348 |
} |
| 349 |
} else { //normal shape |
| 350 |
DiagramComponent shape = |
| 351 |
CompositeDiagramShape |
| 352 |
.createOpenFromBoundaryCells( |
| 353 |
workGrid, set, cellWidth, cellHeight, allCornersRound); |
| 354 |
|
| 355 |
if(shape != null){ |
| 356 |
if(shape instanceof CompositeDiagramShape){ |
| 357 |
addToCompositeShapes((CompositeDiagramShape) shape); |
| 358 |
((CompositeDiagramShape) shape).connectEndsToAnchors(workGrid, this); |
| 359 |
} else if(shape instanceof DiagramShape) { |
| 360 |
addToShapes((DiagramShape) shape); |
| 361 |
((DiagramShape) shape).connectEndsToAnchors(workGrid, this); |
| 362 |
((DiagramShape) shape).moveEndsToCellEdges(grid, this); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
//assign color codes to shapes |
| 370 |
//TODO: text on line should not change its color |
| 371 |
//TODO: each color tag should be assigned to the smallest containing shape (like shape tags) |
| 372 |
|
| 373 |
Iterator cellColorPairs = grid.findColorCodes().iterator(); |
| 374 |
while(cellColorPairs.hasNext()){ |
| 375 |
TextGrid.CellColorPair pair = |
| 376 |
(TextGrid.CellColorPair) cellColorPairs.next(); |
| 377 |
ShapePoint point = |
| 378 |
new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell)); |
| 379 |
Iterator shapes = getShapes().iterator(); |
| 380 |
while(shapes.hasNext()){ |
| 381 |
DiagramShape shape = (DiagramShape) shapes.next(); |
| 382 |
if(shape.contains(point)) shape.setFillColor(pair.color); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
//assign markup to shapes |
| 387 |
Iterator cellTagPairs = grid.findMarkupTags().iterator(); |
| 388 |
while(cellTagPairs.hasNext()){ |
| 389 |
TextGrid.CellTagPair pair = |
| 390 |
(TextGrid.CellTagPair) cellTagPairs.next(); |
| 391 |
ShapePoint point = |
| 392 |
new ShapePoint(getCellMidX(pair.cell), getCellMidY(pair.cell)); |
| 393 |
|
| 394 |
//find the smallest shape that contains the tag |
| 395 |
DiagramShape containingShape = null; |
| 396 |
Iterator shapes = getShapes().iterator(); |
| 397 |
while(shapes.hasNext()){ |
| 398 |
DiagramShape shape = (DiagramShape) shapes.next(); |
| 399 |
if(shape.contains(point)){ |
| 400 |
if(containingShape == null){ |
| 401 |
containingShape = shape; |
| 402 |
} else { |
| 403 |
if(shape.isSmallerThan(containingShape)){ |
| 404 |
containingShape = shape; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
//this tag is not within a shape, skip |
| 411 |
if(containingShape == null) continue; |
| 412 |
|
| 413 |
//TODO: the code below could be a lot more concise |
| 414 |
if(pair.tag.equals("d")){ |
| 415 |
CustomShapeDefinition def = |
| 416 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
processingOptions.getFromCustomShapes(pair.tag); |
| 480 |
containingShape.setType(DiagramShape.TYPE_CUSTOM); |
| 481 |
containingShape.setDefinition(def); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
//make arrowheads |
| 486 |
Iterator arrowheadCells = workGrid.findArrowheads().iterator(); |
| 487 |
while(arrowheadCells.hasNext()){ |
| 488 |
TextGrid.Cell cell = (TextGrid.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 |
//make point markers |
| 495 |
Iterator markersIt = grid.getPointMarkersOnLine().iterator(); |
| 496 |
while (markersIt.hasNext()) { |
| 497 |
TextGrid.Cell cell = (TextGrid.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 |
//copy again |
| 515 |
workGrid = new TextGrid(grid); |
| 516 |
workGrid.removeNonText(); |
| 517 |
|
| 518 |
|
| 519 |
// ****** handle text ******* |
| 520 |
//break up text into groups |
| 521 |
TextGrid textGroupGrid = new TextGrid(workGrid); |
| 522 |
CellSet gaps = textGroupGrid.getAllBlanksBetweenCharacters(); |
| 523 |
//kludge |
| 524 |
textGroupGrid.fillCellsWith(gaps, '|'); |
| 525 |
CellSet nonBlank = textGroupGrid.getAllNonBlank(); |
| 526 |
ArrayList 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 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 strings = isolationGrid.findStrings(); |
| 539 |
Iterator it = strings.iterator(); |
| 540 |
while(it.hasNext()){ |
| 541 |
TextGrid.CellStringPair pair = (TextGrid.CellStringPair) 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){ //does not fit horizontally |
| 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 |
//TODO: if the strings start with bullets they should be aligned to the left |
| 561 |
|
| 562 |
//position text correctly |
| 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 |
//correct the color of the text objects according |
| 585 |
//to the underlying color |
| 586 |
Iterator shapes = this.getAllDiagramShapes().iterator(); |
| 587 |
while(shapes.hasNext()){ |
| 588 |
DiagramShape shape = (DiagramShape) shapes.next(); |
| 589 |
Color fillColor = shape.getFillColor(); |
| 590 |
if(shape.isClosed() |
| 591 |
&& shape.getType() != DiagramShape.TYPE_ARROWHEAD |
| 592 |
&& fillColor != null |
| 593 |
&& BitmapRenderer.isColorDark(fillColor)){ |
| 594 |
Iterator textObjects = getTextObjects().iterator(); |
| 595 |
while(textObjects.hasNext()){ |
| 596 |
DiagramText textObject = (DiagramText) textObjects.next(); |
| 597 |
if(shape.intersects(textObject.getBounds())){ |
| 598 |
textObject.setColor(Color.white); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
//set outline to true for test within custom shapes |
| 605 |
shapes = this.getAllDiagramShapes().iterator(); |
| 606 |
while(shapes.hasNext()){ |
| 607 |
DiagramShape shape = (DiagramShape) shapes.next(); |
| 608 |
if(shape.getType() == DiagramShape.TYPE_CUSTOM){ |
| 609 |
Iterator textObjects = getTextObjects().iterator(); |
| 610 |
while(textObjects.hasNext()){ |
| 611 |
DiagramText textObject = (DiagramText) textObjects.next(); |
| 612 |
textObject.setHasOutline(true); |
| 613 |
textObject.setColor(DiagramText.DEFAULT_COLOR); |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
if (DEBUG) |
| 619 |
System.out.println("Corrected color of text according to underlying color"); |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Returns a list of all DiagramShapes in the Diagram, including |
| 625 |
* the ones within CompositeDiagramShapes |
| 626 |
* |
| 627 |
* @return |
| 628 |
*/ |
| 629 |
public ArrayList getAllDiagramShapes(){ |
| 630 |
ArrayList shapes = new ArrayList(); |
| 631 |
shapes.addAll(this.getShapes()); |
| 632 |
|
| 633 |
Iterator shapesIt = this.getCompositeShapes().iterator(); |
| 634 |
while(shapesIt.hasNext()){ |
| 635 |
CompositeDiagramShape compShape = (CompositeDiagramShape) shapesIt.next(); |
| 636 |
shapes.addAll(compShape.getShapes()); |
| 637 |
} |
| 638 |
return shapes; |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Removes the sets from <code>sets</code>that are the sum of their parts |
| 643 |
* when plotted as filled shapes. |
| 644 |
* |
| 645 |
* @return true if it removed any obsolete. |
| 646 |
* |
| 647 |
*/ |
| 648 |
private boolean removeObsoleteShapes(TextGrid grid, ArrayList<CellSet> sets){ |
| 649 |
if (DEBUG) |
| 650 |
System.out.println("******* Removing obsolete shapes *******"); |
| 651 |
|
| 652 |
boolean removedAny = false; |
| 653 |
|
| 654 |
ArrayList<CellSet> filledSets = new ArrayList<CellSet>(); |
| 655 |
|
| 656 |
Iterator it; |
| 657 |
|
| 658 |
if(VERBOSE_DEBUG) { |
| 659 |
System.out.println("******* Sets before *******"); |
| 660 |
it = sets.iterator(); |
| 661 |
while(it.hasNext()){ |
| 662 |
CellSet set = (CellSet) it.next(); |
| 663 |
set.printAsGrid(); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
//make filled versions of all the boundary sets |
| 668 |
it = sets.iterator(); |
| 669 |
while(it.hasNext()){ |
| 670 |
CellSet set = (CellSet) it.next(); |
| 671 |
set = set.getFilledEquivalent(grid); |
| 672 |
if(set == null){ |
| 673 |
return false; |
| 674 |
} else filledSets.add(set); |
| 675 |
} |
| 676 |
|
| 677 |
ArrayList<Integer> toBeRemovedIndices = new ArrayList<Integer>(); |
| 678 |
it = filledSets.iterator(); |
| 679 |
while(it.hasNext()){ |
| 680 |
CellSet set = (CellSet) it.next(); |
| 681 |
|
| 682 |
if(VERBOSE_DEBUG){ |
| 683 |
System.out.println("*** Deciding if the following should be removed:"); |
| 684 |
set.printAsGrid(); |
| 685 |
} |
| 686 |
|
| 687 |
//find the other sets that have common cells with set |
| 688 |
ArrayList<CellSet> common = new ArrayList<CellSet>(); |
| 689 |
common.add(set); |
| 690 |
Iterator it2 = filledSets.iterator(); |
| 691 |
while(it2.hasNext()){ |
| 692 |
CellSet set2 = (CellSet) it2.next(); |
| 693 |
if(set != set2 && set.hasCommonCells(set2)){ |
| 694 |
common.add(set2); |
| 695 |
} |
| 696 |
} |
| 697 |
//it only makes sense for more than 2 sets |
| 698 |
if(common.size() == 2) continue; |
| 699 |
|
| 700 |
//find largest set |
| 701 |
CellSet largest = set; |
| 702 |
it2 = common.iterator(); |
| 703 |
while(it2.hasNext()){ |
| 704 |
CellSet set2 = (CellSet) it2.next(); |
| 705 |
if(set2.size() > largest.size()){ |
| 706 |
largest = set2; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if(VERBOSE_DEBUG){ |
| 711 |
System.out.println("Largest:"); |
| 712 |
largest.printAsGrid(); |
| 713 |
} |
| 714 |
|
| 715 |
//see if largest is sum of others |
| 716 |
common.remove(largest); |
| 717 |
|
| 718 |
//make the sum set of the small sets on a grid |
| 719 |
TextGrid gridOfSmalls = new TextGrid(largest.getMaxX() + 2, largest.getMaxY() + 2); |
| 720 |
CellSet sumOfSmall = new CellSet(); |
| 721 |
it2 = common.iterator(); |
| 722 |
while(it2.hasNext()){ |
| 723 |
CellSet set2 = (CellSet) it2.next(); |
| 724 |
if(VERBOSE_DEBUG){ |
| 725 |
System.out.println("One of smalls:"); |
| 726 |
set2.printAsGrid(); |
| 727 |
} |
| 728 |
gridOfSmalls.fillCellsWith(set2, '*'); |
| 729 |
} |
| 730 |
if(VERBOSE_DEBUG){ |
| 731 |
System.out.println("Sum of smalls:"); |
| 732 |
gridOfSmalls.printDebug(); |
| 733 |
} |
| 734 |
TextGrid gridLargest = new TextGrid(largest.getMaxX() + 2, largest.getMaxY() + 2); |
| 735 |
gridLargest.fillCellsWith(largest, '*'); |
| 736 |
|
| 737 |
int index = filledSets.indexOf(largest); |
| 738 |
if(gridLargest.equals(gridOfSmalls) |
| 739 |
&& !toBeRemovedIndices.contains(new Integer(index))) { |
| 740 |
toBeRemovedIndices.add(new Integer(index)); |
| 741 |
if (DEBUG){ |
| 742 |
System.out.println("Decided to remove set:"); |
| 743 |
largest.printAsGrid(); |
| 744 |
} |
| 745 |
} else if (DEBUG){ |
| 746 |
System.out.println("This set WILL NOT be removed:"); |
| 747 |
largest.printAsGrid(); |
| 748 |
} |
| 749 |
//if(gridLargest.equals(gridOfSmalls)) toBeRemovedIndices.add(new Integer(index)); |
| 750 |
} |
| 751 |
|
| 752 |
ArrayList<CellSet> setsToBeRemoved = new ArrayList<CellSet>(); |
| 753 |
it = toBeRemovedIndices.iterator(); |
| 754 |
while(it.hasNext()){ |
| 755 |
int i = ((Integer) it.next()).intValue(); |
| 756 |
setsToBeRemoved.add(sets.get(i)); |
| 757 |
} |
| 758 |
|
| 759 |
it = setsToBeRemoved.iterator(); |
| 760 |
while(it.hasNext()){ |
| 761 |
CellSet set = (CellSet) it.next(); |
| 762 |
removedAny = true; |
| 763 |
sets.remove(set); |
| 764 |
} |
| 765 |
|
| 766 |
if(VERBOSE_DEBUG) { |
| 767 |
System.out.println("******* Sets after *******"); |
| 768 |
it = sets.iterator(); |
| 769 |
while(it.hasNext()){ |
| 770 |
CellSet set = (CellSet) it.next(); |
| 771 |
set.printAsGrid(); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
return removedAny; |
| 776 |
} |
| 777 |
|
| 778 |
public float getMinimumOfCellDimension(){ |
| 779 |
return Math.min(getCellWidth(), getCellHeight()); |
| 780 |
} |
| 781 |
|
| 782 |
private void separateCommonEdges(ArrayList shapes){ |
| 783 |
|
| 784 |
float offset = getMinimumOfCellDimension() / 5; |
| 785 |
|
| 786 |
ArrayList<ShapeEdge> edges = new ArrayList<ShapeEdge>(); |
| 787 |
|
| 788 |
//get all adges |
| 789 |
Iterator it = shapes.iterator(); |
| 790 |
while (it.hasNext()) { |
| 791 |
DiagramShape shape = (DiagramShape) it.next(); |
| 792 |
edges.addAll(shape.getEdges()); |
| 793 |
} |
| 794 |
|
| 795 |
//group edges into pairs of touching edges |
| 796 |
ArrayList<Pair<ShapeEdge, ShapeEdge>> listOfPairs = new ArrayList<Pair<ShapeEdge, ShapeEdge>>(); |
| 797 |
it = edges.iterator(); |
| 798 |
|
| 799 |
//all-against-all touching test for the edges |
| 800 |
int startIndex = 1; //skip some to avoid duplicate comparisons and self-to-self comparisons |
| 801 |
|
| 802 |
while(it.hasNext()){ |
| 803 |
ShapeEdge edge1 = (ShapeEdge) it.next(); |
| 804 |
|
| 805 |
for(int k = startIndex; k < edges.size(); k++) { |
| 806 |
ShapeEdge edge2 = edges.get(k); |
| 807 |
|
| 808 |
if(edge1.touchesWith(edge2)) { |
| 809 |
listOfPairs.add(new Pair<ShapeEdge, ShapeEdge>(edge1, edge2)); |
| 810 |
} |
| 811 |
} |
| 812 |
startIndex++; |
| 813 |
} |
| 814 |
|
| 815 |
ArrayList<ShapeEdge> movedEdges = new ArrayList<ShapeEdge>(); |
| 816 |
|
| 817 |
//move equivalent edges inwards |
| 818 |
it = listOfPairs.iterator(); |
| 819 |
while(it.hasNext()){ |
| 820 |
Pair<ShapeEdge, ShapeEdge> pair = (Pair<ShapeEdge, ShapeEdge>) it.next(); |
| 821 |
if(!movedEdges.contains(pair.first)) { |
| 822 |
pair.first.moveInwardsBy(offset); |
| 823 |
movedEdges.add(pair.first); |
| 824 |
} |
| 825 |
if(!movedEdges.contains(pair.second)) { |
| 826 |
pair.second.moveInwardsBy(offset); |
| 827 |
movedEdges.add(pair.second); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
//TODO: removes more than it should |
| 835 |
private void removeDuplicateShapes() { |
| 836 |
ArrayList originalShapes = new ArrayList(); |
| 837 |
|
| 838 |
Iterator shapesIt = getShapesIterator(); |
| 839 |
while(shapesIt.hasNext()){ |
| 840 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 841 |
boolean isOriginal = true; |
| 842 |
Iterator originals = originalShapes.iterator(); |
| 843 |
while(originals.hasNext()){ |
| 844 |
DiagramShape originalShape = (DiagramShape) originals.next(); |
| 845 |
if(shape.equals(originalShape)){ |
| 846 |
isOriginal = false; |
| 847 |
} |
| 848 |
} |
| 849 |
if(isOriginal) originalShapes.add(shape); |
| 850 |
} |
| 851 |
|
| 852 |
shapes.clear(); |
| 853 |
shapes.addAll(originalShapes); |
| 854 |
} |
| 855 |
|
| 856 |
private void addToTextObjects(DiagramText shape){ |
| 857 |
textObjects.add(shape); |
| 858 |
} |
| 859 |
|
| 860 |
private void addToCompositeShapes(CompositeDiagramShape shape){ |
| 861 |
compositeShapes.add(shape); |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
private void addToShapes(DiagramShape shape){ |
| 866 |
shapes.add(shape); |
| 867 |
} |
| 868 |
|
| 869 |
public Iterator getShapesIterator(){ |
| 870 |
return shapes.iterator(); |
| 871 |
} |
| 872 |
|
| 873 |
/** |
| 874 |
* @return |
| 875 |
*/ |
| 876 |
public int getHeight() { |
| 877 |
return height; |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
* @return |
| 882 |
*/ |
| 883 |
public int getWidth() { |
| 884 |
return width; |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* @return |
| 889 |
*/ |
| 890 |
public int getCellWidth() { |
| 891 |
return cellWidth; |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
* @return |
| 896 |
*/ |
| 897 |
public int getCellHeight() { |
| 898 |
return cellHeight; |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
* @return |
| 903 |
*/ |
| 904 |
public ArrayList getCompositeShapes() { |
| 905 |
return compositeShapes; |
| 906 |
} |
| 907 |
|
| 908 |
/** |
| 909 |
* @return |
| 910 |
*/ |
| 911 |
public ArrayList getShapes() { |
| 912 |
return shapes; |
| 913 |
} |
| 914 |
|
| 915 |
public int getCellMinX(TextGrid.Cell cell){ |
| 916 |
return getCellMinX(cell, cellWidth); |
| 917 |
} |
| 918 |
public static int getCellMinX(TextGrid.Cell cell, int cellXSize){ |
| 919 |
return cell.x * cellXSize; |
| 920 |
} |
| 921 |
|
| 922 |
public int getCellMidX(TextGrid.Cell cell){ |
| 923 |
return getCellMidX(cell, cellWidth); |
| 924 |
} |
| 925 |
public static int getCellMidX(TextGrid.Cell cell, int cellXSize){ |
| 926 |
return cell.x * cellXSize + cellXSize / 2; |
| 927 |
} |
| 928 |
|
| 929 |
public int getCellMaxX(TextGrid.Cell cell){ |
| 930 |
return getCellMaxX(cell, cellWidth); |
| 931 |
} |
| 932 |
public static int getCellMaxX(TextGrid.Cell cell, int cellXSize){ |
| 933 |
return cell.x * cellXSize + cellXSize; |
| 934 |
} |
| 935 |
|
| 936 |
public int getCellMinY(TextGrid.Cell cell){ |
| 937 |
return getCellMinY(cell, cellHeight); |
| 938 |
} |
| 939 |
public static int getCellMinY(TextGrid.Cell cell, int cellYSize){ |
| 940 |
return cell.y * cellYSize; |
| 941 |
} |
| 942 |
|
| 943 |
public int getCellMidY(TextGrid.Cell cell){ |
| 944 |
return getCellMidY(cell, cellHeight); |
| 945 |
} |
| 946 |
public static int getCellMidY(TextGrid.Cell cell, int cellYSize){ |
| 947 |
return cell.y * cellYSize + cellYSize / 2; |
| 948 |
} |
| 949 |
|
| 950 |
public int getCellMaxY(TextGrid.Cell cell){ |
| 951 |
return getCellMaxY(cell, cellHeight); |
| 952 |
} |
| 953 |
public static int getCellMaxY(TextGrid.Cell cell, int cellYSize){ |
| 954 |
return cell.y * cellYSize + cellYSize; |
| 955 |
} |
| 956 |
|
| 957 |
public TextGrid.Cell getCellFor(ShapePoint point){ |
| 958 |
if(point == null) throw new IllegalArgumentException("ShapePoint cannot be null"); |
| 959 |
//TODO: the fake grid is a problem |
| 960 |
TextGrid g = new TextGrid(); |
| 961 |
return g.new Cell((int) point.x / cellWidth, |
| 962 |
(int) point.y / cellHeight); |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
/** |
| 967 |
* @return |
| 968 |
*/ |
| 969 |
public ArrayList getTextObjects() { |
| 970 |
return textObjects; |
| 971 |
} |
| 972 |
|
| 973 |
} |