spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
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
use super::*;

fn rect_with_fill(
    page: &spindrift::Page,
    fill: CssColor,
) -> &crate::document::paint::shapes::RenderedRect {
    page.rects()
        .iter()
        .find(|rect| rect.fill == Some(fill))
        .expect("painted item background")
}

#[tokio::test]
async fn grid_lanes_stacking_content_distribution_moves_only_in_flow_items() {
    for (row_lanes, alignment, stacking_size, expected_offset) in [
        (false, "start", 200.0, 0.0),
        (false, "center", 200.0, 30.0),
        (false, "end", 200.0, 60.0),
        (false, "safe end", 100.0, 0.0),
        (true, "start", 200.0, 0.0),
        (true, "center", 200.0, 30.0),
        (true, "end", 200.0, 60.0),
        (true, "safe end", 100.0, 0.0),
    ] {
        let (
            grid_axis_declaration,
            stacking_axis_declaration,
            content_alignment_property,
            item_size_declaration,
            oof_inset,
        ) = if row_lanes {
            (
                "grid-template-rows: 80pt",
                format!("width: {stacking_size}pt"),
                "justify-content",
                "width: 40pt; height: 80pt",
                "top: 0",
            )
        } else {
            (
                "grid-template-columns: 80pt",
                format!("height: {stacking_size}pt"),
                "align-content",
                "width: 80pt; height: 40pt",
                "left: 0",
            )
        };
        let document = Html::from_string(format!(
            "<!doctype html><style>
             @page {{ size: 400pt 400pt; margin: 0 }}
             body {{ margin: 0 }}
             #grid {{ display: grid-lanes; width: 200pt; height: 200pt;
                      {grid_axis_declaration}; {stacking_axis_declaration};
                      gap: 10pt; {content_alignment_property}: {alignment}; position: relative }}
             .item {{ {item_size_declaration}; background: rgb(0 0 255) }}
             .oof {{ position: absolute; width: 20pt; height: 20pt;
                     background: rgb(255 0 0); {oof_inset} }}
             </style>
             <div id=grid><div class=item></div><div class=item></div><div class=item></div>
             <div class=oof></div></div>"
        ))
        .render(&RenderOptions::default())
        .await
        .unwrap();

        let page = &document.pages[0];
        let item = rect_with_fill(page, CssColor::new(0, 0, 255));
        let oof = rect_with_fill(page, CssColor::new(255, 0, 0));
        let observed_offset = if row_lanes {
            (item.x() - oof.x()).abs()
        } else {
            ((item.y() + item.height()) - (oof.y() + oof.height())).abs()
        };
        assert!(
            (observed_offset - expected_offset).abs() < 0.01,
            "row_lanes={row_lanes}, alignment={alignment}, item={item:?}, oof={oof:?}"
        );
    }
}

#[tokio::test]
async fn row_grid_lanes_stretch_simple_auto_tracks() {
    for (alignment, expected_separation) in [("stretch", 150.0), ("normal", 150.0), ("start", 20.0)]
    {
        let document = Html::from_string(format!(
            "<!doctype html><style>
             @page {{ size: 400pt 400pt; margin: 0 }}
             body {{ margin: 0 }}
             #grid {{ display: grid-lanes; width: 300pt; height: 300pt;
                      grid-template-rows: repeat(2, auto); align-content: {alignment} }}
             #grid > div {{ width: 20pt; height: 20pt }}
             #first {{ background: rgb(255 0 0) }}
             #second {{ background: rgb(0 0 255) }}
             </style><div id=\"grid\"><div id=\"first\"></div><div id=\"second\"></div></div>"
        ))
        .render(&RenderOptions::default())
        .await
        .unwrap();

        let page = &document.pages[0];
        let first = rect_with_fill(page, CssColor::new(255, 0, 0));
        let second = rect_with_fill(page, CssColor::new(0, 0, 255));
        assert!(
            ((first.y() - second.y()).abs() - expected_separation).abs() < 0.01,
            "align-content: {alignment}; first={first:?}, second={second:?}"
        );
    }
}

