ttf_parser/tables/
head.rs

1//! A [Font Header Table](
2//! https://docs.microsoft.com/en-us/typography/opentype/spec/head) implementation.
3
4use crate::Rect;
5use crate::parser::{Stream, Fixed};
6
7/// An index format used by the [Index to Location Table](
8/// https://docs.microsoft.com/en-us/typography/opentype/spec/loca).
9#[allow(missing_docs)]
10#[derive(Clone, Copy, PartialEq, Debug)]
11pub enum IndexToLocationFormat {
12    Short,
13    Long,
14}
15
16
17/// A [Font Header Table](https://docs.microsoft.com/en-us/typography/opentype/spec/head).
18#[derive(Clone, Copy, Debug)]
19pub struct Table {
20    /// Units per EM.
21    ///
22    /// Guarantee to be in a 16..=16384 range.
23    pub units_per_em: u16,
24    /// A bounding box that large enough to enclose any glyph from the face.
25    pub global_bbox: Rect,
26    /// An index format used by the [Index to Location Table](
27    /// https://docs.microsoft.com/en-us/typography/opentype/spec/loca).
28    pub index_to_location_format: IndexToLocationFormat,
29}
30
31impl Table {
32    /// Parses a table from raw data.
33    pub fn parse(data: &[u8]) -> Option<Self> {
34        if data.len() != 54 {
35            return None
36        }
37
38        let mut s = Stream::new(data);
39        s.skip::<u32>(); // version
40        s.skip::<Fixed>(); // font revision
41        s.skip::<u32>(); // checksum adjustment
42        s.skip::<u32>(); // magic number
43        s.skip::<u16>(); // flags
44        let units_per_em = s.read::<u16>()?;
45        s.skip::<u64>(); // created time
46        s.skip::<u64>(); // modified time
47        let x_min = s.read::<i16>()?;
48        let y_min = s.read::<i16>()?;
49        let x_max = s.read::<i16>()?;
50        let y_max = s.read::<i16>()?;
51        s.skip::<u16>(); // mac style
52        s.skip::<u16>(); // lowest PPEM
53        s.skip::<i16>(); // font direction hint
54        let index_to_location_format = s.read::<u16>()?;
55
56        if !(units_per_em >= 16 && units_per_em <= 16384) {
57            return None;
58        }
59
60        let index_to_location_format = match index_to_location_format {
61            0 => IndexToLocationFormat::Short,
62            1 => IndexToLocationFormat::Long,
63            _ => return None,
64        };
65
66        Some(Table {
67            units_per_em,
68            global_bbox: Rect { x_min, y_min, x_max, y_max },
69            index_to_location_format,
70        })
71    }
72}