ferri-pdf 0.1.12

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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! CSS Backgrounds 3 longhands and CSS Images 4 images and gradients from match trees.
use crate::paint::{
    AbsoluteColor, Attachment, BackgroundClip, BackgroundImage, BackgroundSize, BoxArea,
    CircleSize, EllipseSize, FadeLayer, Gradient, GradientItem, GradientShape, ImageFunction,
    Interpolation, LengthPercentage, LineDirection, Position, PositionComponent, RadialEnding,
    RadialExtent, Repeat, SizeComponent,
};
use crate::style::{
    StyleError,
    box_values::{length_percentage, unsupported},
    calc::Kind,
    component::Component,
    grammar::{Keyword, Label, Production},
    units::Relative,
};

const TO_TOP_DEG: f32 = 0.0;
const TO_RIGHT_DEG: f32 = 90.0;
const TO_BOTTOM_DEG: f32 = 180.0;
const TO_LEFT_DEG: f32 = 270.0;
/// `[ <angle> | <zero> | to <side-or-corner> ] || <color-interpolation-method>` slots.
const DIRECTION_SLOT: usize = 0;
/// `<side-or-corner>` slots.
const HORIZONTAL_SLOT: usize = 0;
const VERTICAL_SLOT: usize = 1;
const STOP_LABELS: [Label; 2] = [
    Label::Production(Production::LinearColorStop),
    Label::Production(Production::LinearColorHint),
];
const ANGULAR_STOP_LABELS: [Label; 2] = [
    Label::Production(Production::AngularColorStop),
    Label::Production(Production::AngularColorHint),
];
const FULL_TURN_DEG: f32 = 360.0;
/// `<resolution> || type(<string>)` slots of an `<image-set-option>`.
const RESOLUTION_SLOT: usize = 0;
const TYPE_SLOT: usize = 1;
/// `[ <image> | <color> ] && <percentage [0,100]>?` slots of a `<cf-image>`.
const CF_IMAGE_SLOT: usize = 0;
const CF_PERCENTAGE_SLOT: usize = 1;
/// An `<image-set-option>` without a `<resolution>` selects as `1x` (CSS Images 4 §2.4).
const DEFAULT_RESOLUTION_DPPX: f32 = 1.0;
/// `type()` MIME type essences of the image formats the engine decodes.
const SUPPORTED_IMAGE_TYPES: [&str; 8] = [
    "image/png",
    "image/jpeg",
    "image/gif",
    "image/webp",
    "image/avif",
    "image/heic",
    "image/heif",
    "image/svg+xml",
];
const MIME_PARAMETER_SEPARATOR: char = ';';
const MIME_SUBTYPE_SEPARATOR: char = '/';

/// One item per comma-separated layer.
fn list<T>(
    value: &Component,
    item: impl Fn(&Component) -> Result<T, StyleError>,
) -> Result<Vec<T>, StyleError> {
    value.items().iter().map(item).collect()
}

pub(super) fn images(
    value: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
) -> Result<Vec<BackgroundImage>, StyleError> {
    list(value, |item| image(item, relative, color))
}
pub(super) fn sizes(
    value: &Component,
    relative: &Relative<'_>,
) -> Result<Vec<BackgroundSize>, StyleError> {
    list(value, |item| size(item, relative))
}
pub(super) fn repeats(value: &Component) -> Result<Vec<[Repeat; 2]>, StyleError> {
    list(value, repeat)
}
pub(super) fn boxes(value: &Component) -> Result<Vec<BoxArea>, StyleError> {
    list(value, box_area)
}
pub(super) fn clips(value: &Component) -> Result<Vec<BackgroundClip>, StyleError> {
    list(value, |item| {
        let mut keywords = Vec::new();
        item.keywords(&mut keywords);
        let border_area = keywords.contains(&Keyword::BorderArea);
        let text = keywords.contains(&Keyword::Text);
        if border_area || text {
            Ok(BackgroundClip::Painted { border_area, text })
        } else {
            box_area(item).map(BackgroundClip::Box)
        }
    })
}
pub(super) fn attachments(value: &Component) -> Result<Vec<Attachment>, StyleError> {
    list(value, attachment)
}

/// `<bg-image>`: `none` or an `<image>`.
pub(super) fn image(
    value: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
) -> Result<BackgroundImage, StyleError> {
    any_image(value, relative, color, false)
}

