-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPageBreakerIntegrationTest.java
More file actions
382 lines (322 loc) · 15.7 KB
/
PageBreakerIntegrationTest.java
File metadata and controls
382 lines (322 loc) · 15.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.demcha.compose.engine.integration;
import com.demcha.compose.GraphCompose;
import com.demcha.compose.testsupport.engine.assembly.ComponentBuilder;
import com.demcha.compose.engine.components.content.shape.Stroke;
import com.demcha.compose.engine.components.content.text.BlockTextData;
import com.demcha.compose.engine.components.content.text.LineTextData;
import com.demcha.compose.engine.components.content.text.TextStyle;
import com.demcha.compose.engine.components.core.Entity;
import com.demcha.compose.engine.components.layout.Align;
import com.demcha.compose.engine.components.layout.Anchor;
import com.demcha.compose.engine.components.layout.coordinator.Placement;
import com.demcha.compose.engine.components.style.ComponentColor;
import com.demcha.compose.engine.components.style.Margin;
import com.demcha.compose.engine.components.style.Padding;
import com.demcha.compose.testsupport.EngineComposerHarness;
import com.demcha.compose.font.FontName;
import com.demcha.testing.VisualTestOutputs;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.junit.jupiter.api.Test;
import java.awt.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for page breaking functionality.
* Tests that large content correctly spans multiple pages.
*/
class PageBreakerIntegrationTest {
@Test
void shouldBreakTextAcrossPages() throws Exception {
Path outputFile = VisualTestOutputs.preparePdf("page_breaker_text_test", "guides", "integration");
try (EngineComposerHarness composer = com.demcha.compose.testsupport.EngineComposerHarness.pdf(outputFile)
.pageSize(PDRectangle.A4)
.margin(20, 20, 20, 20)
.guideLines(true)
.create()) {
ComponentBuilder cb = composer.componentBuilder();
List<Entity> textBlocks = createLargeTextContent(cb, 95, 3);
var containerBuilder = cb.vContainer(Align.middle(5))
.entityName("MainContainer")
.anchor(Anchor.topCenter())
.margin(Margin.of(50));
for (Entity block : textBlocks) {
containerBuilder.addChild(block);
}
containerBuilder.build();
composer.build();
}
assertThat(outputFile).exists();
assertThat(outputFile).isNotEmptyFile();
// Verify multiple pages were created
try (PDDocument doc = Loader.loadPDF(outputFile.toFile())) {
assertThat(doc.getNumberOfPages()).isGreaterThan(1);
}
}
@Test
void shouldPreserveCachedLineMeasurementsAcrossPageBreakCopies() throws Exception {
Path outputFile = VisualTestOutputs.preparePdf("page_breaker_block_text_cache_test", "clean", "integration");
Entity blockText;
List<Double> widthsBeforeLayout;
List<Double> baselinesBeforeLayout;
try (EngineComposerHarness composer = com.demcha.compose.testsupport.EngineComposerHarness.pdf(outputFile)
.pageSize(PDRectangle.A4)
.margin(20, 20, 20, 20)
.markdown(true)
.create()) {
ComponentBuilder cb = composer.componentBuilder();
blockText = cb.blockText(Align.left(3), TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(9)
.color(ComponentColor.BLACK)
.build())
.size(320, 2)
.anchor(Anchor.topLeft())
.padding(Padding.of(6))
.margin(Margin.of(8))
.entityName("PageBreakCacheProbe")
.text(List.of(createNestedMarkdownHeadingText(22)),
TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(9)
.color(ComponentColor.BLACK)
.build(),
Padding.zero(),
Margin.zero())
.build();
BlockTextData beforeLayout = blockText.getComponent(BlockTextData.class).orElseThrow();
widthsBeforeLayout = beforeLayout.lines().stream()
.map(LineTextData::lineWidth)
.collect(Collectors.toList());
baselinesBeforeLayout = beforeLayout.lines().stream()
.map(LineTextData::baselineOffset)
.collect(Collectors.toList());
composer.build();
}
BlockTextData afterLayout = blockText.getComponent(BlockTextData.class).orElseThrow();
assertThat(afterLayout.lines()).isNotEmpty();
assertThat(afterLayout.lines()).allSatisfy(line -> {
assertThat(line.hasCachedLineWidth()).isTrue();
assertThat(line.hasCachedLineMetrics()).isTrue();
assertThat(line.hasCachedBaselineOffset()).isTrue();
});
assertThat(afterLayout.lines().stream().map(LineTextData::lineWidth).toList())
.containsExactlyElementsOf(widthsBeforeLayout);
assertThat(afterLayout.lines().stream().map(LineTextData::baselineOffset).toList())
.containsExactlyElementsOf(baselinesBeforeLayout);
assertThat(afterLayout.lines().stream().map(LineTextData::page).distinct().count()).isGreaterThan(1);
try (PDDocument doc = Loader.loadPDF(outputFile.toFile())) {
assertThat(doc.getNumberOfPages()).isGreaterThan(1);
}
}
@Test
void shouldBreakColoredRectanglesAcrossPages() throws Exception {
Path outputFile = VisualTestOutputs.preparePdf("page_breaker_rectangles_test", "guides", "integration");
try (EngineComposerHarness composer = com.demcha.compose.testsupport.EngineComposerHarness.pdf(outputFile)
.pageSize(PDRectangle.A4)
.margin(20, 20, 20, 20)
.guideLines(true)
.create()) {
ComponentBuilder cb = composer.componentBuilder();
List<Entity> rectangles = createColoredRectangles(cb);
var containerBuilder = cb.vContainer(Align.middle(5))
.entityName("MainContainer")
.anchor(Anchor.topCenter())
.margin(Margin.of(50));
for (Entity rect : rectangles) {
containerBuilder.addChild(rect);
}
containerBuilder.build();
composer.build();
}
assertThat(outputFile).exists();
assertThat(outputFile).isNotEmptyFile();
try (PDDocument doc = Loader.loadPDF(outputFile.toFile())) {
assertThat(doc.getNumberOfPages()).isGreaterThan(1);
}
}
@Test
void shouldBreakNestedContainersWithMarkdownHeadingsAcrossPages() throws Exception {
Path outputFile = VisualTestOutputs.preparePdf(
"page_breaker_nested_container_markdown_heading_test",
"guides",
"integration");
try (EngineComposerHarness composer = com.demcha.compose.testsupport.EngineComposerHarness.pdf(outputFile)
.pageSize(PDRectangle.A4)
.margin(18, 18, 18, 18)
.markdown(true)
.guideLines(true)
.create()) {
ComponentBuilder cb = composer.componentBuilder();
Entity title = cb.text()
.textWithAutoSize("Nested Container Markdown Heading Pagination Preview")
.textStyle(TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(12)
.decoration(com.demcha.compose.engine.components.content.text.TextDecoration.BOLD)
.color(ComponentColor.BLACK)
.build())
.anchor(Anchor.topLeft())
.padding(Padding.zero())
.margin(Margin.zero())
.entityName("NestedPreviewTitle")
.build();
Entity markdownBlock = cb.blockText(Align.left(2), TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(9)
.color(ComponentColor.BLACK)
.build())
.size(320, 2)
.anchor(Anchor.topLeft())
.padding(Padding.of(6))
.margin(Margin.of(4))
.entityName("NestedMarkdownHeadingBlock")
.text(List.of(createNestedMarkdownHeadingText(20)),
TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(9)
.color(ComponentColor.BLACK)
.build(),
Padding.zero(),
Margin.zero())
.build();
Entity innerContainer = cb.vContainer(Align.middle(8))
.entityName("InnerMarkdownContainer")
.anchor(Anchor.topLeft())
.padding(Padding.of(12))
.margin(Margin.of(8))
.addChild(markdownBlock)
.build();
cb.vContainer(Align.middle(12))
.entityName("OuterPreviewContainer")
.anchor(Anchor.topCenter())
.padding(Padding.of(10))
.margin(Margin.of(24))
.addChild(title)
.addChild(innerContainer)
.build();
composer.build();
}
assertThat(outputFile).exists();
assertThat(outputFile).isNotEmptyFile();
try (PDDocument doc = Loader.loadPDF(outputFile.toFile())) {
assertThat(doc.getNumberOfPages()).isGreaterThan(1);
}
}
@Test
void shouldExpandParentPlacementAfterLeafMovesToNextPage() throws Exception {
Path outputFile = VisualTestOutputs.preparePdf(
"page_breaker_parent_after_leaf_shift_test",
"guides",
"integration");
Placement parentPlacement;
Placement leafPlacement;
try (EngineComposerHarness composer = com.demcha.compose.testsupport.EngineComposerHarness.pdf(outputFile)
.pageSize(PDRectangle.A4)
.margin(20, 20, 20, 20)
.guideLines(true)
.create()) {
ComponentBuilder cb = composer.componentBuilder();
Entity spacer = cb.rectangle()
.size(320, 680)
.fillColor(ComponentColor.LIGHT_GRAY)
.margin(Margin.of(8))
.entityName("PaginationSpacer")
.build();
Entity shiftedLeaf = cb.rectangle()
.size(320, 220)
.fillColor(ComponentColor.ROYAL_BLUE)
.stroke(new Stroke(ComponentColor.DARK_BLUE, 2))
.margin(Margin.of(8))
.entityName("ShiftedLeaf")
.build();
Entity container = cb.vContainer(Align.middle(10))
.entityName("ShiftedLeafContainer")
.anchor(Anchor.topCenter())
.margin(Margin.of(24))
.addChild(spacer)
.addChild(shiftedLeaf)
.build();
composer.build();
parentPlacement = container.getComponent(Placement.class).orElseThrow();
leafPlacement = shiftedLeaf.getComponent(Placement.class).orElseThrow();
}
assertThat(outputFile).exists();
assertThat(outputFile).isNotEmptyFile();
try (PDDocument doc = Loader.loadPDF(outputFile.toFile())) {
assertThat(doc.getNumberOfPages()).isGreaterThan(1);
}
assertThat(leafPlacement.startPage()).isEqualTo(leafPlacement.endPage());
assertThat(leafPlacement.startPage()).isGreaterThan(0);
int parentFirstPage = Math.min(parentPlacement.startPage(), parentPlacement.endPage());
int parentLastPage = Math.max(parentPlacement.startPage(), parentPlacement.endPage());
assertThat(parentFirstPage).isEqualTo(0);
assertThat(parentLastPage).isGreaterThan(parentFirstPage);
assertThat(leafPlacement.startPage()).isBetween(parentFirstPage, parentLastPage);
}
private List<Entity> createLargeTextContent(ComponentBuilder cb, int rows, double spacing) {
List<Entity> data = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= rows; i++) {
sb.append(i).append(". Test text line - We will see how we break our text into pages\n");
if (i > 0 && i % 20 == 0) {
data.add(createBlockText(cb, sb.toString(), 302, spacing, "textBlock" + i / 20));
sb = new StringBuilder();
}
if (i == rows && !sb.isEmpty()) {
data.add(createBlockText(cb, sb.toString(), 302, spacing, "textBlockFinal"));
}
}
return data;
}
private Entity createBlockText(ComponentBuilder cb, String text, double width, double spacing, String name) {
return cb.blockText(Align.middle(spacing), TextStyle.builder()
.fontName(FontName.HELVETICA)
.size(9)
.color(ComponentColor.BLACK)
.build())
.size(width, 2)
.anchor(Anchor.center())
.padding(Padding.of(5))
.margin(Margin.of(5))
.entityName(name)
.text(List.of(text), TextStyle.DEFAULT_STYLE, Padding.of(5), Margin.of(5))
.build();
}
private List<Entity> createColoredRectangles(ComponentBuilder cb) {
List<Entity> data = new ArrayList<>();
List<Color> colors = List.of(Color.BLUE, Color.DARK_GRAY, Color.GREEN, Color.RED, Color.YELLOW);
for (int i = 0; i < 5; i++) {
Entity rect = cb.rectangle()
.size(300, 300)
.stroke(new Stroke(ComponentColor.PURPLE, 2))
.fillColor(colors.get(i))
.entityName("Rectangle" + i)
.build();
data.add(rect);
}
return data;
}
private String createNestedMarkdownHeadingText(int sections) {
String paragraph = "This paragraph lives inside a nested container and is intentionally long so the page breaker " +
"has to keep recalculating line positions while markdown headings change the local line metrics. " +
"We want to see that the heading breathes a bit more than the body text and that the container still " +
"flows correctly across page boundaries without collapsing the reserved height. ";
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= sections; i++) {
sb.append("# Section ").append(i).append('\n');
sb.append(paragraph.repeat(3)).append('\n');
sb.append("Continuation line for section ").append(i)
.append(" with **bold text** and *italic text* so markdown token styles stay mixed inside the same block.")
.append('\n');
sb.append("Another continuation line for section ").append(i)
.append(" to force wrapping inside the inner container and expose page splitting more clearly.")
.append('\n');
}
return sb.toString();
}
}