use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ContainerSpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum TaffySpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum InlineSpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum BlockSpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum GridSpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum DocumentCanvasSpace {}
pub(super) type ContainerPoint = euclid::Point2D<f32, ContainerSpace>;
pub(super) type ContainerSize = euclid::Size2D<f32, ContainerSpace>;
pub(super) type ContainerVector = euclid::Vector2D<f32, ContainerSpace>;
pub(super) type ContainerRect = euclid::Rect<f32, ContainerSpace>;
pub(super) type InlinePoint = euclid::Point2D<f32, InlineSpace>;
pub(super) type InlineVector = euclid::Vector2D<f32, InlineSpace>;
pub(super) type InlineSize = euclid::Size2D<f32, InlineSpace>;
pub(super) type InlineRect = euclid::Rect<f32, InlineSpace>;
pub(super) type BlockPoint = euclid::Point2D<f32, BlockSpace>;
pub(super) type BlockSize = euclid::Size2D<f32, BlockSpace>;
pub(super) type BlockRect = euclid::Rect<f32, BlockSpace>;
pub(super) type GridPoint = euclid::Point2D<f32, GridSpace>;
pub(super) type GridSize = euclid::Size2D<f32, GridSpace>;
pub(super) type GridRect = euclid::Rect<f32, GridSpace>;
pub(super) type TaffyPoint = euclid::Point2D<f32, TaffySpace>;
pub(super) type TaffySize = euclid::Size2D<f32, TaffySpace>;
pub(super) type TaffyRect = euclid::Rect<f32, TaffySpace>;
pub(super) type DocumentCanvasPoint = euclid::Point2D<f32, DocumentCanvasSpace>;
pub(super) type DocumentCanvasSize = euclid::Size2D<f32, DocumentCanvasSpace>;
pub(super) type DocumentCanvasRect = euclid::Rect<f32, DocumentCanvasSpace>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct PageInlineSpan {
start_x: f32,
width: f32,
}
impl PageInlineSpan {
pub(super) fn new(start_x: f32, width: f32) -> Self {
Self {
start_x,
width: width.max(0.0),
}
}
pub(super) fn from_edges(left_x: f32, right_x: f32) -> Self {
Self::new(left_x, (right_x - left_x).max(0.0))
}
pub(super) fn left_x(self) -> f32 {
self.start_x
}
pub(super) fn right_x(self) -> f32 {
self.start_x + self.width
}
pub(super) fn width(self) -> f32 {
self.width
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct PageBlockSpan {
top_y: f32,
height: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout) struct PageTopBlockPosition(f32);
impl PageTopBlockPosition {
pub(in crate::layout) const fn new(top_y: f32) -> Self {
Self(top_y)
}
pub(in crate::layout) fn points(self) -> f32 {
self.0
}
pub(in crate::layout) fn toward_block_end(self, distance: LayoutLength) -> Self {
Self(self.0 - distance.points())
}
pub(in crate::layout) fn toward_block_start(self, distance: LayoutLength) -> Self {
Self(self.0 + distance.points())
}
pub(in crate::layout) fn block_extent_to(self, block_end: Self) -> LayoutLength {
layout_pt((self.0 - block_end.0).max(0.0))
}
pub(in crate::layout) fn min(self, other: Self) -> Self {
if self.0 <= other.0 { self } else { other }
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout) struct PageInlinePosition(f32);
impl PageInlinePosition {
pub(in crate::layout) const fn new(x: f32) -> Self {
Self(x)
}
pub(in crate::layout) fn points(self) -> f32 {
self.0
}
}
impl PageBlockSpan {
pub(super) fn new(top_y: f32, height: f32) -> Self {
Self {
top_y,
height: height.max(0.0),
}
}
pub(super) fn from_edges(top_y: f32, bottom_y: f32) -> Self {
Self::new(top_y, (top_y - bottom_y).max(0.0))
}
pub(super) fn top_y(self) -> f32 {
self.top_y
}
pub(super) fn bottom_y(self) -> f32 {
self.top_y - self.height
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct PageTopPoint {
x: f32,
top_y: f32,
}
impl PageTopPoint {
pub(super) const fn new(x: f32, top_y: f32) -> Self {
Self { x, top_y }
}
pub(super) fn from_inline_x_and_block_position(x: f32, top: PageTopBlockPosition) -> Self {
Self::new(x, top.points())
}
pub(super) fn x(self) -> f32 {
self.x
}
pub(super) fn top_y(self) -> f32 {
self.top_y
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::layout) enum PageTopSpace {}
type PageTopEuclidRect = euclid::Rect<f32, PageTopSpace>;
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(transparent)]
pub(super) struct PageTopRect(PageTopEuclidRect);
const MAX_PAINT_GEOMETRY_EXTENT: f32 = 1_000_000.0;
impl PageTopRect {
pub(super) fn new(x: f32, top_y: f32, width: f32, height: f32) -> Self {
Self(PageTopEuclidRect::new(
euclid::Point2D::new(x, top_y),
euclid::Size2D::new(width.max(0.0), height.max(0.0)),
))
}
pub(super) fn x(self) -> f32 {
self.0.origin.x
}
pub(super) fn top_y(self) -> f32 {
self.0.origin.y
}
pub(super) fn width(self) -> f32 {
self.0.size.width
}
pub(super) fn height(self) -> f32 {
self.0.size.height
}
pub(super) fn bottom_y(self) -> f32 {
self.top_y() - self.height()
}
pub(super) fn paint_rect(self) -> PaintRect {
let height = self.height().min(MAX_PAINT_GEOMETRY_EXTENT);
PaintRect::new(
PaintPoint::new(self.x(), self.top_y() - height),
PaintSize::new(self.width().min(MAX_PAINT_GEOMETRY_EXTENT), height),
)
}
pub(super) fn paint_clip(self) -> PaintClip {
paint_rect_to_clip(self.paint_rect())
}
pub(super) fn overflow_clip(self) -> OverflowClip {
OverflowClip::from_paint_rect(self.paint_rect())
}
pub(super) fn rendered_rect(self, fill: Option<CssColor>) -> RenderedRect {
RenderedRect::from_paint_rect(self.paint_rect(), fill)
}
}
pub(super) fn paint_rect_to_clip(rect: PaintRect) -> PaintClip {
PaintClip::from_paint_rect(rect)
}
pub(super) fn paint_space_rect(x: f32, y: f32, width: f32, height: f32) -> PaintRect {
PaintRect::new(
PaintPoint::new(x, y),
PaintSize::new(width.max(0.0), height.max(0.0)),
)
}
pub(super) fn inset_paint_rect(rect: PaintRect, edges: css::Edges) -> PaintRect {
PaintRect::new(
PaintPoint::new(rect.origin.x + edges.left, rect.origin.y + edges.bottom),
PaintSize::new(
(rect.size.width - edges.left - edges.right).max(0.0),
(rect.size.height - edges.top - edges.bottom).max(0.0),
),
)
}
pub(super) fn intersect_paint_rect_or_empty(left: PaintRect, right: PaintRect) -> PaintRect {
let origin = PaintPoint::new(
left.origin.x.max(right.origin.x),
left.origin.y.max(right.origin.y),
);
left.intersection(&right)
.unwrap_or_else(|| PaintRect::new(origin, PaintSize::new(0.0, 0.0)))
}
pub(super) fn paint_space_point(x: f32, y: f32) -> PaintPoint {
PaintPoint::new(x, y)
}
pub(super) fn grid_rect_to_page_top_rect(
rect: GridRect,
container_origin: PageTopPoint,
) -> PageTopRect {
PageTopRect::new(
container_origin.x() + rect.origin.x,
container_origin.top_y() - rect.origin.y,
rect.size.width.max(0.0),
rect.size.height.max(0.0),
)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct LogicalPoint {
pub(super) inline: f32,
pub(super) block: f32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct LogicalSize {
pub(super) inline: f32,
pub(super) block: f32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct LogicalInlineContentSize(ContentBoxLength);
impl LogicalInlineContentSize {
pub(in crate::layout) fn new(value: ContentBoxLength) -> Self {
Self(value)
}
pub(in crate::layout) fn points(self) -> f32 {
self.0.points()
}
pub(in crate::layout) fn max(self, other: Self) -> Self {
if self.0 >= other.0 { self } else { other }
}
pub(in crate::layout) fn content_box_length(self) -> ContentBoxLength {
self.0
}
}
impl SemanticLengthExt for LogicalInlineContentSize {
fn points(self) -> f32 {
self.0.points()
}
}
impl crate::units::IntoLayoutLength for LogicalInlineContentSize {
fn into_layout_length(self) -> LayoutLength {
crate::units::IntoLayoutLength::into_layout_length(self.0)
}
}
pub(in crate::layout) type LogicalInlinePercentageBasis<Source = ()> =
PercentageBasis<LogicalInlineContentSize, Source>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct LogicalBlockContentSize(ContentBoxLength);
impl LogicalBlockContentSize {
pub(in crate::layout) fn new(value: ContentBoxLength) -> Self {
Self(value)
}
#[allow(
dead_code,
reason = "logical block scalar access is staged for broader axis-typed sizing callers"
)]
pub(in crate::layout) fn points(self) -> f32 {
self.0.points()
}
pub(in crate::layout) fn content_box_length(self) -> ContentBoxLength {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct PhysicalContentWidth(ContentBoxLength);
impl PhysicalContentWidth {
pub(in crate::layout) fn new(value: ContentBoxLength) -> Self {
Self(value)
}
pub(in crate::layout) fn points(self) -> f32 {
self.0.points()
}
pub(in crate::layout) fn non_negative(self) -> Self {
Self::new(self.0.max(content_box_pt(0.0)))
}
pub(in crate::layout) fn max(self, other: Self) -> Self {
if self.0 >= other.0 { self } else { other }
}
pub(in crate::layout) fn content_box_length(self) -> ContentBoxLength {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout) struct PhysicalContentHeight(ContentBoxLength);
impl PhysicalContentHeight {
pub(in crate::layout) fn new(value: ContentBoxLength) -> Self {
Self(value)
}
pub(in crate::layout) fn points(self) -> f32 {
self.0.points()
}
pub(in crate::layout) fn non_negative(self) -> Self {
Self::new(self.0.max(content_box_pt(0.0)))
}
pub(in crate::layout) fn max(self, other: Self) -> Self {
if self.0 >= other.0 { self } else { other }
}
pub(in crate::layout) fn content_box_length(self) -> ContentBoxLength {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct LogicalInlineSpan {
start: f32,
size: f32,
}
impl LogicalInlineSpan {
pub(super) fn new(start: f32, size: f32) -> Self {
Self {
start: start.max(0.0),
size: size.max(0.0),
}
}
pub(super) fn start(self) -> f32 {
self.start
}
pub(super) fn size(self) -> f32 {
self.size
}
pub(super) fn end(self) -> f32 {
self.start + self.size
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct LogicalRect {
pub(super) origin: LogicalPoint,
pub(super) size: LogicalSize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct FlowAxes {
axes: WritingModeAxes,
}
impl FlowAxes {
pub(super) const fn new(writing_mode: WritingMode, direction: Direction) -> Self {
Self {
axes: WritingModeAxes::new(writing_mode, direction),
}
}
pub(super) fn for_style(style: &ComputedStyle) -> Self {
Self::new(style.writing_mode, style.used_direction())
}
pub(super) fn direction(self) -> Direction {
self.axes.direction()
}
pub(super) fn inline_start_side(self) -> PhysicalSide {
self.axes.physical_side(LogicalSide::InlineStart)
}
pub(super) fn inline_end_side(self) -> PhysicalSide {
self.axes.physical_side(LogicalSide::InlineEnd)
}
pub(super) fn block_start_side(self) -> PhysicalSide {
self.axes.physical_side(LogicalSide::BlockStart)
}
pub(super) fn line_left_side(self) -> PhysicalSide {
self.axes.line_left_side()
}
pub(super) fn line_right_side(self) -> PhysicalSide {
self.axes.line_right_side()
}
pub(super) fn physical_size<T>(self, inline: T, block: T) -> (T, T) {
self.axes.physical_size(inline, block)
}
pub(in crate::layout) fn continuous_block_to_local_paint_translation(
self,
source_block_start: LayoutLength,
) -> PaintTranslation {
let offset = source_block_start.points();
match self.block_start_side() {
PhysicalSide::Top => PaintTranslation::new(0.0, offset),
PhysicalSide::Bottom => PaintTranslation::new(0.0, -offset),
PhysicalSide::Left => PaintTranslation::new(-offset, 0.0),
PhysicalSide::Right => PaintTranslation::new(offset, 0.0),
}
}
pub(super) const fn writing_mode(self) -> WritingMode {
self.axes.writing_mode()
}
pub(super) fn physical_size_from_logical(self, size: LogicalSize) -> ContainerSize {
let (width, height) = self.axes.physical_size(size.inline, size.block);
ContainerSize::new(width, height)
}
pub(in crate::layout) fn physical_width_from_logical_content_sizes(
self,
inline: LogicalInlineContentSize,
block: LogicalBlockContentSize,
) -> PhysicalContentWidth {
if self.axes.swaps_physical_axes() {
PhysicalContentWidth::new(block.content_box_length())
} else {
PhysicalContentWidth::new(inline.content_box_length())
}
}
#[allow(
dead_code,
reason = "height projection is added with width projection so both physical axes share one typed model"
)]
pub(in crate::layout) fn physical_height_from_logical_content_sizes(
self,
inline: LogicalInlineContentSize,
block: LogicalBlockContentSize,
) -> PhysicalContentHeight {
if self.axes.swaps_physical_axes() {
PhysicalContentHeight::new(inline.content_box_length())
} else {
PhysicalContentHeight::new(block.content_box_length())
}
}
pub(in crate::layout) fn logical_block_from_physical_content_sizes(
self,
width: PhysicalContentWidth,
height: PhysicalContentHeight,
) -> LogicalBlockContentSize {
if self.axes.swaps_physical_axes() {
LogicalBlockContentSize::new(width.content_box_length())
} else {
LogicalBlockContentSize::new(height.content_box_length())
}
}
pub(super) fn rect_from_logical(
self,
containing: ContainerRect,
logical: LogicalRect,
) -> ContainerRect {
let size = self.physical_size_from_logical(logical.size);
let inline_origin = self.physical_axis_origin(
containing,
self.inline_start_side(),
logical.origin.inline,
logical.size.inline,
);
let block_origin = self.physical_axis_origin(
containing,
self.axes.physical_side(LogicalSide::BlockStart),
logical.origin.block,
logical.size.block,
);
let (x, y) = self.axes.physical_size(inline_origin, block_origin);
let origin = ContainerPoint::new(x, y);
ContainerRect::new(origin, size)
}
fn physical_axis_origin(
self,
containing: ContainerRect,
side: PhysicalSide,
start: f32,
size: f32,
) -> f32 {
match side {
PhysicalSide::Left => containing.origin.x + start,
PhysicalSide::Right => containing.origin.x + containing.size.width - start - size,
PhysicalSide::Top => containing.origin.y + start,
PhysicalSide::Bottom => containing.origin.y + containing.size.height - start - size,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rect(width: f32, height: f32) -> ContainerRect {
ContainerRect::new(
ContainerPoint::new(0.0, 0.0),
ContainerSize::new(width, height),
)
}
#[test]
fn maps_horizontal_ltr_and_rtl_inline_coordinates() {
let ltr = FlowAxes::new(WritingMode::HorizontalTb, Direction::Ltr);
let rtl = FlowAxes::new(WritingMode::HorizontalTb, Direction::Rtl);
let logical = LogicalRect {
origin: LogicalPoint {
inline: 10.0,
block: 5.0,
},
size: LogicalSize {
inline: 30.0,
block: 20.0,
},
};
assert_eq!(
ltr.rect_from_logical(rect(100.0, 80.0), logical),
ContainerRect::new(
ContainerPoint::new(10.0, 5.0),
ContainerSize::new(30.0, 20.0)
)
);
assert_eq!(
rtl.rect_from_logical(rect(100.0, 80.0), logical),
ContainerRect::new(
ContainerPoint::new(60.0, 5.0),
ContainerSize::new(30.0, 20.0)
)
);
}
#[test]
fn localizes_continuous_block_offsets_in_each_writing_mode() {
let offset = layout_pt(12.0);
for (writing_mode, expected) in [
(WritingMode::HorizontalTb, PaintTranslation::new(0.0, 12.0)),
(WritingMode::VerticalLr, PaintTranslation::new(-12.0, 0.0)),
(WritingMode::VerticalRl, PaintTranslation::new(12.0, 0.0)),
(WritingMode::SidewaysLr, PaintTranslation::new(-12.0, 0.0)),
(WritingMode::SidewaysRl, PaintTranslation::new(12.0, 0.0)),
] {
assert_eq!(
FlowAxes::new(writing_mode, Direction::Ltr)
.continuous_block_to_local_paint_translation(offset),
expected,
"{writing_mode:?} must restore a later source slice to local paint coordinates",
);
}
}
#[test]
fn maps_all_writing_modes_to_physical_rects() {
let logical = LogicalRect {
origin: LogicalPoint {
inline: 10.0,
block: 5.0,
},
size: LogicalSize {
inline: 30.0,
block: 20.0,
},
};
for (writing_mode, direction, expected) in [
(
WritingMode::HorizontalTb,
Direction::Ltr,
ContainerRect::new(
ContainerPoint::new(10.0, 5.0),
ContainerSize::new(30.0, 20.0),
),
),
(
WritingMode::HorizontalTb,
Direction::Rtl,
ContainerRect::new(
ContainerPoint::new(60.0, 5.0),
ContainerSize::new(30.0, 20.0),
),
),
(
WritingMode::VerticalRl,
Direction::Ltr,
ContainerRect::new(
ContainerPoint::new(75.0, 10.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::VerticalRl,
Direction::Rtl,
ContainerRect::new(
ContainerPoint::new(75.0, 40.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::VerticalLr,
Direction::Ltr,
ContainerRect::new(
ContainerPoint::new(5.0, 10.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::VerticalLr,
Direction::Rtl,
ContainerRect::new(
ContainerPoint::new(5.0, 40.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::SidewaysRl,
Direction::Ltr,
ContainerRect::new(
ContainerPoint::new(75.0, 10.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::SidewaysRl,
Direction::Rtl,
ContainerRect::new(
ContainerPoint::new(75.0, 40.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::SidewaysLr,
Direction::Ltr,
ContainerRect::new(
ContainerPoint::new(5.0, 40.0),
ContainerSize::new(20.0, 30.0),
),
),
(
WritingMode::SidewaysLr,
Direction::Rtl,
ContainerRect::new(
ContainerPoint::new(5.0, 10.0),
ContainerSize::new(20.0, 30.0),
),
),
] {
assert_eq!(
FlowAxes::new(writing_mode, direction)
.rect_from_logical(rect(100.0, 80.0), logical),
expected,
"{writing_mode:?} {direction:?}"
);
}
}
#[test]
fn projects_logical_content_sizes_to_physical_axes() {
let inline = LogicalInlineContentSize::new(content_box_pt(40.0));
let block = LogicalBlockContentSize::new(content_box_pt(20.0));
for (writing_mode, width, height) in [
(WritingMode::HorizontalTb, 40.0, 20.0),
(WritingMode::VerticalRl, 20.0, 40.0),
(WritingMode::VerticalLr, 20.0, 40.0),
(WritingMode::SidewaysRl, 20.0, 40.0),
(WritingMode::SidewaysLr, 20.0, 40.0),
] {
let axes = FlowAxes::new(writing_mode, Direction::Ltr);
assert_eq!(
axes.physical_width_from_logical_content_sizes(inline, block)
.points(),
width
);
assert_eq!(
axes.physical_height_from_logical_content_sizes(inline, block)
.points(),
height
);
}
}
#[test]
fn page_top_rect_projects_to_bottom_left_paint_geometry() {
let rect = PageTopRect::new(20.0, 180.0, 50.0, 30.0);
assert_eq!(
rect.0,
PageTopEuclidRect::new(
euclid::Point2D::new(20.0, 180.0),
euclid::Size2D::new(50.0, 30.0),
)
);
assert_eq!(rect.bottom_y(), 150.0);
assert_eq!(
rect.paint_rect(),
PaintRect::new(PaintPoint::new(20.0, 150.0), PaintSize::new(50.0, 30.0))
);
assert_eq!(
rect.paint_clip(),
PaintClip::from_paint_rect(paint_space_rect(20.0, 150.0, 50.0, 30.0))
);
}
#[test]
fn paint_rect_inset_respects_upward_paint_coordinates() {
let rect = paint_space_rect(10.0, 20.0, 100.0, 80.0);
let inset = inset_paint_rect(
rect,
css::Edges {
top: 7.0,
right: 11.0,
bottom: 13.0,
left: 17.0,
},
);
assert_eq!(inset, paint_space_rect(27.0, 33.0, 72.0, 60.0));
}
#[test]
fn disjoint_paint_rect_intersection_is_empty_at_shared_corner() {
let intersection = intersect_paint_rect_or_empty(
paint_space_rect(10.0, 20.0, 5.0, 5.0),
paint_space_rect(30.0, 40.0, 5.0, 5.0),
);
assert_eq!(intersection, paint_space_rect(30.0, 40.0, 0.0, 0.0));
}
#[test]
fn grid_rect_projects_local_downward_y_to_page_top_geometry() {
let rect = GridRect::new(GridPoint::new(15.0, 40.0), GridSize::new(60.0, 25.0));
assert_eq!(
grid_rect_to_page_top_rect(
rect,
PageTopPoint::from_inline_x_and_block_position(
100.0,
PageTopBlockPosition::new(300.0),
),
),
PageTopRect::new(115.0, 260.0, 60.0, 25.0)
);
}
#[test]
fn page_top_block_position_keeps_block_end_motion_explicit() {
let top = PageTopBlockPosition::new(300.0);
let bottom = top.toward_block_end(layout_pt(25.0));
assert_eq!(bottom, PageTopBlockPosition::new(275.0));
assert_eq!(top.block_extent_to(bottom), layout_pt(25.0));
}
}