/// An `<image>`; `in_set` inside `image-set()`, which cannot nest inside itself directly or
/// through another image's arguments (CSS Images 4 §2.4).
fn any_image(
    value: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
    in_set: bool,
) -> Result<BackgroundImage, StyleError> {
    Ok(match value.unwrap() {
        Component::Keyword(Keyword::None) => BackgroundImage::None,
        Component::Url(url) | Component::String(url) => url_image(url, None),
        Component::Function(Keyword::Image, Some(arguments)) => {
            image_function(arguments, relative, color)?
        }
        Component::Function(Keyword::ImageSet, Some(options)) if !in_set => {
            image_set(options, relative, color)?
        }
        Component::Function(Keyword::ImageSet, _) => {
            return Err(StyleError::Invalid(format!(
                "image-set() nested in image-set(): {value}"
            )));
        }
        Component::Function(Keyword::CrossFade, Some(arguments)) => {
            cross_fade(arguments, relative, color, in_set)?
        }
        // Without script no paint worklet registers a name, so drawing the image finds no
        // document paint definition and outputs an invalid image (CSS Painting API 1 §7).
        Component::Function(Keyword::Paint, _) => BackgroundImage::Invalid,
        Component::Function(Keyword::Element, Some(arguments)) => match arguments.unwrap() {
            Component::Hash(id) => BackgroundImage::Element(id.as_ref().into()),
            other => return Err(unsupported(other)),
        },
        Component::Function(name, Some(arguments)) => {
            let repeating = matches!(
                name,
                Keyword::RepeatingLinearGradient
                    | Keyword::RepeatingRadialGradient
                    | Keyword::RepeatingConicGradient
            );
            BackgroundImage::Gradient(match name {
                Keyword::LinearGradient | Keyword::RepeatingLinearGradient => {
                    linear(arguments, relative, color, repeating)?
                }
                Keyword::RadialGradient | Keyword::RepeatingRadialGradient => {
                    radial(arguments, relative, color, repeating)?
                }
                Keyword::ConicGradient | Keyword::RepeatingConicGradient => {
                    conic(arguments, relative, color, repeating)?
                }
                _ => return Err(unsupported(value)),
            })
        }
        other => return Err(unsupported(other)),
    })
}

fn url_image(url: &str, function: Option<ImageFunction>) -> BackgroundImage {
    BackgroundImage::Url {
        url: url.to_owned(),
        resolution: None,
        scale: crate::paint::INITIAL_IMAGE_SCALE,
        function,
    }
}

/// CSS Images 4 §2.5 `image()`: a URL, which a string also writes, with its directionality
/// and fallback color; or a solid-color image.
fn image_function(
    arguments: &Component,
    relative: &Relative<'_>,
    current: &AbsoluteColor,
) -> Result<BackgroundImage, StyleError> {
    let rtl = arguments
        .find(Label::Production(Production::ImageTags))
        .and_then(Component::keyword)
        .map(|tag| tag == Keyword::Rtl);
    // `[ <image-src>? , <color>? ]!`: the color is the member that is not the source.
    let fallback = arguments
        .items()
        .last()
        .into_iter()
        .flat_map(Component::items)
        .flat_map(Component::items)
        .find(|item| {
            !matches!(
                item,
                Component::Delim(_)
                    | Component::Reference(Label::Production(Production::ImageSrc), _)
            )
        })
        .map(|color| crate::style::colors::absolute(color, relative, current))
        .transpose()?;
    Ok(
        match arguments
            .find(Label::Production(Production::ImageSrc))
            .map(Component::unwrap)
        {
            Some(Component::Url(url) | Component::String(url)) => {
                url_image(url, Some(ImageFunction { rtl, fallback }))
            }
            Some(other) => return Err(unsupported(other)),
            None => BackgroundImage::Color(fallback.ok_or_else(|| unsupported(arguments))?),
        },
    )
}

/// CSS Images 4 §2.6 `cross-fade()`: images and colours, each with the fraction of it the
/// result retains, omitted percentages sharing what the specified ones leave below 100%.
fn cross_fade(
    arguments: &Component,
    relative: &Relative<'_>,
    current: &AbsoluteColor,
    in_set: bool,
) -> Result<BackgroundImage, StyleError> {
    let mut images = Vec::new();
    let mut percentages = Vec::new();
    for argument in arguments.items() {
        let image = argument
            .slot(CF_IMAGE_SLOT)
            .ok_or_else(|| unsupported(argument))?;
        images.push(match image {
            Component::Reference(Label::Production(Production::Image), _) => {
                any_image(image, relative, current, in_set)?
            }
            color => {
                BackgroundImage::Color(crate::style::colors::absolute(color, relative, current)?)
            }
        });
        // The optional percentage matches as a group of at most one.
        percentages.push(
            argument
                .slot(CF_PERCENTAGE_SLOT)
                .and_then(|optional| optional.items().first())
                .map(|percentage| crate::style::values::percentage(percentage, relative))
                .transpose()?,
        );
    }
    Ok(BackgroundImage::CrossFade(
        images
            .into_iter()
            .zip(crate::paint::distribute_mix(&percentages))
            .map(|(image, percentage)| FadeLayer { image, percentage })
            .collect(),
    ))
}

