-
Notifications
You must be signed in to change notification settings - Fork 432
/
TestTextElement.java
215 lines (166 loc) · 10.4 KB
/
TestTextElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package technology.tabula;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestTextElement {
@Test
public void createTextElement() {
TextElement textElement = new TextElement(5f, 15f, 10f, 20f, new PDType1Font(Standard14Fonts.FontName.HELVETICA), 1f, "A", 1f);
Assert.assertNotNull(textElement);
Assert.assertEquals("A", textElement.getText());
Assert.assertEquals(1f, textElement.getFontSize(), 0);
Assert.assertEquals(15f, textElement.getLeft(), 0);
Assert.assertEquals(5f, textElement.getTop(), 0);
Assert.assertEquals(10f, textElement.getWidth(), 0);
Assert.assertEquals(20f, textElement.getHeight(), 0);
Assert.assertEquals(Standard14Fonts.FontName.HELVETICA.getName(), textElement.getFont().getName());
Assert.assertEquals(1f, textElement.getWidthOfSpace(), 0);
Assert.assertEquals(0f, textElement.getDirection(), 0);
}
@Test
public void createTextElementWithDirection() {
TextElement textElement = new TextElement(5f, 15f, 10f, 20f, new PDType1Font(Standard14Fonts.FontName.HELVETICA), 1f, "A", 1f, 6f);
Assert.assertNotNull(textElement);
Assert.assertEquals("A", textElement.getText());
Assert.assertEquals(1f, textElement.getFontSize(), 0);
Assert.assertEquals(15f, textElement.getLeft(), 0);
Assert.assertEquals(5f, textElement.getTop(), 0);
Assert.assertEquals(10f, textElement.getWidth(), 0);
Assert.assertEquals(20f, textElement.getHeight(), 0);
Assert.assertEquals(Standard14Fonts.FontName.HELVETICA.getName(), textElement.getFont().getName());
Assert.assertEquals(1f, textElement.getWidthOfSpace(), 0);
Assert.assertEquals(6f, textElement.getDirection(), 0);
}
@Test
public void mergeFourElementsIntoFourWords() {
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(20f, 15f, 10f, 20f, font, 1f, "B", 1f, 6f));
elements.add(new TextElement(40f, 15f, 10f, 20f, font, 1f, "C", 1f, 6f));
elements.add(new TextElement(60f, 15f, 10f, 20f, font, 1f, "D", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> expectedWords = new ArrayList<>();
expectedWords.add(new TextChunk(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f)));
expectedWords.add(new TextChunk(new TextElement(20f, 15f, 10f, 20f, font, 1f, "B", 1f, 6f)));
expectedWords.add(new TextChunk(new TextElement(40f, 15f, 10f, 20f, font, 1f, "C", 1f, 6f)));
expectedWords.add(new TextChunk(new TextElement(60f, 15f, 10f, 20f, font, 1f, "D", 1f, 6f)));
Assert.assertEquals(expectedWords, words);
}
@Test
public void mergeFourElementsIntoOneWord() {
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(0f, 25f, 10f, 20f, font, 1f, "B", 1f, 6f));
elements.add(new TextElement(0f, 35f, 10f, 20f, font, 1f, "C", 1f, 6f));
elements.add(new TextElement(0f, 45f, 10f, 20f, font, 1f, "D", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> expectedWords = new ArrayList<>();
TextChunk textChunk = new TextChunk(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
textChunk.add(new TextElement(0f, 25f, 10f, 20f, font, 1f, "B", 1f, 6f));
textChunk.add(new TextElement(0f, 35f, 10f, 20f, font, 1f, "C", 1f, 6f));
textChunk.add(new TextElement(0f, 45f, 10f, 20f, font, 1f, "D", 1f, 6f));
expectedWords.add(textChunk);
Assert.assertEquals(expectedWords, words);
}
@Test
public void mergeElementsShouldBeIdempotent() {
/*
* a bug in TextElement.merge_words would delete the first TextElement in the array
* it was called with. Discussion here: https://github.com/tabulapdf/tabula-java/issues/78
*/
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(0f, 25f, 10f, 20f, font, 1f, "B", 1f, 6f));
elements.add(new TextElement(0f, 35f, 10f, 20f, font, 1f, "C", 1f, 6f));
elements.add(new TextElement(0f, 45f, 10f, 20f, font, 1f, "D", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> words2 = TextElement.mergeWords(elements);
Assert.assertEquals(words, words2);
}
@Test
public void mergeElementsWithSkippingRules() {
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(0f, 17f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(0f, 25f, 10f, 20f, font, 1f, "B", 1f, 6f));
elements.add(new TextElement(0.001f, 25f, 10f, 20f, font, 1f, " ", 1f, 6f));
elements.add(new TextElement(0f, 35f, 10f, 20f, font, 1f, "C", 1f, 6f));
PDFont TIMES_ROMAN = new PDType1Font(Standard14Fonts.FontName.TIMES_ROMAN);
elements.add(new TextElement(0f, 45f, 10f, 20f, TIMES_ROMAN, 10f, "D", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> expectedWords = new ArrayList<>();
TextChunk textChunk = new TextChunk(new TextElement(0f, 15f, 10f, 20f, font, 1f, "A", 1f, 6f));
textChunk.add(new TextElement(0f, 25f, 10f, 20f, font, 1f, "B", 1f, 6f));
textChunk.add(new TextElement(0f, 35f, 10f, 20f, font, 1f, "C", 1f, 6f));
textChunk.add(new TextElement(0f, 45f, 10f, 20f, TIMES_ROMAN, 10f, "D", 1f, 6f));
expectedWords.add(textChunk);
Assert.assertEquals(expectedWords, words);
}
@Test
public void mergeTenElementsIntoTwoWords() {
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 0f, 10f, 20f, font, 1f, "H", 1f, 6f));
elements.add(new TextElement(0f, 10f, 10f, 20f, font, 1f, "O", 1f, 6f));
elements.add(new TextElement(0f, 20f, 10f, 20f, font, 1f, "L", 1f, 6f));
elements.add(new TextElement(0f, 30f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(0f, 60f, 10f, 20f, font, 1f, "M", 1f, 6f));
elements.add(new TextElement(0f, 70f, 10f, 20f, font, 1f, "U", 1f, 6f));
elements.add(new TextElement(0f, 80f, 10f, 20f, font, 1f, "N", 1f, 6f));
elements.add(new TextElement(0f, 90f, 10f, 20f, font, 1f, "D", 1f, 6f));
elements.add(new TextElement(0f, 100f, 10f, 20f, font, 1f, "O", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> expectedWords = new ArrayList<>();
TextChunk textChunk = new TextChunk(new TextElement(0f, 0f, 10f, 20f, font, 1f, "H", 1f, 6f));
textChunk.add(new TextElement(0f, 10f, 10f, 20f, font, 1f, "O", 1f, 6f));
textChunk.add(new TextElement(0f, 20f, 10f, 20f, font, 1f, "L", 1f, 6f));
textChunk.add(new TextElement(0f, 30f, 10f, 20f, font, 1f, "A", 1f, 6f));
textChunk.add(new TextElement(0f, 30f, 10.5f, 20f, font, 1f, " ", 1f)); //Check why width=10.5?
expectedWords.add(textChunk);
TextChunk textChunk2 = new TextChunk(new TextElement(0f, 60f, 10f, 20f, font, 1f, "M", 1f, 6f));
textChunk2.add(new TextElement(0f, 70f, 10f, 20f, font, 1f, "U", 1f, 6f));
textChunk2.add(new TextElement(0f, 80f, 10f, 20f, font, 1f, "N", 1f, 6f));
textChunk2.add(new TextElement(0f, 90f, 10f, 20f, font, 1f, "D", 1f, 6f));
textChunk2.add(new TextElement(0f, 100f, 10f, 20f, font, 1f, "O", 1f, 6f));
expectedWords.add(textChunk2);
Assert.assertEquals(2, words.size());
Assert.assertEquals(expectedWords, words);
}
@Test
public void mergeTenElementsIntoTwoLines() {
List<TextElement> elements = new ArrayList<>();
PDType1Font font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
elements.add(new TextElement(0f, 0f, 10f, 20f, font, 1f, "H", 1f, 6f));
elements.add(new TextElement(0f, 10f, 10f, 20f, font, 1f, "O", 1f, 6f));
elements.add(new TextElement(0f, 20f, 10f, 20f, font, 1f, "L", 1f, 6f));
elements.add(new TextElement(0f, 30f, 10f, 20f, font, 1f, "A", 1f, 6f));
elements.add(new TextElement(20f, 0f, 10f, 20f, font, 1f, "M", 1f, 6f));
elements.add(new TextElement(20f, 10f, 10f, 20f, font, 1f, "U", 1f, 6f));
elements.add(new TextElement(20f, 20f, 10f, 20f, font, 1f, "N", 1f, 6f));
elements.add(new TextElement(20f, 30f, 10f, 20f, font, 1f, "D", 1f, 6f));
elements.add(new TextElement(20f, 40f, 10f, 20f, font, 1f, "O", 1f, 6f));
List<TextChunk> words = TextElement.mergeWords(elements);
List<TextChunk> expectedWords = new ArrayList<>();
TextChunk textChunk = new TextChunk(new TextElement(0f, 0f, 10f, 20f, font, 1f, "H", 1f, 6f));
textChunk.add(new TextElement(0f, 10f, 10f, 20f, font, 1f, "O", 1f, 6f));
textChunk.add(new TextElement(0f, 20f, 10f, 20f, font, 1f, "L", 1f, 6f));
textChunk.add(new TextElement(0f, 30f, 10f, 20f, font, 1f, "A", 1f, 6f));
expectedWords.add(textChunk);
TextChunk textChunk2 = new TextChunk(new TextElement(20f, 0f, 10f, 20f, font, 1f, "M", 1f, 6f));
textChunk2.add(new TextElement(20f, 10f, 10f, 20f, font, 1f, "U", 1f, 6f));
textChunk2.add(new TextElement(20f, 20f, 10f, 20f, font, 1f, "N", 1f, 6f));
textChunk2.add(new TextElement(20f, 30f, 10f, 20f, font, 1f, "D", 1f, 6f));
textChunk2.add(new TextElement(20f, 40f, 10f, 20f, font, 1f, "O", 1f, 6f));
expectedWords.add(textChunk2);
Assert.assertEquals(2, words.size());
Assert.assertEquals(expectedWords, words);
}
}