Skip to content

Commit 490c0e5

Browse files
authored
Conversions between Euclid and Kurbo types (#463)
Inspired after #450 We could use those in servo and blitz. Theoretically they could also live in euclid, but given higher number of breaking releases in kurbo, kurbo seems better place for this. Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
1 parent c42ef54 commit 490c0e5

4 files changed

Lines changed: 112 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kurbo/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ workspace = true
2121

2222
[features]
2323
default = ["std"]
24-
std = []
25-
libm = ["dep:libm"]
24+
std = ["euclid?/std"]
25+
libm = ["dep:libm", "euclid?/libm"]
2626
mint = ["dep:mint"]
2727
serde = ["smallvec/serde", "dep:serde"]
2828
schemars = ["schemars/smallvec", "dep:schemars"]
29+
euclid = ["dep:euclid"]
2930

3031
[dependencies]
3132
smallvec = "1.15.0"
33+
euclid = { version = "0.22", optional = true, default-features = false }
3234

3335
[dependencies.arrayvec]
3436
version = "0.7.6"

kurbo/src/interop_euclid.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2025 the Kurbo Authors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
use euclid::UnknownUnit;
5+
6+
impl From<euclid::Vector2D<f64, UnknownUnit>> for crate::Vec2 {
7+
fn from(value: euclid::Vector2D<f64, UnknownUnit>) -> Self {
8+
Self::new(value.x, value.y)
9+
}
10+
}
11+
12+
impl From<crate::Vec2> for euclid::Vector2D<f64, UnknownUnit> {
13+
fn from(value: crate::Vec2) -> Self {
14+
Self::new(value.x, value.y)
15+
}
16+
}
17+
18+
impl From<euclid::Point2D<f64, UnknownUnit>> for crate::Point {
19+
fn from(value: euclid::Point2D<f64, UnknownUnit>) -> Self {
20+
Self::new(value.x, value.y)
21+
}
22+
}
23+
24+
impl From<crate::Point> for euclid::Point2D<f64, UnknownUnit> {
25+
fn from(value: crate::Point) -> Self {
26+
Self::new(value.x, value.y)
27+
}
28+
}
29+
30+
impl From<euclid::Size2D<f64, UnknownUnit>> for crate::Size {
31+
fn from(value: euclid::Size2D<f64, UnknownUnit>) -> Self {
32+
Self::new(value.width, value.height)
33+
}
34+
}
35+
36+
impl From<crate::Size> for euclid::Size2D<f64, UnknownUnit> {
37+
fn from(value: crate::Size) -> Self {
38+
Self::new(value.width, value.height)
39+
}
40+
}
41+
42+
impl From<euclid::Rect<f64, UnknownUnit>> for crate::Rect {
43+
fn from(value: euclid::Rect<f64, UnknownUnit>) -> Self {
44+
Self::from_origin_size(value.origin, value.size)
45+
}
46+
}
47+
48+
impl From<crate::Rect> for euclid::Rect<f64, UnknownUnit> {
49+
fn from(value: crate::Rect) -> Self {
50+
Self::new(value.origin().into(), value.size().into())
51+
}
52+
}
53+
54+
impl From<euclid::Box2D<f64, UnknownUnit>> for crate::Rect {
55+
fn from(value: euclid::Box2D<f64, UnknownUnit>) -> Self {
56+
Self::from_points(value.min, value.max)
57+
}
58+
}
59+
60+
impl From<crate::Rect> for euclid::Box2D<f64, UnknownUnit> {
61+
fn from(value: crate::Rect) -> Self {
62+
Self::new(
63+
euclid::Point2D::new(value.min_x(), value.min_y()),
64+
euclid::Point2D::new(value.max_x(), value.max_y()),
65+
)
66+
}
67+
}
68+
69+
impl From<euclid::Transform2D<f64, UnknownUnit, UnknownUnit>> for crate::Affine {
70+
fn from(t: euclid::Transform2D<f64, UnknownUnit, UnknownUnit>) -> Self {
71+
Self::new(t.to_array())
72+
}
73+
}
74+
75+
impl From<crate::Affine> for euclid::Transform2D<f64, UnknownUnit, UnknownUnit> {
76+
fn from(a: crate::Affine) -> Self {
77+
Self::from_array(a.as_coeffs())
78+
}
79+
}

kurbo/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ mod translate_scale;
142142
mod triangle;
143143
mod vec2;
144144

145+
#[cfg(feature = "euclid")]
146+
mod interop_euclid;
147+
145148
pub use crate::affine::Affine;
146149
pub use crate::arc::{Arc, ArcAppendIter};
147150
pub use crate::bezpath::{

0 commit comments

Comments
 (0)