#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LayoutUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RasterPixelUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CssPixelUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ContentBoxUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BorderBoxUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NonContentUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MarginBoxUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ContentBoxBaselineUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AlphabeticBaselineRelativeUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BaselineTableAlignmentUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AuthorBaselineShiftUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum GlyphBaselineDisplacementUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AtomicInlineBaselineSourceUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AtomicInlineMarginBoxBaselineUnit {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AtomicInlinePaintPlacementBaselineUnit {}
pub(crate) type LayoutLength = euclid::Length<f32, LayoutUnit>;
pub(crate) type LayoutSize = euclid::Size2D<f32, LayoutUnit>;
pub(crate) type RasterPixelSize = euclid::Size2D<u32, RasterPixelUnit>;
pub(crate) type CssPixelSize = euclid::Size2D<u32, CssPixelUnit>;
pub(crate) type ContentBoxLength = euclid::Length<f32, ContentBoxUnit>;
pub(crate) type BorderBoxLength = euclid::Length<f32, BorderBoxUnit>;
pub(crate) type NonContentLength = euclid::Length<f32, NonContentUnit>;
pub(crate) type MarginBoxLength = euclid::Length<f32, MarginBoxUnit>;
pub(crate) type ContentBoxBaselineOffset = euclid::Length<f32, ContentBoxBaselineUnit>;
pub(crate) type AlphabeticBaselineRelativeOffset =
euclid::Length<f32, AlphabeticBaselineRelativeUnit>;
pub(crate) type BaselineTableAlignmentDelta = euclid::Length<f32, BaselineTableAlignmentUnit>;
pub(crate) type AuthorBaselineShift = euclid::Length<f32, AuthorBaselineShiftUnit>;
pub(crate) type GlyphBaselineDisplacement = euclid::Length<f32, GlyphBaselineDisplacementUnit>;
pub(crate) type AtomicInlineBaselineSourceOffset =
euclid::Length<f32, AtomicInlineBaselineSourceUnit>;
pub(crate) type AtomicInlineMarginBoxBaselineOffset =
euclid::Length<f32, AtomicInlineMarginBoxBaselineUnit>;
pub(crate) type AtomicInlinePaintPlacementBaselineOffset =
euclid::Length<f32, AtomicInlinePaintPlacementBaselineUnit>;
pub(crate) type MarginBoxSize = euclid::Size2D<f32, MarginBoxUnit>;
pub(crate) type ContentBoxSize = euclid::Size2D<f32, ContentBoxUnit>;
pub(crate) type BorderBoxSize = euclid::Size2D<f32, BorderBoxUnit>;
pub(crate) const fn layout_pt(value: f32) -> LayoutLength {
LayoutLength::new(value)
}
pub(crate) const fn layout_px(value: f32) -> LayoutLength {
layout_pt(value * crate::css::CSS_PX_TO_PT)
}
pub(crate) const fn content_box_pt(value: f32) -> ContentBoxLength {
ContentBoxLength::new(value)
}
pub(crate) const fn border_box_pt(value: f32) -> BorderBoxLength {
BorderBoxLength::new(value)
}
pub(crate) const fn non_content_pt(value: f32) -> NonContentLength {
NonContentLength::new(value)
}
pub(crate) const fn margin_box_pt(value: f32) -> MarginBoxLength {
MarginBoxLength::new(value)
}
pub(crate) const fn content_box_baseline_pt(value: f32) -> ContentBoxBaselineOffset {
ContentBoxBaselineOffset::new(value)
}
pub(crate) const fn alphabetic_baseline_relative_pt(
value: f32,
) -> AlphabeticBaselineRelativeOffset {
AlphabeticBaselineRelativeOffset::new(value)
}
pub(crate) const fn baseline_table_alignment_pt(value: f32) -> BaselineTableAlignmentDelta {
BaselineTableAlignmentDelta::new(value)
}
pub(crate) const fn author_baseline_shift_pt(value: f32) -> AuthorBaselineShift {
AuthorBaselineShift::new(value)
}
pub(crate) const fn glyph_baseline_displacement_pt(value: f32) -> GlyphBaselineDisplacement {
GlyphBaselineDisplacement::new(value)
}
pub(crate) const fn atomic_inline_baseline_source_pt(
value: f32,
) -> AtomicInlineBaselineSourceOffset {
AtomicInlineBaselineSourceOffset::new(value)
}
pub(crate) const fn atomic_inline_margin_box_baseline_pt(
value: f32,
) -> AtomicInlineMarginBoxBaselineOffset {
AtomicInlineMarginBoxBaselineOffset::new(value)
}
pub(crate) const fn atomic_inline_paint_placement_baseline_pt(
value: f32,
) -> AtomicInlinePaintPlacementBaselineOffset {
AtomicInlinePaintPlacementBaselineOffset::new(value)
}
pub(crate) const fn margin_box_size_pt(width: f32, height: f32) -> MarginBoxSize {
MarginBoxSize::new(width, height)
}
pub(crate) const fn content_box_size_pt(width: f32, height: f32) -> ContentBoxSize {
ContentBoxSize::new(width, height)
}
pub(crate) const fn border_box_size_pt(width: f32, height: f32) -> BorderBoxSize {
BorderBoxSize::new(width, height)
}
pub(crate) fn layout_points(length: LayoutLength) -> f32 {
length.get()
}
pub(crate) fn layout_to_content_box_length(length: LayoutLength) -> ContentBoxLength {
length.cast_unit()
}
pub(crate) fn layout_to_border_box_length(length: LayoutLength) -> BorderBoxLength {
length.cast_unit()
}
pub(crate) trait SemanticLengthExt {
fn points(self) -> f32;
}
impl<Unit> SemanticLengthExt for euclid::Length<f32, Unit> {
fn points(self) -> f32 {
self.get()
}
}
pub(crate) trait IntoLayoutLength {
fn into_layout_length(self) -> LayoutLength;
}
impl IntoLayoutLength for LayoutLength {
fn into_layout_length(self) -> LayoutLength {
self
}
}
impl IntoLayoutLength for ContentBoxLength {
fn into_layout_length(self) -> LayoutLength {
self.cast_unit()
}
}
impl IntoLayoutLength for BorderBoxLength {
fn into_layout_length(self) -> LayoutLength {
self.cast_unit()
}
}
impl IntoLayoutLength for NonContentLength {
fn into_layout_length(self) -> LayoutLength {
self.cast_unit()
}
}
impl IntoLayoutLength for MarginBoxLength {
fn into_layout_length(self) -> LayoutLength {
self.cast_unit()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum PercentageBasis<T, Source = ()> {
Definite { value: T, source: Source },
Indefinite,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub(crate) struct Definite<T>(T);
impl<T> Definite<T> {
pub(crate) fn new(value: T) -> Self {
Self(value)
}
pub(crate) fn value(self) -> T {
self.0
}
}
impl<T> PercentageBasis<T, ()> {
pub(crate) fn definite(value: T) -> Self {
Self::Definite { value, source: () }
}
}
impl<T, Source> PercentageBasis<T, Source> {
pub(crate) fn definite_from(value: T, source: Source) -> Self {
Self::Definite { value, source }
}
pub(crate) fn indefinite() -> Self {
Self::Indefinite
}
pub(crate) fn is_definite(&self) -> bool {
matches!(self, Self::Definite { .. })
}
pub(crate) fn value(self) -> Option<T> {
match self {
Self::Definite { value, .. } => Some(value),
Self::Indefinite => None,
}
}
pub(crate) fn map_value<U>(self, map: impl FnOnce(T) -> U) -> PercentageBasis<U, Source> {
match self {
Self::Definite { value, source } => PercentageBasis::Definite {
value: map(value),
source,
},
Self::Indefinite => PercentageBasis::Indefinite,
}
}
pub(crate) fn map_source<NewSource>(
self,
map: impl FnOnce(Source) -> NewSource,
) -> PercentageBasis<T, NewSource> {
match self {
Self::Definite { value, source } => PercentageBasis::Definite {
value,
source: map(source),
},
Self::Indefinite => PercentageBasis::Indefinite,
}
}
}
impl<T, Source> PercentageBasis<T, Source>
where
T: SemanticLengthExt,
{
pub(crate) fn points(self) -> Option<f32> {
self.value().map(SemanticLengthExt::points)
}
}
pub(crate) fn content_box_to_border_box_length(
content: ContentBoxLength,
extras: NonContentLength,
) -> BorderBoxLength {
border_box_pt((content.points() + extras.points()).max(0.0))
}
pub(crate) fn content_box_to_margin_box_length(
content: ContentBoxLength,
non_content: NonContentLength,
margins: LayoutLength,
) -> MarginBoxLength {
margin_box_pt(content.points() + non_content.points() + margins.points())
}
pub(crate) fn border_box_to_content_box_length(
border: BorderBoxLength,
extras: NonContentLength,
) -> ContentBoxLength {
content_box_pt((border.points() - extras.points()).max(0.0))
}
pub(crate) fn content_box_to_border_box_size(
content: ContentBoxSize,
horizontal_extras: NonContentLength,
vertical_extras: NonContentLength,
) -> BorderBoxSize {
border_box_size_pt(
(content.width + horizontal_extras.points()).max(0.0),
(content.height + vertical_extras.points()).max(0.0),
)
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn border_box_to_content_box_size(
border: BorderBoxSize,
horizontal_extras: NonContentLength,
vertical_extras: NonContentLength,
) -> ContentBoxSize {
content_box_size_pt(
(border.width - horizontal_extras.points()).max(0.0),
(border.height - vertical_extras.points()).max(0.0),
)
}
pub(crate) fn css_pixel_natural_layout_size(size: CssPixelSize) -> LayoutSize {
LayoutSize::new(
layout_points(layout_px(size.width as f32)),
layout_points(layout_px(size.height as f32)),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn semantic_relabeling_preserves_points_without_box_model_arithmetic() {
let layout: LayoutLength = content_box_pt(42.0).into_layout_length();
let content: ContentBoxLength = layout_to_content_box_length(layout);
assert_eq!(layout, layout_pt(42.0));
assert_eq!(content, content_box_pt(42.0));
assert_eq!(border_box_pt(42.0).into_layout_length(), layout_pt(42.0));
assert_eq!(non_content_pt(42.0).into_layout_length(), layout_pt(42.0));
}
#[test]
fn content_box_size_expands_to_border_box_size() {
let border = content_box_to_border_box_size(
content_box_size_pt(150.0, 150.0),
non_content_pt(150.0),
non_content_pt(150.0),
);
assert_eq!(border.width, 300.0);
assert_eq!(border.height, 300.0);
}
#[test]
fn border_box_size_shrinks_to_non_negative_content_box_size() {
let content = border_box_to_content_box_size(
border_box_size_pt(100.0, 100.0),
non_content_pt(150.0),
non_content_pt(150.0),
);
assert_eq!(content.width, 0.0);
assert_eq!(content.height, 0.0);
}
#[test]
fn content_box_to_margin_box_keeps_signed_used_margins_explicit() {
assert_eq!(
content_box_to_margin_box_length(
content_box_pt(40.0),
non_content_pt(10.0),
layout_pt(-15.0),
),
margin_box_pt(35.0)
);
}
#[test]
fn percentage_basis_map_source_preserves_value_and_indefiniteness() {
#[derive(Debug, Clone, Copy, PartialEq)]
enum Source {
Parent,
Child,
}
let definite = PercentageBasis::definite_from(content_box_pt(42.0), Source::Parent)
.map_source(|_| Source::Child);
assert_eq!(
definite,
PercentageBasis::definite_from(content_box_pt(42.0), Source::Child)
);
let indefinite: PercentageBasis<ContentBoxLength, Source> = PercentageBasis::indefinite();
assert_eq!(
indefinite.map_source(|_| Source::Child),
PercentageBasis::indefinite()
);
}
}