swash/internal/
glyf.rs

1//! Glyph data table.
2
3use super::{raw_tag, Bytes, RawTag, Stream};
4
5pub const GLYF: RawTag = raw_tag(b"glyf");
6pub const LOCA: RawTag = raw_tag(b"loca");
7pub const CVT_: RawTag = raw_tag(b"cvt ");
8pub const FPGM: RawTag = raw_tag(b"fpgm");
9pub const PREP: RawTag = raw_tag(b"prep");
10pub const CVAR: RawTag = raw_tag(b"cvar");
11pub const GVAR: RawTag = raw_tag(b"gvar");
12
13/// Returns the data for the specified glyph.
14pub fn get<'a>(
15    data: &'a [u8],
16    loca_fmt: u8,
17    loca: u32,
18    glyf: u32,
19    glyph_id: u16,
20) -> Option<&'a [u8]> {
21    let range = {
22        let b = Bytes::with_offset(data, loca as usize)?;
23        let (start, end) = if loca_fmt == 0 {
24            let offset = glyph_id as usize * 2;
25            let start = b.read::<u16>(offset)? as usize * 2;
26            let end = b.read::<u16>(offset + 2)? as usize * 2;
27            (start, end)
28        } else if loca_fmt == 1 {
29            let offset = glyph_id as usize * 4;
30            let start = b.read::<u32>(offset)? as usize;
31            let end = b.read::<u32>(offset + 4)? as usize;
32            (start, end)
33        } else {
34            return None;
35        };
36        if end < start {
37            return None;
38        }
39        start..end
40    };
41    let glyf = Bytes::with_offset(data, glyf as usize)?;
42    glyf.data().get(range)
43}
44
45/// Returns the y-max value of the specified glyph from the bounding box in the
46/// `glyf` table.
47pub fn ymax(data: &[u8], loca_fmt: u8, loca: u32, glyf: u32, glyph_id: u16) -> Option<i16> {
48    let glyph_data = get(data, loca_fmt, loca, glyf, glyph_id)?;
49    let mut s = Stream::new(glyph_data);
50    s.skip(8)?;
51    s.read()
52}