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

Add optional freetype integration #39

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

33 changes: 33 additions & 0 deletions src/freetype.rs
Original file line number Diff line number Diff line change
@@ -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<Font<'a>> {
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) }
}
}

3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ mod face;
mod font;
pub mod font_funcs;

#[cfg(feature = "freetype")]
pub mod freetype;

#[cfg(feature = "rusttype")]
pub mod rusttype;

Expand Down