1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Suppresses `minor_version` variable warning.
#![allow(unused_variables)]

#[cfg(feature = "variable-fonts")]
use super::FeatureVariations;
use super::LookupList;
#[cfg(feature = "variable-fonts")]
use crate::parser::Offset32;
use crate::parser::{FromData, LazyArray16, Offset, Offset16, Stream};
use crate::Tag;

/// A [Layout Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#table-organization).
#[derive(Clone, Copy, Debug)]
pub struct LayoutTable<'a> {
    /// A list of all supported scripts.
    pub scripts: ScriptList<'a>,
    /// A list of all supported features.
    pub features: FeatureList<'a>,
    /// A list of all lookups.
    pub lookups: LookupList<'a>,
    /// Used to substitute an alternate set of lookup tables
    /// to use for any given feature under specified conditions.
    #[cfg(feature = "variable-fonts")]
    pub variations: Option<FeatureVariations<'a>>,
}

impl<'a> LayoutTable<'a> {
    pub(crate) fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);

        let major_version = s.read::<u16>()?;
        let minor_version = s.read::<u16>()?;
        if major_version != 1 {
            return None;
        }

        let scripts = ScriptList::parse(s.read_at_offset16(data)?)?;
        let features = FeatureList::parse(s.read_at_offset16(data)?)?;
        let lookups = LookupList::parse(s.read_at_offset16(data)?)?;

        #[cfg(feature = "variable-fonts")]
        {
            let mut variations_offset = None;
            if minor_version >= 1 {
                variations_offset = s.read::<Option<Offset32>>()?;
            }

            let variations = match variations_offset {
                Some(offset) => data
                    .get(offset.to_usize()..)
                    .and_then(FeatureVariations::parse),
                None => None,
            };

            Some(Self {
                scripts,
                features,
                lookups,
                variations,
            })
        }

        #[cfg(not(feature = "variable-fonts"))]
        {
            Some(Self {
                scripts,
                features,
                lookups,
            })
        }
    }
}

/// An index in [`ScriptList`].
pub type ScriptIndex = u16;
/// An index in [`LanguageSystemList`].
pub type LanguageIndex = u16;
/// An index in [`FeatureList`].
pub type FeatureIndex = u16;
/// An index in [`LookupList`].
pub type LookupIndex = u16;
/// An index in [`FeatureVariations`].
pub type VariationIndex = u32;

/// A trait to parse item in [`RecordList`].
///
/// Internal use only.
pub trait RecordListItem<'a>: Sized {
    /// Parses raw data.
    fn parse(tag: Tag, data: &'a [u8]) -> Option<Self>;
}

/// A data storage used by [`ScriptList`], [`LanguageSystemList`] and [`FeatureList`] data types.
#[derive(Clone, Copy, Debug)]
pub struct RecordList<'a, T: RecordListItem<'a>> {
    data: &'a [u8],
    records: LazyArray16<'a, TagRecord>,
    data_type: core::marker::PhantomData<T>,
}

impl<'a, T: RecordListItem<'a>> RecordList<'a, T> {
    fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let count = s.read::<u16>()?;
        let records = s.read_array16(count)?;
        Some(Self {
            data,
            records,
            data_type: core::marker::PhantomData,
        })
    }

    /// Returns a number of items in the RecordList.
    pub fn len(&self) -> u16 {
        self.records.len()
    }

    /// Checks that RecordList is empty.
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }

    /// Returns RecordList value by index.
    pub fn get(&self, index: u16) -> Option<T> {
        let record = self.records.get(index)?;
        self.data
            .get(record.offset.to_usize()..)
            .and_then(|data| T::parse(record.tag, data))
    }

    /// Returns RecordList value by [`Tag`].
    pub fn find(&self, tag: Tag) -> Option<T> {
        let record = self
            .records
            .binary_search_by(|record| record.tag.cmp(&tag))
            .map(|p| p.1)?;
        self.data
            .get(record.offset.to_usize()..)
            .and_then(|data| T::parse(record.tag, data))
    }

    /// Returns RecordList value index by [`Tag`].
    pub fn index(&self, tag: Tag) -> Option<u16> {
        self.records
            .binary_search_by(|record| record.tag.cmp(&tag))
            .map(|p| p.0)
    }
}

