read-fonts 0.46.0

Reading OpenType font files.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Names of fonts and glyphs.

mod glyph;

pub(crate) use glyph::{glyph_name, glyph_names, GlyphNameIndex};
#[doc(inline)]
pub use glyph::{GlyphName, GlyphNameSource};

use crate::{
    ps::{
        cff::{CffFontRef, Metadata as CffMetadata},
        type1::Type1Font,
    },
    tables::name::{language_id_to_bcp47, Encoding, Name as SfntName, NameRecord, NameString},
    FontData, FontRead, TableProvider,
};
use core::cmp::Ordering;
use core::fmt;

#[doc(inline)]
pub use crate::types::NameId;

/// Access to names in a font.
///
/// SFNT names come from the `name` table. For Type 1 fonts, the available
/// notice, family, weight, full, version, and PostScript names are exposed as
/// name IDs 0, 1, 2, 4, 5, and 6, respectively. A Type 1 `Notice` may contain
/// either copyright or trademark text; it is exposed as ID 0. The axes of a
/// multiple master font are named by IDs from 256, one per axis, which
/// [`Axis::name`](super::variation::Axis::name) returns. Standalone CFF
/// fonts expose family, weight, full, and PostScript names as IDs 1, 2, 4,
/// and 6, and copyright, version, and notice as IDs 0, 5, and 7.
#[derive(Clone, Copy)]
pub struct Names<'a> {
    source: Source<'a>,
}

#[derive(Clone, Copy)]
enum Source<'a> {
    /// The data of the `name` table, if the font has one.
    Sfnt(Option<FontData<'a>>),
    Type1(&'a Type1Font),
    StandaloneCff(Option<CffMetadata<'a>>),
}

impl<'a> Names<'a> {
    /// Creates a name facade from an SFNT table provider.
    pub fn from_sfnt(tables: &impl TableProvider<'a>) -> Self {
        Self {
            source: Source::Sfnt(tables.name().ok().map(|name| name.offset_data())),
        }
    }

    /// Creates a name facade from a Type 1 font.
    pub fn from_type1(font: &'a Type1Font) -> Self {
        Self {
            source: Source::Type1(font),
        }
    }

    /// Creates a name facade from a standalone CFF font.
    pub fn from_cff(font: &CffFontRef<'a>) -> Self {
        Self {
            source: Source::StandaloneCff(font.metadata()),
        }
    }

    /// Returns the name entry for the given identifier.
    ///
    /// Returns `None` if the font has no name with this identifier.
    pub fn get(&self, id: NameId) -> Option<Name<'a>> {
        // Records are sorted by platform, encoding and language before
        // identifier, so those for one identifier are spread out. The first
        // is kept so the strings are found without scanning the records
        // before it again.
        let start = match &self.source {
            Source::Sfnt(data) => sfnt_name(*data)?
                .name_record()
                .iter()
                .position(|record| record.name_id() == id)?,
            Source::Type1(font) => type1_name(font, id).map(|_| 0)?,
            Source::StandaloneCff(meta) => standalone_cff_name(meta.as_ref()?, id).map(|_| 0)?,
        };
        Some(Name {
            source: self.source,
            id,
            start,
        })
    }
}

/// Reads the `name` table from its data.
fn sfnt_name(data: Option<FontData<'_>>) -> Option<SfntName<'_>> {
    SfntName::read(data?).ok()
}

/// Returns the Type 1 string for a name identifier.
fn type1_name(font: &Type1Font, id: NameId) -> Option<&str> {
    match id {
        NameId::COPYRIGHT_NOTICE => font.notice(),
        NameId::FAMILY_NAME => font.family_name(),
        NameId::SUBFAMILY_NAME => font.weight(),
        NameId::FULL_NAME => font.full_name(),
        NameId::VERSION_STRING => font.version(),
        NameId::POSTSCRIPT_NAME => font.name(),
        id => {
            let index = id.to_u16().checked_sub(TYPE1_AXIS_NAMES)?;
            font.multiple_master()?.axes().get(index as usize)?.name()
        }
    }
}

/// The name ID of the first axis of a Type 1 multiple master font. Each
/// later axis takes the next.
pub(crate) const TYPE1_AXIS_NAMES: u16 = 256;

/// Returns the standalone CFF string for a name identifier.
fn standalone_cff_name<'a>(meta: &CffMetadata<'a>, id: NameId) -> Option<&'a str> {
    match id {
        NameId::COPYRIGHT_NOTICE => meta.copyright(),
        NameId::FAMILY_NAME => meta.family_name(),
        NameId::SUBFAMILY_NAME => meta.weight(),
        NameId::FULL_NAME => meta.full_name(),
        NameId::VERSION_STRING => meta.version(),
        NameId::POSTSCRIPT_NAME => meta.name(),
        NameId::TRADEMARK => meta.notice(),
        _ => None,
    }
}

