Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose linear gradients to the C API #439

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pathfinder_color::{ColorF, ColorU};
use pathfinder_content::fill::FillRule;
use pathfinder_content::outline::ArcDirection;
use pathfinder_content::stroke::LineCap;
use pathfinder_content::gradient::{Gradient, ColorStop};
use pathfinder_geometry::rect::{RectF, RectI};
use pathfinder_geometry::transform2d::{Matrix2x2F, Transform2F};
use pathfinder_geometry::transform3d::{Perspective, Transform4F};
Expand Down Expand Up @@ -102,6 +103,7 @@ pub type FKHandleRef = *mut Handle;
pub type PFCanvasRef = *mut CanvasRenderingContext2D;
pub type PFPathRef = *mut Path2D;
pub type PFCanvasFontContextRef = *mut CanvasFontContext;
pub type PFGradientRef = *mut Gradient;
pub type PFFillStyleRef = *mut FillStyle;
pub type PFLineCap = u8;
pub type PFLineJoin = u8;
Expand All @@ -128,6 +130,11 @@ pub struct PFColorU {
pub b: u8,
pub a: u8,
}
#[repr(C)]
pub struct PFColorStop {
pub offset: f32,
pub color: PFColorU,
}

// `geometry`
#[repr(C)]
Expand Down Expand Up @@ -524,6 +531,21 @@ pub unsafe extern "C" fn PFFillStyleCreateColor(color: *const PFColorU) -> PFFil
Box::into_raw(Box::new(FillStyle::Color((*color).to_rust())))
}

#[no_mangle]
pub unsafe extern "C" fn PFGradientLinear(from: *const PFVector2F, to: *const PFVector2F) -> PFGradientRef {
Box::into_raw(Box::new(Gradient::linear_from_points((*from).to_rust(), (*to).to_rust())))
}

#[no_mangle]
pub unsafe extern "C" fn PFGradientAdd(gradient: PFGradientRef, stop: PFColorStop) {
(*gradient).add(stop.to_rust());
}

#[no_mangle]
pub unsafe extern "C" fn PFFillStyleCreateGradient(gradient: PFGradientRef) -> PFFillStyleRef {
Box::into_raw(Box::new(FillStyle::Gradient((*gradient).clone())))
}

#[no_mangle]
pub unsafe extern "C" fn PFFillStyleDestroy(fill_style: PFFillStyleRef) {
drop(Box::from_raw(fill_style))
Expand Down Expand Up @@ -867,6 +889,13 @@ impl PFColorU {
}
}

impl PFColorStop {
#[inline]
pub fn to_rust(&self) -> ColorStop {
ColorStop { offset: self.offset, color: self.color.to_rust() }
}
}

// Helpers for `geometry`

impl PFRectF {
Expand Down