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