use super::{
CollapsedBorderSegment, ComputedStyle, ContainerPoint, ContainerRect, ContainingBlock,
ContentBoxLength, Direction, FlowAxes, LayoutLength, LogicalAxis, LogicalBlockContentSize,
LogicalInlineContentSize, LogicalPoint, LogicalRect, LogicalSide, LogicalSize,
NonContentLength, OverflowClip, PageTopPoint, PageTopRect, PercentageBasis, PhysicalAxis,
PhysicalContentWidth, PhysicalSide, SemanticLengthExt, TableCellPlacement, WritingMode,
WritingModeAxes, constrain_content_height, constrain_content_width, content_box_pt, css,
non_content_pt,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum TableGridSpace {}
pub(super) type TableGridLength = euclid::Length<f32, TableGridSpace>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout::table) struct TableCellPadding(LayoutLength);
impl TableCellPadding {
pub(in crate::layout::table) fn new(value: LayoutLength) -> Self {
Self(value)
}
pub(in crate::layout::table) fn points(self) -> f32 {
self.0.points()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout::table) struct TableCellBaselineOffset(LayoutLength);
impl TableCellBaselineOffset {
pub(in crate::layout::table) fn new(value: LayoutLength) -> Self {
Self(value)
}
pub(in crate::layout::table) fn points(self) -> f32 {
self.0.points()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout::table) struct TableRowBaselineOffset(LayoutLength);
impl TableRowBaselineOffset {
pub(in crate::layout::table) fn new(value: LayoutLength) -> Self {
Self(value)
}
pub(in crate::layout::table) fn points(self) -> f32 {
self.0.points()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout::table) struct TableCellOuterBlockSize(LayoutLength);
impl TableCellOuterBlockSize {
pub(in crate::layout::table) fn new(value: LayoutLength) -> Self {
Self(value)
}
pub(in crate::layout::table) fn points(self) -> f32 {
self.0.points()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(super) struct TableGridBorderWidth(TableGridLength);
impl TableGridBorderWidth {
pub(super) fn new(value: TableGridLength) -> Self {
Self(value)
}
pub(super) fn points(self) -> f32 {
self.0.get()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(super) struct TableGridInlineOffset(TableGridLength);
impl TableGridInlineOffset {
pub(super) fn new(value: TableGridLength) -> Self {
Self(value)
}
pub(super) fn length(self) -> TableGridLength {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(super) struct TableGridBlockOffset(TableGridLength);
impl TableGridBlockOffset {
pub(super) fn new(value: TableGridLength) -> Self {
Self(value)
}
pub(super) fn length(self) -> TableGridLength {
self.0
}
}
pub(super) type TableGridPoint = euclid::Point2D<f32, TableGridSpace>;
pub(super) type TableGridSize = euclid::Size2D<f32, TableGridSpace>;
pub(super) type TableGridRect = euclid::Rect<f32, TableGridSpace>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout::table) struct TableGridContentBoxTopLeft(PageTopPoint);
impl TableGridContentBoxTopLeft {
pub(in crate::layout::table) fn new(point: PageTopPoint) -> Self {
Self(point)
}
pub(in crate::layout::table) fn page_top_point(self) -> PageTopPoint {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableGridLogicalSize {
inline: LogicalInlineContentSize,
block: LogicalBlockContentSize,
}
impl TableGridLogicalSize {
pub(super) fn new(inline: LogicalInlineContentSize, block: LogicalBlockContentSize) -> Self {
Self { inline, block }
}
pub(super) fn inline(self) -> LogicalInlineContentSize {
self.inline
}
pub(super) fn block(self) -> LogicalBlockContentSize {
self.block
}
pub(super) fn physical_width(self, axes: TableAxes) -> PhysicalContentWidth {
let physical = axes.flow.physical_size_from_logical(LogicalSize {
inline: self.inline.points(),
block: self.block.points(),
});
PhysicalContentWidth::new(content_box_pt(physical.width))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct TableAxes {
pub(super) flow: FlowAxes,
pub(super) direction: Direction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum TableGridEdge {
InlineStart,
InlineEnd,
BlockStart,
BlockEnd,
}
impl TableAxes {
pub(super) fn for_style(style: &ComputedStyle) -> Self {
Self {
flow: FlowAxes::for_style(style),
direction: style.used_direction(),
}
}
pub(super) fn grid_edge_for_physical_side(self, side: PhysicalSide) -> TableGridEdge {
let axes = WritingModeAxes::new(self.flow.writing_mode(), self.direction);
for (logical_side, grid_edge) in [
(LogicalSide::InlineStart, TableGridEdge::InlineStart),
(LogicalSide::InlineEnd, TableGridEdge::InlineEnd),
(LogicalSide::BlockStart, TableGridEdge::BlockStart),
(LogicalSide::BlockEnd, TableGridEdge::BlockEnd),
] {
if axes.physical_side(logical_side) == side {
return grid_edge;
}
}
unreachable!("writing-mode side mapping must be bijective")
}
pub(super) fn physical_side_for_grid_edge(self, edge: TableGridEdge) -> PhysicalSide {
let logical_side = match edge {
TableGridEdge::InlineStart => LogicalSide::InlineStart,
TableGridEdge::InlineEnd => LogicalSide::InlineEnd,
TableGridEdge::BlockStart => LogicalSide::BlockStart,
TableGridEdge::BlockEnd => LogicalSide::BlockEnd,
};
WritingModeAxes::new(self.flow.writing_mode(), self.direction).physical_side(logical_side)
}
#[cfg(test)]
pub(super) const fn for_direction(direction: Direction) -> Self {
Self {
flow: FlowAxes::new(WritingMode::HorizontalTb, direction),
direction,
}
}
pub(super) fn boundary_x(
self,
total_width: TableGridLength,
logical_boundary_x: TableGridLength,
) -> TableGridLength {
match self.direction {
Direction::Ltr => logical_boundary_x,
Direction::Rtl => total_width - logical_boundary_x,
}
}
pub(super) fn span_start_x(
self,
total_width: TableGridLength,
logical_start_x: TableGridLength,
logical_end_x: TableGridLength,
) -> TableGridLength {
self.boundary_x(total_width, logical_start_x)
.min(self.boundary_x(total_width, logical_end_x))
}
pub(super) fn physical_rect_from_logical_grid(
self,
logical_inline_start: f32,
logical_block_start: f32,
logical_inline_size: f32,
logical_block_size: f32,
logical_inline_extent: f32,
logical_block_extent: f32,
) -> ContainerRect {
let axes = FlowAxes::new(self.flow.writing_mode(), Direction::Ltr);
axes.rect_from_logical(
ContainerRect::new(
ContainerPoint::new(0.0, 0.0),
axes.physical_size_from_logical(LogicalSize {
inline: logical_inline_extent.max(0.0),
block: logical_block_extent.max(0.0),
}),
),
LogicalRect {
origin: LogicalPoint {
inline: logical_inline_start,
block: logical_block_start,
},
size: LogicalSize {
inline: logical_inline_size.max(0.0),
block: logical_block_size.max(0.0),
},
},
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum TableRootTrackAxis {
Inline,
Block,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct TableInlineTrackSizing {
physical_axis: PhysicalAxis,
}
impl TableInlineTrackSizing {
pub(super) fn for_table(table_style: &ComputedStyle) -> Self {
Self {
physical_axis: TableCellAxisAdapter::for_table(table_style)
.root_track_physical_axis(TableRootTrackAxis::Inline),
}
}
pub(super) fn uses_physical_width(self) -> bool {
self.physical_axis == PhysicalAxis::Horizontal
}
pub(super) fn declared_size(
self,
style: &ComputedStyle,
) -> css::ComputedLengthPercentageOrAuto {
if self.uses_physical_width() {
style.box_values.width.clone()
} else {
style.box_values.height.value().clone()
}
}
pub(super) fn parallel_insets(self, edges: css::Edges) -> NonContentLength {
if self.uses_physical_width() {
non_content_pt(edges.left + edges.right)
} else {
non_content_pt(edges.top + edges.bottom)
}
}
pub(super) fn constrain_content_box_size(
self,
style: &ComputedStyle,
value: ContentBoxLength,
percentage_basis: PercentageBasis<LayoutLength>,
) -> ContentBoxLength {
if self.uses_physical_width() {
constrain_content_width(style, value, percentage_basis)
} else {
constrain_content_height(style, value, percentage_basis)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct TableCellAxisAdapter {
table: FlowAxes,
cell: FlowAxes,
}
impl TableCellAxisAdapter {
pub(super) fn for_table(table_style: &ComputedStyle) -> Self {
Self {
table: FlowAxes::for_style(table_style),
cell: FlowAxes::for_style(table_style),
}
}
pub(super) fn for_cell(table_style: &ComputedStyle, cell_style: &ComputedStyle) -> Self {
Self {
table: FlowAxes::for_style(table_style),
cell: FlowAxes::for_style(cell_style),
}
}
pub(super) fn root_track_physical_axis(self, track: TableRootTrackAxis) -> PhysicalAxis {
let logical_axis = match track {
TableRootTrackAxis::Inline => LogicalAxis::Inline,
TableRootTrackAxis::Block => LogicalAxis::Block,
};
WritingModeAxes::new(self.table.writing_mode(), Direction::Ltr).physical_axis(logical_axis)
}
pub(super) fn root_track_uses_physical_width(self, track: TableRootTrackAxis) -> bool {
self.root_track_physical_axis(track) == PhysicalAxis::Horizontal
}
pub(super) fn cell_inline_uses_physical_width(self) -> bool {
WritingModeAxes::new(self.cell.writing_mode(), Direction::Ltr)
.physical_axis(LogicalAxis::Inline)
== PhysicalAxis::Horizontal
}
pub(super) fn content_geometry(
self,
content_box: TableCellContentBox,
block_offset_toward_end: f32,
) -> TableCellContentGeometry {
let rect = content_box.page_top_rect();
let (x_offset, top_offset) =
match WritingModeAxes::new(self.cell.writing_mode(), Direction::Ltr)
.physical_side(LogicalSide::BlockStart)
{
PhysicalSide::Top => (0.0, -block_offset_toward_end),
PhysicalSide::Bottom => (0.0, block_offset_toward_end),
PhysicalSide::Left => (block_offset_toward_end, 0.0),
PhysicalSide::Right => (-block_offset_toward_end, 0.0),
};
let rect = PageTopRect::new(
rect.x() + x_offset,
rect.top_y() + top_offset,
rect.width(),
rect.height(),
);
let inline_size = if self.cell_inline_uses_physical_width() {
rect.width()
} else {
rect.height()
};
let block_size = if self.cell_inline_uses_physical_width() {
rect.height()
} else {
rect.width()
};
TableCellContentGeometry {
content_box: TableCellContentBox::from_page_top_rect(rect),
inline_size: LogicalInlineContentSize::new(content_box_pt(inline_size.max(0.0))),
block_size: LogicalBlockContentSize::new(content_box_pt(block_size.max(0.0))),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct TableGridArea {
pub(super) row: usize,
pub(super) column: usize,
pub(super) rowspan: usize,
pub(super) colspan: usize,
}
impl TableGridArea {
pub(super) fn from_placement(row: usize, placement: &TableCellPlacement) -> Self {
Self {
row,
column: placement.column,
rowspan: placement.rowspan.max(1),
colspan: placement.colspan.max(1),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableInlineBounds {
pub(super) start: TableGridLength,
pub(super) size: TableGridLength,
}
impl TableInlineBounds {
pub(super) fn new(start: TableGridLength, size: TableGridLength) -> Self {
Self {
start,
size: size.max(TableGridLength::new(0.0)),
}
}
pub(super) fn logical_start(self) -> TableGridLength {
self.start
}
pub(super) fn logical_size(self) -> TableGridLength {
self.size
}
pub(super) fn rect_for_block(self, block: TableRowBounds) -> TableGridRect {
TableGridRect::new(
TableGridPoint::from_lengths(self.start, TableGridLength::new(block.start)),
TableGridSize::from_lengths(self.size, TableGridLength::new(block.size)),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableRowBounds {
pub(super) start: f32,
pub(super) size: f32,
}
impl TableRowBounds {
pub(super) fn new(start: f32, size: f32) -> Self {
Self {
start,
size: size.max(0.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableCellBorderBox {
rect: TableGridRect,
}
impl TableCellBorderBox {
pub(super) fn from_bounds(inline: TableInlineBounds, row: TableRowBounds) -> Self {
Self {
rect: inline.rect_for_block(row),
}
}
pub(super) fn rect(self) -> TableGridRect {
self.rect
}
pub(super) fn logical_inline_size(self) -> TableGridLength {
TableGridLength::new(self.rect.size.width)
}
pub(super) fn x(self, placement: TableGridPlacement) -> f32 {
placement.page_top_rect_for(self.rect).x()
}
pub(super) fn top_y(self, placement: TableGridPlacement) -> f32 {
placement.page_top_rect_for(self.rect).top_y()
}
pub(super) fn page_top_rect(self, placement: TableGridPlacement) -> PageTopRect {
placement.page_top_rect_for(self.rect)
}
pub(super) fn content_box(
self,
placement: TableGridPlacement,
padding: css::Edges,
borders: css::Edges,
) -> TableCellContentBox {
let border_box = placement.page_top_rect_for(self.rect);
let x = border_box.x() + borders.left + padding.left;
let right = border_box.x() + border_box.width() - borders.right - padding.right;
let top_y = border_box.top_y() - borders.top - padding.top;
let height =
(border_box.height() - borders.top - borders.bottom - padding.top - padding.bottom)
.max(0.0);
TableCellContentBox {
rect: PageTopRect::new(x, top_y, (right - x).max(0.0), height),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableCellContentBox {
rect: PageTopRect,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableCellContentGeometry {
content_box: TableCellContentBox,
inline_size: LogicalInlineContentSize,
block_size: LogicalBlockContentSize,
}
impl TableCellContentGeometry {
pub(super) fn content_box(self) -> TableCellContentBox {
self.content_box
}
pub(super) fn inline_size(self) -> LogicalInlineContentSize {
self.inline_size
}
pub(super) fn block_size(self) -> LogicalBlockContentSize {
self.block_size
}
}
impl TableCellContentBox {
pub(super) fn from_page_top_rect(rect: PageTopRect) -> Self {
Self { rect }
}
pub(super) fn left(self) -> f32 {
self.rect.x()
}
pub(super) fn right(self) -> f32 {
self.left() + self.width()
}
pub(super) fn top_y(self) -> f32 {
self.rect.top_y()
}
pub(super) fn width(self) -> f32 {
self.rect.width()
}
pub(super) fn height(self) -> f32 {
self.rect.height()
}
pub(super) fn page_top_rect(self) -> PageTopRect {
self.rect
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableGridPlacement {
origin: PageTopPoint,
axes: TableAxes,
logical_size: TableGridLogicalSize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct TableGridFrames {
wrapper_grid: TableGridPlacement,
cell_grid: TableGridPlacement,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout::table) struct TableSourceGridFrame {
grid: TableGridPlacement,
}
impl TableSourceGridFrame {
pub(super) fn new(grid: TableGridPlacement) -> Self {
Self { grid }
}
pub(super) fn grid(self) -> TableGridPlacement {
self.grid
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::layout::table) struct TableDestinationCellGridFrame {
grid: TableGridPlacement,
}
impl TableDestinationCellGridFrame {
fn new(grid: TableGridPlacement) -> Self {
Self { grid }
}
pub(super) fn grid(self) -> TableGridPlacement {
self.grid
}
#[cfg(test)]
pub(in crate::layout::table) fn fixture(grid: TableGridPlacement) -> Self {
Self::new(grid)
}
}
impl TableGridFrames {
pub(super) fn new(
wrapper_grid: TableGridPlacement,
block_edge_spacing: TableGridLength,
) -> Self {
let edge = block_edge_spacing.get().max(0.0);
let cell_grid = if edge == 0.0 {
wrapper_grid
} else {
let origin = if wrapper_grid.writing_mode().has_vertical_lines() {
PageTopPoint::new(wrapper_grid.origin.x() + edge, wrapper_grid.origin.top_y())
} else {
PageTopPoint::new(wrapper_grid.origin.x(), wrapper_grid.origin.top_y() - edge)
};
let block = (wrapper_grid.logical_block_grid_extent().get() - edge * 2.0).max(0.0);
TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(origin),
wrapper_grid.axes,
TableGridLogicalSize::new(
wrapper_grid.logical_size.inline(),
LogicalBlockContentSize::new(content_box_pt(block)),
),
)
};
Self {
wrapper_grid,
cell_grid,
}
}
pub(super) fn from_cell_grid(
cell_grid: TableGridPlacement,
block_edge_spacing: TableGridLength,
) -> Self {
let edge = block_edge_spacing.get().max(0.0);
if edge == 0.0 {
return Self {
wrapper_grid: cell_grid,
cell_grid,
};
}
let origin = if cell_grid.writing_mode().has_vertical_lines() {
PageTopPoint::new(cell_grid.origin.x() - edge, cell_grid.origin.top_y())
} else {
PageTopPoint::new(cell_grid.origin.x(), cell_grid.origin.top_y() + edge)
};
let block = cell_grid.logical_block_grid_extent().get() + edge * 2.0;
let wrapper_grid = TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(origin),
cell_grid.axes,
TableGridLogicalSize::new(
cell_grid.logical_size.inline(),
LogicalBlockContentSize::new(content_box_pt(block)),
),
);
Self {
wrapper_grid,
cell_grid,
}
}
pub(super) fn wrapper_grid(self) -> TableGridPlacement {
self.wrapper_grid
}
pub(super) fn cell_grid(self) -> TableGridPlacement {
self.cell_grid
}
}
#[derive(Debug, Clone, Copy)]
pub(in crate::layout::table) struct TableFragmentainerFrame {
placement: super::layout::TableFragmentainerPlacement,
grids: TableGridFrames,
}
impl TableFragmentainerFrame {
pub(super) fn from_cell_grid(
placement: super::layout::TableFragmentainerPlacement,
cell_grid: TableGridPlacement,
block_edge_spacing: TableGridLength,
) -> Self {
Self {
placement,
grids: TableGridFrames::from_cell_grid(cell_grid, block_edge_spacing),
}
}
pub(super) fn placement(self) -> super::layout::TableFragmentainerPlacement {
self.placement
}
pub(super) fn wrapper_grid(self) -> TableGridPlacement {
self.grids.wrapper_grid()
}
pub(super) fn cell_grid_frame(self) -> TableDestinationCellGridFrame {
TableDestinationCellGridFrame::new(self.grids.cell_grid())
}
}
impl TableGridPlacement {
pub(super) fn new(origin: TableGridContentBoxTopLeft) -> Self {
Self::with_axes(
origin,
TableAxes {
flow: FlowAxes::new(WritingMode::HorizontalTb, Direction::Ltr),
direction: Direction::Ltr,
},
TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(0.0)),
LogicalBlockContentSize::new(content_box_pt(0.0)),
),
)
}
pub(super) fn with_axes(
origin: TableGridContentBoxTopLeft,
axes: TableAxes,
logical_size: TableGridLogicalSize,
) -> Self {
Self {
origin: origin.page_top_point(),
axes,
logical_size,
}
}
pub(super) fn origin(self) -> PageTopPoint {
self.origin
}
pub(super) fn overflow_clip_for(self, rect: TableGridRect) -> OverflowClip {
OverflowClip::from_page_top_rect(self.page_top_rect_for(rect))
}
pub(super) fn page_top_rect_for(self, rect: TableGridRect) -> PageTopRect {
let logical_inline_extent = self.logical_size.inline().points();
let logical_block_extent = self.logical_size.block().points();
let physical = self.axes.physical_rect_from_logical_grid(
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
logical_inline_extent,
logical_block_extent,
);
PageTopRect::new(
self.origin.x() + physical.origin.x,
self.origin.top_y() - physical.origin.y,
physical.size.width,
physical.size.height,
)
}
pub(super) fn project_inline_line(
self,
inline: TableGridInlineOffset,
block_start: TableGridBlockOffset,
block_size: TableGridBlockOffset,
) -> CollapsedBorderSegment {
CollapsedBorderSegment::from_projected_line(self.page_top_rect_for(TableGridRect::new(
TableGridPoint::from_lengths(inline.length(), block_start.length()),
TableGridSize::from_lengths(TableGridLength::new(0.0), block_size.length()),
)))
}
pub(super) fn project_block_line(
self,
inline_start: TableGridInlineOffset,
inline_size: TableGridInlineOffset,
block: TableGridBlockOffset,
) -> CollapsedBorderSegment {
CollapsedBorderSegment::from_projected_line(self.page_top_rect_for(TableGridRect::new(
TableGridPoint::from_lengths(inline_start.length(), block.length()),
TableGridSize::from_lengths(inline_size.length(), TableGridLength::new(0.0)),
)))
}
pub(super) fn block_offset_from_page_top(self, page_top: f32) -> TableGridBlockOffset {
TableGridBlockOffset::new(TableGridLength::new(self.origin.top_y() - page_top))
}
pub(super) fn full_page_top_rect(self) -> PageTopRect {
self.page_top_rect_for(TableGridRect::new(
TableGridPoint::from_lengths(TableGridLength::new(0.0), TableGridLength::new(0.0)),
TableGridSize::from_lengths(
self.logical_size.inline().content_box_length().cast_unit(),
self.logical_size.block().content_box_length().cast_unit(),
),
))
}
pub(super) fn logical_inline_grid_extent(self) -> TableGridLength {
self.logical_size.inline().content_box_length().cast_unit()
}
pub(super) fn logical_block_grid_extent(self) -> TableGridLength {
self.logical_size.block().content_box_length().cast_unit()
}
pub(super) fn writing_mode(self) -> WritingMode {
self.axes.flow.writing_mode()
}
pub(super) fn containing_block_for(
self,
border_box: TableCellBorderBox,
borders: css::Edges,
) -> ContainingBlock {
let border_box = self.page_top_rect_for(border_box.rect());
ContainingBlock::from_page_top_rect(PageTopRect::new(
border_box.x() + borders.left,
border_box.top_y() - borders.top,
border_box.width() - borders.left - borders.right,
border_box.height() - borders.top - borders.bottom,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::css::layout_pt;
use crate::layout::{ContainerSize, paint_space_rect};
#[test]
fn typed_cell_padding_and_baselines_preserve_zero_and_absence() {
let padding = TableCellPadding::new(layout_pt(12.0));
assert_eq!(padding.points(), 12.0);
let absent: Option<TableCellBaselineOffset> = None;
let zero = Some(TableCellBaselineOffset::new(layout_pt(0.0)));
assert_ne!(absent, zero);
assert!(absent.is_none());
}
fn grid_length(value: f32) -> TableGridLength {
TableGridLength::new(value)
}
#[test]
fn maps_ltr_column_span_bounds() {
let axes = TableAxes::for_direction(Direction::Ltr);
assert_eq!(
axes.boundary_x(grid_length(100.0), grid_length(25.0)),
grid_length(25.0)
);
assert_eq!(
axes.span_start_x(grid_length(100.0), grid_length(10.0), grid_length(45.0)),
grid_length(10.0)
);
}
#[test]
fn maps_rtl_boundaries_and_spans_from_the_right() {
let axes = TableAxes::for_direction(Direction::Rtl);
assert_eq!(
axes.boundary_x(grid_length(100.0), grid_length(0.0)),
grid_length(100.0)
);
assert_eq!(
axes.boundary_x(grid_length(100.0), grid_length(25.0)),
grid_length(75.0)
);
assert_eq!(
axes.span_start_x(grid_length(100.0), grid_length(10.0), grid_length(45.0)),
grid_length(55.0)
);
}
#[test]
fn table_grid_edges_round_trip_through_every_root_writing_mode() {
for writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let axes = TableAxes {
flow: FlowAxes::new(writing_mode, direction),
direction,
};
for edge in [
TableGridEdge::InlineStart,
TableGridEdge::InlineEnd,
TableGridEdge::BlockStart,
TableGridEdge::BlockEnd,
] {
let physical = axes.physical_side_for_grid_edge(edge);
assert_eq!(axes.grid_edge_for_physical_side(physical), edge);
}
}
}
}
#[test]
fn projects_vertical_rl_table_slots_from_logical_axes() {
let axes = TableAxes {
flow: FlowAxes::new(WritingMode::VerticalRl, Direction::Ltr),
direction: Direction::Ltr,
};
let rect = axes.physical_rect_from_logical_grid(10.0, 20.0, 30.0, 40.0, 100.0, 200.0);
assert_eq!(rect.origin, ContainerPoint::new(140.0, 10.0));
assert_eq!(rect.size, ContainerSize::new(40.0, 30.0));
}
#[test]
fn projects_vertical_lr_table_slots_from_logical_axes() {
let axes = TableAxes {
flow: FlowAxes::new(WritingMode::VerticalLr, Direction::Ltr),
direction: Direction::Ltr,
};
let rect = axes.physical_rect_from_logical_grid(10.0, 20.0, 30.0, 40.0, 100.0, 200.0);
assert_eq!(rect.origin, ContainerPoint::new(20.0, 10.0));
assert_eq!(rect.size, ContainerSize::new(40.0, 30.0));
}
#[test]
fn preserves_negative_logical_inline_offsets_for_collapsed_rule_extensions() {
let horizontal = TableAxes::for_direction(Direction::Ltr);
let vertical = TableAxes {
flow: FlowAxes::new(WritingMode::VerticalLr, Direction::Rtl),
direction: Direction::Rtl,
};
assert_eq!(
horizontal
.physical_rect_from_logical_grid(-1.5, 0.0, 3.0, 2.0, 20.0, 10.0)
.origin,
ContainerPoint::new(-1.5, 0.0)
);
assert_eq!(
vertical
.physical_rect_from_logical_grid(-1.5, 0.0, 3.0, 2.0, 20.0, 10.0)
.origin,
ContainerPoint::new(0.0, -1.5)
);
}
#[test]
fn table_grid_logical_size_projects_physical_width_by_writing_mode() {
let size = TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(100.0)),
LogicalBlockContentSize::new(content_box_pt(240.0)),
);
let horizontal = TableAxes {
flow: FlowAxes::new(WritingMode::HorizontalTb, Direction::Ltr),
direction: Direction::Ltr,
};
let vertical = TableAxes {
flow: FlowAxes::new(WritingMode::VerticalRl, Direction::Ltr),
direction: Direction::Ltr,
};
assert_eq!(size.physical_width(horizontal).points(), 100.0);
assert_eq!(size.physical_width(vertical).points(), 240.0);
}
#[test]
fn cell_axis_adapter_keeps_root_track_axes_direction_independent() {
for (writing_mode, root_block_axis) in [
(WritingMode::HorizontalTb, PhysicalAxis::Vertical),
(WritingMode::VerticalRl, PhysicalAxis::Horizontal),
(WritingMode::VerticalLr, PhysicalAxis::Horizontal),
(WritingMode::SidewaysRl, PhysicalAxis::Horizontal),
(WritingMode::SidewaysLr, PhysicalAxis::Horizontal),
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let adapter = TableCellAxisAdapter {
table: FlowAxes::new(writing_mode, direction),
cell: FlowAxes::new(WritingMode::HorizontalTb, direction),
};
assert_eq!(
adapter.root_track_physical_axis(TableRootTrackAxis::Block),
root_block_axis
);
}
}
}
#[test]
fn cell_content_geometry_keeps_root_tracks_and_cell_flow_separate() {
let content_box =
TableCellContentBox::from_page_top_rect(PageTopRect::new(10.0, 200.0, 30.0, 40.0));
for root_writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let mut table = ComputedStyle::initial();
table.writing_mode = root_writing_mode;
table.direction = direction;
let mut horizontal_cell = ComputedStyle::initial();
horizontal_cell.writing_mode = WritingMode::HorizontalTb;
horizontal_cell.direction = direction;
let horizontal = TableCellAxisAdapter::for_cell(&table, &horizontal_cell)
.content_geometry(content_box, 5.0);
assert_eq!(horizontal.inline_size().points(), 30.0);
assert_eq!(horizontal.block_size().points(), 40.0);
assert_eq!(horizontal.content_box().top_y(), 195.0);
let mut vertical_cell = ComputedStyle::initial();
vertical_cell.writing_mode = WritingMode::VerticalRl;
vertical_cell.direction = direction;
let vertical = TableCellAxisAdapter::for_cell(&table, &vertical_cell)
.content_geometry(content_box, 5.0);
assert_eq!(vertical.inline_size().points(), 40.0);
assert_eq!(vertical.block_size().points(), 30.0);
assert_eq!(vertical.content_box().left(), 5.0);
}
}
}
#[test]
fn full_grid_projection_is_root_flow_owned_for_every_mode_and_direction() {
for writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let axes = TableAxes {
flow: FlowAxes::new(writing_mode, direction),
direction,
};
let rect = axes.physical_rect_from_logical_grid(0.0, 0.0, 30.0, 40.0, 30.0, 40.0);
assert_eq!(rect.origin, ContainerPoint::new(0.0, 0.0));
assert_eq!(
rect.size,
axes.flow.physical_size_from_logical(LogicalSize {
inline: 30.0,
block: 40.0,
})
);
}
}
}
#[test]
fn projects_row_bounds_to_page_coordinates() {
let placement = TableGridPlacement::new(TableGridContentBoxTopLeft::new(
PageTopPoint::new(20.0, 200.0),
));
let border_box = TableCellBorderBox::from_bounds(
TableInlineBounds::new(grid_length(15.0), grid_length(60.0)),
TableRowBounds::new(25.0, 30.0),
);
assert_eq!(border_box.x(placement), 35.0);
assert_eq!(border_box.top_y(placement), 175.0);
assert_eq!(
border_box.top_y(placement) - border_box.rect().size.height,
145.0
);
assert_eq!(
placement.overflow_clip_for(border_box.rect()),
OverflowClip::from_paint_rect(paint_space_rect(35.0, 145.0, 60.0, 30.0))
);
}
#[test]
fn cell_border_boxes_project_logical_inline_and_block_spans_in_every_flow() {
let inline = TableInlineBounds::new(grid_length(10.0), grid_length(30.0));
let block = TableRowBounds::new(20.0, 40.0);
let border_box = TableCellBorderBox::from_bounds(inline, block);
for writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let axes = TableAxes {
flow: FlowAxes::new(writing_mode, direction),
direction,
};
let placement = TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(PageTopPoint::new(100.0, 200.0)),
axes,
TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(100.0)),
LogicalBlockContentSize::new(content_box_pt(300.0)),
),
);
let expected = placement.page_top_rect_for(inline.rect_for_block(block));
assert_eq!(border_box.page_top_rect(placement), expected);
assert_eq!(
border_box.logical_inline_size(),
inline.logical_size(),
"{writing_mode:?} {direction:?}"
);
}
}
}
#[test]
fn vertical_row_source_rect_keeps_the_unfragmented_logical_block_range() {
let placement = TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(PageTopPoint::new(20.0, 200.0)),
TableAxes {
flow: FlowAxes::new(WritingMode::VerticalRl, Direction::Rtl),
direction: Direction::Rtl,
},
TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(100.0)),
LogicalBlockContentSize::new(content_box_pt(300.0)),
),
);
let source_row = placement.page_top_rect_for(TableGridRect::new(
TableGridPoint::from_lengths(grid_length(0.0), grid_length(120.0)),
TableGridSize::from_lengths(grid_length(100.0), grid_length(60.0)),
));
assert_eq!(source_row, PageTopRect::new(140.0, 200.0, 60.0, 100.0));
}
#[test]
fn grid_frames_remove_block_edge_spacing_once_in_every_root_flow() {
for writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let placement = TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(PageTopPoint::new(20.0, 200.0)),
TableAxes {
flow: FlowAxes::new(writing_mode, direction),
direction,
},
TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(100.0)),
LogicalBlockContentSize::new(content_box_pt(300.0)),
),
);
let frames = TableGridFrames::new(placement, grid_length(10.0));
assert_eq!(frames.wrapper_grid(), placement);
assert_eq!(
frames.cell_grid().logical_size.inline(),
placement.logical_size.inline()
);
assert_eq!(frames.cell_grid().logical_size.block().points(), 280.0);
let expected_origin = if writing_mode.has_vertical_lines() {
PageTopPoint::new(30.0, 200.0)
} else {
PageTopPoint::new(20.0, 190.0)
};
assert_eq!(frames.cell_grid().origin(), expected_origin);
}
}
}
#[test]
fn cell_grid_reconstructs_its_wrapper_frame_without_a_second_rebase() {
for writing_mode in [
WritingMode::HorizontalTb,
WritingMode::VerticalRl,
WritingMode::VerticalLr,
WritingMode::SidewaysRl,
WritingMode::SidewaysLr,
] {
for direction in [Direction::Ltr, Direction::Rtl] {
let wrapper = TableGridPlacement::with_axes(
TableGridContentBoxTopLeft::new(PageTopPoint::new(20.0, 200.0)),
TableAxes {
flow: FlowAxes::new(writing_mode, direction),
direction,
},
TableGridLogicalSize::new(
LogicalInlineContentSize::new(content_box_pt(100.0)),
LogicalBlockContentSize::new(content_box_pt(300.0)),
),
);
let initial = TableGridFrames::new(wrapper, grid_length(10.0));
let reconstructed =
TableGridFrames::from_cell_grid(initial.cell_grid(), grid_length(10.0));
assert_eq!(reconstructed.wrapper_grid(), wrapper);
assert_eq!(reconstructed.cell_grid(), initial.cell_grid());
}
}
}
}