use core::cmp::Ordering;
include!("../../generated/generated_svg.rs");
impl<'a> Svg<'a> {
pub fn glyph_data(&self, glyph_id: GlyphId) -> Result<Option<&'a [u8]>, ReadError> {
let document_list = self.svg_document_list()?;
let svg_document = document_list
.document_records()
.binary_search_by(|r| {
if r.start_glyph_id.get() > glyph_id {
Ordering::Greater
} else if r.end_glyph_id.get() < glyph_id {
Ordering::Less
} else {
Ordering::Equal
}
})
.ok()
.and_then(|index| document_list.document_records().get(index))
.and_then(|r| {
let all_data = document_list.data.as_bytes();
all_data.get(
r.svg_doc_offset.get() as usize
..(r.svg_doc_offset.get() + r.svg_doc_length.get()) as usize,
)
});
Ok(svg_document)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::BeBuffer;
#[test]
fn read_dummy_svg_file() {
let data: [u16; 32] = [
0, 0, 10, 0, 0, 3, 1, 3, 0, 38, 0, 10, 6, 7, 0, 48, 0, 6, 9, 9, 0, 38, 0, 10,
1, 0, 0, 0, 1, 2, 0, 0, ];
let mut buf = BeBuffer::new();
buf = buf.extend(data);
let table = Svg::read(buf.font_data()).unwrap();
let first_document = &[0, 1, 0, 0, 0, 0, 0, 0, 0, 1][..];
let second_document = &[0, 2, 0, 0, 0, 0][..];
assert_eq!(table.glyph_data(GlyphId::new(0)).unwrap(), None);
assert_eq!(
table.glyph_data(GlyphId::new(1)).unwrap(),
Some(first_document)
);
assert_eq!(
table.glyph_data(GlyphId::new(2)).unwrap(),
Some(first_document)
);
assert_eq!(
table.glyph_data(GlyphId::new(3)).unwrap(),
Some(first_document)
);
assert_eq!(table.glyph_data(GlyphId::new(4)).unwrap(), None);
assert_eq!(table.glyph_data(GlyphId::new(5)).unwrap(), None);
assert_eq!(
table.glyph_data(GlyphId::new(6)).unwrap(),
Some(second_document)
);
assert_eq!(
table.glyph_data(GlyphId::new(7)).unwrap(),
Some(second_document)
);
assert_eq!(table.glyph_data(GlyphId::new(8)).unwrap(), None);
assert_eq!(
table.glyph_data(GlyphId::new(9)).unwrap(),
Some(first_document)
);
assert_eq!(table.glyph_data(GlyphId::new(10)).unwrap(), None);
}
}