write_fonts/tables/
layout.rs

1//! OpenType layout.
2
3use std::{collections::HashSet, hash::Hash};
4
5pub use read_fonts::tables::layout::LookupFlag;
6use 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<read_fonts::tables::layout::FeatureParams<'_>> for FeatureParams {
238    fn from_obj_ref(from: &read_fonts::tables::layout::FeatureParams, data: FontData) -> Self {
239        use read_fonts::tables::layout::FeatureParams as FromType;
240        match from {
241            FromType::Size(thing) => Self::Size(SizeParams::from_obj_ref(thing, data)),
242            FromType::StylisticSet(thing) => {
243                Self::StylisticSet(FromObjRef::from_obj_ref(thing, data))
244            }
245            FromType::CharacterVariant(thing) => {
246                Self::CharacterVariant(FromObjRef::from_obj_ref(thing, data))
247            }
248        }
249    }
250}
251
252impl FromTableRef<read_fonts::tables::layout::FeatureParams<'_>> for FeatureParams {}
253
254impl ClassDefFormat1 {
255    fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
256        self.class_value_array.iter().enumerate().map(|(i, cls)| {
257            (
258                GlyphId16::new(self.start_glyph_id.to_u16().saturating_add(i as u16)),
259                *cls,
260            )
261        })
262    }
263}
264
265impl ClassRangeRecord {
266    fn validate_glyph_range(&self, ctx: &mut ValidationCtx) {
267        if self.start_glyph_id > self.end_glyph_id {
268            ctx.report(format!(
269                "start_glyph_id {} larger than end_glyph_id {}",
270                self.start_glyph_id, self.end_glyph_id
271            ));
272        }
273    }
274
275    fn contains(&self, gid: GlyphId16) -> bool {
276        (self.start_glyph_id..=self.end_glyph_id).contains(&gid)
277    }
278}
279
280impl ClassDefFormat2 {
281    fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
282        self.class_range_records.iter().flat_map(|rcd| {
283            (rcd.start_glyph_id.to_u16()..=rcd.end_glyph_id.to_u16())
284                .map(|gid| (GlyphId16::new(gid), rcd.class))
285        })
286    }
287}
288
289impl ClassDef {
290    pub fn iter(&self) -> impl Iterator<Item = (GlyphId16, u16)> + '_ {
291        let (one, two) = match self {
292            Self::Format1(table) => (Some(table.iter()), None),
293            Self::Format2(table) => (None, Some(table.iter())),
294        };
295
296        one.into_iter().flatten().chain(two.into_iter().flatten())
297    }
298
299    /// Return the glyph class for the provided glyph.
300    ///
301    /// Glyphs which have not been assigned a class are given class 0
302    pub fn get(&self, glyph: GlyphId16) -> u16 {
303        self.get_raw(glyph).unwrap_or(0)
304    }
305
306    // exposed for testing
307    fn get_raw(&self, glyph: GlyphId16) -> Option<u16> {
308        match self {
309            ClassDef::Format1(table) => glyph
310                .to_u16()
311                .checked_sub(table.start_glyph_id.to_u16())
312                .and_then(|idx| table.class_value_array.get(idx as usize))
313                .copied(),
314            ClassDef::Format2(table) => table
315                .class_range_records
316                .iter()
317                .find_map(|rec| rec.contains(glyph).then_some(rec.class)),
318        }
319    }
320
321    pub fn class_count(&self) -> u16 {
322        //TODO: implement a good integer set!!
323        self.iter()
324            .map(|(_gid, cls)| cls)
325            .chain(std::iter::once(0))
326            .collect::<HashSet<_>>()
327            .len()
328            .try_into()
329            .unwrap()
330    }
331}
332
333impl CoverageFormat1 {
334    fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
335        self.glyph_array.iter().copied()
336    }
337
338    fn len(&self) -> usize {
339        self.glyph_array.len()
340    }
341}
342
343impl CoverageFormat2 {
344    fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
345        self.range_records
346            .iter()
347            .flat_map(|rcd| iter_gids(rcd.start_glyph_id, rcd.end_glyph_id))
348    }
349
350    fn len(&self) -> usize {
351        self.range_records
352            .iter()
353            .map(|rcd| {
354                rcd.end_glyph_id
355                    .to_u16()
356                    .saturating_sub(rcd.start_glyph_id.to_u16()) as usize
357                    + 1
358            })
359            .sum()
360    }
361}
362
363impl CoverageTable {
364    pub fn iter(&self) -> impl Iterator<Item = GlyphId16> + '_ {
365        let (one, two) = match self {
366            Self::Format1(table) => (Some(table.iter()), None),
367            Self::Format2(table) => (None, Some(table.iter())),
368        };
369
370        one.into_iter().flatten().chain(two.into_iter().flatten())
371    }
372
373    pub fn len(&self) -> usize {
374        match self {
375            Self::Format1(table) => table.len(),
376            Self::Format2(table) => table.len(),
377        }
378    }
379
380    pub fn is_empty(&self) -> bool {
381        self.len() == 0
382    }
383}
384
385impl FromIterator<GlyphId16> for CoverageTable {
386    fn from_iter<T: IntoIterator<Item = GlyphId16>>(iter: T) -> Self {
387        let glyphs = iter.into_iter().collect::<Vec<_>>();
388        builders::CoverageTableBuilder::from_glyphs(glyphs).build()
389    }
390}
391
392impl From<Vec<GlyphId16>> for CoverageTable {
393    fn from(value: Vec<GlyphId16>) -> Self {
394        builders::CoverageTableBuilder::from_glyphs(value).build()
395    }
396}
397
398impl FromIterator<(GlyphId16, u16)> for ClassDef {
399    fn from_iter<T: IntoIterator<Item = (GlyphId16, u16)>>(iter: T) -> Self {
400        builders::ClassDefBuilderImpl::from_iter(iter).build()
401    }
402}
403
404impl RangeRecord {
405    /// An iterator over records for this array of glyphs.
406    ///
407    /// # Note
408    ///
409    /// this function expects that glyphs are already sorted.
410    pub fn iter_for_glyphs(glyphs: &[GlyphId16]) -> impl Iterator<Item = RangeRecord> + '_ {
411        let mut cur_range = glyphs.first().copied().map(|g| (g, g));
412        let mut len = 0u16;
413        let mut iter = glyphs.iter().skip(1).copied();
414
415        #[allow(clippy::while_let_on_iterator)]
416        std::iter::from_fn(move || {
417            while let Some(glyph) = iter.next() {
418                match cur_range {
419                    None => return None,
420                    Some((a, b)) if are_sequential(b, glyph) => cur_range = Some((a, glyph)),
421                    Some((a, b)) => {
422                        let result = RangeRecord {
423                            start_glyph_id: a,
424                            end_glyph_id: b,
425                            start_coverage_index: len,
426                        };
427                        cur_range = Some((glyph, glyph));
428                        len += 1 + b.to_u16().saturating_sub(a.to_u16());
429                        return Some(result);
430                    }
431                }
432            }
433            cur_range
434                .take()
435                .map(|(start_glyph_id, end_glyph_id)| RangeRecord {
436                    start_glyph_id,
437                    end_glyph_id,
438                    start_coverage_index: len,
439                })
440        })
441    }
442}
443
444fn iter_gids(gid1: GlyphId16, gid2: GlyphId16) -> impl Iterator<Item = GlyphId16> {
445    (gid1.to_u16()..=gid2.to_u16()).map(GlyphId16::new)
446}
447
448fn are_sequential(gid1: GlyphId16, gid2: GlyphId16) -> bool {
449    gid2.to_u16().saturating_sub(gid1.to_u16()) == 1
450}
451
452impl Device {
453    pub fn new(start_size: u16, end_size: u16, values: &[i8]) -> Self {
454        debug_assert_eq!(
455            (start_size..=end_size).count(),
456            values.len(),
457            "device range and values must match"
458        );
459        let delta_format: DeltaFormat = values
460            .iter()
461            .map(|val| match val {
462                -2..=1 => DeltaFormat::Local2BitDeltas,
463                -8..=7 => DeltaFormat::Local4BitDeltas,
464                _ => DeltaFormat::Local8BitDeltas,
465            })
466            .max()
467            .unwrap_or_default();
468        let delta_value = encode_delta(delta_format, values);
469
470        Device {
471            start_size,
472            end_size,
473            delta_format,
474            delta_value,
475        }
476    }
477}
478
479impl DeviceOrVariationIndex {
480    /// Create a new [`Device`] subtable
481    pub fn device(start_size: u16, end_size: u16, values: &[i8]) -> Self {
482        DeviceOrVariationIndex::Device(Device::new(start_size, end_size, values))
483    }
484}
485
486impl FontWrite for PendingVariationIndex {
487    fn write_into(&self, _writer: &mut TableWriter) {
488        panic!(
489            "Attempted to write PendingVariationIndex.\n\
490            VariationIndex tables should always be resolved before compilation.\n\
491            Please report this bug at <https://github.com/googlefonts/fontations/issues>"
492        )
493    }
494}
495
496fn encode_delta(format: DeltaFormat, values: &[i8]) -> Vec<u16> {
497    let (chunk_size, mask, bits) = match format {
498        DeltaFormat::Local2BitDeltas => (8, 0b11, 2),
499        DeltaFormat::Local4BitDeltas => (4, 0b1111, 4),
500        DeltaFormat::Local8BitDeltas => (2, 0b11111111, 8),
501        _ => panic!("invalid format"),
502    };
503    values
504        .chunks(chunk_size)
505        .map(|chunk| encode_chunk(chunk, mask, bits))
506        .collect()
507}
508
509fn encode_chunk(chunk: &[i8], mask: u8, bits: usize) -> u16 {
510    let mut out = 0u16;
511    for (i, val) in chunk.iter().enumerate() {
512        out |= ((val.to_be_bytes()[0] & mask) as u16) << ((16 - bits) - i * bits);
513    }
514    out
515}
516
517impl From<VariationIndex> for u32 {
518    fn from(value: VariationIndex) -> Self {
519        ((value.delta_set_outer_index as u32) << 16) | value.delta_set_inner_index as u32
520    }
521}
522
523#[cfg(test)]
524mod tests {
525    use super::*;
526
527    #[test]
528    #[should_panic(expected = "array exceeds max length")]
529    fn array_len_smoke_test() {
530        let table = ScriptList {
531            script_records: vec![ScriptRecord {
532                script_tag: Tag::new(b"hihi"),
533                script: OffsetMarker::new(Script {
534                    default_lang_sys: NullableOffsetMarker::new(None),
535                    lang_sys_records: vec![LangSysRecord {
536                        lang_sys_tag: Tag::new(b"coco"),
537                        lang_sys: OffsetMarker::new(LangSys {
538                            required_feature_index: 0xffff,
539                            feature_indices: vec![69; (u16::MAX) as usize + 5],
540                        }),
541                    }],
542                }),
543            }],
544        };
545
546        table.validate().unwrap();
547    }
548
549    #[test]
550    #[should_panic(expected = "larger than end_glyph_id")]
551    fn validate_classdef_ranges() {
552        let classdef = ClassDefFormat2::new(vec![ClassRangeRecord::new(
553            GlyphId16::new(12),
554            GlyphId16::new(3),
555            7,
556        )]);
557
558        classdef.validate().unwrap();
559    }
560
561    #[test]
562    fn delta_encode() {
563        let inp = [1i8, 2, 3, -1];
564        let result = encode_delta(DeltaFormat::Local4BitDeltas, &inp);
565        assert_eq!(result.len(), 1);
566        assert_eq!(result[0], 0x123f_u16);
567
568        let inp = [1i8, 1, 1, 1, 1];
569        let result = encode_delta(DeltaFormat::Local2BitDeltas, &inp);
570        assert_eq!(result.len(), 1);
571        assert_eq!(result[0], 0x5540_u16);
572    }
573}