diff --git a/Cargo.toml b/Cargo.toml index c2e2348..0d74d14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,11 +23,15 @@ appveyor = { repository = "manuel-rhdt/harfbuzz_rs", branch = "master", service [features] default = ["build-native-harfbuzz"] build-native-harfbuzz = [] +freetype = ["dep:freetype-rs", "dep:freetype-sys"] [dependencies] rusttype = { version = "0.9", optional = true } +freetype-rs = { version = "0.36", optional = true } +freetype-sys = { version = "0.20.1", optional = true } bitflags = "^1" [build-dependencies] cc = "1.0" pkg-config = "0.3.24" + diff --git a/src/freetype.rs b/src/freetype.rs new file mode 100644 index 0000000..5a18ab6 --- /dev/null +++ b/src/freetype.rs @@ -0,0 +1,33 @@ +//! This module allows you to create a harfbuzz font from freetype faces + +use crate::Font; +use crate::bindings::hb_font_t; +use crate::common::Owned; + +use freetype as ft; + +extern "C" { + fn hb_ft_font_create_referenced(face: freetype_sys::FT_Face) -> *mut hb_font_t; +} + +impl<'a> Font<'a> { + /// Create a font from a freetype face. + /// + /// ``` + /// let font_data = include_bytes!("../Inter-Regular.otf") as &[u8]; + /// let face = harfbuzz_rs::Face::from_bytes(font_data, 0); + /// + /// let ftlib = ft::Library::init().unwrap(); + /// let ft_face = ftlib.new_memory_face2(font_data, 0).unwrap(); + /// + /// let hb_font = harfbuzz_rs::Font::from_freetype_face(ft_face.clone()); + /// ``` + pub fn from_freetype_face(mut ft_face: ft::Face<&'a [u8]>) -> Owned> { + let hb_face = unsafe { + let ft_face_ptr: freetype_sys::FT_Face = ft_face.raw_mut(); + hb_ft_font_create_referenced(ft_face_ptr) + }; + unsafe { Owned::from_raw(hb_face) } + } +} + diff --git a/src/lib.rs b/src/lib.rs index db693cc..88991c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,6 +89,9 @@ mod face; mod font; pub mod font_funcs; +#[cfg(feature = "freetype")] +pub mod freetype; + #[cfg(feature = "rusttype")] pub mod rusttype;