/// CSS Images 4 §2.4: options with an unsupported `type()` or an already listed resolution
/// drop out; for resolution-independent PDF output the highest resolution is chosen, and it
/// becomes a raster image's natural resolution. No remaining option is an invalid image,
/// which paints nothing, as `none` does.
fn image_set(
    options: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
) -> Result<BackgroundImage, StyleError> {
    let mut candidates: Vec<(&Component, f32)> = Vec::new();
    for option in options.items() {
        let parts = option.items();
        let image = parts.first().ok_or_else(|| unsupported(option))?;
        // `[ <resolution> || type(<string>) ]?` matches as a group of at most one.
        let modifiers = parts.get(1).and_then(|optional| optional.items().first());
        if let Some(mime) = modifiers.and_then(|modifiers| modifiers.slot(TYPE_SLOT))
            && !mime.arguments().first().is_some_and(supported_image_type)
        {
            continue;
        }
        let resolution = match modifiers.and_then(|modifiers| modifiers.slot(RESOLUTION_SLOT)) {
            Some(resolution) => crate::style::values::resolution(resolution, relative)?,
            None => DEFAULT_RESOLUTION_DPPX,
        };
        if resolution <= 0.0 {
            return Err(StyleError::Invalid(format!(
                "non-positive image-set() resolution: {option}"
            )));
        }
        if candidates.iter().all(|(_, listed)| *listed != resolution) {
            candidates.push((image, resolution));
        }
    }
    let Some((image, resolution)) = candidates.into_iter().max_by(|a, b| a.1.total_cmp(&b.1))
    else {
        return Ok(BackgroundImage::None);
    };
    Ok(match any_image(image, relative, color, true)? {
        BackgroundImage::Url {
            url,
            scale,
            function,
            ..
        } => BackgroundImage::Url {
            url,
            resolution: Some(resolution),
            scale,
            function,
        },
        other => other,
    })
}

/// Whether a `type()` string is a valid MIME type whose essence names a decoded format.
fn supported_image_type(mime: &Component) -> bool {
    let Component::String(mime) = mime.unwrap() else {
        return false;
    };
    let essence = mime
        .split(MIME_PARAMETER_SEPARATOR)
        .next()
        .unwrap_or_default()
        .trim();
    essence.split_once(MIME_SUBTYPE_SEPARATOR).is_some()
        && SUPPORTED_IMAGE_TYPES
            .iter()
            .any(|supported| essence.eq_ignore_ascii_case(supported))
}

/// The `<color-interpolation-method>` of a gradient, or the default for its stops.
fn interpolation(
    arguments: &Component,
    items: &[GradientItem],
) -> Result<Interpolation, StyleError> {
    let Some(method) = arguments.find(Label::Production(Production::ColorInterpolationMethod))
    else {
        return Ok(Interpolation::default_for(items.iter().filter_map(
            |item| match item {
                GradientItem::Stop { color, .. } => Some(color),
                GradientItem::Hint(_) => None,
            },
        )));
    };
    interpolation_method(method)
}

/// A matched `<color-interpolation-method>`.
pub(super) fn interpolation_method(method: &Component) -> Result<Interpolation, StyleError> {
    let mut keywords = Vec::new();
    method.keywords(&mut keywords);
    let words: Vec<&str> = keywords
        .iter()
        .filter(|keyword| **keyword != Keyword::In)
        .map(|keyword| keyword.name())
        .collect();
    crate::style::colors::interpolation_method(&words).ok_or_else(|| unsupported(method))
}

