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
use super::*;
use crate::units::IntoLayoutLength;

/// A replaced flex item's physical content-box dimensions before aspect-ratio
/// constraint resolution. Keeping the axes together prevents a width value
/// being re-used as the height half of a later constraint pass.
#[derive(Debug, Clone, Copy)]
struct FlexReplacedContentSize {
    width: ContentBoxLength,
    height: ContentBoxLength,
}

#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
    use super::*;

    #[test]
    fn replaced_flex_estimate_resolves_percentage_max_height_from_block_basis() {
        let mut style = ComputedStyle::initial();
        style.box_values.max_height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
            css::ComputedLengthPercentage::from_percent(1.0),
        );
        let available = FlexItemAvailableSpace {
            width: PhysicalContentWidth::new(content_box_pt(200.0)),
            width_basis: PercentageBasis::definite_from(
                content_box_pt(200.0),
                FlexAvailableSizeSource::ContainingBlock,
            ),
            height: Some(PhysicalContentHeight::new(content_box_pt(100.0))),
            height_basis: PercentageBasis::definite_from(
                content_box_pt(100.0),
                FlexAvailableSizeSource::ContainingBlock,
            ),
            stretched_width: None,
            stretched_height: None,
        };
        let estimate = estimate_replaced_flex_item(
            IntrinsicReplacedSize {
                width: content_box_pt(200.0),
                height: content_box_pt(200.0),
                preferred_aspect_ratio: Some(1.0),
                has_intrinsic_size: true,
                attr_width: None,
                attr_height: None,
            },
            &style,
            false,
            PhysicalContentWidth::new(content_box_pt(200.0)),
            available,
        )
        .expect("a square intrinsic image is estimable");

        assert_eq!(estimate.width.points(), 100.0);
        assert_eq!(estimate.height.points(), 100.0);
    }

    #[test]
    fn ratio_only_svg_keeps_default_object_size_separate_from_flex_stretch() {
        let style = ComputedStyle::initial();
        let available = FlexItemAvailableSpace {
            width: PhysicalContentWidth::new(content_box_pt(600.0)),
            width_basis: PercentageBasis::definite_from(
                content_box_pt(600.0),
                FlexAvailableSizeSource::ContainingBlock,
            ),
            height: None,
            height_basis: PercentageBasis::indefinite(),
            stretched_width: Some(PhysicalContentWidth::new(content_box_pt(600.0))),
            stretched_height: None,
        };
        let estimate = estimate_replaced_flex_item(
            IntrinsicReplacedSize {
                width: content_box_pt(225.0),
                height: content_box_pt(112.5),
                preferred_aspect_ratio: Some(2.0),
                has_intrinsic_size: false,
                attr_width: None,
                attr_height: None,
            },
            &style,
            false,
            PhysicalContentWidth::new(content_box_pt(600.0)),
            available,
        )
        .expect("a ratio-only SVG is estimable");

        assert_eq!(estimate.width.points(), 225.0);
        assert_eq!(estimate.height.points(), 112.5);
        let automatic = estimate
            .automatic_preferred_physical_size
            .expect("the CSS default object size is retained");
        assert_eq!(automatic.width.points(), 225.0);
        assert_eq!(automatic.height.points(), 112.5);
    }
}

impl FlexReplacedContentSize {
    fn new(width: ContentBoxLength, height: ContentBoxLength) -> Self {
        Self { width, height }
    }

    fn zero() -> Self {
        Self::new(content_box_pt(0.0), content_box_pt(0.0))
    }

    fn constrain_with_aspect_ratio(
        &mut self,
        aspect_ratio: f32,
        preferred_sizes: ReplacedPreferredSizeAxes,
        constraints: ReplacedSizeConstraints,
    ) {
        let constrained = resolve_replaced_size_with_aspect_ratio(
            content_box_size_pt(self.width.points(), self.height.points()),
            aspect_ratio,
            preferred_sizes,
            constraints,
        );
        *self = Self::new(
            content_box_pt(constrained.width),
            content_box_pt(constrained.height),
        );
    }

