swash/
variation.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use super::internal::{var::*, RawFont};
use super::{
    setting::Setting,
    string::{LocalizedString, StringId},
    FontRef, NormalizedCoord, Tag,
};

/// Proxy for rematerializing variations collections.
#[derive(Copy, Clone)]
pub struct VariationsProxy {
    fvar: u32,
    avar: u32,
    len: usize,
}

impl VariationsProxy {
    /// Creates a variations proxy from the specified font.
    pub fn from_font(font: &FontRef) -> Self {
        let fvar = font.table_offset(FVAR);
        let table = Fvar::from_font(font).unwrap_or_else(|| Fvar::new(&[]));
        let avar = font.table_offset(AVAR);
        let len = table.axis_count() as usize;
        Self { fvar, avar, len }
    }

    /// Materializes variations from the specified font. This proxy must have
    /// been created by the same font.
    pub fn materialize<'a>(&self, font: &FontRef<'a>) -> Variations<'a> {
        let data = if self.fvar != 0 {
            font.data.get(self.fvar as usize..).unwrap_or(&[])
        } else {
            &[]
        };
        Variations {
            font: *font,
            fvar: Fvar::new(data),
            avar: self.avar,
            len: self.len,
            pos: 0,
        }
    }
}

/// Iterator over a collection of font variations.
#[derive(Copy, Clone)]
pub struct Variations<'a> {
    font: FontRef<'a>,
    fvar: Fvar<'a>,
    avar: u32,
    len: usize,
    pos: usize,
}

impl<'a> Variations<'a> {
    pub(crate) fn from_font(font: &FontRef<'a>) -> Self {
        let fvar = Fvar::from_font(font).unwrap_or_else(|| Fvar::new(&[]));
        let avar = font.table_offset(AVAR);
        let len = fvar.axis_count() as usize;
        Self {
            font: *font,
            fvar,
            avar,
            len,
            pos: 0,
        }
    }

    fn get(&self, index: usize) -> Option<Variation<'a>> {
        let axis = self.fvar.get_axis(index as u16)?;
        Some(Variation {
            font: self.font,
            axis,
            avar: self.avar,
        })
    }

    /// Searches for a variation with the specified tag.
    ///
    /// ## Iteration behavior
    /// This function searches the entire variation collection without regard
    /// for the current state of the iterator.
    pub fn find_by_tag(&self, tag: Tag) -> Option<Variation<'a>> {
        for i in 0..self.len {
            if let Some(var) = self.get(i) {
                if var.tag() == tag {
                    return Some(var);
                }
            }
        }
        None
    }

    /// Returns an iterator over the set of normalized coordinates
    /// corresponding to the specified variation settings.
    pub fn normalized_coords<I>(&self, settings: I) -> impl Iterator<Item = NormalizedCoord> + Clone
    where
        I: IntoIterator,
        I::Item: Into<Setting<f32>>,
    {
        let mut copy = *self;
        copy.pos = 0;
        let mut coords = [0i16; 32];
        let len = self.len.min(32);
        for setting in settings {
            let val = setting.into();
            let tag = val.tag;
            for (var, coord) in copy.take(len).zip(coords.iter_mut()) {
                if var.axis.tag == tag {
                    *coord = var.normalize(val.value);
                }
            }
        }
        (0..len).map(move |i| coords[i])
    }
}

impl_iter!(Variations, Variation);

/// Axis of variation in a variable font.
#[derive(Copy, Clone)]
pub struct Variation<'a> {
    font: FontRef<'a>,
    axis: VarAxis,
    avar: u32,
}

impl<'a> Variation<'a> {
    /// Returns the index of the variation.
    pub fn index(&self) -> usize {
        self.axis.index as usize
    }

    /// Returns the tag that identifies the variation.
    pub fn tag(&self) -> Tag {
        self.axis.tag
    }

    /// Returns the name identifier for the variation.
    pub fn name_id(&self) -> StringId {
        StringId::Other(self.axis.name_id)
    }

    /// Returns the name for the variation, optionally for a
    /// particular language.
    pub fn name(&self, language: Option<&str>) -> Option<LocalizedString<'a>> {
        self.font
            .localized_strings()
            .find_by_id(self.name_id(), language)
    }

    /// Returns true if the variation should be hidden from users.
    pub fn is_hidden(&self) -> bool {
        self.axis.is_hidden()
    }

    /// Returns the minimum value of the variation.
    pub fn min_value(&self) -> f32 {
        self.axis.min.to_f32()
    }

    /// Returns the maximum value of the variation.
    pub fn max_value(&self) -> f32 {
        self.axis.max.to_f32()
    }

    /// Returns the default value of the variation.
    pub fn default_value(&self) -> f32 {
        self.axis.default.to_f32()
    }

    /// Computes a normalized coordinate for the specified value.
    pub fn normalize(&self, value: f32) -> NormalizedCoord {
        let avar = if self.avar != 0 {
            Some((self.font.data, self.avar))
        } else {
            None
        };
        self.axis.normalized_coord(value.into(), avar)
    }
}

