use super::*;
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ClipPath {
None,
Polygon(Vec<ClipPathPolygonPoint>),
Inset {
top: ComputedLengthPercentage,
right: ComputedLengthPercentage,
bottom: ComputedLengthPercentage,
left: ComputedLengthPercentage,
},
Shape,
Url,
}
impl ClipPath {
pub(crate) fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
match self {
Self::Polygon(points) => {
for point in points {
point.x.resolve_root_font_metric_lengths(basis);
point.y.resolve_root_font_metric_lengths(basis);
}
}
Self::Inset {
top,
right,
bottom,
left,
} => {
for value in [top, right, bottom, left] {
value.resolve_root_font_metric_lengths(basis);
}
}
Self::None | Self::Shape | Self::Url => {}
}
}
pub(crate) fn requires_root_font_metrics(&self) -> bool {
match self {
Self::Polygon(points) => points.iter().any(|point| {
point.x.requires_root_font_metrics() || point.y.requires_root_font_metrics()
}),
Self::Inset {
top,
right,
bottom,
left,
} => [top, right, bottom, left]
.into_iter()
.any(|value| value.requires_root_font_metrics()),
Self::None | Self::Shape | Self::Url => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum LegacyClip {
Auto,
Rect([LegacyClipEdge; 4]),
}
impl LegacyClip {
pub(crate) const AUTO: Self = Self::Auto;
pub(crate) fn forces_flattening(&self) -> bool {
!matches!(self, Self::Auto)
}
pub(crate) fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
let Self::Rect(edges) = self else {
return;
};
for edge in edges {
if let LegacyClipEdge::Length(value) = edge {
value.resolve_root_font_metric_lengths(basis);
}
}
}
pub(crate) fn requires_root_font_metrics(&self) -> bool {
matches!(self, Self::Rect(edges) if edges.iter().any(|edge| {
matches!(edge, LegacyClipEdge::Length(value) if value.requires_root_font_metrics())
}))
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum LegacyClipEdge {
Auto,
Length(ComputedLengthPercentage),
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ClipPathPolygonPoint {
pub(crate) x: ComputedLengthPercentage,
pub(crate) y: ComputedLengthPercentage,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ShapeOutside {
None,
Box(ShapeBox),
Image(BackgroundImage),
Basic {
shape: BasicShape,
reference_box: ShapeBox,
},
}
impl ShapeOutside {
pub(crate) const NONE: Self = Self::None;
pub(crate) fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
match self {
Self::Image(image) => image.resolve_root_font_metric_lengths(basis),
Self::Basic { shape, .. } => shape.resolve_root_font_metric_lengths(basis),
Self::None | Self::Box(_) => {}
}
}
pub(crate) fn requires_root_font_metrics(&self) -> bool {
match self {
Self::Image(image) => image.requires_root_font_metrics(),
Self::Basic { shape, .. } => shape.requires_root_font_metrics(),
Self::None | Self::Box(_) => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ShapeBox {
Margin,
Border,
Padding,
Content,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BasicShape {
Inset(ShapeInset),
Circle(ShapeCircle),
Ellipse(ShapeEllipse),
Polygon(ShapePolygon),
Path(ShapePath),
Shape(ShapeFunction),
}
impl BasicShape {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
match self {
Self::Inset(shape) => {
for value in [
&mut shape.top,
&mut shape.right,
&mut shape.bottom,
&mut shape.left,
] {
value.resolve_root_font_metric_lengths(basis);
}
shape.radii.resolve_root_font_metric_lengths(basis);
}
Self::Circle(shape) => {
shape.radius.resolve_root_font_metric_lengths(basis);
shape.position.resolve_root_font_metric_lengths(basis);
}
Self::Ellipse(shape) => {
shape
.horizontal_radius
.resolve_root_font_metric_lengths(basis);
shape
.vertical_radius
.resolve_root_font_metric_lengths(basis);
shape.position.resolve_root_font_metric_lengths(basis);
}
Self::Polygon(shape) => {
for point in &mut shape.vertices {
point.x.resolve_root_font_metric_lengths(basis);
point.y.resolve_root_font_metric_lengths(basis);
}
}
Self::Path(_) => {}
Self::Shape(shape) => shape.resolve_root_font_metric_lengths(basis),
}
}
fn requires_root_font_metrics(&self) -> bool {
match self {
Self::Inset(shape) => {
[&shape.top, &shape.right, &shape.bottom, &shape.left]
.into_iter()
.any(|value| value.requires_root_font_metrics())
|| shape.radii.requires_root_font_metrics()
}
Self::Circle(shape) => {
shape.radius.requires_root_font_metrics()
|| shape.position.requires_root_font_metrics()
}
Self::Ellipse(shape) => {
shape.horizontal_radius.requires_root_font_metrics()
|| shape.vertical_radius.requires_root_font_metrics()
|| shape.position.requires_root_font_metrics()
}
Self::Polygon(shape) => shape.vertices.iter().any(|point| {
point.x.requires_root_font_metrics() || point.y.requires_root_font_metrics()
}),
Self::Path(_) => false,
Self::Shape(shape) => shape.requires_root_font_metrics(),
}
}
}
impl ShapeCircleRadius {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
if let Self::LengthPercentage(value) = self {
value.resolve_root_font_metric_lengths(basis);
}
}
fn requires_root_font_metrics(&self) -> bool {
matches!(self, Self::LengthPercentage(value) if value.requires_root_font_metrics())
}
}
impl ShapeEllipseRadius {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
if let Self::LengthPercentage(value) = self {
value.resolve_root_font_metric_lengths(basis);
}
}
fn requires_root_font_metrics(&self) -> bool {
matches!(self, Self::LengthPercentage(value) if value.requires_root_font_metrics())
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapeInset {
pub(crate) top: ComputedLengthPercentage,
pub(crate) right: ComputedLengthPercentage,
pub(crate) bottom: ComputedLengthPercentage,
pub(crate) left: ComputedLengthPercentage,
pub(crate) radii: BorderRadius,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapeCircle {
pub(crate) radius: ShapeCircleRadius,
pub(crate) position: ShapePosition,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ShapeCircleRadius {
LengthPercentage(ComputedLengthPercentage),
ClosestSide,
FarthestSide,
ClosestCorner,
FarthestCorner,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapeEllipse {
pub(crate) horizontal_radius: ShapeEllipseRadius,
pub(crate) vertical_radius: ShapeEllipseRadius,
pub(crate) position: ShapePosition,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ShapeEllipseRadius {
LengthPercentage(ComputedLengthPercentage),
ClosestSide,
FarthestSide,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapePolygon {
pub(crate) fill_rule: ShapeFillRule,
pub(crate) vertices: Vec<ShapePolygonPoint>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ShapeFillRule {
NonZero,
EvenOdd,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapePolygonPoint {
pub(crate) x: ComputedLengthPercentage,
pub(crate) y: ComputedLengthPercentage,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapePath {
pub(crate) fill_rule: ShapeFillRule,
pub(crate) data: String,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapeFunction {
pub(crate) fill_rule: ShapeFillRule,
pub(crate) origin: ShapePosition,
pub(crate) commands: Vec<ShapeCommand>,
}
impl ShapeFunction {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
self.origin.resolve_root_font_metric_lengths(basis);
for command in &mut self.commands {
command.resolve_root_font_metric_lengths(basis);
}
}
fn requires_root_font_metrics(&self) -> bool {
self.origin.requires_root_font_metrics()
|| self
.commands
.iter()
.any(ShapeCommand::requires_root_font_metrics)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ShapeCommandDirection {
To,
By,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ShapeCommand {
Move {
direction: ShapeCommandDirection,
point: ShapePosition,
},
Line {
direction: ShapeCommandDirection,
point: ShapePosition,
},
HorizontalLine {
direction: ShapeCommandDirection,
value: ComputedLengthPercentage,
},
VerticalLine {
direction: ShapeCommandDirection,
value: ComputedLengthPercentage,
},
Curve {
direction: ShapeCommandDirection,
point: ShapePosition,
control_start: ShapePosition,
control_end: ShapePosition,
},
Smooth {
direction: ShapeCommandDirection,
point: ShapePosition,
control_end: ShapePosition,
},
Arc {
direction: ShapeCommandDirection,
point: ShapePosition,
radius_x: ComputedLengthPercentage,
radius_y: ComputedLengthPercentage,
rotation_radians: f32,
large: bool,
clockwise: bool,
},
Close,
}
impl ShapeCommand {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
let resolve_point =
|point: &mut ShapePosition| point.resolve_root_font_metric_lengths(basis);
match self {
Self::Move { point, .. } | Self::Line { point, .. } => resolve_point(point),
Self::HorizontalLine { value, .. } | Self::VerticalLine { value, .. } => {
value.resolve_root_font_metric_lengths(basis)
}
Self::Curve {
point,
control_start,
control_end,
..
} => {
resolve_point(point);
resolve_point(control_start);
resolve_point(control_end);
}
Self::Smooth {
point, control_end, ..
} => {
resolve_point(point);
resolve_point(control_end);
}
Self::Arc {
point,
radius_x,
radius_y,
..
} => {
resolve_point(point);
radius_x.resolve_root_font_metric_lengths(basis);
radius_y.resolve_root_font_metric_lengths(basis);
}
Self::Close => {}
}
}
fn requires_root_font_metrics(&self) -> bool {
let point_requires = |point: &ShapePosition| point.requires_root_font_metrics();
match self {
Self::Move { point, .. } | Self::Line { point, .. } => point_requires(point),
Self::HorizontalLine { value, .. } | Self::VerticalLine { value, .. } => {
value.requires_root_font_metrics()
}
Self::Curve {
point,
control_start,
control_end,
..
} => {
point_requires(point)
|| point_requires(control_start)
|| point_requires(control_end)
}
Self::Smooth {
point, control_end, ..
} => point_requires(point) || point_requires(control_end),
Self::Arc {
point,
radius_x,
radius_y,
..
} => {
point_requires(point)
|| radius_x.requires_root_font_metrics()
|| radius_y.requires_root_font_metrics()
}
Self::Close => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ShapePosition {
pub(crate) x: ComputedLengthPercentage,
pub(crate) y: ComputedLengthPercentage,
}
impl ShapePosition {
pub(crate) fn center() -> Self {
Self {
x: ComputedLengthPercentage::from_percent(0.5),
y: ComputedLengthPercentage::from_percent(0.5),
}
}
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
self.x.resolve_root_font_metric_lengths(basis);
self.y.resolve_root_font_metric_lengths(basis);
}
fn requires_root_font_metrics(&self) -> bool {
self.x.requires_root_font_metrics() || self.y.requires_root_font_metrics()
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BorderShape {
None,
Circle(BorderShapeCircle),
Ellipse(BorderShapeEllipse),
Path(BorderShapePath),
Inset(BorderShapeInset),
Polygon(BorderShapePolygon),
Pair {
outer: Box<BorderShape>,
inner: Box<BorderShape>,
},
}
impl BorderShape {
pub(crate) fn set_geometry_box(&mut self, geometry_box: BorderShapeGeometryBox) -> Option<()> {
match self {
Self::Circle(shape) => shape.geometry_box = geometry_box,
Self::Ellipse(shape) => shape.geometry_box = geometry_box,
Self::Path(shape) => shape.geometry_box = geometry_box,
Self::Inset(shape) => shape.geometry_box = geometry_box,
Self::Polygon(shape) => shape.geometry_box = geometry_box,
Self::None | Self::Pair { .. } => return None,
}
Some(())
}
pub(crate) fn replace_half_border_box(&mut self, geometry_box: BorderShapeGeometryBox) {
let current = match self {
Self::Circle(shape) => &mut shape.geometry_box,
Self::Ellipse(shape) => &mut shape.geometry_box,
Self::Path(shape) => &mut shape.geometry_box,
Self::Inset(shape) => &mut shape.geometry_box,
Self::Polygon(shape) => &mut shape.geometry_box,
Self::None | Self::Pair { .. } => return,
};
if *current == BorderShapeGeometryBox::HalfBorder {
*current = geometry_box;
}
}
pub(crate) fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
match self {
Self::Circle(shape) => {
shape.radius.resolve_root_font_metric_lengths(basis);
shape.position.resolve_root_font_metric_lengths(basis);
}
Self::Ellipse(shape) => {
shape
.horizontal_radius
.resolve_root_font_metric_lengths(basis);
shape
.vertical_radius
.resolve_root_font_metric_lengths(basis);
shape.position.resolve_root_font_metric_lengths(basis);
}
Self::Path(shape) => {
for vertex in &mut shape.vertices {
vertex.resolve_root_font_metric_lengths(basis);
}
}
Self::Inset(shape) => {
for value in [
&mut shape.top,
&mut shape.right,
&mut shape.bottom,
&mut shape.left,
] {
value.resolve_root_font_metric_lengths(basis);
}
if let Some(radius) = &mut shape.corner_radius {
radius.resolve_root_font_metric_lengths(basis);
}
}
Self::Polygon(shape) => {
for vertex in &mut shape.vertices {
vertex.resolve_root_font_metric_lengths(basis);
}
}
Self::Pair { outer, inner } => {
outer.resolve_root_font_metric_lengths(basis);
inner.resolve_root_font_metric_lengths(basis);
}
Self::None => {}
}
}
pub(crate) fn requires_root_font_metrics(&self) -> bool {
match self {
Self::Circle(shape) => {
shape.radius.requires_root_font_metrics()
|| shape.position.requires_root_font_metrics()
}
Self::Ellipse(shape) => {
shape.horizontal_radius.requires_root_font_metrics()
|| shape.vertical_radius.requires_root_font_metrics()
|| shape.position.requires_root_font_metrics()
}
Self::Path(shape) => shape
.vertices
.iter()
.any(BorderShapePosition::requires_root_font_metrics),
Self::Polygon(shape) => shape
.vertices
.iter()
.any(BorderShapePosition::requires_root_font_metrics),
Self::Inset(shape) => {
[&shape.top, &shape.right, &shape.bottom, &shape.left]
.into_iter()
.any(|value| value.requires_root_font_metrics())
|| shape
.corner_radius
.as_ref()
.is_some_and(ComputedLengthPercentage::requires_root_font_metrics)
}
Self::Pair { outer, inner } => {
outer.requires_root_font_metrics() || inner.requires_root_font_metrics()
}
Self::None => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapePath {
pub(crate) vertices: Vec<BorderShapePosition>,
pub(crate) geometry_box: BorderShapeGeometryBox,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapeInset {
pub(crate) top: ComputedLengthPercentage,
pub(crate) right: ComputedLengthPercentage,
pub(crate) bottom: ComputedLengthPercentage,
pub(crate) left: ComputedLengthPercentage,
pub(crate) corner_radius: Option<ComputedLengthPercentage>,
pub(crate) geometry_box: BorderShapeGeometryBox,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapePolygon {
pub(crate) vertices: Vec<BorderShapePosition>,
pub(crate) geometry_box: BorderShapeGeometryBox,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapeCircle {
pub(crate) radius: BorderShapeCircleRadius,
pub(crate) position: BorderShapePosition,
pub(crate) geometry_box: BorderShapeGeometryBox,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BorderShapeCircleRadius {
LengthPercentage(ComputedLengthPercentage),
ClosestSide,
FarthestSide,
ClosestCorner,
FarthestCorner,
}
impl BorderShapeCircleRadius {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
if let Self::LengthPercentage(value) = self {
value.resolve_root_font_metric_lengths(basis);
}
}
fn requires_root_font_metrics(&self) -> bool {
matches!(self, Self::LengthPercentage(value) if value.requires_root_font_metrics())
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapeEllipse {
pub(crate) horizontal_radius: BorderShapeEllipseRadius,
pub(crate) vertical_radius: BorderShapeEllipseRadius,
pub(crate) position: BorderShapePosition,
pub(crate) geometry_box: BorderShapeGeometryBox,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BorderShapeEllipseRadius {
LengthPercentage(ComputedLengthPercentage),
ClosestSide,
FarthestSide,
ClosestCorner,
FarthestCorner,
}
impl BorderShapeEllipseRadius {
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
if let Self::LengthPercentage(value) = self {
value.resolve_root_font_metric_lengths(basis);
}
}
fn requires_root_font_metrics(&self) -> bool {
matches!(self, Self::LengthPercentage(value) if value.requires_root_font_metrics())
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct BorderShapePosition {
pub(crate) x: ComputedLengthPercentage,
pub(crate) y: ComputedLengthPercentage,
}
impl BorderShapePosition {
pub(crate) fn center() -> Self {
Self {
x: ComputedLengthPercentage::from_percent(0.5),
y: ComputedLengthPercentage::from_percent(0.5),
}
}
fn resolve_root_font_metric_lengths(&mut self, basis: RootFontMetricLengthBasis) {
self.x.resolve_root_font_metric_lengths(basis);
self.y.resolve_root_font_metric_lengths(basis);
}
fn requires_root_font_metrics(&self) -> bool {
self.x.requires_root_font_metrics() || self.y.requires_root_font_metrics()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BorderShapeGeometryBox {
Border,
Padding,
Content,
Margin,
HalfBorder,
}