async fn assert_mixed_grid_lanes_track_separation(
    row_lanes: bool,
    grid_axis_size: f32,
    content_alignment: &str,
    expected_separation: f32,
) {
    let (grid_dimensions, template_property, alignment_property, first_item_axis_size) =
        if row_lanes {
            (
                format!("width: 300pt; height: {grid_axis_size}pt"),
                "grid-template-rows",
                "align-content",
                "height",
            )
        } else {
            (
                format!("width: {grid_axis_size}pt; height: 300pt"),
                "grid-template-columns",
                "justify-content",
                "width",
            )
        };
    let document = Html::from_string(format!(
        "<!doctype html><style>
         @page {{ size: 500pt 500pt; margin: 0 }}
         body {{ margin: 0 }}
         #grid {{ display: grid-lanes; {grid_dimensions};
                  {template_property}: 100pt auto auto;
                  {alignment_property}: {content_alignment} }}
         #grid > div {{ width: 20pt; height: 20pt }}
         #grid > #first {{ {first_item_axis_size}: 100pt; background: rgb(255 0 0) }}
         #third {{ background: rgb(0 255 0) }}
         </style><div id=\"grid\"><div id=\"first\"></div><div></div><div id=\"third\"></div></div>"
    ))
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let first = rect_with_fill(page, CssColor::new(255, 0, 0));
    let third = rect_with_fill(page, CssColor::new(0, 255, 0));
    let separation = if row_lanes {
        ((first.y() + first.height()) - (third.y() + third.height())).abs()
    } else {
        (first.x() - third.x()).abs()
    };
    assert!(
        (separation - expected_separation).abs() < 0.01,
        "row_lanes={row_lanes}, align={content_alignment}; first={first:?}, third={third:?}"
    );
}

#[tokio::test]
async fn mixed_grid_lanes_auto_tracks_receive_automatic_item_contributions() {
    for row_lanes in [false, true] {
        assert_mixed_grid_lanes_track_separation(row_lanes, 300.0, "start", 200.0).await;
    }
}

#[tokio::test]
async fn mixed_grid_lanes_stretch_only_auto_tracks() {
    for row_lanes in [false, true] {
        assert_mixed_grid_lanes_track_separation(row_lanes, 400.0, "stretch", 250.0).await;
    }
}

async fn assert_named_grid_lanes_auto_track_start(row_lanes: bool, template: &str) {
    let (
        grid_dimensions,
        template_property,
        alignment_property,
        item_axis_size,
        placement_property,
    ) = if row_lanes {
        (
            "width: 300pt; height: 400pt",
            "grid-template-rows",
            "align-content",
            "height",
            "grid-row",
        )
    } else {
        (
            "width: 400pt; height: 300pt",
            "grid-template-columns",
            "justify-content",
            "width",
            "grid-column",
        )
    };
    let document = Html::from_string(format!(
        "<!doctype html><style>
         @page {{ size: 500pt 500pt; margin: 0 }}
         body {{ margin: 0 }}
         #grid {{ display: grid-lanes; {grid_dimensions};
                  {template_property}: {template}; {alignment_property}: stretch }}
         #grid > div {{ width: 20pt; height: 20pt }}
         #grid > #first {{ {placement_property}: first; {item_axis_size}: 120pt;
                           background: rgb(255 0 0) }}
         #grid > #second {{ {placement_property}: second; {item_axis_size}: 40pt;
                            background: rgb(0 0 255) }}
         </style><div id=\"grid\"><div id=\"first\"></div><div id=\"second\"></div></div>"
    ))
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let second = rect_with_fill(page, CssColor::new(0, 0, 255));
    let second_track_start = if row_lanes {
        500.0 - (second.y() + second.height())
    } else {
        second.x()
    };
    assert!(
        (second_track_start - 290.0).abs() < 0.01,
        "row_lanes={row_lanes}, template={template}; second={second:?}"
    );
}

#[tokio::test]
async fn named_grid_lanes_tracks_keep_explicit_contributions_local() {
    for row_lanes in [false, true] {
        assert_named_grid_lanes_auto_track_start(row_lanes, "100pt [first] auto [second] auto")
            .await;
    }
}

#[tokio::test]
async fn numbered_repeat_named_grid_lanes_tracks_size_from_named_placement() {
    assert_named_grid_lanes_auto_track_start(false, "100pt repeat(1, [first] auto [second] auto)")
        .await;
}

async fn assert_template_area_grid_lanes_auto_track_start(
    row_lanes: bool,
    template: &str,
    areas: &str,
) {
    let (
        grid_dimensions,
        template_property,
        alignment_property,
        item_axis_size,
        placement_property,
    ) = if row_lanes {
        (
            "width: 300pt; height: 400pt",
            "grid-template-rows",
            "align-content",
            "height",
            "grid-row",
        )
    } else {
        (
            "width: 400pt; height: 300pt",
            "grid-template-columns",
            "justify-content",
            "width",
            "grid-column",
        )
    };
    let document = Html::from_string(format!(
        "<!doctype html><style>
         @page {{ size: 500pt 500pt; margin: 0 }}
         body {{ margin: 0 }}
         #grid {{ display: grid-lanes; {grid_dimensions};
                  {template_property}: {template}; grid-template-areas: {areas};
                  {alignment_property}: stretch }}
         #grid > div {{ width: 20pt; height: 20pt }}
         #grid > #first {{ {placement_property}: first; {item_axis_size}: 120pt;
                           background: rgb(255 0 0) }}
         #grid > #second {{ {placement_property}: second; {item_axis_size}: 40pt;
                            background: rgb(0 0 255) }}
         </style><div id=\"grid\"><div id=\"first\"></div><div id=\"second\"></div></div>"
    ))
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let second = rect_with_fill(page, CssColor::new(0, 0, 255));
    let second_track_start = if row_lanes {
        500.0 - (second.y() + second.height())
    } else {
        second.x()
    };
    assert!(
        (second_track_start - 290.0).abs() < 0.01,
        "row_lanes={row_lanes}, template={template}, areas={areas}; second={second:?}"
    );
}

