use ttf_parser::{Face, GlyphId, RasterImageFormat, Tag};
pub(super) fn may_supply_ink(face: &Face<'_>, glyph: GlyphId) -> bool {
let raw = face.raw_face();
if raw.table(Tag::from_bytes(b"sbix")).is_some() {
return true;
}
for (tag, table) in [
(b"EBLC", face.tables().ebdt),
(b"CBLC", face.tables().cbdt),
(b"bloc", face.tables().bdat),
] {
let Some(locations) = raw.table(Tag::from_bytes(tag)) else {
continue;
};
let Some(table) = table else { return true };
let Some(header) = locations.get(..8) else {
return true;
};
let count = u32::from_be_bytes(header[4..8].try_into().unwrap()) as usize;
if count > 256 || count > locations.len().saturating_sub(8) / 48 {
return true;
}
let mut sizes = std::collections::BTreeSet::new();
for index in 0..count {
let record = &locations[8 + index * 48..8 + (index + 1) * 48];
let first = u16::from_be_bytes([record[40], record[41]]);
let last = u16::from_be_bytes([record[42], record[43]]);
if first > last {
return true;
}
if (first..=last).contains(&glyph.0) {
sizes.insert(u16::from(record[44]));
}
}
for size in sizes {
let Some(image) = table.get(glyph, size) else {
return true;
};
match image.format {
RasterImageFormat::BitmapMono
| RasterImageFormat::BitmapMonoPacked
| RasterImageFormat::BitmapGray2
| RasterImageFormat::BitmapGray2Packed
| RasterImageFormat::BitmapGray4
| RasterImageFormat::BitmapGray4Packed
| RasterImageFormat::BitmapGray8 => {
if image.data.iter().any(|byte| *byte != 0) {
return true;
}
}
_ => return true,
}
}
}
false
}