Skip to content
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
29 changes: 17 additions & 12 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,13 @@ impl ParsedFont {
}

pub trait PrepFont {
fn lgi(&self, codepoint: u32) -> Option<u32>;
fn lookup_glyph_index(&self, codepoint: u32) -> Option<u16>;

fn index_to_cid(&self, index: u16) -> Option<u16>;
}

impl PrepFont for ParsedFont {
fn lgi(&self, codepoint: u32) -> Option<u32> {
fn lookup_glyph_index(&self, codepoint: u32) -> Option<u16> {
self.lookup_glyph_index(codepoint).map(Into::into)
}

Expand Down Expand Up @@ -590,7 +590,6 @@ impl ParsedFont {
})
})
.flatten()
.map(|x| x)
.collect::<BTreeSet<_>>();

if codepoints.is_empty() && chars.is_empty() {
Expand Down Expand Up @@ -682,27 +681,34 @@ impl ParsedFont {
pub(crate) fn generate_cmap_string(
&self,
font_id: &FontId,
gid_to_cid_map: &[(u16, u16)],
gid_to_cid_map: &[(u16, u32)],
) -> String {
// Convert the glyph_ids map to a ToUnicodeCMap structure
let mappings = gid_to_cid_map
.iter()
.map(|&(gid, cp)| (gid as u32, vec![cp as u32]))
.map(|&(gid, cp)| (gid as u32, vec![cp]))
.collect();

// Create the CMap and generate its string representation
let cmap = ToUnicodeCMap { mappings };
cmap.to_cmap_string(&font_id.0)
}

pub(crate) fn generate_gid_to_cid_map(&self, glyph_ids: &[u16]) -> Vec<(u16, u16)> {
pub(crate) fn generate_gid_to_cid_map(&self, glyph_ids: &[(u16, char)]) -> Vec<(u16, u32)> {
glyph_ids
.iter()
.filter_map(|gid| self.index_to_cid(*gid).map(|cid| (*gid, cid)))
.map(|&(gid, c)| {
self.index_to_cid(gid)
.map(|cid| (gid, cid as u32))
.unwrap_or((gid, c as u32))
})
.collect()
}

pub(crate) fn get_normalized_widths_ttf(&self, glyph_ids: &[u16]) -> Vec<lopdf::Object> {
pub(crate) fn get_normalized_widths_ttf(
&self,
glyph_ids: &[(u16, char)],
) -> Vec<lopdf::Object> {
let mut widths_list = Vec::new();
let mut current_low_gid = 0;
let mut current_high_gid = 0;
Expand All @@ -711,7 +717,7 @@ impl ParsedFont {
// scale the font width so that it sort-of fits into an 1000 unit square
let percentage_font_scaling = 1000.0 / (self.font_metrics.units_per_em as f32);

for &gid in glyph_ids {
for &(gid, _) in glyph_ids {
let width = match self.get_glyph_width_internal(gid) {
Some(s) => s,
None => match self.get_space_width() {
Expand Down Expand Up @@ -744,7 +750,7 @@ impl ParsedFont {

pub(crate) fn get_normalized_widths_cff(
&self,
gid_to_cid_map: &[(u16, u16)],
gid_to_cid_map: &[(u16, u32)],
) -> Vec<lopdf::Object> {
let mut widths_list = Vec::new();

Expand Down Expand Up @@ -1447,8 +1453,7 @@ impl ParsedFont {
}

let glyph_index = glyph_index as u16;
// Create identity map for compatibility with CFF fonts
index_to_cid.insert(glyph_index, glyph_index);

let horz_advance = match allsorts_subset_browser::glyph_info::advance(
&maxp_table,
&hhea_table,
Expand Down
15 changes: 9 additions & 6 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ fn encode_text_items_to_pdf<T: PrepFont>(
let bytes = if true {
text.chars()
.flat_map(|c| {
font.lgi(c as u32)
.and_then(|src_gdi| font.index_to_cid(src_gdi as u16))
font.lookup_glyph_index(c as u32)
.map(|src_gdi| font.index_to_cid(src_gdi).unwrap_or(src_gdi))
.unwrap_or(0)
.to_be_bytes()
})
Expand Down Expand Up @@ -848,9 +848,11 @@ impl PreparedFont {
let font = ParsedFont::from_bytes(&subset.bytes, 0, warnings)?;
assert_eq!(font.original_bytes.len(), subset.bytes.len());

let new_glyph_ids: Vec<u16> = glyph_ids
let new_glyph_ids: Vec<_> = glyph_ids
.iter()
.filter_map(|(orig_gid, _)| subset.glyph_mapping.get(orig_gid).map(|(gid, _)| *gid))
.filter_map(|(orig_gid, _)| {
subset.glyph_mapping.get(orig_gid).map(|&(gid, c)| (gid, c))
})
.collect();

let gid_to_cid_map = font.generate_gid_to_cid_map(&new_glyph_ids);
Expand All @@ -874,9 +876,10 @@ impl PreparedFont {
})
}
}

impl PrepFont for PreparedFont {
fn lgi(&self, codepoint: u32) -> Option<u32> {
self.original.lgi(codepoint) // .lookup_glyph_index(codepoint).map(Into::into)
fn lookup_glyph_index(&self, codepoint: u32) -> Option<u16> {
self.original.lookup_glyph_index(codepoint)
}

fn index_to_cid(&self, index: u16) -> Option<u16> {
Expand Down
Loading