org.stathissideris.ascii2image.graphics.DiagramComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml-mit Show documentation
Show all versions of plantuml-mit Show documentation
PlantUML is a component that allows to quickly write diagrams from text.
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
package org.stathissideris.ascii2image.graphics;
import org.stathissideris.ascii2image.text.CellSet;
import org.stathissideris.ascii2image.text.TextGrid;
/**
*
* @author Efstathios Sideris
*/
public abstract class DiagramComponent {
private static final boolean DEBUG = false;
protected static ShapePoint makePointForCell(TextGrid.Cell cell, TextGrid grid, int cellWidth, int cellHeight, boolean allRound){
if (DEBUG)
System.out.println("Found point at cell "+cell);
if(grid.isCorner(cell) && allRound){
return new ShapePoint(
cell.x * cellWidth + cellWidth/2,
cell.y * cellHeight + cellHeight/2,
ShapePoint.TYPE_ROUND
);
} else if(grid.isNormalCorner(cell)){
return new ShapePoint(
cell.x * cellWidth + cellWidth/2,
cell.y * cellHeight + cellHeight/2,
ShapePoint.TYPE_NORMAL
);
} else if(grid.isRoundCorner(cell)){
return new ShapePoint(
cell.x * cellWidth + cellWidth/2,
cell.y * cellHeight + cellHeight/2,
ShapePoint.TYPE_ROUND
);
} else if(grid.isLinesEnd(cell)){
return new ShapePoint(
cell.x * cellWidth + cellWidth/2,
cell.y * cellHeight + cellHeight/2,
ShapePoint.TYPE_NORMAL
);
} else if(grid.isIntersection(cell)){
return new ShapePoint(
cell.x * cellWidth + cellWidth/2,
cell.y * cellHeight + cellHeight/2,
ShapePoint.TYPE_NORMAL
);
}
throw new RuntimeException("Cannot make point for cell "+cell);
}
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight){
return createClosedFromBoundaryCells(grid, cells, cellWidth, cellHeight, false);
}
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight, boolean allRound){
if(cells.getType(grid) == CellSet.TYPE_OPEN) throw new IllegalArgumentException("CellSet is closed and cannot be handled by this method");
if(cells.size() < 2) return null;
DiagramShape shape = new DiagramShape();
shape.setClosed(true);
if(grid.containsAtLeastOneDashedLine(cells)) shape.setStrokeDashed(true);
TextGrid workGrid = new TextGrid(grid.getWidth(), grid.getHeight());
grid.copyCellsTo(cells, workGrid);
if (DEBUG){
System.out.println("Making closed shape from buffer:");
workGrid.printDebug();
}
TextGrid.Cell start = (TextGrid.Cell) cells.getFirst();
if(workGrid.isCorner(start)) shape.addToPoints(makePointForCell(start, workGrid, cellWidth, cellHeight, allRound));
TextGrid.Cell previous = start;
TextGrid.Cell cell = null;
CellSet nextCells = workGrid.followCell(previous);
if(nextCells.size() == 0) return null;
cell = (TextGrid.Cell) nextCells.getFirst();
if(workGrid.isCorner(cell)) shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
while(!cell.equals(start)){
nextCells = workGrid.followCell(cell, previous);
if(nextCells.size() == 1) {
previous = cell;
cell = (TextGrid.Cell) nextCells.getFirst();
if(!cell.equals(start) && workGrid.isCorner(cell))
shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
} else if(nextCells.size() > 1) {
return null;
} else {
throw new RuntimeException("cannot create closed shape from boundary cells, nowhere to go from "
+ cell + " coming from " + previous + " in grid:\n" + grid
+"\nmaybe you have an edge pointing nowhere?");
}
}
return shape;
}
}