ferri-pdf 0.1.1

HTML and CSS to PDF renderer for print, in pure Rust
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use super::common;
use crate::render_html;
use common::{PROFILE, assert_labelled_pt, local_options};

const RED: [u8; 3] = [255, 0, 0];
const GREEN: [u8; 3] = [0, 128, 0];
const BLUE: [u8; 3] = [0, 0, 255];
const BLACK: [u8; 3] = [0, 0, 0];

fn trace(css: &str, body: &str) -> crate::LayoutTrace {
    render_html(
        &format!("<style>{PROFILE}ol,ul{{margin:0;padding-left:60pt}}{css}</style>{body}"),
        &local_options(),
    )
    .unwrap_or_else(|error| panic!("render failed: {error}\ncss: {css}\nbody: {body}"))
    .layout
    .expect("layout requested")
}

/// Every run on the first page as (text, colour, font size, line x + run x).
fn runs(trace: &crate::LayoutTrace) -> Vec<(String, [u8; 3], f32, f32)> {
    trace.pages[0]
        .iter()
        .flat_map(|line| {
            line.runs.iter().map(move |run| {
                (
                    run.text.clone(),
                    run.color.rgb,
                    run.font_size,
                    line.x + run.x,
                )
            })
        })
        .collect()
}

fn run<'a>(runs: &'a [(String, [u8; 3], f32, f32)], text: &str) -> &'a (String, [u8; 3], f32, f32) {
    runs.iter()
        .find(|run| run.0 == text)
        .unwrap_or_else(|| panic!("no run {text:?} among {runs:#?}"))
}

#[test]
fn marker_rules_style_the_marker_text_and_not_the_list_item() {
    let trace = trace(
        "li{color:blue}li::marker{color:red;font-size:20pt}",
        "<ol><li>Item</li></ol>",
    );
    let runs = runs(&trace);
    let marker = run(&runs, "1. ");
    assert_eq!(marker.1, RED, "::marker colour: {runs:#?}");
    assert_labelled_pt("::marker font size", marker.2, 20.0);
    let item = run(&runs, "Item");
    assert_eq!(
        item.1, BLUE,
        "list item text keeps its own colour: {runs:#?}"
    );
    assert_labelled_pt("list item font size", item.2, 12.0);
}

#[test]
fn marker_inherits_from_its_list_item_and_ignores_box_properties() {
    let plain = runs(&trace("li{color:green}", "<ol><li>Item</li></ol>"));
    let boxed = runs(&trace(
        "li{color:green}li::marker{margin-left:50pt;padding:9pt;border:3pt solid;display:block;\
         position:absolute;float:right;width:80pt;text-decoration:underline;background:red}",
        "<ol><li>Item</li></ol>",
    ));
    let (plain, boxed) = (run(&plain, "1. "), run(&boxed, "1. "));
    assert_eq!(plain.1, GREEN, "the marker inherits the list item colour");
    assert_eq!(
        plain, boxed,
        "only inherited text properties, content, direction, unicode-bidi and \
         text-combine-upright apply to ::marker (CSS Lists 3 §3.1.1)"
    );
}

#[test]
fn marker_content_replaces_the_counter_text_or_suppresses_the_marker() {
    let trace = trace(
        "ol{list-style-type:lower-roman}li.note::marker{content:'Note ' counter(list-item) ': '}\
         li.none::marker{content:none}li.empty::marker{content:''}",
        "<ol><li>One</li><li class=note>Two</li><li class=none>Three</li><li class=empty>Four</li><li>Five</li></ol>",
    );
    let text: Vec<_> = trace.pages[0]
        .iter()
        .map(|line| line.text.as_str())
        .collect();
    assert_eq!(
        text,
        [
            "i. ", "One", "Note 2: ", "Two", "Three", "Four", "v. ", "Five"
        ],
        "content on ::marker is generated as for ::before; none and empty content make no box"
    );
}