#[tokio::test]
async fn template_area_grid_lanes_tracks_keep_named_area_contributions_local() {
    assert_template_area_grid_lanes_auto_track_start(
        false,
        "100pt auto auto",
        "\". first second\"",
    )
    .await;
    assert_template_area_grid_lanes_auto_track_start(
        true,
        "100pt auto auto",
        "\".\" \"first\" \"second\"",
    )
    .await;
}

#[tokio::test]
async fn numbered_repeat_template_area_grid_lanes_tracks_size_from_named_area_placement() {
    assert_template_area_grid_lanes_auto_track_start(
        false,
        "100pt repeat(1, auto auto)",
        "\". first second\"",
    )
    .await;
}

async fn assert_area_created_grid_lanes_track_separation(
    row_lanes: bool,
    template: &str,
    areas: &str,
    auto_tracks: &str,
    expected_separation: f32,
) {
    let (
        grid_dimensions,
        template_property,
        auto_track_property,
        alignment_property,
        item_axis_size,
    ) = if row_lanes {
        (
            "width: 300pt; height: 300pt",
            "grid-template-rows",
            "grid-auto-rows",
            "align-content",
            "height",
        )
    } else {
        (
            "width: 300pt; height: 300pt",
            "grid-template-columns",
            "grid-auto-columns",
            "justify-content",
            "width",
        )
    };
    let document = Html::from_string(format!(
        "<!doctype html><style>
         @page {{ size: 400pt 400pt; margin: 0 }}
         body {{ margin: 0 }}
         #grid {{ display: grid-lanes; {grid_dimensions};
                  {template_property}: {template}; grid-template-areas: {areas};
                  {auto_track_property}: {auto_tracks}; {alignment_property}: start }}
         #grid > div {{ width: 20pt; height: 20pt }}
         #grid > #first {{ {item_axis_size}: 100pt; background: rgb(255 0 0) }}
         #third {{ background: rgb(0 255 0) }}
         </style><div id=\"grid\"><div id=\"first\"></div><div></div><div id=\"third\"></div></div>"
    ))
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let first = rect_with_fill(page, CssColor::new(255, 0, 0));
    let third = rect_with_fill(page, CssColor::new(0, 255, 0));
    let separation = if row_lanes {
        ((first.y() + first.height()) - (third.y() + third.height())).abs()
    } else {
        (first.x() - third.x()).abs()
    };
    assert!(
        (separation - expected_separation).abs() < 0.01,
        "row_lanes={row_lanes}, template={template}, areas={areas}, \
         auto_tracks={auto_tracks}; first={first:?}, third={third:?}"
    );
}

#[tokio::test]
async fn area_created_grid_lanes_auto_tracks_receive_automatic_item_contributions() {
    for (row_lanes, areas) in [(false, "\". . .\""), (true, "\".\" \".\" \".\"")] {
        assert_area_created_grid_lanes_track_separation(row_lanes, "100pt", areas, "auto", 200.0)
            .await;
    }
}

#[tokio::test]
async fn area_only_column_grid_lanes_tracks_receive_automatic_item_contributions() {
    assert_area_created_grid_lanes_track_separation(false, "none", "\". . .\"", "auto", 200.0)
        .await;
}

#[tokio::test]
async fn area_created_grid_lanes_tracks_cycle_fixed_and_auto_grid_auto_tracks() {
    assert_area_created_grid_lanes_track_separation(false, "50pt", "\". . .\"", "auto 50pt", 150.0)
        .await;
}

#[tokio::test]
async fn area_created_grid_lanes_stretch_only_auto_grid_auto_tracks() {
    let document = Html::from_string(
        "<!doctype html><style>
         @page { size: 500pt 500pt; margin: 0 }
         body { margin: 0 }
         #grid { display: grid-lanes; width: 400pt; height: 300pt;
                 grid-template-columns: 50pt; grid-template-areas: \". . .\";
                 grid-auto-columns: auto 50pt; justify-content: stretch }
         #grid > div { width: 20pt; height: 20pt }
         #first { width: 100pt; background: rgb(255 0 0) }
         #third { background: rgb(0 255 0) }
         </style><div id=\"grid\"><div id=\"first\"></div><div></div><div id=\"third\"></div></div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let third = rect_with_fill(&document.pages[0], CssColor::new(0, 255, 0));
    assert!((third.x() - 350.0).abs() < 0.01, "third={third:?}");
}