Skip to main content

alpha2_geometry/
lib.rs

1//! Deterministic vector geometry for alpha2 shapes.
2//!
3//! A shapes generator's content tree, evaluated at one time
4//! ([`EvaluatedItem`]), becomes a [`VectorScene`]: fills and stroke outlines
5//! in device pixels, in drawing order, with the group layers that fade them.
6//! [`raster::rasterize`] turns one of its paths into tile coverage. Queries
7//! the editor needs (bounds, hit tests, lengths, trims) use the same code.
8//!
9//! Every result is the same bits on every target. Geometry is `f64`,
10//! trigonometry is `libm`'s, arc lengths and flattening use only `+ - * /`
11//! and `sqrt`, and coverage is accumulated in scalar `f32` in a fixed order.
12//! Stroke expansion is kurbo's, and its transcendental calls are `libm`'s
13//! too, unless another crate in the build turns on kurbo's `std`: then
14//! outlines and scene keys can differ in the last bits, and the shared hash
15//! goldens compare coverage only.
16
17pub mod affine;
18pub mod arclen;
19mod far;
20mod hash;
21#[doc(hidden)]
22pub mod parity;
23pub mod query;
24pub mod raster;
25mod scene;
26pub mod shapes;
27pub mod spatial;
28mod stroke;
29mod tree;
30pub mod trim;
31
32#[cfg(test)]
33mod tests;
34
35pub use far::{MAX_DEVICE, project_far};
36pub use hash::StableHash;
37pub use kurbo;
38pub use scene::{IRect, PaintKind, ScenePaint, SceneStop, VectorOp, VectorScene};
39pub use stroke::{StrokeStyle, stroke_outline};
40pub use tree::{
41    EvaluatedGeometry, EvaluatedItem, EvaluatedPath, EvaluatedSubpath, EvaluatedTransform,
42    EvaluatedVertex, ItemPath, build_scene, build_scene_in, build_scene_with_items, item_paths,
43    scene_paint,
44};
45
46/// The size, in device pixels, of the picture a content tree is drawn on:
47/// normalized `(1, 1)` is `(width, height)`, and lengths are fractions of
48/// the shorter side.
49#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
50pub struct CanvasSize {
51    /// Width in pixels.
52    pub width: u32,
53    /// Height in pixels.
54    pub height: u32,
55}
56
57impl CanvasSize {
58    /// A canvas of `width` by `height` pixels.
59    #[must_use]
60    pub const fn new(width: u32, height: u32) -> Self {
61        Self { width, height }
62    }
63
64    /// The scale from normalized coordinates to pixels, per axis.
65    #[must_use]
66    pub fn scale(self) -> kurbo::Vec2 {
67        kurbo::Vec2::new(f64::from(self.width), f64::from(self.height))
68    }
69
70    /// The pixel length of a normalized length: a fraction of the shorter
71    /// side.
72    #[must_use]
73    pub fn unit(self) -> f64 {
74        f64::from(self.width.min(self.height))
75    }
76
77    /// The pixel point of a normalized point.
78    #[must_use]
79    pub fn point(self, x: f64, y: f64) -> kurbo::Point {
80        kurbo::Point::new(x * f64::from(self.width), y * f64::from(self.height))
81    }
82}