swash/feature/
mod.rs

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
mod aat;
mod at;
mod util;

use super::internal::{self, raw_tag, Bytes, RawFont};
use super::{FontRef, Tag};
use crate::text::{Language, Script};

const DFLT: u32 = raw_tag(b"DFLT");

#[derive(Copy, Clone)]
enum Kind {
    None,
    /// GSUB, GPOS offsets
    At(u32, u32),
    /// Morx offset, kerning available
    Aat(u32, bool),
}

impl Kind {
    fn from_font(font: &FontRef) -> Self {
        let gsub = font.table_offset(raw_tag(b"GSUB"));
        let gpos = font.table_offset(raw_tag(b"GPOS"));
        if gsub != 0 || gpos != 0 {
            return Self::At(gsub, gpos);
        }
        let morx = font.table_offset(raw_tag(b"morx"));
        if morx != 0 {
            let kern = font.table_offset(raw_tag(b"kern")) != 0
                || font.table_offset(raw_tag(b"kerx")) != 0;
            return Self::Aat(morx, kern);
        }
        Self::None
    }
}

#[derive(Copy, Clone)]
enum WritingSystemsKind<'a> {
    None,
    At(at::WritingSystems<'a>),
    Aat(aat::OnceItem<'a>),
}

/// Iterator over a collection of writing systems.
#[derive(Copy, Clone)]
pub struct WritingSystems<'a> {
    kind: WritingSystemsKind<'a>,
}

impl<'a> WritingSystems<'a> {
    pub(crate) fn from_font(font: &FontRef<'a>) -> Self {
        let kind = Kind::from_font(font);
        WritingSystems {
            kind: match kind {
                Kind::At(gsub, gpos) => WritingSystemsKind::At(at::WritingSystems::new(
                    at::Scripts::new(Bytes::new(font.data), gsub, gpos),
                )),
                Kind::Aat(morx, kern) => WritingSystemsKind::Aat(Some(aat::Item {
                    chains: aat::chains(font.data, morx),
                    kern,
                })),
                _ => WritingSystemsKind::None,
            },
        }
    }
}

#[derive(Copy, Clone)]
enum WritingSystemKind<'a> {
    At(at::WritingSystem<'a>),
    Aat(aat::Item<'a>),
}

impl<'a> Iterator for WritingSystems<'a> {
    type Item = WritingSystem<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.kind {
            WritingSystemsKind::At(iter) => {
                let item = iter.next()?;
                Some(WritingSystem {
                    kind: WritingSystemKind::At(item),
                    script_tag: item.script_tag(),
                    lang_tag: item.language_tag(),
                    lang: Language::from_opentype(item.language_tag()),
                })
            }
            WritingSystemsKind::Aat(iter) => {
                let item = iter.take()?;
                Some(WritingSystem {
                    kind: WritingSystemKind::Aat(item),
                    script_tag: DFLT,
                    lang_tag: DFLT,
                    lang: None,
                })
            }
            _ => None,
        }
    }
}

/// Script, language and associated typographic features available in a font.
#[derive(Copy, Clone)]
pub struct WritingSystem<'a> {
    kind: WritingSystemKind<'a>,
    script_tag: Tag,
    lang_tag: Tag,
    lang: Option<Language>,
}

impl<'a> WritingSystem<'a> {
    /// Returns the OpenType script tag for the writing system.
    pub fn script_tag(&self) -> Tag {
        self.script_tag
    }

    /// Returns the OpenType language tag for the writing system.
    pub fn language_tag(&self) -> Tag {
        self.lang_tag
    }

    /// Returns the script for the writing system.
    pub fn script(&self) -> Option<Script> {
        Script::from_opentype(self.script_tag)
    }

    /// Returns the language for the writing system.
    pub fn language(&self) -> Option<Language> {
        self.lang
    }

    /// Returns an iterator over the features provided by the writing
    /// system.
    pub fn features(&self) -> Features<'a> {
        Features {
            kind: match self.kind {
                WritingSystemKind::At(item) => FeaturesKind::At(item.features()),
                WritingSystemKind::Aat(item) => {
                    FeaturesKind::Aat(aat::Features::new(item.chains, item.kern))
                }
            },
        }
    }
}

#[derive(Copy, Clone)]
enum FeaturesKind<'a> {
    None,
    At(at::Features<'a>),
    AtAll(at::AllFeatures<'a>),
    Aat(aat::Features<'a>),
}

/// Typographic rule that produces modifications to a sequence of glyphs.
#[derive(Copy, Clone)]
pub struct Feature {
    tag: Tag,
    name: Option<&'static str>,
    action: Action,
}

impl Feature {
    fn from_tag(tag: Tag, action: Action) -> Self {
        Self {
            tag,
            name: util::desc_from_at(tag).map(|x| x.1),
            action,
        }
    }

    /// Returns the feature tag.
    pub fn tag(&self) -> Tag {
        self.tag
    }

    /// Returns the name of the feature, if available.
    pub fn name(&self) -> Option<&'static str> {
        self.name
    }

    /// Returns the action of the feature.
    pub fn action(&self) -> Action {
        self.action
    }
}

/// Modification performed by a feature.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Action {
    /// Replaces one or more glyphs such as in ligation.
    Substitution,
    /// Attaches one glyph to another such as in accent mark placement.
    Attachment,
    /// Adjusts the position of one or more glyphs such as in kerning.
    Adjustment,
}

/// Iterator over a collection of typographic features.
#[derive(Copy, Clone)]
pub struct Features<'a> {
    kind: FeaturesKind<'a>,
}

impl<'a> Features<'a> {
    pub(crate) fn from_font(font: &FontRef<'a>) -> Self {
        let kind = Kind::from_font(font);
        Self {
            kind: match kind {
                Kind::At(gsub, gpos) => {
                    FeaturesKind::AtAll(at::AllFeatures::new(Bytes::new(font.data), gsub, gpos))
                }
                Kind::Aat(morx, kern) => {
                    FeaturesKind::Aat(aat::Features::new(aat::chains(font.data, morx), kern))
                }
                _ => FeaturesKind::None,
            },
        }
    }
}

const MARK: u32 = raw_tag(b"mark");
const MKMK: u32 = raw_tag(b"mkmk");

impl<'a> Iterator for Features<'a> {
    type Item = Feature;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.kind {
            FeaturesKind::At(iter) => {
                let item = iter.next()?;
                let action = if item.stage == 0 {
                    Action::Substitution
                } else {
                    match item.tag {
                        MARK | MKMK => Action::Attachment,
                        _ => Action::Adjustment,
                    }
                };
                Some(Feature::from_tag(item.tag, action))
            }
            FeaturesKind::AtAll(iter) => {
                let (stage, tag) = iter.next()?;
                let action = if stage == 0 {
                    Action::Substitution
                } else {
                    match tag {
                        MARK | MKMK => Action::Attachment,
                        _ => Action::Adjustment,
                    }
                };
                Some(Feature::from_tag(tag, action))
            }
            FeaturesKind::Aat(iter) => {
                let (tag, name) = iter.next()?;
                let action = if tag == raw_tag(b"kern") {
                    Action::Adjustment
                } else {
                    Action::Substitution
                };
                Some(Feature {
                    tag,
                    name: Some(name),
                    action,
                })
            }
            _ => None,
        }
    }
}