f_office_fonts/
script_glyphs.rs1use std::collections::HashMap;
6
7use skrifa::raw::tables::gsub::{
8 AlternateSubst, AlternateSubstFormat1, ExtensionSubtable, Gsub, SingleSubst, SubstitutionLookup,
9};
10use skrifa::raw::types::Tag;
11use skrifa::raw::{ReadError, TableProvider};
12
13use crate::ligature::{DEFAULT, feature_lookups};
14use crate::{FontDb, FontId};
15
16const MATH: Tag = Tag::new(b"math");
17const SSTY: Tag = Tag::new(b"ssty");
18const FLAC: Tag = Tag::new(b"flac");
19const DTLS: Tag = Tag::new(b"dtls");
20
21type Lookup = HashMap<u32, Substitute>;
25
26#[derive(Debug)]
27enum Substitute {
28 Single(u32),
30 Alternates(Vec<u32>),
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
38pub enum ScriptLevel {
39 Script,
41 ScriptOfScript,
43}
44
45impl ScriptLevel {
46 fn alternate(self) -> usize {
48 match self {
49 Self::Script => 0,
50 Self::ScriptOfScript => 1,
51 }
52 }
53}
54
55#[derive(Debug, Default)]
57pub(crate) struct ScriptGlyphs(Vec<Lookup>);
58
59impl ScriptGlyphs {
60 pub(crate) fn read(gsub: Result<Gsub<'_>, ReadError>) -> Self {
64 gsub.and_then(|gsub| feature(&gsub, SSTY))
65 .unwrap_or_default()
66 }
67
68 fn read_feature(gsub: Result<Gsub<'_>, ReadError>, tag: Tag) -> Self {
71 gsub.and_then(|gsub| feature(&gsub, tag))
72 .unwrap_or_default()
73 }
74
75 fn apply(&self, glyph: u32, level: ScriptLevel) -> Option<u32> {
79 let index = level.alternate();
80 let mut current = glyph;
81 for lookup in &self.0 {
82 current = match lookup.get(¤t) {
83 Some(Substitute::Single(substitute)) => *substitute,
84 Some(Substitute::Alternates(alternates)) => {
85 alternates.get(index).copied().unwrap_or(current)
86 }
87 None => current,
88 };
89 }
90 (current != glyph).then_some(current)
91 }
92}
93
94fn feature(gsub: &Gsub<'_>, tag: Tag) -> Result<ScriptGlyphs, ReadError> {
95 let indices = feature_lookups(
96 &gsub.script_list()?,
97 &gsub.feature_list()?,
98 tag,
99 &[MATH, DEFAULT],
100 )?;
101 let list = gsub.lookup_list()?;
102 let mut lookups = Vec::new();
103 for index in indices {
104 let mut map = Lookup::new();
105 read_lookup(&list.lookups().get(usize::from(index))?, &mut map)?;
106 if !map.is_empty() {
107 lookups.push(map);
108 }
109 }
110 Ok(ScriptGlyphs(lookups))
111}
112
113fn read_lookup(lookup: &SubstitutionLookup<'_>, map: &mut Lookup) -> Result<(), ReadError> {
117 match lookup {
118 SubstitutionLookup::Single(lookup) => {
119 for subtable in lookup.subtables().iter() {
120 read_single(&subtable?, map)?;
121 }
122 }
123 SubstitutionLookup::Alternate(lookup) => {
124 for subtable in lookup.subtables().iter() {
125 read_alternates(&subtable?, map)?;
126 }
127 }
128 SubstitutionLookup::Extension(lookup) => {
129 for subtable in lookup.subtables().iter() {
130 match subtable? {
131 ExtensionSubtable::Single(extension) => {
132 read_single(&extension.extension()?, map)?;
133 }
134 ExtensionSubtable::Alternate(extension) => {
135 read_alternates(&extension.extension()?, map)?;
136 }
137 _ => {}
138 }
139 }
140 }
141 _ => {}
142 }
143 Ok(())
144}
145
146fn read_single(table: &SingleSubst<'_>, map: &mut Lookup) -> Result<(), ReadError> {
147 match table {
148 SingleSubst::Format1(table) => {
149 let delta = i64::from(table.delta_glyph_id());
150 for glyph in table.coverage()?.iter() {
151 let from = glyph.to_u32();
152 let to = (i64::from(from) + delta).rem_euclid(65_536);
154 if let Ok(to) = u32::try_from(to) {
155 map.entry(from).or_insert(Substitute::Single(to));
156 }
157 }
158 }
159 SingleSubst::Format2(table) => {
160 let substitutes = table.substitute_glyph_ids();
161 for (glyph, substitute) in table.coverage()?.iter().zip(substitutes) {
162 map.entry(glyph.to_u32())
163 .or_insert(Substitute::Single(u32::from(substitute.get().to_u16())));
164 }
165 }
166 _ => {}
168 }
169 Ok(())
170}
171
172fn read_alternates(table: &AlternateSubst<'_>, map: &mut Lookup) -> Result<(), ReadError> {
173 if let AlternateSubst::Format1(table) = table {
175 read_alternates_16(table, map)?;
176 }
177 Ok(())
178}
179
180fn read_alternates_16(
181 table: &AlternateSubstFormat1<'_>,
182 map: &mut Lookup,
183) -> Result<(), ReadError> {
184 let sets = table.alternate_sets();
185 for (index, glyph) in table.coverage()?.iter().enumerate() {
186 let alternates = sets
187 .get(index)?
188 .alternate_glyph_ids()
189 .iter()
190 .map(|g| u32::from(g.get().to_u16()))
191 .collect();
192 map.entry(glyph.to_u32())
193 .or_insert(Substitute::Alternates(alternates));
194 }
195 Ok(())
196}
197
198impl FontDb {
199 #[must_use]
207 pub fn script_glyph(&self, id: FontId, glyph: u32, level: ScriptLevel) -> Option<u32> {
208 let face = self.face(id)?;
209 let font = face.font()?;
210 face.script_glyphs
211 .get_or_init(|| ScriptGlyphs::read(font.tables().gsub()))
212 .apply(glyph, level)
213 }
214
215 #[must_use]
222 pub fn flattened_accent(&self, id: FontId, glyph: u32) -> Option<u32> {
223 let face = self.face(id)?;
224 let font = face.font()?;
225 face.flattened_accents
226 .get_or_init(|| ScriptGlyphs::read_feature(font.tables().gsub(), FLAC))
227 .apply(glyph, ScriptLevel::Script)
228 }
229
230 #[must_use]
236 pub fn dotless_form(&self, id: FontId, glyph: u32) -> Option<u32> {
237 let face = self.face(id)?;
238 let font = face.font()?;
239 face.dotless_forms
240 .get_or_init(|| ScriptGlyphs::read_feature(font.tables().gsub(), DTLS))
241 .apply(glyph, ScriptLevel::Script)
242 }
243}
244
245#[cfg(test)]
246mod tests;