Skip to content

Commit

Permalink
Fix font embedding
Browse files Browse the repository at this point in the history
All glyphs were not on width table
  • Loading branch information
RagibHasin committed Jan 24, 2023
1 parent dca1956 commit d046137
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,23 @@ impl ExternalFont {
// scale the font width so that it sort-of fits into an 1000 unit square
let percentage_font_scaling = 1000.0 / (face_metrics.units_per_em as f64);

for (gid, width) in widths {
if gid == current_high_gid {
current_width_vec.push(Integer((width as f64 * percentage_font_scaling) as i64));
current_high_gid += 1;
for gid in 0..self.font_data.glyph_count() {
if let Some(GlyphMetrics { width, .. }) = self.font_data.glyph_metrics(gid) {
if gid == current_high_gid {
current_width_vec
.push(Integer((width as f64 * percentage_font_scaling) as i64));
current_high_gid += 1;
} else {
widths_list.push(Integer(current_low_gid as i64));
widths_list.push(Array(current_width_vec.drain(..).collect()));

current_width_vec
.push(Integer((width as f64 * percentage_font_scaling) as i64));
current_low_gid = gid;
current_high_gid = gid + 1;
}
} else {
widths_list.push(Integer(current_low_gid as i64));
widths_list.push(Array(current_width_vec.drain(..).collect()));

current_width_vec.push(Integer((width as f64 * percentage_font_scaling) as i64));
current_low_gid = gid;
current_high_gid = gid + 1;
continue;
}
}
// push the last widths, because the loop is delayed by one iteration
Expand Down Expand Up @@ -507,6 +513,9 @@ pub trait FontData: FontDataClone + std::fmt::Debug {
/// Returns a mapping from glyph IDs to Unicode characters for all supported characters.
fn glyph_ids(&self) -> std::collections::HashMap<u16, char>;

/// Returns the number of glyphs in this font.
fn glyph_count(&self) -> u16;

/// Returns the glyph metrics for a glyph of this font, if available.
fn glyph_metrics(&self, glyph_id: u16) -> Option<GlyphMetrics>;
}
Expand Down Expand Up @@ -571,6 +580,10 @@ impl FontData for TtfFace {
map
}

fn glyph_count(&self) -> u16 {
self.face().number_of_glyphs()
}

fn glyph_metrics(&self, glyph_id: u16) -> Option<GlyphMetrics> {
let glyph_id = owned_ttf_parser::GlyphId(glyph_id);
if let Some(width) = self.face().glyph_hor_advance(glyph_id) {
Expand Down

0 comments on commit d046137

Please sign in to comment.