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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// https://docs.microsoft.com/en-us/typography/opentype/spec/name

use std::convert::TryFrom;

use crate::parser::{Stream, FromData, SafeStream, LazyArray};
use crate::{Font, TableName, Result, Error};


/// A [platform ID](https://docs.microsoft.com/en-us/typography/opentype/spec/name#platform-ids).
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum PlatformId {
    Unicode,
    Macintosh,
    Iso,
    Windows,
    Custom,
}

impl TryFrom<u16> for PlatformId {
    type Error = &'static str;

    #[inline]
    fn try_from(value: u16) -> std::result::Result<Self, Self::Error> {
        match value {
            0 => Ok(PlatformId::Unicode),
            1 => Ok(PlatformId::Macintosh),
            2 => Ok(PlatformId::Iso),
            3 => Ok(PlatformId::Windows),
            4 => Ok(PlatformId::Custom),
            _ => Err("invalid id"),
        }
    }
}


/// A [name ID](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids).
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum NameId {
    CopyrightNotice,
    Family,
    Subfamily,
    UniqueID,
    FullName,
    Version,
    PostScriptName,
    Trademark,
    Manufacturer,
    Designer,
    Description,
    VendorUrl,
    DesignerUrl,
    License,
    LicenseUrl,
    TypographicFamily,
    TypographicSubfamily,
    CompatibleFull,
    SampleText,
    PostScriptCID,
    WWSFamily,
    WWSSubfamily,
    LightBackgroundPalette,
    DarkBackgroundPalette,
    VariationsPostScriptNamePrefix,
}

impl TryFrom<u16> for NameId {
    type Error = &'static str;

    #[inline]
    fn try_from(value: u16) -> std::result::Result<Self, Self::Error> {
        match value {
            0 => Ok(NameId::CopyrightNotice),
            1 => Ok(NameId::Family),
            2 => Ok(NameId::Subfamily),
            3 => Ok(NameId::UniqueID),
            4 => Ok(NameId::FullName),
            5 => Ok(NameId::Version),
            6 => Ok(NameId::PostScriptName),
            7 => Ok(NameId::Trademark),
            8 => Ok(NameId::Manufacturer),
            9 => Ok(NameId::Designer),
            10 => Ok(NameId::Description),
            11 => Ok(NameId::VendorUrl),
            12 => Ok(NameId::DesignerUrl),
            13 => Ok(NameId::License),
            14 => Ok(NameId::LicenseUrl),
            // 15 - reserved
            16 => Ok(NameId::TypographicFamily),
            17 => Ok(NameId::TypographicSubfamily),
            18 => Ok(NameId::CompatibleFull),
            19 => Ok(NameId::SampleText),
            20 => Ok(NameId::PostScriptCID),
            21 => Ok(NameId::WWSFamily),
            22 => Ok(NameId::WWSSubfamily),
            23 => Ok(NameId::LightBackgroundPalette),
            24 => Ok(NameId::DarkBackgroundPalette),
            25 => Ok(NameId::VariationsPostScriptNamePrefix),
            _ => Err("invalid id"),
        }
    }
}


/// A [Name Record](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records).
#[derive(Clone, Copy)]
pub struct Name<'a> {
    /// Raw name data.
    pub name: &'a [u8],

    /// Platform ID.
    pub platform_id: PlatformId,

    /// Platform-specific encoding ID.
    pub encoding_id: u16,

    /// Language ID.
    pub language_id: u16,

    /// Name ID.
    pub name_id: NameId,
}

impl<'a> Name<'a> {
    /// Converts Name's data into a `String`.
    ///
    /// Only Unicode names are supported. And since they are stored as UTF-16BE,
    /// we can't return `&str` and have to allocate a `String`.
    ///
    /// Supports:
    /// - Unicode Platform ID
    /// - Windows Platform ID + Unicode BMP
    #[inline(never)]
    pub fn to_string(&self) -> Option<String> {
        if self.is_supported_encoding() {
            self.name_from_utf16_be()
        } else {
            None
        }
    }

    #[inline]
    fn is_supported_encoding(&self) -> bool {
        // https://docs.microsoft.com/en-us/typography/opentype/spec/name#windows-encoding-ids
        const WINDOWS_UNICODE_BMP_ENCODING_ID: u16 = 1;

        match self.platform_id {
            PlatformId::Unicode => true,
            PlatformId::Windows if self.encoding_id == WINDOWS_UNICODE_BMP_ENCODING_ID => true,
            _ => false,
        }
    }

