use anyhow::Result;
use crate::{FontId, FontRun, GlyphId, Pixels, TextSystem};
pub(crate) fn em_width(
text_system: &TextSystem,
font_id: FontId,
font_size: Pixels,
) -> Result<Pixels> {
if let Some((fallback_id, glyph_id)) = fallback_m(text_system, font_id, font_size) {
let bounds = text_system
.platform_text_system
.typographic_bounds(fallback_id, glyph_id)?;
return Ok(font_size * bounds.size.width / text_system.units_per_em(fallback_id) as f32);
}
Ok(text_system
.typographic_bounds(font_id, font_size, 'm')?
.size
.width)
}
pub(crate) fn em_advance(
text_system: &TextSystem,
font_id: FontId,
font_size: Pixels,
) -> Result<Pixels> {
if let Some((fallback_id, glyph_id)) = fallback_m(text_system, font_id, font_size) {
let advance = text_system
.platform_text_system
.advance(fallback_id, glyph_id)?;
return Ok(font_size * advance.width / text_system.units_per_em(fallback_id) as f32);
}
Ok(text_system.advance(font_id, font_size, 'm')?.width)
}
fn fallback_m(
text_system: &TextSystem,
font_id: FontId,
font_size: Pixels,
) -> Option<(FontId, GlyphId)> {
let platform = &text_system.platform_text_system;
if platform.glyph_for_char(font_id, 'm').is_some() {
return None;
}
let layout = platform.layout_line("m", font_size, &[FontRun { len: 1, font_id }]);
layout
.runs
.iter()
.filter(|run| run.font_id != font_id)
.find_map(|run| Some((run.font_id, run.glyphs.first()?.id)))
}