ttf_parser/tables/cmap/
format6.rs

1// https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-6-trimmed-table-mapping
2
3use core::convert::TryFrom;
4
5use crate::parser::Stream;
6
7pub fn parse(data: &[u8], code_point: u32) -> Option<u16> {
8    // This subtable supports code points only in a u16 range.
9    let code_point = u16::try_from(code_point).ok()?;
10
11    let mut s = Stream::new(data);
12    s.skip::<u16>(); // format
13    s.skip::<u16>(); // length
14    s.skip::<u16>(); // language
15    let first_code_point: u16 = s.read()?;
16    let count: u16 = s.read()?;
17    let glyphs = s.read_array16::<u16>(count)?;
18
19    let idx = code_point.checked_sub(first_code_point)?;
20    glyphs.get(idx)
21}
22
23pub fn codepoints(data: &[u8], mut f: impl FnMut(u32)) -> Option<()> {
24    let mut s = Stream::new(data);
25    s.skip::<u16>(); // format
26    s.skip::<u16>(); // length
27    s.skip::<u16>(); // language
28    let first_code_point: u16 = s.read()?;
29    let count: u16 = s.read()?;
30
31    for i in 0..count {
32        let code_point = first_code_point.checked_add(i)?;
33        f(u32::from(code_point));
34    }
35
36    Some(())
37}