ttf_parser/tables/
head.rs

1// https://docs.microsoft.com/en-us/typography/opentype/spec/head
2
3use crate::Rect;
4use crate::parser::Stream;
5
6
7const TABLE_SIZE: usize = 54;
8const UNITS_PER_EM_OFFSET: usize = 18;
9const BBOX_OFFSET: usize = 36;
10const INDEX_TO_LOC_FORMAT_OFFSET: usize = 50;
11
12
13#[derive(Clone, Copy, PartialEq, Debug)]
14pub(crate) enum IndexToLocationFormat {
15    Short,
16    Long,
17}
18
19#[inline]
20pub fn parse(data: &[u8]) -> Option<&[u8]> {
21    if data.len() == TABLE_SIZE {
22        Some(data)
23    } else {
24        None
25    }
26}
27
28#[inline]
29pub fn units_per_em(data: &[u8]) -> Option<u16> {
30    let num: u16 = Stream::read_at(data, UNITS_PER_EM_OFFSET)?;
31    if num >= 16 && num <= 16384 {
32        Some(num)
33    } else {
34        None
35    }
36}
37
38#[inline]
39pub fn global_bbox(data: &[u8]) -> Option<Rect> {
40    let mut s = Stream::new_at(data, BBOX_OFFSET)?;
41    Some(Rect {
42        x_min: s.read::<i16>()?,
43        y_min: s.read::<i16>()?,
44        x_max: s.read::<i16>()?,
45        y_max: s.read::<i16>()?,
46    })
47}
48
49#[inline]
50pub(crate) fn index_to_loc_format(data: &[u8]) -> Option<IndexToLocationFormat> {
51    let format: i16 = Stream::read_at(data, INDEX_TO_LOC_FORMAT_OFFSET)?;
52    match format {
53        0 => Some(IndexToLocationFormat::Short),
54        1 => Some(IndexToLocationFormat::Long),
55        _ => None,
56    }
57}