mod base;
mod cblc;
mod cff;
mod cmap;
mod colr;
mod cpal;
mod fvar;
mod gdef;
mod glyf_loca;
mod gpos;
mod graph;
mod gsub;
mod gsubgpos;
mod gvar;
mod hdmx;
mod head;
mod hmtx;
mod hvar;
mod inc_bimap;
mod layout;
mod math;
mod maxp;
mod metrics;
mod name;
mod offset;
mod offset_array;
mod os2;
mod parsing_util;
mod post;
mod priority_queue;
mod repack;
mod sbix;
pub mod serialize;
mod stat;
mod unicode_closure;
mod variations;
mod vmtx;
mod vorg;
mod vvar;
use crate::repack::resolve_overflows;
use gdef::CollectUsedMarkSets;
use inc_bimap::IncBiMap;
use layout::{
collect_feature_variations_name_ids, collect_features_with_retained_subs,
collect_variable_feature_indices, find_duplicate_features, prune_features,
remap_feature_indices, PruneLangSysContext, SubsetLayoutContext,
};
pub use parsing_util::{
parse_glyph_map, parse_name_ids, parse_name_languages, parse_tag_list, parse_unicodes,
populate_gids,
};
use unicode_closure::unicode_closure;
use read_fonts::{
collections::{int_set::Domain, IntSet},
tables::{
avar::Avar,
base::Base,
cbdt::Cbdt,
cblc::Cblc,
cff::Cff,
cff2::Cff2,
cmap::{Cmap, Dmap, PlatformId},
colr::Colr,
cpal::Cpal,
cvar::Cvar,
fvar::Fvar,
gasp,
gdef::Gdef,
glyf::{Glyf, Glyph},
gpos::Gpos,
gsub::Gsub,
gvar::{Gvar, GvarExtended},
hdmx::Hdmx,
head::Head,
hhea::{Hhea, HheaExtended},
hmtx::{Hmtx, HmtxExtended},
hvar::Hvar,
loca::Loca,
math::Math,
maxp::{Maxp, MaxpExtended},
name::Name,
os2::Os2,
post::Post,
sbix::Sbix,
stat::Stat,
variations::NO_VARIATION_INDEX,
vhea::{Vhea, VheaExtended},
vmtx::{Vmtx, VmtxExtended},
vorg::Vorg,
vvar::Vvar,
},
types::{GlyphId, NameId, Tag},
FontRef, TableProvider, TopLevelTable,
};
use serialize::{SerializeErrorFlags, Serializer};
use skrifa::MetadataProvider;
use thiserror::Error;
use write_fonts::FontBuilder;
pub type FastHashMap<K, V> = std::collections::HashMap<K, V, foldhash::fast::FixedState>;
pub(crate) type FastHashSet<T> = std::collections::HashSet<T, foldhash::fast::FixedState>;
const MAX_COMPOSITE_OPERATIONS_PER_GLYPH: u8 = 64;
const MAX_NESTING_LEVEL: usize = read_fonts::limits::MAX_RECURSION_DEPTH;
const MAX_GID: GlyphId = GlyphId::new(0xFFFFFFFF);
pub(crate) const INVALID_GID: GlyphId = GlyphId::new(u32::MAX);
pub static DEFAULT_LAYOUT_FEATURES: &[Tag] = &[
Tag::new(b"rvrn"),
Tag::new(b"ccmp"),
Tag::new(b"liga"),
Tag::new(b"locl"),
Tag::new(b"mark"),
Tag::new(b"mkmk"),
Tag::new(b"rlig"),
Tag::new(b"frac"),
Tag::new(b"numr"),
Tag::new(b"dnom"),
Tag::new(b"calt"),
Tag::new(b"clig"),
Tag::new(b"curs"),
Tag::new(b"kern"),
Tag::new(b"rclt"),
Tag::new(b"valt"),
Tag::new(b"vert"),
Tag::new(b"vkrn"),
Tag::new(b"vpal"),
Tag::new(b"vrt2"),
Tag::new(b"ltra"),
Tag::new(b"ltrm"),
Tag::new(b"rtla"),
Tag::new(b"rtlm"),
Tag::new(b"rand"),
Tag::new(b"jalt"),
Tag::new(b"chws"),
Tag::new(b"vchw"),
Tag::new(b"halt"),
Tag::new(b"vhal"),
Tag::new(b"palt"),
Tag::new(b"Harf"),
Tag::new(b"HARF"),
Tag::new(b"Buzz"),
Tag::new(b"BUZZ"),
Tag::new(b"init"),
Tag::new(b"medi"),
Tag::new(b"fina"),
Tag::new(b"isol"),
Tag::new(b"med2"),
Tag::new(b"fin2"),
Tag::new(b"fin3"),
Tag::new(b"cswh"),
Tag::new(b"mset"),
Tag::new(b"stch"),
Tag::new(b"ljmo"),
Tag::new(b"vjmo"),
Tag::new(b"tjmo"),
Tag::new(b"abvs"),
Tag::new(b"blws"),
Tag::new(b"abvm"),
Tag::new(b"blwm"),
Tag::new(b"nukt"),
Tag::new(b"akhn"),
Tag::new(b"rphf"),
Tag::new(b"rkrf"),
Tag::new(b"pref"),
Tag::new(b"blwf"),
Tag::new(b"half"),
Tag::new(b"abvf"),
Tag::new(b"pstf"),
Tag::new(b"cfar"),
Tag::new(b"vatu"),
Tag::new(b"cjct"),
Tag::new(b"init"),
Tag::new(b"pres"),
Tag::new(b"abvs"),
Tag::new(b"blws"),
Tag::new(b"psts"),
Tag::new(b"haln"),
Tag::new(b"dist"),
Tag::new(b"abvm"),
Tag::new(b"blwm"),
];
pub static DEFAULT_DROP_TABLES: &[Tag] = &[
Tag::new(b"morx"),
Tag::new(b"mort"),
Tag::new(b"kerx"),
Tag::new(b"kern"),
Tag::new(b"JSTF"),
Tag::new(b"DSIG"),
Tag::new(b"EBDT"),
Tag::new(b"EBLC"),
Tag::new(b"EBSC"),
Tag::new(b"SVG "),
Tag::new(b"PCLT"),
Tag::new(b"LTSH"),
Tag::new(b"Feat"),
Tag::new(b"Glat"),
Tag::new(b"Gloc"),
Tag::new(b"Silf"),
Tag::new(b"Sill"),
];
#[derive(Clone, Copy, Debug)]
pub struct SubsetFlags(u16);
impl SubsetFlags {
pub const SUBSET_FLAGS_DEFAULT: Self = Self(0x0000);
pub const SUBSET_FLAGS_NO_HINTING: Self = Self(0x0001);
pub const SUBSET_FLAGS_RETAIN_GIDS: Self = Self(0x0002);
pub const SUBSET_FLAGS_DESUBROUTINIZE: Self = Self(0x0004);
pub const SUBSET_FLAGS_NAME_LEGACY: Self = Self(0x0008);
pub const SUBSET_FLAGS_SET_OVERLAPS_FLAG: Self = Self(0x0010);
pub const SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED: Self = Self(0x0020);
pub const SUBSET_FLAGS_NOTDEF_OUTLINE: Self = Self(0x0040);
pub const SUBSET_FLAGS_GLYPH_NAMES: Self = Self(0x0080);
pub const SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES: Self = Self(0x0100);
pub const SUBSET_FLAGS_NO_LAYOUT_CLOSURE: Self = Self(0x0200);
pub const SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS: Self = Self(0x0400);
pub const SUBSET_FLAGS_NO_BIDI_CLOSURE: Self = Self(0x0800);
pub const SUBSET_FLAGS_IFTB_REQUIREMENTS: Self = Self(0x1000);
pub const SUBSET_FLAGS_RETAIN_NUM_GLYPHS: Self = Self(0x2000);
pub const KEEP_EVERYTHING: Self = Self(
Self::SUBSET_FLAGS_NOTDEF_OUTLINE.0
| Self::SUBSET_FLAGS_GLYPH_NAMES.0
| Self::SUBSET_FLAGS_NAME_LEGACY.0
| Self::SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES.0
| Self::SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED.0,
);
#[inline]
pub const fn contains(&self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
}
impl Default for SubsetFlags {
fn default() -> Self {
Self::SUBSET_FLAGS_DEFAULT
}
}
impl PartialEq for SubsetFlags {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl std::ops::BitOr for SubsetFlags {
type Output = Self;
#[inline]
fn bitor(self, other: SubsetFlags) -> Self {
Self(self.0 | other.0)
}
}
impl From<u16> for SubsetFlags {
fn from(value: u16) -> Self {
Self(value)
}
}
impl std::ops::BitOrAssign for SubsetFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.0 |= other.0;
}
}
#[allow(dead_code)]
#[derive(Default)]
pub struct Plan {
unicodes: IntSet<u32>,
glyphs_requested: IntSet<GlyphId>,
glyphset_cmaped: IntSet<GlyphId>,
glyphset_mathed: IntSet<GlyphId>,
glyphset_gsub: IntSet<GlyphId>,
glyphset_colred: IntSet<GlyphId>,
glyphset: IntSet<GlyphId>,
glyph_map: FastHashMap<GlyphId, GlyphId>,
glyph_map_gsub: Vec<GlyphId>,
reverse_glyph_map: FastHashMap<GlyphId, GlyphId>,
new_to_old_gid_list: Vec<(GlyphId, GlyphId)>,
num_output_glyphs: usize,
font_num_glyphs: usize,
unicode_to_new_gid_list: Vec<(u32, GlyphId)>,
subset_flags: SubsetFlags,
no_subset_tables: IntSet<Tag>,
drop_tables: IntSet<Tag>,
name_ids: IntSet<NameId>,
name_languages: IntSet<u16>,
name_table_overrides: FastHashMap<name::RecordKey, Vec<u8>>,
layout_scripts: IntSet<Tag>,
layout_features: IntSet<Tag>,
gsub_features: FastHashMap<u16, u16>,
gpos_features: FastHashMap<u16, u16>,
gsub_features_w_duplicates: FastHashMap<u16, u16>,
gpos_features_w_duplicates: FastHashMap<u16, u16>,
gsub_lookups: FastHashMap<u16, u16>,
gpos_lookups: FastHashMap<u16, u16>,
gsub_script_langsys: FastHashMap<u16, IntSet<u16>>,
gpos_script_langsys: FastHashMap<u16, IntSet<u16>>,
used_mark_sets_map: FastHashMap<u16, u16>,
colrv1_layers: FastHashMap<u32, u32>,
colr_palettes: FastHashMap<u16, u16>,
colr_varstore_inner_maps: Vec<IncBiMap>,
colr_varidx_delta_map: FastHashMap<u32, (u32, i32)>,
colr_new_deltaset_idx_varidx_map: FastHashMap<u32, u32>,
os2_info: Os2Info,
base_varidx_delta_map: FastHashMap<u32, (u32, i32)>,
base_varstore_inner_maps: Vec<IncBiMap>,
layout_varidx_delta_map: FastHashMap<u32, (u32, i32)>,
gdef_varstore_inner_maps: Vec<IncBiMap>,
}
#[derive(Default)]
struct Os2Info {
min_cmap_codepoint: u32,
max_cmap_codepoint: u32,
}
impl Plan {
pub fn keep_everything(font: &FontRef) -> Self {
Self::new(
&IntSet::all(),
&IntSet::all(),
font,
SubsetFlags::KEEP_EVERYTHING,
&IntSet::empty(),
&IntSet::all(),
&IntSet::all(),
&IntSet::all(),
&IntSet::all(),
)
}
pub fn old_to_new_glyph_mapping(&self) -> impl Iterator<Item = (GlyphId, GlyphId)> + '_ {
self.glyph_map.iter().map(|(&old, &new)| (old, new))
}
pub fn unicode_to_old_glyph_mapping(&self) -> impl Iterator<Item = (u32, GlyphId)> + '_ {
self.unicode_to_new_gid_list
.iter()
.filter_map(|&(unicode, new)| {
self.reverse_glyph_map.get(&new).map(|&old| (unicode, old))
})
}
pub fn override_name_table(
&mut self,
name_id: NameId,
platform_id: u16,
encoding_id: u16,
language_id: u16,
text: Option<&str>,
) -> Result<(), SubsetError> {
let text = text.unwrap_or_default();
let is_macintosh = platform_id == PlatformId::Macintosh as u16;
let capacity = if is_macintosh {
if !text.is_ascii() {
return Err(SubsetError::InvalidNameOverride(
"Macintosh overrides require ASCII text",
));
}
text.len()
} else if text.len() <= u16::MAX as usize / 2 {
text.len() * 2
} else {
text.encode_utf16().take(u16::MAX as usize / 2 + 1).count() * 2
};
if capacity > u16::MAX as usize {
return Err(SubsetError::InvalidNameOverride(
"encoded name exceeds 65535 bytes",
));
}
let bytes = if is_macintosh {
text.as_bytes().to_vec()
} else {
let mut bytes = Vec::with_capacity(capacity);
for unit in text.encode_utf16() {
bytes.extend_from_slice(&unit.to_be_bytes());
}
bytes.shrink_to_fit();
bytes
};
self.name_table_overrides
.insert((platform_id, encoding_id, language_id, name_id), bytes);
Ok(())
}
pub fn set_no_subset_tables(&mut self, tables: &IntSet<Tag>) {
self.no_subset_tables.clone_from(tables);
}
fn has_identity_glyph_map(&self) -> bool {
self.num_output_glyphs == self.font_num_glyphs
&& self.new_to_old_gid_list.len() == self.font_num_glyphs
&& self
.new_to_old_gid_list
.iter()
.enumerate()
.all(|(gid, &(new_gid, old_gid))| {
new_gid.to_u32() as usize == gid && old_gid == new_gid
})
}
#[allow(clippy::too_many_arguments)]
pub fn new(
input_gids: &IntSet<GlyphId>,
input_unicodes: &IntSet<u32>,
font: &FontRef,
flags: SubsetFlags,
drop_tables: &IntSet<Tag>,
layout_scripts: &IntSet<Tag>,
layout_features: &IntSet<Tag>,
name_ids: &IntSet<NameId>,
name_languages: &IntSet<u16>,
) -> Self {
Self::new_impl(
input_gids,
input_unicodes,
font,
flags,
drop_tables,
layout_scripts,
layout_features,
name_ids,
name_languages,
&FastHashMap::default(),
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_with_glyph_map(
input_gids: &IntSet<GlyphId>,
input_unicodes: &IntSet<u32>,
font: &FontRef,
flags: SubsetFlags,
drop_tables: &IntSet<Tag>,
layout_scripts: &IntSet<Tag>,
layout_features: &IntSet<Tag>,
name_ids: &IntSet<NameId>,
name_languages: &IntSet<u16>,
glyph_map: &FastHashMap<GlyphId, GlyphId>,
) -> Result<Self, SubsetError> {
if !glyph_map.is_empty() {
let new_gids: IntSet<GlyphId> = glyph_map.values().copied().collect();
if new_gids.len() != glyph_map.len() as u64 {
return Err(SubsetError::NonUniqueGlyphMap);
}
if flags.contains(SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS) {
return Err(SubsetError::GlyphMapWithRetainGids);
}
}
Ok(Self::new_impl(
input_gids,
input_unicodes,
font,
flags,
drop_tables,
layout_scripts,
layout_features,
name_ids,
name_languages,
glyph_map,
))
}
#[allow(clippy::too_many_arguments)]
fn new_impl(
input_gids: &IntSet<GlyphId>,
input_unicodes: &IntSet<u32>,
font: &FontRef,
flags: SubsetFlags,
drop_tables: &IntSet<Tag>,
layout_scripts: &IntSet<Tag>,
layout_features: &IntSet<Tag>,
name_ids: &IntSet<NameId>,
name_languages: &IntSet<u16>,
requested_glyph_map: &FastHashMap<GlyphId, GlyphId>,
) -> Self {
let mut this = Plan {
glyphs_requested: input_gids.clone(),
font_num_glyphs: get_font_num_glyphs(font),
subset_flags: flags,
drop_tables: drop_tables.clone(),
layout_scripts: layout_scripts.clone(),
layout_features: layout_features.clone(),
name_ids: name_ids.clone(),
name_languages: name_languages.clone(),
..Default::default()
};
let default_no_subset_tables = [gasp::Gasp::TAG, FPGM, PREP, VDMX, DSIG];
this.no_subset_tables
.extend(default_no_subset_tables.iter().copied());
this.populate_unicodes_to_retain(input_gids, input_unicodes, font);
this.populate_gids_to_retain(font);
this.create_old_gid_to_new_gid_map(requested_glyph_map);
if this.subset_flags.contains(
SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS | SubsetFlags::SUBSET_FLAGS_RETAIN_NUM_GLYPHS,
) {
this.num_output_glyphs = this.font_num_glyphs;
}
this.create_glyph_map_gsub();
let num = this.unicode_to_new_gid_list.len();
for i in 0..num {
let old_gid = this.unicode_to_new_gid_list[i].1;
let new_gid = this.glyph_map.get(&old_gid).unwrap();
this.unicode_to_new_gid_list[i].1 = *new_gid;
}
this.collect_base_var_indices(font);
this
}
fn populate_unicodes_to_retain(
&mut self,
input_gids: &IntSet<GlyphId>,
input_unicodes: &IntSet<u32>,
font: &FontRef,
) {
let unicodes = unicode_closure(
input_unicodes,
!self
.subset_flags
.contains(SubsetFlags::SUBSET_FLAGS_NO_BIDI_CLOSURE),
);
let charmap = font.charmap();
if input_gids.is_empty() && unicodes.len() < (self.font_num_glyphs as u64) {
let cap: usize = unicodes.len().try_into().unwrap_or(usize::MAX);
self.unicode_to_new_gid_list.reserve(cap);
for cp in unicodes.iter() {
match charmap.map(cp) {
Some(gid) => {
self.unicode_to_new_gid_list.push((cp, gid));
}
None => {
continue;
}
}
}
} else {
let vec_cap: u64 = input_gids.len() + unicodes.len();
let vec_cap: usize = vec_cap
.min(self.font_num_glyphs as u64)
.try_into()
.unwrap_or(usize::MAX);
self.unicode_to_new_gid_list.reserve(vec_cap);
for (cp, gid) in charmap.mappings() {
if !input_gids.contains(gid) && !unicodes.contains(cp) {
continue;
}
self.unicode_to_new_gid_list.push((cp, gid));
}
for range in input_gids.iter_ranges() {
if range.start().to_u32() as usize >= self.font_num_glyphs {
break;
}
let mut last = range.end().to_u32() as usize;
if last >= self.font_num_glyphs {
last = self.font_num_glyphs - 1;
}
self.glyphset_gsub
.insert_range(*range.start()..=GlyphId::from(last as u32));
}
}
self.unicode_to_new_gid_list.sort();
self.glyphset_gsub
.extend(self.unicode_to_new_gid_list.iter().map(|t| t.1));
self.unicodes
.extend(self.unicode_to_new_gid_list.iter().map(|t| t.0));
self.os2_info.min_cmap_codepoint = self.unicodes.first().unwrap_or(0xFFFF_u32);
self.os2_info.max_cmap_codepoint = self.unicodes.last().unwrap_or(0xFFFF_u32);
self.collect_variation_selectors(font, &unicodes);
}
fn collect_variation_selectors(&mut self, font: &FontRef, input_unicodes: &IntSet<u32>) {
for cmap in [
font.cmap().ok(),
font.dmap().ok().map(|dmap| dmap.as_cmap()),
]
.into_iter()
.flatten()
{
let Some((_, subtable)) = cmap.uvs_subtable() else {
continue;
};
self.unicodes.extend(
subtable
.var_selector()
.iter()
.map(|s| s.var_selector().to_u32())
.filter(|v| input_unicodes.contains(*v)),
);
}
}
fn populate_gids_to_retain(&mut self, font: &FontRef) {
self.glyphset_gsub.insert(GlyphId::NOTDEF);
if let Ok(cmap) = font.cmap() {
cmap.closure_glyphs(&self.unicodes, &mut self.glyphset_gsub);
}
if let Ok(dmap) = font.dmap() {
dmap.as_cmap()
.closure_glyphs(&self.unicodes, &mut self.glyphset_gsub);
}
self.glyphset_cmaped = self.glyphset_gsub.clone();
if !self.drop_tables.contains(Math::TAG) {
math::closure(font, &mut self.glyphset_gsub);
remove_invalid_gids(&mut self.glyphset_gsub, self.font_num_glyphs);
}
self.glyphset_mathed = self.glyphset_gsub.clone();
self.layout_populate_gids_to_retain(font);
remove_invalid_gids(&mut self.glyphset_gsub, self.font_num_glyphs);
if !self.drop_tables.contains(Tag::new(b"COLR")) {
self.colr_closure(font);
remove_invalid_gids(&mut self.glyphset_colred, self.font_num_glyphs);
} else {
self.glyphset_colred = self.glyphset_gsub.clone();
}
if let Ok((glyf, loca)) = font.selected_glyf_loca(None) {
let operation_count =
self.glyphset_gsub.len() * (MAX_COMPOSITE_OPERATIONS_PER_GLYPH as u64);
for gid in self.glyphset_colred.iter() {
glyf_closure_glyphs(
&loca,
&glyf,
gid,
&mut self.glyphset,
operation_count as i32,
0,
);
}
remove_invalid_gids(&mut self.glyphset, self.font_num_glyphs);
} else {
self.glyphset = self.glyphset_colred.clone();
if !self.drop_tables.contains(Cff::TAG)
&& self.glyphset.len() != self.font_num_glyphs as u64
{
cff::closure(font, &mut self.glyphset);
remove_invalid_gids(&mut self.glyphset, self.font_num_glyphs);
}
}
self.nameid_closure(font);
self.collect_layout_var_indices(font);
}
fn layout_populate_gids_to_retain(&mut self, font: &FontRef) {
if !self.drop_tables.contains(Tag::new(b"GSUB")) {
if let Ok(gsub) = font.gsub() {
gsub.closure_glyphs_lookups_features(self);
}
}
if !self.drop_tables.contains(Tag::new(b"GPOS")) {
if let Ok(gpos) = font.gpos() {
gpos.closure_glyphs_lookups_features(self);
}
}
}
fn create_old_gid_to_new_gid_map(
&mut self,
requested_glyph_map: &FastHashMap<GlyphId, GlyphId>,
) {
let pop = self.glyphset.len();
self.glyph_map.reserve(pop as usize);
self.reverse_glyph_map.reserve(pop as usize);
self.new_to_old_gid_list.reserve(pop as usize);
if !requested_glyph_map.is_empty() {
let mut max_glyph = 0;
for old_gid in self.glyphset.iter() {
if old_gid == GlyphId::NOTDEF {
self.new_to_old_gid_list
.push((GlyphId::NOTDEF, GlyphId::NOTDEF));
continue;
}
if let Some(new_gid) = requested_glyph_map.get(&old_gid) {
max_glyph = max_glyph.max(new_gid.to_u32());
self.new_to_old_gid_list.push((*new_gid, old_gid));
}
}
self.new_to_old_gid_list.sort_unstable();
for old_gid in self.glyphset.iter() {
if old_gid != GlyphId::NOTDEF && !requested_glyph_map.contains_key(&old_gid) {
max_glyph += 1;
self.new_to_old_gid_list
.push((GlyphId::new(max_glyph), old_gid));
}
}
self.num_output_glyphs = max_glyph as usize + 1;
} else if !self
.subset_flags
.contains(SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS)
{
self.new_to_old_gid_list.extend(
self.glyphset
.iter()
.zip(0u32..)
.map(|x| (GlyphId::from(x.1), x.0)),
);
self.num_output_glyphs = self.new_to_old_gid_list.len();
} else {
self.new_to_old_gid_list
.extend(self.glyphset.iter().map(|x| (x, x)));
let Some(max_glyph) = self.glyphset.last() else {
return;
};
self.num_output_glyphs = max_glyph.to_u32() as usize + 1;
}
self.glyph_map
.extend(self.new_to_old_gid_list.iter().map(|x| (x.1, x.0)));
self.reverse_glyph_map
.extend(self.new_to_old_gid_list.iter().map(|x| (x.0, x.1)));
}
fn create_glyph_map_gsub(&mut self) {
let Some(max_gid) = self.glyphset_gsub.last() else {
return;
};
self.glyph_map_gsub
.resize(max_gid.to_u32() as usize + 1, INVALID_GID);
for g in self.glyphset_gsub.iter() {
if let Some(new_gid) = self.glyph_map.get(&g) {
self.glyph_map_gsub[g.to_u32() as usize] = *new_gid;
}
}
}
fn colr_closure(&mut self, font: &FontRef) {
if let Ok(colr) = font.colr() {
colr.v0_closure_glyphs(&self.glyphset_gsub, &mut self.glyphset_colred);
let mut layer_indices = IntSet::empty();
let mut palette_indices = IntSet::empty();
let mut variation_indices = IntSet::empty();
colr.v1_closure(
&mut self.glyphset_colred,
&mut layer_indices,
&mut palette_indices,
&mut variation_indices,
);
colr.v0_closure_palette_indices(&self.glyphset_colred, &mut palette_indices);
let _ = std::mem::replace(&mut self.colrv1_layers, remap_indices(layer_indices));
let _ = std::mem::replace(
&mut self.colr_palettes,
remap_palette_indices(palette_indices),
);
if variation_indices.is_empty() {
return;
}
if let Some(Ok(var_store)) = colr.item_variation_store() {
let vardata_count = var_store.item_variation_data_count() as u32;
let Ok(var_index_map) = colr.var_index_map().transpose() else {
return;
};
let mut delta_set_indices = IntSet::empty();
let mut deltaset_idx_var_idx_map = FastHashMap::default();
if let Some(var_index_map) = &var_index_map {
delta_set_indices.extend(variation_indices.iter());
variation_indices.clear();
deltaset_idx_var_idx_map.reserve(delta_set_indices.len() as usize);
for idx in delta_set_indices.iter() {
if let Ok(var_idx) = var_index_map.get(idx) {
let var_idx = ((var_idx.outer as u32) << 16) + var_idx.inner as u32;
variation_indices.insert(var_idx);
deltaset_idx_var_idx_map.insert(idx, var_idx);
}
}
}
remap_variation_indices(
vardata_count,
&variation_indices,
&mut self.colr_varidx_delta_map,
);
generate_varstore_inner_maps(
&variation_indices,
vardata_count,
&mut self.colr_varstore_inner_maps,
);
if var_index_map.is_some() {
let (new_deltaset_idx_varidx_map, deltaset_idx_delta_map) =
remap_delta_set_indices(
&delta_set_indices,
&deltaset_idx_var_idx_map,
&self.colr_varidx_delta_map,
);
let _ = std::mem::replace(
&mut self.colr_new_deltaset_idx_varidx_map,
new_deltaset_idx_varidx_map,
);
let _ =
std::mem::replace(&mut self.colr_varidx_delta_map, deltaset_idx_delta_map);
}
}
} else {
self.glyphset_colred.union(&self.glyphset_gsub);
}
}
fn nameid_closure(&mut self, font: &FontRef) {
if !self.drop_tables.contains(Tag::new(b"STAT")) {
if let Ok(stat) = font.stat() {
stat.collect_name_ids(self);
}
};
if !self.drop_tables.contains(Tag::new(b"fvar")) {
if let Ok(fvar) = font.fvar() {
fvar.collect_name_ids(self);
}
}
if !self.drop_tables.contains(Tag::new(b"CPAL")) {
if let Ok(cpal) = font.cpal() {
cpal.collect_name_ids(self);
}
}
if !self.drop_tables.contains(Tag::new(b"GSUB")) {
if let Ok(gsub) = font.gsub() {
gsub.collect_name_ids(self);
}
}
if !self.drop_tables.contains(Tag::new(b"GPOS")) {
if let Ok(gpos) = font.gpos() {
gpos.collect_name_ids(self);
}
}
}
fn collect_layout_var_indices(&mut self, font: &FontRef) {
if self.drop_tables.contains(Tag::new(b"GDEF")) {
return;
}
let Ok(gdef) = font.gdef() else {
return;
};
let mut used_mark_sets = IntSet::empty();
gdef.collect_used_mark_sets(self, &mut used_mark_sets);
let _ = std::mem::replace(&mut self.used_mark_sets_map, remap_indices(used_mark_sets));
let Some(Ok(var_store)) = gdef.item_var_store() else {
return;
};
let mut varidx_set = IntSet::empty();
gdef.collect_variation_indices(self, &mut varidx_set);
if let Ok(gpos) = font.gpos() {
gpos.collect_variation_indices(self, &mut varidx_set);
if let Some(Ok(variations)) = gpos.feature_variations() {
layout::collect_feature_variation_condition_indices(
&variations,
self,
&self.gpos_features_w_duplicates,
&self.gpos_lookups,
&mut varidx_set,
);
}
}
if let Ok(gsub) = font.gsub() {
if let Some(Ok(variations)) = gsub.feature_variations() {
layout::collect_feature_variation_condition_indices(
&variations,
self,
&self.gsub_features_w_duplicates,
&self.gsub_lookups,
&mut varidx_set,
);
}
}
if !self.drop_tables.contains(Math::TAG) {
if let Ok(math) = font.math() {
math.collect_variation_indices(self, &mut varidx_set);
}
}
let vardata_count = var_store.item_variation_data_count() as u32;
remap_variation_indices(
vardata_count,
&varidx_set,
&mut self.layout_varidx_delta_map,
);
generate_varstore_inner_maps(
&varidx_set,
vardata_count,
&mut self.gdef_varstore_inner_maps,
);
}
fn collect_base_var_indices(&mut self, font: &FontRef) {
if self.drop_tables.contains(Tag::new(b"BASE")) {
return;
}
if font.fvar().is_err() {
return;
}
let Ok(base) = font.base() else {
return;
};
let Some(Ok(var_store)) = base.item_var_store() else {
return;
};
let mut varidx_set = IntSet::empty();
{
base.collect_variation_indices(self, &mut varidx_set);
}
let vardata_count = var_store.item_variation_data_count() as u32;
remap_variation_indices(vardata_count, &varidx_set, &mut self.base_varidx_delta_map);
generate_varstore_inner_maps(
&varidx_set,
vardata_count,
&mut self.base_varstore_inner_maps,
);
}
}
fn remap_variation_indices(
vardata_count: u32,
varidx_set: &IntSet<u32>,
varidx_delta_map: &mut FastHashMap<u32, (u32, i32)>,
) {
if vardata_count == 0 || varidx_set.is_empty() {
return;
}
let mut new_major: u32 = 0;
let mut new_minor: u32 = 0;
let mut last_major = varidx_set.first().unwrap() >> 16;
for var_idx in varidx_set.iter() {
let major = var_idx >> 16;
if major >= vardata_count {
break;
}
if major != last_major {
new_minor = 0;
new_major += 1;
}
let new_idx = (new_major << 16) + new_minor;
varidx_delta_map.insert(var_idx, (new_idx, 0));
new_minor += 1;
last_major = major;
}
}
fn generate_varstore_inner_maps(
varidx_set: &IntSet<u32>,
vardata_count: u32,
inner_maps: &mut Vec<IncBiMap>,
) {
if varidx_set.is_empty() || vardata_count == 0 {
return;
}
inner_maps.resize_with(vardata_count as usize, Default::default);
for idx in varidx_set.iter() {
let major = idx >> 16;
let minor = idx & 0xFFFF;
if major >= vardata_count {
break;
}
inner_maps[major as usize].add(minor);
}
}
fn remap_delta_set_indices(
delta_set_indices: &IntSet<u32>,
deltaset_idx_var_idx_map: &FastHashMap<u32, u32>,
varidx_delta_map: &FastHashMap<u32, (u32, i32)>,
) -> (FastHashMap<u32, u32>, FastHashMap<u32, (u32, i32)>) {
let capacity = delta_set_indices.len() as usize;
let mut new_deltaset_idx_varidx_map =
FastHashMap::with_capacity_and_hasher(capacity, foldhash::fast::FixedState::default());
let mut deltaset_idx_delta_map =
FastHashMap::with_capacity_and_hasher(capacity, foldhash::fast::FixedState::default());
let mut new_idx = 0_u32;
for deltaset_idx in delta_set_indices.iter() {
let Some(&var_idx) = deltaset_idx_var_idx_map.get(&deltaset_idx) else {
continue;
};
let (new_var_idx, delta) = if var_idx == NO_VARIATION_INDEX {
(var_idx, 0)
} else if let Some((new_idx, d)) = varidx_delta_map.get(&var_idx) {
(*new_idx, *d)
} else {
continue;
};
new_deltaset_idx_varidx_map.insert(new_idx, new_var_idx);
deltaset_idx_delta_map.insert(deltaset_idx, (new_idx, delta));
new_idx += 1;
}
(new_deltaset_idx_varidx_map, deltaset_idx_delta_map)
}
fn glyf_closure_glyphs(
loca: &Loca,
glyf: &Glyf,
gid: GlyphId,
gids_to_retain: &mut IntSet<GlyphId>,
operation_count: i32,
depth: usize,
) -> i32 {
if gids_to_retain.contains(gid) {
return operation_count;
}
gids_to_retain.insert(gid);
if depth > MAX_NESTING_LEVEL {
return operation_count;
}
let depth = depth + 1;
let mut operation_count = operation_count - 1;
if operation_count < 0 {
return operation_count;
}
if let Some(Glyph::Composite(glyph)) = loca.get(gid, glyf).and_then(|g| g.into_glyph()) {
for child in glyph.components() {
operation_count = glyf_closure_glyphs(
loca,
glyf,
child.glyph,
gids_to_retain,
operation_count,
depth,
);
}
}
operation_count
}
fn remove_invalid_gids(gids: &mut IntSet<GlyphId>, num_glyphs: usize) {
gids.remove_range(GlyphId::new(num_glyphs as u32)..=MAX_GID);
}
fn get_font_num_glyphs(font: &FontRef) -> usize {
let ret = font
.selected_glyf_loca(None)
.map(|(_, loca)| loca.len())
.unwrap_or_default();
let maxp = font
.selected_maxp()
.expect("Error reading maximum profile table");
ret.max(maxp.num_glyphs() as usize)
}
pub(crate) fn remap_indices<T: Domain + std::cmp::Eq + std::hash::Hash + TryFrom<usize>>(
indices: IntSet<T>,
) -> FastHashMap<T, T> {
let mut map = FastHashMap::with_capacity_and_hasher(
indices.len() as usize,
foldhash::fast::FixedState::default(),
);
map.extend(
indices
.iter()
.enumerate()
.filter_map(|(new_idx, old_idx)| Some((old_idx, T::try_from(new_idx).ok()?))),
);
map
}
fn remap_palette_indices(indices: IntSet<u16>) -> FastHashMap<u16, u16> {
indices
.iter()
.enumerate()
.map(|x| {
if x.1 == 0xFFFF {
(0xFFFF, 0xFFFF)
} else {
(x.1, x.0 as u16)
}
})
.collect()
}
#[derive(Default)]
pub struct SubsetState {
has_gdef_varstore: bool,
}
#[derive(Debug, Error)]
pub enum SubsetError {
#[error("Invalid name override: {0}")]
InvalidNameOverride(&'static str),
#[error("Invalid input gid {0}")]
InvalidGid(String),
#[error("Invalid gid range {start}-{end}")]
InvalidGidRange { start: u32, end: u32 },
#[error("Invalid input unicode {0}")]
InvalidUnicode(String),
#[error("Invalid unicode range {start}-{end}")]
InvalidUnicodeRange { start: u32, end: u32 },
#[error("Invalid tag {0}")]
InvalidTag(String),
#[error("Invalid ID {0}")]
InvalidId(String),
#[error("Invalid glyph map entry {0}, expected old_gid:new_gid")]
InvalidGlyphMapEntry(String),
#[error("Subsetting table '{0}' failed")]
SubsetTableError(Tag),
#[error("The provided custom glyph mapping is not unique")]
NonUniqueGlyphMap,
#[error("SUBSET_FLAGS_RETAIN_GIDS cannot be set if a custom glyph mapping has been provided")]
GlyphMapWithRetainGids,
}
pub trait NameIdClosure {
fn collect_name_ids(&self, plan: &mut Plan);
}
pub(crate) trait CollectVariationIndices {
fn collect_variation_indices(&self, plan: &Plan, varidx_set: &mut IntSet<u32>);
}
pub(crate) trait LayoutClosure {
fn prune_features(
&self,
lookup_indices: &IntSet<u16>,
feature_indices: IntSet<u16>,
) -> IntSet<u16>;
fn find_duplicate_features(
&self,
lookup_indices: &IntSet<u16>,
feature_indices: IntSet<u16>,
) -> FastHashMap<u16, u16>;
fn prune_langsys(
&self,
duplicate_feature_index_map: &FastHashMap<u16, u16>,
layout_scripts: &IntSet<Tag>,
) -> (FastHashMap<u16, IntSet<u16>>, IntSet<u16>);
fn closure_glyphs_lookups_features(&self, plan: &mut Plan);
}
pub const CVT: Tag = Tag::new(b"cvt ");
pub const DSIG: Tag = Tag::new(b"DSIG");
pub const EBSC: Tag = Tag::new(b"EBSC");
pub const FPGM: Tag = Tag::new(b"fpgm");
pub const GLAT: Tag = Tag::new(b"Glat");
pub const GLOC: Tag = Tag::new(b"Gloc");
pub const JSTF: Tag = Tag::new(b"JSTF");
pub const LTSH: Tag = Tag::new(b"LTSH");
pub const MORX: Tag = Tag::new(b"morx");
pub const MORT: Tag = Tag::new(b"mort");
pub const KERX: Tag = Tag::new(b"kerx");
pub const KERN: Tag = Tag::new(b"kern");
pub const PCLT: Tag = Tag::new(b"PCLT");
pub const PREP: Tag = Tag::new(b"prep");
pub const SILF: Tag = Tag::new(b"Silf");
pub const SILL: Tag = Tag::new(b"Sill");
pub const VDMX: Tag = Tag::new(b"VDMX");
pub const MVAR: Tag = Tag::new(b"MVAR");
pub trait Subset {
fn subset(
&self,
_plan: &Plan,
_font: &FontRef,
_s: &mut Serializer,
_builder: &mut FontBuilder,
) -> Result<(), SubsetError> {
Ok(())
}
fn subset_with_state(
&self,
_plan: &Plan,
_font: &FontRef,
_state: &mut SubsetState,
_s: &mut Serializer,
_builder: &mut FontBuilder,
) -> Result<(), SubsetError> {
Ok(())
}
}
pub(crate) trait SubsetTable<'a> {
type ArgsForSubset: 'a;
type Output: 'a;
fn subset(
&self,
plan: &Plan,
s: &mut Serializer,
args: Self::ArgsForSubset,
) -> Result<Self::Output, SerializeErrorFlags>;
}
trait Serialize<'a> {
type Args: 'a;
fn serialize(s: &mut Serializer, args: Self::Args) -> Result<(), SerializeErrorFlags>;
}
pub fn subset_font<'a>(font: &FontRef<'a>, plan: &Plan) -> Result<Vec<u8>, SubsetError> {
let mut builder = FontBuilder::<'a>::default();
let mut state = SubsetState::default();
let mut tags_with_dependencies = Vec::with_capacity(5);
for record in font.table_directory().table_records() {
let tag = record.tag();
if should_drop_table(tag, plan) {
continue;
}
if tag == Cbdt::TAG {
continue;
}
if tag == Cblc::TAG {
if let (Ok(cblc), Ok(cbdt)) = (font.cblc(), font.cbdt()) {
if cblc::can_passthrough_bitmap_tables(&cblc, &cbdt, plan) {
builder.add_raw_with_checksum(
Cblc::TAG,
cblc.offset_data().as_bytes(),
record.checksum(),
);
if let Some(cbdt_record) = font
.table_directory()
.table_records()
.iter()
.find(|record| record.tag() == Cbdt::TAG)
{
builder.add_raw_with_checksum(
Cbdt::TAG,
cbdt.offset_data().as_bytes(),
cbdt_record.checksum(),
);
} else {
builder.add_raw(Cbdt::TAG, cbdt.offset_data().as_bytes());
}
continue;
}
}
}
match tag {
Gpos::TAG => tags_with_dependencies.push(tag),
_ => subset(tag, font, plan, &mut builder, &mut state)?,
}
}
for tag in tags_with_dependencies {
subset(tag, font, plan, &mut builder, &mut state)?;
}
Ok(builder.build())
}
fn should_drop_table(tag: Tag, plan: &Plan) -> bool {
if plan.drop_tables.contains(tag) {
return true;
}
let no_hinting = plan
.subset_flags
.contains(SubsetFlags::SUBSET_FLAGS_NO_HINTING);
match tag {
Cvar::TAG | CVT | FPGM | PREP | Hdmx::TAG | VDMX => no_hinting,
_ => false,
}
}
fn subset<'a>(
table_tag: Tag,
font: &FontRef<'a>,
plan: &Plan,
builder: &mut FontBuilder<'a>,
state: &mut SubsetState,
) -> Result<(), SubsetError> {
if (table_tag == Cff::TAG || table_tag == Cff2::TAG)
&& !plan.no_subset_tables.contains(table_tag)
{
let data = cff::subset_data(font, plan, table_tag == Cff2::TAG)?;
if !data.is_empty() {
builder.add_raw(table_tag, data);
}
return Ok(());
}
let max_buf_size = max_subset_table_size(font, table_tag, plan);
let buf_size = estimate_subset_table_size(font, table_tag, plan).min(max_buf_size);
let mut s = Serializer::new(buf_size).map_err(|_| SubsetError::SubsetTableError(table_tag))?;
let needed = try_subset(table_tag, font, plan, builder, &mut s, max_buf_size, state);
if s.in_error() && !s.only_offset_overflow() {
return Err(SubsetError::SubsetTableError(table_tag));
}
if needed.is_err() {
return Ok(());
}
let subsetted_data = if !s.offset_overflow() {
s.copy_bytes()
} else {
resolve_overflows(s, table_tag, 32).map_err(|_| SubsetError::SubsetTableError(table_tag))?
};
if !subsetted_data.is_empty() {
builder.add_raw(table_tag, subsetted_data);
}
Ok(())
}
fn try_subset<'a>(
table_tag: Tag,
font: &FontRef<'a>,
plan: &Plan,
builder: &mut FontBuilder<'a>,
s: &mut Serializer,
max_buf_size: usize,
state: &mut SubsetState,
) -> Result<(), SubsetError> {
s.start_serialize()
.map_err(|_| SubsetError::SubsetTableError(table_tag))?;
let ret = subset_table(table_tag, font, plan, builder, s, state);
if !s.ran_out_of_room() {
s.end_serialize();
return ret;
}
let Some(buf_size) = s
.allocated()
.checked_mul(2)
.and_then(|size| size.checked_add(16))
else {
return ret;
};
if buf_size > max_buf_size {
return ret;
}
s.reset_size(buf_size)
.map_err(|_| SubsetError::SubsetTableError(table_tag))?;
try_subset(table_tag, font, plan, builder, s, max_buf_size, state)
}
fn subset_table<'a>(
tag: Tag,
font: &FontRef<'a>,
plan: &Plan,
builder: &mut FontBuilder<'a>,
s: &mut Serializer,
state: &mut SubsetState,
) -> Result<(), SubsetError> {
if plan.no_subset_tables.contains(tag) {
return passthrough_table(tag, font, s);
}
match tag {
Math::TAG => font
.math()
.map_err(|_| SubsetError::SubsetTableError(Math::TAG))?
.subset(plan, font, s, builder),
Base::TAG => font
.base()
.map_err(|_| SubsetError::SubsetTableError(Base::TAG))?
.subset(plan, font, s, builder),
Cbdt::TAG => Ok(()),
Cblc::TAG => font
.cblc()
.map_err(|_| SubsetError::SubsetTableError(Cblc::TAG))?
.subset(plan, font, s, builder),
Cmap::TAG => font
.cmap()
.map_err(|_| SubsetError::SubsetTableError(Cmap::TAG))?
.subset(plan, font, s, builder),
Dmap::TAG => font
.dmap()
.map_err(|_| {
s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
SubsetError::SubsetTableError(Dmap::TAG)
})?
.subset(plan, font, s, builder),
Colr::TAG => font
.colr()
.map_err(|_| SubsetError::SubsetTableError(Colr::TAG))?
.subset(plan, font, s, builder),
Cpal::TAG => font
.cpal()
.map_err(|_| SubsetError::SubsetTableError(Cpal::TAG))?
.subset(plan, font, s, builder),
Gdef::TAG => font
.gdef()
.map_err(|_| SubsetError::SubsetTableError(Gdef::TAG))?
.subset_with_state(plan, font, state, s, builder),
Glyf::TAG
if font.data_for_tag(glyf_loca::GLYF).is_some()
&& !plan.drop_tables.contains(glyf_loca::GLYF) =>
{
Ok(())
}
Glyf::TAG => font
.glyf()
.map_err(|_| SubsetError::SubsetTableError(Glyf::TAG))?
.subset(plan, font, s, builder),
glyf_loca::GLYF => {
let glyf = font.glyf_extended().map_err(|_| {
s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
SubsetError::SubsetTableError(glyf_loca::GLYF)
})?;
glyf_loca::subset_glyf(&glyf, plan, font, s, builder, true)
}
Gpos::TAG => font
.gpos()
.map_err(|_| SubsetError::SubsetTableError(Gpos::TAG))?
.subset_with_state(plan, font, state, s, builder),
Gsub::TAG => font
.gsub()
.map_err(|_| SubsetError::SubsetTableError(Gsub::TAG))?
.subset_with_state(plan, font, state, s, builder),
Gvar::TAG
if font.data_for_tag(GvarExtended::TAG).is_some()
&& !plan.drop_tables.contains(GvarExtended::TAG) =>
{
Ok(())
}
Gvar::TAG => font
.gvar()
.map_err(|_| SubsetError::SubsetTableError(Gvar::TAG))?
.subset(plan, font, s, builder),
GvarExtended::TAG => font
.gvar_extended()
.map_err(|_| {
s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
SubsetError::SubsetTableError(GvarExtended::TAG)
})?
.subset(plan, font, s, builder),
Hdmx::TAG => font
.hdmx()
.map_err(|_| SubsetError::SubsetTableError(Hdmx::TAG))?
.subset(plan, font, s, builder),
Head::TAG => font.selected_glyf_loca(None).map(|_| ()).or_else(|_| {
font.head()
.map_err(|_| SubsetError::SubsetTableError(Head::TAG))?
.subset(plan, font, s, builder)
}),
Hhea::TAG | HheaExtended::TAG => Ok(()),
Hmtx::TAG
if font.data_for_tag(HmtxExtended::TAG).is_some()
&& !plan.drop_tables.contains(HmtxExtended::TAG) =>
{
Ok(())
}
Hmtx::TAG => font
.hmtx()
.map_err(|_| SubsetError::SubsetTableError(Hmtx::TAG))?
.subset(plan, font, s, builder),
HmtxExtended::TAG => font
.hmtx_extended()
.map_err(|_| {
s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
SubsetError::SubsetTableError(HmtxExtended::TAG)
})?
.subset(plan, font, s, builder),
Vhea::TAG | VheaExtended::TAG => Ok(()),
Vmtx::TAG
if font.data_for_tag(VmtxExtended::TAG).is_some()
&& !plan.drop_tables.contains(VmtxExtended::TAG) =>
{
Ok(())
}
Vmtx::TAG => font
.vmtx()
.map_err(|_| SubsetError::SubsetTableError(Vmtx::TAG))?
.subset(plan, font, s, builder),
VmtxExtended::TAG => font
.vmtx_extended()
.map_err(|_| {
s.set_err(SerializeErrorFlags::SERIALIZE_ERROR_READ_ERROR);
SubsetError::SubsetTableError(VmtxExtended::TAG)
})?
.subset(plan, font, s, builder),
Hvar::TAG => font
.hvar()
.map_err(|_| SubsetError::SubsetTableError(Hvar::TAG))?
.subset(plan, font, s, builder),
Vvar::TAG => font
.vvar()
.map_err(|_| SubsetError::SubsetTableError(Vvar::TAG))?
.subset(plan, font, s, builder),
Fvar::TAG | Cvar::TAG | Avar::TAG => passthrough_table(tag, font, s),
MVAR | CVT => passthrough_table(tag, font, s),
Loca::TAG | glyf_loca::LOCA => Ok(()),
Maxp::TAG
if font.data_for_tag(MaxpExtended::TAG).is_some()
&& !plan.drop_tables.contains(MaxpExtended::TAG) =>
{
Ok(())
}
Maxp::TAG => font
.maxp()
.map_err(|_| SubsetError::SubsetTableError(Maxp::TAG))?
.subset(plan, font, s, builder),
MaxpExtended::TAG => font
.maxp_extended()
.map_err(|_| SubsetError::SubsetTableError(MaxpExtended::TAG))?
.subset(plan, font, s, builder),
Name::TAG => font
.name()
.map_err(|_| SubsetError::SubsetTableError(Name::TAG))?
.subset(plan, font, s, builder),
Os2::TAG => font
.os2()
.map_err(|_| SubsetError::SubsetTableError(Os2::TAG))?
.subset(plan, font, s, builder),
Post::TAG => font
.post()
.map_err(|_| SubsetError::SubsetTableError(Post::TAG))?
.subset(plan, font, s, builder),
Sbix::TAG => font
.sbix()
.map_err(|_| SubsetError::SubsetTableError(Sbix::TAG))?
.subset(plan, font, s, builder),
Vorg::TAG => font
.vorg()
.map_err(|_| SubsetError::SubsetTableError(Vorg::TAG))?
.subset(plan, font, s, builder),
Stat::TAG => passthrough_table(tag, font, s),
_ if plan
.subset_flags
.contains(SubsetFlags::SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED) =>
{
passthrough_table(tag, font, s)
}
_ => Ok(()),
}
}
fn passthrough_table(tag: Tag, font: &FontRef<'_>, s: &mut Serializer) -> Result<(), SubsetError> {
if let Some(data) = font.data_for_tag(tag) {
s.embed_bytes(data.as_bytes())
.map_err(|_| SubsetError::SubsetTableError(tag))?;
}
Ok(())
}
pub fn estimate_subset_table_size(font: &FontRef, table_tag: Tag, plan: &Plan) -> usize {
let Some(table_data) = font.data_for_tag(table_tag) else {
return 0;
};
let table_len = table_data.len();
let mut bulk: usize = 8192;
if table_tag == Name::TAG {
bulk += name_overrides_len(plan);
}
let src_glyphs = plan.font_num_glyphs;
let dst_glyphs = plan.num_output_glyphs;
let same_size: bool =
table_tag == Gsub::TAG || table_tag == Gpos::TAG || table_tag == Name::TAG;
if plan
.subset_flags
.contains(SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS)
{
if table_tag == Cff::TAG {
bulk += src_glyphs * 16;
} else if table_tag == Cff2::TAG {
bulk += src_glyphs * 4;
}
}
if src_glyphs == 0 || same_size {
return bulk + table_len;
}
if table_tag == Glyf::TAG {
if let Some(len) = retained_glyf_len(font, plan, table_len) {
return bulk + len;
}
}
bulk + (table_len as f32 * (dst_glyphs as f32 / src_glyphs as f32).sqrt()) as usize
}
fn max_subset_table_size(font: &FontRef, table_tag: Tag, plan: &Plan) -> usize {
let max = font.data().len().checked_mul(2);
let max = if table_tag == Name::TAG {
max.and_then(|max| max.checked_add(name_overrides_len(plan)))
} else {
max
};
max.unwrap_or(usize::MAX)
}
fn name_overrides_len(plan: &Plan) -> usize {
plan.name_table_overrides
.values()
.map(|bytes| bytes.len() + name::NAME_RECORD_SIZE)
.sum()
}
fn retained_glyf_len(font: &FontRef, plan: &Plan, glyf_len: usize) -> Option<usize> {
let loca = font.loca(None).ok()?;
let total: usize = plan
.new_to_old_gid_list
.iter()
.map(|(_, old_gid)| {
let gid = old_gid.to_u32() as usize;
match (loca.get_raw(gid), loca.get_raw(gid + 1)) {
(Some(start), Some(end)) if start <= end && end as usize <= glyf_len => {
let len = (end - start) as usize;
len + len % 2
}
_ => 0,
}
})
.fold(0usize, usize::saturating_add);
Some(total.min(glyf_len.saturating_add(plan.new_to_old_gid_list.len())))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn max_subset_table_size_is_twice_the_font() {
let bytes = std::fs::read("test-data/fonts/Roboto-Regular.ttf").unwrap();
let font = FontRef::new(&bytes).unwrap();
let mut plan = Plan::new(
&IntSet::empty(),
&parse_unicodes("*").unwrap(),
&font,
SubsetFlags::default(),
&IntSet::empty(),
&IntSet::all(),
&DEFAULT_LAYOUT_FEATURES.iter().copied().collect(),
&IntSet::empty(),
&IntSet::empty(),
);
let twice_font = bytes.len() * 2;
assert_eq!(max_subset_table_size(&font, Glyf::TAG, &plan), twice_font);
assert_eq!(max_subset_table_size(&font, Name::TAG, &plan), twice_font);
plan.override_name_table(NameId::new(1), 3, 1, 0x409, Some(&"x".repeat(1000)))
.unwrap();
let overrides = name_overrides_len(&plan);
assert!(overrides > 1000);
assert_eq!(
max_subset_table_size(&font, Name::TAG, &plan),
twice_font + overrides
);
assert_eq!(max_subset_table_size(&font, Glyf::TAG, &plan), twice_font);
}
#[test]
fn glyf_size_estimate_covers_output() {
for (path, unicodes) in [
("test-data/fonts/Roboto-Regular.ttf", "*"),
("test-data/fonts/Roboto-Regular.ttf", "41-5A,61-7A"),
("test-data/fonts/Amiri-Regular.ttf", "*"),
("test-data/fonts/Amiri-Regular.ttf", "600-6FF"),
] {
let bytes = std::fs::read(path).unwrap();
let font = FontRef::new(&bytes).unwrap();
let plan = Plan::new(
&IntSet::empty(),
&parse_unicodes(unicodes).unwrap(),
&font,
SubsetFlags::default(),
&IntSet::empty(),
&IntSet::all(),
&DEFAULT_LAYOUT_FEATURES.iter().copied().collect(),
&IntSet::empty(),
&IntSet::empty(),
);
let estimate = estimate_subset_table_size(&font, Glyf::TAG, &plan);
let out = subset_font(&font, &plan).unwrap();
let glyf_len = FontRef::new(&out)
.unwrap()
.glyf()
.unwrap()
.offset_data()
.len();
assert!(
estimate - 8192 >= glyf_len,
"{path} {unicodes}: estimate {estimate}, glyf {glyf_len}"
);
}
}
fn font_with_hostile_loca(num_glyphs: u16, odd: u32) -> Vec<u8> {
let mut head = vec![0u8; 54];
head[50..52].copy_from_slice(&1i16.to_be_bytes());
let mut maxp = 0x0000_5000u32.to_be_bytes().to_vec();
maxp.extend(num_glyphs.to_be_bytes());
let loca: Vec<u8> = (0..=num_glyphs as u32)
.flat_map(|i| (if i % 2 == 1 { odd } else { 0 }).to_be_bytes())
.collect();
FontBuilder::new()
.add_raw(Head::TAG, head)
.add_raw(Maxp::TAG, maxp)
.add_raw(Loca::TAG, loca)
.add_raw(Glyf::TAG, vec![0u8; 4])
.build()
}
#[test]
fn glyf_size_estimate_ignores_hostile_loca() {
const NUM_GLYPHS: u16 = 60;
const GLYF_LEN: usize = 4;
for odd in [0xFFFF_FFFE, GLYF_LEN as u32] {
let bytes = font_with_hostile_loca(NUM_GLYPHS, odd);
let font = FontRef::new(&bytes).unwrap();
let gids: IntSet<GlyphId> = (0..NUM_GLYPHS as u32).map(GlyphId::new).collect();
let plan = Plan::new(
&gids,
&IntSet::empty(),
&font,
SubsetFlags::default(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
);
let estimate = estimate_subset_table_size(&font, Glyf::TAG, &plan);
let bound = 8192 + GLYF_LEN + NUM_GLYPHS as usize;
assert!(
estimate <= bound,
"loca {odd:#x}: estimate {estimate} exceeds {bound}"
);
assert!(
subset_font(&font, &plan).is_err(),
"loca {odd:#x}: malformed glyphs were accepted"
);
}
}
#[test]
fn requested_glyph_map() {
let bytes = std::fs::read("test-data/fonts/Roboto-Regular.ttf").unwrap();
let font = FontRef::new(&bytes).unwrap();
let new_plan = |gids: &[u32], flags, glyph_map: &[(u32, u32)]| {
let glyph_map: FastHashMap<_, _> = glyph_map
.iter()
.map(|(old, new)| (GlyphId::new(*old), GlyphId::new(*new)))
.collect();
Plan::new_with_glyph_map(
&gids.iter().copied().map(GlyphId::new).collect(),
&IntSet::empty(),
&font,
flags,
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&glyph_map,
)
};
let plan = new_plan(
&[37, 39, 69, 74],
SubsetFlags::default(),
&[(0, 5), (69, 2), (37, 7)],
)
.unwrap();
let pairs: Vec<_> = plan
.new_to_old_gid_list
.iter()
.map(|(new, old)| (new.to_u32(), old.to_u32()))
.collect();
assert_eq!(pairs, [(0, 0), (2, 69), (7, 37), (8, 39), (9, 74)]);
assert_eq!(plan.num_output_glyphs, 10);
assert_eq!(
plan.glyph_map.get(&GlyphId::new(37)),
Some(&GlyphId::new(7))
);
assert_eq!(
plan.reverse_glyph_map.get(&GlyphId::new(8)),
Some(&GlyphId::new(39))
);
assert!(matches!(
new_plan(&[37, 39], SubsetFlags::default(), &[(37, 1), (39, 1)]),
Err(SubsetError::NonUniqueGlyphMap)
));
assert!(matches!(
new_plan(&[37], SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS, &[(37, 1)]),
Err(SubsetError::GlyphMapWithRetainGids)
));
assert!(new_plan(&[37], SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS, &[]).is_ok());
}
#[test]
fn configurable_passthrough_copies_an_unrecognized_table() {
let original = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
let mut builder = FontBuilder::new();
for record in original.table_directory().table_records() {
builder.add_raw(record.tag(), original.data_for_tag(record.tag()).unwrap());
}
let tag = Tag::new(b"TEST");
builder.add_raw(tag, b"opaque table".to_vec());
let bytes = builder.build();
let font = FontRef::new(&bytes).unwrap();
let mut plan = Plan::new(
&IntSet::empty(),
&IntSet::empty(),
&font,
SubsetFlags::default(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
);
let before = subset_font(&font, &plan).unwrap();
assert!(FontRef::new(&before).unwrap().data_for_tag(tag).is_none());
plan.set_no_subset_tables(&[tag].into_iter().collect());
let after = subset_font(&font, &plan).unwrap();
assert_eq!(
FontRef::new(&after)
.unwrap()
.data_for_tag(tag)
.unwrap()
.as_bytes(),
b"opaque table"
);
}
#[test]
fn public_plan_mappings_include_closure_and_original_unicode_glyphs() {
let font = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
let unicodes = [0x2C, 0x31].into_iter().collect();
let glyphs = IntSet::empty();
for flags in [
SubsetFlags::default(),
SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS,
] {
let mut plan = Plan::new(
&glyphs,
&unicodes,
&font,
flags,
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
&IntSet::empty(),
);
let mapping: std::collections::BTreeMap<_, _> =
plan.old_to_new_glyph_mapping().collect();
assert!(mapping.contains_key(&GlyphId::new(0)));
assert!(mapping.contains_key(&GlyphId::new(2)));
assert!(mapping.contains_key(&GlyphId::new(4)));
let unicode_mapping: std::collections::BTreeMap<_, _> =
plan.unicode_to_old_glyph_mapping().collect();
assert_eq!(unicode_mapping[&0x2C], GlyphId::new(2));
assert_eq!(unicode_mapping[&0x31], GlyphId::new(4));
if flags.contains(SubsetFlags::SUBSET_FLAGS_RETAIN_GIDS) {
assert!(mapping.iter().all(|(old, new)| old == new));
}
let no_subset = [Tag::new(b"name")].into_iter().collect();
plan.set_no_subset_tables(&no_subset);
assert_eq!(plan.no_subset_tables, no_subset);
}
}
#[test]
fn populate_unicodes_wo_input_gid() {
let mut plan = Plan::default();
let font = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
plan.font_num_glyphs = get_font_num_glyphs(&font);
let input_gids = IntSet::empty();
let mut input_unicodes = IntSet::empty();
input_unicodes.insert(0x2c_u32);
input_unicodes.insert(0x31_u32);
plan.populate_unicodes_to_retain(&input_gids, &input_unicodes, &font);
assert_eq!(plan.unicodes.len(), 2);
assert!(plan.unicodes.contains(0x2c_u32));
assert!(plan.unicodes.contains(0x31_u32));
assert_eq!(plan.glyphset_gsub.len(), 2);
assert!(plan.glyphset_gsub.contains(GlyphId::new(2)));
assert!(plan.glyphset_gsub.contains(GlyphId::new(4)));
assert_eq!(plan.unicode_to_new_gid_list.len(), 2);
assert_eq!(plan.unicode_to_new_gid_list[0], (0x2c_u32, GlyphId::new(2)));
assert_eq!(plan.unicode_to_new_gid_list[1], (0x31_u32, GlyphId::new(4)));
}
#[test]
fn populate_unicodes_w_input_gid() {
let mut plan = Plan::default();
let font = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
plan.font_num_glyphs = get_font_num_glyphs(&font);
let mut input_gids = IntSet::empty();
let input_unicodes = IntSet::empty();
input_gids.insert(GlyphId::new(2));
input_gids.insert(GlyphId::new(4));
plan.populate_unicodes_to_retain(&input_gids, &input_unicodes, &font);
assert_eq!(plan.unicodes.len(), 2);
assert!(plan.unicodes.contains(0x2c_u32));
assert!(plan.unicodes.contains(0x31_u32));
assert_eq!(plan.glyphset_gsub.len(), 2);
assert!(plan.glyphset_gsub.contains(GlyphId::new(2)));
assert!(plan.glyphset_gsub.contains(GlyphId::new(4)));
assert_eq!(plan.unicode_to_new_gid_list.len(), 2);
assert_eq!(plan.unicode_to_new_gid_list[0], (0x2c_u32, GlyphId::new(2)));
assert_eq!(plan.unicode_to_new_gid_list[1], (0x31_u32, GlyphId::new(4)));
}
#[test]
fn glyf_closure_composite_glyphs() {
let font = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
let loca = font.loca(None).unwrap();
let glyf = font.glyf().unwrap();
let mut gids = IntSet::empty();
glyf_closure_glyphs(&loca, &glyf, GlyphId::new(5), &mut gids, 64, 0);
assert_eq!(gids.len(), 2);
assert!(gids.contains(GlyphId::new(5)));
assert!(gids.contains(GlyphId::new(1)));
}
#[test]
fn populate_gids_wo_cmap_colr_layout() {
let mut plan = Plan::default();
let font = FontRef::new(font_test_data::GLYF_COMPONENTS).unwrap();
plan.font_num_glyphs = get_font_num_glyphs(&font);
plan.unicodes.insert(0x2c_u32);
plan.unicodes.insert(0x34_u32);
plan.glyphset_gsub.insert(GlyphId::new(2));
plan.glyphset_gsub.insert(GlyphId::new(7));
plan.populate_gids_to_retain(&font);
assert_eq!(plan.glyphset_gsub.len(), 3);
assert!(plan.glyphset_gsub.contains(GlyphId::new(0)));
assert!(plan.glyphset_gsub.contains(GlyphId::new(2)));
assert!(plan.glyphset_gsub.contains(GlyphId::new(7)));
assert_eq!(plan.glyphset.len(), 5);
assert!(plan.glyphset.contains(GlyphId::new(0)));
assert!(plan.glyphset.contains(GlyphId::new(1)));
assert!(plan.glyphset.contains(GlyphId::new(2)));
assert!(plan.glyphset.contains(GlyphId::new(4)));
assert!(plan.glyphset.contains(GlyphId::new(7)));
}
}