fn linear(
    arguments: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
    repeating: bool,
) -> Result<Gradient, StyleError> {
    let syntax = arguments
        .find(Label::Production(Production::LinearGradientSyntax))
        .ok_or_else(|| unsupported(arguments))?;
    let direction = match syntax
        .items()
        .first()
        .and_then(|prelude| prelude.items().first())
        .and_then(|slots| slots.slot(DIRECTION_SLOT))
    {
        None => LineDirection::Angle(TO_BOTTOM_DEG),
        Some(direction) => match direction.find(Label::Production(Production::SideOrCorner)) {
            Some(side) => {
                let horizontal = side.slot(HORIZONTAL_SLOT).and_then(Component::keyword);
                let vertical = side.slot(VERTICAL_SLOT).and_then(Component::keyword);
                match (horizontal, vertical) {
                    (Some(h), Some(v)) => LineDirection::Corner {
                        right: h == Keyword::Right,
                        bottom: v == Keyword::Bottom,
                    },
                    (Some(Keyword::Right), None) => LineDirection::Angle(TO_RIGHT_DEG),
                    (Some(_), None) => LineDirection::Angle(TO_LEFT_DEG),
                    (None, Some(Keyword::Bottom)) => LineDirection::Angle(TO_BOTTOM_DEG),
                    _ => LineDirection::Angle(TO_TOP_DEG),
                }
            }
            None => LineDirection::Angle(crate::style::values::angle(direction, relative)?),
        },
    };
    let items = stop_list(syntax, relative, color, StopList::Linear)?;
    Ok(Gradient {
        shape: GradientShape::Linear(direction),
        interpolation: interpolation(arguments, &items)?,
        items,
        repeating,
    })
}

fn radial(
    arguments: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
    repeating: bool,
) -> Result<Gradient, StyleError> {
    let circle = arguments
        .find(Label::Production(Production::RadialShape))
        .and_then(Component::keyword)
        .map(|shape| shape == Keyword::Circle);
    let size = arguments.find(Label::Production(Production::RadialSize));
    let extent = size
        .and_then(|size| size.find(Label::Production(Production::RadialExtent)))
        .and_then(Component::keyword)
        .map(|keyword| match keyword {
            Keyword::ClosestSide => RadialExtent::ClosestSide,
            Keyword::FarthestSide => RadialExtent::FarthestSide,
            Keyword::ClosestCorner => RadialExtent::ClosestCorner,
            _ => RadialExtent::FarthestCorner,
        });
    let radii = match size.filter(|_| extent.is_none()) {
        Some(size) => size
            .items()
            .iter()
            .map(|radius| length_percentage(radius, relative))
            .collect::<Result<Vec<_>, _>>()?,
        None => Vec::new(),
    };
    // CSS Images 3 §3.2.1: one length is a circle's radius, two are an ellipse's.
    let ending = match (circle, extent, radii.as_slice()) {
        (Some(true) | None, None, [LengthPercentage::Length(radius)]) => {
            RadialEnding::Circle(CircleSize::Radius(*radius))
        }
        (Some(false) | None, None, [x, y]) => {
            RadialEnding::Ellipse(EllipseSize::Radii([x.clone(), y.clone()]))
        }
        (Some(true), extent, []) => RadialEnding::Circle(CircleSize::Extent(
            extent.unwrap_or(RadialExtent::FarthestCorner),
        )),
        (Some(false) | None, extent, []) => RadialEnding::Ellipse(EllipseSize::Extent(
            extent.unwrap_or(RadialExtent::FarthestCorner),
        )),
        _ => return Err(format!("invalid radial gradient size: {arguments}").into()),
    };
    let center = match arguments.find(Label::Production(Production::Position)) {
        Some(center) => position(center, relative)?,
        None => [PositionComponent::CENTER; 2],
    };
    let items = stop_list(arguments, relative, color, StopList::Linear)?;
    Ok(Gradient {
        shape: GradientShape::Radial { ending, center },
        interpolation: interpolation(arguments, &items)?,
        items,
        repeating,
    })
}

/// CSS Images 4 §3.3: `from` rotates the gradient and `at` places its centre.
fn conic(
    arguments: &Component,
    relative: &Relative<'_>,
    color: &AbsoluteColor,
    repeating: bool,
) -> Result<Gradient, StyleError> {
    let syntax = arguments
        .find(Label::Production(Production::ConicGradientSyntax))
        .ok_or_else(|| unsupported(arguments))?;
    let from = match after_keyword(syntax, Keyword::From) {
        Some(angle) => crate::style::values::angle(angle, relative)?,
        None => 0.0,
    };
    let center = match syntax.find(Label::Production(Production::Position)) {
        Some(center) => position(center, relative)?,
        None => [PositionComponent::CENTER; 2],
    };
    let items = stop_list(syntax, relative, color, StopList::Angular)?;
    Ok(Gradient {
        shape: GradientShape::Conic { from, center },
        interpolation: interpolation(arguments, &items)?,
        items,
        repeating,
    })
}

