use ttf_parser::{cmap, GlyphId, RawFace};
use super::{
error::{BROKEN_CMAP_TABLE, CMAP_TAG, MISSING_CMAP_TABLE},
Result,
};
pub struct CMapTable<'a> {
subtables: Vec<cmap::Subtable<'a>>,
}
impl<'a> CMapTable<'a> {
pub fn parse(rf: RawFace<'a>) -> Result<Self> {
let cmap_data = rf.table(CMAP_TAG).ok_or(MISSING_CMAP_TABLE)?;
let table = cmap::Table::parse(cmap_data).ok_or(BROKEN_CMAP_TABLE)?;
let mut subtables = vec![];
for i in 0..table.subtables.len() {
let subtable = table.subtables.get(i).ok_or(BROKEN_CMAP_TABLE)?;
if subtable.is_unicode() {
subtables.push(subtable)
}
}
Ok(Self { subtables })
}
pub fn glyph_index(&self, c: char) -> Option<GlyphId> {
self.subtables
.iter()
.filter_map(|subtable| subtable.glyph_index(c as u32))
.next()
}
}