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

hhea table #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,34 @@ impl<'a> CompoundGlyph<'a> {
}
}

struct Hhea<'a>(&'a [u8]);

impl<'a> Hhea<'a> {
fn get_ascent(&self) -> i16 {
get_i16(self.0, 4).unwrap()
}

fn get_descent(&self) -> i16 {
get_i16(self.0, 6).unwrap()
}

fn get_line_gap(&self) -> i16 {
get_i16(self.0, 8).unwrap()
}

fn get_advance_width_max(&self) -> u16 {
get_u16(self.0, 10).unwrap()
}
}

pub struct Font<'a> {
_version: u32,
_tables: HashMap<Tag, &'a [u8]>,
head: Head<'a>,
maxp: Maxp<'a>,
cmap: Option<Cmap<'a>>,
loca: Option<Loca<'a>>,
hhea: Option<Hhea<'a>>,
glyf: Option<&'a [u8]>,
encoding_index: Option<u16>,
}
Expand Down Expand Up @@ -826,6 +847,26 @@ impl<'a> Font<'a> {
None => None,
}
}

/// Gets the max descent of the font from the `hhea` table.
pub fn max_descent(&self, font_size: u32) -> Option<i32> {
self.hhea.as_ref().map(|hhea| hhea.get_descent() as i32 * font_size as i32 / self.head.units_per_em() as i32)
}

/// Gets the max ascent of the font from the `hhea` table.
pub fn max_ascent(&self, font_size: u32) -> Option<i32> {
self.hhea.as_ref().map(|hhea| hhea.get_ascent() as i32 * font_size as i32 / self.head.units_per_em() as i32)
}

/// Gets the line gap of the font from the `hhea` table.
pub fn line_gap(&self, font_size: u32) -> Option<i32> {
self.hhea.as_ref().map(|hhea| hhea.get_line_gap() as i32 * font_size as i32 / self.head.units_per_em() as i32)
}

/// Gets the max advance width of the font from the `hhea` table.
pub fn max_advance_width(&self, font_size: u32) -> Option<u32> {
self.hhea.as_ref().map(|hhea| hhea.get_advance_width_max() as u32 * font_size as u32 / self.head.units_per_em() as u32)
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -968,13 +1009,15 @@ pub fn parse(data: &[u8]) -> Result<Font, FontError> {
let loca = tables.get(&Tag::from_str("loca")).map(|&data| Loca(data));
let glyf = tables.get(&Tag::from_str("glyf")).map(|&data| data);
let cmap = tables.get(&Tag::from_str("cmap")).map(|&data| Cmap(data));
let hhea = tables.get(&Tag::from_str("hhea")).map(|&data| Hhea(data));
let encoding_index = cmap.as_ref().and_then(|cmap| cmap.find_format_4_encoding());
let f = Font {
_version: version,
_tables: tables,
head: head,
maxp: maxp,
loca: loca,
hhea: hhea,
cmap: cmap,
glyf: glyf,
encoding_index: encoding_index,
Expand Down