/// The localized strings for one name identifier.
#[derive(Clone, Copy)]
pub struct Name<'a> {
    source: Source<'a>,
    id: NameId,
    /// The index of the first `name` record with this identifier.
    start: usize,
}

impl<'a> Name<'a> {
    /// Returns the identifier for this name.
    pub fn id(&self) -> NameId {
        self.id
    }

    /// Returns `en-US` or `en` if present, falling back to the first entry.
    ///
    /// This prefers `en-US`, then `en`, then a name without a language tag,
    /// and finally the first name in table order. Records with unsupported
    /// encodings are skipped. Returns `None` if no decodable name with this
    /// identifier is available.
    pub fn english_or_first(&self) -> Option<Localized<'a>> {
        let mut best_rank = -1;
        let mut best = None;
        for (index, name) in self.iter().enumerate() {
            let rank = match (index, name.language().as_ref()) {
                (_, Some(language)) if *language == "en-US" => return Some(name),
                (_, Some(language)) if *language == "en" => 2,
                (_, None) => 1,
                (0, _) => 0,
                _ => continue,
            };
            if rank > best_rank {
                best_rank = rank;
                best = Some(name);
            }
        }
        best
    }

    /// Iterates over the localized strings for this identifier, in table order.
    ///
    /// Records with unsupported encodings are skipped. Malformed text in a
    /// supported encoding may contain replacement characters.
    pub fn iter(&self) -> impl Iterator<Item = Localized<'a>> + 'a {
        let (id, start) = (self.id, self.start);
        let sfnt = match self.source {
            Source::Sfnt(data) => sfnt_name(data),
            _ => None,
        };
        let sfnt = sfnt.into_iter().flat_map(move |table| {
            table
                .name_record()
                .get(start..)
                .unwrap_or_default()
                .iter()
                .filter(move |record| record.name_id() == id)
                .filter_map(move |record| Localized::from_record(&table, record))
        });
        let type1 = match &self.source {
            Source::Type1(font) => type1_name(font, id),
            _ => None,
        }
        .map(|name| Localized {
            name: Encoded::from_str(name),
            language: None,
            sfnt_record: None,
        });
        let standalone_cff = match &self.source {
            Source::StandaloneCff(Some(meta)) => standalone_cff_name(meta, id),
            _ => None,
        }
        .map(|name| Localized {
            name: Encoded::from_str(name),
            language: None,
            sfnt_record: None,
        });
        sfnt.chain(type1).chain(standalone_cff)
    }
}

/// A font name and its optional BCP 47 language tag.
///
/// The language is `None` when the source supplies no recognized language or
/// when the name comes from a Type 1 or standalone CFF font.
#[derive(Clone, Copy, Debug)]
pub struct Localized<'a> {
    name: Encoded<'a>,
    language: Option<Encoded<'a>>,
    sfnt_record: Option<NameRecord>,
}

impl<'a> Localized<'a> {
    /// Returns the encoded name text.
    pub fn name(self) -> Encoded<'a> {
        self.name
    }

    /// Returns the encoded language tag, if available.
    pub fn language(self) -> Option<Encoded<'a>> {
        self.language
    }

    /// Returns the source `name` record for an SFNT name, or `None` for Type 1 or CFF.
    pub fn sfnt_record(self) -> Option<NameRecord> {
        self.sfnt_record
    }

    fn from_record(table: &SfntName<'a>, record: &NameRecord) -> Option<Self> {
        if Encoding::new(record.platform_id(), record.encoding_id()) == Encoding::Unknown {
            return None;
        }
        let name = Encoded::from_name_string(record.string(table.string_data()).ok()?);
        let language = if table.version() == 1 && record.language_id() >= 0x8000 {
            let index = (record.language_id() - 0x8000) as usize;
            table
                .lang_tag_record()
                .and_then(|tags| tags.get(index))
                .and_then(|tag| tag.lang_tag(table.string_data()).ok())
                .filter(|tag| tag.chars().all(|ch| ch.is_ascii()))
                .map(Encoded::from_name_string)
        } else {
            language_id_to_bcp47(record.platform_id(), record.language_id()).map(Encoded::from_str)
        };
        Some(Self {
            name,
            language,
            sfnt_record: Some(*record),
        })
    }
}