/// Iterator over a collection of named variation instances.
#[derive(Copy, Clone)]
pub struct Instances<'a> {
    font: FontRef<'a>,
    fvar: Fvar<'a>,
    avar: u32,
    len: usize,
    pos: usize,
}

impl<'a> Instances<'a> {
    pub(crate) fn from_font(font: &FontRef<'a>) -> Self {
        let fvar = Fvar::from_font(font).unwrap_or_else(|| Fvar::new(&[]));
        let avar = font.table_offset(AVAR);
        Self {
            font: *font,
            fvar,
            avar,
            len: fvar.instance_count() as usize,
            pos: 0,
        }
    }

    fn get(&self, index: usize) -> Option<Instance<'a>> {
        let inner = self.fvar.get_instance(index as u16)?;
        Some(Instance {
            parent: *self,
            inner,
        })
    }

    /// Searches for an instance with the specified name.
    ///
    /// ## Iteration behavior
    /// This function searches the entire instance collection without regard
    /// for the current state of the iterator.
    pub fn find_by_name(&self, name: &str) -> Option<Instance<'a>> {
        let strings = self.font.localized_strings();
        for i in 0..self.len {
            if let Some(instance) = self.get(i) {
                let id = instance.name_id();
                for instance_name in strings.filter(|s| s.id() == id) {
                    if instance_name.chars().eq(name.chars()) {
                        return Some(instance);
                    }
                }
            }
        }
        None
    }
    /// Searches for an instance with the specified PostScript name.
    ///
    /// ## Iteration behavior
    /// This function searches the entire instance collection without regard
    /// for the current state of the iterator.
    pub fn find_by_postscript_name(&self, name: &str) -> Option<Instance<'a>> {
        let strings = self.font.localized_strings();
        for i in 0..self.len {
            if let Some(instance) = self.get(i) {
                if let Some(id) = instance.postscript_name_id() {
                    for instance_name in strings.filter(|s| s.id() == id) {
                        if instance_name.chars().eq(name.chars()) {
                            return Some(instance);
                        }
                    }
                }
            }
        }
        None
    }
}

impl_iter!(Instances, Instance);

/// Named instance in a variable font.
#[derive(Copy, Clone)]
pub struct Instance<'a> {
    parent: Instances<'a>,
    inner: VarInstance<'a>,
}

impl<'a> Instance<'a> {
    /// Returns the index of the instance.
    pub fn index(&self) -> usize {
        self.inner.index as usize
    }

    /// Returns the name identifier for the instance.
    pub fn name_id(&self) -> StringId {
        StringId::Other(self.inner.name_id)
    }

    /// Returns the name for the instance, optionally for a
    /// particular language.
    pub fn name(&self, language: Option<&str>) -> Option<LocalizedString<'a>> {
        self.parent
            .font
            .localized_strings()
            .find_by_id(self.name_id(), language)
    }

    /// Returns the PostScript name identifier for the instance.
    pub fn postscript_name_id(&self) -> Option<StringId> {
        self.inner.postscript_name_id.map(StringId::Other)
    }

    /// Returns the PostScript name for the instance, optionally for a
    /// particular language.
    pub fn postscript_name(&self, language: Option<&str>) -> Option<LocalizedString<'a>> {
        self.parent
            .font
            .localized_strings()
            .find_by_id(self.postscript_name_id()?, language)
    }

    /// Returns an iterator over the variation values of the instance.
    pub fn values(&self) -> impl Iterator<Item = f32> + 'a {
        self.inner.values.iter().map(|v| v.to_f32())
    }

    /// Returns an iterator over the normalized coordinates for the instance.
    pub fn normalized_coords(&self) -> impl Iterator<Item = NormalizedCoord> + 'a {
        let avar = if self.parent.avar != 0 {
            Some((self.parent.font.data, self.parent.avar))
        } else {
            None
        };
        let fvar = self.parent.fvar;
        (0..fvar.axis_count())
            .map(move |i| fvar.get_axis(i).unwrap_or_default())
            .zip(self.inner.values)
            .map(move |(axis, value)| axis.normalized_coord(value, avar))
    }
}