Skip to content

Commit

Permalink
Merge pull request #269 from rockyzhengwu/master
Browse files Browse the repository at this point in the history
feat: charmap api
  • Loading branch information
bvssvni authored Feb 15, 2024
2 parents 91416e3 + 468e105 commit 989b3f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/charmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use freetype_sys::FT_CharMap;

pub struct CharMap {
raw: FT_CharMap,
}

impl CharMap {
pub fn new(raw: FT_CharMap) -> Self {
CharMap { raw }
}

pub fn platform_id(&self) -> u16 {
unsafe { (*self.raw).platform_id }
}

pub fn encoding_id(&self) -> u16 {
unsafe { (*self.raw).encoding_id }
}

pub fn encoding(&self) -> u32 {
unsafe { (*self.raw).encoding }
}

pub fn raw(&self) -> FT_CharMap {
self.raw
}
}
32 changes: 27 additions & 5 deletions src/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::ffi::{CStr, CString};
use std::fmt;
use std::num::NonZeroU32;
use std::rc::Rc;

use crate::charmap::CharMap;
use {ffi, FtResult, GlyphSlot, Matrix, Vector};

#[repr(u32)]
Expand Down Expand Up @@ -212,12 +214,13 @@ impl<BYTES> Face<BYTES> {
}
}

pub fn get_char_index(&self, charcode: usize) -> FtResult<NonZeroU32> {
pub fn get_char_index(&self, charcode: usize) -> Option<u32> {
let res = unsafe { ffi::FT_Get_Char_Index(self.raw, charcode as ffi::FT_ULong) };

// Per freetype.h, 0 means 'undefined character code' (in #return) and 'missing glyph' (in notes)
// TODO: Should this be Error::InvalidCharacterCode instead?
NonZeroU32::new(res).ok_or(crate::Error::InvalidGlyphIndex)
if res == 0 {
None
} else {
Some(res)
}
}

pub fn get_name_index(&self, glyph_name: &str) -> Option<u32> {
Expand Down Expand Up @@ -261,6 +264,20 @@ impl<BYTES> Face<BYTES> {
}
}

pub fn get_charmap(&self, charmap_index: isize) -> CharMap {
let charmap = unsafe { *self.raw().charmaps.offset(charmap_index) };
CharMap::new(charmap)
}

pub fn set_charmap(&self, charmap: &CharMap) -> FtResult<()> {
let err = unsafe { ffi::FT_Set_Charmap(self.raw, charmap.raw()) };
if err == ffi::FT_Err_Ok {
Ok(())
} else {
Err(err.into())
}
}

// According to FreeType doc, each time you load a new glyph image,
// the previous one is erased from the glyph slot.
#[inline(always)]
Expand Down Expand Up @@ -338,6 +355,11 @@ impl<BYTES> Face<BYTES> {
unsafe { (*self.raw).ascender }
}

#[inline(always)]
pub fn num_charmaps(&self) -> i32 {
unsafe { (*self.raw).num_charmaps }
}

#[inline(always)]
pub fn descender(&self) -> ffi::FT_Short {
unsafe { (*self.raw).descender }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub use stroker::{Stroker, StrokerLineCap, StrokerLineJoin};

pub mod bitmap;
pub mod bitmap_glyph;
pub mod charmap;
pub mod error;
pub mod face;
pub mod glyph;
Expand Down

0 comments on commit 989b3f9

Please sign in to comment.