/// The term after `keyword` in a juxtaposed group, outside function arguments.
fn after_keyword(component: &Component, keyword: Keyword) -> Option<&Component> {
    match component.unwrap() {
        Component::Group(items) => items
            .windows(2)
            .find(|pair| pair[0].keyword() == Some(keyword))
            .map(|pair| &pair[1])
            .or_else(|| items.iter().find_map(|item| after_keyword(item, keyword))),
        Component::Slots(slots) => slots
            .iter()
            .flatten()
            .find_map(|slot| after_keyword(slot, keyword)),
        _ => None,
    }
}

#[derive(Clone, Copy, PartialEq)]
enum StopList {
    /// `<color-stop-list>`: lengths and percentages along the gradient line.
    Linear,
    /// `<angular-color-stop-list>`: angles and percentages of a turn, kept as fractions.
    Angular,
}

/// An `<angle-percentage>` or `<zero>` as a fraction of a full turn.
fn turn_fraction(value: &Component, relative: &Relative<'_>) -> Result<f32, StyleError> {
    match value.unwrap() {
        Component::Percentage(fraction) => Ok(*fraction),
        Component::Math(source, Kind::Angle, range) => {
            Ok(
                crate::style::values::math(source, relative, Kind::Angle, Some(Kind::Angle))?
                    .resolve(FULL_TURN_DEG, *range)
                    / FULL_TURN_DEG,
            )
        }
        _ => Ok(crate::style::values::angle(value, relative)? / FULL_TURN_DEG),
    }
}

/// A colour stop list: stops with up to two positions and the hints between them.
fn stop_list(
    syntax: &Component,
    relative: &Relative<'_>,
    current: &AbsoluteColor,
    kind: StopList,
) -> Result<Vec<GradientItem>, StyleError> {
    let (list_label, labels, positions_label) = match kind {
        StopList::Linear => (
            Production::ColorStopList,
            STOP_LABELS,
            Production::ColorStopLength,
        ),
        StopList::Angular => (
            Production::AngularColorStopList,
            ANGULAR_STOP_LABELS,
            Production::ColorStopAngle,
        ),
    };
    let list = syntax
        .find(Label::Production(list_label))
        .ok_or_else(|| unsupported(syntax))?;
    let position = |value: &Component| match kind {
        StopList::Linear => length_percentage(value, relative),
        StopList::Angular => turn_fraction(value, relative).map(LengthPercentage::Percentage),
    };
    let mut found = Vec::new();
    list.references(&labels, &mut found);
    let mut items = Vec::new();
    for (label, item) in found {
        if label == labels[1] {
            items.push(GradientItem::Hint(position(item)?));
            continue;
        }
        let parts = item.items();
        let color = crate::style::colors::absolute(&parts[0], relative, current)?;
        let positions = match item.find(Label::Production(positions_label)) {
            Some(lengths) => lengths
                .items()
                .iter()
                .map(|value| position(value).map(Some))
                .collect::<Result<Vec<_>, _>>()?,
            None => vec![None],
        };
        items.extend(
            positions
                .into_iter()
                .map(|position| GradientItem::Stop { color, position }),
        );
    }
    Ok(items)
}

/// Keywords and offsets in source order, outside nested slots.
fn terms(component: &Component) -> Vec<&Component> {
    match component {
        Component::Group(items) => items.iter().flat_map(terms).collect(),
        Component::Reference(_, inner) => terms(inner),
        other => vec![other],
    }
}

/// One axis: an edge keyword with an optional offset, `center`, or an offset from the start.
/// The flow-relative edges of CSS Backgrounds 4 compute to the physical edge they name in the
/// element's horizontal writing mode, where x is the inline axis and y the block axis.
fn axis(
    part: &Component,
    relative: &Relative<'_>,
    rtl: bool,
) -> Result<PositionComponent, StyleError> {
    let parts = terms(part);
    let from_end = match parts.first().and_then(|term| term.keyword()) {
        Some(Keyword::Center) => return Ok(PositionComponent::CENTER),
        Some(Keyword::XStart) => rtl,
        Some(Keyword::XEnd) => !rtl,
        Some(Keyword::Right | Keyword::Bottom | Keyword::YEnd) => true,
        _ => false,
    };
    let offset = parts
        .iter()
        .find(|term| term.keyword().is_none())
        .map(|offset| length_percentage(offset, relative))
        .transpose()?
        .unwrap_or(LengthPercentage::ZERO);
    Ok(PositionComponent { from_end, offset })
}

