#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PaintStrokeWidth(euclid::Length<f32, PaintSpace>);
impl PaintStrokeWidth {
pub const ZERO: Self = Self(euclid::Length::new(0.0));
pub const fn new(points: f32) -> Self {
Self(euclid::Length::new(points))
}
pub fn points(self) -> f32 {
self.0.get()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaintSpace {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PdfUserSpace {}
pub type PaintPoint = euclid::Point2D<f32, PaintSpace>;
pub(crate) type PaintDisplacement = euclid::Vector2D<f32, PaintSpace>;
pub(crate) type PaintTranslation = euclid::Translation2D<f32, PaintSpace, PaintSpace>;
pub type PaintSize = euclid::Size2D<f32, PaintSpace>;
pub type PaintRect = euclid::Rect<f32, PaintSpace>;
pub(crate) type PdfPoint = euclid::Point2D<f32, PdfUserSpace>;
pub(crate) type PdfSize = euclid::Size2D<f32, PdfUserSpace>;
pub(crate) type PdfRect = euclid::Rect<f32, PdfUserSpace>;
pub(crate) fn paint_rect_to_pdf(rect: PaintRect) -> PdfRect {
PdfRect::new(
PdfPoint::new(rect.origin.x, rect.origin.y),
PdfSize::new(rect.size.width, rect.size.height),
)
}
pub(crate) fn paint_point_to_pdf(point: PaintPoint) -> PdfPoint {
PdfPoint::new(point.x, point.y)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PaintTransform(euclid::Transform2D<f32, PaintSpace, PaintSpace>);
impl PaintTransform {
pub(crate) fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
Self(euclid::Transform2D::new(a, b, c, d, e, f))
}
pub(crate) fn from_transform(
transform: euclid::Transform2D<f32, PaintSpace, PaintSpace>,
) -> Self {
Self(transform)
}
pub(crate) fn identity() -> Self {
Self(euclid::Transform2D::identity())
}
pub(crate) fn translate(offset: PaintTranslation) -> Self {
Self(offset.to_transform())
}
pub(crate) fn multiply(self, right: Self) -> Self {
Self(right.0.then(&self.0))
}
pub(crate) fn scale(x: f32, y: f32) -> Self {
Self(euclid::Transform2D::scale(x, y))
}
pub(crate) fn a(self) -> f32 {
self.0.m11
}
pub(crate) fn b(self) -> f32 {
self.0.m12
}
pub(crate) fn c(self) -> f32 {
self.0.m21
}
pub(crate) fn d(self) -> f32 {
self.0.m22
}
pub(crate) fn e(self) -> f32 {
self.0.m31
}
pub(crate) fn f(self) -> f32 {
self.0.m32
}
pub(crate) fn pdf_components(self) -> [f32; 6] {
[self.a(), self.b(), self.c(), self.d(), self.e(), self.f()]
}
pub(crate) fn preserves_axis_aligned_rectangles(self) -> bool {
self.a().is_finite()
&& self.b().is_finite()
&& self.c().is_finite()
&& self.d().is_finite()
&& self.e().is_finite()
&& self.f().is_finite()
&& self.b() == 0.0
&& self.c() == 0.0
}
pub(crate) fn is_invertible(self) -> bool {
let determinant = self.a() * self.d() - self.b() * self.c();
determinant.is_finite() && determinant != 0.0
}
pub(crate) fn apply_point(self, point: PaintPoint) -> PaintPoint {
self.0.transform_point(point)
}
pub(crate) fn apply_clip_to_aabb(self, clip: PaintClip) -> PaintClip {
let points = [
self.apply_point(clip.bottom_left()),
self.apply_point(clip.bottom_right()),
self.apply_point(clip.top_left()),
self.apply_point(clip.top_right()),
];
let min_x = points
.iter()
.map(|point| point.x)
.fold(f32::INFINITY, f32::min);
let max_x = points
.iter()
.map(|point| point.x)
.fold(f32::NEG_INFINITY, f32::max);
let min_y = points
.iter()
.map(|point| point.y)
.fold(f32::INFINITY, f32::min);
let max_y = points
.iter()
.map(|point| point.y)
.fold(f32::NEG_INFINITY, f32::max);
PaintClip::from_paint_rect(PaintRect::new(
PaintPoint::new(min_x, min_y),
PaintSize::new((max_x - min_x).max(0.0), (max_y - min_y).max(0.0)),
))
}
pub(crate) fn inverse_apply_clip_to_aabb(self, clip: PaintClip) -> Option<PaintClip> {
let inverse = self.0.inverse()?;
Some(Self::from_transform(inverse).apply_clip_to_aabb(clip))
}
pub(crate) fn inverse_apply_point(self, point: PaintPoint) -> Option<PaintPoint> {
Some(Self::from_transform(self.0.inverse()?).apply_point(point))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Affine3dPaintTransform(euclid::Transform3D<f32, PaintSpace, PaintSpace>);
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Projective3dPaintTransform(euclid::Transform3D<f32, PaintSpace, PaintSpace>);
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum ProjectedPaintPoint {
Visible {
point: PaintPoint,
depth: f32,
},
AtViewer {
direction: euclid::Vector3D<f32, PaintSpace>,
},
BehindViewer,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ProjectedScenePlane {
pub(crate) polygon: Vec<PaintPoint>,
pub(crate) depth: f32,
pub(crate) source_transform: Projective3dPaintTransform,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ProjectedPlane {
Visible(ProjectedScenePlane),
ClippedAtViewer(ProjectedScenePlane),
BehindViewer,
}
impl Projective3dPaintTransform {
const EPSILON: f32 = 1e-6;
pub(crate) fn identity() -> Self {
Self(euclid::Transform3D::identity())
}
pub(crate) fn from_transform(
transform: euclid::Transform3D<f32, PaintSpace, PaintSpace>,
) -> Self {
Self(transform)
}
pub(crate) fn from_paint_transform(transform: PaintTransform) -> Self {
Self(euclid::Transform3D::new(
transform.a(),
transform.b(),
0.0,
0.0,
transform.c(),
transform.d(),
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
transform.e(),
transform.f(),
0.0,
1.0,
))
}
pub(crate) fn multiply(self, right: Self) -> Self {
Self(right.0.then(&self.0))
}
pub(crate) fn is_invertible(self) -> bool {
self.0.inverse().is_some()
}
pub(crate) fn try_into_affine_pdf_ctm(self) -> Option<Affine3dPaintTransform> {
Affine3dPaintTransform::try_from_transform(self.0)
}
pub(crate) fn faces_away_from_viewer(self) -> bool {
self.0.m33 < 0.0
}
pub(crate) fn project_plane_point(self, point: PaintPoint) -> ProjectedPaintPoint {
let (x, y, z, w) = self.homogeneous_plane_point(point);
if w > Self::EPSILON {
ProjectedPaintPoint::Visible {
point: PaintPoint::new(x / w, y / w),
depth: z / w,
}
} else if w < -Self::EPSILON {
ProjectedPaintPoint::BehindViewer
} else {
ProjectedPaintPoint::AtViewer {
direction: euclid::Vector3D::new(x, y, z),
}
}
}
pub(crate) fn project_visible_polygon(self, points: &[PaintPoint]) -> Vec<PaintPoint> {
if points.len() < 3 {
return Vec::new();
}
let mut visible = Vec::with_capacity(points.len() + 2);
let mut previous = *points.last().expect("nonempty polygon");
let mut previous_w = self.homogeneous_plane_point(previous).3;
for current in points.iter().copied() {
let current_w = self.homogeneous_plane_point(current).3;
let previous_inside = previous_w > Self::EPSILON;
let current_inside = current_w > Self::EPSILON;
if previous_inside != current_inside {
let t = (Self::EPSILON - previous_w) / (current_w - previous_w);
visible.push(PaintPoint::new(
previous.x + (current.x - previous.x) * t,
previous.y + (current.y - previous.y) * t,
));
}
if current_inside {
visible.push(current);
}
previous = current;
previous_w = current_w;
}
visible
.into_iter()
.map(|point| {
let (x, y, _, w) = self.homogeneous_plane_point(point);
PaintPoint::new(x / w, y / w)
})
.collect()
}
pub(crate) fn project_plane(self, points: &[PaintPoint]) -> ProjectedPlane {
if points.len() < 3 {
return ProjectedPlane::BehindViewer;
}
let viewer_clipped = points
.iter()
.any(|point| self.homogeneous_plane_point(*point).3 <= Self::EPSILON);
let polygon = self.project_visible_polygon(points);
if polygon.len() < 3 {
return ProjectedPlane::BehindViewer;
}
let depth = points
.iter()
.filter_map(|point| match self.project_plane_point(*point) {
ProjectedPaintPoint::Visible { depth, .. } => Some(depth),
ProjectedPaintPoint::AtViewer { .. } | ProjectedPaintPoint::BehindViewer => None,
})
.next()
.unwrap_or(0.0);
let plane = ProjectedScenePlane {
polygon,
depth,
source_transform: self,
};
if viewer_clipped {
ProjectedPlane::ClippedAtViewer(plane)
} else {
ProjectedPlane::Visible(plane)
}
}
fn homogeneous_plane_point(self, point: PaintPoint) -> (f32, f32, f32, f32) {
(
point.x * self.0.m11 + point.y * self.0.m21 + self.0.m41,
point.x * self.0.m12 + point.y * self.0.m22 + self.0.m42,
point.x * self.0.m13 + point.y * self.0.m23 + self.0.m43,
point.x * self.0.m14 + point.y * self.0.m24 + self.0.m44,
)
}
}
impl Affine3dPaintTransform {
const EPSILON: f32 = 1e-6;
pub(crate) fn identity() -> Self {
Self(euclid::Transform3D::identity())
}
pub(crate) fn try_from_transform(
transform: euclid::Transform3D<f32, PaintSpace, PaintSpace>,
) -> Option<Self> {
(transform.m14.abs() <= Self::EPSILON
&& transform.m24.abs() <= Self::EPSILON
&& transform.m34.abs() <= Self::EPSILON
&& transform.m44.abs() > Self::EPSILON)
.then_some(Self(transform))
}
pub(crate) fn multiply(self, right: Self) -> Self {
Self(right.0.then(&self.0))
}
pub(crate) fn is_invertible(self) -> bool {
self.0.inverse().is_some()
}
pub(crate) fn into_projective(self) -> Projective3dPaintTransform {
Projective3dPaintTransform::from_transform(self.0)
}
pub(crate) fn flatten_to_paint_transform(self) -> PaintTransform {
let inverse_w = self.0.m44.recip();
PaintTransform::from_transform(euclid::Transform2D::new(
self.0.m11 * inverse_w,
self.0.m12 * inverse_w,
self.0.m21 * inverse_w,
self.0.m22 * inverse_w,
self.0.m41 * inverse_w,
self.0.m42 * inverse_w,
))
}
pub(crate) fn faces_away_from_viewer(self) -> bool {
self.0.m33 < 0.0
}
pub(crate) fn depth_at(self, point: PaintPoint) -> f32 {
self.0.m13 * point.x + self.0.m23 * point.y + self.0.m43
}
pub(crate) fn depth_at_projected(self, point: PaintPoint) -> Option<f32> {
let local = self
.flatten_to_paint_transform()
.inverse_apply_point(point)?;
Some(self.depth_at(local))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PaintClip {
pub(in crate::document) rect: PaintRect,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PaintClipUnion {
clips: [PaintClip; Self::MAX_REGIONS],
len: u8,
}
impl PaintClipUnion {
pub(crate) const MAX_REGIONS: usize = 8;
pub(crate) fn from_clips(clips: &[PaintClip]) -> Option<Self> {
let first = *clips.first()?;
let mut union = Self {
clips: [first; Self::MAX_REGIONS],
len: 0,
};
for clip in clips.iter().copied().take(Self::MAX_REGIONS) {
union.clips[usize::from(union.len)] = clip;
union.len += 1;
}
Some(union)
}
pub(crate) fn clips(&self) -> &[PaintClip] {
&self.clips[..usize::from(self.len)]
}
pub(crate) fn translated(mut self, offset: PaintTranslation) -> Self {
for clip in &mut self.clips[..usize::from(self.len)] {
*clip = clip.translated(offset);
}
self
}
}
impl PaintClip {
pub(crate) fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self::from_paint_rect(PaintRect::new(
PaintPoint::new(x, y),
PaintSize::new(width.max(0.0), height.max(0.0)),
))
}
pub(crate) fn from_paint_rect(rect: PaintRect) -> Self {
Self { rect }
}
pub(crate) fn x(self) -> f32 {
self.rect.origin.x
}
pub(crate) fn y(self) -> f32 {
self.rect.origin.y
}
pub(crate) fn width(self) -> f32 {
self.rect.size.width
}
pub(crate) fn height(self) -> f32 {
self.rect.size.height
}
pub(crate) fn paint_rect(self) -> PaintRect {
self.rect
}
pub(crate) fn bottom_left(self) -> PaintPoint {
self.rect.origin
}
pub(crate) fn bottom_right(self) -> PaintPoint {
PaintPoint::new(self.x() + self.width(), self.y())
}
pub(crate) fn top_left(self) -> PaintPoint {
PaintPoint::new(self.x(), self.y() + self.height())
}
pub(crate) fn top_right(self) -> PaintPoint {
PaintPoint::new(self.x() + self.width(), self.y() + self.height())
}
pub(crate) fn translated(self, offset: PaintTranslation) -> Self {
Self::from_paint_rect(offset.transform_rect(&self.rect))
}
pub(crate) fn intersect(self, other: Self) -> Option<Self> {
self.rect
.intersection(&other.rect)
.map(Self::from_paint_rect)
}
pub(crate) fn intersects_with_positive_area(self, other: Self) -> bool {
let left = self.x().max(other.x());
let right = (self.x() + self.width()).min(other.x() + other.width());
let bottom = self.y().max(other.y());
let top = (self.y() + self.height()).min(other.y() + other.height());
const EDGE_EPSILON: f32 = 0.001;
right - left > EDGE_EPSILON && top - bottom > EDGE_EPSILON
}
pub(crate) fn contains(self, other: Self) -> bool {
other.x() >= self.x()
&& other.y() >= self.y()
&& other.x() + other.width() <= self.x() + self.width()
&& other.y() + other.height() <= self.y() + self.height()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct AxisSelectivePaintClip {
bounds: PaintClip,
clips_x: bool,
clips_y: bool,
}
impl AxisSelectivePaintClip {
pub(crate) const fn new(bounds: PaintClip, clips_x: bool, clips_y: bool) -> Self {
Self {
bounds,
clips_x,
clips_y,
}
}
pub(crate) const fn clips_x(self) -> bool {
self.clips_x
}
pub(crate) const fn clips_y(self) -> bool {
self.clips_y
}
pub(crate) const fn bounds(self) -> PaintClip {
self.bounds
}
pub(crate) fn translated(self, offset: PaintTranslation) -> Self {
Self::new(self.bounds.translated(offset), self.clips_x, self.clips_y)
}
pub(crate) fn resolved_against_page(self, page: PaintClip) -> PaintClip {
debug_assert!(self.clips_x() || self.clips_y());
let left = if self.clips_x {
self.bounds.x()
} else {
page.x()
};
let right = if self.clips_x {
self.bounds.x() + self.bounds.width()
} else {
page.x() + page.width()
};
let bottom = if self.clips_y {
self.bounds.y()
} else {
page.y()
};
let top = if self.clips_y {
self.bounds.y() + self.bounds.height()
} else {
page.y() + page.height()
};
PaintClip::new(left, bottom, right - left, top - bottom)
}
}
pub(in crate::document) fn rect_bounds(rect: PaintRect) -> Option<PaintClip> {
(rect.size.width > 0.0 && rect.size.height > 0.0).then_some(PaintClip::from_paint_rect(rect))
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(in crate::document) struct PaintBounds {
min: PaintPoint,
max: PaintPoint,
}
impl PaintBounds {
#[cfg(test)]
pub(in crate::document) fn from_paint_point(point: PaintPoint) -> Self {
Self {
min: point,
max: point,
}
}
pub(in crate::document) fn from_paint_rect(rect: PaintRect) -> Self {
Self {
min: rect.origin,
max: PaintPoint::new(rect.max_x(), rect.max_y()),
}
}
pub(in crate::document) fn include_paint_point(&mut self, point: PaintPoint) {
self.min.x = self.min.x.min(point.x);
self.min.y = self.min.y.min(point.y);
self.max.x = self.max.x.max(point.x);
self.max.y = self.max.y.max(point.y);
}
pub(in crate::document) fn include_paint_rect(&mut self, rect: PaintRect) {
self.include_paint_point(rect.origin);
self.include_paint_point(PaintPoint::new(rect.max_x(), rect.max_y()));
}
pub(in crate::document) fn paint_rect(self) -> PaintRect {
PaintRect::new(
self.min,
PaintSize::new(self.max.x - self.min.x, self.max.y - self.min.y),
)
}
pub(in crate::document) fn into_paint_clip(self) -> PaintClip {
PaintClip::from_paint_rect(self.paint_rect())
}
}
#[cfg(test)]
mod tests {
use super::{
AxisSelectivePaintClip, PaintBounds, PaintClip, PaintPoint, PaintRect, PaintSize,
PaintTransform, PaintTranslation, PdfPoint, PdfRect, PdfSize, ProjectedPaintPoint,
Projective3dPaintTransform, paint_point_to_pdf, paint_rect_to_pdf,
};
fn paint_rect(x: f32, y: f32, width: f32, height: f32) -> PaintRect {
PaintRect::new(PaintPoint::new(x, y), PaintSize::new(width, height))
}
#[test]
fn typed_affine_composition_keeps_css_matrix_order() {
let transform = PaintTransform::translate(PaintTranslation::new(10.0, 20.0))
.multiply(PaintTransform::scale(2.0, 3.0));
assert_eq!(
transform.apply_point(PaintPoint::new(1.0, 2.0)),
PaintPoint::new(12.0, 26.0)
);
}
#[test]
fn projective_plane_clips_edges_at_the_viewer_instead_of_dropping_it() {
let transform = Projective3dPaintTransform::from_transform(euclid::Transform3D::new(
1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
));
assert!(matches!(
transform.project_plane_point(PaintPoint::new(0.0, 0.0)),
ProjectedPaintPoint::Visible { .. }
));
let polygon = transform.project_visible_polygon(&[
PaintPoint::new(-2.0, -1.0),
PaintPoint::new(0.0, -1.0),
PaintPoint::new(0.0, 1.0),
PaintPoint::new(-2.0, 1.0),
]);
assert!(polygon.len() >= 3);
assert!(
polygon
.iter()
.all(|point| point.x.is_finite() && point.y.is_finite())
);
}
#[test]
fn paint_clip_round_trips_through_typed_rect() {
let rect = paint_rect(10.0, 20.0, 30.0, 40.0);
let clip = PaintClip::from_paint_rect(rect);
assert_eq!(clip, PaintClip::new(10.0, 20.0, 30.0, 40.0));
assert_eq!(clip.paint_rect(), rect);
}
#[test]
fn paint_clip_translation_and_intersection_delegate_to_typed_rects() {
let clip = PaintClip::new(10.0, 20.0, 30.0, 40.0);
assert_eq!(
clip.translated(PaintTranslation::new(-5.0, 7.0))
.paint_rect(),
paint_rect(5.0, 27.0, 30.0, 40.0),
);
assert_eq!(
clip.intersect(PaintClip::new(25.0, 30.0, 30.0, 40.0))
.unwrap()
.paint_rect(),
paint_rect(25.0, 30.0, 15.0, 30.0),
);
assert!(
clip.intersect(PaintClip::new(40.0, 20.0, 10.0, 10.0))
.is_none()
);
}
#[test]
fn axis_selective_clip_keeps_the_visible_axis_unbounded() {
let clip = AxisSelectivePaintClip::new(PaintClip::new(10.0, 20.0, 30.0, 40.0), false, true);
assert!(!clip.clips_x());
assert!(clip.clips_y());
assert_eq!(
clip.resolved_against_page(PaintClip::new(0.0, 0.0, 100.0, 100.0)),
PaintClip::new(0.0, 20.0, 100.0, 40.0)
);
}
#[test]
fn axis_selective_clip_uses_page_bounds_in_the_transformed_local_space() {
let page = PaintClip::new(0.0, 0.0, 100.0, 100.0);
let transform = PaintTransform::translate(PaintTranslation::new(-10.0, 0.0));
let local_page = transform
.inverse_apply_clip_to_aabb(page)
.expect("translation is invertible");
let clip = AxisSelectivePaintClip::new(PaintClip::new(20.0, 30.0, 40.0, 10.0), false, true);
assert_eq!(
clip.resolved_against_page(local_page),
PaintClip::new(10.0, 30.0, 100.0, 10.0)
);
}
#[test]
fn paint_bounds_retains_degenerate_path_geometry() {
let mut bounds = PaintBounds::from_paint_point(PaintPoint::new(10.0, 20.0));
bounds.include_paint_point(PaintPoint::new(30.0, 20.0));
bounds.include_paint_rect(paint_rect(5.0, 2.0, 0.0, 5.0));
assert_eq!(bounds.paint_rect(), paint_rect(5.0, 2.0, 25.0, 18.0));
}
#[test]
fn paint_rect_to_pdf_is_identity_for_unrotated_pages() {
let rect = paint_rect(7.0, 8.0, 9.0, 10.0);
assert_eq!(
paint_rect_to_pdf(rect),
PdfRect::new(PdfPoint::new(7.0, 8.0), PdfSize::new(9.0, 10.0))
);
}
#[test]
fn paint_point_to_pdf_is_identity_for_unrotated_pages() {
assert_eq!(
paint_point_to_pdf(PaintPoint::new(11.0, 12.0)),
PdfPoint::new(11.0, 12.0)
);
}
#[test]
fn paint_transform_maps_typed_points_and_clips() {
let transform = PaintTransform::translate(PaintTranslation::new(5.0, -2.0));
assert_eq!(
transform.apply_point(PaintPoint::new(10.0, 20.0)),
PaintPoint::new(15.0, 18.0)
);
assert_eq!(
transform.apply_clip_to_aabb(PaintClip::from_paint_rect(paint_rect(
10.0, 20.0, 30.0, 40.0,
))),
PaintClip::from_paint_rect(paint_rect(15.0, 18.0, 30.0, 40.0))
);
}
}