| 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.text; |
| 22 |
|
| 23 |
import java.awt.Color; |
| 24 |
import java.io.File; |
| 25 |
import java.io.FileNotFoundException; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.UnsupportedEncodingException; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Collection; |
| 30 |
import java.util.HashMap; |
| 31 |
import java.util.HashSet; |
| 32 |
import java.util.Iterator; |
| 33 |
import java.util.Stack; |
| 34 |
import java.util.regex.Matcher; |
| 35 |
import java.util.regex.Pattern; |
| 36 |
|
| 37 |
import org.stathissideris.ascii2image.core.FileUtils; |
| 38 |
import org.stathissideris.ascii2image.core.ProcessingOptions; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* |
| 43 |
* @author Efstathios Sideris |
| 44 |
*/ |
| 45 |
public class TextGrid { |
| 46 |
|
| 47 |
private static final boolean DEBUG = false; |
| 48 |
|
| 49 |
private ArrayList<StringBuffer> rows; |
| 50 |
|
| 51 |
private static char[] boundaries = {'/', '\\', '|', '-', '*', '=', ':'}; |
| 52 |
private static char[] undisputableBoundaries = {'|', '-', '*', '=', ':'}; |
| 53 |
private static char[] horizontalLines = {'-', '='}; |
| 54 |
private static char[] verticalLines = {'|', ':'}; |
| 55 |
private static char[] arrowHeads = {'<', '>', '^', 'v', 'V'}; |
| 56 |
private static char[] cornerChars = {'\\', '/', '+'}; |
| 57 |
private static char[] pointMarkers = {'*'}; |
| 58 |
private static char[] dashedLines = {':', '~', '='}; |
| 59 |
|
| 60 |
private static char[] entryPoints1 = {'\\'}; |
| 61 |
private static char[] entryPoints2 = {'|', ':', '+', '\\', '/'}; |
| 62 |
private static char[] entryPoints3 = {'/'}; |
| 63 |
private static char[] entryPoints4 = {'-', '=', '+', '\\', '/'}; |
| 64 |
private static char[] entryPoints5 = {'\\'}; |
| 65 |
private static char[] entryPoints6 = {'|', ':', '+', '\\', '/'}; |
| 66 |
private static char[] entryPoints7 = {'/'}; |
| 67 |
private static char[] entryPoints8 = {'-', '=', '+', '\\', '/'}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private static HashMap<String, String> humanColorCodes = new HashMap<String, String>(); |
| 72 |
static { |
| 73 |
humanColorCodes.put("GRE", "9D9"); |
| 74 |
humanColorCodes.put("BLU", "55B"); |
| 75 |
humanColorCodes.put("PNK", "FAA"); |
| 76 |
humanColorCodes.put("RED", "E32"); |
| 77 |
humanColorCodes.put("YEL", "FF3"); |
| 78 |
humanColorCodes.put("BLK", "000"); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private static HashSet<String> markupTags = |
| 83 |
new HashSet<String>(); |
| 84 |
|
| 85 |
static { |
| 86 |
markupTags.add("d"); |
| 87 |
markupTags.add("s"); |
| 88 |
markupTags.add("io"); |
| 89 |
markupTags.add("c"); |
| 90 |
markupTags.add("mo"); |
| 91 |
markupTags.add("tr"); |
| 92 |
markupTags.add("o"); |
| 93 |
} |
| 94 |
|
| 95 |
public void addToMarkupTags(Collection<String> tags){ |
| 96 |
markupTags.addAll(tags); |
| 97 |
} |
| 98 |
|
| 99 |
public static void main(String[] args) throws Exception { |
| 100 |
TextGrid grid = new TextGrid(); |
| 101 |
grid.loadFrom("tests/text/art10.txt"); |
| 102 |
|
| 103 |
grid.writeStringTo(grid.new Cell(28, 1), "testing"); |
| 104 |
|
| 105 |
grid.findMarkupTags(); |
| 106 |
|
| 107 |
grid.printDebug(); |
| 108 |
//System.out.println(grid.fillContinuousArea(0, 0, '-').size()+" cells filled"); |
| 109 |
//grid.fillContinuousArea(4, 4, '-'); |
| 110 |
//grid.getSubGrid(1,1,3,3).printDebug(); |
| 111 |
//grid.printDebug(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public TextGrid(){ |
| 116 |
rows = new ArrayList<StringBuffer>(); |
| 117 |
} |
| 118 |
|
| 119 |
public TextGrid(int width, int height){ |
| 120 |
String space = StringUtils.repeatString(" ", width); |
| 121 |
rows = new ArrayList<StringBuffer>(); |
| 122 |
for(int i = 0; i < height; i++) |
| 123 |
rows.add(new StringBuffer(space)); |
| 124 |
} |
| 125 |
|
| 126 |
public static TextGrid makeSameSizeAs(TextGrid grid){ |
| 127 |
return new TextGrid(grid.getWidth(), grid.getHeight()); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public TextGrid(TextGrid otherGrid){ |
| 132 |
rows = new ArrayList<StringBuffer>(); |
| 133 |
for(StringBuffer row : otherGrid.getRows()) { |
| 134 |
rows.add(new StringBuffer(row)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void clear(){ |
| 139 |
String blank = StringUtils.repeatString(" ", getWidth()); |
| 140 |
int height = getHeight(); |
| 141 |
rows.clear(); |
| 142 |
for(int i = 0; i < height; i++) |
| 143 |
rows.add(new StringBuffer(blank)); |
| 144 |
} |
| 145 |
|
| 146 |
// duplicated code due to lots of hits to this function |
| 147 |
public char get(int x, int y){ |
| 148 |
if(x > getWidth() - 1 |
| 149 |
|| y > getHeight() - 1 |
| 150 |
|| x < 0 |
| 151 |
|| y < 0) return 0; |
| 152 |
return rows.get(y).charAt(x); |
| 153 |
} |
| 154 |
|
| 155 |
//duplicated code due to lots of hits to this function |
| 156 |
public char get(Cell cell){ |
| 157 |
if(cell.x > getWidth() - 1 |
| 158 |
|| cell.y > getHeight() - 1 |
| 159 |
|| cell.x < 0 |
| 160 |
|| cell.y < 0) return 0; |
| 161 |
return rows.get(cell.y).charAt(cell.x); |
| 162 |
} |
| 163 |
|
| 164 |
public StringBuffer getRow(int y){ |
| 165 |
return rows.get(y); |
| 166 |
} |
| 167 |
|
| 168 |
public TextGrid getSubGrid(int x, int y, int width, int height){ |
| 169 |
TextGrid grid = new TextGrid(width, height); |
| 170 |
for(int i = 0; i < height; i++){ |
| 171 |
grid.setRow(i, new StringBuffer(getRow(y + i).subSequence(x, x + width))); |
| 172 |
} |
| 173 |
return grid; |
| 174 |
} |
| 175 |
|
| 176 |
public TextGrid getTestingSubGrid(Cell cell){ |
| 177 |
return getSubGrid(cell.x - 1, cell.y - 1, 3, 3); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public String getStringAt(int x, int y, int length){ |
| 182 |
return getStringAt(new Cell(x, y), length); |
| 183 |
} |
| 184 |
|
| 185 |
public String getStringAt(Cell cell, int length){ |
| 186 |
int x = cell.x; |
| 187 |
int y = cell.y; |
| 188 |
if(x > getWidth() - 1 |
| 189 |
|| y > getHeight() - 1 |
| 190 |
|| x < 0 |
| 191 |
|| y < 0) return null; |
| 192 |
return rows.get(y).substring(x, x + length); |
| 193 |
} |
| 194 |
|
| 195 |
public char getNorthOf(int x, int y){ return get(x, y - 1); } |
| 196 |
public char getSouthOf(int x, int y){ return get(x, y + 1); } |
| 197 |
public char getEastOf(int x, int y){ return get(x + 1, y); } |
| 198 |
public char getWestOf(int x, int y){ return get(x - 1, y); } |
| 199 |
|
| 200 |
public char getNorthOf(Cell cell){ return getNorthOf(cell.x, cell.y); } |
| 201 |
public char getSouthOf(Cell cell){ return getSouthOf(cell.x, cell.y); } |
| 202 |
public char getEastOf(Cell cell){ return getEastOf(cell.x, cell.y); } |
| 203 |
public char getWestOf(Cell cell){ return getWestOf(cell.x, cell.y); } |
| 204 |
|
| 205 |
public void writeStringTo(int x, int y, String str){ |
| 206 |
writeStringTo(new Cell(x, y), str); |
| 207 |
} |
| 208 |
|
| 209 |
public void writeStringTo(Cell cell, String str){ |
| 210 |
if(isOutOfBounds(cell)) return; |
| 211 |
rows.get(cell.y).replace(cell.x, cell.x + str.length(), str); |
| 212 |
} |
| 213 |
|
| 214 |
public void set(Cell cell, char c){ |
| 215 |
set(cell.x, cell.y, c); |
| 216 |
} |
| 217 |
|
| 218 |
public void set(int x, int y, char c){ |
| 219 |
if(x > getWidth() - 1 || y > getHeight() - 1) return; |
| 220 |
StringBuffer row = rows.get(y); |
| 221 |
row.setCharAt(x, c); |
| 222 |
} |
| 223 |
|
| 224 |
public void setRow(int y, String row){ |
| 225 |
if(y > getHeight() || row.length() != getWidth()) |
| 226 |
throw new IllegalArgumentException("setRow out of bounds or string wrong size"); |
| 227 |
rows.set(y, new StringBuffer(row)); |
| 228 |
} |
| 229 |
|
| 230 |
public void setRow(int y, StringBuffer row){ |
| 231 |
if(y > getHeight() || row.length() != getWidth()) |
| 232 |
throw new IllegalArgumentException("setRow out of bounds or string wrong size"); |
| 233 |
rows.set(y, row); |
| 234 |
} |
| 235 |
|
| 236 |
public int getWidth(){ |
| 237 |
if(rows.size() == 0) return 0; //empty buffer |
| 238 |
return rows.get(0).length(); |
| 239 |
} |
| 240 |
|
| 241 |
public int getHeight(){ |
| 242 |
return rows.size(); |
| 243 |
} |
| 244 |
|
| 245 |
public void printDebug(){ |
| 246 |
Iterator<StringBuffer> it = rows.iterator(); |
| 247 |
int i = 0; |
| 248 |
System.out.println( |
| 249 |
" " |
| 250 |
+StringUtils.repeatString("0123456789", (int) Math.floor(getWidth()/10)+1)); |
| 251 |
while(it.hasNext()){ |
| 252 |
String row = it.next().toString(); |
| 253 |
String index = new Integer(i).toString(); |
| 254 |
if(i < 10) index = " "+index; |
| 255 |
System.out.println(index+" ("+row+")"); |
| 256 |
i++; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public String getDebugString(){ |
| 261 |
StringBuffer buffer = new StringBuffer(); |
| 262 |
Iterator<StringBuffer> it = rows.iterator(); |
| 263 |
int i = 0; |
| 264 |
buffer.append( |
| 265 |
" " |
| 266 |
+StringUtils.repeatString("0123456789", (int) Math.floor(getWidth()/10)+1)+"\n"); |
| 267 |
while(it.hasNext()){ |
| 268 |
String row = it.next().toString(); |
| 269 |
String index = new Integer(i).toString(); |
| 270 |
if(i < 10) index = " "+index; |
| 271 |
row = row.replaceAll("\n", "\\\\n"); |
| 272 |
row = row.replaceAll("\r", "\\\\r"); |
| 273 |
buffer.append(index+" ("+row+")\n"); |
| 274 |
i++; |
| 275 |
} |
| 276 |
return buffer.toString(); |
| 277 |
} |
| 278 |
|
| 279 |
public String toString(){ |
| 280 |
return getDebugString(); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Adds grid to this. Space characters in this grid |
| 285 |
* are replaced with the corresponding contents of |
| 286 |
* grid, otherwise the contents are unchanged. |
| 287 |
* |
| 288 |
* @param grid |
| 289 |
* @return false if the grids are of different size |
| 290 |
*/ |
| 291 |
public boolean add(TextGrid grid){ |
| 292 |
if(getWidth() != grid.getWidth() |
| 293 |
|| getHeight() != grid.getHeight()) return false; |
| 294 |
int width = getWidth(); |
| 295 |
int height = getHeight(); |
| 296 |
for(int yi = 0; yi < height; yi++){ |
| 297 |
for(int xi = 0; xi < width; xi++){ |
| 298 |
if(get(xi, yi) == ' ') set(xi, yi, grid.get(xi, yi)); |
| 299 |
} |
| 300 |
} |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Replaces letters or numbers that are on horizontal or vertical |
| 306 |
* lines, with the appropriate character that will make the line |
| 307 |
* continuous (| for vertical and - for horizontal lines) |
| 308 |
* |
| 309 |
*/ |
| 310 |
public void replaceTypeOnLine(){ |
| 311 |
int width = getWidth(); |
| 312 |
int height = getHeight(); |
| 313 |
for(int yi = 0; yi < height; yi++){ |
| 314 |
for(int xi = 0; xi < width; xi++){ |
| 315 |
char c = get(xi, yi); |
| 316 |
if(Character.isLetterOrDigit(c)) { |
| 317 |
boolean isOnHorizontalLine = isOnHorizontalLine(xi, yi); |
| 318 |
boolean isOnVerticalLine = isOnVerticalLine(xi, yi); |
| 319 |
if(isOnHorizontalLine && isOnVerticalLine){ |
| 320 |
set(xi, yi, '+'); |
| 321 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with +"); |
| 322 |
} else if(isOnHorizontalLine){ |
| 323 |
set(xi, yi, '-'); |
| 324 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with -"); |
| 325 |
} else if(isOnVerticalLine){ |
| 326 |
set(xi, yi, '|'); |
| 327 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with |"); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
public void replacePointMarkersOnLine(){ |
| 335 |
int width = getWidth(); |
| 336 |
int height = getHeight(); |
| 337 |
for(int yi = 0; yi < height; yi++){ |
| 338 |
for(int xi = 0; xi < width; xi++){ |
| 339 |
char c = get(xi, yi); |
| 340 |
Cell cell = new Cell(xi, yi); |
| 341 |
if(StringUtils.isOneOf(c, pointMarkers) |
| 342 |
&& isStarOnLine(cell)){ |
| 343 |
|
| 344 |
boolean isOnHorizontalLine = false; |
| 345 |
if(StringUtils.isOneOf(get(cell.getEast()), horizontalLines)) |
| 346 |
isOnHorizontalLine = true; |
| 347 |
if(StringUtils.isOneOf(get(cell.getWest()), horizontalLines)) |
| 348 |
isOnHorizontalLine = true; |
| 349 |
|
| 350 |
boolean isOnVerticalLine = false; |
| 351 |
if(StringUtils.isOneOf(get(cell.getNorth()), verticalLines)) |
| 352 |
isOnVerticalLine = true; |
| 353 |
if(StringUtils.isOneOf(get(cell.getSouth()), verticalLines)) |
| 354 |
isOnVerticalLine = true; |
| 355 |
|
| 356 |
if(isOnHorizontalLine && isOnVerticalLine){ |
| 357 |
set(xi, yi, '+'); |
| 358 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with +"); |
| 359 |
} else if(isOnHorizontalLine){ |
| 360 |
set(xi, yi, '-'); |
| 361 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with -"); |
| 362 |
} else if(isOnVerticalLine){ |
| 363 |
set(xi, yi, '|'); |
| 364 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with |"); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
public CellSet getPointMarkersOnLine(){ |
| 372 |
CellSet result = new CellSet(); |
| 373 |
int width = getWidth(); |
| 374 |
int height = getHeight(); |
| 375 |
for(int yi = 0; yi < height; yi++){ |
| 376 |
for(int xi = 0; xi < width; xi++){ |
| 377 |
char c = get(xi, yi); |
| 378 |
if(StringUtils.isOneOf(c, pointMarkers) |
| 379 |
&& isStarOnLine(new Cell(xi, yi))){ |
| 380 |
result.add(new Cell(xi, yi)); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
return result; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
public void replaceHumanColorCodes(){ |
| 389 |
int height = getHeight(); |
| 390 |
for(int y = 0; y < height; y++){ |
| 391 |
String row = rows.get(y).toString(); |
| 392 |
Iterator it = humanColorCodes.keySet().iterator(); |
| 393 |
while(it.hasNext()){ |
| 394 |
String humanCode = (String) it.next(); |
| 395 |
String hexCode = (String) humanColorCodes.get(humanCode); |
| 396 |
if(hexCode != null){ |
| 397 |
humanCode = "c" + humanCode; |
| 398 |
hexCode = "c" + hexCode; |
| 399 |
row = row.replaceAll(humanCode, hexCode); |
| 400 |
rows.set(y, new StringBuffer(row)); //TODO: this is not the most efficient way to do this |
| 401 |
row = rows.get(y).toString(); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
/** |
| 409 |
* Replace all occurences of c1 with c2 |
| 410 |
* |
| 411 |
* @param c1 |
| 412 |
* @param c2 |
| 413 |
*/ |
| 414 |
public void replaceAll(char c1, char c2){ |
| 415 |
int width = getWidth(); |
| 416 |
int height = getHeight(); |
| 417 |
for(int yi = 0; yi < height; yi++){ |
| 418 |
for(int xi = 0; xi < width; xi++){ |
| 419 |
char c = get(xi, yi); |
| 420 |
if(c == c1) set(xi, yi, c2); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
public boolean hasBlankCells(){ |
| 426 |
CellSet set = new CellSet(); |
| 427 |
int width = getWidth(); |
| 428 |
int height = getHeight(); |
| 429 |
for(int y = 0; y < height; y++){ |
| 430 |
for(int x = 0; x < width; x++){ |
| 431 |
Cell cell = new Cell(x, y); |
| 432 |
if(isBlank(cell)) return true; |
| 433 |
} |
| 434 |
} |
| 435 |
return false; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
public CellSet getAllNonBlank(){ |
| 440 |
CellSet set = new CellSet(); |
| 441 |
int width = getWidth(); |
| 442 |
int height = getHeight(); |
| 443 |
for(int y = 0; y < height; y++){ |
| 444 |
for(int x = 0; x < width; x++){ |
| 445 |
Cell cell = new Cell(x, y); |
| 446 |
if(!isBlank(cell)) set.add(cell); |
| 447 |
} |
| 448 |
} |
| 449 |
return set; |
| 450 |
} |
| 451 |
|
| 452 |
public CellSet getAllBoundaries(){ |
| 453 |
CellSet set = new CellSet(); |
| 454 |
int width = getWidth(); |
| 455 |
int height = getHeight(); |
| 456 |
for(int y = 0; y < height; y++){ |
| 457 |
for(int x = 0; x < width; x++){ |
| 458 |
Cell cell = new Cell(x, y); |
| 459 |
if(isBoundary(cell)) set.add(cell); |
| 460 |
} |
| 461 |
} |
| 462 |
return set; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
public CellSet getAllBlanksBetweenCharacters(){ |
| 467 |
CellSet set = new CellSet(); |
| 468 |
int width = getWidth(); |
| 469 |
int height = getHeight(); |
| 470 |
for(int y = 0; y < height; y++){ |
| 471 |
for(int x = 0; x < width; x++){ |
| 472 |
Cell cell = new Cell(x, y); |
| 473 |
if(isBlankBetweenCharacters(cell)) set.add(cell); |
| 474 |
} |
| 475 |
} |
| 476 |
return set; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
/** |
| 481 |
* Returns an ArrayList of CellStringPairs that |
| 482 |
* represents all the continuous (non-blank) Strings |
| 483 |
* in the grid. Used on buffers that contain only |
| 484 |
* type, in order to find the positions and the |
| 485 |
* contents of the strings. |
| 486 |
* |
| 487 |
* @return |
| 488 |
*/ |
| 489 |
public ArrayList<CellStringPair> findStrings(){ |
| 490 |
ArrayList<CellStringPair> result = new ArrayList<CellStringPair>(); |
| 491 |
int width = getWidth(); |
| 492 |
int height = getHeight(); |
| 493 |
for(int y = 0; y < height; y++){ |
| 494 |
for(int x = 0; x < width; x++){ |
| 495 |
if(!isBlank(x, y)){ |
| 496 |
Cell start = new Cell(x, y); |
| 497 |
String str = String.valueOf(get(x,y)); |
| 498 |
char c = get(++x, y); |
| 499 |
boolean finished = false; |
| 500 |
//while(c != ' '){ |
| 501 |
while(!finished){ |
| 502 |
str += String.valueOf(c); |
| 503 |
c = get(++x, y); |
| 504 |
char next = get(x + 1, y); |
| 505 |
if((c == ' ' || c == 0) && (next == ' ' || next == 0)) |
| 506 |
finished = true; |
| 507 |
} |
| 508 |
result.add(new CellStringPair(start, str)); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
return result; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* This is done in a bit of a messy way, should be impossible |
| 517 |
* to go out of sync with corresponding GridPatternGroup. |
| 518 |
* |
| 519 |
* @param cell |
| 520 |
* @param entryPointId |
| 521 |
* @return |
| 522 |
*/ |
| 523 |
public boolean hasEntryPoint(Cell cell, int entryPointId){ |
| 524 |
String result = ""; |
| 525 |
char c = get(cell); |
| 526 |
if(entryPointId == 1) { |
| 527 |
return StringUtils.isOneOf(c, entryPoints1); |
| 528 |
|
| 529 |
} else if(entryPointId == 2) { |
| 530 |
return StringUtils.isOneOf(c, entryPoints2); |
| 531 |
|
| 532 |
} else if(entryPointId == 3) { |
| 533 |
return StringUtils.isOneOf(c, entryPoints3); |
| 534 |
|
| 535 |
} else if(entryPointId == 4) { |
| 536 |
return StringUtils.isOneOf(c, entryPoints4); |
| 537 |
|
| 538 |
} else if(entryPointId == 5) { |
| 539 |
return StringUtils.isOneOf(c, entryPoints5); |
| 540 |
|
| 541 |
} else if(entryPointId == 6) { |
| 542 |
return StringUtils.isOneOf(c, entryPoints6); |
| 543 |
|
| 544 |
} else if(entryPointId == 7) { |
| 545 |
return StringUtils.isOneOf(c, entryPoints7); |
| 546 |
|
| 547 |
} else if(entryPointId == 8) { |
| 548 |
return StringUtils.isOneOf(c, entryPoints8); |
| 549 |
} |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* true if cell is blank and the east and west cells are not |
| 555 |
* (used to find gaps between words) |
| 556 |
* |
| 557 |
* @param cell |
| 558 |
* @return |
| 559 |
*/ |
| 560 |
public boolean isBlankBetweenCharacters(Cell cell){ |
| 561 |
return (isBlank(cell) |
| 562 |
&& !isBlank(cell.getEast()) |
| 563 |
&& !isBlank(cell.getWest())); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Makes blank all the cells that contain non-text |
| 568 |
* elements. |
| 569 |
*/ |
| 570 |
public void removeNonText(){ |
| 571 |
//the following order is significant |
| 572 |
//since the south-pointing arrowheads |
| 573 |
//are determined based on the surrounding boundaries |
| 574 |
removeArrowheads(); |
| 575 |
removeColorCodes(); |
| 576 |
removeBoundaries(); |
| 577 |
removeMarkupTags(); |
| 578 |
} |
| 579 |
|
| 580 |
public void removeArrowheads(){ |
| 581 |
int width = getWidth(); |
| 582 |
int height = getHeight(); |
| 583 |
for(int yi = 0; yi < height; yi++){ |
| 584 |
for(int xi = 0; xi < width; xi++){ |
| 585 |
Cell cell = new Cell(xi, yi); |
| 586 |
if(isArrowhead(cell)) set(cell, ' '); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
public void removeColorCodes(){ |
| 592 |
Iterator cells = findColorCodes().iterator(); |
| 593 |
while(cells.hasNext()){ |
| 594 |
Cell cell = ((CellColorPair) cells.next()).cell; |
| 595 |
set(cell, ' '); |
| 596 |
cell = cell.getEast(); set(cell, ' '); |
| 597 |
cell = cell.getEast(); set(cell, ' '); |
| 598 |
cell = cell.getEast(); set(cell, ' '); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
public void removeBoundaries(){ |
| 603 |
ArrayList toBeRemoved = new ArrayList(); |
| 604 |
|
| 605 |
int width = getWidth(); |
| 606 |
int height = getHeight(); |
| 607 |
for(int yi = 0; yi < height; yi++){ |
| 608 |
for(int xi = 0; xi < width; xi++){ |
| 609 |
Cell cell = new Cell(xi, yi); |
| 610 |
if(isBoundary(cell)) toBeRemoved.add(cell); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
//remove in two stages, because decision of |
| 615 |
//isBoundary depends on contants of surrounding |
| 616 |
//cells |
| 617 |
Iterator it = toBeRemoved.iterator(); |
| 618 |
while(it.hasNext()){ |
| 619 |
Cell cell = (Cell) it.next(); |
| 620 |
set(cell, ' '); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
public ArrayList findArrowheads(){ |
| 625 |
ArrayList result = new ArrayList(); |
| 626 |
int width = getWidth(); |
| 627 |
int height = getHeight(); |
| 628 |
for(int yi = 0; yi < height; yi++){ |
| 629 |
for(int xi = 0; xi < width; xi++){ |
| 630 |
Cell cell = new Cell(xi, yi); |
| 631 |
if(isArrowhead(cell)) result.add(cell); |
| 632 |
} |
| 633 |
} |
| 634 |
if(DEBUG) System.out.println(result.size()+" arrowheads found"); |
| 635 |
return result; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
public ArrayList<CellColorPair> findColorCodes(){ |
| 640 |
Pattern colorCodePattern = Pattern.compile("c[A-F0-9]{3}"); |
| 641 |
ArrayList<CellColorPair> result = new ArrayList<CellColorPair>(); |
| 642 |
int width = getWidth(); |
| 643 |
int height = getHeight(); |
| 644 |
for(int yi = 0; yi < height; yi++){ |
| 645 |
for(int xi = 0; xi < width - 3; xi++){ |
| 646 |
Cell cell = new Cell(xi, yi); |
| 647 |
String s = getStringAt(cell, 4); |
| 648 |
Matcher matcher = colorCodePattern.matcher(s); |
| 649 |
if(matcher.matches()){ |
| 650 |
char cR = s.charAt(1); |
| 651 |
char cG = s.charAt(2); |
| 652 |
char cB = s.charAt(3); |
| 653 |
int r = Integer.valueOf(String.valueOf(cR), 16).intValue() * 17; |
| 654 |
int g = Integer.valueOf(String.valueOf(cG), 16).intValue() * 17; |
| 655 |
int b = Integer.valueOf(String.valueOf(cB), 16).intValue() * 17; |
| 656 |
result.add(new CellColorPair(cell, new Color(r, g, b))); |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
if(DEBUG) System.out.println(result.size()+" color codes found"); |
| 661 |
return result; |
| 662 |
} |
| 663 |
|
| 664 |
public ArrayList<CellTagPair> findMarkupTags(){ |
| 665 |
Pattern tagPattern = Pattern.compile("\\{(.+?)\\}"); |
| 666 |
ArrayList<CellTagPair> result = new ArrayList<CellTagPair>(); |
| 667 |
|
| 668 |
int width = getWidth(); |
| 669 |
int height = getHeight(); |
| 670 |
for(int y = 0; y < height; y++){ |
| 671 |
for(int x = 0; x < width - 3; x++){ |
| 672 |
Cell cell = new Cell(x, y); |
| 673 |
char c = get(cell); |
| 674 |
if(c == '{'){ |
| 675 |
String rowPart = rows.get(y).substring(x); |
| 676 |
Matcher matcher = tagPattern.matcher(rowPart); |
| 677 |
if(matcher.find()){ |
| 678 |
String tagName = matcher.group(1); |
| 679 |
if(markupTags.contains(tagName)){ |
| 680 |
if(DEBUG) System.out.println("found tag "+tagName+" at "+x+", "+y); |
| 681 |
result.add(new CellTagPair(new Cell(x, y), tagName)); |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
return result; |
| 688 |
} |
| 689 |
|
| 690 |
public void removeMarkupTags(){ |
| 691 |
Iterator it = findMarkupTags().iterator(); |
| 692 |
while (it.hasNext()) { |
| 693 |
CellTagPair pair = (CellTagPair) it.next(); |
| 694 |
String tagName = pair.tag; |
| 695 |
if(tagName == null) continue; |
| 696 |
int length = 2 + tagName.length(); |
| 697 |
writeStringTo(pair.cell, StringUtils.repeatString(" ", length)); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
public boolean matchesAny(GridPatternGroup criteria){ |
| 704 |
return criteria.isAnyMatchedBy(this); |
| 705 |
} |
| 706 |
|
| 707 |
public boolean matchesAll(GridPatternGroup criteria){ |
| 708 |
return criteria.areAllMatchedBy(this); |
| 709 |
} |
| 710 |
|
| 711 |
public boolean matches(GridPattern criteria){ |
| 712 |
return criteria.isMatchedBy(this); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
public boolean isOnHorizontalLine(Cell cell){ return isOnHorizontalLine(cell.x, cell.y); } |
| 717 |
private boolean isOnHorizontalLine(int x, int y){ |
| 718 |
char c1 = get(x - 1, y); |
| 719 |
char c2 = get(x + 1, y); |
| 720 |
if(isHorizontalLine(c1) && isHorizontalLine(c2)) return true; |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
public boolean isOnVerticalLine(Cell cell){ return isOnVerticalLine(cell.x, cell.y); } |
| 725 |
private boolean isOnVerticalLine(int x, int y){ |
| 726 |
char c1 = get(x, y - 1); |
| 727 |
char c2 = get(x, y + 1); |
| 728 |
if(isVerticalLine(c1) && isVerticalLine(c2)) return true; |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
public static boolean isBoundary(char c){ |
| 734 |
return StringUtils.isOneOf(c, boundaries); |
| 735 |
} |
| 736 |
public boolean isBoundary(int x, int y){ return isBoundary(new Cell(x, y)); } |
| 737 |
public boolean isBoundary(Cell cell){ |
| 738 |
char c = get(cell.x, cell.y); |
| 739 |
if(0 == c) return false; |
| 740 |
if('+' == c || '\\' == c || '/' == c){ |
| 741 |
// System.out.print(""); |
| 742 |
if( |
| 743 |
isIntersection(cell) |
| 744 |
|| isCorner(cell) |
| 745 |
|| isStub(cell) |
| 746 |
|| isCrossOnLine(cell)){ |
| 747 |
return true; |
| 748 |
} else return false; |
| 749 |
} |
| 750 |
//return StringUtils.isOneOf(c, undisputableBoundaries); |
| 751 |
if(StringUtils.isOneOf(c, boundaries) && !isLoneDiagonal(cell)){ |
| 752 |
return true; |
| 753 |
} |
| 754 |
return false; |
| 755 |
} |
| 756 |
|
| 757 |
public boolean isLine(Cell cell){ |
| 758 |
return isHorizontalLine(cell) || isVerticalLine(cell); |
| 759 |
} |
| 760 |
|
| 761 |
public static boolean isHorizontalLine(char c){ |
| 762 |
return StringUtils.isOneOf(c, horizontalLines); |
| 763 |
} |
| 764 |
public boolean isHorizontalLine(Cell cell){ return isHorizontalLine(cell.x, cell.y); } |
| 765 |
public boolean isHorizontalLine(int x, int y){ |
| 766 |
char c = get(x, y); |
| 767 |
if(0 == c) return false; |
| 768 |
return StringUtils.isOneOf(c, horizontalLines); |
| 769 |
} |
| 770 |
|
| 771 |
public static boolean isVerticalLine(char c){ |
| 772 |
return StringUtils.isOneOf(c, verticalLines); |
| 773 |
} |
| 774 |
public boolean isVerticalLine(Cell cell){ return isVerticalLine(cell.x, cell.y); } |
| 775 |
public boolean isVerticalLine(int x, int y){ |
| 776 |
char c = get(x, y); |
| 777 |
if(0 == c) return false; |
| 778 |
return StringUtils.isOneOf(c, verticalLines); |
| 779 |
} |
| 780 |
|
| 781 |
public boolean isLinesEnd(int x, int y){ |
| 782 |
return isLinesEnd(new Cell(x, y)); |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Stubs are also considered end of lines |
| 787 |
* |
| 788 |
* @param cell |
| 789 |
* @return |
| 790 |
*/ |
| 791 |
public boolean isLinesEnd(Cell cell){ |
| 792 |
return matchesAny(cell, GridPatternGroup.linesEndCriteria); |
| 793 |
} |
| 794 |
|
| 795 |
public boolean isVerticalLinesEnd(Cell cell){ |
| 796 |
return matchesAny(cell, GridPatternGroup.verticalLinesEndCriteria); |
| 797 |
} |
| 798 |
|
| 799 |
public boolean isHorizontalLinesEnd(Cell cell){ |
| 800 |
return matchesAny(cell, GridPatternGroup.horizontalLinesEndCriteria); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
public boolean isPointCell(Cell cell){ |
| 805 |
return ( |
| 806 |
isCorner(cell) |
| 807 |
|| isIntersection(cell) |
| 808 |
|| isStub(cell) |
| 809 |
|| isLinesEnd(cell)); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
public boolean containsAtLeastOneDashedLine(CellSet set){ |
| 814 |
Iterator it = set.iterator(); |
| 815 |
while(it.hasNext()) { |
| 816 |
Cell cell = (Cell) it.next(); |
| 817 |
if(StringUtils.isOneOf(get(cell), dashedLines)) return true; |
| 818 |
} |
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
public boolean exactlyOneNeighbourIsBoundary(Cell cell) { |
| 823 |
int howMany = 0; |
| 824 |
if(isBoundary(cell.getNorth())) howMany++; |
| 825 |
if(isBoundary(cell.getSouth())) howMany++; |
| 826 |
if(isBoundary(cell.getEast())) howMany++; |
| 827 |
if(isBoundary(cell.getWest())) howMany++; |
| 828 |
return (howMany == 1); |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
* |
| 833 |
* A stub looks like that: |
| 834 |
* |
| 835 |
* <pre> |
| 836 |
* |
| 837 |
* +- or -+ or + or + or /- or -/ or / (you get the point) |
| 838 |
* | | | |
| 839 |
* |
| 840 |
* </pre> |
| 841 |
* |
| 842 |
* @param cell |
| 843 |
* @return |
| 844 |
*/ |
| 845 |
|
| 846 |
public boolean isStub(Cell cell){ |
| 847 |
return matchesAny(cell, GridPatternGroup.stubCriteria); |
| 848 |
} |
| 849 |
|
| 850 |
public boolean isCrossOnLine(Cell cell){ |
| 851 |
return matchesAny(cell, GridPatternGroup.crossOnLineCriteria); |
| 852 |
} |
| 853 |
|
| 854 |
public boolean isHorizontalCrossOnLine(Cell cell){ |
| 855 |
return matchesAny(cell, GridPatternGroup.horizontalCrossOnLineCriteria); |
| 856 |
} |
| 857 |
|
| 858 |
public boolean isVerticalCrossOnLine(Cell cell){ |
| 859 |
return matchesAny(cell, GridPatternGroup.verticalCrossOnLineCriteria); |
| 860 |
} |
| 861 |
|
| 862 |
public boolean isStarOnLine(Cell cell){ |
| 863 |
return matchesAny(cell, GridPatternGroup.starOnLineCriteria); |
| 864 |
} |
| 865 |
|
| 866 |
public boolean isLoneDiagonal(Cell cell){ |
| 867 |
return matchesAny(cell, GridPatternGroup.loneDiagonalCriteria); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
public boolean isHorizontalStarOnLine(Cell cell){ |
| 872 |
return matchesAny(cell, GridPatternGroup.horizontalStarOnLineCriteria); |
| 873 |
} |
| 874 |
|
| 875 |
public boolean isVerticalStarOnLine(Cell cell){ |
| 876 |
return matchesAny(cell, GridPatternGroup.verticalStarOnLineCriteria); |
| 877 |
} |
| 878 |
|
| 879 |
public boolean isArrowhead(Cell cell){ |
| 880 |
return (isNorthArrowhead(cell) |
| 881 |
|| isSouthArrowhead(cell) |
| 882 |
|| isWestArrowhead(cell) |
| 883 |
|| isEastArrowhead(cell)); |
| 884 |
} |
| 885 |
|
| 886 |
public boolean isNorthArrowhead(Cell cell){ |
| 887 |
return get(cell) == '^'; |
| 888 |
} |
| 889 |
|
| 890 |
public boolean isEastArrowhead(Cell cell){ |
| 891 |
return get(cell) == '>'; |
| 892 |
} |
| 893 |
|
| 894 |
public boolean isWestArrowhead(Cell cell){ |
| 895 |
return get(cell) == '<'; |
| 896 |
} |
| 897 |
|
| 898 |
public boolean isSouthArrowhead(Cell cell){ |
| 899 |
return (get(cell) == 'v' || get(cell) == 'V') |
| 900 |
&& isVerticalLine(cell.getNorth()); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
// unicode for bullets |
| 905 |
// |
| 906 |
// 2022 bullet |
| 907 |
// 25CF black circle |
| 908 |
// 25AA black circle (small) |
| 909 |
// 25A0 black square |
| 910 |
// 25A1 white square |
| 911 |
// 25CB white circle |
| 912 |
// 25BA black right-pointing pointer |
| 913 |
|
| 914 |
|
| 915 |
public boolean isBullet(int x, int y){ |
| 916 |
return isBullet(new Cell(x, y)); |
| 917 |
} |
| 918 |
|
| 919 |
public boolean isBullet(Cell cell){ |
| 920 |
char c = get(cell); |
| 921 |
if((c == 'o' || c == '*') |
| 922 |
&& isBlank(cell.getEast()) |
| 923 |
&& isBlank(cell.getWest()) |
| 924 |
&& Character.isLetterOrDigit(get(cell.getEast().getEast())) ) |
| 925 |
return true; |
| 926 |
return false; |
| 927 |
} |
| 928 |
|
| 929 |
public void replaceBullets(){ |
| 930 |
int width = getWidth(); |
| 931 |
int height = getHeight(); |
| 932 |
for(int yi = 0; yi < height; yi++){ |
| 933 |
for(int xi = 0; xi < width; xi++){ |
| 934 |
Cell cell = new Cell(xi, yi); |
| 935 |
if(isBullet(cell)){ |
| 936 |
set(cell, ' '); |
| 937 |
set(cell.getEast(), '\u2022'); |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
/** |
| 944 |
* true if the cell is not blank |
| 945 |
* but the previous (west) is |
| 946 |
* |
| 947 |
* @param cell |
| 948 |
* @return |
| 949 |
*/ |
| 950 |
public boolean isStringsStart(Cell cell){ |
| 951 |
return (!isBlank(cell) && isBlank(cell.getWest())); |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* true if the cell is not blank |
| 956 |
* but the next (east) is |
| 957 |
* |
| 958 |
* @param cell |
| 959 |
* @return |
| 960 |
*/ |
| 961 |
public boolean isStringsEnd(Cell cell){ |
| 962 |
return (!isBlank(cell) |
| 963 |
//&& (isBlank(cell.getEast()) || get(cell.getEast()) == 0)); |
| 964 |
&& isBlank(cell.getEast())); |
| 965 |
} |
| 966 |
|
| 967 |
public int otherStringsStartInTheSameColumn(Cell cell){ |
| 968 |
if(!isStringsStart(cell)) return 0; |
| 969 |
int result = 0; |
| 970 |
int height = getHeight(); |
| 971 |
for(int y = 0; y < height; y++){ |
| 972 |
Cell cCell = new Cell(cell.x, y); |
| 973 |
if(!cCell.equals(cell) && isStringsStart(cCell)){ |
| 974 |
result++; |
| 975 |
} |
| 976 |
} |
| 977 |
return result; |
| 978 |
} |
| 979 |
|
| 980 |
public int otherStringsEndInTheSameColumn(Cell cell){ |
| 981 |
if(!isStringsEnd(cell)) return 0; |
| 982 |
int result = 0; |
| 983 |
int height = getHeight(); |
| 984 |
for(int y = 0; y < height; y++){ |
| 985 |
Cell cCell = new Cell(cell.x, y); |
| 986 |
if(!cCell.equals(cell) && isStringsEnd(cCell)){ |
| 987 |
result++; |
| 988 |
} |
| 989 |
} |
| 990 |
return result; |
| 991 |
} |
| 992 |
|
| 993 |
public boolean isColumnBlank(int x){ |
| 994 |
int height = getHeight(); |
| 995 |
for(int y = 0; y < height; y++){ |
| 996 |
if(!isBlank(x, y)) return false; |
| 997 |
} |
| 998 |
return true; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
public CellSet followLine(int x, int y){ |
| 1003 |
return followLine(new Cell(x, y)); |
| 1004 |
} |
| 1005 |
|
| 1006 |
public CellSet followIntersection(Cell cell){ |
| 1007 |
return followIntersection(cell, null); |
| 1008 |
} |
| 1009 |
|
| 1010 |
public CellSet followIntersection(Cell cell, Cell blocked){ |
| 1011 |
if(!isIntersection(cell)) return null; |
| 1012 |
CellSet result = new CellSet(); |
| 1013 |
Cell cN = cell.getNorth(); |
| 1014 |
Cell cS = cell.getSouth(); |
| 1015 |
Cell cE = cell.getEast(); |
| 1016 |
Cell cW = cell.getWest(); |
| 1017 |
if(hasEntryPoint(cN, 6)) result.add(cN); |
| 1018 |
if(hasEntryPoint(cS, 2)) result.add(cS); |
| 1019 |
if(hasEntryPoint(cE, 8)) result.add(cE); |
| 1020 |
if(hasEntryPoint(cW, 4)) result.add(cW); |
| 1021 |
if(result.contains(blocked)) result.remove(blocked); |
| 1022 |
return result; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/** |
| 1026 |
* Returns the neighbours of a line-cell that are boundaries |
| 1027 |
* (0 to 2 cells are returned) |
| 1028 |
* |
| 1029 |
* @param cell |
| 1030 |
* @return null if the cell is not a line |
| 1031 |
*/ |
| 1032 |
public CellSet followLine(Cell cell){ |
| 1033 |
if(isHorizontalLine(cell)){ |
| 1034 |
CellSet result = new CellSet(); |
| 1035 |
if(isBoundary(cell.getEast())) result.add(cell.getEast()); |
| 1036 |
if(isBoundary(cell.getWest())) result.add(cell.getWest()); |
| 1037 |
return result; |
| 1038 |
} else if (isVerticalLine(cell)){ |
| 1039 |
CellSet result = new CellSet(); |
| 1040 |
if(isBoundary(cell.getNorth())) result.add(cell.getNorth()); |
| 1041 |
if(isBoundary(cell.getSouth())) result.add(cell.getSouth()); |
| 1042 |
return result; |
| 1043 |
} |
| 1044 |
return null; |
| 1045 |
} |
| 1046 |
|
| 1047 |
public CellSet followLine(Cell cell, Cell blocked){ |
| 1048 |
CellSet nextCells = followLine(cell); |
| 1049 |
if(nextCells.contains(blocked)) nextCells.remove(blocked); |
| 1050 |
return nextCells; |
| 1051 |
} |
| 1052 |
|
| 1053 |
public CellSet followCorner(Cell cell){ |
| 1054 |
return followCorner(cell, null); |
| 1055 |
} |
| 1056 |
|
| 1057 |
public CellSet followCorner(Cell cell, Cell blocked){ |
| 1058 |
if(!isCorner(cell)) return null; |
| 1059 |
if(isCorner1(cell)) return followCorner1(cell, blocked); |
| 1060 |
if(isCorner2(cell)) return followCorner2(cell, blocked); |
| 1061 |
if(isCorner3(cell)) return followCorner3(cell, blocked); |
| 1062 |
if(isCorner4(cell)) return followCorner4(cell, blocked); |
| 1063 |
return null; |
| 1064 |
} |
| 1065 |
|
| 1066 |
public CellSet followCorner1(Cell cell){ |
| 1067 |
return followCorner1(cell, null); |
| 1068 |
} |
| 1069 |
public CellSet followCorner1(Cell cell, Cell blocked){ |
| 1070 |
if(!isCorner1(cell)) return null; |
| 1071 |
CellSet result = new CellSet(); |
| 1072 |
if(!cell.getSouth().equals(blocked)) result.add(cell.getSouth()); |
| 1073 |
if(!cell.getEast().equals(blocked)) result.add(cell.getEast()); |
| 1074 |
return result; |
| 1075 |
} |
| 1076 |
|
| 1077 |
public CellSet followCorner2(Cell cell){ |
| 1078 |
return followCorner2(cell, null); |
| 1079 |
} |
| 1080 |
public CellSet followCorner2(Cell cell, Cell blocked){ |
| 1081 |
if(!isCorner2(cell)) return null; |
| 1082 |
CellSet result = new CellSet(); |
| 1083 |
if(!cell.getSouth().equals(blocked)) result.add(cell.getSouth()); |
| 1084 |
if(!cell.getWest().equals(blocked)) result.add(cell.getWest()); |
| 1085 |
return result; |
| 1086 |
} |
| 1087 |
|
| 1088 |
public CellSet followCorner3(Cell cell){ |
| 1089 |
return followCorner3(cell, null); |
| 1090 |
} |
| 1091 |
public CellSet followCorner3(Cell cell, Cell blocked){ |
| 1092 |
if(!isCorner3(cell)) return null; |
| 1093 |
CellSet result = new CellSet(); |
| 1094 |
if(!cell.getNorth().equals(blocked)) result.add(cell.getNorth()); |
| 1095 |
if(!cell.getWest().equals(blocked)) result.add(cell.getWest()); |
| 1096 |
return result; |
| 1097 |
} |
| 1098 |
|
| 1099 |
public CellSet followCorner4(Cell cell){ |
| 1100 |
return followCorner4(cell, null); |
| 1101 |
} |
| 1102 |
public CellSet followCorner4(Cell cell, Cell blocked){ |
| 1103 |
if(!isCorner4(cell)) return null; |
| 1104 |
CellSet result = new CellSet(); |
| 1105 |
if(!cell.getNorth().equals(blocked)) result.add(cell.getNorth()); |
| 1106 |
if(!cell.getEast().equals(blocked)) result.add(cell.getEast()); |
| 1107 |
return result; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
public CellSet followStub(Cell cell){ |
| 1112 |
return followStub(cell, null); |
| 1113 |
} |
| 1114 |
public CellSet followStub(Cell cell, Cell blocked){ |
| 1115 |
if(!isStub(cell)) return null; |
| 1116 |
CellSet result = new CellSet(); |
| 1117 |
if(isBoundary(cell.getEast())) result.add(cell.getEast()); |
| 1118 |
else if(isBoundary(cell.getWest())) result.add(cell.getWest()); |
| 1119 |
else if(isBoundary(cell.getNorth())) result.add(cell.getNorth()); |
| 1120 |
else if(isBoundary(cell.getSouth())) result.add(cell.getSouth()); |
| 1121 |
if(result.contains(blocked)) result.remove(blocked); |
| 1122 |
return result; |
| 1123 |
} |
| 1124 |
|
| 1125 |
public CellSet followCell(Cell cell){ |
| 1126 |
return followCell(cell, null); |
| 1127 |
} |
| 1128 |
|
| 1129 |
public CellSet followCell(Cell cell, Cell blocked){ |
| 1130 |
if(isIntersection(cell)) return followIntersection(cell, blocked); |
| 1131 |
if(isCorner(cell)) return followCorner(cell, blocked); |
| 1132 |
if(isLine(cell)) return followLine(cell, blocked); |
| 1133 |
if(isStub(cell)) return followStub(cell, blocked); |
| 1134 |
if(isCrossOnLine(cell)) return followCrossOnLine(cell, blocked); |
| 1135 |
System.err.println("Umbiguous input at position "+cell+":"); |
| 1136 |
TextGrid subGrid = getTestingSubGrid(cell); |
| 1137 |
subGrid.printDebug(); |
| 1138 |
throw new RuntimeException("Cannot follow cell "+cell+": cannot determine cell type"); |
| 1139 |
} |
| 1140 |
|
| 1141 |
public String getCellTypeAsString(Cell cell){ |
| 1142 |
if(isK(cell)) return "K"; |
| 1143 |
if(isT(cell)) return "T"; |
| 1144 |
if(isInverseK(cell)) return "inverse K"; |
| 1145 |
if(isInverseT(cell)) return "inverse T"; |
| 1146 |
if(isCorner1(cell)) return "corner 1"; |
| 1147 |
if(isCorner2(cell)) return "corner 2"; |
| 1148 |
if(isCorner3(cell)) return "corner 3"; |
| 1149 |
if(isCorner4(cell)) return "corner 4"; |
| 1150 |
if(isLine(cell)) return "line"; |
| 1151 |
if(isStub(cell)) return "stub"; |
| 1152 |
if(isCrossOnLine(cell)) return "crossOnLine"; |
| 1153 |
return "unrecognisable type"; |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
public CellSet followCrossOnLine(Cell cell, Cell blocked){ |
| 1158 |
CellSet result = new CellSet(); |
| 1159 |
if(isHorizontalCrossOnLine(cell)){ |
| 1160 |
result.add(cell.getEast()); |
| 1161 |
result.add(cell.getWest()); |
| 1162 |
} else if(isVerticalCrossOnLine(cell)){ |
| 1163 |
result.add(cell.getNorth()); |
| 1164 |
result.add(cell.getSouth()); |
| 1165 |
} |
| 1166 |
if(result.contains(blocked)) result.remove(blocked); |
| 1167 |
return result; |
| 1168 |
} |
| 1169 |
|
| 1170 |
public boolean isOutOfBounds(Cell cell){ |
| 1171 |
if(cell.x > getWidth() - 1 |
| 1172 |
|| cell.y > getHeight() - 1 |
| 1173 |
|| cell.x < 0 |
| 1174 |
|| cell.y < 0) return true; |
| 1175 |
return false; |
| 1176 |
} |
| 1177 |
|
| 1178 |
public boolean isOutOfBounds(int x, int y){ |
| 1179 |
char c = get(x, y); |
| 1180 |
if(0 == c) return true; |
| 1181 |
return false; |
| 1182 |
} |
| 1183 |
|
| 1184 |
public boolean isBlank(Cell cell){ |
| 1185 |
char c = get(cell); |
| 1186 |
if(0 == c) return false; |
| 1187 |
return c == ' '; |
| 1188 |
} |
| 1189 |
|
| 1190 |
public boolean isBlank(int x, int y){ |
| 1191 |
char c = get(x, y); |
| 1192 |
if(0 == c) return true; |
| 1193 |
return c == ' '; |
| 1194 |
} |
| 1195 |
|
| 1196 |
public boolean isCorner(Cell cell){ |
| 1197 |
return isCorner(cell.x, cell.y); |
| 1198 |
} |
| 1199 |
public boolean isCorner(int x, int y){ |
| 1200 |
return (isNormalCorner(x,y) || isRoundCorner(x,y)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
public boolean matchesAny(Cell cell, GridPatternGroup criteria){ |
| 1205 |
TextGrid subGrid = getTestingSubGrid(cell); |
| 1206 |
return subGrid.matchesAny(criteria); |
| 1207 |
} |
| 1208 |
|
| 1209 |
public boolean isCorner1(Cell cell){ |
| 1210 |
return matchesAny(cell, GridPatternGroup.corner1Criteria); |
| 1211 |
} |
| 1212 |
|
| 1213 |
public boolean isCorner2(Cell cell){ |
| 1214 |
return matchesAny(cell, GridPatternGroup.corner2Criteria); |
| 1215 |
} |
| 1216 |
|
| 1217 |
public boolean isCorner3(Cell cell){ |
| 1218 |
return matchesAny(cell, GridPatternGroup.corner3Criteria); |
| 1219 |
} |
| 1220 |
|
| 1221 |
public boolean isCorner4(Cell cell){ |
| 1222 |
return matchesAny(cell, GridPatternGroup.corner4Criteria); |
| 1223 |
} |
| 1224 |
|
| 1225 |
public boolean isCross(Cell cell){ |
| 1226 |
return matchesAny(cell, GridPatternGroup.crossCriteria); |
| 1227 |
} |
| 1228 |
|
| 1229 |
public boolean isK(Cell cell){ |
| 1230 |
return matchesAny(cell, GridPatternGroup.KCriteria); |
| 1231 |
} |
| 1232 |
|
| 1233 |
public boolean isInverseK(Cell cell){ |
| 1234 |
return matchesAny(cell, GridPatternGroup.inverseKCriteria); |
| 1235 |
} |
| 1236 |
|
| 1237 |
public boolean isT(Cell cell){ |
| 1238 |
return matchesAny(cell, GridPatternGroup.TCriteria); |
| 1239 |
} |
| 1240 |
|
| 1241 |
public boolean isInverseT(Cell cell){ |
| 1242 |
return matchesAny(cell, GridPatternGroup.inverseTCriteria); |
| 1243 |
} |
| 1244 |
|
| 1245 |
public boolean isNormalCorner(Cell cell){ |
| 1246 |
return matchesAny(cell, GridPatternGroup.normalCornerCriteria); |
| 1247 |
} |
| 1248 |
public boolean isNormalCorner(int x, int y){ |
| 1249 |
return isNormalCorner(new Cell(x, y)); |
| 1250 |
} |
| 1251 |
|
| 1252 |
public boolean isRoundCorner(Cell cell){ |
| 1253 |
return matchesAny(cell, GridPatternGroup.roundCornerCriteria); |
| 1254 |
} |
| 1255 |
|
| 1256 |
public boolean isRoundCorner(int x, int y){ |
| 1257 |
return isRoundCorner(new Cell(x, y)); |
| 1258 |
} |
| 1259 |
|
| 1260 |
public boolean isIntersection(Cell cell){ |
| 1261 |
return matchesAny(cell, GridPatternGroup.intersectionCriteria); |
| 1262 |
} |
| 1263 |
public boolean isIntersection(int x, int y){ |
| 1264 |
return isIntersection(new Cell(x, y)); |
| 1265 |
} |
| 1266 |
|
| 1267 |
public void copyCellsTo(CellSet cells, TextGrid grid){ |
| 1268 |
Iterator it = cells.iterator(); |
| 1269 |
while(it.hasNext()){ |
| 1270 |
Cell cell = (Cell) it.next(); |
| 1271 |
grid.set(cell, this.get(cell)); |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
public boolean equals(TextGrid grid){ |
| 1276 |
if(grid.getHeight() != this.getHeight() |
| 1277 |
|| grid.getWidth() != this.getWidth() |
| 1278 |
){ |
| 1279 |
return false; |
| 1280 |
} |
| 1281 |
int height = grid.getHeight(); |
| 1282 |
for(int i = 0; i < height; i++){ |
| 1283 |
String row1 = this.getRow(i).toString(); |
| 1284 |
String row2 = grid.getRow(i).toString(); |
| 1285 |
if(!row1.equals(row2)) return false; |
| 1286 |
} |
| 1287 |
return true; |
| 1288 |
} |
| 1289 |
|
| 1290 |
// @Override |
| 1291 |
// public int hashCode() { |
| 1292 |
// int h = 0; |
| 1293 |
// for(StringBuffer row : rows) { |
| 1294 |
// h += row.hashCode(); |
| 1295 |
// } |
| 1296 |
// return h; |
| 1297 |
// } |
| 1298 |
|
| 1299 |
/** |
| 1300 |
* Fills all the cells in <code>cells</code> with <code>c</code> |
| 1301 |
* |
| 1302 |
* @param cells |
| 1303 |
* @param c |
| 1304 |
*/ |
| 1305 |
public void fillCellsWith(Iterable cells, char c){ |
| 1306 |
Iterator<Cell> it = cells.iterator(); |
| 1307 |
while(it.hasNext()){ |
| 1308 |
Cell cell = it.next(); |
| 1309 |
set(cell.x, cell.y, c); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
/** |
| 1314 |
* |
| 1315 |
* Fills the continuous area with if c1 characters with c2, |
| 1316 |
* flooding from cell x, y |
| 1317 |
* |
| 1318 |
* @param x |
| 1319 |
* @param y |
| 1320 |
* @param c1 the character to replace |
| 1321 |
* @param c2 the character to replace c1 with |
| 1322 |
* @return the list of cells filled |
| 1323 |
*/ |
| 1324 |
// public CellSet fillContinuousArea(int x, int y, char c1, char c2){ |
| 1325 |
// CellSet cells = new CellSet(); |
| 1326 |
// //fillContinuousArea_internal(x, y, c1, c2, cells); |
| 1327 |
// seedFill(new Cell(x, y), c1, c2); |
| 1328 |
// return cells; |
| 1329 |
// } |
| 1330 |
|
| 1331 |
public CellSet fillContinuousArea(int x, int y, char c){ |
| 1332 |
return fillContinuousArea(new Cell(x, y), c); |
| 1333 |
} |
| 1334 |
|
| 1335 |
public CellSet fillContinuousArea(Cell cell, char c){ |
| 1336 |
if(isOutOfBounds(cell)) throw new IllegalArgumentException("Attempted to fill area out of bounds: "+cell); |
| 1337 |
return seedFillOld(cell, c); |
| 1338 |
} |
| 1339 |
|
| 1340 |
private CellSet seedFill(Cell seed, char newChar){ |
| 1341 |
CellSet cellsFilled = new CellSet(); |
| 1342 |
char oldChar = get(seed); |
| 1343 |
|
| 1344 |
if(oldChar == newChar) return cellsFilled; |
| 1345 |
if(isOutOfBounds(seed)) return cellsFilled; |
| 1346 |
|
| 1347 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1348 |
|
| 1349 |
stack.push(seed); |
| 1350 |
|
| 1351 |
while(!stack.isEmpty()){ |
| 1352 |
Cell cell = (Cell) stack.pop(); |
| 1353 |
|
| 1354 |
//set(cell, newChar); |
| 1355 |
cellsFilled.add(cell); |
| 1356 |
|
| 1357 |
Cell nCell = cell.getNorth(); |
| 1358 |
Cell sCell = cell.getSouth(); |
| 1359 |
Cell eCell = cell.getEast(); |
| 1360 |
Cell wCell = cell.getWest(); |
| 1361 |
|
| 1362 |
if(get(nCell) == oldChar && !cellsFilled.contains(nCell)) stack.push(nCell); |
| 1363 |
if(get(sCell) == oldChar && !cellsFilled.contains(sCell)) stack.push(sCell); |
| 1364 |
if(get(eCell) == oldChar && !cellsFilled.contains(eCell)) stack.push(eCell); |
| 1365 |
if(get(wCell) == oldChar && !cellsFilled.contains(wCell)) stack.push(wCell); |
| 1366 |
} |
| 1367 |
|
| 1368 |
return cellsFilled; |
| 1369 |
} |
| 1370 |
|
| 1371 |
private CellSet seedFillOld(Cell seed, char newChar){ |
| 1372 |
CellSet cellsFilled = new CellSet(); |
| 1373 |
char oldChar = get(seed); |
| 1374 |
|
| 1375 |
if(oldChar == newChar) return cellsFilled; |
| 1376 |
if(isOutOfBounds(seed)) return cellsFilled; |
| 1377 |
|
| 1378 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1379 |
|
| 1380 |
stack.push(seed); |
| 1381 |
|
| 1382 |
while(!stack.isEmpty()){ |
| 1383 |
Cell cell = (Cell) stack.pop(); |
| 1384 |
|
| 1385 |
set(cell, newChar); |
| 1386 |
cellsFilled.add(cell); |
| 1387 |
|
| 1388 |
Cell nCell = cell.getNorth(); |
| 1389 |
Cell sCell = cell.getSouth(); |
| 1390 |
Cell eCell = cell.getEast(); |
| 1391 |
Cell wCell = cell.getWest(); |
| 1392 |
|
| 1393 |
if(get(nCell) == oldChar) stack.push(nCell); |
| 1394 |
if(get(sCell) == oldChar) stack.push(sCell); |
| 1395 |
if(get(eCell) == oldChar) stack.push(eCell); |
| 1396 |
if(get(wCell) == oldChar) stack.push(wCell); |
| 1397 |
} |
| 1398 |
|
| 1399 |
return cellsFilled; |
| 1400 |
} |
| 1401 |
|
| 1402 |
|
| 1403 |
/** |
| 1404 |
* |
| 1405 |
* Locates and returns the '*' boundaries that we would |
| 1406 |
* encounter if we did a flood-fill at <code>seed</code>. |
| 1407 |
* |
| 1408 |
* @param seed |
| 1409 |
* @return |
| 1410 |
*/ |
| 1411 |
public CellSet findBoundariesExpandingFrom(Cell seed){ |
| 1412 |
CellSet boundaries = new CellSet(); |
| 1413 |
char oldChar = get(seed); |
| 1414 |
|
| 1415 |
if(isOutOfBounds(seed)) return boundaries; |
| 1416 |
|
| 1417 |
char newChar = 1; //TODO: kludge |
| 1418 |
|
| 1419 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1420 |
|
| 1421 |
stack.push(seed); |
| 1422 |
|
| 1423 |
while(!stack.isEmpty()){ |
| 1424 |
Cell cell = (Cell) stack.pop(); |
| 1425 |
|
| 1426 |
set(cell, newChar); |
| 1427 |
|
| 1428 |
Cell nCell = cell.getNorth(); |
| 1429 |
Cell sCell = cell.getSouth(); |
| 1430 |
Cell eCell = cell.getEast(); |
| 1431 |
Cell wCell = cell.getWest(); |
| 1432 |
|
| 1433 |
if(get(nCell) == oldChar) stack.push(nCell); |
| 1434 |
else if(get(nCell) == '*') boundaries.add(nCell); |
| 1435 |
|
| 1436 |
if(get(sCell) == oldChar) stack.push(sCell); |
| 1437 |
else if(get(sCell) == '*') boundaries.add(sCell); |
| 1438 |
|
| 1439 |
if(get(eCell) == oldChar) stack.push(eCell); |
| 1440 |
else if(get(eCell) == '*') boundaries.add(eCell); |
| 1441 |
|
| 1442 |
if(get(wCell) == oldChar) stack.push(wCell); |
| 1443 |
else if(get(wCell) == '*') boundaries.add(wCell); |
| 1444 |
} |
| 1445 |
|
| 1446 |
return boundaries; |
| 1447 |
} |
| 1448 |
|
| 1449 |
|
| 1450 |
//TODO: incomplete method seedFillLine() |
| 1451 |
private CellSet seedFillLine(Cell cell, char newChar){ |
| 1452 |
CellSet cellsFilled = new CellSet(); |
| 1453 |
|
| 1454 |
Stack stack = new Stack(); |
| 1455 |
|
| 1456 |
char oldChar = get(cell); |
| 1457 |
|
| 1458 |
if(oldChar == newChar) return cellsFilled; |
| 1459 |
if(isOutOfBounds(cell)) return cellsFilled; |
| 1460 |
|
| 1461 |
stack.push(new LineSegment(cell.x, cell.x, cell.y, 1)); |
| 1462 |
stack.push(new LineSegment(cell.x, cell.x, cell.y + 1, -1)); |
| 1463 |
|
| 1464 |
int left; |
| 1465 |
while(!stack.isEmpty()){ |
| 1466 |
LineSegment segment = (LineSegment) stack.pop(); |
| 1467 |
int x; |
| 1468 |
//expand to the left |
| 1469 |
for( |
| 1470 |
x = segment.x1; |
| 1471 |
x >= 0 && get(x, segment.y) == oldChar; |
| 1472 |
--x){ |
| 1473 |
set(x, segment.y, newChar); |
| 1474 |
cellsFilled.add(new Cell(x, segment.y)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
left = cell.getEast().x; |
| 1478 |
boolean skip = (x > segment.x1)? true : false; |
| 1479 |
|
| 1480 |
if(left < segment.x1){ //leak on left? |
| 1481 |
//TODO: i think the first param should be x |
| 1482 |
stack.push( |
| 1483 |
//new LineSegment(segment.y, left, segment.x1 - 1, -segment.dy)); |
| 1484 |
new LineSegment(x, left, segment.y - 1, -segment.dy)); |
| 1485 |
} |
| 1486 |
|
| 1487 |
x = segment.x1 + 1; |
| 1488 |
do { |
| 1489 |
if(!skip) { |
| 1490 |
for( ; x < getWidth() && get(x, segment.y) == oldChar; ++x){ |
| 1491 |
set(x, segment.y, newChar); |
| 1492 |
cellsFilled.add(new Cell(x, segment.y)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
stack.push(new LineSegment(left, x - 1, segment.y, segment.dy)); |
| 1496 |
if(x > segment.x2 + 1) //leak on right? |
| 1497 |
stack.push(new LineSegment(segment.x2 + 1, x - 1, segment.y, -segment.dy)); |
| 1498 |
} |
| 1499 |
skip = false; //skip only once |
| 1500 |
|
| 1501 |
for(++x; x <= segment.x2 && get(x, segment.y) != oldChar; ++x){;} |
| 1502 |
left = x; |
| 1503 |
} while( x < segment.x2); |
| 1504 |
} |
| 1505 |
|
| 1506 |
return cellsFilled; |
| 1507 |
} |
| 1508 |
|
| 1509 |
public boolean cellContainsDashedLineChar(Cell cell){ |
| 1510 |
char c = get(cell); |
| 1511 |
return StringUtils.isOneOf(c, dashedLines); |
| 1512 |
} |
| 1513 |
|
| 1514 |
public boolean loadFrom(String filename) |
| 1515 |
throws FileNotFoundException, IOException |
| 1516 |
{ |
| 1517 |
return loadFrom(filename, null); |
| 1518 |
} |
| 1519 |
|
| 1520 |
public boolean loadFrom(String filename, ProcessingOptions options) |
| 1521 |
throws IOException |
| 1522 |
{ |
| 1523 |
|
| 1524 |
String encoding = (options == null) ? null : options.getCharacterEncoding(); |
| 1525 |
ArrayList<StringBuffer> lines = new ArrayList<StringBuffer>(); |
| 1526 |
String[] linesArray = FileUtils.readFile(new File(filename), encoding).split("(\r)?\n"); |
| 1527 |
for(int i = 0; i < linesArray.length; i++) |
| 1528 |
lines.add(new StringBuffer(linesArray[i])); |
| 1529 |
|
| 1530 |
return initialiseWithLines(lines, options); |
| 1531 |
} |
| 1532 |
|
| 1533 |
public boolean initialiseWithText(String text, ProcessingOptions options) throws UnsupportedEncodingException { |
| 1534 |
|
| 1535 |
ArrayList<StringBuffer> lines = new ArrayList<StringBuffer>(); |
| 1536 |
String[] linesArray = text.split("(\r)?\n"); |
| 1537 |
for(int i = 0; i < linesArray.length; i++) |
| 1538 |
lines.add(new StringBuffer(linesArray[i])); |
| 1539 |
|
| 1540 |
return initialiseWithLines(lines, options); |
| 1541 |
} |
| 1542 |
|
| 1543 |
public boolean initialiseWithLines(ArrayList<StringBuffer> lines, ProcessingOptions options) throws UnsupportedEncodingException { |
| 1544 |
|
| 1545 |
//remove blank rows at the bottom |
| 1546 |
boolean done = false; |
| 1547 |
int i; |
| 1548 |
for(i = lines.size() - 1; !done; i--){ |
| 1549 |
StringBuffer row = lines.get(i); |
| 1550 |
if(!StringUtils.isBlank(row.toString())) done = true; |
| 1551 |
} |
| 1552 |
rows = new ArrayList<StringBuffer>(lines.subList(0, i + 2)); |
| 1553 |
|
| 1554 |
if(options != null) fixTabs(options.getTabSize()); |
| 1555 |
else fixTabs(options.DEFAULT_TAB_SIZE); |
| 1556 |
|
| 1557 |
|
| 1558 |
// make all lines of equal length |
| 1559 |
// add blank outline around the buffer to prevent fill glitch |
| 1560 |
// convert tabs to spaces (or remove them if setting is 0) |
| 1561 |
|
| 1562 |
int blankBorderSize = 2; |
| 1563 |
|
| 1564 |
int maxLength = 0; |
| 1565 |
int index = 0; |
| 1566 |
|
| 1567 |
String encoding = null; |
| 1568 |
//if(options != null) encoding = options.getCharacterEncoding(); |
| 1569 |
|
| 1570 |
Iterator<StringBuffer> it = rows.iterator(); |
| 1571 |
while(it.hasNext()){ |
| 1572 |
String row = it.next().toString(); |
| 1573 |
if(encoding != null){ |
| 1574 |
byte[] bytes = row.getBytes(); |
| 1575 |
row = new String(bytes, encoding); |
| 1576 |
} |
| 1577 |
if(row.length() > maxLength) maxLength = row.length(); |
| 1578 |
rows.set(index, new StringBuffer(row)); |
| 1579 |
index++; |
| 1580 |
} |
| 1581 |
|
| 1582 |
it = rows.iterator(); |
| 1583 |
ArrayList<StringBuffer> newRows = new ArrayList<StringBuffer>(); |
| 1584 |
//TODO: make the following depend on blankBorderSize |
| 1585 |
|
| 1586 |
StringBuffer topBottomRow = |
| 1587 |
new StringBuffer(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2)); |
| 1588 |
|
| 1589 |
newRows.add(topBottomRow); |
| 1590 |
newRows.add(topBottomRow); |
| 1591 |
while(it.hasNext()){ |
| 1592 |
StringBuffer row = it.next(); |
| 1593 |
|
| 1594 |
if(row.length() < maxLength) { |
| 1595 |
String borderString = StringUtils.repeatString(" ", blankBorderSize); |
| 1596 |
StringBuffer newRow = new StringBuffer(); |
| 1597 |
|
| 1598 |
newRow.append(borderString); |
| 1599 |
newRow.append(row); |
| 1600 |
newRow.append(StringUtils.repeatString(" ", maxLength - row.length())); |
| 1601 |
newRow.append(borderString); |
| 1602 |
|
| 1603 |
newRows.add(newRow); |
| 1604 |
} else { //TODO: why is the following line like that? |
| 1605 |
newRows.add(new StringBuffer(" ").append(row).append(" ")); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
//TODO: make the following depend on blankBorderSize |
| 1609 |
newRows.add(topBottomRow); |
| 1610 |
newRows.add(topBottomRow); |
| 1611 |
rows = newRows; |
| 1612 |
|
| 1613 |
replaceBullets(); |
| 1614 |
replaceHumanColorCodes(); |
| 1615 |
|
| 1616 |
return true; |
| 1617 |
} |
| 1618 |
|
| 1619 |
private void fixTabs(int tabSize){ |
| 1620 |
|
| 1621 |
int rowIndex = 0; |
| 1622 |
Iterator<StringBuffer> it = rows.iterator(); |
| 1623 |
|
| 1624 |
while(it.hasNext()){ |
| 1625 |
String row = it.next().toString(); |
| 1626 |
StringBuffer newRow = new StringBuffer(); |
| 1627 |
|
| 1628 |
char[] chars = row.toCharArray(); |
| 1629 |
for(int i = 0; i < chars.length; i++){ |
| 1630 |
if(chars[i] == '\t'){ |
| 1631 |
int spacesLeft = tabSize - newRow.length() % tabSize; |
| 1632 |
if(DEBUG){ |
| 1633 |
System.out.println("Found tab. Spaces left: "+spacesLeft); |
| 1634 |
} |
| 1635 |
String spaces = StringUtils.repeatString(" ", spacesLeft); |
| 1636 |
newRow.append(spaces); |
| 1637 |
} else { |
| 1638 |
String character = Character.toString(chars[i]); |
| 1639 |
newRow.append(character); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
rows.set(rowIndex, newRow); |
| 1643 |
rowIndex++; |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
/** |
| 1648 |
* @return |
| 1649 |
*/ |
| 1650 |
protected ArrayList<StringBuffer> getRows() { |
| 1651 |
return rows; |
| 1652 |
} |
| 1653 |
|
| 1654 |
public class CellColorPair{ |
| 1655 |
public CellColorPair(Cell cell, Color color){ |
| 1656 |
this.cell = cell; |
| 1657 |
this.color = color; |
| 1658 |
} |
| 1659 |
public Color color; |
| 1660 |
public Cell cell; |
| 1661 |
} |
| 1662 |
|
| 1663 |
public class CellStringPair{ |
| 1664 |
public CellStringPair(Cell cell, String string){ |
| 1665 |
this.cell = cell; |
| 1666 |
this.string = string; |
| 1667 |
} |
| 1668 |
public Cell cell; |
| 1669 |
public String string; |
| 1670 |
} |
| 1671 |
|
| 1672 |
public class CellTagPair{ |
| 1673 |
public CellTagPair(Cell cell, String tag){ |
| 1674 |
this.cell = cell; |
| 1675 |
this.tag = tag; |
| 1676 |
} |
| 1677 |
public Cell cell; |
| 1678 |
public String tag; |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
public class Cell{ |
| 1683 |
|
| 1684 |
public int x, y; |
| 1685 |
|
| 1686 |
public Cell(Cell cell){ |
| 1687 |
this(cell.x, cell.y); |
| 1688 |
} |
| 1689 |
|
| 1690 |
public Cell(int x, int y){ |
| 1691 |
this.x = x; |
| 1692 |
this.y = y; |
| 1693 |
} |
| 1694 |
|
| 1695 |
public Cell getNorth(){ return new Cell(x, y - 1); } |
| 1696 |
public Cell getSouth(){ return new Cell(x, y + 1); } |
| 1697 |
public Cell getEast(){ return new Cell(x + 1, y); } |
| 1698 |
public Cell getWest(){ return new Cell(x - 1, y); } |
| 1699 |
|
| 1700 |
public Cell getNW(){ return new Cell(x - 1, y - 1); } |
| 1701 |
public Cell getNE(){ return new Cell(x + 1, y - 1); } |
| 1702 |
public Cell getSW(){ return new Cell(x - 1, y + 1); } |
| 1703 |
public Cell getSE(){ return new Cell(x + 1, y + 1); } |
| 1704 |
|
| 1705 |
public CellSet getNeighbours4(){ |
| 1706 |
CellSet result = new CellSet(); |
| 1707 |
|
| 1708 |
result.add(getNorth()); |
| 1709 |
result.add(getSouth()); |
| 1710 |
result.add(getWest()); |
| 1711 |
result.add(getEast()); |
| 1712 |
|
| 1713 |
return result; |
| 1714 |
} |
| 1715 |
|
| 1716 |
public CellSet getNeighbours8(){ |
| 1717 |
CellSet result = new CellSet(); |
| 1718 |
|
| 1719 |
result.add(getNorth()); |
| 1720 |
result.add(getSouth()); |
| 1721 |
result.add(getWest()); |
| 1722 |
result.add(getEast()); |
| 1723 |
|
| 1724 |
result.add(getNW()); |
| 1725 |
result.add(getNE()); |
| 1726 |
result.add(getSW()); |
| 1727 |
result.add(getSE()); |
| 1728 |
|
| 1729 |
return result; |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
public boolean isNorthOf(Cell cell){ |
| 1734 |
if(this.y < cell.y) return true; |
| 1735 |
return false; |
| 1736 |
} |
| 1737 |
|
| 1738 |
public boolean isSouthOf(Cell cell){ |
| 1739 |
if(this.y > cell.y) return true; |
| 1740 |
return false; |
| 1741 |
} |
| 1742 |
|
| 1743 |
public boolean isWestOf(Cell cell){ |
| 1744 |
if(this.x < cell.x) return true; |
| 1745 |
return false; |
| 1746 |
} |
| 1747 |
|
| 1748 |
public boolean isEastOf(Cell cell){ |
| 1749 |
if(this.x > cell.x) return true; |
| 1750 |
return false; |
| 1751 |
} |
| 1752 |
|
| 1753 |
|
| 1754 |
public boolean equals(Object o){ |
| 1755 |
Cell cell = (Cell) o; |
| 1756 |
if(cell == null) return false; |
| 1757 |
if(x == cell.x && y == cell.y) return true; |
| 1758 |
else return false; |
| 1759 |
} |
| 1760 |
|
| 1761 |
public int hashCode() { |
| 1762 |
return (x << 16) | y; |
| 1763 |
} |
| 1764 |
|
| 1765 |
public boolean isNextTo(int x2, int y2){ |
| 1766 |
if(Math.abs(x2 - x) == 1 && Math.abs(y2 - y) == 1) return false; |
| 1767 |
if(Math.abs(x2 - x) == 1 && y2 == y) return true; |
| 1768 |
if(Math.abs(y2 - y) == 1 && x2 == x) return true; |
| 1769 |
return false; |
| 1770 |
} |
| 1771 |
|
| 1772 |
public boolean isNextTo(Cell cell){ |
| 1773 |
if(cell == null) throw new IllegalArgumentException("cell cannot be null"); |
| 1774 |
return this.isNextTo(cell.x, cell.y); |
| 1775 |
} |
| 1776 |
|
| 1777 |
public String toString(){ |
| 1778 |
return "("+x+", "+y+")"; |
| 1779 |
} |
| 1780 |
|
| 1781 |
public void scale(int s){ |
| 1782 |
x = x * s; |
| 1783 |
y = y * s; |
| 1784 |
} |
| 1785 |
|
| 1786 |
} |
| 1787 |
|
| 1788 |
private class LineSegment{ |
| 1789 |
int x1, x2, y, dy; |
| 1790 |
public LineSegment(int x1, int x2, int y, int dy){ |
| 1791 |
this.x1 = x1; |
| 1792 |
this.x2 = x2; |
| 1793 |
this.y = y; |
| 1794 |
this.dy = dy; |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|