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
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
use super::{Direction, WritingMode};

/// A CSS logical dimension.
///
/// Logical axes are resolved against a box's writing mode before they are
/// projected onto the physical page coordinate system:
/// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LogicalAxis {
    Inline,
    Block,
}

/// A flow-relative edge of a CSS box.
///
/// CSS Logical Properties uses these edges for logical box, inset, border,
/// and corner-radius longhands:
/// <https://www.w3.org/TR/css-logical-1/#box>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LogicalSide {
    InlineStart,
    InlineEnd,
    BlockStart,
    BlockEnd,
}

/// A physical side of a rectangular CSS box.
///
/// CSS Writing Modes maps logical block/inline start and end sides to these
/// physical sides from the box's computed `writing-mode` and `direction`:
/// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PhysicalSide {
    Top,
    Right,
    Bottom,
    Left,
}

impl PhysicalSide {
    pub(crate) const fn axis(self) -> PhysicalAxis {
        match self {
            Self::Top | Self::Bottom => PhysicalAxis::Vertical,
            Self::Right | Self::Left => PhysicalAxis::Horizontal,
        }
    }

    pub(crate) const fn is_start_edge(self) -> bool {
        matches!(self, Self::Top | Self::Left)
    }

    pub(crate) const fn is_end_edge(self) -> bool {
        matches!(self, Self::Right | Self::Bottom)
    }

    pub(crate) const fn opposite(self) -> Self {
        match self {
            Self::Top => Self::Bottom,
            Self::Right => Self::Left,
            Self::Bottom => Self::Top,
            Self::Left => Self::Right,
        }
    }
}

/// A physical coordinate axis in page space.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PhysicalAxis {
    Horizontal,
    Vertical,
}

/// The complete logical-to-physical axis mapping for one computed flow.
///
/// This is the sole authority for mapping CSS logical box axes and sides from
/// `writing-mode` and `direction` into physical page geometry. Layout code
/// should carry this value to its physical-geometry boundary instead of
/// independently matching a writing mode:
/// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct WritingModeAxes {
    writing_mode: WritingMode,
    direction: Direction,
}

impl WritingModeAxes {
    pub(crate) const fn new(writing_mode: WritingMode, direction: Direction) -> Self {
        Self {
            writing_mode,
            direction,
        }
    }

    pub(crate) const fn writing_mode(self) -> WritingMode {
        self.writing_mode
    }

    pub(crate) const fn direction(self) -> Direction {
        self.direction
    }

    /// Resolve a logical box side into its physical page edge.
    pub(crate) const fn physical_side(self, side: LogicalSide) -> PhysicalSide {
        match side {
            LogicalSide::BlockStart => match self.writing_mode {
                WritingMode::HorizontalTb => PhysicalSide::Top,
                WritingMode::VerticalRl | WritingMode::SidewaysRl => PhysicalSide::Right,
                WritingMode::VerticalLr | WritingMode::SidewaysLr => PhysicalSide::Left,
            },
            LogicalSide::BlockEnd => match self.writing_mode {
                WritingMode::HorizontalTb => PhysicalSide::Bottom,
                WritingMode::VerticalRl | WritingMode::SidewaysRl => PhysicalSide::Left,
                WritingMode::VerticalLr | WritingMode::SidewaysLr => PhysicalSide::Right,
            },
            LogicalSide::InlineStart => match (self.writing_mode, self.direction) {
                (WritingMode::HorizontalTb, Direction::Ltr) => PhysicalSide::Left,
                (WritingMode::HorizontalTb, Direction::Rtl) => PhysicalSide::Right,
                (WritingMode::SidewaysLr, Direction::Ltr) => PhysicalSide::Bottom,
                (WritingMode::SidewaysLr, Direction::Rtl) => PhysicalSide::Top,
                (_, Direction::Ltr) => PhysicalSide::Top,
                (_, Direction::Rtl) => PhysicalSide::Bottom,
            },
            LogicalSide::InlineEnd => self.physical_side(LogicalSide::InlineStart).opposite(),
        }
    }

    /// Resolve the physical axis occupied by a logical axis.
    pub(crate) const fn physical_axis(self, axis: LogicalAxis) -> PhysicalAxis {
        let side = match axis {
            LogicalAxis::Inline => LogicalSide::InlineStart,
            LogicalAxis::Block => LogicalSide::BlockStart,
        };
        self.physical_side(side).axis()
    }

