-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableCellComposedContentTest.java
More file actions
280 lines (244 loc) · 12.1 KB
/
TableCellComposedContentTest.java
File metadata and controls
280 lines (244 loc) · 12.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.demcha.compose.document.table;
import com.demcha.compose.GraphCompose;
import com.demcha.compose.document.api.DocumentSession;
import com.demcha.compose.document.layout.LayoutGraph;
import com.demcha.compose.document.layout.PlacedFragment;
import com.demcha.compose.document.layout.payloads.ParagraphFragmentPayload;
import com.demcha.compose.document.layout.payloads.TableRowFragmentPayload;
import com.demcha.compose.document.node.ParagraphNode;
import com.demcha.compose.document.node.TableNode;
import com.demcha.compose.document.node.TextAlign;
import com.demcha.compose.document.style.DocumentInsets;
import com.demcha.compose.document.style.DocumentTextStyle;
import com.demcha.compose.testing.layout.LayoutSnapshotAssertions;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
/**
* Phase B (v1.6) — composed table cell content via
* {@link DocumentTableCell#node(com.demcha.compose.document.node.DocumentNode)}.
*
* <p>Verifies that a {@code DocumentNode} child placed inside a table
* cell is prepared against the cell's resolved inner width, sizes the
* row to its measured height, and contributes its own fragments to
* the layout graph at the cell's absolute position. The renderer
* dispatches the child's fragments through the standard
* {@code NodeDefinition} pipeline so any registered child type
* works inside a cell automatically.</p>
*/
class TableCellComposedContentTest {
@Test
void composedCellEmitsChildParagraphFragmentInsideTableLayout() throws Exception {
DocumentTableCell composedCell = DocumentTableCell.node(
new ParagraphNode("CellParagraph", "Composed paragraph in a cell.",
DocumentTextStyle.DEFAULT, TextAlign.LEFT, 0.0,
DocumentInsets.zero(), DocumentInsets.zero()));
DocumentTableCell plainCell = DocumentTableCell.text("Plain right");
TableNode table = new TableNode(
"ComposedTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(composedCell, plainCell)),
DocumentTableStyle.empty(),
400.0,
DocumentInsets.zero(),
DocumentInsets.zero());
try (DocumentSession session = GraphCompose.document()
.pageSize(440, 200)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
LayoutGraph graph = session.layoutGraph();
// Two row-fragments (one per row of plain text) plus one
// child paragraph fragment for the composed cell.
List<PlacedFragment> tableRowFragments = graph.fragments().stream()
.filter(f -> f.payload() instanceof TableRowFragmentPayload)
.toList();
List<PlacedFragment> paragraphFragments = graph.fragments().stream()
.filter(f -> f.payload() instanceof ParagraphFragmentPayload)
.toList();
assertThat(tableRowFragments).hasSize(1);
assertThat(paragraphFragments).hasSize(1);
ParagraphFragmentPayload childPayload = (ParagraphFragmentPayload)
paragraphFragments.get(0).payload();
// The paragraph wraps within the cell's inner width, so the
// full sentence is split across two visual lines. Combine
// them before checking the rendered text.
String composed = childPayload.lines().stream()
.map(l -> l.text())
.reduce("", (a, b) -> a + " " + b)
.trim();
assertThat(composed).contains("Composed paragraph in")
.contains("a cell.");
}
}
@Test
void composedCellHeightDrivesRowHeightWhenChildIsTallerThanText() throws Exception {
// Multi-line paragraph that wraps inside the cell's width.
DocumentTableCell composedCell = DocumentTableCell.node(
new ParagraphNode(
"TallParagraph",
"Line one of the composed paragraph. "
+ "Line two extends the height beyond a single text row "
+ "so the table row grows to accommodate the prepared child.",
DocumentTextStyle.DEFAULT,
TextAlign.LEFT,
2.0,
DocumentInsets.zero(),
DocumentInsets.zero()));
DocumentTableCell plainCell = DocumentTableCell.text("Plain");
TableNode table = new TableNode(
"TallTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(composedCell, plainCell)),
DocumentTableStyle.empty(),
360.0,
DocumentInsets.zero(),
DocumentInsets.zero());
try (DocumentSession session = GraphCompose.document()
.pageSize(400, 220)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
LayoutGraph graph = session.layoutGraph();
PlacedFragment rowFragment = graph.fragments().stream()
.filter(f -> f.payload() instanceof TableRowFragmentPayload)
.findFirst().orElseThrow();
// The single composed paragraph wraps to >= 2 visual lines
// at this width, so the row must be taller than the
// single-line plain-text minimum.
double singleLineHeight = 12.0;
assertThat(rowFragment.height())
.as("Composed cell content drives row height")
.isGreaterThan(singleLineHeight * 1.5);
}
}
@Test
void composedCellRendersInsideCellPadding() throws Exception {
DocumentTableStyle paddedStyle = DocumentTableStyle.builder()
.padding(new DocumentInsets(6, 8, 6, 8))
.build();
DocumentTableCell composedCell = DocumentTableCell.node(
new ParagraphNode("InsetText", "Inset",
DocumentTextStyle.DEFAULT, TextAlign.LEFT, 0.0,
DocumentInsets.zero(), DocumentInsets.zero()))
.withStyle(paddedStyle);
DocumentTableCell plainCell = DocumentTableCell.text("Right").withStyle(paddedStyle);
TableNode table = new TableNode(
"InsetTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(composedCell, plainCell)),
paddedStyle,
360.0,
DocumentInsets.zero(),
DocumentInsets.zero());
try (DocumentSession session = GraphCompose.document()
.pageSize(400, 200)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
LayoutGraph graph = session.layoutGraph();
PlacedFragment rowFragment = graph.fragments().stream()
.filter(f -> f.payload() instanceof TableRowFragmentPayload)
.findFirst().orElseThrow();
PlacedFragment childFragment = graph.fragments().stream()
.filter(f -> f.payload() instanceof ParagraphFragmentPayload)
.findFirst().orElseThrow();
// Child should be placed inside the row's left padding.
assertThat(childFragment.x())
.as("Composed child sits inside the cell's left padding")
.isGreaterThan(rowFragment.x() + 7.5) // padding.left = 8
.isLessThan(rowFragment.x() + 200); // first column width
}
}
@Test
void composedCellWritesChildTextToPdfOutput() throws Exception {
DocumentTableCell composedCell = DocumentTableCell.node(
new ParagraphNode("SignatureLine", "Composed cell text reaches PDF.",
DocumentTextStyle.DEFAULT, TextAlign.LEFT, 0.0,
DocumentInsets.zero(), DocumentInsets.zero()));
DocumentTableCell plainCell = DocumentTableCell.text("Plain");
TableNode table = new TableNode(
"PdfTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(composedCell, plainCell)),
DocumentTableStyle.empty(),
360.0,
DocumentInsets.zero(),
DocumentInsets.zero());
byte[] pdfBytes;
try (DocumentSession session = GraphCompose.document()
.pageSize(400, 200)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
pdfBytes = session.toPdfBytes();
}
try (PDDocument document = Loader.loadPDF(pdfBytes)) {
String extracted = new PDFTextStripper().getText(document);
// The composed paragraph wraps to two visual lines inside
// the cell width, so check the wrapped fragments instead of
// the original concatenation.
assertThat(extracted).contains("Composed cell text");
assertThat(extracted).contains("reaches PDF.");
assertThat(extracted).contains("Plain");
}
}
@Test
void plainTextCellsStayBackCompatibleAfterPhaseBChange() throws Exception {
DocumentTableCell a = DocumentTableCell.text("Alpha");
DocumentTableCell b = DocumentTableCell.text("Beta");
TableNode table = new TableNode(
"BackCompatTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(a, b)),
DocumentTableStyle.empty(),
360.0,
DocumentInsets.zero(),
DocumentInsets.zero());
try (DocumentSession session = GraphCompose.document()
.pageSize(400, 200)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
LayoutGraph graph = session.layoutGraph();
// Plain-text rows must NOT emit child paragraph fragments —
// the cells render text inline through the existing
// PdfTableRowFragmentRenderHandler.
long paragraphFragmentsCount = graph.fragments().stream()
.filter(f -> f.payload() instanceof ParagraphFragmentPayload)
.count();
assertThat(paragraphFragmentsCount).isZero();
long rowFragmentsCount = graph.fragments().stream()
.filter(f -> f.payload() instanceof TableRowFragmentPayload)
.count();
assertThat(rowFragmentsCount).isEqualTo(1);
}
}
@Test
void composedCellLayoutSnapshot() throws Exception {
DocumentTableCell composedCell = DocumentTableCell.node(
new ParagraphNode("CellParagraph", "Composed paragraph in a cell.",
DocumentTextStyle.DEFAULT, TextAlign.LEFT, 0.0,
DocumentInsets.zero(), DocumentInsets.zero()));
DocumentTableCell plainCell = DocumentTableCell.text("Plain right");
TableNode table = new TableNode(
"SnapshotTable",
List.of(DocumentTableColumn.fixed(160), DocumentTableColumn.fixed(160)),
List.of(List.of(composedCell, plainCell)),
DocumentTableStyle.empty(),
360.0,
DocumentInsets.zero(),
DocumentInsets.zero());
try (DocumentSession session = GraphCompose.document()
.pageSize(400, 200)
.margin(DocumentInsets.of(20))
.create()) {
session.add(table);
LayoutSnapshotAssertions.assertMatches(session, "document/table_cell_with_paragraph");
}
}
}