mod bitmap;
use std::collections::BTreeSet;
use ttf_parser::{Face, GlyphId, Tag};
use unicode_properties::{GeneralCategory, UnicodeGeneralCategory};
pub fn font_supports_text(data: &[u8], face_index: u32, text: &str) -> bool {
font_missing_text(data, face_index, text).is_some_and(|missing| missing.is_empty())
}
pub fn font_missing_text(data: &[u8], face_index: u32, text: &str) -> Option<String> {
font_missing_text_impl(data, face_index, text, false)
}
pub fn font_missing_text_with_spaces(data: &[u8], face_index: u32, text: &str) -> Option<String> {
font_missing_text_impl(data, face_index, text, true)
}
fn font_missing_text_impl(
data: &[u8],
face_index: u32,
text: &str,
include_spaces: bool,
) -> Option<String> {
if data.len() > crate::paint::MAX_PORTABLE_FONT_BLOB_BYTES
|| text.chars().count() > crate::paint::MAX_PORTABLE_GLYPHS_PER_RUN
{
return None;
}
let face = Face::parse(data, face_index).ok()?;
Some(
text.chars()
.filter(|character| {
(if include_spaces {
!character.is_control()
} else {
!character.is_whitespace()
}) && !face
.glyph_index(*character)
.is_some_and(|glyph| glyph.0 != 0)
})
.collect(),
)
}
pub fn is_visible_symbol(character: char) -> bool {
character != '\u{2800}'
&& matches!(
character.general_category(),
GeneralCategory::MathSymbol
| GeneralCategory::CurrencySymbol
| GeneralCategory::ModifierSymbol
| GeneralCategory::OtherSymbol
)
}
fn declared_empty_glyf(face: &Face<'_>, glyph: GlyphId) -> Option<bool> {
use ttf_parser::head::IndexToLocationFormat;
let raw = face.raw_face();
let loca = raw.table(Tag::from_bytes(b"loca"))?;
let glyf = raw.table(Tag::from_bytes(b"glyf"))?;
let offset = |id: usize| -> Option<usize> {
match face.tables().head.index_to_location_format {
IndexToLocationFormat::Short => Some(
usize::from(u16::from_be_bytes(
loca.get(id * 2..id * 2 + 2)?.try_into().ok()?,
)) * 2,
),
IndexToLocationFormat::Long => usize::try_from(u32::from_be_bytes(
loca.get(id * 4..id * 4 + 4)?.try_into().ok()?,
))
.ok(),
}
};
let start = offset(usize::from(glyph.0))?;
let end = offset(usize::from(glyph.0) + 1)?;
let data = glyf.get(start..end)?;
if data.is_empty() {
return Some(true);
}
(data.len() >= 10).then(|| i16::from_be_bytes([data[0], data[1]]) == 0)
}
pub fn empty_symbol_codepoints(data: &[u8], face_index: u32) -> BTreeSet<u32> {
let mut empty = BTreeSet::new();
let Ok(face) = Face::parse(data, face_index) else {
return empty;
};
if face.is_variable() || face.tables().glyf.is_none() {
return empty;
}
static SYMBOLS: std::sync::OnceLock<Vec<char>> = std::sync::OnceLock::new();
let symbols = SYMBOLS.get_or_init(|| {
(0..=0x10FFFF)
.filter_map(char::from_u32)
.filter(|ch| is_visible_symbol(*ch))
.collect()
});
for character in symbols {
let Some(glyph) = face.glyph_index(*character) else {
continue;
};
if glyph.0 != 0
&& declared_empty_glyf(&face, glyph) == Some(true)
&& !face.is_color_glyph(glyph)
&& face.glyph_svg_image(glyph).is_none()
&& !bitmap::may_supply_ink(&face, glyph)
{
empty.insert(*character as u32);
}
}
empty
}
#[cfg_attr(feature = "wasm-api", wasm_bindgen::prelude::wasm_bindgen(js_name = fontSymbolUnicodeRange))]
pub fn font_symbol_unicode_range(data: &[u8], face_index: u32) -> String {
let empty = empty_symbol_codepoints(data, face_index);
if empty.is_empty() {
return String::new();
}
let mut ranges = Vec::new();
let mut start = 0;
for codepoint in empty {
if start < codepoint {
ranges.push(format!("U+{start:X}-{:X}", codepoint - 1));
}
start = codepoint + 1;
}
if start <= 0x10FFFF {
ranges.push(format!("U+{start:X}-10FFFF"));
}
ranges.join(",")
}