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
4 changes: 2 additions & 2 deletions src/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
19 changes: 17 additions & 2 deletions src/cff/remapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::remapper::Remapper;
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};

pub type FontDictRemapper = Remapper<u8, u8>;
// FDSelect stores an 8-bit index, so the count can reach 256.
pub type FontDictRemapper = Remapper<u16, u8>;

/// Remap old SIDs to new SIDs, and also allow the insertion of
/// new strings.
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/cff/subroutines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct SubroutineCollection<'a> {

impl<'a> SubroutineCollection<'a> {
pub fn new(subroutines: Vec<Vec<CharString<'a>>>) -> Self {
debug_assert!(subroutines.len() <= 255);
Self {
subroutines: subroutines.into_iter().map(SubroutineContainer::new).collect(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/glyf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn closure(face: &Face, glyph_remapper: &mut GlyphRemapper) -> Result<()> {
let mut process_glyphs = glyph_remapper.remapped_gids().collect::<Vec<_>>();

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,
Expand Down
61 changes: 46 additions & 15 deletions src/remapper.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -58,7 +59,7 @@ impl CheckedAdd for u32 {
impl<C, T> Remapper<C, T>
where
C: CheckedAdd + Copy + From<u8>,
T: From<C> + Copy + From<u8> + Eq + Hash,
T: TryFrom<C> + Copy + Eq + Hash,
{
/// Create a new instance of a remapper.
pub fn new() -> Self
Expand All @@ -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<T> {
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
Expand Down Expand Up @@ -124,10 +127,10 @@ pub struct GlyphRemapper(Remapper<u16, u16>);

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
}
}

Expand Down Expand Up @@ -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<u16> {
self.0.try_remap(old)
}

/// Get the mapping of a glyph ID, if it has been remapped before.
Expand All @@ -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));
}
}
Loading