#[test]
fn marker_white_space_and_text_transform_defaults_yield_to_author_rules() {
    let trace = trace(
        "ul{list-style-type:'a  b';text-transform:uppercase}li+li::marker{white-space:normal;\
         text-transform:inherit}",
        "<ul><li>x</li><li>y</li></ul>",
    );
    let text: Vec<_> = trace.pages[0]
        .iter()
        .map(|line| line.text.as_str())
        .collect();
    assert_eq!(
        text,
        ["a  b", "X", "A B", "Y"],
        "the UA rule ::marker {{ white-space: pre; text-transform: none }} is cascaded"
    );
}

#[test]
fn inside_and_inline_list_item_markers_are_styled_inline_boxes() {
    let trace = trace(
        "li{list-style-position:inside}li::marker{color:red;font-weight:bold}\
         span{display:inline list-item;list-style-type:'* '}span::marker{color:blue}",
        "<ol><li>\n   Item</li></ol><p>Lead <span>inline</span></p>",
    );
    assert_eq!(
        trace.pages[0][0].text, "1. Item",
        "collapsible white space after the pre-spaced inside marker is discarded"
    );
    let runs = runs(&trace);
    let marker = run(&runs, "1. ");
    let item = run(&runs, "Item");
    assert_eq!(marker.1, RED, "inside ::marker colour: {runs:#?}");
    assert_eq!(item.1, BLACK, "list item text keeps its colour: {runs:#?}");
    assert!(
        marker.3 < item.3,
        "the inside marker precedes the content on its line: {runs:#?}"
    );
    let inline = runs
        .iter()
        .find(|run| run.0.starts_with('*'))
        .unwrap_or_else(|| panic!("no inline list item marker among {runs:#?}"));
    assert_eq!(
        inline.1, BLUE,
        "inline list item ::marker colour: {runs:#?}"
    );
}

#[test]
fn marker_rules_cascade_by_specificity_and_reject_pseudo_class_syntax() {
    let trace = trace(
        "ol li::marker{color:red}::marker{color:blue}li:marker{color:green}",
        "<ol><li>Item</li></ol>",
    );
    let runs = runs(&trace);
    assert_eq!(
        run(&runs, "1. ").1,
        RED,
        "ol li::marker outweighs ::marker, and the single-colon :marker is not a \
         pseudo-element (Selectors 4 §16): {runs:#?}"
    );
}

type StyledRun = (String, [u8; 3], f32);

/// The first page's text lines as (text, [(run text, colour, font size)]).
fn styled_lines(trace: &crate::LayoutTrace) -> Vec<(String, Vec<StyledRun>)> {
    trace.pages[0]
        .iter()
        .map(|line| {
            (
                line.text.clone(),
                line.runs
                    .iter()
                    .map(|run| (run.text.clone(), run.color.rgb, run.font_size))
                    .collect(),
            )
        })
        .collect()
}

fn line_runs(lines: &[(String, Vec<StyledRun>)], text: &str) -> Vec<StyledRun> {
    lines
        .iter()
        .find(|line| line.0 == text)
        .unwrap_or_else(|| panic!("no line {text:?} among {lines:#?}"))
        .1
        .clone()
}

fn styled(text: &str, color: [u8; 3], size: f32) -> StyledRun {
    (text.to_owned(), color, size)
}

