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