    /// Resolve the start side of a logical axis.
    pub(crate) const fn physical_start_side(self, axis: LogicalAxis) -> PhysicalSide {
        let side = match axis {
            LogicalAxis::Inline => LogicalSide::InlineStart,
            LogicalAxis::Block => LogicalSide::BlockStart,
        };
        self.physical_side(side)
    }

    /// Resolve the line-relative left edge of an inline formatting context.
    ///
    /// This mapping is independent of `direction`: `direction` chooses
    /// whether inline content progresses from line-left or line-right, while
    /// the writing mode determines which physical edge represents each
    /// line-relative side.
    /// <https://drafts.csswg.org/css-writing-modes-4/#line-directions>
    pub(crate) const fn line_left_side(self) -> PhysicalSide {
        match self.writing_mode {
            WritingMode::HorizontalTb => PhysicalSide::Left,
            WritingMode::VerticalRl | WritingMode::VerticalLr | WritingMode::SidewaysRl => {
                PhysicalSide::Top
            }
            WritingMode::SidewaysLr => PhysicalSide::Bottom,
        }
    }

    /// Resolve the line-relative right edge of an inline formatting context.
    /// <https://drafts.csswg.org/css-writing-modes-4/#line-directions>
    pub(crate) const fn line_right_side(self) -> PhysicalSide {
        self.line_left_side().opposite()
    }

    /// Resolve the line-relative over edge.
    /// <https://drafts.csswg.org/css-writing-modes-4/#line-directions>
    pub(crate) const fn line_over_side(self) -> PhysicalSide {
        match self.writing_mode {
            WritingMode::HorizontalTb => PhysicalSide::Top,
            WritingMode::VerticalRl | WritingMode::VerticalLr | WritingMode::SidewaysRl => {
                PhysicalSide::Right
            }
            WritingMode::SidewaysLr => PhysicalSide::Left,
        }
    }

    /// Resolve the line-relative under edge.
    /// <https://drafts.csswg.org/css-writing-modes-4/#line-directions>
    pub(crate) const fn line_under_side(self) -> PhysicalSide {
        self.line_over_side().opposite()
    }

    /// Return the logical axis occupying a physical page axis.
    pub(crate) fn logical_axis_for_physical(self, axis: PhysicalAxis) -> LogicalAxis {
        if self.physical_axis(LogicalAxis::Inline) == axis {
            LogicalAxis::Inline
        } else {
            LogicalAxis::Block
        }
    }

    /// Whether positive logical offsets run toward a physical end edge.
    pub(crate) const fn is_reversed(self, axis: LogicalAxis) -> bool {
        self.physical_start_side(axis).is_end_edge()
    }

    /// Whether logical inline/block dimensions project to physical height/width.
    pub(crate) const fn swaps_physical_axes(self) -> bool {
        matches!(
            self.physical_axis(LogicalAxis::Inline),
            PhysicalAxis::Vertical
        )
    }

    /// Project an inline/block pair into physical width/height order.
    pub(crate) fn physical_size<T>(self, inline: T, block: T) -> (T, T) {
        if self.swaps_physical_axes() {
            (block, inline)
        } else {
            (inline, block)
        }
    }
}

/// Returns the physical side corresponding to logical block-start.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#block-flow>.
pub(crate) fn block_start_side(writing_mode: WritingMode) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, Direction::Ltr).physical_side(LogicalSide::BlockStart)
}

/// Returns the physical side corresponding to logical block-end.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#block-flow>.
pub(crate) fn block_end_side(writing_mode: WritingMode) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, Direction::Ltr).physical_side(LogicalSide::BlockEnd)
}

/// Returns the physical side corresponding to logical inline-start.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#inline-direction>.
pub(crate) fn inline_start_side(writing_mode: WritingMode, direction: Direction) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, direction).physical_side(LogicalSide::InlineStart)
}

/// Returns the physical side corresponding to logical inline-end.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#inline-direction>.
pub(crate) fn inline_end_side(writing_mode: WritingMode, direction: Direction) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, direction).physical_side(LogicalSide::InlineEnd)
}

