From 3f601be08cecefde489c103a81d6e2036818fd7f Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 20 Jun 2019 16:57:20 -0700 Subject: [PATCH] Add C bindings to fill and stroke styles --- c/include/pathfinder/pathfinder.h | 10 ++++++++ c/src/lib.rs | 40 +++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/c/include/pathfinder/pathfinder.h b/c/include/pathfinder/pathfinder.h index 2833da22..3ab80705 100644 --- a/c/include/pathfinder/pathfinder.h +++ b/c/include/pathfinder/pathfinder.h @@ -53,6 +53,8 @@ struct PFPath; typedef struct PFPath *PFPathRef; struct PFCanvasFontContext; typedef struct PFCanvasFontContext *PFCanvasFontContextRef; +struct PFFillStyle; +typedef struct PFFillStyle *PFFillStyleRef; typedef uint8_t PFLineCap; typedef uint8_t PFLineJoin; typedef uint8_t PFArcDirection; @@ -67,6 +69,10 @@ struct PFColorF { float r, g, b, a; }; typedef struct PFColorF PFColorF; +struct PFColorU { + uint8_t r, g, b, a; +}; +typedef struct PFColorU PFColorU; struct PFVector2F { float x, y; }; @@ -149,6 +155,8 @@ void PFCanvasSetLineDash(PFCanvasRef canvas, const float *new_line_dashes, size_t new_line_dash_count); void PFCanvasSetLineDashOffset(PFCanvasRef canvas, float offset); +void PFCanvasSetFillStyle(PFCanvasRef canvas, PFFillStyleRef fill_style); +void PFCanvasSetStrokeStyle(PFCanvasRef canvas, PFFillStyleRef stroke_style); void PFCanvasFillPath(PFCanvasRef canvas, PFPathRef path); void PFCanvasStrokePath(PFCanvasRef canvas, PFPathRef path); PFPathRef PFPathCreate(); @@ -176,6 +184,8 @@ void PFPathEllipse(PFPathRef path, float start_angle, float end_angle); void PFPathClosePath(PFPathRef path); +PFFillStyleRef PFFillStyleCreateColor(PFColorU color); +void PFFillStyleDestroy(PFFillStyleRef fill_style); // `gl` diff --git a/c/src/lib.rs b/c/src/lib.rs index f071bdba..b70f62c9 100644 --- a/c/src/lib.rs +++ b/c/src/lib.rs @@ -11,11 +11,11 @@ //! C bindings to Pathfinder. use gl; -use pathfinder_canvas::{CanvasFontContext, CanvasRenderingContext2D, LineJoin}; +use pathfinder_canvas::{CanvasFontContext, CanvasRenderingContext2D, FillStyle, LineJoin}; use pathfinder_canvas::{Path2D, TextMetrics}; use pathfinder_geometry::basic::rect::{RectF, RectI}; use pathfinder_geometry::basic::vector::{Vector2F, Vector2I}; -use pathfinder_geometry::color::ColorF; +use pathfinder_geometry::color::{ColorF, ColorU}; use pathfinder_geometry::outline::ArcDirection; use pathfinder_geometry::stroke::LineCap; use pathfinder_gl::{GLDevice, GLVersion}; @@ -59,6 +59,7 @@ pub const PF_RENDERER_OPTIONS_FLAGS_HAS_BACKGROUND_COLOR: u8 = 0x1; pub type PFCanvasRef = *mut CanvasRenderingContext2D; pub type PFPathRef = *mut Path2D; pub type PFCanvasFontContextRef = *mut CanvasFontContext; +pub type PFFillStyleRef = *mut FillStyle; pub type PFLineCap = u8; pub type PFLineJoin = u8; pub type PFArcDirection = u8; @@ -95,6 +96,13 @@ pub struct PFColorF { pub b: f32, pub a: f32, } +#[repr(C)] +pub struct PFColorU { + pub r: u8, + pub g: u8, + pub b: u8, + pub a: u8, +} // `gl` pub type PFGLDeviceRef = *mut GLDevice; @@ -240,6 +248,17 @@ pub unsafe extern "C" fn PFCanvasSetLineDashOffset(canvas: PFCanvasRef, new_offs (*canvas).set_line_dash_offset(new_offset) } +#[no_mangle] +pub unsafe extern "C" fn PFCanvasSetFillStyle(canvas: PFCanvasRef, fill_style: PFFillStyleRef) { + (*canvas).set_fill_style(*fill_style) +} + +#[no_mangle] +pub unsafe extern "C" fn PFCanvasSetStrokeStyle(canvas: PFCanvasRef, + stroke_style: PFFillStyleRef) { + (*canvas).set_stroke_style(*stroke_style) +} + /// Consumes the path. #[no_mangle] pub unsafe extern "C" fn PFCanvasFillPath(canvas: PFCanvasRef, path: PFPathRef) { @@ -331,6 +350,16 @@ pub unsafe extern "C" fn PFPathClosePath(path: PFPathRef) { (*path).close_path() } +#[no_mangle] +pub unsafe extern "C" fn PFFillStyleCreateColor(color: *const PFColorU) -> PFFillStyleRef { + Box::into_raw(Box::new(FillStyle::Color((*color).to_rust()))) +} + +#[no_mangle] +pub unsafe extern "C" fn PFFillStyleDestroy(fill_style: PFFillStyleRef) { + drop(Box::from_raw(fill_style)) +} + // `gl` #[no_mangle] @@ -452,6 +481,13 @@ impl PFColorF { } } +impl PFColorU { + #[inline] + pub fn to_rust(&self) -> ColorU { + ColorU { r: self.r, g: self.g, b: self.b, a: self.a } + } +} + impl PFRectF { #[inline] pub fn to_rust(&self) -> RectF {