Skip to content

Commit 8aeec9d

Browse files
authored
Merge pull request tabulapdf#92 from batterseapower/master
Fix numerous bugs in collapseOrientedRulings
2 parents 4178d40 + aab5ffb commit 8aeec9d

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/main/java/technology/tabula/Ruling.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ public void setEnd(float v) {
117117
this.setRight(v);
118118
}
119119
}
120+
121+
private void setStartEnd(float start, float end) {
122+
if (this.oblique()) {
123+
throw new UnsupportedOperationException();
124+
}
125+
if (this.vertical()) {
126+
this.setTop(start);
127+
this.setBottom(end);
128+
}
129+
else {
130+
this.setLeft(start);
131+
this.setRight(end);
132+
}
133+
}
120134

121135
// -----
122136

@@ -396,23 +410,32 @@ public static List<Ruling> collapseOrientedRulings(List<Ruling> lines) {
396410

397411
public static List<Ruling> collapseOrientedRulings(List<Ruling> lines, int expandAmount) {
398412
ArrayList<Ruling> rv = new ArrayList<Ruling>();
399-
if (lines.size() == 0) {
400-
return rv;
401-
}
402413
Collections.sort(lines, new Comparator<Ruling>() {
403414
@Override
404415
public int compare(Ruling a, Ruling b) {
405-
return (int) (!Utils.feq(a.getPosition(), b.getPosition()) ? a.getPosition() - b.getPosition() : a.getStart() - b.getStart());
416+
final float diff = a.getPosition() - b.getPosition();
417+
return java.lang.Float.compare(diff == 0 ? a.getStart() - b.getStart() : diff, 0f);
406418
}
407419
});
408-
409-
rv.add(lines.remove(0));
420+
410421
for (Ruling next_line : lines) {
411-
Ruling last = rv.get(rv.size() - 1);
422+
Ruling last = rv.isEmpty() ? null : rv.get(rv.size() - 1);
412423
// if current line colinear with next, and are "close enough": expand current line
413-
if (Utils.feq(next_line.getPosition(), last.getPosition()) && last.nearlyIntersects(next_line, expandAmount)) {
414-
last.setStart(next_line.getStart() < last.getStart() ? next_line.getStart() : last.getStart());
415-
last.setEnd(next_line.getEnd() < last.getEnd() ? last.getEnd() : next_line.getEnd());
424+
if (last != null && Utils.feq(next_line.getPosition(), last.getPosition()) && last.nearlyIntersects(next_line, expandAmount)) {
425+
final float lastStart = last.getStart();
426+
final float lastEnd = last.getEnd();
427+
428+
final boolean lastFlipped = lastStart > lastEnd;
429+
final boolean nextFlipped = next_line.getStart() > next_line.getEnd();
430+
431+
boolean differentDirections = nextFlipped != lastFlipped;
432+
float nextS = differentDirections ? next_line.getEnd() : next_line.getStart();
433+
float nextE = differentDirections ? next_line.getStart() : next_line.getEnd();
434+
435+
final float newStart = lastFlipped ? Math.max(nextS, lastStart) : Math.min(nextS, lastStart);
436+
final float newEnd = lastFlipped ? Math.min(nextE, lastEnd) : Math.max(nextE, lastEnd);
437+
last.setStartEnd(newStart, newEnd);
438+
assert !last.oblique();
416439
}
417440
else if (next_line.length() == 0) {
418441
continue;

src/test/java/technology/tabula/UtilsForTesting.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44

5-
import java.io.BufferedReader;
6-
import java.io.File;
7-
import java.io.FileReader;
8-
import java.io.IOException;
5+
import java.io.*;
96
import java.nio.charset.Charset;
107
import java.util.List;
118

@@ -52,7 +49,7 @@ public static void assertTableEquals(Table table, String[][] arrayOfRows) {
5249

5350
public static String loadJson(String path) throws IOException {
5451

55-
BufferedReader reader = new BufferedReader( new FileReader (path));
52+
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
5653
StringBuilder stringBuilder = new StringBuilder();
5754
String line = null;
5855

0 commit comments

Comments
 (0)