    #[inline(never)]
    fn name_from_utf16_be(&self) -> Option<String> {
        let mut name: Vec<u16> = Vec::new();
        for c in LazyArray::new(self.name) {
            name.push(c);
        }

        String::from_utf16(&name).ok()
    }
}

impl<'a> std::fmt::Debug for Name<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        // TODO: https://github.com/rust-lang/rust/issues/50264

        let name = self.to_string();
        f.debug_struct("Name")
            .field("name", &name.as_ref().map(std::ops::Deref::deref)
                                .unwrap_or("unsupported encoding"))
            .field("platform_id", &self.platform_id)
            .field("encoding_id", &self.encoding_id)
            .field("language_id", &self.language_id)
            .field("name_id", &self.name_id)
            .finish()
    }
}


#[derive(Clone, Copy)]
struct NameRecord {
    platform_id: u16,
    encoding_id: u16,
    language_id: u16,
    name_id: u16,
    length: u16,
    offset: u16,
}

impl FromData for NameRecord {
    #[inline]
    fn parse(s: &mut SafeStream) -> Self {
        NameRecord {
            platform_id: s.read(),
            encoding_id: s.read(),
            language_id: s.read(),
            name_id: s.read(),
            length: s.read(),
            offset: s.read(),
        }
    }
}


/// An iterator over font's names.
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
pub struct Names<'a> {
    names: LazyArray<'a, NameRecord>,
    storage: &'a [u8],
    index: u16,
}

impl<'a> Iterator for Names<'a> {
    type Item = Name<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index as usize == self.names.len() {
            return None;
        }

        let index = self.index;
        self.index += 1;
        let name = self.names.get(index)?;

        let platform_id = match PlatformId::try_from(name.platform_id) {
            Ok(v) => v,
            Err(_) => return self.next(),
        };

        let name_id = match NameId::try_from(name.name_id) {
            Ok(v) => v,
            Err(_) => return self.next(),
        };

        let start = name.offset as usize;
        let end = start + name.length as usize;
        let data = match self.storage.get(start..end) {
            Some(data) => data,
            None => return self.next(),
        };

        Some(Name {
            name: data,
            platform_id,
            encoding_id: name.encoding_id,
            language_id: name.language_id,
            name_id,
        })
    }
}


impl<'a> Font<'a> {
    /// Returns an iterator over [Name Records].
    ///
    /// [Name Records]: https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records
    pub fn names(&self) -> Names {
        match self._names() {
            Ok(v) => v,
            Err(_) => Names { names: LazyArray::new(&[]), storage: &[], index: 0 },
        }
    }

    #[inline(never)]
    fn _names(&self) -> Result<Names> {
        // https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-format-1
        const LANG_TAG_RECORD_SIZE: u16 = 4;

        let data = self.name.ok_or_else(|| Error::TableMissing(TableName::Naming))?;
        let mut s = Stream::new(data);
        let format: u16 = s.read()?;
        let count: u16 = s.read()?;
        s.skip::<u16>(); // offset
        let names = s.read_array(count)?;

        if format == 0 {
            Ok(Names {
                names,
                storage: s.tail()?,
                index: 0,
            })
        } else if format == 1 {
            let lang_tag_count: u16 = s.read()?;
            let lang_tag_len = lang_tag_count
                .checked_mul(LANG_TAG_RECORD_SIZE)
                .ok_or_else(|| Error::NotATrueType)?;

            s.skip_len(lang_tag_len); // langTagRecords
            Ok(Names {
                names,
                storage: s.tail()?,
                index: 0,
            })
        } else {
            // Invalid format.
            // The error type doesn't matter, since we will ignore it anyway.
            Err(Error::NotATrueType)
        }
    }

    /// Returns font's family name.
    ///
    /// Note that font can have multiple names. You can use [`names()`] to list them all.
    ///
    /// [`names()`]: #method.names
    pub fn family_name(&self) -> Option<String> {
        // Prefer Typographic Family name.

        let name = self.names()
            .find(|name| name.name_id == NameId::TypographicFamily && name.is_supported_encoding())
            .and_then(|name| name.to_string());

        match name {
            Some(name) => return Some(name),
            None => {}
        }

        self.names()
            .find(|name| name.name_id == NameId::Family && name.is_supported_encoding())
            .and_then(|name| name.to_string())
    }

    /// Returns font's PostScript name.
    ///
    /// Note that font can have multiple names. You can use [`names()`] to list them all.
    ///
    /// [`names()`]: #method.names
    pub fn post_script_name(&self) -> Option<String> {
        self.names()
            .find(|name| name.name_id == NameId::PostScriptName && name.is_supported_encoding())
            .and_then(|name| name.to_string())
    }
}