#[test]
fn first_letter_wraps_the_first_letter_text_and_its_punctuation() {
    let lines = styled_lines(&trace(
        "p::first-letter{color:red;font-size:20pt}p.q::first-letter::prefix{color:blue}\
         p.q::first-letter::suffix{color:green}",
        "<p>Hello</p><p class=q>“(A)” quoted</p><p>67 million</p><p>\"\u{2009}Spaced\"</p>",
    ));
    assert_eq!(
        line_runs(&lines, "Hello"),
        [styled("H", RED, 20.0), styled("ello", BLACK, 12.0)]
    );
    assert_eq!(
        line_runs(&lines, "“(A)” quoted"),
        [
            styled("“(", BLUE, 20.0),
            styled("A", RED, 20.0),
            styled(")”", GREEN, 20.0),
            styled(" quoted", BLACK, 12.0)
        ],
        "::prefix and ::suffix nest inside the innermost ::first-letter and inherit from it"
    );
    assert_eq!(
        line_runs(&lines, "67 million"),
        [styled("6", RED, 20.0), styled("7 million", BLACK, 12.0)],
        "a digit is a first letter"
    );
    assert_eq!(
        line_runs(&lines, "\"\u{2009}Spaced\""),
        [
            styled("\"\u{2009}", RED, 20.0),
            styled("S", RED, 20.0),
            styled("paced\"", BLACK, 12.0)
        ],
        "typographic space between the preceding punctuation and the letter joins the \
         ::prefix, which inherits from the ::first-letter"
    );
}

#[test]
fn first_letter_takes_generated_and_descendant_text_and_nests_by_origin() {
    let lines = styled_lines(&trace(
        "p::first-letter{color:red}div::first-letter{font-size:20pt;color:green}\
         p.n::before{content:'Note: '}span{color:blue}p.s::first-letter{color:inherit}",
        "<p class=n>with before</p><div><p>Nested</p></div><p class=s><span>The</span> span</p>",
    ));
    assert_eq!(
        line_runs(&lines, "Note: with before"),
        [
            styled("N", RED, 12.0),
            styled("ote: ", BLACK, 12.0),
            styled("with before", BLACK, 12.0)
        ],
        "in-flow ::before content holds the first letter"
    );
    assert_eq!(
        line_runs(&lines, "Nested"),
        [styled("N", RED, 20.0), styled("ested", BLACK, 12.0)],
        "p::first-letter nests in, and inherits from, div::first-letter"
    );
    assert_eq!(
        line_runs(&lines, "The span"),
        [
            styled("T", BLUE, 12.0),
            styled("he", BLUE, 12.0),
            styled(" span", BLACK, 12.0)
        ],
        "the ::first-letter wraps text inside a descendant and inherits from it"
    );
}

#[test]
fn first_letter_needs_qualifying_text_first_on_the_first_formatted_line() {
    let lines = styled_lines(&trace(
        "p::first-letter,div::first-letter,li::first-letter{color:red}\
         li{list-style-position:inside}.flex{display:flex}",
        "<p><br>Second</p><p>...</p><div><span style='display:inline-block'>Inline</span> \
         block</div><div class=flex><span>Flex</span></div><ol><li>Item</li></ol>\
         <div><table><tr><td>Cell</td></tr></table>After</div>",
    ));
    for (line, reason) in [
        ("Second", "a forced break empties the first formatted line"),
        ("...", "punctuation alone has no first letter"),
        (
            "Inline",
            "an inline-block's own lines are not its container's",
        ),
        ("Flex", "a flex container is not a block container"),
        (
            "Cell",
            "a table cell's first line is not the enclosing block's",
        ),
        (
            "After",
            "the table wrapper first in flow takes the first formatted line",
        ),
    ] {
        assert!(
            line_runs(&lines, line).iter().all(|run| run.1 == BLACK),
            "{reason}: {lines:#?}"
        );
    }
    assert_eq!(
        line_runs(&lines, "1. Item"),
        [
            styled("1. ", BLACK, 12.0),
            styled("I", RED, 12.0),
            styled("tem", BLACK, 12.0)
        ],
        "an inside marker is ignored as if out of flow"
    );
}

