diff --git a/piet-scene/src/lib.rs b/piet-scene/src/lib.rs index 4f8da3d..8501d33 100644 --- a/piet-scene/src/lib.rs +++ b/piet-scene/src/lib.rs @@ -28,9 +28,10 @@ mod kurbo_conv { use super::geometry::{Affine, Point, Rect}; use super::path::Element; - impl From for Point { - fn from(p: kurbo::Point) -> Self { - Self::new(p.x as f32, p.y as f32) + impl Point { + /// Creates a new point from the equivalent kurbo type. + pub fn from_kurbo(point: kurbo::Point) -> Self { + Self::new(point.x as f32, point.y as f32) } } @@ -40,9 +41,10 @@ mod kurbo_conv { } } - impl From for Affine { - fn from(a: kurbo::Affine) -> Self { - let c = a.as_coeffs(); + impl Affine { + /// Creates a new affine transformation from the equivalent kurbo type. + pub fn from_kurbo(affine: kurbo::Affine) -> Self { + let c = affine.as_coeffs(); Self { xx: c[0] as f32, yx: c[1] as f32, @@ -67,11 +69,12 @@ mod kurbo_conv { } } - impl From for Rect { - fn from(r: kurbo::Rect) -> Self { + impl Rect { + /// Creates a new rectangle from the equivalent kurbo type. + pub fn from_kurbo(rect: kurbo::Rect) -> Self { Self { - min: Point::new(r.x0 as f32, r.y0 as f32), - max: Point::new(r.x1 as f32, r.y1 as f32), + min: Point::new(rect.x0 as f32, rect.y0 as f32), + max: Point::new(rect.x1 as f32, rect.y1 as f32), } } } @@ -100,6 +103,23 @@ mod kurbo_conv { } } + impl Element { + /// Creates a new path element from the equivalent kurbo type. + pub fn from_kurbo(el: kurbo::PathEl) -> Self { + use kurbo::PathEl::*; + use Point::from_kurbo; + match e { + MoveTo(p0) => Self::MoveTo(from_kurbo(p0)), + LineTo(p0) => Self::LineTo(from_kurbo(p0)), + QuadTo(p0, p1) => Self::QuadTo(from_kurbo(p0), from_kurbo(p1)), + CurveTo(p0, p1, p2) => { + Self::CurveTo(from_kurbo(p0), from_kurbo(p1), from_kurbo(p2)) + } + ClosePath => Self::Close, + } + } + } + impl From for kurbo::PathEl { fn from(e: Element) -> Self { use Element::*;