Add C bindings to fill and stroke styles

This commit is contained in:
Patrick Walton 2019-06-20 16:57:20 -07:00
parent 36db39883c
commit 3f601be08c
2 changed files with 48 additions and 2 deletions

View File

@ -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`

View File

@ -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 {