use crate::paint::{
BackgroundImage, BoxArea, BoxPaint, Contour, LengthPercentage, POINTS_PER_PIXEL, Position,
RadialExtent, Radii, Rect, Shape, rounded,
};
use serde::Serialize;
use std::convert::Infallible;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) enum ShapeBox {
Margin,
Visual(BoxArea),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) enum GeometryBox {
Shape(ShapeBox),
Fill,
Stroke,
View,
}
impl GeometryBox {
pub(crate) fn layout(self) -> ShapeBox {
match self {
Self::Shape(reference) => reference,
Self::Fill => ShapeBox::Visual(BoxArea::ContentBox),
Self::Stroke | Self::View => ShapeBox::Visual(BoxArea::BorderBox),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum CircleRadius {
Extent(RadialExtent),
Length(LengthPercentage),
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum AxisRadius {
ClosestSide,
FarthestSide,
Length(LengthPercentage),
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum EllipseRadii {
Extent(RadialExtent),
Axes([AxisRadius; 2]),
}
pub(crate) type CornerRadii = [[LengthPercentage; 2]; 4];
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum BasicShape {
Inset {
insets: [LengthPercentage; 4],
radii: CornerRadii,
},
Rect {
edges: [LengthPercentage; 4],
radii: CornerRadii,
},
Circle {
radius: CircleRadius,
center: Position,
},
Ellipse {
radii: EllipseRadii,
center: Position,
},
Polygon {
even_odd: bool,
points: Vec<[LengthPercentage; 2]>,
},
Path {
even_odd: bool,
contours: Vec<Contour>,
},
}
impl ShapeBox {
pub(crate) fn resolve<I>(self, paint: &BoxPaint<I>, margins: [f32; 4]) -> (Rect, Radii) {
match self {
Self::Margin => (
crate::paint::outset(paint.decoration_rect(), margins),
crate::paint::outset_radii(paint.radii(), margins),
),
Self::Visual(BoxArea::BorderBox) => (paint.decoration_rect(), paint.radii()),
Self::Visual(area) => {
let padding = if area == BoxArea::ContentBox {
paint.padding
} else {
[0.0; 4]
};
let clip = paint.inner_clip(padding, [true; 2]);
(clip.reference_box.unwrap_or(clip.rect), clip.radii)
}
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct ClipPath {
pub shape: Option<BasicShape>,
pub reference: GeometryBox,
}
impl ClipPath {
pub(crate) fn outline<I>(&self, paint: &BoxPaint<I>) -> Shape {
let (rect, radii) = self.reference.layout().resolve(paint, paint.margins);
match &self.shape {
Some(shape) => shape.outline(rect),
None => Shape::Path {
contours: vec![rounded::rounded_rect(rect, radii)],
even_odd: false,
clip: None,
},
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[serde(bound(serialize = ""))]
pub(crate) enum ShapeOutside<I = Infallible> {
#[default]
None,
Box(ShapeBox),
Shape {
shape: BasicShape,
reference: ShapeBox,
},
Image(BackgroundImage<I>),
}
impl ShapeOutside<Infallible> {
pub(crate) fn load_image<I, E>(
&self,
load: &mut impl FnMut(&str) -> Result<I, E>,
) -> Result<ShapeOutside<I>, E> {
Ok(match self {
Self::None => ShapeOutside::None,
Self::Box(reference) => ShapeOutside::Box(*reference),
Self::Shape { shape, reference } => ShapeOutside::Shape {
shape: shape.clone(),
reference: *reference,
},
Self::Image(image) => {
let mut image = image.with_image_type();
image.load(load)?;
ShapeOutside::Image(image)
}
})
}
}
fn direction_agnostic([width, height]: [f32; 2]) -> f32 {
width.hypot(height) / std::f32::consts::SQRT_2
}
fn used_radii(radii: &CornerRadii, basis: [f32; 2], size: [f32; 2]) -> Radii {
let resolved = std::array::from_fn(|corner| {
let [horizontal, vertical] = &radii[corner];
let used = [horizontal.resolve(basis[0]), vertical.resolve(basis[1])];
if used[0] > 0.0 && used[1] > 0.0 {
used
} else {
[0.0; 2]
}
});
rounded::constrain(resolved, size)
}
fn fitted([start, end]: [f32; 2], extent: f32) -> [f32; 2] {
let sum = start + end;
if sum > extent && sum > 0.0 {
let scale = extent / sum;
[start * scale, end * scale]
} else {
[start, end]
}
}
fn ellipse(center: [f32; 2], [rx, ry]: [f32; 2]) -> Contour {
rounded::rounded_rect(
Rect {
x: center[0] - rx,
y: center[1] - ry,
width: 2.0 * rx,
height: 2.0 * ry,
},
[[rx, ry]; 4],
)
}
impl AxisRadius {
fn resolve(&self, center: f32, extent: f32) -> f32 {
let sides = [center.abs(), (extent - center).abs()];
match self {
Self::ClosestSide => sides[0].min(sides[1]),
Self::FarthestSide => sides[0].max(sides[1]),
Self::Length(length) => length.resolve(extent),
}
}
}
impl BasicShape {
pub(crate) fn outline(&self, reference: Rect) -> Shape {
let (mut contours, even_odd) = self.path(reference);
for contour in &mut contours {
contour.closed = true;
}
Shape::Path {
contours,
even_odd,
clip: None,
}
}
pub(crate) fn path(&self, reference: Rect) -> (Vec<Contour>, bool) {
let size = [reference.width, reference.height];
let origin = [reference.x, reference.y];
let (contours, even_odd) = match self {
Self::Inset { insets, radii } => {
let [top, bottom] = fitted(
[insets[0].resolve(size[1]), insets[2].resolve(size[1])],
size[1],
);
let [left, right] = fitted(
[insets[3].resolve(size[0]), insets[1].resolve(size[0])],
size[0],
);
let rect = Rect {
x: origin[0] + left,
y: origin[1] + top,
width: (size[0] - left - right).max(0.0),
height: (size[1] - top - bottom).max(0.0),
};
(vec![rounded_inset(rect, radii, size)], false)
}
Self::Rect { edges, radii } => {
let top = edges[0].resolve(size[1]);
let left = edges[3].resolve(size[0]);
let right = edges[1].resolve(size[0]).max(left);
let bottom = edges[2].resolve(size[1]).max(top);
let rect = Rect {
x: origin[0] + left,
y: origin[1] + top,
width: right - left,
height: bottom - top,
};
(vec![rounded_inset(rect, radii, size)], false)
}
Self::Circle { radius, center } => {
let center = [center[0].resolve(size[0]), center[1].resolve(size[1])];
let radius = match radius {
CircleRadius::Extent(extent) => extent.circle(center, size),
CircleRadius::Length(length) => length.resolve(direction_agnostic(size)),
};
(
vec![ellipse(
[origin[0] + center[0], origin[1] + center[1]],
[radius; 2],
)],
false,
)
}
Self::Ellipse { radii, center } => {
let center = [center[0].resolve(size[0]), center[1].resolve(size[1])];
let radii = match radii {
EllipseRadii::Extent(extent) => extent.ellipse(center, size),
EllipseRadii::Axes([x, y]) => {
[x.resolve(center[0], size[0]), y.resolve(center[1], size[1])]
}
};
(
vec![ellipse(
[origin[0] + center[0], origin[1] + center[1]],
radii,
)],
false,
)
}
Self::Polygon { even_odd, points } => {
let points = points
.iter()
.map(|[x, y]| {
[
origin[0] + x.resolve(size[0]),
origin[1] + y.resolve(size[1]),
]
})
.collect::<Vec<_>>();
(vec![Contour::polygon(&points)], *even_odd)
}
Self::Path { even_odd, contours } => {
let place = |[x, y]: [f32; 2]| {
[
origin[0] + x * POINTS_PER_PIXEL,
origin[1] + y * POINTS_PER_PIXEL,
]
};
let contours = contours.iter().map(|contour| contour.map(place)).collect();
(contours, *even_odd)
}
};
(contours, even_odd)
}
}
fn rounded_inset(rect: Rect, radii: &CornerRadii, reference: [f32; 2]) -> Contour {
rounded::rounded_rect(
rect,
used_radii(radii, reference, [rect.width, rect.height]),
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::paint::{PositionComponent, Segment};
const REFERENCE: Rect = Rect {
x: 10.0,
y: 20.0,
width: 100.0,
height: 200.0,
};
fn square() -> CornerRadii {
std::array::from_fn(|_| [LengthPercentage::ZERO; 2])
}
fn contours(shape: &BasicShape) -> Vec<Contour> {
let Shape::Path { contours, .. } = shape.outline(REFERENCE) else {
unreachable!("basic shapes outline as paths")
};
contours
}
#[test]
fn rectangle_functions_resolve_overlapping_edges_as_their_examples_show() {
let percent = LengthPercentage::Percentage;
let point = LengthPercentage::Length;
for (label, shape, equivalent) in [
(
"§3.1: inset(75% 0 50% 0) reduces its vertical insets to 60% and 40%",
BasicShape::Inset {
insets: [percent(0.75), point(0.0), percent(0.5), point(0.0)],
radii: square(),
},
BasicShape::Inset {
insets: [percent(0.6), point(0.0), percent(0.4), point(0.0)],
radii: square(),
},
),
(
"§3.1: rect(10px 0 0 20px) floors its right and bottom edges to rect(10px 20px \
10px 20px)",
BasicShape::Rect {
edges: [point(7.5), point(0.0), point(0.0), point(15.0)],
radii: square(),
},
BasicShape::Rect {
edges: [point(7.5), point(15.0), point(7.5), point(15.0)],
radii: square(),
},
),
] {
assert_eq!(contours(&shape), contours(&equivalent), "{label}");
}
}
#[test]
fn circle_percentages_and_extents_resolve_against_the_reference_box() {
let center = [PositionComponent::START; 2];
for (label, radius, expected) in [
(
"50% of the direction-agnostic size",
CircleRadius::Length(LengthPercentage::Percentage(0.5)),
0.5 * 100.0_f32.hypot(200.0) / std::f32::consts::SQRT_2,
),
(
"closest-side from the top-left corner",
CircleRadius::Extent(RadialExtent::ClosestSide),
0.0,
),
(
"farthest-corner from the top-left corner",
CircleRadius::Extent(RadialExtent::FarthestCorner),
100.0_f32.hypot(200.0),
),
] {
let contour = &contours(&BasicShape::Circle {
radius,
center: center.clone(),
})[0];
let right = contour
.segments
.iter()
.flat_map(|segment| match segment {
Segment::Line(point) => vec![*point],
Segment::Cubic(points) => points.to_vec(),
})
.map(|point| point[0])
.fold(contour.start[0], f32::max);
assert!(
(right - REFERENCE.x - expected).abs() < 1e-3,
"{label}: the circle reaches {right}, expected radius {expected}"
);
}
}
}