impl<'a, T: RecordListItem<'a>> IntoIterator for RecordList<'a, T> {
    type Item = T;
    type IntoIter = RecordListIter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        RecordListIter {
            list: self,
            index: 0,
        }
    }
}

/// An iterator over [`RecordList`] values.
#[allow(missing_debug_implementations)]
pub struct RecordListIter<'a, T: RecordListItem<'a>> {
    list: RecordList<'a, T>,
    index: u16,
}

impl<'a, T: RecordListItem<'a>> Iterator for RecordListIter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.list.len() {
            self.index += 1;
            self.list.get(self.index - 1)
        } else {
            None
        }
    }
}

/// A list of [`Script`] records.
pub type ScriptList<'a> = RecordList<'a, Script<'a>>;
/// A list of [`LanguageSystem`] records.
pub type LanguageSystemList<'a> = RecordList<'a, LanguageSystem<'a>>;
/// A list of [`Feature`] records.
pub type FeatureList<'a> = RecordList<'a, Feature<'a>>;

#[derive(Clone, Copy, Debug)]
struct TagRecord {
    tag: Tag,
    offset: Offset16,
}

impl FromData for TagRecord {
    const SIZE: usize = 6;

    #[inline]
    fn parse(data: &[u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        Some(Self {
            tag: s.read::<Tag>()?,
            offset: s.read::<Offset16>()?,
        })
    }
}

/// A [Script Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-table-and-language-system-record).
#[derive(Clone, Copy, Debug)]
pub struct Script<'a> {
    /// Script tag.
    pub tag: Tag,
    /// Default language.
    pub default_language: Option<LanguageSystem<'a>>,
    /// List of supported languages, excluding the default one. Listed alphabetically.
    pub languages: LanguageSystemList<'a>,
}

impl<'a> RecordListItem<'a> for Script<'a> {
    fn parse(tag: Tag, data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let mut default_language = None;
        if let Some(offset) = s.read::<Option<Offset16>>()? {
            default_language =
                LanguageSystem::parse(Tag::from_bytes(b"dflt"), data.get(offset.to_usize()..)?);
        }
        let mut languages = RecordList::parse(s.tail()?)?;
        // Offsets are relative to this table.
        languages.data = data;
        Some(Self {
            tag,
            default_language,
            languages,
        })
    }
}

/// A [Language System Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#language-system-table).
#[derive(Clone, Copy, Debug)]
pub struct LanguageSystem<'a> {
    /// Language tag.
    pub tag: Tag,
    /// Index of a feature required for this language system.
    pub required_feature: Option<FeatureIndex>,
    /// Array of indices into the FeatureList, in arbitrary order.
    pub feature_indices: LazyArray16<'a, FeatureIndex>,
}

impl<'a> RecordListItem<'a> for LanguageSystem<'a> {
    fn parse(tag: Tag, data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let _lookup_order = s.read::<Offset16>()?; // Unsupported.
        let required_feature = match s.read::<FeatureIndex>()? {
            0xFFFF => None,
            v => Some(v),
        };
        let count = s.read::<u16>()?;
        let feature_indices = s.read_array16(count)?;
        Some(Self {
            tag,
            required_feature,
            feature_indices,
        })
    }
}

/// A [Feature](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#feature-table).
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub struct Feature<'a> {
    pub tag: Tag,
    pub lookup_indices: LazyArray16<'a, LookupIndex>,
}

impl<'a> RecordListItem<'a> for Feature<'a> {
    fn parse(tag: Tag, data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        let _params_offset = s.read::<Offset16>()?; // Unsupported.
        let count = s.read::<u16>()?;
        let lookup_indices = s.read_array16(count)?;
        Some(Self {
            tag,
            lookup_indices,
        })
    }
}