Add some font functions to the C API

This commit is contained in:
Patrick Walton 2019-06-21 13:17:11 -07:00
parent 1caad35b46
commit 1c34b12948
3 changed files with 28 additions and 0 deletions

1
Cargo.lock generated
View File

@ -1397,6 +1397,7 @@ name = "pathfinder_c"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cbindgen 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "cbindgen 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)",
"font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gl 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "gl 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
"pathfinder_canvas 0.1.0", "pathfinder_canvas 0.1.0",

View File

@ -9,6 +9,7 @@ build = "build.rs"
crate-type = ["staticlib"] crate-type = ["staticlib"]
[dependencies] [dependencies]
font-kit = "0.2"
gl = "0.6" gl = "0.6"
libc = "0.2" libc = "0.2"

View File

@ -10,6 +10,7 @@
//! C bindings to Pathfinder. //! C bindings to Pathfinder.
use font_kit::handle::Handle;
use gl; use gl;
use pathfinder_canvas::{CanvasFontContext, CanvasRenderingContext2D, FillStyle, LineJoin}; use pathfinder_canvas::{CanvasFontContext, CanvasRenderingContext2D, FillStyle, LineJoin};
use pathfinder_canvas::{Path2D, TextMetrics}; use pathfinder_canvas::{Path2D, TextMetrics};
@ -55,6 +56,9 @@ pub const PF_RENDERER_OPTIONS_FLAGS_HAS_BACKGROUND_COLOR: u8 = 0x1;
// Types // Types
// External: `font-kit`
pub type FKHandleRef = *mut Handle;
// `canvas` // `canvas`
pub type PFCanvasRef = *mut CanvasRenderingContext2D; pub type PFCanvasRef = *mut CanvasRenderingContext2D;
pub type PFPathRef = *mut Path2D; pub type PFPathRef = *mut Path2D;
@ -154,6 +158,16 @@ pub unsafe extern "C" fn PFCanvasFontContextCreateWithSystemSource() -> PFCanvas
Box::into_raw(Box::new(CanvasFontContext::from_system_source())) Box::into_raw(Box::new(CanvasFontContext::from_system_source()))
} }
#[no_mangle]
pub unsafe extern "C" fn PFCanvasFontContextCreateWithFonts(fonts: *const FKHandleRef,
font_count: usize)
-> PFCanvasFontContextRef {
let fonts = slice::from_raw_parts(fonts, font_count);
Box::into_raw(Box::new(CanvasFontContext::from_fonts(fonts.into_iter().map(|font| {
(**font).clone()
}))))
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn PFCanvasFontContextDestroy(font_context: PFCanvasFontContextRef) { pub unsafe extern "C" fn PFCanvasFontContextDestroy(font_context: PFCanvasFontContextRef) {
drop(Box::from_raw(font_context)) drop(Box::from_raw(font_context))
@ -250,6 +264,18 @@ pub unsafe extern "C" fn PFCanvasSetLineDashOffset(canvas: PFCanvasRef, new_offs
(*canvas).set_line_dash_offset(new_offset) (*canvas).set_line_dash_offset(new_offset)
} }
#[no_mangle]
pub unsafe extern "C" fn PFCanvasSetFontByPostScriptName(canvas: PFCanvasRef,
postscript_name: *const c_char,
postscript_name_len: usize) {
(*canvas).set_font_by_postscript_name(to_rust_string(&postscript_name, postscript_name_len))
}
#[no_mangle]
pub unsafe extern "C" fn PFCanvasSetFontSize(canvas: PFCanvasRef, new_font_size: f32) {
(*canvas).set_font_size(new_font_size)
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn PFCanvasSetFillStyle(canvas: PFCanvasRef, fill_style: PFFillStyleRef) { pub unsafe extern "C" fn PFCanvasSetFillStyle(canvas: PFCanvasRef, fill_style: PFFillStyleRef) {
(*canvas).set_fill_style(*fill_style) (*canvas).set_fill_style(*fill_style)