#[test]
fn legacy_single_colon_first_letter_and_floated_first_letter() {
    let trace = trace(
        "p:first-letter{float:left;font-size:36pt;line-height:36pt;color:red;margin-right:3pt}",
        "<p>Floated first letter paragraph whose text wraps beside the floated letter for \
         several lines of ordinary body text.</p>",
    );
    let lines = &trace.pages[0];
    let geometry: Vec<_> = lines
        .iter()
        .map(|line| (line.text.as_str(), line.x, line.width))
        .collect();
    let letter = lines
        .iter()
        .find(|line| line.text == "F")
        .unwrap_or_else(|| panic!("the floated first letter has its own line: {geometry:#?}"));
    assert_eq!(letter.runs[0].color.rgb, RED);
    assert_labelled_pt("floated letter size", letter.runs[0].font_size, 36.0);
    let following: Vec<_> = lines.iter().filter(|line| line.text != "F").collect();
    assert!(
        following[0].text.starts_with("loated"),
        "the rest of the word follows the float: {geometry:#?}"
    );
    assert!(
        following[0].x >= letter.x + letter.width + 3.0 - 0.01,
        "lines beside the float start after its margin box: {geometry:#?}"
    );
    let last = following.last().expect("the paragraph has lines");
    assert!(
        (last.x - letter.x).abs() < 0.01,
        "lines below the float return to the content edge: {geometry:#?}"
    );
}

const PARAGRAPH: &str = "This is a somewhat long paragraph that will be broken into several \
                         lines by the layout of its block.";

fn texts(trace: &crate::LayoutTrace) -> Vec<String> {
    trace.pages[0]
        .iter()
        .map(|line| line.text.clone())
        .collect()
}

#[test]
fn first_line_styles_the_first_formatted_line_after_line_breaking() {
    let styled = trace(
        "p{width:200pt}p::first-line{font-size:16pt;color:red;text-transform:uppercase}",
        &format!("<p>{PARAGRAPH}</p>"),
    );
    let large = trace(
        "p{width:200pt;font-size:16pt;text-transform:uppercase}",
        &format!("<p>{PARAGRAPH}</p>"),
    );
    let plain = trace("p{width:200pt}", &format!("<p>{PARAGRAPH}</p>"));
    let (styled, large, plain) = (texts(&styled), texts(&large), texts(&plain));
    assert_eq!(
        styled[0], large[0],
        "the first line breaks where its ::first-line font size and casing fit: \
         {styled:#?} {large:#?}"
    );
    assert_ne!(
        large[0].len(),
        plain[0].len(),
        "the test needs the first-line styles to move the break"
    );
    let rest = PARAGRAPH[large[0].len()..].trim_start();
    let following = texts(&trace("p{width:200pt}", &format!("<p>{rest}</p>")));
    assert_eq!(
        styled[1..],
        following[..],
        "later lines lay out the remaining text with the paragraph's own styles"
    );
    let lines = styled_lines(&trace(
        "p{width:200pt}p::first-line{font-size:16pt;color:red}",
        &format!("<p>{PARAGRAPH}</p>"),
    ));
    assert!(
        lines[0].1.iter().all(|run| run.1 == RED && run.2 == 16.0),
        "the first line takes the ::first-line styles: {lines:#?}"
    );
    assert!(
        lines[1..]
            .iter()
            .flat_map(|line| &line.1)
            .all(|run| run.1 == BLACK && run.2 == 12.0),
        "later lines keep the paragraph's styles: {lines:#?}"
    );
}

#[test]
fn first_line_inherits_into_descendants_unless_they_set_their_own_values() {
    let lines = styled_lines(&trace(
        "p{width:300pt}p::first-line{font-size:16pt;color:red;direction:rtl}span{color:blue}\
         em{font-size:12pt;font-style:normal}b{font-weight:normal}p::first-letter{color:green}",
        "<p>Start <span>own colour</span> <em>own size</em> <b>inherited</b> then enough text \
         to reach a second line of the paragraph</p>",
    ));
    assert_eq!(
        lines[0].1,
        [
            styled("S", GREEN, 16.0),
            styled("tart ", RED, 16.0),
            styled("own colour", BLUE, 16.0),
            styled(" ", RED, 16.0),
            styled("own size", RED, 12.0),
            styled(" ", RED, 16.0),
            styled("inherited", RED, 16.0),
        ],
        "descendants on the first line inherit from ::first-line; ::first-letter nests \
         inside it; direction does not apply (CSS Pseudo-Elements 4 §2.1.2): {lines:#?}"
    );
    assert!(
        lines[1].1.iter().all(|run| run.2 == 12.0),
        "the second line keeps the original styles: {lines:#?}"
    );
}

