findMarkupTags(){
}
return result;
}
-
+
public void removeMarkupTags(){
Iterator it = findMarkupTags().iterator();
while (it.hasNext()) {
@@ -689,7 +689,7 @@ public void removeMarkupTags(){
writeStringTo(pair.cell, StringUtils.repeatString(" ", length));
}
}
-
+
public boolean matchesAny(GridPatternGroup criteria){
@@ -732,9 +732,9 @@ public boolean isBoundary(Cell cell){
if('+' == c || '\\' == c || '/' == c){
System.out.print("");
if(
- isIntersection(cell)
+ isIntersection(cell)
|| isCorner(cell)
- || isStub(cell)
+ || isStub(cell)
|| isCrossOnLine(cell)){
return true;
} else return false;
@@ -773,10 +773,10 @@ public boolean isVerticalLine(int x, int y){
public boolean isLinesEnd(int x, int y){
return isLinesEnd(new Cell(x, y));
}
-
+
/**
* Stubs are also considered end of lines
- *
+ *
* @param cell
* @return
*/
@@ -821,16 +821,16 @@ public boolean exactlyOneNeighbourIsBoundary(Cell cell) {
}
/**
- *
+ *
* A stub looks like that:
- *
+ *
*
- *
+ *
* +- or -+ or + or + or /- or -/ or / (you get the point)
* | | |
- *
+ *
*
- *
+ *
* @param cell
* @return
*/
@@ -874,7 +874,7 @@ public boolean isArrowhead(Cell cell){
|| isWestArrowhead(cell)
|| isEastArrowhead(cell));
}
-
+
public boolean isNorthArrowhead(Cell cell){
return get(cell) == '^';
}
@@ -891,8 +891,8 @@ public boolean isSouthArrowhead(Cell cell){
return (get(cell) == 'v' || get(cell) == 'V')
&& isVerticalLine(cell.getNorth());
}
-
-
+
+
// unicode for bullets
//
// 2022 bullet
@@ -907,7 +907,7 @@ public boolean isSouthArrowhead(Cell cell){
public boolean isBullet(int x, int y){
return isBullet(new Cell(x, y));
}
-
+
public boolean isBullet(Cell cell){
char c = get(cell);
if((c == 'o' || c == '*')
@@ -917,7 +917,7 @@ && isBlank(cell.getWest())
return true;
return false;
}
-
+
public void replaceBullets(){
int width = getWidth();
int height = getHeight();
@@ -931,11 +931,11 @@ public void replaceBullets(){
}
}
}
-
+
/**
* true if the cell is not blank
* but the previous (west) is
- *
+ *
* @param cell
* @return
*/
@@ -946,7 +946,7 @@ public boolean isStringsStart(Cell cell){
/**
* true if the cell is not blank
* but the next (east) is
- *
+ *
* @param cell
* @return
*/
@@ -1017,7 +1017,7 @@ public CellSet followIntersection(Cell cell, Cell blocked){
/**
* Returns the neighbours of a line-cell that are boundaries
* (0 to 2 cells are returned)
- *
+ *
* @param cell
* @return null if the cell is not a line
*/
@@ -1031,7 +1031,7 @@ public CellSet followLine(Cell cell){
CellSet result = new CellSet();
if(isBoundary(cell.getNorth())) result.add(cell.getNorth());
if(isBoundary(cell.getSouth())) result.add(cell.getSouth());
- return result;
+ return result;
}
return null;
}
@@ -1113,11 +1113,11 @@ public CellSet followStub(Cell cell, Cell blocked){
if(result.contains(blocked)) result.remove(blocked);
return result;
}
-
+
public CellSet followCell(Cell cell){
return followCell(cell, null);
}
-
+
public CellSet followCell(Cell cell, Cell blocked){
if(isIntersection(cell)) return followIntersection(cell, blocked);
if(isCorner(cell)) return followCorner(cell, blocked);
@@ -1145,7 +1145,7 @@ public String getCellTypeAsString(Cell cell){
return "unrecognisable type";
}
-
+
public CellSet followCrossOnLine(Cell cell, Cell blocked){
CellSet result = new CellSet();
if(isHorizontalCrossOnLine(cell)){
@@ -1197,7 +1197,7 @@ public boolean matchesAny(Cell cell, GridPatternGroup criteria){
TextGrid subGrid = getTestingSubGrid(cell);
return subGrid.matchesAny(criteria);
}
-
+
public boolean isCorner1(Cell cell){
return matchesAny(cell, GridPatternGroup.corner1Criteria);
}
@@ -1263,7 +1263,7 @@ public void copyCellsTo(CellSet cells, TextGrid grid){
grid.set(cell, this.get(cell));
}
}
-
+
public boolean equals(TextGrid grid){
if(grid.getHeight() != this.getHeight()
|| grid.getWidth() != this.getWidth()
@@ -1278,10 +1278,10 @@ public boolean equals(TextGrid grid){
}
return true;
}
-
+
/**
* Fills all the cells in cells with c
- *
+ *
* @param cells
* @param c
*/
@@ -1294,10 +1294,10 @@ public void fillCellsWith(Iterable cells, char c){
}
/**
- *
+ *
* Fills the continuous area with if c1 characters with c2,
* flooding from cell x, y
- *
+ *
* @param x
* @param y
* @param c1 the character to replace
@@ -1323,17 +1323,17 @@ public CellSet fillContinuousArea(Cell cell, char c){
private CellSet seedFill(Cell seed, char newChar){
CellSet cellsFilled = new CellSet();
char oldChar = get(seed);
-
+
if(oldChar == newChar) return cellsFilled;
if(isOutOfBounds(seed)) return cellsFilled;
Stack stack = new Stack();
stack.push(seed);
-
+
while(!stack.isEmpty()){
Cell cell = (Cell) stack.pop();
-
+
//set(cell, newChar);
cellsFilled.add(cell);
@@ -1341,30 +1341,30 @@ private CellSet seedFill(Cell seed, char newChar){
Cell sCell = cell.getSouth();
Cell eCell = cell.getEast();
Cell wCell = cell.getWest();
-
+
if(get(nCell) == oldChar && !cellsFilled.contains(nCell)) stack.push(nCell);
if(get(sCell) == oldChar && !cellsFilled.contains(sCell)) stack.push(sCell);
if(get(eCell) == oldChar && !cellsFilled.contains(eCell)) stack.push(eCell);
if(get(wCell) == oldChar && !cellsFilled.contains(wCell)) stack.push(wCell);
}
-
+
return cellsFilled;
}
private CellSet seedFillOld(Cell seed, char newChar){
CellSet cellsFilled = new CellSet();
char oldChar = get(seed);
-
+
if(oldChar == newChar) return cellsFilled;
if(isOutOfBounds(seed)) return cellsFilled;
Stack stack = new Stack();
stack.push(seed);
-
+
while(!stack.isEmpty()){
Cell cell = (Cell) stack.pop();
-
+
set(cell, newChar);
cellsFilled.add(cell);
@@ -1372,22 +1372,22 @@ private CellSet seedFillOld(Cell seed, char newChar){
Cell sCell = cell.getSouth();
Cell eCell = cell.getEast();
Cell wCell = cell.getWest();
-
+
if(get(nCell) == oldChar) stack.push(nCell);
if(get(sCell) == oldChar) stack.push(sCell);
if(get(eCell) == oldChar) stack.push(eCell);
if(get(wCell) == oldChar) stack.push(wCell);
}
-
+
return cellsFilled;
}
/**
- *
+ *
* Locates and returns the '*' boundaries that we would
- * encounter if we did a flood-fill at seed.
- *
+ * encounter if we did a flood-fill at seed.
+ *
* @param seed
* @return
*/
@@ -1402,48 +1402,48 @@ public CellSet findBoundariesExpandingFrom(Cell seed){
Stack stack = new Stack| ();
stack.push(seed);
-
+
while(!stack.isEmpty()){
Cell cell = (Cell) stack.pop();
-
+
set(cell, newChar);
Cell nCell = cell.getNorth();
Cell sCell = cell.getSouth();
Cell eCell = cell.getEast();
Cell wCell = cell.getWest();
-
+
if(get(nCell) == oldChar) stack.push(nCell);
else if(get(nCell) == '*') boundaries.add(nCell);
-
+
if(get(sCell) == oldChar) stack.push(sCell);
else if(get(sCell) == '*') boundaries.add(sCell);
-
+
if(get(eCell) == oldChar) stack.push(eCell);
else if(get(eCell) == '*') boundaries.add(eCell);
-
+
if(get(wCell) == oldChar) stack.push(wCell);
else if(get(wCell) == '*') boundaries.add(wCell);
}
-
+
return boundaries;
}
-
-
+
+
//TODO: incomplete method seedFillLine()
private CellSet seedFillLine(Cell cell, char newChar){
CellSet cellsFilled = new CellSet();
-
+
Stack stack = new Stack();
-
+
char oldChar = get(cell);
-
+
if(oldChar == newChar) return cellsFilled;
if(isOutOfBounds(cell)) return cellsFilled;
-
+
stack.push(new LineSegment(cell.x, cell.x, cell.y, 1));
stack.push(new LineSegment(cell.x, cell.x, cell.y + 1, -1));
-
+
int left;
while(!stack.isEmpty()){
LineSegment segment = (LineSegment) stack.pop();
@@ -1456,9 +1456,9 @@ private CellSet seedFillLine(Cell cell, char newChar){
set(x, segment.y, newChar);
cellsFilled.add(new Cell(x, segment.y));
}
-
+
left = cell.getEast().x;
- boolean skip = (x > segment.x1)? true : false;
+ boolean skip = (x > segment.x1)? true : false;
if(left < segment.x1){ //leak on left?
//TODO: i think the first param should be x
@@ -1474,13 +1474,13 @@ private CellSet seedFillLine(Cell cell, char newChar){
set(x, segment.y, newChar);
cellsFilled.add(new Cell(x, segment.y));
}
-
+
stack.push(new LineSegment(left, x - 1, segment.y, segment.dy));
if(x > segment.x2 + 1) //leak on right?
stack.push(new LineSegment(segment.x2 + 1, x - 1, segment.y, -segment.dy));
}
skip = false; //skip only once
-
+
for(++x; x <= segment.x2 && get(x, segment.y) != oldChar; ++x){;}
left = x;
} while( x < segment.x2);
@@ -1488,7 +1488,7 @@ private CellSet seedFillLine(Cell cell, char newChar){
return cellsFilled;
}
-
+
public boolean cellContainsDashedLineChar(Cell cell){
char c = get(cell);
return StringUtils.isOneOf(c, dashedLines);
@@ -1503,7 +1503,7 @@ public boolean loadFrom(String filename)
public boolean loadFrom(String filename, ProcessingOptions options)
throws IOException
{
-
+
String encoding = (options == null) ? null : options.getCharacterEncoding();
ArrayList lines = new ArrayList();
InputStream is;
@@ -1514,7 +1514,7 @@ public boolean loadFrom(String filename, ProcessingOptions options)
String[] linesArray = FileUtils.readFile(is, filename, encoding).split("(\r)?\n");
for(int i = 0; i < linesArray.length; i++)
lines.add(new StringBuilder(linesArray[i]));
-
+
return initialiseWithLines(lines, options);
}
@@ -1546,15 +1546,21 @@ public boolean initialiseWithLines(ArrayList lines, ProcessingOpt
// make all lines of equal length
// add blank outline around the buffer to prevent fill glitch
// convert tabs to spaces (or remove them if setting is 0)
-
- int blankBorderSize = 2;
-
+
+
int maxLength = 0;
int index = 0;
-
+
+ // Changed from original hardcoded value of 2. Do not make 0 as that
+ // breaks TextGrid intersection code (and I'm too lazy to fix that)
+ // - APN
+ int blankBorderSize;
+ if(options != null) blankBorderSize = options.getBorderWidth();
+ else blankBorderSize = options.DEFAULT_BORDER_WIDTH;
+
String encoding = null;
if(options != null) encoding = options.getCharacterEncoding();
-
+
Iterator it = rows.iterator();
while(it.hasNext()){
String row = it.next().toString();
@@ -1569,41 +1575,41 @@ public boolean initialiseWithLines(ArrayList lines, ProcessingOpt
it = rows.iterator();
ArrayList newRows = new ArrayList();
- //TODO: make the following depend on blankBorderSize
-
+
StringBuilder topBottomRow =
new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2));
-
- newRows.add(topBottomRow);
- newRows.add(topBottomRow);
+ String borderString = StringUtils.repeatString(" ", blankBorderSize);
+
+ for (i = 0; i < blankBorderSize; i++) {
+ newRows.add(topBottomRow);
+ }
while(it.hasNext()){
StringBuilder row = it.next();
-
+
if(row.length() < maxLength) {
- String borderString = StringUtils.repeatString(" ", blankBorderSize);
StringBuilder newRow = new StringBuilder();
-
+
newRow.append(borderString);
newRow.append(row);
newRow.append(StringUtils.repeatString(" ", maxLength - row.length()));
newRow.append(borderString);
-
+
newRows.add(newRow);
- } else { //TODO: why is the following line like that?
- newRows.add(new StringBuilder(" ").append(row).append(" "));
+ } else {
+ newRows.add(new StringBuilder().append(borderString).append(row).append(borderString));
}
}
- //TODO: make the following depend on blankBorderSize
- newRows.add(topBottomRow);
- newRows.add(topBottomRow);
+ for (i = 0; i < blankBorderSize; i++) {
+ newRows.add(topBottomRow);
+ }
rows = newRows;
-
+
replaceBullets();
replaceHumanColorCodes();
-
+
return true;
}
-
+
private void fixTabs(int tabSize){
int rowIndex = 0;
@@ -1612,7 +1618,7 @@ private void fixTabs(int tabSize){
while(it.hasNext()){
String row = it.next().toString();
StringBuilder newRow = new StringBuilder();
-
+
char[] chars = row.toCharArray();
for(int i = 0; i < chars.length; i++){
if(chars[i] == '\t'){
@@ -1631,14 +1637,14 @@ private void fixTabs(int tabSize){
rowIndex++;
}
}
-
+
/**
* @return
*/
protected ArrayList getRows() {
return rows;
}
-
+
public class CellColorPair{
public CellColorPair(Cell cell, Color color){
this.cell = cell;
@@ -1666,20 +1672,20 @@ public CellTagPair(Cell cell, String tag){
public String tag;
}
-
+
public class Cell{
public int x, y;
-
+
public Cell(Cell cell){
this(cell.x, cell.y);
}
-
+
public Cell(int x, int y){
this.x = x;
this.y = y;
}
-
+
public Cell getNorth(){ return new Cell(x, y - 1); }
public Cell getSouth(){ return new Cell(x, y + 1); }
public Cell getEast(){ return new Cell(x + 1, y); }
@@ -1745,32 +1751,32 @@ public boolean equals(Object o){
if(x == cell.x && y == cell.y) return true;
else return false;
}
-
+
public int hashCode() {
return (x << 16) | y;
}
-
+
public boolean isNextTo(int x2, int y2){
if(Math.abs(x2 - x) == 1 && Math.abs(y2 - y) == 1) return false;
if(Math.abs(x2 - x) == 1 && y2 == y) return true;
if(Math.abs(y2 - y) == 1 && x2 == x) return true;
return false;
}
-
+
public boolean isNextTo(Cell cell){
if(cell == null) throw new IllegalArgumentException("cell cannot be null");
return this.isNextTo(cell.x, cell.y);
}
-
+
public String toString(){
return "("+x+", "+y+")";
}
-
+
public void scale(int s){
x = x * s;
y = y * s;
}
-
+
}
private class LineSegment{
| | | | | |