Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ trivial-copy-size-limit = 16
# END LINEBENDER LINT SET

# Don't warn about these identifiers when using clippy::doc_markdown.
doc-valid-idents = ["Direct2D", "PostScript", ".."]
doc-valid-idents = ["AppKit", "Direct2D", "PostScript", ".."]
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ You can find its changes [documented below](#0131-2026-05-13).

## [Unreleased][]

## Added

- Interop with the Apple platform types via the `objc2` feature. ([#450][] by [@waywardmonkeys][])

This release has an [MSRV][] of 1.85.

## [0.13.1][] (2026-05-13)
Expand Down Expand Up @@ -258,6 +262,7 @@ Note: A changelog was not kept for or before this release
[#429]: https://github.com/linebender/kurbo/pull/429
[#447]: https://github.com/linebender/kurbo/pull/447
[#448]: https://github.com/linebender/kurbo/pull/448
[#450]: https://github.com/linebender/kurbo/pull/450
[#451]: https://github.com/linebender/kurbo/pull/451
[#452]: https://github.com/linebender/kurbo/pull/452
[#454]: https://github.com/linebender/kurbo/pull/454
Expand Down
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ criterion = { version = "0.7.0", default-features = false }
euclid = { version = "0.22", default-features = false }
libm = "0.2.16"
mint = "0.5.9"
objc2-core-foundation = { version = "0.3.2", default-features = false }
objc2-core-graphics = { version = "0.3.2", default-features = false }
rand = "0.9.4"
schemars = "0.8.22"
serde = { version = "1.0.228", default-features = false }
Expand Down
9 changes: 7 additions & 2 deletions kurbo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ rust-version.workspace = true

[package.metadata.docs.rs]
all-features = true
# There are no platform specific docs.
default-target = "x86_64-unknown-linux-gnu"
targets = []
targets = ["aarch64-apple-darwin"]

[features]
default = ["std"]
Expand All @@ -26,6 +25,8 @@ libm = ["dep:libm", "polycool/libm", "euclid?/libm"]
mint = ["dep:mint"]
# Enable `From`/`Into` conversion of Kurbo and euclid types
euclid = ["dep:euclid"]
# Enable conversion of Kurbo and Apple platform types on 64-bit Apple targets.
objc2 = ["dep:objc2-core-foundation", "dep:objc2-core-graphics"]
# Implement `serde::Deserialize` and `serde::Serialize` on various types
serde = ["smallvec/serde", "dep:serde"]
# Add best-effort support for using Kurbo types in JSON schemas using schemars
Expand All @@ -41,6 +42,10 @@ schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true, features = ["alloc", "derive"] }
smallvec = { workspace = true, features = ["const_new"] }

[target.'cfg(all(target_vendor = "apple", target_pointer_width = "64"))'.dependencies]
objc2-core-foundation = { workspace = true, optional = true, features = ["CFCGTypes"] }
objc2-core-graphics = { workspace = true, optional = true, features = ["CGPath"] }

[dev-dependencies]
# This is used for research but not really needed; maybe refactor.
rand = { workspace = true }
Expand Down
223 changes: 223 additions & 0 deletions kurbo/src/interop_objc2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Copyright 2025 the Kurbo Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Interoperability with Apple graphics types from `objc2` crates.
//!
//! `objc2-foundation` defines `NSPoint`, `NSSize`, and `NSRect` as aliases of
//! `CGPoint`, `CGSize`, and `CGRect`, so these conversions also cover the
//! AppKit geometry names without depending on AppKit.

use objc2_core_foundation::{CGAffineTransform, CGPoint, CGRect, CGSize, CGVector};
use objc2_core_graphics::{CGLineCap, CGLineJoin};

use crate::{Affine, Cap, Join, Point, Rect, Size, Vec2};

impl TryFrom<CGLineCap> for Cap {
type Error = CGLineCap;

fn try_from(cap: CGLineCap) -> Result<Self, Self::Error> {
match cap {
CGLineCap::Butt => Ok(Cap::Butt),
CGLineCap::Square => Ok(Cap::Square),
CGLineCap::Round => Ok(Cap::Round),
_ => Err(cap),
}
}
}

impl From<Cap> for CGLineCap {
fn from(cap: Cap) -> Self {
match cap {
Cap::Butt => CGLineCap::Butt,
Cap::Round => CGLineCap::Round,
Cap::Square => CGLineCap::Square,
}
}
}

impl TryFrom<CGLineJoin> for Join {
type Error = CGLineJoin;

fn try_from(join: CGLineJoin) -> Result<Self, Self::Error> {
match join {
CGLineJoin::Miter => Ok(Join::Miter),
CGLineJoin::Round => Ok(Join::Round),
CGLineJoin::Bevel => Ok(Join::Bevel),
_ => Err(join),
}
}
}

impl From<Join> for CGLineJoin {
fn from(join: Join) -> Self {
match join {
Join::Bevel => CGLineJoin::Bevel,
Join::Miter => CGLineJoin::Miter,
Join::Round => CGLineJoin::Round,
}
}
}

impl From<CGPoint> for Point {
#[inline]
fn from(point: CGPoint) -> Self {
Point::new(point.x, point.y)
}
}

impl From<Point> for CGPoint {
#[inline]
fn from(point: Point) -> Self {
CGPoint::new(point.x, point.y)
}
}

impl From<CGRect> for Rect {
#[inline]
fn from(rect: CGRect) -> Self {
Rect::from_origin_size(rect.origin, rect.size)
}
}

impl From<Rect> for CGRect {
#[inline]
fn from(rect: Rect) -> Self {
CGRect::new(
CGPoint::new(rect.x0, rect.y0),
CGSize::new(rect.width(), rect.height()),
)
}
}

impl From<CGSize> for Size {
#[inline]
fn from(size: CGSize) -> Self {
Size::new(size.width, size.height)
}
}

impl From<Size> for CGSize {
#[inline]
fn from(size: Size) -> Self {
CGSize::new(size.width, size.height)
}
}

impl From<CGVector> for Vec2 {
#[inline]
fn from(vector: CGVector) -> Self {
Vec2::new(vector.dx, vector.dy)
}
}

impl From<Vec2> for CGVector {
#[inline]
fn from(vector: Vec2) -> Self {
CGVector::new(vector.x, vector.y)
}
}

impl From<CGAffineTransform> for Affine {
#[inline]
fn from(transform: CGAffineTransform) -> Self {
Affine::new([
transform.a,
transform.b,
transform.c,
transform.d,
transform.tx,
transform.ty,
])
}
}

impl From<Affine> for CGAffineTransform {
#[inline]
fn from(transform: Affine) -> Self {
let [a, b, c, d, tx, ty] = transform.as_coeffs();
CGAffineTransform { a, b, c, d, tx, ty }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn line_cap_conversion() {
assert_eq!(Cap::try_from(CGLineCap::Butt), Ok(Cap::Butt));
assert_eq!(Cap::try_from(CGLineCap::Round), Ok(Cap::Round));
assert_eq!(Cap::try_from(CGLineCap::Square), Ok(Cap::Square));
assert_eq!(Cap::try_from(CGLineCap(-1)), Err(CGLineCap(-1)));
assert_eq!(Cap::try_from(CGLineCap(3)), Err(CGLineCap(3)));

assert_eq!(CGLineCap::from(Cap::Butt), CGLineCap::Butt);
assert_eq!(CGLineCap::from(Cap::Round), CGLineCap::Round);
assert_eq!(CGLineCap::from(Cap::Square), CGLineCap::Square);
}

#[test]
fn line_join_conversion() {
assert_eq!(Join::try_from(CGLineJoin::Miter), Ok(Join::Miter));
assert_eq!(Join::try_from(CGLineJoin::Round), Ok(Join::Round));
assert_eq!(Join::try_from(CGLineJoin::Bevel), Ok(Join::Bevel));
assert_eq!(Join::try_from(CGLineJoin(-1)), Err(CGLineJoin(-1)));
assert_eq!(Join::try_from(CGLineJoin(3)), Err(CGLineJoin(3)));

assert_eq!(CGLineJoin::from(Join::Miter), CGLineJoin::Miter);
assert_eq!(CGLineJoin::from(Join::Round), CGLineJoin::Round);
assert_eq!(CGLineJoin::from(Join::Bevel), CGLineJoin::Bevel);
}

#[test]
fn point_conversion() {
let point = Point::new(1.25, -2.5);
let cg_point = CGPoint::new(1.25, -2.5);

assert_eq!(CGPoint::from(point), cg_point);
assert_eq!(Point::from(cg_point), point);
}

#[test]
fn rect_conversion() {
let rect = Rect::new(1.0, 2.0, 4.0, 6.0);
let cg_rect = CGRect::new(CGPoint::new(1.0, 2.0), CGSize::new(3.0, 4.0));

assert_eq!(CGRect::from(rect), cg_rect);
assert_eq!(Rect::from(cg_rect), rect);
}

#[test]
fn size_conversion() {
let size = Size::new(7.0, 9.0);
let cg_size = CGSize::new(7.0, 9.0);

assert_eq!(CGSize::from(size), cg_size);
assert_eq!(Size::from(cg_size), size);
}

#[test]
fn vector_conversion() {
let vector = Vec2::new(-3.0, 8.0);
let cg_vector = CGVector::new(-3.0, 8.0);

assert_eq!(CGVector::from(vector), cg_vector);
assert_eq!(Vec2::from(cg_vector), vector);
}

#[test]
fn affine_conversion() {
let affine = Affine::new([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let cg_affine = CGAffineTransform {
a: 1.0,
b: 2.0,
c: 3.0,
d: 4.0,
tx: 5.0,
ty: 6.0,
};

assert_eq!(CGAffineTransform::from(affine), cg_affine);
assert_eq!(Affine::from(cg_affine), affine);
}
}
10 changes: 10 additions & 0 deletions kurbo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
//! - `euclid`: Enable `From`/`Into` conversion of Kurbo and [euclid][] types.
//! Note that if you're using both Kurbo and euclid at the same time, you *must*
//! also enable one of euclid's `std` or `libm` features.
//! - `objc2`: Enable conversion between Kurbo types and Apple Core Graphics
//! geometry and stroke types on 64-bit Apple targets, using
//! `objc2-core-foundation` and `objc2-core-graphics`.
//! - `serde`: Implement `serde::Deserialize` and `serde::Serialize` on various types.
//! - `schemars`: Add best-effort support for using Kurbo types in JSON schemas using [schemars][].
//!
Expand Down Expand Up @@ -151,6 +154,13 @@ mod vec2;
#[cfg(feature = "euclid")]
mod interop_euclid;

#[cfg(all(
feature = "objc2",
target_vendor = "apple",
target_pointer_width = "64"
))]
mod interop_objc2;

pub use crate::affine::Affine;
pub use crate::arc::{Arc, ArcAppendIter};
pub use crate::axis::Axis;
Expand Down
Loading