ttf_parser/tables/cmap/format0.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-0-byte-encoding-table
use crate::parser::{Stream, NumFrom};
pub fn parse(data: &[u8], code_point: u32) -> Option<u16> {
let mut s = Stream::new(data);
s.skip::<u16>(); // format
s.skip::<u16>(); // length
s.skip::<u16>(); // language
s.advance(usize::num_from(code_point));
let glyph_id: u8 = s.read()?;
// Make sure that the glyph is not zero, the array always has length 256,
// but some codepoints may be mapped to zero.
if glyph_id != 0 {
Some(u16::from(glyph_id))
} else {
None
}
}
pub fn codepoints(data: &[u8], mut f: impl FnMut(u32)) -> Option<()> {
let mut s = Stream::new(data);
s.skip::<u16>(); // format
s.skip::<u16>(); // length
s.skip::<u16>(); // language
for code_point in 0..256 {
// In contrast to every other format, here we take a look at the glyph
// id and check whether it is zero because otherwise this method would
// always simply call `f` for `0..256` which would be kind of pointless
// (this array always has length 256 even when the face has only fewer
// glyphs).
let glyph_id: u8 = s.read()?;
if glyph_id != 0 {
f(code_point);
}
}
Some(())
}
#[cfg(test)]
mod tests {
use super::{parse, codepoints};
#[test]
fn maps_not_all_256_codepoints() {
let mut data = vec![
0x00, 0x00, // format: 0
0x01, 0x06, // subtable size: 262
0x00, 0x00, // language ID: 0
];
// Map (only) codepoint 0x40 to 100.
data.extend(std::iter::repeat(0).take(256));
data[6 + 0x40] = 100;
assert_eq!(parse(&data, 0), None);
assert_eq!(parse(&data, 0x40), Some(100));
assert_eq!(parse(&data, 100), None);
let mut vec = vec![];
codepoints(&data, |c| vec.push(c));
assert_eq!(vec, [0x40]);
}
}