ttf_parser/tables/
ankr.rs1use core::num::NonZeroU16;
5
6use crate::aat;
7use crate::parser::{FromData, LazyArray32, Offset, Offset32, Stream};
8use crate::GlyphId;
9
10#[allow(missing_docs)]
12#[derive(Clone, Copy, PartialEq, Eq, 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#[derive(Clone)]
34pub struct Table<'a> {
35 lookup: aat::Lookup<'a>,
36 glyphs_data: &'a [u8],
40}
41
42impl core::fmt::Debug for Table<'_> {
43 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
44 write!(f, "Table {{ ... }}")
45 }
46}
47
48impl<'a> Table<'a> {
49 pub fn parse(number_of_glyphs: NonZeroU16, data: &'a [u8]) -> Option<Self> {
53 let mut s = Stream::new(data);
54
55 let version = s.read::<u16>()?;
56 if version != 0 {
57 return None;
58 }
59
60 s.skip::<u16>(); 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 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}
81
82trait StreamExt<'a> {
83 fn read_at_offset32(&mut self, data: &'a [u8]) -> Option<&'a [u8]>;
84}
85
86impl<'a> StreamExt<'a> for Stream<'a> {
87 fn read_at_offset32(&mut self, data: &'a [u8]) -> Option<&'a [u8]> {
88 let offset = self.read::<Offset32>()?.to_usize();
89 data.get(offset..)
90 }
91}