forked from vandeseer/easytable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerticalTextCellTest.java
204 lines (177 loc) · 9.12 KB
/
VerticalTextCellTest.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
package org.vandeseer.integrationtest;
import de.redsix.pdfcompare.CompareResult;
import de.redsix.pdfcompare.PdfComparator;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.junit.Before;
import org.junit.Test;
import org.vandeseer.TestUtils;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.cell.AbstractCell;
import org.vandeseer.easytable.structure.cell.ImageCell;
import org.vandeseer.easytable.structure.cell.TextCell;
import org.vandeseer.easytable.structure.cell.VerticalTextCell;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import static junit.framework.TestCase.assertTrue;
import static org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName.HELVETICA;
import static org.vandeseer.TestUtils.getActualPdfFor;
import static org.vandeseer.TestUtils.getExpectedPdfFor;
import static org.vandeseer.easytable.settings.HorizontalAlignment.*;
import static org.vandeseer.easytable.settings.VerticalAlignment.*;
public class VerticalTextCellTest {
private static final Color LIGHT_GREEN = new Color(221, 255, 217);
private static final String FILE_NAME = "cellVerticalText.pdf";
private PDFont ownFont;
private PDImageXObject checkImage;
@Before
public void before() throws IOException {
PDDocument document = new PDDocument();
// Load a custom font
final InputStream resourceAsStream = this.getClass()
.getClassLoader()
.getResourceAsStream("OpenSansCondensed-Light.ttf");
ownFont = PDType0Font.load(document, resourceAsStream);
// Load custom image
final byte[] sampleBytes = IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getClassLoader()
.getResourceAsStream("check.png")));
checkImage = PDImageXObject.createFromByteArray(document, sampleBytes, "check");
}
@Test
public void testVerticalTextCell() throws IOException {
TestUtils.createAndSaveDocumentWithTables(FILE_NAME,
createSimpleTable(),
createKnowledgeBaseExampleTable(),
createAlignmentTable()
);
CompareResult compareResult = new PdfComparator<>(getExpectedPdfFor(FILE_NAME), getActualPdfFor(FILE_NAME)).compare();
assertTrue(compareResult.isEqual());
}
private static Table createSimpleTable() {
final Table.TableBuilder tableBuilder = Table.builder()
.addColumnsOfWidth(100, 100, 100, 100)
.fontSize(8)
.font(new PDType1Font(HELVETICA));
tableBuilder
.addRow(Row.builder()
.add(VerticalTextCell.builder().minHeight(80f).borderWidth(1).text("This is a super long text that does not fit in one line").build())
.add(VerticalTextCell.builder().borderWidth(1).text("Two").build())
.add(VerticalTextCell.builder().rowSpan(2).borderWidth(1).text("This is again a very long text that will break at one point :)").build())
.add(VerticalTextCell.builder().borderWidth(1).text("Four").build())
.build())
.addRow(Row.builder()
.add(TextCell.builder().borderWidth(1).text("One 1\nFubarbar").build())
.add(TextCell.builder().borderWidth(1).text("Abc").build())
.add(VerticalTextCell.builder().borderWidth(1).text("Four").build())
.build());
return tableBuilder.build();
}
private Table createKnowledgeBaseExampleTable() {
final Table.TableBuilder tableBuilder = Table.builder()
.addColumnsOfWidth(200, 20, 20, 20, 20, 20)
.fontSize(8)
.font(ownFont)
.addRow(Row.builder()
.add(createEmptySpacingCell())
.add(createPersonCell("Ralph"))
.add(createPersonCell("Homer"))
.add(createPersonCell("Bart"))
.add(createPersonCell("Moe"))
.add(createPersonCell("Krusty"))
.build())
.addRow(Row.builder()
.add(createTechCell("Machine Learning"))
.add(createCheckCell())
.add(createCheckCell())
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createCheckCell())
.backgroundColor(LIGHT_GREEN)
.build())
.addRow(Row.builder()
.add(createTechCell("Software Engineering"))
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createCheckCell())
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.build())
.addRow(Row.builder()
.add(createTechCell("Databases"))
.add(createCheckCell())
.add(createCheckCell())
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createCheckCell())
.backgroundColor(LIGHT_GREEN)
.build())
.addRow(Row.builder()
.add(createTechCell("Network Technology"))
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createCheckCell())
.build())
.addRow(Row.builder()
.add(createTechCell("Security"))
.add(createCheckCell())
.add(createCheckCell())
.add(createEmptyCellWithBorders())
.add(createEmptyCellWithBorders())
.add(createEmptyCellWithBorders())
.backgroundColor(LIGHT_GREEN)
.build());
return tableBuilder.build();
}
private Table createAlignmentTable() {
return Table.builder()
.addColumnsOfWidth(80, 80, 80)
.fontSize(8)
.font(ownFont)
.addRow(Row.builder()
.height(100f)
.add(VerticalTextCell.builder().borderWidth(1).text("Bottom").verticalAlignment(BOTTOM).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Middle").verticalAlignment(MIDDLE).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Top").verticalAlignment(TOP).build())
.build())
.addRow(Row.builder()
.height(100f)
.add(VerticalTextCell.builder().borderWidth(1).text("Left").horizontalAlignment(LEFT).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Center").horizontalAlignment(CENTER).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Right").horizontalAlignment(RIGHT).build())
.build())
.addRow(Row.builder()
.height(100f)
.add(VerticalTextCell.builder().borderWidth(1).text("Bottom Left")
.verticalAlignment(BOTTOM).horizontalAlignment(LEFT).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Middle Center")
.verticalAlignment(MIDDLE).horizontalAlignment(CENTER).build())
.add(VerticalTextCell.builder().borderWidth(1).text("Top Right")
.verticalAlignment(TOP).horizontalAlignment(RIGHT).build())
.build())
.build();
}
private AbstractCell createEmptySpacingCell() {
return TextCell.builder().minHeight(50f).text("").build();
}
private AbstractCell createPersonCell(String ralph) {
return VerticalTextCell.builder().borderWidth(1).text(ralph).build();
}
private AbstractCell createTechCell(String tech) {
return TextCell.builder().borderWidth(1).text(tech).verticalAlignment(MIDDLE).build();
}
private AbstractCell createCheckCell() {
return ImageCell.builder().borderWidth(1).image(checkImage).build();
}
private AbstractCell createEmptyCellWithBorders() {
return TextCell.builder().text("").borderWidth(1).build();
}
}