1 | use crate::parser::{LazyArray32, Stream}; |
2 | use crate::GlyphId; |
3 | |
4 | /// A [format 10](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-10-trimmed-array) |
5 | /// subtable. |
6 | #[derive (Clone, Copy, Debug)] |
7 | pub struct Subtable10<'a> { |
8 | /// First character code covered. |
9 | pub first_code_point: u32, |
10 | /// Array of glyph indices for the character codes covered. |
11 | pub glyphs: LazyArray32<'a, GlyphId>, |
12 | } |
13 | |
14 | impl<'a> Subtable10<'a> { |
15 | /// Parses a subtable from raw data. |
16 | pub fn parse(data: &'a [u8]) -> Option<Self> { |
17 | let mut s = Stream::new(data); |
18 | s.skip::<u16>(); // format |
19 | s.skip::<u16>(); // reserved |
20 | s.skip::<u32>(); // length |
21 | s.skip::<u32>(); // language |
22 | let first_code_point = s.read::<u32>()?; |
23 | let count = s.read::<u32>()?; |
24 | let glyphs = s.read_array32::<GlyphId>(count)?; |
25 | Some(Self { |
26 | first_code_point, |
27 | glyphs, |
28 | }) |
29 | } |
30 | |
31 | /// Returns a glyph index for a code point. |
32 | pub fn glyph_index(&self, code_point: u32) -> Option<GlyphId> { |
33 | let idx = code_point.checked_sub(self.first_code_point)?; |
34 | self.glyphs.get(idx) |
35 | } |
36 | |
37 | /// Calls `f` for each codepoint defined in this table. |
38 | pub fn codepoints(&self, mut f: impl FnMut(u32)) { |
39 | for i in 0..self.glyphs.len() { |
40 | if let Some(code_point) = self.first_code_point.checked_add(i) { |
41 | f(code_point); |
42 | } |
43 | } |
44 | } |
45 | } |
46 | |