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