/// A name or language tag in its source encoding.
///
/// SFNT UTF-16BE and Mac Roman strings are decoded when displayed or compared,
/// without allocating. Type 1 and standalone CFF names and built-in language
/// tags borrow UTF-8 text directly.
#[derive(Clone, Copy)]
pub struct Encoded<'a>(EncodedRepr<'a>);

#[derive(Clone, Copy)]
enum EncodedRepr<'a> {
    Sfnt(NameString<'a>),
    Utf8(&'a str),
}

impl<'a> Encoded<'a> {
    fn from_name_string(name: NameString<'a>) -> Self {
        Self(EncodedRepr::Sfnt(name))
    }

    fn from_str(name: &'a str) -> Self {
        Self(EncodedRepr::Utf8(name))
    }

    /// Iterates over decoded Unicode characters.
    pub fn chars(&self) -> impl Iterator<Item = char> + 'a {
        let sfnt = match self.0 {
            EncodedRepr::Sfnt(name) => Some(name),
            _ => None,
        };
        let utf8 = match self.0 {
            EncodedRepr::Utf8(name) => Some(name),
            _ => None,
        };
        sfnt.into_iter()
            .flat_map(|name| name.chars())
            .chain(utf8.into_iter().flat_map(str::chars))
    }

    /// Returns whether the decoded string begins with `prefix`.
    pub fn starts_with(&self, prefix: &str) -> bool {
        let mut chars = self.chars();
        prefix.chars().all(|ch| chars.next() == Some(ch))
    }
}

impl fmt::Display for Encoded<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for ch in self.chars() {
            ch.fmt(f)?;
        }
        Ok(())
    }
}

impl fmt::Debug for Encoded<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "\"{self}\"")
    }
}

impl<'a, 'b> PartialEq<Encoded<'b>> for Encoded<'a> {
    fn eq(&self, other: &Encoded<'b>) -> bool {
        self.chars().eq(other.chars())
    }
}

impl Eq for Encoded<'_> {}

impl<'a, 'b> PartialOrd<Encoded<'b>> for Encoded<'a> {
    fn partial_cmp(&self, other: &Encoded<'b>) -> Option<Ordering> {
        Some(self.chars().cmp(other.chars()))
    }
}

impl PartialEq<&str> for Encoded<'_> {
    fn eq(&self, other: &&str) -> bool {
        self.chars().eq(other.chars())
    }
}

impl PartialOrd<&str> for Encoded<'_> {
    fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
        Some(self.chars().cmp(other.chars()))
    }
}

impl PartialEq<Encoded<'_>> for &str {
    fn eq(&self, other: &Encoded<'_>) -> bool {
        other == self
    }
}

