use crate::canvas::Canvas;
use crate::color::Color;
use crate::text::{Face, Font, Metrics, TextSystem};
use crate::web_host::{text_draw, text_measure, text_metrics, took};
use std::collections::HashMap;
const KEPT: usize = 8 << 20;
const MEASURES: usize = 4096;
struct Drawn {
left: i32,
baseline: i32,
width: usize,
ink: Ink,
}
enum Ink {
Cover(Vec<u8>),
Colours(Vec<u32>),
}
#[derive(Default)]
pub struct SystemText {
drawn: HashMap<(String, String), Drawn>,
bytes: usize,
measured: HashMap<(String, String), (f32, Vec<f32>)>,
metrics: HashMap<String, [f32; 4]>,
}
impl std::fmt::Debug for SystemText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SystemText").field("drawn", &self.drawn.len()).field("measured", &self.measured.len()).finish_non_exhaustive()
}
}
impl SystemText {
pub fn new() -> Option<SystemText> {
Some(SystemText::default())
}
fn css(font: Font, scale: f32) -> String {
let px = font.size * scale;
let (family, style) = match font.face {
Face::Regular => (None, (400, false)),
Face::Bold => (None, (700, false)),
Face::Italic => (None, (400, true)),
Face::Mono => (Some("mono".to_string()), (400, false)),
Face::Named(_) => {
let (family, of, style) = crate::system_fonts::asked(font.face);
(family.or((of == Face::Mono).then(|| "mono".into())), (style.weight, style.italic))
}
};
format!("{} {} {px}px\n{}", if style.1 { "italic" } else { "normal" }, style.0, family.unwrap_or_else(|| "sans".into()))
}
fn sizes(&mut self, css: &str) -> [f32; 4] {
if let Some(sizes) = self.metrics.get(css) {
return *sizes;
}
let mut sizes = [0.0f32; 4];
unsafe { text_metrics(css.as_ptr(), css.len(), sizes.as_mut_ptr()) };
self.metrics.insert(css.into(), sizes);
sizes
}
fn measured(&mut self, css: &str, text: &str) -> &(f32, Vec<f32>) {
if self.measured.len() >= MEASURES {
self.measured.clear();
}
let key = (css.to_string(), text.to_string());
self.measured.entry(key).or_insert_with(|| {
let mut stops = vec![0.0f32; text.chars().count()];
let width = unsafe { text_measure(css.as_ptr(), css.len(), text.as_ptr(), text.len(), stops.as_mut_ptr(), stops.len()) };
(width, stops)
})
}
fn drawn(&mut self, css: &str, text: &str) -> Option<&Drawn> {
let key = (css.to_string(), text.to_string());
if !self.drawn.contains_key(&key) {
let drawn = draw_line(css, text)?;
if self.bytes > KEPT {
(self.drawn, self.bytes) = (HashMap::new(), 0);
}
self.bytes += drawn.width * 4 + text.len();
self.drawn.insert(key.clone(), drawn);
}
self.drawn.get(&key)
}
}
fn draw_line(css: &str, text: &str) -> Option<Drawn> {
let mut place = [0.0f32; 4];
let bytes = unsafe { text_draw(css.as_ptr(), css.len(), text.as_ptr(), text.len(), place.as_mut_ptr()) };
let width = place[2] as usize;
if bytes == 0 || width == 0 {
return None;
}
let rgba = took(bytes);
let pixels = rgba.as_chunks::<4>().0;
let coloured = pixels.iter().any(|pixel| pixel[3] > 0 && (pixel[0] != 255 || pixel[1] != 255 || pixel[2] != 255));
let ink = match coloured {
false => Ink::Cover(pixels.iter().map(|pixel| pixel[3]).collect()),
true => Ink::Colours(pixels.iter().map(premultiplied).collect()),
};
Some(Drawn { left: place[0] as i32, baseline: place[1] as i32, width, ink })
}
fn premultiplied(pixel: &[u8; 4]) -> u32 {
let alpha = u32::from(pixel[3]);
let times = |channel: u8| (u32::from(channel) * alpha + 127) / 255;
alpha << 24 | times(pixel[0]) << 16 | times(pixel[1]) << 8 | times(pixel[2])
}
impl TextSystem for SystemText {
fn metrics(&mut self, font: Font, scale: f32) -> Metrics {
let [ascent, descent, cap, small] = self.sizes(&SystemText::css(font, scale));
Metrics { ascent: ascent.ceil() / scale, descent: descent.ceil() / scale, cap_height: cap / scale, x_height: small / scale }
}
fn measure(&mut self, text: &str, font: Font, scale: f32, stops: Option<&mut Vec<f32>>) -> f32 {
if text.is_empty() {
return 0.0;
}
let (width, ends) = self.measured(&SystemText::css(font, scale), text);
if let Some(stops) = stops {
stops.extend(ends.iter().map(|end| end / scale));
}
width / scale
}
fn draw(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
let scale = canvas.scale();
let css = SystemText::css(font, scale);
let ascent = self.sizes(&css)[0].ceil() as i32;
let Some(drawn) = self.drawn(&css, text) else { return };
let at = [(x * scale).round() as i32 - drawn.left, (y * scale).round() as i32 + ascent - drawn.baseline];
match &drawn.ink {
Ink::Cover(cover) => canvas.cover(at[0], at[1], drawn.width, cover, color),
Ink::Colours(pixels) => canvas.pixels(pixels, drawn.width, at, false),
}
}
}