use std::collections::HashMap;
use crate::edt::compute_sdf;
use crate::raster::{rasterize, Affine};
use crate::vector_font::{path_bounding_box, VectorFont};
pub const TILE: u32 = 64;
pub const RASTER: u32 = 256;
pub const PAD_FRAC: f64 = 0.18;
pub const MAX_DISTANCE: f64 = RASTER as f64 * 0.125;
pub const ALPHA_THRESHOLD: u8 = 128;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GlyphMetrics {
pub u: f64,
pub v: f64,
pub w: f64,
pub h: f64,
pub view_box: [f64; 4],
}
pub struct VectorFontAtlas {
cols: u32,
rows: u32,
atlas_data: Vec<f32>,
next_slot: u32,
order: Vec<char>,
glyphs: HashMap<char, GlyphMetrics>,
}
impl VectorFontAtlas {
pub fn new(atlas_size: u32) -> Self {
let cols = atlas_size / TILE;
let size = (cols * TILE) as usize;
VectorFontAtlas {
cols,
rows: cols,
atlas_data: vec![0.0; size * size],
next_slot: 0,
order: Vec::new(),
glyphs: HashMap::new(),
}
}
pub fn cell_size(&self) -> u32 {
TILE
}
pub fn atlas_size(&self) -> u32 {
self.cols * TILE
}
pub fn cols(&self) -> u32 {
self.cols
}
pub fn rows(&self) -> u32 {
self.rows
}
pub fn atlas_data(&self) -> &[f32] {
&self.atlas_data
}
pub fn atlas_bytes(&self) -> &[u8] {
bytemuck::cast_slice(&self.atlas_data)
}
pub fn reset(&mut self) {
self.glyphs.clear();
self.order.clear();
self.next_slot = 0;
self.atlas_data.fill(0.0);
}
pub fn has_glyph(&self, ch: char) -> bool {
self.glyphs.contains_key(&ch)
}
pub fn slot_of(&self, ch: char) -> Option<u32> {
self.order.iter().position(|&c| c == ch).map(|i| i as u32)
}
pub fn ensure_glyphs(
&mut self,
font: Option<&VectorFont>,
chars: impl IntoIterator<Item = char>,
) -> bool {
let mut added = false;
for ch in chars {
if !self.glyphs.contains_key(&ch) {
self.get_glyph(font, ch);
added = true;
}
}
added
}
pub fn get_glyph(&mut self, font: Option<&VectorFont>, ch: char) -> GlyphMetrics {
if let Some(m) = self.glyphs.get(&ch) {
return *m;
}
let m = self.rasterize_glyph(font, ch);
self.glyphs.insert(ch, m);
m
}
fn rasterize_glyph(&mut self, font: Option<&VectorFont>, ch: char) -> GlyphMetrics {
let slot = self.next_slot;
self.next_slot += 1;
self.order.push(ch);
let col = slot % self.cols;
let row = slot / self.cols;
let atlas_w = self.atlas_size();
let mut view_box = [0.0f64; 4];
let mut alpha = vec![0u8; (RASTER * RASTER) as usize];
if let Some(font) = font {
let gid = font.glyph_for_char(ch);
let commands = font.glyph_path_y_down(gid);
let pb = path_bounding_box(&commands);
let gw = pb.x2 - pb.x1;
let gh = pb.y2 - pb.y1;
if gw > 0.0 && gh > 0.0 {
let avail = RASTER as f64 - 2.0 * (RASTER as f64 * PAD_FRAC);
let s = avail / gw.max(gh);
let rw = gw * s;
let rh = gh * s;
let off_x = (RASTER as f64 - rw) / 2.0;
let off_y = (RASTER as f64 - rh) / 2.0;
let affine = Affine {
off_x,
off_y,
s,
min_x: pb.x1,
min_y: pb.y1,
};
alpha = rasterize(&commands, &affine, RASTER);
view_box = [
off_x / RASTER as f64,
off_y / RASTER as f64,
(off_x + rw) / RASTER as f64,
(off_y + rh) / RASTER as f64,
];
}
}
let sdf = compute_sdf(&alpha, RASTER as usize, RASTER as usize, ALPHA_THRESHOLD);
let scale = (RASTER / TILE) as f64;
for sy in 0..TILE {
for sx in 0..TILE {
let src_x = (((sx as f64 + 0.5) * scale).floor() as u32).min(RASTER - 1);
let src_y = (((sy as f64 + 0.5) * scale).floor() as u32).min(RASTER - 1);
let dist = sdf[(src_y * RASTER + src_x) as usize];
let normalized = 0.5 - dist / (2.0 * MAX_DISTANCE);
let clamped = normalized.clamp(0.0, 1.0);
let atlas_x = col * TILE + sx;
let atlas_y = row * TILE + sy;
self.atlas_data[(atlas_y * atlas_w + atlas_x) as usize] = clamped as f32;
}
}
GlyphMetrics {
u: (col * TILE) as f64 / atlas_w as f64,
v: (row * TILE) as f64 / atlas_w as f64,
w: TILE as f64 / atlas_w as f64,
h: TILE as f64 / atlas_w as f64,
view_box,
}
}
pub fn tile(&self, slot: u32) -> Vec<f32> {
let col = slot % self.cols;
let row = slot / self.cols;
let atlas_w = self.atlas_size();
let mut out = Vec::with_capacity((TILE * TILE) as usize);
for y in 0..TILE {
for x in 0..TILE {
out.push(self.atlas_data[((row * TILE + y) * atlas_w + col * TILE + x) as usize]);
}
}
out
}
}
pub fn normalize_sdf_value(dist: f64, max_distance: f64) -> f64 {
(0.5 - dist / (2.0 * max_distance)).clamp(0.0, 1.0)
}