fontcull_write_fonts/tables/
layout.rs

1//! OpenType layout.
2
3use std::{collections::HashSet, hash::Hash};
4
5pub use fontcull_read_fonts::tables::layout::LookupFlag;
6use fontcull_read_fonts::FontRead;
7
8pub mod builders;
9#[cfg(test)]
10mod spec_tests;
11
12include!("../../generated/generated_layout.rs");
13
14/// A macro to implement the [LookupSubtable] trait.
15macro_rules! lookup_type {
16    (gpos, $ty:ty, $val:expr) => {
17        impl LookupSubtable for $ty {
18            const TYPE: LookupType = LookupType::Gpos($val);
19        }
20    };
21
22    (gsub, $ty:ty, $val:expr) => {
23        impl LookupSubtable for $ty {
24            const TYPE: LookupType = LookupType::Gsub($val);
25        }
26    };
27}
28
29/// A macro to define a newtype around an existing table, that defers all
30/// impls to that table.
31///
32/// We use this to ensure that shared lookup types (Sequence/Chain
33/// lookups) can be given different lookup ids for each of GSUB/GPOS.
34macro_rules! table_newtype {
35    ($name:ident, $inner:ident, $read_type:path) => {
36        /// A typed wrapper around a shared table.
37        ///
38        /// This is used so that we can associate the correct lookup ids for
39        /// lookups that are shared between GPOS/GSUB.
40        ///
41        /// You can access the inner type via `Deref` or the `as_inner` method.
42        #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
43        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44        pub struct $name($inner);
45
46        impl $name {
47            /// Return a reference to the inner type.
48            pub fn as_inner(&self) -> &$inner {
49                &self.0
50            }
51        }
52
53        impl std::ops::Deref for $name {
54            type Target = $inner;
55            fn deref(&self) -> &Self::Target {
56                &self.0
57            }
58        }
59
60        impl std::ops::DerefMut for $name {
61            fn deref_mut(&mut self) -> &mut Self::Target {
62                &mut self.0
63            }
64        }
65
66        impl FontWrite for $name {
67            fn write_into(&self, writer: &mut TableWriter) {
68                self.0.write_into(writer)
69            }
70
71            fn table_type(&self) -> crate::table_type::TableType {
72                self.0.table_type()
73            }
74        }
75
76        impl Validate for $name {
77            fn validate_impl(&self, ctx: &mut ValidationCtx) {
78                self.0.validate_impl(ctx)
79            }
80        }
81
82        impl<'a> FromObjRef<$read_type> for $name {
83            fn from_obj_ref(obj: &$read_type, _data: FontData) -> Self {
84                Self(FromObjRef::from_obj_ref(obj, _data))
85            }
86        }
87
88        impl<'a> FromTableRef<$read_type> for $name {}
89
90        impl From<$inner> for $name {
91            fn from(src: $inner) -> $name {
92                $name(src)
93            }
94        }
95    };
96}
97
98pub(crate) use lookup_type;
99pub(crate) use table_newtype;
100
101impl FontWrite for LookupFlag {
102    fn write_into(&self, writer: &mut TableWriter) {
103        self.to_bits().write_into(writer)
104    }
105}
106
107impl<T: LookupSubtable + FontWrite> FontWrite for Lookup<T> {
108    fn write_into(&self, writer: &mut TableWriter) {
109        T::TYPE.write_into(writer);
110        self.lookup_flag.write_into(writer);
111        u16::try_from(self.subtables.len())
112            .unwrap()
113            .write_into(writer);
114        self.subtables.write_into(writer);
115        self.mark_filtering_set.write_into(writer);
116    }
117
118    fn table_type(&self) -> crate::table_type::TableType {
119        T::TYPE.into()
120    }
121}
122
123impl Lookup<SequenceContext> {
124    /// Convert this untyped SequenceContext into its GSUB or GPOS specific version
125    pub fn into_concrete<T: From<SequenceContext>>(self) -> Lookup<T> {
126        let Lookup {
127            lookup_flag,
128            subtables,
129            mark_filtering_set,
130        } = self;
131        let subtables = subtables
132            .into_iter()
133            .map(|offset| OffsetMarker::new(offset.into_inner().into()))
134            .collect();
135        Lookup {
136            lookup_flag,
137            subtables,
138            mark_filtering_set,
139        }
140    }
141}
142
143impl Lookup<ChainedSequenceContext> {
144    /// Convert this untyped SequenceContext into its GSUB or GPOS specific version
145    pub fn into_concrete<T: From<ChainedSequenceContext>>(self) -> Lookup<T> {
146        let Lookup {
147            lookup_flag,
148            subtables,
149            mark_filtering_set,
150        } = self;
151        let subtables = subtables
152            .into_iter()
153            .map(|offset| OffsetMarker::new(offset.into_inner().into()))
154            .collect();
155        Lookup {
156            lookup_flag,
157            subtables,
158            mark_filtering_set,
159        }
160    }
161}
162
163/// A utility trait for writing lookup tables.
164///
165/// This allows us to attach the numerical lookup type to the appropriate concrete
166/// types, so that we can write it as needed without passing it around.
167pub trait LookupSubtable {
168    /// The lookup type of this layout subtable.
169    const TYPE: LookupType;
170}
171
172/// Raw values for the different layout subtables
173#[derive(Clone, Copy, Debug, PartialEq, Eq)]
174pub enum LookupType {
175    Gpos(u16),
176    Gsub(u16),
177}
178
179impl LookupType {
180    pub(crate) const GSUB_EXT_TYPE: u16 = 7;
181    pub(crate) const GPOS_EXT_TYPE: u16 = 9;
182    pub(crate) const PAIR_POS: u16 = 2;
183    pub(crate) const MARK_TO_BASE: u16 = 4;
184
185    pub(crate) fn to_raw(self) -> u16 {
186        match self {
187            LookupType::Gpos(val) => val,
188            LookupType::Gsub(val) => val,
189        }
190    }
191
192    pub(crate) fn promote(self) -> Self {
193        match self {
194            LookupType::Gpos(Self::GPOS_EXT_TYPE) | LookupType::Gsub(Self::GSUB_EXT_TYPE) => {
195                panic!("should never be promoting an extension subtable")
196            }
197            LookupType::Gpos(_) => LookupType::Gpos(Self::GPOS_EXT_TYPE),
198            LookupType::Gsub(_) => LookupType::Gsub(Self::GSUB_EXT_TYPE),
199        }
200    }
201}
202
203impl FontWrite for LookupType {
204    fn write_into(&self, writer: &mut TableWriter) {
205        self.to_raw().write_into(writer)
206    }
207}
208
209#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
210#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
211pub enum FeatureParams {
212    StylisticSet(StylisticSetParams),
213    Size(SizeParams),
214    CharacterVariant(CharacterVariantParams),
215}
216
217impl FontWrite for FeatureParams {
218    fn write_into(&self, writer: &mut TableWriter) {
219        match self {
220            FeatureParams::StylisticSet(table) => table.write_into(writer),
221            FeatureParams::Size(table) => table.write_into(writer),
222            FeatureParams::CharacterVariant(table) => table.write_into(writer),
223        }
224    }
225}
226
227impl Validate for FeatureParams {
228    fn validate_impl(&self, ctx: &mut ValidationCtx) {
229        match self {
230            Self::StylisticSet(table) => table.validate_impl(ctx),
231            Self::Size(table) => table.validate_impl(ctx),
232            Self::CharacterVariant(table) => table.validate_impl(ctx),
233        }
234    }
235}
236
237impl FromObjRef<fontcull_read_fonts::tables::layout::FeatureParams<'_>> for FeatureParams {
238    fn from_obj_ref(
239        from: &fontcull_read_fonts::tables::layout::FeatureParams,
240        data: FontData,
241    ) -> Self {
242        use fontcull_read_fonts::tables::layout::FeatureParams as FromType;
243        match from {
244            FromType::Size(thing) => Self::Size(SizeParams::from_obj_ref(thing, data)),
245            FromType::StylisticSet(thing) => {
246                Self::StylisticSet(FromObjRef::from_obj_ref(thing, data))
247            }
248            FromType::CharacterVariant(thing) => {
249                Self::CharacterVariant(FromObjRef::from_obj_ref(thing, data))
250            }
251        }
252    }
253}
254
255impl FromTableRef<fontcull_read_fonts::tables::layout::FeatureParams<'_>> for FeatureParams {}
256
257impl ClassDefFormat1 {
258    fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
259        self.class_value_array.iter().enumerate().map(|(i, cls)| {
260            (
261                GlyphId16::new(self.start_glyph_id.to_u16().saturating_add(i as u16)),
262                *cls,
263            )
264        })
265    }
266}
267
268impl ClassRangeRecord {
269    fn validate_glyph_range(&self, ctx: &mut ValidationCtx) {
270        if self.start_glyph_id > self.end_glyph_id {
271            ctx.report(format!(
272                "start_glyph_id {} larger than end_glyph_id {}",
273                self.start_glyph_id, self.end_glyph_id
274            ));
275        }
276    }
277
278    fn contains(&self, gid: GlyphId16) -> bool {
279        (self.start_glyph_id..=self.end_glyph_id).contains(&gid)
280    }
281}
282
283impl ClassDefFormat2 {
284    fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
285        self.class_range_records.iter().flat_map(|rcd| {
286            (rcd.start_glyph_id.to_u16()..=rcd.end_glyph_id.to_u16())
287                .map(|gid| (GlyphId16::new(gid), rcd.class))
288        })
289    }
290}
291
292impl ClassDef {
293    pub fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
294        let (one, two) = match self {
295            Self::Format1(table) => (Some(table.iter()), None),
296            Self::Format2(table) => (None, Some(table.iter())),
297        };
298
299        one.into_iter().flatten().chain(two.into_iter().flatten())
300    }
301
302    /// Return the glyph class for the provided glyph.
303    ///
304    /// Glyphs which have not been assigned a class are given class 0
305    pub fn get(&self, glyph: GlyphId16) -> u16 {
306        self.get_raw(glyph).unwrap_or(0)
307    }
308
309    // exposed for testing
310    fn get_raw(&self, glyph: GlyphId16) -> Option<u16> {
311        match self {
312            ClassDef::Format1(table) => glyph
313                .to_u16()
314                .checked_sub(table.start_glyph_id.to_u16())
315                .and_then(|idx| table.class_value_array.get(idx as usize))
316                .copied(),
317            ClassDef::Format2(table) => table
318                .class_range_records
319                .iter()
320                .find_map(|rec| rec.contains(glyph).then_some(rec.class)),
321        }
322    }
323
324    pub fn class_count(&self) -> u16 {
325        //TODO: implement a good integer set!!
326        self.iter()
327            .map(|(_gid, cls)| cls)
328            .chain(std::iter::once(0))
329            .collect::<HashSet<_>>()
330            .len()
331            .try_into()
332            .unwrap()
333    }
334}
335
336impl CoverageFormat1 {
337    fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
338        self.glyph_array.iter().copied()
339    }
340
341    fn len(&self) -> usize {
342        self.glyph_array.len()
343    }
344}
345
346impl CoverageFormat2 {
347    fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
348        self.range_records
349            .iter()
350            .flat_map(|rcd| iter_gids(rcd.start_glyph_id, rcd.end_glyph_id))
351    }
352
353    fn len(&self) -> usize {
354        self.range_records
355            .iter()
356            .map(|rcd| {
357                rcd.end_glyph_id
358                    .to_u16()
359                    .saturating_sub(rcd.start_glyph_id.to_u16()) as usize
360                    + 1
361            })
362            .sum()
363    }
364}
365
366impl CoverageTable {
367    pub fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
368        let (one, two) = match self {
369            Self::Format1(table) => (Some(table.iter()), None),
370            Self::Format2(table) => (None, Some(table.iter())),
371        };
372
373        one.into_iter().flatten().chain(two.into_iter().flatten())
374    }
375
376    pub fn len(&self) -> usize {
377        match self {
378            Self::Format1(table) => table.len(),
379            Self::Format2(table) => table.len(),
380        }
381    }
382
383    pub fn is_empty(&self) -> bool {
384        self.len() == 0
385    }
386}
387
388impl FromIterator<GlyphId16> for CoverageTable {
389    fn from_iter<T: IntoIterator<Item = GlyphId16>>(iter: T) -> Self {
390        let glyphs = iter.into_iter().collect::<Vec<_>>();
391        builders::CoverageTableBuilder::from_glyphs(glyphs).build()
392    }
393}
394
395impl From<Vec<GlyphId16>> for CoverageTable {
396    fn from(value: Vec<GlyphId16>) -> Self {
397        builders::CoverageTableBuilder::from_glyphs(value).build()
398    }
399}
400
401impl FromIterator<(GlyphId16, u16)> for ClassDef {
402    fn from_iter<T: IntoIterator<Item = (GlyphId16, u16)>>(iter: T) -> Self {
403        builders::ClassDefBuilderImpl::from_iter(iter).build()
404    }
405}
406
407impl RangeRecord {
408    /// An iterator over records for this array of glyphs.
409    ///
410    /// # Note
411    ///
412    /// this function expects that glyphs are already sorted.
413    pub fn iter_for_glyphs(glyphs: &[GlyphId16]) -> impl Iterator<Item = RangeRecord> + '_ {
414        let mut cur_range = glyphs.first().copied().map(|g| (g, g));
415        let mut len = 0u16;
416        let mut iter = glyphs.iter().skip(1).copied();
417
418        #[allow(clippy::while_let_on_iterator)]
419        std::iter::from_fn(move || {
420            while let Some(glyph) = iter.next() {
421                match cur_range {
422                    None => return None,
423                    Some((a, b)) if are_sequential(b, glyph) => cur_range = Some((a, glyph)),
424                    Some((a, b)) => {
425                        let result = RangeRecord {
426                            start_glyph_id: a,
427                            end_glyph_id: b,
428                            start_coverage_index: len,
429                        };
430                        cur_range = Some((glyph, glyph));
431                        len += 1 + b.to_u16().saturating_sub(a.to_u16());
432                        return Some(result);
433                    }
434                }
435            }
436            cur_range
437                .take()
438                .map(|(start_glyph_id, end_glyph_id)| RangeRecord {
439                    start_glyph_id,
440                    end_glyph_id,
441                    start_coverage_index: len,
442                })
443        })
444    }
445}
446
447fn iter_gids(gid1: GlyphId16, gid2: GlyphId16) -> impl Iterator<Item = GlyphId16> {
448    (gid1.to_u16()..=gid2.to_u16()).map(GlyphId16::new)
449}
450
451fn are_sequential(gid1: GlyphId16, gid2: GlyphId16) -> bool {
452    gid2.to_u16().saturating_sub(gid1.to_u16()) == 1
453}
454
455impl Device {
456    pub fn new(start_size: u16, end_size: u16, values: &[i8]) -> Self {
457        debug_assert_eq!(
458            (start_size..=end_size).count(),
459            values.len(),
460            "device range and values must match"
461        );
462        let delta_format: DeltaFormat = values
463            .iter()
464            .map(|val| match val {
465                -2..=1 => DeltaFormat::Local2BitDeltas,
466                -8..=7 => DeltaFormat::Local4BitDeltas,
467                _ => DeltaFormat::Local8BitDeltas,
468            })
469            .max()
470            .unwrap_or_default();
471        let delta_value = encode_delta(delta_format, values);
472
473        Device {
474            start_size,
475            end_size,
476            delta_format,
477            delta_value,
478        }
479    }
480}
481
482impl DeviceOrVariationIndex {
483    /// Create a new [`Device`] subtable
484    pub fn device(start_size: u16, end_size: u16, values: &[i8]) -> Self {
485        DeviceOrVariationIndex::Device(Device::new(start_size, end_size, values))
486    }
487}
488
489impl FontWrite for PendingVariationIndex {
490    fn write_into(&self, _writer: &mut TableWriter) {
491        panic!(
492            "Attempted to write PendingVariationIndex.\n\
493            VariationIndex tables should always be resolved before compilation.\n\
494            Please report this bug at <https://github.com/googlefonts/fontations/issues>"
495        )
496    }
497}
498
499fn encode_delta(format: DeltaFormat, values: &[i8]) -> Vec<u16> {
500    let (chunk_size, mask, bits) = match format {
501        DeltaFormat::Local2BitDeltas => (8, 0b11, 2),
502        DeltaFormat::Local4BitDeltas => (4, 0b1111, 4),
503        DeltaFormat::Local8BitDeltas => (2, 0b11111111, 8),
504        _ => panic!("invalid format"),
505    };
506    values
507        .chunks(chunk_size)
508        .map(|chunk| encode_chunk(chunk, mask, bits))
509        .collect()
510}
511
512fn encode_chunk(chunk: &[i8], mask: u8, bits: usize) -> u16 {
513    let mut out = 0u16;
514    for (i, val) in chunk.iter().enumerate() {
515        out |= ((val.to_be_bytes()[0] & mask) as u16) << ((16 - bits) - i * bits);
516    }
517    out
518}
519
520impl From<VariationIndex> for u32 {
521    fn from(value: VariationIndex) -> Self {
522        ((value.delta_set_outer_index as u32) << 16) | value.delta_set_inner_index as u32
523    }
524}
525
526#[cfg(test)]
527mod tests {
528    use super::*;
529
530    #[test]
531    #[should_panic(expected = "array exceeds max length")]
532    fn array_len_smoke_test() {
533        let table = ScriptList {
534            script_records: vec![ScriptRecord {
535                script_tag: Tag::new(b"hihi"),
536                script: OffsetMarker::new(Script {
537                    default_lang_sys: NullableOffsetMarker::new(None),
538                    lang_sys_records: vec![LangSysRecord {
539                        lang_sys_tag: Tag::new(b"coco"),
540                        lang_sys: OffsetMarker::new(LangSys {
541                            required_feature_index: 0xffff,
542                            feature_indices: vec![69; (u16::MAX) as usize + 5],
543                        }),
544                    }],
545                }),
546            }],
547        };
548
549        table.validate().unwrap();
550    }
551
552    #[test]
553    #[should_panic(expected = "larger than end_glyph_id")]
554    fn validate_classdef_ranges() {
555        let classdef = ClassDefFormat2::new(vec![ClassRangeRecord::new(
556            GlyphId16::new(12),
557            GlyphId16::new(3),
558            7,
559        )]);
560
561        classdef.validate().unwrap();
562    }
563
564    #[test]
565    fn delta_encode() {
566        let inp = [1i8, 2, 3, -1];
567        let result = encode_delta(DeltaFormat::Local4BitDeltas, &inp);
568        assert_eq!(result.len(), 1);
569        assert_eq!(result[0], 0x123f_u16);
570
571        let inp = [1i8, 1, 1, 1, 1];
572        let result = encode_delta(DeltaFormat::Local2BitDeltas, &inp);
573        assert_eq!(result.len(), 1);
574        assert_eq!(result[0], 0x5540_u16);
575    }
576}