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
//! CSS Logical 1 ยง4: each flow-relative box property pairs with the physical property its
//! element's own writing mode and direction map it to, and the pair shares one computed value.
use crate::style::{Style, grammar::Property};
use std::sync::Arc;

pub(super) const TOP: usize = 0;
pub(super) const RIGHT: usize = 1;
pub(super) const BOTTOM: usize = 2;
pub(super) const LEFT: usize = 3;
/// Physical corners in `border-radius` order, as the two sides meeting there.
const CORNER_SIDES: [[usize; 2]; 4] = [[TOP, LEFT], [TOP, RIGHT], [BOTTOM, RIGHT], [BOTTOM, LEFT]];
/// Physical dimensions: `width`/`overflow-x`, then `height`/`overflow-y`.
const HORIZONTAL: usize = 0;
const VERTICAL: usize = 1;

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum WritingMode {
    #[default]
    HorizontalTb,
}

/// How flow-relative terms map to physical ones for one box (CSS Writing Modes 4 ยง6).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Flow {
    pub(crate) writing_mode: WritingMode,
    pub(crate) rtl: bool,
}

/// An element's flow and its parent's, whose corresponding properties it inherits.
#[derive(Clone, Copy)]
pub(crate) struct Flows {
    pub(crate) element: Flow,
    pub(crate) parent: Flow,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Axis {
    Block,
    Inline,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Edge {
    Start,
    End,
}

#[derive(Clone, Copy)]
enum Position {
    Side(Axis, Edge),
    /// The block-axis edge, then the inline-axis edge (CSS Logical 1 ยง4.6).
    Corner(Edge, Edge),
    Dimension(Axis),
}

const SIDES: [Position; 4] = [
    Position::Side(Axis::Block, Edge::Start),
    Position::Side(Axis::Block, Edge::End),
    Position::Side(Axis::Inline, Edge::Start),
    Position::Side(Axis::Inline, Edge::End),
];
const CORNERS: [Position; 4] = [
    Position::Corner(Edge::Start, Edge::Start),
    Position::Corner(Edge::Start, Edge::End),
    Position::Corner(Edge::End, Edge::Start),
    Position::Corner(Edge::End, Edge::End),
];
const DIMENSIONS: [Position; 2] = [
    Position::Dimension(Axis::Block),
    Position::Dimension(Axis::Inline),
];

/// A logical property group: physical members by physical index, and flow-relative members
/// at the positions of the matching `positions` list.
struct Group {
    physical: &'static [Property],
    flow_relative: &'static [Property],
    positions: &'static [Position],
}

const GROUPS: [Group; 16] = {
    use Property as P;
    [
        Group {
            physical: &[P::MarginTop, P::MarginRight, P::MarginBottom, P::MarginLeft],
            flow_relative: &[
                P::MarginBlockStart,
                P::MarginBlockEnd,
                P::MarginInlineStart,
                P::MarginInlineEnd,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::PaddingTop,
                P::PaddingRight,
                P::PaddingBottom,
                P::PaddingLeft,
            ],
            flow_relative: &[
                P::PaddingBlockStart,
                P::PaddingBlockEnd,
                P::PaddingInlineStart,
                P::PaddingInlineEnd,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[P::Top, P::Right, P::Bottom, P::Left],
            flow_relative: &[
                P::InsetBlockStart,
                P::InsetBlockEnd,
                P::InsetInlineStart,
                P::InsetInlineEnd,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::BorderTopWidth,
                P::BorderRightWidth,
                P::BorderBottomWidth,
                P::BorderLeftWidth,
            ],
            flow_relative: &[
                P::BorderBlockStartWidth,
                P::BorderBlockEndWidth,
                P::BorderInlineStartWidth,
                P::BorderInlineEndWidth,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::BorderTopStyle,
                P::BorderRightStyle,
                P::BorderBottomStyle,
                P::BorderLeftStyle,
            ],
            flow_relative: &[
                P::BorderBlockStartStyle,
                P::BorderBlockEndStyle,
                P::BorderInlineStartStyle,
                P::BorderInlineEndStyle,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::BorderTopColor,
                P::BorderRightColor,
                P::BorderBottomColor,
                P::BorderLeftColor,
            ],
            flow_relative: &[
                P::BorderBlockStartColor,
                P::BorderBlockEndColor,
                P::BorderInlineStartColor,
                P::BorderInlineEndColor,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::BorderTopLeftRadius,
                P::BorderTopRightRadius,
                P::BorderBottomRightRadius,
                P::BorderBottomLeftRadius,
            ],
            flow_relative: &[
                P::BorderStartStartRadius,
                P::BorderStartEndRadius,
                P::BorderEndStartRadius,
                P::BorderEndEndRadius,
            ],
            positions: &CORNERS,
        },
        Group {
            physical: &[P::Width, P::Height],
            flow_relative: &[P::BlockSize, P::InlineSize],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[P::MinWidth, P::MinHeight],
            flow_relative: &[P::MinBlockSize, P::MinInlineSize],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[P::MaxWidth, P::MaxHeight],
            flow_relative: &[P::MaxBlockSize, P::MaxInlineSize],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[P::OverflowX, P::OverflowY],
            flow_relative: &[P::OverflowBlock, P::OverflowInline],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[P::ContainIntrinsicWidth, P::ContainIntrinsicHeight],
            flow_relative: &[P::ContainIntrinsicBlockSize, P::ContainIntrinsicInlineSize],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[
                P::ScrollMarginTop,
                P::ScrollMarginRight,
                P::ScrollMarginBottom,
                P::ScrollMarginLeft,
            ],
            flow_relative: &[
                P::ScrollMarginBlockStart,
                P::ScrollMarginBlockEnd,
                P::ScrollMarginInlineStart,
                P::ScrollMarginInlineEnd,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[
                P::ScrollPaddingTop,
                P::ScrollPaddingRight,
                P::ScrollPaddingBottom,
                P::ScrollPaddingLeft,
            ],
            flow_relative: &[
                P::ScrollPaddingBlockStart,
                P::ScrollPaddingBlockEnd,
                P::ScrollPaddingInlineStart,
                P::ScrollPaddingInlineEnd,
            ],
            positions: &SIDES,
        },
        Group {
            physical: &[P::OverscrollBehaviorX, P::OverscrollBehaviorY],
            flow_relative: &[P::OverscrollBehaviorBlock, P::OverscrollBehaviorInline],
            positions: &DIMENSIONS,
        },
        Group {
            physical: &[
                P::OverflowClipMarginTop,
                P::OverflowClipMarginRight,
                P::OverflowClipMarginBottom,
                P::OverflowClipMarginLeft,
            ],
            flow_relative: &[
                P::OverflowClipMarginBlockStart,
                P::OverflowClipMarginBlockEnd,
                P::OverflowClipMarginInlineStart,
                P::OverflowClipMarginInlineEnd,
            ],
            positions: &SIDES,
        },
    ]
};
/// The groups of properties without print effect, whose values live in `Style::inert`.
const INERT_GROUPS: std::ops::Range<usize> = 12..15;

impl Flow {
    pub(crate) fn of(style: &Style) -> Self {
        Self {
            // The registry computes no `writing-mode` but its initial `horizontal-tb`.
            writing_mode: WritingMode::HorizontalTb,
            rtl: style.rtl,
        }
    }

    fn side(self, axis: Axis, edge: Edge) -> usize {
        match (self.writing_mode, axis) {
            (WritingMode::HorizontalTb, Axis::Block) => match edge {
                Edge::Start => TOP,
                Edge::End => BOTTOM,
            },
            (WritingMode::HorizontalTb, Axis::Inline) => {
                if (edge == Edge::Start) == self.rtl {
                    RIGHT
                } else {
                    LEFT
                }
            }
        }
    }

    fn dimension(self, axis: Axis) -> usize {
        match (self.writing_mode, axis) {
            (WritingMode::HorizontalTb, Axis::Inline) => HORIZONTAL,
            (WritingMode::HorizontalTb, Axis::Block) => VERTICAL,
        }
    }

    /// The physical index of `position` within its group.
    fn physical(self, position: Position) -> usize {
        match position {
            Position::Side(axis, edge) => self.side(axis, edge),
            Position::Corner(block, inline) => {
                let sides = [
                    self.side(Axis::Block, block),
                    self.side(Axis::Inline, inline),
                ];
                CORNER_SIDES
                    .iter()
                    .position(|corner| sides.iter().all(|side| corner.contains(side)))
                    .expect("a block-axis and an inline-axis side meet at a corner")
            }
            Position::Dimension(axis) => self.dimension(axis),
        }
    }

    /// The index in `from`'s flow holding what physical index `index` holds in this flow.
    fn source(self, from: Self, positions: &[Position], index: usize) -> usize {
        let position = positions
            .iter()
            .find(|position| self.physical(**position) == index)
            .expect("every physical index has a flow-relative position");
        from.physical(*position)
    }
}

fn group(property: Property) -> Option<&'static Group> {
    GROUPS
        .iter()
        .find(|group| group.physical.contains(&property) || group.flow_relative.contains(&property))
}

/// The physical properties a flow-relative property can share its computed value with.
pub(crate) fn physical_properties(property: Property) -> Option<&'static [Property]> {
    group(property)
        .filter(|group| group.flow_relative.contains(&property))
        .map(|group| group.physical)
}

pub(crate) fn is_flow_relative(property: Property) -> bool {
    physical_properties(property).is_some()
}

/// The flow-relative members of a physical property's group.
pub(crate) fn flow_relative_properties(property: Property) -> &'static [Property] {
    group(property)
        .filter(|group| group.physical.contains(&property))
        .map_or(&[], |group| group.flow_relative)
}

/// The physical property `property` maps to in `flow`; physical properties map to themselves.
pub(crate) fn physical(property: Property, flow: Flow) -> Property {
    let Some(group) = group(property) else {
        return property;
    };
    group
        .flow_relative
        .iter()
        .position(|candidate| *candidate == property)
        .map_or(property, |index| {
            group.physical[flow.physical(group.positions[index])]
        })
}

/// The flow-relative property sharing a physical property's computed value in `flow`.
pub(crate) fn counterpart(property: Property, flow: Flow) -> Option<Property> {
    let group = group(property)?;
    let index = group
        .physical
        .iter()
        .position(|candidate| *candidate == property)?;
    group
        .positions
        .iter()
        .position(|position| flow.physical(*position) == index)
        .map(|position| group.flow_relative[position])
}

/// The physical property holding, in flow `to`, what `property` holds in flow `from`.
pub(crate) fn corresponding(property: Property, from: Flow, to: Flow) -> Property {
    counterpart(property, from).map_or(property, |logical| physical(logical, to))
}

/// `source` with its box properties arranged for `flow`: each physical property holds the
/// value of the property corresponding to it in `source`'s own flow.
pub(crate) fn reflowed(source: &Style, flow: Flow) -> Style {
    let from = Flow::of(source);
    let side = |index| flow.source(from, &SIDES, index);
    let corner = |index| flow.source(from, &CORNERS, index);
    let dimension = |index| flow.source(from, &DIMENSIONS, index);
    let dimensions = |values: [&crate::style::Size; 2]| -> [crate::style::Size; 2] {
        std::array::from_fn(|index| values[dimension(index)].clone())
    };
    let mut style = source.clone();
    style.margin = std::array::from_fn(|index| source.margin[side(index)].clone());
    style.padding = std::array::from_fn(|index| source.padding[side(index)].clone());
    style.inset = std::array::from_fn(|index| source.inset[side(index)].clone());
    style.decoration.borders = std::array::from_fn(|index| source.decoration.borders[side(index)]);
    style.decoration.radii = Arc::new(std::array::from_fn(|index| {
        source.decoration.radii[corner(index)].clone()
    }));
    [style.width, style.height] = dimensions([&source.width, &source.height]);
    [style.min_width, style.min_height] = dimensions([&source.min_width, &source.min_height]);
    [style.max_width, style.max_height] = dimensions([&source.max_width, &source.max_height]);
    style.overflow = std::array::from_fn(|index| source.overflow[dimension(index)]);
    style.overflow_clip_margin =
        std::array::from_fn(|index| source.overflow_clip_margin[side(index)]);
    for group in &GROUPS[INERT_GROUPS] {
        for (index, property) in group.physical.iter().enumerate() {
            let from = group.physical[flow.source(from, group.positions, index)];
            crate::style::inert::copy_from(*property, &mut style, from, source);
        }
    }
    style
}