Skip to content

Commit 3fa8a32

Browse files
giorgigajazzido
authored andcommitted
Merged the implementations of compareTo in Rectangle and TextChunk
Since both classes implement Comparable<Rectangle>, their compareTo implementations have to be compatible (previously the result of comparing a Rectangle with an RTL TextChunk depended on which compareTo implementation happened to be used).
1 parent e89cc0c commit 3fa8a32

2 files changed

Lines changed: 17 additions & 34 deletions

File tree

src/main/java/technology/tabula/Rectangle.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ public Rectangle(float top, float left, float width, float height) {
1919
}
2020

2121
@Override
22+
/*
23+
We're comparing based on ordering in the logical ordering of text here.
24+
Assuming identical Y-axis positions, if TextChunk A has a lower X-axis
25+
than TextChunk B, then A is "before" it -- iff this is LTR text. Otherwise,
26+
it is A is after B.
27+
*/
2228
public int compareTo(Rectangle other) {
2329
double thisBottom = this.getBottom();
2430
double otherBottom = other.getBottom();
2531
int rv;
2632

27-
if (this.equals(other)) return 0;
33+
if (this.equals(other)) return 0;
2834

29-
if (this.verticalOverlap(other) > VERTICAL_COMPARISON_THRESHOLD) {
35+
if (this.verticalOverlap(other) > VERTICAL_COMPARISON_THRESHOLD) {
3036
rv = java.lang.Double.compare(this.getX(), other.getX());
31-
}
32-
else {
33-
rv = java.lang.Double.compare(thisBottom, otherBottom);
34-
}
35-
return rv;
37+
38+
// reverse the ordering if both TextChunks are RTL
39+
if (this.isLtrDominant() == -1 && other.isLtrDominant() == -1) {
40+
rv = -1 * rv;
41+
}
42+
} else {
43+
rv = java.lang.Double.compare(thisBottom, otherBottom);
44+
}
45+
return rv;
3646
}
3747

3848
// I'm bad at Java and need this for fancy sorting in technology.tabula.TextChunk.

src/main/java/technology/tabula/TextChunk.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,6 @@ public TextChunk groupByDirectionality(Boolean isLtrDominant) {
126126
return new TextChunk(everything);
127127
}
128128

129-
@Override
130-
/*
131-
We're comparing based on ordering in the logical ordering of text here.
132-
Assuming identical Y-axis positions, if TextChunk A has a lower X-axis
133-
than TextChunk B, then A is "before" it -- iff this is LTR text. Otherwise,
134-
it is A is after B.
135-
*/
136-
public int compareTo(Rectangle other) {
137-
double thisBottom = this.getBottom();
138-
double otherBottom = other.getBottom();
139-
int rv;
140-
141-
if (this.equals(other)) return 0;
142-
143-
if (this.verticalOverlap(other) > VERTICAL_COMPARISON_THRESHOLD) {
144-
rv = java.lang.Double.compare(this.getX(), other.getX());
145-
146-
// reverse the ordering if both TextChunks are RTL
147-
if (this.isLtrDominant() == -1 && other.isLtrDominant() == -1) {
148-
rv = -1 * rv;
149-
}
150-
} else {
151-
rv = java.lang.Double.compare(thisBottom, otherBottom);
152-
}
153-
return rv;
154-
}
155-
156129
public int isLtrDominant() {
157130
int ltrCnt = 0;
158131
int rtlCnt = 0;

0 commit comments

Comments
 (0)