read_fonts/tables/
ltag.rsinclude!("../../generated/generated_ltag.rs");
impl<'a> Ltag<'a> {
pub fn tag_indices(&self) -> impl Iterator<Item = (u32, &'a str)> {
let table_data = self.offset_data().as_bytes();
self.tag_ranges()
.iter()
.enumerate()
.filter_map(move |(index, range)| {
let start = range.offset() as usize;
let range = start..start + range.length() as usize;
let string_bytes = table_data.get(range)?;
let s = core::str::from_utf8(string_bytes).ok()?;
Some((index as u32, s))
})
}
pub fn index_for_tag(&self, tag: &str) -> Option<u32> {
self.tag_indices().find(|x| x.1 == tag).map(|x| x.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::BeBuffer;
#[test]
fn tags() {
let mut buf = BeBuffer::new();
buf = buf.extend([1u32, 0, 3]);
buf = buf.extend([24u16, 2, 26, 2, 28, 2]);
buf = buf.extend("enspsr".as_bytes().iter().copied());
let expected_tags = [(0, "en"), (1, "sp"), (2, "sr")];
let ltag = Ltag::read(buf.font_data()).unwrap();
let tags = ltag.tag_indices().collect::<Vec<_>>();
assert_eq!(tags, expected_tags);
assert_eq!(ltag.index_for_tag("en"), Some(0));
assert_eq!(ltag.index_for_tag("sp"), Some(1));
assert_eq!(ltag.index_for_tag("sr"), Some(2));
assert_eq!(ltag.index_for_tag("ar"), None);
assert_eq!(ltag.index_for_tag("hi"), None);
}
}