Skip to content

Commit 85e1618

Browse files
WIP
1 parent 609a159 commit 85e1618

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/font.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,13 @@ impl ParsedFont {
439439
}
440440

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

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

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

@@ -590,7 +590,6 @@ impl ParsedFont {
590590
})
591591
})
592592
.flatten()
593-
.map(|x| x)
594593
.collect::<BTreeSet<_>>();
595594

596595
if codepoints.is_empty() && chars.is_empty() {
@@ -682,27 +681,34 @@ impl ParsedFont {
682681
pub(crate) fn generate_cmap_string(
683682
&self,
684683
font_id: &FontId,
685-
gid_to_cid_map: &[(u16, u16)],
684+
gid_to_cid_map: &[(u16, u32)],
686685
) -> String {
687686
// Convert the glyph_ids map to a ToUnicodeCMap structure
688687
let mappings = gid_to_cid_map
689688
.iter()
690-
.map(|&(gid, cp)| (gid as u32, vec![cp as u32]))
689+
.map(|&(gid, cp)| (gid as u32, vec![cp]))
691690
.collect();
692691

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

698-
pub(crate) fn generate_gid_to_cid_map(&self, glyph_ids: &[u16]) -> Vec<(u16, u16)> {
697+
pub(crate) fn generate_gid_to_cid_map(&self, glyph_ids: &[(u16, char)]) -> Vec<(u16, u32)> {
699698
glyph_ids
700699
.iter()
701-
.filter_map(|gid| self.index_to_cid(*gid).map(|cid| (*gid, cid)))
700+
.map(|&(gid, c)| {
701+
self.index_to_cid(gid)
702+
.map(|cid| (gid, cid as u32))
703+
.unwrap_or((gid, c as u32))
704+
})
702705
.collect()
703706
}
704707

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

714-
for &gid in glyph_ids {
720+
for &(gid, _) in glyph_ids {
715721
let width = match self.get_glyph_width_internal(gid) {
716722
Some(s) => s,
717723
None => match self.get_space_width() {
@@ -744,7 +750,7 @@ impl ParsedFont {
744750

745751
pub(crate) fn get_normalized_widths_cff(
746752
&self,
747-
gid_to_cid_map: &[(u16, u16)],
753+
gid_to_cid_map: &[(u16, u32)],
748754
) -> Vec<lopdf::Object> {
749755
let mut widths_list = Vec::new();
750756

@@ -1447,8 +1453,7 @@ impl ParsedFont {
14471453
}
14481454

14491455
let glyph_index = glyph_index as u16;
1450-
// Create identity map for compatibility with CFF fonts
1451-
index_to_cid.insert(glyph_index, glyph_index);
1456+
14521457
let horz_advance = match allsorts_subset_browser::glyph_info::advance(
14531458
&maxp_table,
14541459
&hhea_table,

src/serialize.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ fn encode_text_items_to_pdf<T: PrepFont>(
722722
let bytes = if true {
723723
text.chars()
724724
.flat_map(|c| {
725-
font.lgi(c as u32)
726-
.and_then(|src_gdi| font.index_to_cid(src_gdi as u16))
725+
font.lookup_glyph_index(c as u32)
726+
.map(|src_gdi| font.index_to_cid(src_gdi).unwrap_or(src_gdi))
727727
.unwrap_or(0)
728728
.to_be_bytes()
729729
})
@@ -848,9 +848,11 @@ impl PreparedFont {
848848
let font = ParsedFont::from_bytes(&subset.bytes, 0, warnings)?;
849849
assert_eq!(font.original_bytes.len(), subset.bytes.len());
850850

851-
let new_glyph_ids: Vec<u16> = glyph_ids
851+
let new_glyph_ids: Vec<_> = glyph_ids
852852
.iter()
853-
.filter_map(|(orig_gid, _)| subset.glyph_mapping.get(orig_gid).map(|(gid, _)| *gid))
853+
.filter_map(|(orig_gid, _)| {
854+
subset.glyph_mapping.get(orig_gid).map(|&(gid, c)| (gid, c))
855+
})
854856
.collect();
855857

856858
let gid_to_cid_map = font.generate_gid_to_cid_map(&new_glyph_ids);
@@ -874,9 +876,10 @@ impl PreparedFont {
874876
})
875877
}
876878
}
879+
877880
impl PrepFont for PreparedFont {
878-
fn lgi(&self, codepoint: u32) -> Option<u32> {
879-
self.original.lgi(codepoint) // .lookup_glyph_index(codepoint).map(Into::into)
881+
fn lookup_glyph_index(&self, codepoint: u32) -> Option<u16> {
882+
self.original.lookup_glyph_index(codepoint)
880883
}
881884

882885
fn index_to_cid(&self, index: u16) -> Option<u16> {

0 commit comments

Comments
 (0)