use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use cosmic_text::{
Attrs, Buffer, Family, FontSystem, Metrics, Shaping, Style as FontStyle, Weight, fontdb,
};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use crate::raster::{INTER, INTER_ITALIC, MONO, MONO_ITALIC, emoji_font, text_pixels};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Font {
pub mono: bool,
pub bold: bool,
pub italic: bool,
pub size: u8,
}
impl Default for Font {
fn default() -> Self {
Self {
mono: false,
bold: false,
italic: false,
size: 100,
}
}
}
pub(crate) fn family(font: Font) -> Family<'static> {
if font.mono {
Family::Monospace
} else {
Family::SansSerif
}
}
pub(crate) fn attrs(font: Font) -> Attrs<'static> {
let mut attrs = Attrs::new().family(family(font));
if font.bold {
attrs = attrs.weight(Weight::SEMIBOLD);
}
if font.italic {
attrs = attrs.style(FontStyle::Italic);
}
attrs
}
pub(crate) fn emoji_letter(letter: &str) -> bool {
letter.contains('\u{fe0f}')
|| letter.chars().next().is_some_and(|c| {
let code = c as u32;
(0x1f000..=0x1faff).contains(&code)
|| ((0x2300..=0x2bff).contains(&code) && c.width() == Some(2))
})
}
pub const EMOJI_AIR: f32 = 0.14;
pub(crate) fn runs<'a>(
text: &'a str,
font: Font,
emoji: Option<&'a str>,
) -> Vec<(&'a str, Attrs<'a>)> {
let plain = attrs(font);
let Some(family) = emoji.filter(|_| !text.is_ascii()) else {
return vec![(text, plain)];
};
let coloured = plain
.clone()
.family(Family::Name(family))
.letter_spacing(EMOJI_AIR);
let pick = |emoji: bool| {
if emoji {
coloured.clone()
} else {
plain.clone()
}
};
let mut out = Vec::new();
let (mut start, mut kind) = (0, None);
for (at, letter) in text.grapheme_indices(true) {
let is = emoji_letter(letter);
if let Some(was) = kind.filter(|was| *was != is) {
out.push((&text[start..at], pick(was)));
start = at;
}
kind = Some(is);
}
out.push((&text[start..], pick(kind == Some(true))));
out
}
pub trait Measure {
fn advances(&self, text: &str, font: Font) -> Rc<Vec<f32>>;
fn in_cells(&self) -> bool;
fn cell_pixels(&self) -> Option<(u32, u32)> {
None
}
fn shows_emoji(&self) -> bool {
true
}
fn width(&self, text: &str, font: Font) -> f32 {
self.advances(text, font).iter().sum()
}
}
pub struct CellMeasure;
impl Measure for CellMeasure {
fn advances(&self, text: &str, _font: Font) -> Rc<Vec<f32>> {
if text.is_ascii() {
let column = |byte: u8| f32::from(u8::from(!byte.is_ascii_control()));
return Rc::new(text.bytes().map(column).collect());
}
let mut out = Vec::with_capacity(text.len());
for letter in text.graphemes(true) {
let count = letter.chars().count();
if count == 1 || letter.chars().any(char::is_control) {
out.extend(letter.chars().map(|c| c.width().unwrap_or(0) as f32));
} else {
out.push(letter.width() as f32);
out.extend(std::iter::repeat_n(0.0, count - 1));
}
}
Rc::new(out)
}
fn in_cells(&self) -> bool {
true
}
}
type Advances = Rc<Vec<f32>>;
pub struct FontMeasure {
fonts: RefCell<FontSystem>,
with_emoji: RefCell<Option<(FontSystem, Option<String>)>>,
cell: (usize, usize),
cache: RefCell<HashMap<(String, Font), Advances>>,
}
fn built_in() -> fontdb::Database {
let mut db = fontdb::Database::new();
for data in [INTER, INTER_ITALIC, MONO, MONO_ITALIC] {
db.load_font_data(data.to_vec());
}
db.set_monospace_family("JetBrains Mono");
db.set_sans_serif_family("Inter Variable");
db
}
impl FontMeasure {
pub fn new(cell_width: usize, cell_height: usize) -> Self {
Self {
fonts: RefCell::new(FontSystem::new_with_locale_and_db(
"en-US".into(),
built_in(),
)),
with_emoji: RefCell::new(None),
cell: (cell_width, cell_height),
cache: RefCell::new(HashMap::new()),
}
}
fn shape(
&self,
fonts: &mut FontSystem,
emoji: Option<&str>,
text: &str,
font: Font,
) -> Vec<f32> {
let px = text_pixels(self.cell.1, font);
let mut buffer = Buffer::new(fonts, Metrics::new(px, px * 1.4));
buffer.set_size(None, None);
buffer.set_rich_text(
runs(text, font, emoji),
&attrs(font),
Shaping::Advanced,
None,
);
buffer.shape_until_scroll(fonts, false);
let mut by_byte = vec![0.0; text.len() + 1];
for run in buffer.layout_runs() {
for glyph in run.glyphs {
by_byte[glyph.start] += glyph.w;
}
}
by_byte
}
fn shape_with_emoji(&self, text: &str, font: Font) -> Vec<f32> {
let mut with_emoji = self.with_emoji.borrow_mut();
let (fonts, emoji) = with_emoji.get_or_insert_with(|| {
let mut db = built_in();
let family = emoji_font().map(|(family, source)| {
db.load_font_source(source.clone());
family.clone()
});
let fonts = FontSystem::new_with_locale_and_db("en-US".into(), db);
(fonts, family)
});
self.shape(fonts, emoji.as_deref(), text, font)
}
}
impl Measure for FontMeasure {
fn advances(&self, text: &str, font: Font) -> Rc<Vec<f32>> {
let key = (text.to_string(), font);
if let Some(found) = self.cache.borrow().get(&key) {
return found.clone();
}
let emoji = !text.is_ascii() && text.graphemes(true).any(emoji_letter);
let by_byte = if emoji {
self.shape_with_emoji(text, font)
} else {
self.shape(&mut self.fonts.borrow_mut(), None, text, font)
};
let cell = self.cell.0 as f32;
let advances: Vec<f32> = text
.char_indices()
.map(|(at, _)| by_byte[at] / cell)
.collect();
let advances = Rc::new(advances);
let mut cache = self.cache.borrow_mut();
if cache.len() > 20_000 {
cache.clear();
}
cache.insert(key, advances.clone());
advances
}
fn in_cells(&self) -> bool {
false
}
fn cell_pixels(&self) -> Option<(u32, u32)> {
Some((self.cell.0 as u32, self.cell.1 as u32))
}
fn shows_emoji(&self) -> bool {
emoji_font().is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cells_count_columns() {
assert_eq!(CellMeasure.width("abc", Font::default()), 3.0);
}
#[test]
fn an_emoji_of_several_code_points_is_two_columns() {
let font = Font::default();
for emoji in ["π", "β€\u{fe0f}", "ππ½", "π©\u{200d}π»", "πΉπ·"] {
assert_eq!(CellMeasure.width(emoji, font), 2.0, "{emoji}");
let advances = CellMeasure.advances(emoji, font);
assert_eq!(advances.len(), emoji.chars().count(), "{emoji}");
}
assert_eq!(CellMeasure.width("aβ€\u{fe0f}b", font), 4.0);
assert_eq!(CellMeasure.width("e\u{301}\r\n", font), 1.0);
}
#[test]
fn proportional_text_has_real_widths() {
let m = FontMeasure::new(17, 37);
let font = Font::default();
assert!(m.width("iiii", font) < m.width("WWWW", font));
assert_eq!(m.advances("hΓ©llo", font).len(), 5);
}
#[test]
fn mono_text_keeps_even_advances() {
let m = FontMeasure::new(17, 37);
let font = Font {
mono: true,
..Font::default()
};
let advances = m.advances("iW", font);
assert!((advances[0] - advances[1]).abs() < 0.01);
}
#[test]
fn an_emoji_is_measured_in_the_font_that_draws_it() {
let m = FontMeasure::new(17, 37);
let font = Font::default();
if !m.shows_emoji() {
return;
}
let advances = m.advances("aπb", font);
assert_eq!(advances.len(), 3);
let letter = m.width("a", font);
assert!((advances[0] - letter).abs() < 0.01, "{advances:?}");
assert!(advances[1] > letter && advances[1] < 3.0, "{advances:?}");
let heart = m.width("β€\u{fe0f}", font);
let air = EMOJI_AIR * crate::raster::INTERFACE_SIZE * 37.0 / 17.0;
let wider = heart - advances[1];
assert!(
(-0.02..air + 0.02).contains(&wider),
"{heart} beside {advances:?} and {air} of air"
);
}
#[test]
fn a_line_is_set_in_runs_with_its_emoji_apart() {
let font = Font::default();
let all = runs("ok β€\u{fe0f}π then β
", font, Some("Some Emoji"));
let words: Vec<&str> = all.iter().map(|(text, _)| *text).collect();
assert_eq!(words, ["ok ", "β€\u{fe0f}π", " then ", "β
"]);
assert_eq!(runs("plain", font, Some("Some Emoji")).len(), 1);
assert_eq!(runs("ok π", font, None).len(), 1);
for letter in ["δΈ", "β", "β", "Γ©"] {
assert!(!emoji_letter(letter), "{letter}");
}
}
#[test]
fn larger_text_is_wider() {
let m = FontMeasure::new(17, 37);
let big = Font {
size: 200,
..Font::default()
};
let width = m.width("Title", big);
assert!((width - 2.0 * m.width("Title", Font::default())).abs() < 0.3);
}
}