Skip to content

Commit 7308e25

Browse files
Conversions between CoreGraphics and Kurbo types
This is an optional feature, not enabled by default.
1 parent daaa86b commit 7308e25

4 files changed

Lines changed: 145 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 17 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ default = ["std"]
2424
std = []
2525
libm = ["dep:libm"]
2626
mint = ["dep:mint"]
27+
objc2-core-graphics = ["dep:objc2-core-foundation", "dep:objc2-core-graphics"]
2728
serde = ["smallvec/serde", "dep:serde"]
2829
schemars = ["schemars/smallvec", "dep:schemars"]
2930

@@ -42,6 +43,18 @@ optional = true
4243
version = "0.5.9"
4344
optional = true
4445

46+
[dependencies.objc2-core-foundation]
47+
version = "0.3.1"
48+
optional = true
49+
default-features = false
50+
features = ["CFCGTypes"]
51+
52+
[dependencies.objc2-core-graphics]
53+
version = "0.3.1"
54+
optional = true
55+
default-features = false
56+
features = ["CGPath"]
57+
4558
[dependencies.schemars]
4659
version = "0.8.22"
4760
optional = true
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2025 the Kurbo Authors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
use objc2_core_foundation::{CGPoint, CGRect, CGSize, CGVector};
5+
use objc2_core_graphics::{CGLineCap, CGLineJoin};
6+
7+
use crate::{Cap, Join, Point, Rect, Size, Vec2};
8+
9+
impl From<CGLineCap> for Cap {
10+
fn from(cap: CGLineCap) -> Self {
11+
match cap {
12+
CGLineCap::Butt => Cap::Butt,
13+
CGLineCap::Square => Cap::Square,
14+
CGLineCap::Round => Cap::Round,
15+
CGLineCap(i32::MIN..=-1_i32) | CGLineCap(3_i32..=i32::MAX) => {
16+
panic!("Unsupported line cap {}.", cap.0)
17+
}
18+
}
19+
}
20+
}
21+
22+
impl From<Cap> for CGLineCap {
23+
fn from(cap: Cap) -> Self {
24+
match cap {
25+
Cap::Butt => CGLineCap::Butt,
26+
Cap::Round => CGLineCap::Round,
27+
Cap::Square => CGLineCap::Square,
28+
}
29+
}
30+
}
31+
32+
impl From<CGLineJoin> for Join {
33+
fn from(join: CGLineJoin) -> Self {
34+
match join {
35+
CGLineJoin::Miter => Join::Miter,
36+
CGLineJoin::Round => Join::Round,
37+
CGLineJoin::Bevel => Join::Bevel,
38+
CGLineJoin(i32::MIN..=-1_i32) | CGLineJoin(3_i32..=i32::MAX) => {
39+
panic!("Unsupported line join {}.", join.0)
40+
}
41+
}
42+
}
43+
}
44+
45+
impl From<Join> for CGLineJoin {
46+
fn from(join: Join) -> Self {
47+
match join {
48+
Join::Bevel => CGLineJoin::Bevel,
49+
Join::Miter => CGLineJoin::Miter,
50+
Join::Round => CGLineJoin::Round,
51+
}
52+
}
53+
}
54+
55+
impl From<CGPoint> for Point {
56+
#[inline]
57+
fn from(point: CGPoint) -> Self {
58+
Point::new(point.x, point.y)
59+
}
60+
}
61+
62+
impl From<Point> for CGPoint {
63+
#[inline]
64+
fn from(point: Point) -> Self {
65+
CGPoint::new(point.x, point.y)
66+
}
67+
}
68+
69+
impl From<CGRect> for Rect {
70+
#[inline]
71+
fn from(rect: CGRect) -> Self {
72+
Rect::from_origin_size(rect.origin, rect.size)
73+
}
74+
}
75+
76+
impl From<Rect> for CGRect {
77+
#[inline]
78+
fn from(rect: Rect) -> Self {
79+
CGRect::new(
80+
CGPoint::new(rect.x0, rect.y0),
81+
CGSize::new(rect.width(), rect.height()),
82+
)
83+
}
84+
}
85+
86+
impl From<CGSize> for Size {
87+
#[inline]
88+
fn from(size: CGSize) -> Self {
89+
Size::new(size.width, size.height)
90+
}
91+
}
92+
93+
impl From<Size> for CGSize {
94+
#[inline]
95+
fn from(size: Size) -> Self {
96+
CGSize::new(size.width, size.height)
97+
}
98+
}
99+
100+
impl From<CGVector> for Vec2 {
101+
#[inline]
102+
fn from(vector: CGVector) -> Self {
103+
Vec2::new(vector.dx, vector.dy)
104+
}
105+
}
106+
107+
impl From<Vec2> for CGVector {
108+
#[inline]
109+
fn from(vector: Vec2) -> Self {
110+
CGVector::new(vector.x, vector.y)
111+
}
112+
}

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 = "objc2-core-graphics")]
146+
mod interop_objc2_core_graphics;
147+
145148
pub use crate::affine::Affine;
146149
pub use crate::arc::{Arc, ArcAppendIter};
147150
pub use crate::bezpath::{

0 commit comments

Comments
 (0)