ttf_parser/tables/cmap/
format12.rs

1// https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-12-segmented-coverage
2
3use core::convert::TryFrom;
4
5use crate::parser::{Stream, FromData};
6
7#[derive(Clone, Copy)]
8pub struct SequentialMapGroup {
9    pub start_char_code: u32,
10    pub end_char_code: u32,
11    pub start_glyph_id: u32,
12}
13
14impl FromData for SequentialMapGroup {
15    const SIZE: usize = 12;
16
17    #[inline]
18    fn parse(data: &[u8]) -> Option<Self> {
19        let mut s = Stream::new(data);
20        Some(SequentialMapGroup {
21            start_char_code: s.read::<u32>()?,
22            end_char_code: s.read::<u32>()?,
23            start_glyph_id: s.read::<u32>()?,
24        })
25    }
26}
27
28pub fn parse(data: &[u8], code_point: u32) -> Option<u16> {
29    let mut s = Stream::new(data);
30    s.skip::<u16>(); // format
31    s.skip::<u16>(); // reserved
32    s.skip::<u32>(); // length
33    s.skip::<u32>(); // language
34    let count: u32 = s.read()?;
35    let groups = s.read_array32::<SequentialMapGroup>(count)?;
36    for group in groups {
37        let start_char_code = group.start_char_code;
38        if code_point >= start_char_code && code_point <= group.end_char_code {
39            let id = group.start_glyph_id.checked_add(code_point)?.checked_sub(start_char_code)?;
40            return u16::try_from(id).ok();
41        }
42    }
43
44    None
45}
46
47pub fn codepoints(data: &[u8], mut f: impl FnMut(u32)) -> Option<()> {
48    let mut s = Stream::new(data);
49    s.skip::<u16>(); // format
50    s.skip::<u16>(); // reserved
51    s.skip::<u32>(); // length
52    s.skip::<u32>(); // language
53    let count: u32 = s.read()?;
54    let groups = s.read_array32::<SequentialMapGroup>(count)?;
55    for group in groups {
56        for code_point in group.start_char_code..=group.end_char_code {
57            f(code_point);
58        }
59    }
60
61    Some(())
62}