    fn width_at_ratio(width: ContentBoxLength, ratio: f32) -> Self {
        Self::new(width, content_box_pt(width.points() / ratio))
    }

    fn height_at_ratio(height: ContentBoxLength, ratio: f32) -> Self {
        Self::new(content_box_pt(height.points() * ratio), height)
    }
}

/// Estimates a replaced flex item without letting main-size constraints alter flex basis.
///
/// CSS Flexbox computes the flex base size from the item's used flex-basis
/// while ignoring min/max main-size constraints, but the hypothetical size and
/// cross-size contribution still reflect replaced-element aspect-ratio sizing.
/// For replaced elements with an intrinsic ratio, cross-axis min/max constraints
/// transfer through the ratio into the content-basis candidate used by
/// `flex-basis:auto`:
/// <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>,
/// <https://www.w3.org/TR/css-flexbox-1/#algo-cross-item>, and
/// <https://www.w3.org/TR/css-sizing-3/#aspect-ratio>.
pub(in crate::layout::flex) fn estimate_replaced_flex_item(
    intrinsic: IntrinsicReplacedSize,
    style: &ComputedStyle,
    has_size_containment: bool,
    containing_width: PhysicalContentWidth,
    available: FlexItemAvailableSpace,
) -> Option<FlexItemEstimate> {
    let attribute_aspect_ratio = intrinsic.attribute_aspect_ratio();
    let aspect_ratio = style.aspect_ratio.preferred_ratio(
        true,
        if has_size_containment {
            attribute_aspect_ratio
        } else {
            intrinsic.natural_aspect_ratio()
        },
    );
    let borders = used_border_widths(style);
    let horizontal_non_content =
        borders.left + borders.right + style.padding.left + style.padding.right;
    let vertical_non_content =
        borders.top + borders.bottom + style.padding.top + style.padding.bottom;
    // A flex item's percentage block-size constraints resolve against the
    // containing block's block-size basis, which is independent from its
    // physical inline measurement width. In particular, a row flex item can
    // have a definite stretched height while its width is larger.
    // <https://www.w3.org/TR/css-sizing-3/#percentage-sizing>
    // <https://www.w3.org/TR/css-flexbox-1/#definite-sizes>
    let block_constraints = ReplacedSizeConstraints {
        min_width: used_min_width(
            style,
            PercentageBasis::definite(containing_width.content_box_length()),
        )
        .map(|width| width.max(content_box_pt(0.0))),
        max_width: used_max_width(
            style,
            PercentageBasis::definite(containing_width.content_box_length()),
        )
        .map(|width| width.max(content_box_pt(0.0))),
        min_height: used_length_percentage_or_auto_with_basis(
            style.box_values.min_height.clone(),
            available.height_basis,
        )
        .map(|height| content_box_pt(height.points().max(0.0))),
        max_height: used_length_percentage_or_auto_with_basis(
            style.box_values.max_height.clone(),
            available.height_basis,
        )
        .map(|height| content_box_pt(height.points().max(0.0))),
    };
    // A viewBox-only SVG has a preferred ratio but no intrinsic dimensions.
    // Its automatic preferred size remains CSS Images' default object size;
    // flex stretch must not masquerade as an authored definite width or
    // height while establishing that size.
    let ratio_only_automatic_size = !intrinsic.has_intrinsic_size
        && aspect_ratio.is_some()
        && intrinsic.attr_width.is_none()
        && intrinsic.attr_height.is_none()
        && style.box_values.width.is_auto()
        && style.box_values.height.is_auto();
    let specified_width = used_content_box_width_or_auto(
        style,
        containing_width.content_box_length().into_layout_length(),
        non_content_pt(horizontal_non_content),
    )
    .or(intrinsic.attr_width)
    .or_else(|| {
        (!ratio_only_automatic_size)
            .then(|| {
                available
                    .stretched_width
                    .map(|width| content_box_pt((width.points() - horizontal_non_content).max(0.0)))
            })
            .flatten()
    });
    let specified_height = used_content_box_height_or_auto_with_basis(
        style,
        available.height_basis,
        non_content_pt(vertical_non_content),
    )
    .or(intrinsic.attr_height)
    .or_else(|| {
        (!ratio_only_automatic_size)
            .then(|| {
                available
                    .stretched_height
                    .map(|height| content_box_pt((height.points() - vertical_non_content).max(0.0)))
            })
            .flatten()
    });
    let width_is_auto = specified_width.is_none();
    let height_is_auto = specified_height.is_none();
    let contained_intrinsic_width = style
        .contain
        .size
        .then(|| {
            style.contain_intrinsic_size.width.clone().map(|width| {
                used_length_percentage(
                    width,
                    PercentageBasis::definite(containing_width.content_box_length()),
                )
                .cast_unit()
            })
        })
        .flatten()
        .unwrap_or_else(|| content_box_pt(0.0));
    let contained_intrinsic_height = style
        .contain
        .size
        .then(|| {
            style.contain_intrinsic_size.height.clone().map(|height| {
                used_length_percentage(
                    height,
                    PercentageBasis::definite(containing_width.content_box_length()),
                )
                .cast_unit()
            })
        })
        .flatten()
        .unwrap_or_else(|| content_box_pt(0.0));
    let base_size = match (specified_width, specified_height, aspect_ratio) {
        (Some(width), None, Some(ratio)) => FlexReplacedContentSize::width_at_ratio(width, ratio),
        (None, Some(height), Some(ratio)) => {
            FlexReplacedContentSize::height_at_ratio(height, ratio)
        }
        // An SVG image with only a `viewBox` provides a preferred aspect
        // ratio but no intrinsic dimensions. CSS Images still supplies its
        // default object size as the automatic preferred physical size.
        // Preserve that size rather than replacing it with the flex
        // container's available cross space; the automatic-minimum phase
        // later transfers the applicable physical axis through the ratio.
        // <https://www.w3.org/TR/css-sizing-3/#intrinsic-sizes>
        // <https://www.w3.org/TR/css-flexbox-1/#algo-main-item>
        (None, None, Some(ratio)) if !intrinsic.has_intrinsic_size => {
            let width = intrinsic.width.max(content_box_pt(0.0));
            let height = intrinsic.height.max(content_box_pt(0.0));
            if width > content_box_pt(0.0) {
                FlexReplacedContentSize::width_at_ratio(width, ratio)
            } else {
                FlexReplacedContentSize::height_at_ratio(height, ratio)
            }
        }
        // Size containment's fallback is an intrinsic size, not a preferred
        // aspect ratio.  When an authored dimension leaves the other axis
        // auto, retain that axis's fallback intrinsic dimension rather than
        // scaling it from the specified one.
        // <https://drafts.csswg.org/css-sizing-4/#intrinsic-size-override>
        (Some(width), None, None) if has_size_containment => {
            FlexReplacedContentSize::new(width, contained_intrinsic_height)
        }
        (None, Some(height), None) if has_size_containment => {
            FlexReplacedContentSize::new(contained_intrinsic_width, height)
        }
        (Some(width), None, None) => FlexReplacedContentSize::new(width, content_box_pt(0.0)),
        (None, Some(height), None) => FlexReplacedContentSize::new(content_box_pt(0.0), height),
        (None, None, _) if has_size_containment => {
            FlexReplacedContentSize::new(contained_intrinsic_width, contained_intrinsic_height)
        }
        (None, None, _) => FlexReplacedContentSize::new(intrinsic.width, intrinsic.height),
        (Some(width), Some(height), _) => FlexReplacedContentSize::new(width, height),
    };
    let Some(aspect_ratio) = aspect_ratio else {
        let width = constrain_content_width(
            style,
            base_size.width,
            PercentageBasis::definite(content_box_pt(containing_width.points().max(1.0))),
        );
        let height = super::sizing::constrain_flex_item_estimated_height(
            style,
            base_size.height,
            base_size.height,
            base_size.height,
            available.height_basis,
            non_content_pt(vertical_non_content),
        )
        .max(content_box_pt(1.0));
        return Some(FlexItemEstimate::from_physical_intrinsic_metrics(
            FlexPhysicalIntrinsicMetrics {
                width: PhysicalContentWidth::new(width),
                height: PhysicalContentHeight::new(height),
                min_width: PhysicalContentWidth::new(width),
                min_height: PhysicalContentHeight::new(height),
                content_width: PhysicalContentWidth::new(width),
                content_height: PhysicalContentHeight::new(height),
            },
            None,
            FlexItemBaselineEstimate::default(),
        ));
    };
    let mut constrained_size = base_size;
    constrained_size.constrain_with_aspect_ratio(
        aspect_ratio,
        ReplacedPreferredSizeAxes {
            width: ReplacedPreferredSize::from_is_automatic(width_is_auto),
            height: ReplacedPreferredSize::from_is_automatic(height_is_auto),
        },
        block_constraints,
    );

    let mut width_constrained_size = base_size;
    width_constrained_size.constrain_with_aspect_ratio(
        aspect_ratio,
        ReplacedPreferredSizeAxes {
            width: ReplacedPreferredSize::from_is_automatic(width_is_auto),
            height: ReplacedPreferredSize::from_is_automatic(height_is_auto),
        },
        ReplacedSizeConstraints {
            min_width: used_min_width(
                style,
                PercentageBasis::definite(containing_width.content_box_length()),
            )
            .map(|width| width.max(content_box_pt(0.0))),
            max_width: used_max_width(
                style,
                PercentageBasis::definite(containing_width.content_box_length()),
            )
            .map(|width| width.max(content_box_pt(0.0))),
            min_height: None,
            max_height: None,
        },
    );

    let mut height_constrained_size = base_size;
    height_constrained_size.constrain_with_aspect_ratio(
        aspect_ratio,
        ReplacedPreferredSizeAxes {
            width: ReplacedPreferredSize::from_is_automatic(width_is_auto),
            height: ReplacedPreferredSize::from_is_automatic(height_is_auto),
        },
        ReplacedSizeConstraints {
            min_width: None,
            max_width: None,
            min_height: block_constraints.min_height,
            max_height: block_constraints.max_height,
        },
    );

    // An SVG with only a preferred ratio has no intrinsic content-size
    // suggestion for Flexbox's automatic minimum. It still uses the default
    // object size to establish an auto flex base size, but treating that
    // fallback as min-content would prevent normal flex shrinking and make a
    // ratio-only SVG overflow a definite flex container.
    // <https://www.w3.org/TR/css-flexbox-1/#min-size-auto>
    // <https://www.w3.org/TR/css-images-3/#default-sizing>
    let min_size = if intrinsic.has_intrinsic_size {
        FlexReplacedContentSize::new(
            constrained_size.width.max(content_box_pt(1.0)),
            constrained_size.height.max(content_box_pt(1.0)),
        )
    } else {
        FlexReplacedContentSize::zero()
    };
    let mut estimate = FlexItemEstimate::from_physical_intrinsic_metrics(
        FlexPhysicalIntrinsicMetrics {
            width: PhysicalContentWidth::new(constrained_size.width.max(content_box_pt(1.0))),
            height: PhysicalContentHeight::new(constrained_size.height.max(content_box_pt(1.0))),
            min_width: PhysicalContentWidth::new(min_size.width),
            min_height: PhysicalContentHeight::new(min_size.height),
            content_width: PhysicalContentWidth::new(
                height_constrained_size.width.max(content_box_pt(1.0)),
            ),
            content_height: PhysicalContentHeight::new(
                width_constrained_size.height.max(content_box_pt(1.0)),
            ),
        },
        Some(aspect_ratio),
        FlexItemBaselineEstimate::default(),
    );
    if ratio_only_automatic_size {
        estimate.set_automatic_preferred_physical_size(FlexAutomaticPreferredPhysicalSize {
            width: PhysicalContentWidth::new(base_size.width.max(content_box_pt(0.0))),
            height: PhysicalContentHeight::new(base_size.height.max(content_box_pt(0.0))),
        });
    }
    Some(estimate)
}