use crate::{
FontId, FontRun, GlyphId, LineLayout, Pixels, PlatformTextSystem, ShapedGlyph, ShapedRun,
point, px,
};
use collections::FxHashMap;
const CHARACTERS: &[u8] = b"0123456789.,+-%$KMBT";
const MAX_LEN: usize = 64;
pub(crate) const CHECKED_FIRST: u32 = 32;
const CHECKED_EVERY: u64 = 256;
const TOLERANCE: f32 = 1e-3;
const fn slots() -> [u8; 128] {
let mut slots = [u8::MAX; 128];
let mut i = 0;
while i < CHARACTERS.len() {
slots[CHARACTERS[i] as usize] = i as u8;
i += 1;
}
slots
}
const SLOTS: [u8; 128] = slots();
const COUNT: usize = CHARACTERS.len();
#[derive(Clone, Copy)]
struct Glyph {
id: GlyphId,
advance: f32,
}
struct Font {
glyphs: [Option<Option<Glyph>>; COUNT],
kerning: Box<[Option<Option<f32>>; COUNT * COUNT]>,
ascent: Pixels,
descent: Pixels,
is_emoji: bool,
checked: u32,
lines: u64,
disagreed: bool,
}
impl Font {
fn new() -> Self {
Font {
glyphs: [None; COUNT],
kerning: Box::new([None; COUNT * COUNT]),
ascent: px(0.),
descent: px(0.),
is_emoji: false,
checked: 0,
lines: 0,
disagreed: false,
}
}
}
#[derive(Default)]
pub(crate) struct NumberShaping {
fonts: FxHashMap<(FontId, u32), Font>,
fonts_generation: u64,
}
impl NumberShaping {
pub(crate) fn shape(
&mut self,
platform: &dyn PlatformTextSystem,
text: &str,
font_size: Pixels,
runs: &[FontRun],
) -> Option<LineLayout> {
let [run] = runs else {
return None;
};
let bytes = text.as_bytes();
if bytes.is_empty()
|| bytes.len() > MAX_LEN
|| run.len != bytes.len()
|| !bytes
.iter()
.all(|&byte| byte < 128 && SLOTS[byte as usize] != u8::MAX)
{
return None;
}
let fonts_generation = crate::fast::text::fonts_generation();
if fonts_generation != self.fonts_generation {
self.fonts.clear();
self.fonts_generation = fonts_generation;
}
let font_id = run.font_id;
let font = self
.fonts
.entry((font_id, font_size.0.to_bits()))
.or_insert_with(Font::new);
if font.disagreed {
return None;
}
let layout = put_together(font, platform, bytes, font_id, font_size)?;
font.lines += 1;
if font.checked < CHECKED_FIRST || font.lines.is_multiple_of(CHECKED_EVERY) {
let shaped = platform.layout_line(text, font_size, runs);
if !agree(&layout, &shaped) {
log::debug!(
"numbers in font {font_id:?} at {font_size:?} are left to the platform: \
it shapes {text:?} differently"
);
font.disagreed = true;
return Some(shaped);
}
font.checked = font.checked.saturating_add(1);
}
Some(layout)
}
}
fn put_together(
font: &mut Font,
platform: &dyn PlatformTextSystem,
bytes: &[u8],
font_id: FontId,
font_size: Pixels,
) -> Option<LineLayout> {
let mut glyphs = Vec::with_capacity(bytes.len());
let mut x = 0f32;
let mut previous: Option<(usize, Glyph)> = None;
for (index, &byte) in bytes.iter().enumerate() {
let slot = SLOTS[byte as usize] as usize;
let glyph = glyph(font, platform, slot, font_id, font_size)?;
if let Some((previous_slot, previous_glyph)) = previous {
let kerning = kerning(font, platform, previous_slot, slot, font_id, font_size)?;
x += previous_glyph.advance + kerning;
}
glyphs.push(ShapedGlyph {
id: glyph.id,
position: point(px(x), px(0.)),
index,
is_emoji: font.is_emoji,
});
previous = Some((slot, glyph));
}
let (_, last) = previous?;
Some(LineLayout {
font_size,
width: px(x + last.advance),
ascent: font.ascent,
descent: font.descent,
runs: vec![ShapedRun { font_id, glyphs }],
len: bytes.len(),
})
}
fn glyph(
font: &mut Font,
platform: &dyn PlatformTextSystem,
slot: usize,
font_id: FontId,
font_size: Pixels,
) -> Option<Glyph> {
if let Some(glyph) = font.glyphs[slot] {
return glyph;
}
let text = &CHARACTERS[slot..slot + 1];
let layout = platform.layout_line(
std::str::from_utf8(text).ok()?,
font_size,
&[FontRun { len: 1, font_id }],
);
let glyph = match layout.runs.as_slice() {
[run] if run.font_id == font_id => match run.glyphs.as_slice() {
[shaped] if shaped.position == point(px(0.), px(0.)) && shaped.index == 0 => {
font.ascent = layout.ascent;
font.descent = layout.descent;
font.is_emoji = shaped.is_emoji;
Some(Glyph {
id: shaped.id,
advance: layout.width.0,
})
}
_ => None,
},
_ => None,
};
font.glyphs[slot] = Some(glyph);
glyph
}
fn kerning(
font: &mut Font,
platform: &dyn PlatformTextSystem,
left: usize,
right: usize,
font_id: FontId,
font_size: Pixels,
) -> Option<f32> {
let pair = left * COUNT + right;
if let Some(kerning) = font.kerning[pair] {
return kerning;
}
let left_glyph = glyph(font, platform, left, font_id, font_size)?;
let right_glyph = glyph(font, platform, right, font_id, font_size)?;
let text = [CHARACTERS[left], CHARACTERS[right]];
let layout = platform.layout_line(
std::str::from_utf8(&text).ok()?,
font_size,
&[FontRun { len: 2, font_id }],
);
let kerning = match layout.runs.as_slice() {
[run] if run.font_id == font_id => match run.glyphs.as_slice() {
[first, second]
if first.id == left_glyph.id
&& second.id == right_glyph.id
&& first.index == 0
&& second.index == 1
&& first.position == point(px(0.), px(0.))
&& second.position.y == px(0.) =>
{
let kerning = second.position.x.0 - left_glyph.advance;
let width = second.position.x.0 + right_glyph.advance;
((layout.width.0 - width).abs() <= TOLERANCE).then_some(kerning)
}
_ => None,
},
_ => None,
};
font.kerning[pair] = Some(kerning);
kerning
}
fn agree(ours: &LineLayout, theirs: &LineLayout) -> bool {
let close = |a: Pixels, b: Pixels| (a.0 - b.0).abs() <= TOLERANCE;
ours.len == theirs.len
&& ours.font_size == theirs.font_size
&& close(ours.width, theirs.width)
&& ours.ascent == theirs.ascent
&& ours.descent == theirs.descent
&& ours.runs.len() == theirs.runs.len()
&& ours.runs.iter().zip(&theirs.runs).all(|(ours, theirs)| {
ours.font_id == theirs.font_id
&& ours.glyphs.len() == theirs.glyphs.len()
&& ours
.glyphs
.iter()
.zip(&theirs.glyphs)
.all(|(ours, theirs)| {
ours.id == theirs.id
&& ours.index == theirs.index
&& ours.is_emoji == theirs.is_emoji
&& close(ours.position.x, theirs.position.x)
&& close(ours.position.y, theirs.position.y)
})
})
}