1mod extended;
3
4use super::{
5 CursivePosFormat1, ExtensionPosFormat1, ExtensionSubtable, Gpos, MarkBasePosFormat1,
6 MarkLigPosFormat1, MarkMarkPosFormat1, PairPos, PairPosFormat1, PairPosFormat2, PairSet,
7 PositionLookup, PositionLookupList, PositionSubtables, SinglePos, SinglePosFormat1,
8 SinglePosFormat2,
9};
10use crate::{
11 collections::IntSet, tables::layout::CoverageTable, ArrayOfOffsets, FontRead, GlyphId, Offset,
12 ReadError, Tag,
13};
14use font_types::Scalar;
15
16#[cfg(feature = "std")]
17use crate::tables::layout::{Intersect, LayoutLookupList, LookupClosure, LookupClosureCtx};
18
19impl Gpos<'_> {
20 pub fn collect_features(
22 &self,
23 scripts: &IntSet<Tag>,
24 languages: &IntSet<Tag>,
25 features: &IntSet<Tag>,
26 ) -> Result<IntSet<u16>, ReadError> {
27 if self.selected_script_list_offset()?.is_null()
28 || self.selected_feature_list_offset()?.is_null()
29 {
30 return Ok(IntSet::empty());
31 }
32 let feature_list = self.feature_list()?;
33 let script_list = self.script_list()?;
34 let head_ptr = self.offset_data().as_bytes().as_ptr() as usize;
35 script_list.collect_features(head_ptr, &feature_list, scripts, languages, features)
36 }
37
38 pub fn collect_lookups(&self, feature_indices: &IntSet<u16>) -> Result<IntSet<u16>, ReadError> {
40 if self.selected_feature_list_offset()?.is_null() {
41 return Ok(IntSet::empty());
42 }
43 let feature_list = self.feature_list()?;
44 let mut lookup_indices = feature_list.collect_lookups(feature_indices)?;
45
46 if let Some(feature_variations) = self.feature_variations().transpose()? {
47 let subs_lookup_indices = feature_variations.collect_lookups(feature_indices)?;
48 lookup_indices.union(&subs_lookup_indices);
49 }
50 Ok(lookup_indices)
51 }
52
53 pub fn closure_lookups(
55 &self,
56 glyphs: &IntSet<GlyphId>,
57 lookup_indices: &mut IntSet<u16>,
58 ) -> Result<(), ReadError> {
59 if self.selected_lookup_list_offset()?.is_null() {
60 return Ok(());
61 }
62 let lookup_list = self.lookup_list()?;
63 lookup_list.closure_lookups(glyphs, lookup_indices)
64 }
65}
66
67impl PositionLookupList<'_> {
68 pub fn closure_lookups(
69 &self,
70 glyph_set: &IntSet<GlyphId>,
71 lookup_indices: &mut IntSet<u16>,
72 ) -> Result<(), ReadError> {
73 lookup_indices.remove_range(self.lookup_count()..=u16::MAX);
74 if lookup_indices.is_empty() {
75 return Ok(());
76 }
77 let lookup_list = LayoutLookupList::Gpos(self);
78 let mut c = LookupClosureCtx::new(glyph_set, &lookup_list);
79
80 let lookups = self.lookups();
81 for idx in lookup_indices.iter() {
82 let lookup = match lookups.get(idx as usize) {
83 Err(ReadError::NullOffset) => {
84 c.set_lookup_inactive(idx);
85 continue;
86 }
87 other => other,
88 }?;
89 lookup.closure_lookups(&mut c, idx)?;
90 }
91
92 lookup_indices.union(c.visited_lookups());
93 lookup_indices.subtract(c.inactive_lookups());
94 Ok(())
95 }
96}
97
98impl LookupClosure for PositionLookup<'_> {
99 fn closure_lookups(
100 &self,
101 c: &mut LookupClosureCtx,
102 lookup_index: u16,
103 ) -> Result<(), ReadError> {
104 if !c.should_visit_lookup(lookup_index) {
105 return Ok(());
106 }
107
108 if !self.intersects(c.glyphs())? {
109 c.set_lookup_inactive(lookup_index);
110 return Ok(());
111 }
112 self.subtables()?.closure_lookups(c, lookup_index)
113 }
114}
115
116impl LookupClosure for PositionSubtables<'_> {
117 fn closure_lookups(&self, c: &mut LookupClosureCtx, arg: u16) -> Result<(), ReadError> {
118 match self {
119 PositionSubtables::Contextual(subtables) => subtables.closure_lookups(c, arg),
120 PositionSubtables::ChainContextual(subtables) => subtables.closure_lookups(c, arg),
121 _ => Ok(()),
122 }
123 }
124}
125
126impl Intersect for PositionLookup<'_> {
127 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
128 match self {
129 PositionLookup::Single(inner) => inner.subtables().intersects(glyph_set),
130 PositionLookup::Pair(inner) => inner.subtables().intersects(glyph_set),
131 PositionLookup::Cursive(inner) => inner.subtables().intersects(glyph_set),
132 PositionLookup::MarkToBase(inner) => inner.subtables().intersects(glyph_set),
133 PositionLookup::MarkToLig(inner) => inner.subtables().intersects(glyph_set),
134 PositionLookup::MarkToMark(inner) => inner.subtables().intersects(glyph_set),
135 PositionLookup::Contextual(inner) => inner.subtables().intersects(glyph_set),
136 PositionLookup::ChainContextual(inner) => inner.subtables().intersects(glyph_set),
137 PositionLookup::Extension(inner) => inner.subtables().intersects(glyph_set),
138 }
139 }
140}
141
142impl Intersect for ExtensionSubtable<'_> {
143 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
144 match self {
145 ExtensionSubtable::Single(inner) => inner.intersects(glyph_set),
146 ExtensionSubtable::Pair(inner) => inner.intersects(glyph_set),
147 ExtensionSubtable::Cursive(inner) => inner.intersects(glyph_set),
148 ExtensionSubtable::MarkToBase(inner) => inner.intersects(glyph_set),
149 ExtensionSubtable::MarkToLig(inner) => inner.intersects(glyph_set),
150 ExtensionSubtable::MarkToMark(inner) => inner.intersects(glyph_set),
151 ExtensionSubtable::Contextual(inner) => inner.intersects(glyph_set),
152 ExtensionSubtable::ChainContextual(inner) => inner.intersects(glyph_set),
153 }
154 }
155}
156
157impl<'a, T> Intersect for ExtensionPosFormat1<'a, T>
158where
159 T: Intersect + FontRead<'a, Args = ()>,
160{
161 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
162 if self.extension_offset().is_null() {
163 return Ok(false);
164 }
165 self.extension()?.intersects(glyph_set)
166 }
167}
168
169impl Intersect for SinglePos<'_> {
170 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
171 match self {
172 Self::Format1(item) => item.intersects(glyph_set),
173 Self::Format2(item) => item.intersects(glyph_set),
174 Self::Format3(item) => item.intersects(glyph_set),
175 Self::Format4(item) => item.intersects(glyph_set),
176 }
177 }
178}
179
180impl Intersect for SinglePosFormat1<'_> {
181 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
182 if self.coverage_offset().is_null() {
183 return Ok(false);
184 }
185 Ok(self.coverage()?.intersects(glyph_set))
186 }
187}
188
189impl Intersect for SinglePosFormat2<'_> {
190 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
191 if self.coverage_offset().is_null() {
192 return Ok(false);
193 }
194 Ok(self.coverage()?.intersects(glyph_set))
195 }
196}
197
198impl Intersect for PairPos<'_> {
199 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
200 match self {
201 Self::Format1(item) => item.intersects(glyph_set),
202 Self::Format2(item) => item.intersects(glyph_set),
203 Self::Format3(item) => item.intersects(glyph_set),
204 Self::Format4(item) => item.intersects(glyph_set),
205 }
206 }
207}
208
209fn pair_pos_intersects<'a, T, O>(
212 coverage: &CoverageTable,
213 pair_sets: ArrayOfOffsets<'a, T, O>,
214 count: u32,
215 glyphs: &IntSet<GlyphId>,
216) -> Result<bool, ReadError>
217where
218 T: FontRead<'a> + Intersect,
219 T::Args: Copy + 'static,
220 O: Scalar + Offset,
221{
222 let num_bits = 32 - count.leading_zeros();
223 if count as u64 > glyphs.len() * num_bits as u64 {
224 for gid in glyphs.iter() {
225 let Some(index) = coverage.get(gid) else {
226 continue;
227 };
228 let set = match pair_sets.get(index as usize) {
229 Err(ReadError::NullOffset) => continue,
230 other => other,
231 }?;
232 if set.intersects(glyphs)? {
233 return Ok(true);
234 }
235 }
236 } else {
237 for (gid, set) in coverage.iter().zip(pair_sets.iter_as_nullable()) {
238 if !glyphs.contains(gid) {
239 continue;
240 }
241 let Some(set) = set.transpose()? else {
242 continue;
243 };
244 if set.intersects(glyphs)? {
245 return Ok(true);
246 }
247 }
248 }
249 Ok(false)
250}
251
252impl Intersect for PairPosFormat1<'_> {
253 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
254 if self.coverage_offset().is_null() {
255 return Ok(false);
256 }
257 pair_pos_intersects(
258 &self.coverage()?,
259 self.pair_sets(),
260 u32::from(self.pair_set_count()),
261 glyph_set,
262 )
263 }
264}
265
266impl Intersect for PairSet<'_> {
267 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
268 for record in self.pair_value_records().iter() {
269 let second_glyph = record?.second_glyph();
270 if glyph_set.contains(GlyphId::from(second_glyph)) {
271 return Ok(true);
272 }
273 }
274 Ok(false)
275 }
276}
277
278impl Intersect for PairPosFormat2<'_> {
279 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
280 if self.coverage_offset().is_null()
281 || self.class_def1_offset().is_null()
282 || self.class_def2_offset().is_null()
283 {
284 return Ok(false);
285 }
286 Ok(self.coverage()?.intersects(glyph_set) && self.class_def2()?.intersects(glyph_set)?)
287 }
288}
289
290impl Intersect for CursivePosFormat1<'_> {
291 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
292 if self.coverage_offset().is_null() {
293 return Ok(false);
294 }
295 Ok(self.coverage()?.intersects(glyph_set))
296 }
297}
298
299impl Intersect for MarkBasePosFormat1<'_> {
300 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
301 if self.mark_coverage_offset().is_null() || self.base_coverage_offset().is_null() {
302 return Ok(false);
303 }
304 Ok(self.mark_coverage()?.intersects(glyph_set)
305 && self.base_coverage()?.intersects(glyph_set))
306 }
307}
308
309impl Intersect for MarkLigPosFormat1<'_> {
310 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
311 if self.mark_coverage_offset().is_null() || self.ligature_coverage_offset().is_null() {
312 return Ok(false);
313 }
314 Ok(self.mark_coverage()?.intersects(glyph_set)
315 && self.ligature_coverage()?.intersects(glyph_set))
316 }
317}
318
319impl Intersect for MarkMarkPosFormat1<'_> {
320 fn intersects(&self, glyph_set: &IntSet<GlyphId>) -> Result<bool, ReadError> {
321 if self.mark1_coverage_offset().is_null() || self.mark2_coverage_offset().is_null() {
322 return Ok(false);
323 }
324 Ok(self.mark1_coverage()?.intersects(glyph_set)
325 && self.mark2_coverage()?.intersects(glyph_set))
326 }
327}
328
329impl Intersect for super::CursivePos<'_> {
330 fn intersects(&self, glyphs: &IntSet<GlyphId>) -> Result<bool, ReadError> {
331 match self {
332 Self::Format1(table) => table.intersects(glyphs),
333 Self::Format2(table) => table.intersects(glyphs),
334 }
335 }
336}
337
338impl Intersect for super::MarkBasePos<'_> {
339 fn intersects(&self, glyphs: &IntSet<GlyphId>) -> Result<bool, ReadError> {
340 match self {
341 Self::Format1(table) => table.intersects(glyphs),
342 Self::Format2(table) => table.intersects(glyphs),
343 }
344 }
345}
346
347impl Intersect for super::MarkLigPos<'_> {
348 fn intersects(&self, glyphs: &IntSet<GlyphId>) -> Result<bool, ReadError> {
349 match self {
350 Self::Format1(table) => table.intersects(glyphs),
351 Self::Format2(table) => table.intersects(glyphs),
352 }
353 }
354}
355
356impl Intersect for super::MarkMarkPos<'_> {
357 fn intersects(&self, glyphs: &IntSet<GlyphId>) -> Result<bool, ReadError> {
358 match self {
359 Self::Format1(table) => table.intersects(glyphs),
360 Self::Format2(table) => table.intersects(glyphs),
361 }
362 }
363}