impl PartialOrd<Encoded<'_>> for &str {
    fn partial_cmp(&self, other: &Encoded<'_>) -> Option<Ordering> {
        Some(self.chars().cmp(other.chars()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{model::Font, types::Tag, FontData};
    use alloc::vec::Vec;

    // The iterators borrow the font, not the name or string they came from,
    // so they outlive both.
    #[test]
    fn iterators_outlive_their_names() {
        let font = Font::new(font_test_data::NAMES_ONLY, 0).unwrap();
        let localized = font.names().get(NameId::FAMILY_NAME).unwrap().iter();
        let chars = font
            .names()
            .get(NameId::FAMILY_NAME)
            .unwrap()
            .english_or_first()
            .unwrap()
            .name()
            .chars();
        assert!(localized.count() > 0);
        assert!(chars.count() > 0);
    }

    struct NameProvider<'a>(&'a [u8]);

    impl<'a> TableProvider<'a> for NameProvider<'a> {
        fn data_for_tag(&self, tag: Tag) -> Option<FontData<'a>> {
            (tag == Tag::new(b"name")).then(|| FontData::new(self.0))
        }
    }

    fn name_table(records: &[(u16, u16, u16, NameId, &str)], tags: &[&str]) -> Vec<u8> {
        let mut bytes = Vec::new();
        let version = if tags.is_empty() { 0_u16 } else { 1 };
        let storage_offset = 6
            + records.len() * 12
            + if tags.is_empty() {
                0
            } else {
                2 + tags.len() * 4
            };
        bytes.extend_from_slice(&version.to_be_bytes());
        bytes.extend_from_slice(&(records.len() as u16).to_be_bytes());
        bytes.extend_from_slice(&(storage_offset as u16).to_be_bytes());
        let mut storage = Vec::new();
        for &(platform, encoding, language, id, text) in records {
            let encoded = if platform == 1 {
                text.as_bytes().to_vec()
            } else {
                text.encode_utf16().flat_map(u16::to_be_bytes).collect()
            };
            for value in [
                platform,
                encoding,
                language,
                id.to_u16(),
                encoded.len() as u16,
                storage.len() as u16,
            ] {
                bytes.extend_from_slice(&value.to_be_bytes());
            }
            storage.extend_from_slice(&encoded);
        }
        if !tags.is_empty() {
            bytes.extend_from_slice(&(tags.len() as u16).to_be_bytes());
            for tag in tags {
                let encoded: Vec<u8> = tag.encode_utf16().flat_map(u16::to_be_bytes).collect();
                bytes.extend_from_slice(&(encoded.len() as u16).to_be_bytes());
                bytes.extend_from_slice(&(storage.len() as u16).to_be_bytes());
                storage.extend_from_slice(&encoded);
            }
        }
        bytes.extend_from_slice(&storage);
        bytes
    }

    #[test]
    fn selects_english_by_skrifa_priority() {
        let data = name_table(
            &[
                (3, 1, 0x040c, NameId::FAMILY_NAME, "Français"),
                (0, 4, 0, NameId::FAMILY_NAME, "Bare"),
                (1, 0, 0, NameId::FAMILY_NAME, "English"),
                (3, 1, 0x0409, NameId::FAMILY_NAME, "American English"),
            ],
            &[],
        );
        let name = Names::from_sfnt(&NameProvider(&data))
            .get(NameId::FAMILY_NAME)
            .unwrap();
        let entries: Vec<_> = name.iter().collect();
        assert_eq!(entries.len(), 4);
        assert_eq!(entries[0].name(), "Français");
        assert_eq!(entries[0].language().unwrap(), "fr-FR");
        let record = entries[0].sfnt_record().unwrap();
        assert_eq!(record.name_id(), NameId::FAMILY_NAME);
        assert_eq!(record.platform_id(), 3);
        assert_eq!(record.encoding_id(), 1);
        assert_eq!(record.language_id(), 0x040c);
        assert!(entries[1].language().is_none());
        assert_eq!(entries[2].language().unwrap(), "en");
        let best = name.english_or_first().unwrap();
        assert_eq!(best.name(), "American English");
        assert_eq!(best.language().unwrap(), "en-US");
        assert_eq!(best.name().to_string(), "American English");
    }

    // A missing identifier has no entry, and an entry starting partway
    // through the records still finds every string for its identifier.
    #[test]
    fn get_finds_an_identifier_wherever_its_records_start() {
        let data = name_table(
            &[
                (1, 0, 0, NameId::FAMILY_NAME, "Mac Family"),
                (1, 0, 0, NameId::FULL_NAME, "Mac Full"),
                (3, 1, 0x0409, NameId::FAMILY_NAME, "Windows Family"),
                (3, 1, 0x0409, NameId::FULL_NAME, "Windows Full"),
            ],
            &[],
        );
        let names = Names::from_sfnt(&NameProvider(&data));
        assert!(names.get(NameId::DESCRIPTION).is_none());
        let full: Vec<_> = names
            .get(NameId::FULL_NAME)
            .unwrap()
            .iter()
            .map(|name| name.name().to_string())
            .collect();
        assert_eq!(full, ["Mac Full", "Windows Full"]);
        assert_eq!(names.get(NameId::FAMILY_NAME).unwrap().iter().count(), 2);
    }

    #[test]
    fn skips_names_with_unsupported_encodings() {
        let data = name_table(
            &[
                (3, 2, 0x0409, NameId::FAMILY_NAME, "Unsupported English"),
                (3, 1, 0x040c, NameId::FAMILY_NAME, "Français"),
            ],
            &[],
        );
        let name = Names::from_sfnt(&NameProvider(&data))
            .get(NameId::FAMILY_NAME)
            .unwrap();
        let entries: Vec<_> = name.iter().collect();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name(), "Français");
        assert_eq!(name.english_or_first().unwrap().name(), "Français");
    }

    #[test]
    fn reads_version_one_language_tags() {
        let data = name_table(
            &[(3, 1, 0x8000, NameId::FULL_NAME, "Tagged Name")],
            &["en-GB"],
        );
        let name = Names::from_sfnt(&NameProvider(&data))
            .get(NameId::FULL_NAME)
            .unwrap();
        let value = name.english_or_first().unwrap();
        assert_eq!(value.name(), "Tagged Name");
        assert_eq!(value.language().unwrap(), "en-GB");

        let invalid = name_table(&[(3, 1, 0x8000, NameId::FULL_NAME, "Tagged Name")], &["é"]);
        let value = Names::from_sfnt(&NameProvider(&invalid))
            .get(NameId::FULL_NAME)
            .unwrap()
            .english_or_first()
            .unwrap();
        assert_eq!(value.name(), "Tagged Name");
        assert!(value.language().is_none());

        let long_tag = "en-Latn-US-x-private-custom-segment";
        assert!(long_tag.len() > 30);
        let data = name_table(
            &[(3, 1, 0x8000, NameId::FULL_NAME, "Tagged Name")],
            &[long_tag],
        );
        let value = Names::from_sfnt(&NameProvider(&data))
            .get(NameId::FULL_NAME)
            .unwrap()
            .english_or_first()
            .unwrap();
        assert_eq!(value.language().unwrap(), long_tag);
    }

    #[test]
    fn exposes_type1_names_by_sfnt_id() {
        let data = font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB;
        let font = Font::new(data, 0).unwrap();
        for (id, expected) in [
            (
                NameId::COPYRIGHT_NOTICE,
                "Copyright 2015-2021 Google LLC. All Rights Reserved.",
            ),
            (NameId::FAMILY_NAME, "Noto Serif"),
            (NameId::SUBFAMILY_NAME, "Book"),
            (NameId::FULL_NAME, "Noto Serif Regular"),
            (
                NameId::VERSION_STRING,
                "2.007; ttfautohint (v1.8) -l 8 -r 50 -G 200 -x 14 -D latn -f none -a qsq -X \"\"",
            ),
            (NameId::POSTSCRIPT_NAME, "NotoSerif-Regular"),
        ] {
            let name = font.names().get(id).unwrap();
            let value = name.english_or_first().unwrap();
            assert_eq!(value.name(), expected);
            assert!(value.language().is_none());
            assert!(value.sfnt_record().is_none());
            assert_eq!(name.iter().count(), 1);
        }
        assert!(font.names().get(NameId::DESCRIPTION).is_none());

        let type1 = Type1Font::new(data).unwrap();
        assert_eq!(
            Names::from_type1(&type1)
                .get(NameId::POSTSCRIPT_NAME)
                .unwrap()
                .english_or_first()
                .unwrap()
                .name(),
            "NotoSerif-Regular"
        );
    }

    #[test]
    fn encoded_strings_compare_and_match_prefixes_without_decoding_to_string() {
        let data = name_table(
            &[
                (1, 0, 0, NameId::FAMILY_NAME, "English"),
                (3, 1, 0x0409, NameId::FAMILY_NAME, "English"),
                (3, 1, 0x040c, NameId::FAMILY_NAME, "Français"),
            ],
            &[],
        );
        let names = Names::from_sfnt(&NameProvider(&data));
        let entries: Vec<_> = names.get(NameId::FAMILY_NAME).unwrap().iter().collect();
        let mac = entries[0].name();
        let utf16 = entries[1].name();
        let accented = entries[2].name();
        assert_eq!(mac, utf16);
        assert_eq!(mac, "English");
        assert!(mac <= "English");
        assert!("English" >= mac);
        assert!(mac < accented);
        assert!(accented > "English");
        assert!(accented.starts_with("Fran"));
        assert!(accented.starts_with("Français"));
        assert!(accented.starts_with(""));
        assert!(!accented.starts_with("France"));
        assert!(!mac.starts_with("English!"));

        let type1 = Encoded::from_str("Français");
        assert_eq!(accented, type1);
        assert!(type1.starts_with("Fran"));
    }
}