Skip to main content

read_fonts/
lib.rs

1//! Reading OpenType tables
2//!
3//! This crate provides memory safe zero-allocation parsing of font files.
4//! It is unopinionated, and attempts to provide raw access to the underlying
5//! font data as it is described in the [OpenType specification][spec].
6//!
7//! This crate is intended for use by other parts of a font stack, such as a
8//! shaping engine or a glyph rasterizer.
9//!
10//! In addition to raw data access, this crate may also provide reference
11//! implementations of algorithms for interpreting that data, where such an
12//! implementation is required for the data to be useful. For instance, we
13//! provide functions for [mapping codepoints to glyph identifiers][cmap-impl]
14//! using the `cmap` table, or for [decoding entries in the `name` table][NameString].
15//!
16//! For higher level/more ergonomic access to font data, you may want to look
17//! into using [`skrifa`] instead.
18//!
19//! ## Structure & codegen
20//!
21//! The root [`tables`] module contains a submodule for each supported
22//! [table][table-directory], and that submodule contains items for each table,
23//! record, flagset or enum described in the relevant portion of the spec.
24//!
25//! The majority of the code in the tables module is auto-generated. For more
26//! information on our use of codegen, see the [codegen tour].
27//!
28//! # Related projects
29//!
30//! - [`write-fonts`] is a companion crate for creating/modifying font files
31//! - [`skrifa`] provides access to glyph outlines and metadata (in the same vein
32//!   as [freetype])
33//!
34//! # Example
35//!
36//! ```no_run
37//! # let path_to_my_font_file = std::path::Path::new("");
38//! use read_fonts::{FontRef, TableProvider};
39//! let font_bytes = std::fs::read(path_to_my_font_file).unwrap();
40//! // Single fonts only. for font collections (.ttc) use FontRef::from_index
41//! let font = FontRef::new(&font_bytes).expect("failed to read font data");
42//! let head = font.head().expect("missing 'head' table");
43//! let maxp = font.maxp().expect("missing 'maxp' table");
44//!
45//! println!("font version {} containing {} glyphs", head.font_revision(), maxp.num_glyphs());
46//! ```
47//!
48//!
49//! [spec]: https://learn.microsoft.com/en-us/typography/opentype/spec/
50//! [codegen-tour]: https://github.com/googlefonts/fontations/blob/main/docs/codegen-tour.md
51//! [cmap-impl]: tables::cmap::Cmap::map_codepoint
52//! [`write-fonts`]: https://docs.rs/write-fonts/
53//! [`skrifa`]: https://docs.rs/skrifa/
54//! [freetype]: http://freetype.org
55//! [codegen tour]: https://github.com/googlefonts/fontations/blob/main/docs/codegen-tour.md
56//! [NameString]: tables::name::NameString
57//! [table-directory]: https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory
58
59#![cfg_attr(docsrs, feature(doc_cfg))]
60#![forbid(unsafe_code)]
61#![deny(rustdoc::broken_intra_doc_links)]
62#![cfg_attr(not(feature = "std"), no_std)]
63
64#[cfg(any(feature = "std", test))]
65#[macro_use]
66extern crate std;
67
68#[cfg(all(not(feature = "std"), not(test)))]
69#[macro_use]
70extern crate core as std;
71
72// Always depend on alloc. Perhaps make this a feature if someone really needs
73// heapless read-fonts.
74extern crate alloc;
75
76pub mod array;
77pub mod collections;
78mod font_data;
79pub mod model;
80mod offset;
81mod offset_array;
82pub mod ps;
83mod read;
84mod table_provider;
85mod table_ref;
86pub mod tables;
87#[cfg(feature = "experimental_traverse")]
88pub mod traversal;
89
90#[cfg(any(test, feature = "codegen_test"))]
91pub mod codegen_test;
92
93pub use font_data::FontData;
94pub use offset::{Offset, ResolveNullableOffset, ResolveOffset};
95pub use offset_array::{ArrayOfNullableOffsets, ArrayOfOffsets};
96pub use read::{ComputeSize, FontRead, FontReadWithArgs, ReadArgs, ReadError, VarSize};
97pub use table_provider::{TableProvider, TopLevelTable};
98pub use table_ref::MinByteRange;
99
100/// Public re-export of the font-types crate.
101pub extern crate font_types as types;
102
103/// All the types that may be referenced in auto-generated code.
104#[doc(hidden)]
105pub(crate) mod codegen_prelude {
106    pub use crate::array::{ComputedArray, VarLenArray};
107    pub use crate::font_data::{Cursor, FontData};
108    pub use crate::offset::{Offset, ResolveNullableOffset, ResolveOffset};
109    pub use crate::offset_array::{ArrayOfNullableOffsets, ArrayOfOffsets};
110    //pub(crate) use crate::read::sealed;
111    pub use crate::read::{
112        ComputeSize, Discriminant, FontRead, FontReadWithArgs, Format, ReadArgs, ReadError, VarSize,
113    };
114    pub use crate::table_provider::TopLevelTable;
115    pub use crate::table_ref::MinByteRange;
116    pub use std::ops::Range;
117
118    pub use types::*;
119
120    #[cfg(feature = "experimental_traverse")]
121    pub use crate::traversal::{self, Field, FieldType, RecordResolver, SomeRecord, SomeTable};
122
123    /// named transforms used in 'count', e.g
124    pub(crate) mod transforms {
125        pub fn to_usize<T: TryInto<usize>>(value: T) -> usize {
126            value.try_into().unwrap_or_default()
127        }
128
129        pub fn subtract<T: TryInto<usize>, U: TryInto<usize>>(lhs: T, rhs: U) -> usize {
130            lhs.try_into()
131                .unwrap_or_default()
132                .saturating_sub(rhs.try_into().unwrap_or_default())
133        }
134
135        pub fn add<T: TryInto<usize>, U: TryInto<usize>>(lhs: T, rhs: U) -> usize {
136            lhs.try_into()
137                .unwrap_or_default()
138                .saturating_add(rhs.try_into().unwrap_or_default())
139        }
140
141        #[allow(dead_code)]
142        pub fn bitmap_len<T: TryInto<usize>>(count: T) -> usize {
143            count.try_into().unwrap_or_default().div_ceil(8)
144        }
145
146        #[cfg(feature = "ift")]
147        pub fn max_value_bitmap_len<T: TryInto<usize>>(count: T) -> usize {
148            let count: usize = count.try_into().unwrap_or_default() + 1usize;
149            count.div_ceil(8)
150        }
151
152        pub fn add_multiply<T: TryInto<usize>, U: TryInto<usize>, V: TryInto<usize>>(
153            a: T,
154            b: U,
155            c: V,
156        ) -> usize {
157            a.try_into()
158                .unwrap_or_default()
159                .saturating_add(b.try_into().unwrap_or_default())
160                .saturating_mul(c.try_into().unwrap_or_default())
161        }
162
163        #[cfg(feature = "ift")]
164        pub fn multiply_add<T: TryInto<usize>, U: TryInto<usize>, V: TryInto<usize>>(
165            a: T,
166            b: U,
167            c: V,
168        ) -> usize {
169            a.try_into()
170                .unwrap_or_default()
171                .saturating_mul(b.try_into().unwrap_or_default())
172                .saturating_add(c.try_into().unwrap_or_default())
173        }
174
175        pub fn half<T: TryInto<usize>>(val: T) -> usize {
176            val.try_into().unwrap_or_default() / 2
177        }
178
179        pub fn subtract_add_two<T: TryInto<usize>, U: TryInto<usize>>(lhs: T, rhs: U) -> usize {
180            lhs.try_into()
181                .unwrap_or_default()
182                .saturating_sub(rhs.try_into().unwrap_or_default())
183                .saturating_add(2)
184        }
185    }
186
187    #[macro_export]
188    macro_rules! basic_table_impls {
189        (impl_the_methods) => {
190            /// Resolve the provided offset from the start of this table.
191            pub fn resolve_offset<O: Offset, R: FontRead<'a>>(
192                &self,
193                offset: O,
194            ) -> Result<R, ReadError> {
195                offset.resolve(self.data)
196            }
197
198            /// Return a reference to this table's raw data.
199            ///
200            /// We use this in the compile crate to resolve offsets.
201            pub fn offset_data(&self) -> FontData<'a> {
202                self.data
203            }
204
205            /// Return a reference to the table's 'Shape' struct.
206            ///
207            /// This is a low level implementation detail, but it can be useful in
208            /// some cases where you want to know things about a table's layout, such
209            /// as the byte offsets of specific fields.
210            #[deprecated(note = "just use the base type directly")]
211            pub fn shape(&self) -> &Self {
212                &self
213            }
214        };
215    }
216
217    pub(crate) use crate::basic_table_impls;
218}
219
220include!("../generated/font.rs");
221
222#[derive(Clone)]
223/// Reference to the content of a font or font collection file.
224pub enum FileRef<'a> {
225    /// A single font.
226    Font(FontRef<'a>),
227    /// A collection of fonts.
228    Collection(CollectionRef<'a>),
229}
230
231impl<'a> FileRef<'a> {
232    /// Creates a new reference to a file representing a font or font collection.
233    pub fn new(data: &'a [u8]) -> Result<Self, ReadError> {
234        Ok(if let Ok(collection) = CollectionRef::new(data) {
235            Self::Collection(collection)
236        } else {
237            Self::Font(FontRef::new(data)?)
238        })
239    }
240
241    /// Returns an iterator over the fonts contained in the file.
242    pub fn fonts(&self) -> impl Iterator<Item = Result<FontRef<'a>, ReadError>> + 'a + Clone {
243        let (iter_one, iter_two) = match self {
244            Self::Font(font) => (Some(Ok(font.clone())), None),
245            Self::Collection(collection) => (None, Some(collection.iter())),
246        };
247        iter_two.into_iter().flatten().chain(iter_one)
248    }
249}
250
251/// Reference to the content of a font collection file.
252#[derive(Clone)]
253pub struct CollectionRef<'a> {
254    data: FontData<'a>,
255    header: TTCHeader<'a>,
256}
257
258impl<'a> CollectionRef<'a> {
259    /// Creates a new reference to a font collection.
260    pub fn new(data: &'a [u8]) -> Result<Self, ReadError> {
261        let data = FontData::new(data);
262        let header = TTCHeader::read(data)?;
263        if header.ttc_tag() != TTC_HEADER_TAG {
264            Err(ReadError::InvalidTtc(header.ttc_tag()))
265        } else {
266            Ok(Self { data, header })
267        }
268    }
269
270    /// Returns the number of fonts in the collection.
271    pub fn len(&self) -> u32 {
272        self.header.table_directory_offsets().len() as u32
273    }
274
275    /// Returns true if the collection is empty.
276    pub fn is_empty(&self) -> bool {
277        self.len() == 0
278    }
279
280    /// Returns the font in the collection at the specified index.
281    pub fn get(&self, index: u32) -> Result<FontRef<'a>, ReadError> {
282        let offset = self
283            .header
284            .table_directory_offsets()
285            .get(index as usize)
286            .ok_or(ReadError::InvalidCollectionIndex(index))?
287            .get() as usize;
288        let table_dir_data = self.data.slice(offset..).ok_or(ReadError::OutOfBounds)?;
289        FontRef::with_table_directory(
290            self.data,
291            TableDirectory::read(table_dir_data)?,
292            Some(index),
293        )
294    }
295
296    /// Returns an iterator over the fonts in the collection.
297    pub fn iter(&self) -> impl Iterator<Item = Result<FontRef<'a>, ReadError>> + 'a + Clone {
298        let copy = self.clone();
299        (0..self.len()).map(move |ix| copy.get(ix))
300    }
301}
302
303impl TableDirectory<'_> {
304    fn is_sorted(&self) -> bool {
305        let mut last_tag = Tag::new(&[0u8; 4]);
306
307        for tag in self.table_records().iter().map(|rec| rec.tag()) {
308            if tag <= last_tag {
309                return false;
310            }
311
312            last_tag = tag;
313        }
314
315        true
316    }
317}
318
319/// Reference to an in-memory font.
320///
321/// This is a simple implementation of the [`TableProvider`] trait backed
322/// by a borrowed slice containing font data.
323#[derive(Clone)]
324pub struct FontRef<'a> {
325    data: FontData<'a>,
326    pub table_directory: TableDirectory<'a>,
327    /// The index of this font in a TrueType collection
328    ttc_index: u32,
329    /// Whether this font is a member of a TrueType collection.
330    ///
331    /// We use a bool rather than an Option to avoid bloating the struct
332    /// size.
333    in_ttc: bool,
334    // Whether the table directory is sorted and thus we can use binary search for
335    // finding table records. In principle, fonts are required to have a sorted
336    // table directory, but certain fonts don't seem to follow that requirement.
337    table_directory_sorted: bool,
338}
339
340impl<'a> FontRef<'a> {
341    /// Creates a new reference to an in-memory font backed by the given data.
342    ///
343    /// The data must be a single font (not a font collection) and must begin with a
344    /// [table directory] to be considered valid.
345    ///
346    /// To load a font from a font collection, use [`FontRef::from_index`] instead.
347    ///
348    /// [table directory]: https://github.com/googlefonts/fontations/pull/549
349    pub fn new(data: &'a [u8]) -> Result<Self, ReadError> {
350        let data = FontData::new(data);
351        Self::with_table_directory(data, TableDirectory::read(data)?, None)
352    }
353
354    /// Creates a new reference to an in-memory font at the specified index
355    /// backed by the given data.
356    ///
357    /// The data slice must begin with either a
358    /// [table directory](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory)
359    /// or a [ttc header](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header)
360    /// to be considered valid.
361    ///
362    /// In other words, this accepts either font collection (ttc) or single
363    /// font (ttf/otf) files. If a single font file is provided, the index
364    /// parameter must be 0.
365    pub fn from_index(data: &'a [u8], index: u32) -> Result<Self, ReadError> {
366        let file = FileRef::new(data)?;
367        match file {
368            FileRef::Font(font) => {
369                if index == 0 {
370                    Ok(font)
371                } else {
372                    Err(ReadError::InvalidCollectionIndex(index))
373                }
374            }
375            FileRef::Collection(collection) => collection.get(index),
376        }
377    }
378
379    /// Returns the underlying font data.
380    ///
381    /// This is the base from which tables are loaded, meaning that for
382    /// TrueType collection files, this will be the entire font file data.
383    pub fn data(&self) -> FontData<'a> {
384        self.data
385    }
386
387    /// If the font is in a TrueType collection (ttc) file, returns the index
388    /// of the font in that collection.
389    pub fn ttc_index(&self) -> Option<u32> {
390        self.in_ttc.then_some(self.ttc_index)
391    }
392
393    /// Returns the associated table directory.
394    pub fn table_directory(&self) -> &TableDirectory<'a> {
395        &self.table_directory
396    }
397
398    /// Returns the data for the table with the specified tag, if present.
399    pub fn table_data(&self, tag: Tag) -> Option<FontData<'a>> {
400        let entry = if self.table_directory_sorted {
401            self.table_directory
402                .table_records()
403                .binary_search_by(|rec| rec.tag.get().cmp(&tag))
404                .ok()
405        } else {
406            self.table_directory
407                .table_records()
408                .iter()
409                .position(|rec| rec.tag.get().eq(&tag))
410        };
411
412        entry
413            .and_then(|idx| self.table_directory.table_records().get(idx))
414            .and_then(|record| {
415                let start = Offset32::new(record.offset()).non_null()?;
416                let len = record.length() as usize;
417                self.data.slice(start..start.checked_add(len)?)
418            })
419    }
420
421    /// Returns an iterator over all of the available fonts in
422    /// the given font data.
423    pub fn fonts(
424        data: &'a [u8],
425    ) -> impl Iterator<Item = Result<FontRef<'a>, ReadError>> + 'a + Clone {
426        let count = match FileRef::new(data) {
427            Ok(FileRef::Font(_)) => 1,
428            Ok(FileRef::Collection(ttc)) => ttc.len(),
429            _ => 0,
430        };
431        (0..count).map(|idx| FontRef::from_index(data, idx))
432    }
433
434    fn with_table_directory(
435        data: FontData<'a>,
436        table_directory: TableDirectory<'a>,
437        ttc_index: Option<u32>,
438    ) -> Result<Self, ReadError> {
439        if [TT_SFNT_VERSION, CFF_SFNT_VERSION, TRUE_SFNT_VERSION]
440            .contains(&table_directory.sfnt_version())
441        {
442            let table_directory_sorted = table_directory.is_sorted();
443
444            Ok(FontRef {
445                data,
446                table_directory,
447                ttc_index: ttc_index.unwrap_or_default(),
448                in_ttc: ttc_index.is_some(),
449                table_directory_sorted,
450            })
451        } else {
452            Err(ReadError::InvalidSfnt(table_directory.sfnt_version()))
453        }
454    }
455}
456
457impl<'a> TableProvider<'a> for FontRef<'a> {
458    fn data_for_tag(&self, tag: Tag) -> Option<FontData<'a>> {
459        self.table_data(tag)
460    }
461}
462
463#[cfg(test)]
464mod tests {
465    use font_test_data::{be_buffer, bebuffer::BeBuffer, ttc::TTC, AHEM};
466    use types::{Tag, TT_SFNT_VERSION};
467
468    use crate::{FileRef, FontRef};
469
470    #[test]
471    fn file_ref_non_collection() {
472        assert!(matches!(FileRef::new(AHEM), Ok(FileRef::Font(_))));
473    }
474
475    #[test]
476    fn file_ref_collection() {
477        let Ok(FileRef::Collection(collection)) = FileRef::new(TTC) else {
478            panic!("Expected a collection");
479        };
480        assert_eq!(2, collection.len());
481        assert!(!collection.is_empty());
482    }
483
484    #[test]
485    fn font_ref_fonts_iter() {
486        assert_eq!(FontRef::fonts(AHEM).count(), 1);
487        assert_eq!(FontRef::fonts(TTC).count(), 2);
488        assert_eq!(FontRef::fonts(b"NOT_A_FONT").count(), 0);
489    }
490
491    #[test]
492    fn ttc_index() {
493        for (idx, font) in FontRef::fonts(TTC).map(|font| font.unwrap()).enumerate() {
494            assert_eq!(font.ttc_index(), Some(idx as u32));
495        }
496        assert!(FontRef::new(AHEM).unwrap().ttc_index().is_none());
497    }
498
499    #[test]
500    fn unsorted_table_directory() {
501        let cff2_data = font_test_data::cff2::EXAMPLE;
502        let post_data = font_test_data::post::SIMPLE;
503        let gdef_data = [
504            font_test_data::gdef::GDEF_HEADER,
505            font_test_data::gdef::GLYPHCLASSDEF_TABLE,
506        ]
507        .concat();
508        let gpos_data = font_test_data::gpos::SINGLEPOSFORMAT1;
509
510        let font_data = be_buffer! {
511            TT_SFNT_VERSION,
512            4u16,    // num tables
513            64u16,   // search range
514            2u16,    // entry selector
515            0u16,    // range shift
516
517            (Tag::new(b"post")),
518            0u32,    // checksum
519            76u32,   // offset
520            (post_data.len() as u32),
521
522            (Tag::new(b"GPOS")),
523            0u32,    // checksum
524            108u32,  // offset
525            (gpos_data.len() as u32),
526
527            (Tag::new(b"GDEF")),
528            0u32,    // checksum
529            128u32,  // offset
530            (gdef_data.len() as u32),
531
532            (Tag::new(b"CFF2")),
533            0u32,    // checksum
534            160u32,  // offset
535            (cff2_data.len() as u32)
536        };
537
538        let mut full_font = font_data.to_vec();
539
540        full_font.extend_from_slice(post_data);
541        full_font.extend_from_slice(gpos_data);
542        full_font.extend_from_slice(&gdef_data);
543        full_font.extend_from_slice(cff2_data);
544
545        let font = FontRef::new(&full_font).unwrap();
546
547        assert!(!font.table_directory_sorted);
548
549        assert!(font.table_data(Tag::new(b"CFF2")).is_some());
550        assert!(font.table_data(Tag::new(b"GDEF")).is_some());
551        assert!(font.table_data(Tag::new(b"GPOS")).is_some());
552        assert!(font.table_data(Tag::new(b"post")).is_some());
553    }
554}