use super::geometry::{
PaintClip, PaintPoint, PaintRect, PaintSize, PaintStrokeWidth, PaintTransform, PaintTranslation,
};
use crate::CssColor;
#[derive(Debug, Clone, PartialEq)]
pub struct RenderedPath {
pub clip: Option<RenderedPathClip>,
pub(crate) transform: PaintTransform,
pub commands: Vec<RenderedPathCommand>,
pub fill: Option<CssColor>,
pub(crate) fill_paint: Option<RenderedPathPaint>,
pub fill_rule: RenderedPathFillRule,
pub stroke: Option<CssColor>,
pub(crate) stroke_paint: Option<RenderedPathPaint>,
pub stroke_width: PaintStrokeWidth,
pub(crate) stroke_style: RenderedPathStrokeStyle,
pub(crate) paint_order: RenderedPathPaintOrder,
pub(crate) opaque_coverage_rect: Option<PaintRect>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum RenderedPathPaint {
Solid(CssColor),
Gradient(RenderedGradient),
SvgPattern(RenderedSvgPathPattern),
}
impl RenderedPathPaint {
fn solid_color(&self) -> Option<CssColor> {
match self {
Self::Solid(color) => Some(*color),
Self::Gradient(_) | Self::SvgPattern(_) => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RenderedSvgPathPattern {
pub(crate) tile_size: PaintSize,
pub(crate) origin: PaintPoint,
pub(crate) transform: PaintTransform,
pub(crate) scene: Box<crate::svg::SvgPaintGroup>,
pub(crate) opacity: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RenderedGradient {
pub(crate) kind: RenderedGradientKind,
pub(crate) color_space: crate::css::CssColorSpace,
pub(crate) stops: Vec<RenderedGradientStop>,
pub(crate) periodic: Option<Box<RenderedPeriodicGradient>>,
pub(crate) transform: PaintTransform,
}
impl RenderedGradient {
pub(crate) fn has_transparent_stop(&self) -> bool {
self.periodic
.as_ref()
.map_or(&self.stops, |periodic| &periodic.stops)
.iter()
.any(|stop| !stop.color.is_opaque())
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RenderedPeriodicGradient {
pub(crate) stops: Vec<RenderedGradientStop>,
pub(crate) start: f32,
pub(crate) period: f32,
pub(crate) domain_length: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum RenderedGradientKind {
Linear {
start: PaintPoint,
end: PaintPoint,
},
Radial {
start_center: PaintPoint,
start_radius: f32,
end_center: PaintPoint,
end_radius: f32,
},
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RenderedGradientStop {
pub(crate) offset: f32,
pub(crate) color: CssColor,
pub(crate) interpolation_exponent: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RenderedPathStrokeStyle {
pub(crate) line_cap: RenderedPathLineCap,
pub(crate) line_join: RenderedPathLineJoin,
pub(crate) miter_limit: f32,
pub(crate) dash_array: Vec<f32>,
pub(crate) dash_offset: f32,
}
impl Default for RenderedPathStrokeStyle {
fn default() -> Self {
Self {
line_cap: RenderedPathLineCap::Butt,
line_join: RenderedPathLineJoin::Miter,
miter_limit: 10.0,
dash_array: Vec::new(),
dash_offset: 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] pub(crate) enum RenderedPathLineCap {
Butt,
Round,
Square,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] pub(crate) enum RenderedPathLineJoin {
Miter,
Round,
Bevel,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[allow(dead_code)] pub(crate) enum RenderedPathPaintOrder {
#[default]
FillThenStroke,
StrokeThenFill,
}
#[allow(dead_code)]
impl RenderedPath {
pub fn bounds(&self) -> Option<PaintRect> {
let mut left = f32::INFINITY;
let mut bottom = f32::INFINITY;
let mut right = f32::NEG_INFINITY;
let mut top = f32::NEG_INFINITY;
let mut include = |point: PaintPoint| {
let point = self.transform.apply_point(point);
left = left.min(point.x);
bottom = bottom.min(point.y);
right = right.max(point.x);
top = top.max(point.y);
};
for command in &self.commands {
match command {
RenderedPathCommand::MoveTo(point) | RenderedPathCommand::LineTo(point) => {
include(*point);
}
RenderedPathCommand::CurveTo {
control_1,
control_2,
end,
} => {
include(*control_1);
include(*control_2);
include(*end);
}
RenderedPathCommand::Close => {}
}
}
(left.is_finite() && bottom.is_finite() && right.is_finite() && top.is_finite()).then(
|| {
PaintRect::new(
PaintPoint::new(left, bottom),
PaintSize::new((right - left).max(0.0), (top - bottom).max(0.0)),
)
},
)
}
pub(crate) fn new(
commands: Vec<RenderedPathCommand>,
fill: Option<CssColor>,
fill_rule: RenderedPathFillRule,
stroke: Option<CssColor>,
stroke_width: PaintStrokeWidth,
clip: Option<RenderedPathClip>,
) -> Self {
Self {
clip,
transform: PaintTransform::identity(),
commands,
fill,
fill_paint: fill.map(RenderedPathPaint::Solid),
fill_rule,
stroke,
stroke_paint: stroke.map(RenderedPathPaint::Solid),
stroke_width,
stroke_style: RenderedPathStrokeStyle::default(),
paint_order: RenderedPathPaintOrder::default(),
opaque_coverage_rect: None,
}
}
pub(crate) fn with_paints(
mut self,
fill: Option<RenderedPathPaint>,
stroke: Option<RenderedPathPaint>,
) -> Self {
self.fill = fill.as_ref().and_then(RenderedPathPaint::solid_color);
self.stroke = stroke.as_ref().and_then(RenderedPathPaint::solid_color);
self.fill_paint = fill;
self.stroke_paint = stroke;
self
}
pub(crate) fn with_stroke_style(mut self, stroke_style: RenderedPathStrokeStyle) -> Self {
self.stroke_style = stroke_style;
self
}
pub(crate) fn with_paint_order(mut self, paint_order: RenderedPathPaintOrder) -> Self {
self.paint_order = paint_order;
self
}
pub(crate) fn with_transform(mut self, transform: PaintTransform) -> Self {
self.transform = transform;
self
}
pub(crate) fn transformed(mut self, transform: PaintTransform) -> Self {
self.transform = transform.multiply(self.transform);
if let Some(rect) = self.opaque_coverage_rect {
self.opaque_coverage_rect = Some(
transform
.apply_clip_to_aabb(PaintClip::from_paint_rect(rect))
.paint_rect(),
);
}
if let Some(clip) = &mut self.clip {
clip.transform(transform);
}
for paint in [&mut self.fill_paint, &mut self.stroke_paint]
.into_iter()
.flatten()
{
if let RenderedPathPaint::Gradient(gradient) = paint {
gradient.transform = transform.multiply(gradient.transform);
}
}
self
}
pub(crate) fn with_opaque_coverage_rect(mut self, rect: PaintRect) -> Self {
self.opaque_coverage_rect = Some(rect);
self
}
pub fn paint_bounds(&self) -> Option<PaintRect> {
let mut min_x = f32::INFINITY;
let mut min_y = f32::INFINITY;
let mut max_x = f32::NEG_INFINITY;
let mut max_y = f32::NEG_INFINITY;
let mut include = |point: PaintPoint| {
let point = self.transform.apply_point(point);
min_x = min_x.min(point.x);
min_y = min_y.min(point.y);
max_x = max_x.max(point.x);
max_y = max_y.max(point.y);
};
for command in &self.commands {
match *command {
RenderedPathCommand::MoveTo(point) | RenderedPathCommand::LineTo(point) => {
include(point);
}
RenderedPathCommand::CurveTo {
control_1,
control_2,
end,
} => {
include(control_1);
include(control_2);
include(end);
}
RenderedPathCommand::Close => {}
}
}
(min_x.is_finite() && min_y.is_finite() && max_x.is_finite() && max_y.is_finite()).then(
|| {
PaintRect::new(
PaintPoint::new(min_x, min_y),
PaintSize::new(max_x - min_x, max_y - min_y),
)
},
)
}
pub(in crate::document) fn translated(mut self, offset: PaintTranslation) -> Self {
self.opaque_coverage_rect = self
.opaque_coverage_rect
.map(|rect| offset.transform_rect(&rect));
let transformed = self.transform != PaintTransform::identity();
if transformed {
self.transform = PaintTransform::translate(offset).multiply(self.transform);
}
for paint in [&mut self.fill_paint, &mut self.stroke_paint]
.into_iter()
.flatten()
{
if let RenderedPathPaint::Gradient(gradient) = paint {
gradient.transform = PaintTransform::translate(offset).multiply(gradient.transform);
}
}
if let Some(clip) = &mut self.clip {
for command in &mut clip.commands {
command.translate(offset);
}
for nested_clip in &mut clip.additional_clips {
for command in &mut nested_clip.commands {
command.translate(offset);
}
}
}
if !transformed {
for command in &mut self.commands {
command.translate(offset);
}
}
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RenderedPathClip {
pub commands: Vec<RenderedPathCommand>,
pub fill_rule: RenderedPathFillRule,
pub additional_clips: Vec<RenderedPathClipPath>,
}
impl RenderedPathClip {
pub(crate) fn new(
commands: Vec<RenderedPathCommand>,
fill_rule: RenderedPathFillRule,
additional_clips: Vec<RenderedPathClipPath>,
) -> Self {
Self {
commands,
fill_rule,
additional_clips,
}
}
pub(crate) fn translated(mut self, offset: PaintTranslation) -> Self {
for command in &mut self.commands {
command.translate(offset);
}
for clip in &mut self.additional_clips {
for command in &mut clip.commands {
command.translate(offset);
}
}
self
}
pub(crate) fn transform(&mut self, transform: PaintTransform) {
for command in &mut self.commands {
command.transform(transform);
}
for clip in &mut self.additional_clips {
for command in &mut clip.commands {
command.transform(transform);
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RenderedPathClipPath {
pub commands: Vec<RenderedPathCommand>,
pub fill_rule: RenderedPathFillRule,
}
impl RenderedPathClipPath {
pub(crate) fn new(commands: Vec<RenderedPathCommand>, fill_rule: RenderedPathFillRule) -> Self {
Self {
commands,
fill_rule,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RenderedPathCommand {
MoveTo(PaintPoint),
LineTo(PaintPoint),
CurveTo {
control_1: PaintPoint,
control_2: PaintPoint,
end: PaintPoint,
},
Close,
}
impl RenderedPathCommand {
pub(crate) fn move_to(point: PaintPoint) -> Self {
Self::MoveTo(point)
}
pub(crate) fn line_to(point: PaintPoint) -> Self {
Self::LineTo(point)
}
pub(crate) fn curve_to(control_1: PaintPoint, control_2: PaintPoint, end: PaintPoint) -> Self {
Self::CurveTo {
control_1,
control_2,
end,
}
}
pub(crate) fn typed_points(self) -> RenderedPathCommandPoints {
match self {
Self::MoveTo(point) => RenderedPathCommandPoints::MoveTo(point),
Self::LineTo(point) => RenderedPathCommandPoints::LineTo(point),
Self::CurveTo {
control_1,
control_2,
end,
} => RenderedPathCommandPoints::CurveTo {
control_1,
control_2,
end,
},
Self::Close => RenderedPathCommandPoints::Close,
}
}
pub(in crate::document) fn translate(&mut self, offset: PaintTranslation) {
match self {
Self::MoveTo(point) | Self::LineTo(point) => {
*point = offset.transform_point(*point);
}
Self::CurveTo {
control_1,
control_2,
end,
} => {
*control_1 = offset.transform_point(*control_1);
*control_2 = offset.transform_point(*control_2);
*end = offset.transform_point(*end);
}
Self::Close => {}
}
}
pub(crate) fn transform(&mut self, transform: PaintTransform) {
match self {
Self::MoveTo(point) | Self::LineTo(point) => {
*point = transform.apply_point(*point);
}
Self::CurveTo {
control_1,
control_2,
end,
} => {
*control_1 = transform.apply_point(*control_1);
*control_2 = transform.apply_point(*control_2);
*end = transform.apply_point(*end);
}
Self::Close => {}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum RenderedPathCommandPoints {
MoveTo(PaintPoint),
LineTo(PaintPoint),
CurveTo {
control_1: PaintPoint,
control_2: PaintPoint,
end: PaintPoint,
},
Close,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum RenderedPathFillRule {
#[default]
NonZero,
EvenOdd,
}
pub(crate) fn paint_rect_path_commands(rect: PaintRect) -> Vec<RenderedPathCommand> {
vec![
RenderedPathCommand::move_to(rect.origin),
RenderedPathCommand::line_to(PaintPoint::new(rect.max_x(), rect.min_y())),
RenderedPathCommand::line_to(PaintPoint::new(rect.max_x(), rect.max_y())),
RenderedPathCommand::line_to(PaintPoint::new(rect.min_x(), rect.max_y())),
RenderedPathCommand::Close,
]
}
pub(in crate::document) fn path_bounds(path: &RenderedPath) -> Option<PaintClip> {
path.paint_bounds().map(|bounds| {
let outset = path.stroke_width.points().max(0.0) / 2.0;
PaintClip::new(
bounds.origin.x - outset,
bounds.origin.y - outset,
bounds.size.width + outset * 2.0,
bounds.size.height + outset * 2.0,
)
})
}
#[cfg(test)]
mod tests {
use super::{
RenderedPath, RenderedPathCommand, RenderedPathCommandPoints, paint_rect_path_commands,
path_bounds,
};
use crate::CssColor;
use crate::document::paint::geometry::{
PaintClip, PaintPoint, PaintRect, PaintSize, PaintStrokeWidth, PaintTransform,
};
#[test]
fn paint_rect_path_commands_preserve_a_nonzero_origin() {
let rect = PaintRect::new(PaintPoint::new(10.0, 20.0), PaintSize::new(30.0, 40.0));
assert_eq!(
paint_rect_path_commands(rect),
vec![
RenderedPathCommand::move_to(PaintPoint::new(10.0, 20.0)),
RenderedPathCommand::line_to(PaintPoint::new(40.0, 20.0)),
RenderedPathCommand::line_to(PaintPoint::new(40.0, 60.0)),
RenderedPathCommand::line_to(PaintPoint::new(10.0, 60.0)),
RenderedPathCommand::Close,
]
);
}
#[test]
fn path_commands_expose_typed_paint_points() {
assert_eq!(
RenderedPathCommand::move_to(PaintPoint::new(1.0, 2.0)).typed_points(),
RenderedPathCommandPoints::MoveTo(PaintPoint::new(1.0, 2.0))
);
assert_eq!(
RenderedPathCommand::curve_to(
PaintPoint::new(1.0, 2.0),
PaintPoint::new(3.0, 4.0),
PaintPoint::new(5.0, 6.0),
)
.typed_points(),
RenderedPathCommandPoints::CurveTo {
control_1: PaintPoint::new(1.0, 2.0),
control_2: PaintPoint::new(3.0, 4.0),
end: PaintPoint::new(5.0, 6.0),
}
);
}
#[test]
fn path_bounds_use_transformed_geometry_and_stroke_outset() {
let path = RenderedPath::new(
paint_rect_path_commands(PaintRect::new(
PaintPoint::new(0.0, 0.0),
PaintSize::new(10.0, 20.0),
)),
Some(CssColor::BLACK),
super::RenderedPathFillRule::NonZero,
Some(CssColor::BLACK),
PaintStrokeWidth::new(4.0),
None,
)
.with_transform(PaintTransform::new(1.0, 0.0, 0.0, 1.0, 30.0, 40.0));
assert_eq!(
path_bounds(&path),
Some(PaintClip::new(28.0, 38.0, 14.0, 24.0))
);
}
}