fop 0.1.1

FOP (Formatting Objects Processor) — Apache FOP-compatible XSL-FO processor in pure Rust
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! XSL-FO 1.1 Conformance: PDF/A compliance and additional formatting features
//!
//! Part of the XSL-FO 1.1 conformance test suite.
//! Reference: https://www.w3.org/TR/xsl11/

use super::process_fo_document;

// ---------------------------------------------------------------------------
// PDF/A compliance output
// ---------------------------------------------------------------------------

#[test]
fn conformance_pdfa_output() {
    // PDF/A mode should produce valid PDF with XMP metadata
    use fop_render::{PdfCompliance, PdfRenderer};

    let fo_xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>PDF/A compliant document</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##;

    let fo_tree = fop_core::FoTreeBuilder::new()
        .parse(std::io::Cursor::new(fo_xml))
        .expect("parse should succeed");
    let engine = fop_layout::LayoutEngine::new();
    let area_tree = engine.layout(&fo_tree).expect("layout should succeed");

    let renderer = PdfRenderer::new();
    let mut pdf_doc = renderer.render(&area_tree).expect("render should succeed");
    pdf_doc
        .set_compliance(PdfCompliance::PdfA1b)
        .expect("set PDF/A compliance should succeed");
    pdf_doc.info.title = Some("PDF/A Test Document".to_string());
    pdf_doc.info.author = Some("Test Suite".to_string());

    let bytes = pdf_doc.to_bytes().expect("to_bytes should succeed");

    assert!(!bytes.is_empty(), "PDF/A output should not be empty");
    assert!(bytes.starts_with(b"%PDF-"), "Should start with PDF header");
    // PDF/A should include XMP metadata
    let pdf_str = String::from_utf8_lossy(&bytes);
    assert!(
        pdf_str.contains("PDF/A")
            || pdf_str.contains("pdfa")
            || pdf_str.contains("XMP")
            || bytes.len() > 1000,
        "PDF/A output should be non-trivial"
    );
}

#[test]
fn conformance_bidi_override() {
    // fo:bidi-override sets text direction (Section 8.2)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        LTR text
        <fo:bidi-override direction="rtl">RTL override text</fo:bidi-override>
        back to LTR
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "fo:bidi-override should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_initial_property_set() {
    // fo:initial-property-set sets properties for first line (Section 8.3)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        <fo:initial-property-set font-weight="bold" color="blue"/>
        This paragraph has its first line styled differently via initial-property-set.
        The rest of the paragraph uses normal styling.
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "fo:initial-property-set should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_wrapper_element() {
    // fo:wrapper provides a transparent property-setting container (Section 8.12)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        Normal text,
        <fo:wrapper font-weight="bold" color="red">bold red text</fo:wrapper>,
        normal again.
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "fo:wrapper should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_character_element() {
    // fo:character renders a single character with full property control (Section 8.5)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        Text with special character:
        <fo:character character="&#x2764;" font-size="16pt" color="red"/>
        end.
      </fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "fo:character should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_percentage_dimensions() {
    // Percentage-based width/height values (Section 5.11.2)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block width="50%" background-color="lightgray">50% width block</fo:block>
      <fo:block width="75%" background-color="lightyellow">75% width block</fo:block>
      <fo:block text-indent="10%">Block with 10% text-indent</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Percentage dimensions should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_page_sequence_restart() {
    // Multiple page sequences, each restarting page numbering (Section 7.8)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4" initial-page-number="1">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>Sequence 1, page <fo:page-number/></fo:block>
    </fo:flow>
  </fo:page-sequence>
  <fo:page-sequence master-reference="A4" initial-page-number="1">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>Sequence 2, page <fo:page-number/></fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Multiple page-sequences with restart should work: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_nested_lists() {
    // Nested fo:list-block with indentation (Section 9.5)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:list-block provisional-distance-between-starts="15mm" provisional-label-separation="5mm">
        <fo:list-item>
          <fo:list-item-label end-indent="label-end()"><fo:block>1.</fo:block></fo:list-item-label>
          <fo:list-item-body start-indent="body-start()">
            <fo:block>First item</fo:block>
            <fo:list-block provisional-distance-between-starts="10mm" start-indent="10mm">
              <fo:list-item>
                <fo:list-item-label end-indent="label-end()"><fo:block>a.</fo:block></fo:list-item-label>
                <fo:list-item-body start-indent="body-start()">
                  <fo:block>Nested item a</fo:block>
                </fo:list-item-body>
              </fo:list-item>
              <fo:list-item>
                <fo:list-item-label end-indent="label-end()"><fo:block>b.</fo:block></fo:list-item-label>
                <fo:list-item-body start-indent="body-start()">
                  <fo:block>Nested item b</fo:block>
                </fo:list-item-body>
              </fo:list-item>
            </fo:list-block>
          </fo:list-item-body>
        </fo:list-item>
        <fo:list-item>
          <fo:list-item-label end-indent="label-end()"><fo:block>2.</fo:block></fo:list-item-label>
          <fo:list-item-body start-indent="body-start()">
            <fo:block>Second item</fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Nested lists should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_table_border_collapse() {
    // border-collapse="collapse" model (Section 9.3.4)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:table border-collapse="collapse" width="150mm">
        <fo:table-column column-width="50mm"/>
        <fo:table-column column-width="50mm"/>
        <fo:table-column column-width="50mm"/>
        <fo:table-header>
          <fo:table-row>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block font-weight="bold">Name</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block font-weight="bold">Value</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block font-weight="bold">Notes</fo:block></fo:table-cell>
          </fo:table-row>
        </fo:table-header>
        <fo:table-body>
          <fo:table-row>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 1 A</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 1 B</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 1 C</fo:block></fo:table-cell>
          </fo:table-row>
          <fo:table-row>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 2 A</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 2 B</fo:block></fo:table-cell>
            <fo:table-cell border="1pt solid black" padding="3pt"><fo:block>Row 2 C</fo:block></fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Table border-collapse should be supported: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_page_number_formatted() {
    // fo:page-number in various contexts with formatting (Section 8.8)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="15mm" margin-bottom="15mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body margin-top="15mm" margin-bottom="15mm"/>
      <fo:region-before extent="15mm"/>
      <fo:region-after extent="15mm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:static-content flow-name="xsl-region-before">
      <fo:block text-align="center" font-size="9pt">
        Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
      </fo:block>
    </fo:static-content>
    <fo:static-content flow-name="xsl-region-after">
      <fo:block text-align="center" font-size="9pt">Footer - Page <fo:page-number/></fo:block>
    </fo:static-content>
    <fo:flow flow-name="xsl-region-body">
      <fo:block>Page number in header and footer test.</fo:block>
      <fo:block break-before="page">Second page content.</fo:block>
      <fo:block id="last-page">Last page marker.</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Page number formatting should work: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}

#[test]
fn conformance_table_auto_columns() {
    // table-layout="auto" with content-driven column widths (Section 9.3.1)
    let result = process_fo_document(
        r##"<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:table table-layout="auto" width="150mm">
        <fo:table-body>
          <fo:table-row>
            <fo:table-cell border="0.5pt solid black" padding="2pt"><fo:block>Short</fo:block></fo:table-cell>
            <fo:table-cell border="0.5pt solid black" padding="2pt"><fo:block>A much longer cell content</fo:block></fo:table-cell>
            <fo:table-cell border="0.5pt solid black" padding="2pt"><fo:block>Medium length</fo:block></fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"##,
    );
    assert!(
        result.is_ok(),
        "Auto column width table should work: {:?}",
        result.err()
    );
    assert!(!result.expect("test: should succeed").is_empty());
}