/// Returns the physical side corresponding to the line-relative over side.
///
/// This is controlled by line orientation, not block flow: `vertical-lr`
/// shares the clockwise line orientation of `vertical-rl`, while
/// `sideways-lr` reverses it.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#line-directions>.
pub(crate) fn line_over_side(writing_mode: WritingMode) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, Direction::Ltr).line_over_side()
}

/// Returns the physical side corresponding to the line-relative under side.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#line-directions>.
pub(crate) fn line_under_side(writing_mode: WritingMode) -> PhysicalSide {
    WritingModeAxes::new(writing_mode, Direction::Ltr).line_under_side()
}

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

    #[test]
    fn maps_horizontal_sides() {
        assert_eq!(
            block_start_side(WritingMode::HorizontalTb),
            PhysicalSide::Top
        );
        assert_eq!(
            block_end_side(WritingMode::HorizontalTb),
            PhysicalSide::Bottom
        );
        assert_eq!(
            inline_start_side(WritingMode::HorizontalTb, Direction::Ltr),
            PhysicalSide::Left
        );
        assert_eq!(
            inline_end_side(WritingMode::HorizontalTb, Direction::Ltr),
            PhysicalSide::Right
        );
        assert_eq!(
            inline_start_side(WritingMode::HorizontalTb, Direction::Rtl),
            PhysicalSide::Right
        );
        assert_eq!(
            inline_end_side(WritingMode::HorizontalTb, Direction::Rtl),
            PhysicalSide::Left
        );
    }

    #[test]
    fn maps_vertical_sides() {
        assert_eq!(
            block_start_side(WritingMode::VerticalRl),
            PhysicalSide::Right
        );
        assert_eq!(block_end_side(WritingMode::VerticalRl), PhysicalSide::Left);
        assert_eq!(
            block_start_side(WritingMode::VerticalLr),
            PhysicalSide::Left
        );
        assert_eq!(block_end_side(WritingMode::VerticalLr), PhysicalSide::Right);
        assert_eq!(
            inline_start_side(WritingMode::VerticalRl, Direction::Ltr),
            PhysicalSide::Top
        );
        assert_eq!(
            inline_end_side(WritingMode::VerticalRl, Direction::Rtl),
            PhysicalSide::Top
        );
    }

    #[test]
    fn maps_sideways_sides_and_line_orientation() {
        let sides = [
            (
                WritingMode::HorizontalTb,
                Direction::Ltr,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
                PhysicalSide::Left,
                PhysicalSide::Right,
            ),
            (
                WritingMode::HorizontalTb,
                Direction::Rtl,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
                PhysicalSide::Right,
                PhysicalSide::Left,
            ),
            (
                WritingMode::VerticalRl,
                Direction::Ltr,
                PhysicalSide::Right,
                PhysicalSide::Left,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
            ),
            (
                WritingMode::VerticalRl,
                Direction::Rtl,
                PhysicalSide::Right,
                PhysicalSide::Left,
                PhysicalSide::Bottom,
                PhysicalSide::Top,
            ),
            (
                WritingMode::VerticalLr,
                Direction::Ltr,
                PhysicalSide::Left,
                PhysicalSide::Right,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
            ),
            (
                WritingMode::VerticalLr,
                Direction::Rtl,
                PhysicalSide::Left,
                PhysicalSide::Right,
                PhysicalSide::Bottom,
                PhysicalSide::Top,
            ),
            (
                WritingMode::SidewaysRl,
                Direction::Ltr,
                PhysicalSide::Right,
                PhysicalSide::Left,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
            ),
            (
                WritingMode::SidewaysRl,
                Direction::Rtl,
                PhysicalSide::Right,
                PhysicalSide::Left,
                PhysicalSide::Bottom,
                PhysicalSide::Top,
            ),
            (
                WritingMode::SidewaysLr,
                Direction::Ltr,
                PhysicalSide::Left,
                PhysicalSide::Right,
                PhysicalSide::Bottom,
                PhysicalSide::Top,
            ),
            (
                WritingMode::SidewaysLr,
                Direction::Rtl,
                PhysicalSide::Left,
                PhysicalSide::Right,
                PhysicalSide::Top,
                PhysicalSide::Bottom,
            ),
        ];
        for (writing_mode, direction, block_start, block_end, inline_start, inline_end) in sides {
            let axes = WritingModeAxes::new(writing_mode, direction);
            assert_eq!(
                block_start_side(writing_mode),
                block_start,
                "{writing_mode:?}"
            );
            assert_eq!(block_end_side(writing_mode), block_end, "{writing_mode:?}");
            assert_eq!(
                inline_start_side(writing_mode, direction),
                inline_start,
                "{writing_mode:?} {direction:?}"
            );
            assert_eq!(
                inline_end_side(writing_mode, direction),
                inline_end,
                "{writing_mode:?} {direction:?}"
            );
            assert_eq!(
                axes.physical_axis(LogicalAxis::Inline),
                inline_start.axis(),
                "{writing_mode:?} {direction:?}"
            );
            assert_eq!(
                axes.physical_axis(LogicalAxis::Block),
                block_start.axis(),
                "{writing_mode:?} {direction:?}"
            );
            assert_eq!(
                axes.is_reversed(LogicalAxis::Inline),
                matches!(inline_start, PhysicalSide::Right | PhysicalSide::Bottom),
                "{writing_mode:?} {direction:?}"
            );
            assert_eq!(
                axes.is_reversed(LogicalAxis::Block),
                matches!(block_start, PhysicalSide::Right | PhysicalSide::Bottom),
                "{writing_mode:?} {direction:?}"
            );
        }
        assert_eq!(line_over_side(WritingMode::SidewaysRl), PhysicalSide::Right);
        assert_eq!(line_under_side(WritingMode::SidewaysRl), PhysicalSide::Left);
        assert_eq!(line_over_side(WritingMode::SidewaysLr), PhysicalSide::Left);
        assert_eq!(
            line_under_side(WritingMode::SidewaysLr),
            PhysicalSide::Right
        );
    }

    #[test]
    fn reports_physical_axes() {
        assert_eq!(PhysicalSide::Left.axis(), PhysicalAxis::Horizontal);
        assert_eq!(PhysicalSide::Right.axis(), PhysicalAxis::Horizontal);
        assert_eq!(PhysicalSide::Top.axis(), PhysicalAxis::Vertical);
        assert_eq!(PhysicalSide::Bottom.axis(), PhysicalAxis::Vertical);
    }

    #[test]
    fn canonical_mapping_is_complete_and_orthogonal() {
        for writing_mode in [
            WritingMode::HorizontalTb,
            WritingMode::VerticalRl,
            WritingMode::VerticalLr,
            WritingMode::SidewaysRl,
            WritingMode::SidewaysLr,
        ] {
            for direction in [Direction::Ltr, Direction::Rtl] {
                let axes = WritingModeAxes::new(writing_mode, direction);
                let inline_start = axes.physical_side(LogicalSide::InlineStart);
                let block_start = axes.physical_side(LogicalSide::BlockStart);

                assert_eq!(
                    axes.physical_side(LogicalSide::InlineEnd),
                    inline_start.opposite(),
                    "{writing_mode:?} {direction:?}"
                );
                assert_eq!(
                    axes.physical_side(LogicalSide::BlockEnd),
                    block_start.opposite(),
                    "{writing_mode:?} {direction:?}"
                );
                assert_ne!(
                    axes.physical_axis(LogicalAxis::Inline),
                    axes.physical_axis(LogicalAxis::Block),
                    "{writing_mode:?} {direction:?}"
                );
                assert_eq!(
                    axes.swaps_physical_axes(),
                    axes.physical_axis(LogicalAxis::Inline) == PhysicalAxis::Vertical,
                    "{writing_mode:?} {direction:?}"
                );
            }
        }
    }
}

/// Four physical CSS edges in top/right/bottom/left order.
///
/// CSS Box Model Level 3 defines physical margin, padding, and border edge
/// properties in this order:
/// <https://www.w3.org/TR/css-box-3/#the-margin-properties>.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PhysicalEdges<T> {
    pub top: T,
    pub right: T,
    pub bottom: T,
    pub left: T,
}

impl<T: Clone> PhysicalEdges<T> {
    pub(crate) fn all(value: T) -> Self {
        Self {
            top: value.clone(),
            right: value.clone(),
            bottom: value.clone(),
            left: value,
        }
    }
}