#[test]
fn first_line_box_paints_its_background_and_decorations_on_the_line_only() {
    let trace = trace(
        "div{width:200pt}div::first-line{background:yellow;text-decoration:underline}",
        &format!("<div><p>{PARAGRAPH}</p><p>Second paragraph</p></div>"),
    );
    let yellow = common::painted_box(&trace, [255, 255, 0]);
    let first = &trace.pages[0][0];
    assert_labelled_pt("first-line background left", yellow.x, first.x);
    assert_labelled_pt("first-line background width", yellow.width, first.width);
    let underlines: Vec<_> = trace
        .display_lists
        .iter()
        .flatten()
        .filter_map(|item| match item {
            crate::DisplayItem::Decoration(paint) => Some(paint.rect),
            _ => None,
        })
        .collect();
    assert_eq!(
        underlines.len(),
        1,
        "only the first formatted line of the div, in its first p, is underlined: \
         {underlines:?}"
    );
    assert_labelled_pt("underline width", underlines[0].width, first.width);
}

#[test]
fn first_line_needs_the_first_formatted_line_and_shrinks_to_fit_with_its_styles() {
    let lines = styled_lines(&trace(
        "p::first-line{color:red}",
        "<p><br>First</p><div style='display:inline-block'><p>Inline</p></div>",
    ));
    assert!(
        line_runs(&lines, "First").iter().all(|run| run.1 == BLACK),
        "a forced break makes an empty first formatted line: {lines:#?}"
    );
    assert!(
        line_runs(&lines, "Inline").iter().all(|run| run.1 == RED),
        "an inline-block's paragraph has its own first line: {lines:#?}"
    );
    let trace = trace(
        "div{float:left}p::first-line{font-size:24pt}",
        "<div><p>Wide line<br>narrow</p></div>",
    );
    let first = &trace.pages[0][0];
    let float = trace.pages[0]
        .iter()
        .map(|line| line.width)
        .fold(0.0f32, f32::max);
    assert_labelled_pt(
        "the float shrinks to the max-content of its ::first-line styled first line",
        float,
        first.width,
    );
    assert!(
        first.runs.iter().all(|run| run.font_size == 24.0),
        "first line at 24pt"
    );
}

#[test]
fn first_line_rejects_inheritance_it_cannot_render() {
    let atomic = render_html(
        &format!(
            "<style>{PROFILE}p::first-line{{color:red}}</style><p>Lead \
             <span style='display:inline-block'>box</span></p>"
        ),
        &local_options(),
    );
    let error = atomic
        .err()
        .expect("an inline-block inheriting first-line colour fails");
    assert!(
        error.to_string().contains("::first-line"),
        "the failure names the unsupported inheritance: {error}"
    );
    let later = render_html(
        &format!(
            "<style>{PROFILE}p{{width:60pt}}p::first-line{{color:red}}</style><p>Lead words \
             wrap <span style='display:inline-block'>box</span></p>"
        ),
        &local_options(),
    );
    assert!(
        later.is_ok(),
        "an inline-block after the first line inherits nothing from it: {:?}",
        later.err()
    );
    let casing = render_html(
        &format!("<style>{PROFILE}p::first-line{{text-transform:uppercase}}</style><p>straße</p>"),
        &local_options(),
    );
    let error = casing
        .err()
        .expect("uppercasing ß changes the source offsets");
    assert!(
        error.to_string().contains("::first-line text-transform"),
        "{error}"
    );
}