ttf_parser/tables/
ankr.rs

1//! An [Anchor Point Table](
2//! https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ankr.html) implementation.
3
4use core::num::NonZeroU16;
5
6use crate::GlyphId;
7use crate::parser::{Stream, FromData, LazyArray32};
8use crate::aat;
9
10/// An anchor point.
11#[allow(missing_docs)]
12#[derive(Clone, Copy, PartialEq, Default, Debug)]
13pub struct Point {
14    pub x: i16,
15    pub y: i16,
16}
17
18impl FromData for Point {
19    const SIZE: usize = 4;
20
21    #[inline]
22    fn parse(data: &[u8]) -> Option<Self> {
23        let mut s = Stream::new(data);
24        Some(Point {
25            x: s.read::<i16>()?,
26            y: s.read::<i16>()?,
27        })
28    }
29}
30
31
32/// An [Anchor Point Table](
33/// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ankr.html).
34#[derive(Clone)]
35pub struct Table<'a> {
36    lookup: aat::Lookup<'a>,
37    // Ideally, Glyphs Data can be represented as an array,
38    // but Apple's spec doesn't specify that Glyphs Data members have padding or not.
39    // Meaning we cannot simply iterate over them.
40    glyphs_data: &'a [u8],
41}
42
43impl core::fmt::Debug for Table<'_> {
44    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
45        write!(f, "Table {{ ... }}")
46    }
47}
48
49impl<'a> Table<'a> {
50    /// Parses a table from raw data.
51    ///
52    /// `number_of_glyphs` is from the `maxp` table.
53    pub fn parse(number_of_glyphs: NonZeroU16, data: &'a [u8]) -> Option<Self> {
54        let mut s = Stream::new(data);
55
56        let version = s.read::<u16>()?;
57        if version != 0 {
58            return None;
59        }
60
61        s.skip::<u16>(); // reserved
62        // TODO: we should probably check that offset is larger than the header size (8)
63        let lookup_table = s.read_at_offset32(data)?;
64        let glyphs_data = s.read_at_offset32(data)?;
65
66        Some(Table {
67            lookup: aat::Lookup::parse(number_of_glyphs, lookup_table)?,
68            glyphs_data,
69        })
70    }
71
72    /// Returns a list of anchor points for the specified glyph.
73    pub fn points(&self, glyph_id: GlyphId) -> Option<LazyArray32<'a, Point>> {
74        let offset = self.lookup.value(glyph_id)?;
75
76        let mut s = Stream::new_at(self.glyphs_data, usize::from(offset))?;
77        let number_of_points = s.read::<u32>()?;
78        s.read_array32::<Point>(number_of_points)
79    }
80}