From 3cd654aafd4b855c1628cd34bf2cede3d86e2d17 Mon Sep 17 00:00:00 2001 From: Sylvain BOILY Date: Thu, 19 Nov 2026 09:37:58 -0500 Subject: [PATCH] Check glyph remapping limits and support all CFF font dict indices --- src/cff/mod.rs | 4 +-- src/cff/remapper.rs | 19 +++++++++++-- src/cff/subroutines.rs | 1 - src/glyf.rs | 2 +- src/remapper.rs | 61 +++++++++++++++++++++++++++++++----------- 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/cff/mod.rs b/src/cff/mod.rs index f3321467..5d10ee49 100644 --- a/src/cff/mod.rs +++ b/src/cff/mod.rs @@ -107,7 +107,7 @@ struct Offsets { } impl Offsets { - pub fn new_cid(num_font_dicts: u8) -> Self { + pub fn new_cid(num_font_dicts: u16) -> Self { Self { char_strings_offset: DUMMY_OFFSET, charset_offset: DUMMY_OFFSET, @@ -291,7 +291,7 @@ fn subset_charstrings( let mut fd_remapper = FontDictRemapper::new(); for fd in used_fds { - fd_remapper.remap(fd); + fd_remapper.try_remap(fd)?; } Ok((char_strings.iter().map(|p| p.compile()).collect(), fd_remapper)) diff --git a/src/cff/remapper.rs b/src/cff/remapper.rs index f6a33bfe..dd2aa1a9 100644 --- a/src/cff/remapper.rs +++ b/src/cff/remapper.rs @@ -3,7 +3,8 @@ use crate::remapper::Remapper; use std::borrow::Cow; use std::collections::{BTreeMap, HashMap}; -pub type FontDictRemapper = Remapper; +// FDSelect stores an 8-bit index, so the count can reach 256. +pub type FontDictRemapper = Remapper; /// Remap old SIDs to new SIDs, and also allow the insertion of /// new strings. @@ -73,9 +74,23 @@ impl<'a> SidRemapper<'a> { #[cfg(test)] mod tests { use crate::cff::number::StringId; - use crate::cff::remapper::SidRemapper; + use crate::cff::remapper::{FontDictRemapper, SidRemapper}; + use crate::cff::subroutines::SubroutineCollection; use std::borrow::Cow; + #[test] + fn all_font_dict_indices_are_addressable() { + let subrs = SubroutineCollection::new(vec![vec![]; 256]); + assert!(subrs.get_handler(u8::MAX).is_some()); + + let mut map = FontDictRemapper::new(); + for fd in 0..=u8::MAX { + assert_eq!(map.try_remap(fd), Ok(fd)); + } + assert_eq!(map.len(), 256); + assert_eq!(map.get(u8::MAX), Some(u8::MAX)); + } + #[test] fn test_remap_1() { let mut sid_remapper = SidRemapper::new(); diff --git a/src/cff/subroutines.rs b/src/cff/subroutines.rs index 4c6181a7..60426ac2 100644 --- a/src/cff/subroutines.rs +++ b/src/cff/subroutines.rs @@ -8,7 +8,6 @@ pub struct SubroutineCollection<'a> { impl<'a> SubroutineCollection<'a> { pub fn new(subroutines: Vec>>) -> Self { - debug_assert!(subroutines.len() <= 255); Self { subroutines: subroutines.into_iter().map(SubroutineContainer::new).collect(), } diff --git a/src/glyf.rs b/src/glyf.rs index 2c2a6c6d..291a3fda 100644 --- a/src/glyf.rs +++ b/src/glyf.rs @@ -18,7 +18,7 @@ pub fn closure(face: &Face, glyph_remapper: &mut GlyphRemapper) -> Result<()> { let mut process_glyphs = glyph_remapper.remapped_gids().collect::>(); while let Some(glyph) = process_glyphs.pop() { - glyph_remapper.remap(glyph); + glyph_remapper.try_remap(glyph)?; let glyph_data = match table.glyph_data(glyph) { Some(glph_data) => glph_data, diff --git a/src/remapper.rs b/src/remapper.rs index fe38eb70..dc98892e 100644 --- a/src/remapper.rs +++ b/src/remapper.rs @@ -1,4 +1,5 @@ -use std::collections::BTreeSet; +use crate::{Error, Result}; +use std::collections::{hash_map::Entry, BTreeSet}; use std::hash::{Hash, Hasher}; use std::ops::Add; @@ -58,7 +59,7 @@ impl CheckedAdd for u32 { impl Remapper where C: CheckedAdd + Copy + From, - T: From + Copy + From + Eq + Hash, + T: TryFrom + Copy + Eq + Hash, { /// Create a new instance of a remapper. pub fn new() -> Self @@ -81,16 +82,18 @@ where /// Remap a new value, either returning the previously assigned number /// if it already has been remapped, and assigning a new number if it /// has not been remapped. - pub fn remap(&mut self, old: T) -> T { - *self.forward.entry(old).or_insert_with(|| { - let value = self.counter; - self.backward.push(old); - self.counter = self - .counter - .checked_add(&C::from(1)) - .expect("remapper was overflowed"); - value.into() - }) + pub fn try_remap(&mut self, old: T) -> Result { + match self.forward.entry(old) { + Entry::Occupied(entry) => Ok(*entry.get()), + Entry::Vacant(entry) => { + let value = + T::try_from(self.counter).map_err(|_| Error::OverflowError)?; + self.counter = + self.counter.checked_add(&C::from(1)).ok_or(Error::OverflowError)?; + self.backward.push(old); + Ok(*entry.insert(value)) + } + } } /// Get the number of elements that have been remapped. Assumes that @@ -124,10 +127,10 @@ pub struct GlyphRemapper(Remapper); impl Default for GlyphRemapper { fn default() -> Self { - let mut remapper = Remapper::new(); + let mut remapper = Self(Remapper::new()); // .notdef is always a part of a subset. remapper.remap(0); - Self(remapper) + remapper } } @@ -165,9 +168,19 @@ impl GlyphRemapper { /// Remap a glyph ID, or return the existing mapping if the /// glyph ID has already been remapped before. + /// + /// # Panics + /// Panics if the subset would contain more than 65,535 glyphs. #[inline] pub fn remap(&mut self, old: u16) -> u16 { - self.0.remap(old) + self.try_remap(old).expect("remapper was overflowed") + } + + /// Remap a glyph ID, returning [`Error::OverflowError`] if the new glyph + /// count would exceed the OpenType limit. Existing mappings remain valid. + #[inline] + pub fn try_remap(&mut self, old: u16) -> Result { + self.0.try_remap(old) } /// Get the mapping of a glyph ID, if it has been remapped before. @@ -188,3 +201,21 @@ impl GlyphRemapper { self.0.backward.iter().copied() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn full_glyph_remapper_keeps_existing_mappings() { + let mut map = GlyphRemapper::new(); + for glyph in 1..u16::MAX { + assert_eq!(map.try_remap(glyph), Ok(glyph)); + } + assert_eq!(map.try_remap(u16::MAX), Err(Error::OverflowError)); + assert_eq!(map.get(u16::MAX), None); + assert_eq!(map.num_gids(), u16::MAX); + assert_eq!(map.remapped_gids().count(), usize::from(u16::MAX)); + assert_eq!(map.try_remap(0), Ok(0)); + } +}