/// `<bg-position>` or `<position>`: one value, a horizontal-vertical pair, or keyword groups
/// in either order.
pub(super) fn position(value: &Component, relative: &Relative<'_>) -> Result<Position, StyleError> {
    let [x, y] = position_axes(value)?;
    let side = |part: Option<&Component>| {
        part.map_or(Ok(PositionComponent::CENTER), |part| {
            axis(part, relative, false)
        })
    };
    Ok([side(x)?, side(y)?])
}

/// The horizontal and vertical parts of a `<bg-position>` or `<position>`; `None` is `center`.
pub(super) fn position_axes(value: &Component) -> Result<[Option<&Component>; 2], StyleError> {
    let value = value.unwrap();
    if let Component::Slots(slots) = value {
        return Ok([slots[0].as_ref(), slots[1].as_ref()]);
    }
    let parts = terms(value);
    match parts.as_slice() {
        [single] if matches!(single.keyword(), Some(Keyword::Top | Keyword::Bottom)) => {
            Ok([None, Some(single)])
        }
        [single] => Ok([Some(single), None]),
        [x, y] => Ok([Some(x), Some(y)]),
        _ => Err(unsupported(value)),
    }
}

/// `background-position-x` or `-y` (CSS Backgrounds 4): per layer, `center` or an edge with
/// an offset from it.
pub(super) fn axis_positions(
    value: &Component,
    relative: &Relative<'_>,
    rtl: bool,
) -> Result<Vec<PositionComponent>, StyleError> {
    list(value, |item| axis(item, relative, rtl))
}

/// `<bg-size>`: `cover`, `contain`, or a width and an optional height.
fn size(value: &Component, relative: &Relative<'_>) -> Result<BackgroundSize, StyleError> {
    match value.keyword() {
        Some(Keyword::Cover) => return Ok(BackgroundSize::Cover),
        Some(Keyword::Contain) => return Ok(BackgroundSize::Contain),
        _ => {}
    }
    let component = |part: &Component| -> Result<SizeComponent, StyleError> {
        match part.keyword() {
            Some(Keyword::Auto) => Ok(SizeComponent::Auto),
            _ => length_percentage(part, relative).map(SizeComponent::Value),
        }
    };
    let parts = terms(value);
    Ok(BackgroundSize::Explicit([
        component(parts[0])?,
        parts
            .get(1)
            .map_or(Ok(SizeComponent::Auto), |height| component(height))?,
    ]))
}

/// `<repeat-style>`: `repeat-x`, `repeat-y`, or one keyword per axis.
fn repeat(value: &Component) -> Result<[Repeat; 2], StyleError> {
    let keyword = |part: &Component| match part.keyword() {
        Some(Keyword::Repeat) => Ok(Repeat::Repeat),
        Some(Keyword::Space) => Ok(Repeat::Space),
        Some(Keyword::Round) => Ok(Repeat::Round),
        Some(Keyword::NoRepeat) => Ok(Repeat::NoRepeat),
        _ => Err(unsupported(part)),
    };
    match value.keyword() {
        Some(Keyword::RepeatX) => return Ok([Repeat::Repeat, Repeat::NoRepeat]),
        Some(Keyword::RepeatY) => return Ok([Repeat::NoRepeat, Repeat::Repeat]),
        _ => {}
    }
    let parts = terms(value);
    let x = keyword(parts[0])?;
    Ok([x, parts.get(1).map_or(Ok(x), |y| keyword(y))?])
}

/// `<visual-box>`.
pub(super) fn box_area(value: &Component) -> Result<BoxArea, StyleError> {
    match value.keyword() {
        Some(Keyword::BorderBox) => Ok(BoxArea::BorderBox),
        Some(Keyword::PaddingBox) => Ok(BoxArea::PaddingBox),
        Some(Keyword::ContentBox) => Ok(BoxArea::ContentBox),
        _ => Err(unsupported(value)),
    }
}

fn attachment(value: &Component) -> Result<Attachment, StyleError> {
    match value.keyword() {
        Some(Keyword::Scroll) => Ok(Attachment::Scroll),
        Some(Keyword::Fixed) => Ok(Attachment::Fixed),
        Some(Keyword::Local) => Ok(Attachment::Local),
        _ => Err(unsupported(value)),
    }
}