use super::*;
#[must_use]
pub(crate) const fn build_ascii_advance_lut() -> [f32; 128] {
let mut table: [f32; 128] = [PROPORTIONAL_ADVANCE_RATIO; 128];
let groups: [(&[u8], f32); 17] = [
(b"ijl|", 0.26),
(b"I ", 0.27),
(b"'.,:;!", 0.29),
(b"/\\", 0.30),
(b"ft", 0.36),
(b"r()[]{}", 0.38),
(b"1*-\"", 0.46),
(b"`?sxJzvky", 0.53),
(b"acLeF", 0.56),
(b"gpqbd", 0.60),
(b"023456789#$+<=>^~TPS", 0.63),
(b"RYBKZAVX", 0.66),
(b"&CDUHNG", 0.73),
(b"OQw", 0.77),
(b"mM", 0.86),
(b"@%", 0.92),
(b"W", 0.96),
];
let mut g_idx: usize = 0;
while g_idx < groups.len() {
let (bytes, ratio): (&[u8], f32) = groups[g_idx];
let mut idx: usize = 0;
while idx < bytes.len() {
table[bytes[idx] as usize] = ratio;
idx += 1;
}
g_idx += 1;
}
table
}
#[inline(always)]
#[must_use]
pub fn text_bold_advance_factor(style: TextStyle) -> f32 {
if style.get_family() == FontFamily::Monospace {
1.0
} else if style.get_weight().is_bold() {
BOLD_PROPORTIONAL_FACTOR
} else {
1.0
}
}
#[inline(always)]
#[must_use]
pub fn glyph_bitmap_rows(ch: char) -> [u8; BITMAP_GLYPH_HEIGHT] {
let code: u32 = ch as u32;
if (ASCII_FIRST_PRINTABLE..=ASCII_LAST_PRINTABLE).contains(&code) {
ASCII_FONT_5X7[(code - ASCII_FIRST_PRINTABLE) as usize]
} else {
FALLBACK_GLYPH_ROWS
}
}
#[inline(always)]
#[must_use]
pub fn measure_char_advance(ch: char, style: TextStyle) -> f32 {
let code: u32 = ch as u32;
if matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F) {
return 0.0;
}
let is_mono: bool = style.get_family() == FontFamily::Monospace;
let bold_factor: f32 = text_bold_advance_factor(style);
if code < 128 {
let ratio: f32 = if is_mono {
MONOSPACE_ADVANCE_RATIO
} else {
ASCII_ADVANCE_LUT[code as usize]
};
return style.get_font_size() * ratio * bold_factor + style.get_letter_spacing();
}
if is_wide_unicode(ch) {
let wide_ratio: f32 = if is_emoji_unicode(ch) {
WIDE_EMOJI_ADVANCE_RATIO
} else {
WIDE_CJK_ADVANCE_RATIO
};
return style.get_font_size() * wide_ratio + style.get_letter_spacing();
}
let ratio: f32 = if is_mono {
MONOSPACE_ADVANCE_RATIO
} else {
NON_ASCII_ADVANCE_RATIO
};
style.get_font_size() * ratio * bold_factor + style.get_letter_spacing()
}
#[inline(always)]
#[must_use]
fn dilate_row_mask(row_bits: u8, bold: bool) -> u8 {
if bold {
row_bits | ((row_bits >> 1) & 0x0F)
} else {
row_bits
}
}
fn compute_edt_1d(
input: &[f32],
output: &mut [f32],
parabola_idx: &mut [usize],
boundary_pos: &mut [f32],
) {
let count: usize = input.len();
if count == 0 {
return;
}
let mut top: usize = 0;
parabola_idx[0] = 0;
boundary_pos[0] = f32::NEG_INFINITY;
boundary_pos[1] = f32::INFINITY;
for q_idx in 1..count {
let fq: f32 = input[q_idx];
let qf: f32 = q_idx as f32;
loop {
let p_idx: usize = parabola_idx[top];
let pf: f32 = p_idx as f32;
let fp: f32 = input[p_idx];
let intersection: f32 = ((fq + qf * qf) - (fp + pf * pf)) / (2.0 * (qf - pf));
if intersection <= boundary_pos[top] {
if top == 0 {
parabola_idx[0] = q_idx;
boundary_pos[1] = f32::INFINITY;
break;
}
top -= 1;
} else {
top += 1;
parabola_idx[top] = q_idx;
boundary_pos[top] = intersection;
boundary_pos[top + 1] = f32::INFINITY;
break;
}
}
}
let mut cursor: usize = 0;
for (q_idx, dst) in output.iter_mut().enumerate().take(count) {
let qf: f32 = q_idx as f32;
while boundary_pos[cursor + 1] < qf {
cursor += 1;
}
let p_idx: usize = parabola_idx[cursor];
let delta: f32 = qf - (p_idx as f32);
*dst = delta * delta + input[p_idx];
}
}
fn compute_edt_2d(grid: &mut [f32], width: usize, height: usize) {
let max_dim: usize = width.max(height);
if max_dim == 0 {
return;
}
let mut line_in: Vec<f32> = vec![0.0; max_dim];
let mut line_out: Vec<f32> = vec![0.0; max_dim];
let mut parabola_idx: Vec<usize> = vec![0; max_dim];
let mut boundary_pos: Vec<f32> = vec![0.0; max_dim + 1];
for col in 0..width {
for row in 0..height {
line_in[row] = grid[row * width + col];
}
compute_edt_1d(
&line_in[..height],
&mut line_out[..height],
&mut parabola_idx[..height],
&mut boundary_pos[..=height],
);
for row in 0..height {
grid[row * width + col] = line_out[row];
}
}
for row in 0..height {
let row_start: usize = row * width;
line_in[..width].copy_from_slice(&grid[row_start..row_start + width]);
compute_edt_1d(
&line_in[..width],
&mut line_out[..width],
&mut parabola_idx[..width],
&mut boundary_pos[..=width],
);
grid[row_start..row_start + width].copy_from_slice(&line_out[..width]);
}
for val in grid.iter_mut() {
*val = val.max(0.0).sqrt();
}
}
#[must_use]
pub fn build_glyph_sdf_field(
source: &RasterizedGlyph,
canonical_size_px: f32,
spread_px: f32,
) -> SdfGlyphField {
let src_w: usize = source.get_width() as usize;
let src_h: usize = source.get_height() as usize;
let safe_canonical: f32 = canonical_size_px.max(GLYPH_MIN_PHYSICAL_PX);
let safe_spread: f32 = spread_px.clamp(1.0, 32.0);
let advance_ratio: f32 = (source.get_advance() as f32) / safe_canonical;
if src_w == 0 || src_h == 0 {
return SdfGlyphField::new(0, 0, safe_canonical, safe_spread, advance_ratio, Vec::new());
}
let total: usize = src_w.saturating_mul(src_h);
let cov: &[u8] = source.get_coverage().as_slice();
let mut outside_dist: Vec<f32> = vec![GLYPH_EDT_INF; total];
let mut inside_dist: Vec<f32> = vec![GLYPH_EDT_INF; total];
let mut any_inside: bool = false;
for (idx, &alpha) in cov.iter().enumerate().take(total) {
if alpha >= 128 {
outside_dist[idx] = 0.0;
any_inside = true;
} else {
inside_dist[idx] = 0.0;
}
}
if !any_inside {
let distances: Vec<f32> = vec![safe_spread; total];
return SdfGlyphField::new(
src_w as u32,
src_h as u32,
safe_canonical,
safe_spread,
advance_ratio,
distances,
);
}
compute_edt_2d(&mut outside_dist, src_w, src_h);
compute_edt_2d(&mut inside_dist, src_w, src_h);
let mut distances: Vec<f32> = Vec::with_capacity(total);
for idx in 0..total {
let alpha_frac: f32 = (cov[idx] as f32) / 255.0;
let subpixel_bias: f32 = 0.5 - alpha_frac;
let signed: f32 =
(outside_dist[idx] - inside_dist[idx] + subpixel_bias).clamp(-safe_spread, safe_spread);
distances.push(signed);
}
SdfGlyphField::new(
src_w as u32,
src_h as u32,
safe_canonical,
safe_spread,
advance_ratio,
distances,
)
}
#[must_use]
pub fn evaluate_glyph_sdf(field: &SdfGlyphField, target_size_px: f32) -> RasterizedGlyph {
let safe_target: f32 =
target_size_px.clamp(GLYPH_MIN_PHYSICAL_PX, GLYPH_ANALYTIC_MAX_PHYSICAL_PX);
let canonical: f32 = field.get_canonical_size_px().max(GLYPH_MIN_PHYSICAL_PX);
let scale_ratio: f32 = safe_target / canonical;
let advance: u32 = (field.get_advance_ratio() * safe_target).ceil().max(4.0) as u32;
let src_w: u32 = field.get_width();
let src_h: u32 = field.get_height();
let distances: &[f32] = field.get_distances().as_slice();
if src_w == 0 || src_h == 0 || distances.is_empty() {
let out_h: u32 = (safe_target * 0.86).round().max(7.0) as u32;
let out_w: u32 = (advance as f32 * 0.86).round().max(3.0) as u32;
return RasterizedGlyph::new(
out_w,
out_h,
advance,
vec![0; (out_w as usize).saturating_mul(out_h as usize)],
);
}
let out_w: u32 = ((src_w as f32) * scale_ratio).round().max(1.0) as u32;
let out_h: u32 = ((src_h as f32) * scale_ratio).round().max(1.0) as u32;
let out_w_usize: usize = out_w as usize;
let src_w_usize: usize = src_w as usize;
let max_sx: f32 = (src_w - 1) as f32;
let max_sy: f32 = (src_h - 1) as f32;
let inv_scale_x: f32 = (src_w as f32) / (out_w as f32);
let inv_scale_y: f32 = (src_h as f32) / (out_h as f32);
let aa_half_canonical: f32 = (0.5 / scale_ratio).max(0.05);
let mut coverage: Vec<u8> = vec![0; out_w_usize.saturating_mul(out_h as usize)];
for py in 0..out_h {
let sy: f32 = (((py as f32) + 0.5) * inv_scale_y - 0.5).clamp(0.0, max_sy);
let y0: usize = sy.floor() as usize;
let y1: usize = (y0 + 1).min((src_h - 1) as usize);
let fy: f32 = sy - (y0 as f32);
let row0: usize = y0 * src_w_usize;
let row1: usize = y1 * src_w_usize;
let dst_row_start: usize = (py as usize) * out_w_usize;
let dst_row: &mut [u8] = &mut coverage[dst_row_start..dst_row_start + out_w_usize];
for (px, dst_px) in dst_row.iter_mut().enumerate() {
let sx: f32 = (((px as f32) + 0.5) * inv_scale_x - 0.5).clamp(0.0, max_sx);
let x0: usize = sx.floor() as usize;
let x1: usize = (x0 + 1).min((src_w - 1) as usize);
let fx: f32 = sx - (x0 as f32);
let d00: f32 = distances[row0 + x0];
let d10: f32 = distances[row0 + x1];
let d01: f32 = distances[row1 + x0];
let d11: f32 = distances[row1 + x1];
let top_d: f32 = d00 + (d10 - d00) * fx;
let bot_d: f32 = d01 + (d11 - d01) * fx;
let dist: f32 = top_d + (bot_d - top_d) * fy;
let linear_cov: f32 =
((aa_half_canonical - dist) / (2.0 * aa_half_canonical)).clamp(0.0, 1.0);
let smooth_cov: f32 = linear_cov * linear_cov * (3.0 - 2.0 * linear_cov);
*dst_px = (smooth_cov * 255.0 + 0.5) as u8;
}
}
RasterizedGlyph::new(out_w, out_h, advance, coverage)
}
#[must_use]
pub fn rasterize_glyph(key: GlyphKey) -> RasterizedGlyph {
if key.uses_analytic_sdf_fallback() {
let canonical_key: GlyphKey = key.with_physical_size(GLYPH_SDF_CANONICAL_PX);
let canonical_glyph: RasterizedGlyph = rasterize_glyph(canonical_key);
let field: SdfGlyphField = build_glyph_sdf_field(
&canonical_glyph,
GLYPH_SDF_CANONICAL_PX,
GLYPH_SDF_DEFAULT_SPREAD_PX,
);
return evaluate_glyph_sdf(&field, key.physical_size_px());
}
let code: u32 = key.get_codepoint() as u32;
if code > ASCII_LAST_PRINTABLE {
return rasterize_unicode_deterministic(key);
}
let size_px: f32 = (key.get_size_tenths() as f32) * 0.1;
let mut prop_style: TextStyle = TextStyle::default();
prop_style
.set_font_size(size_px)
.set_family(if key.get_monospace() {
FontFamily::Monospace
} else {
FontFamily::Sans
})
.set_weight(if key.get_bold() {
FontWeight::Bold
} else {
FontWeight::Normal
});
let target_adv: f32 = measure_char_advance(key.get_codepoint(), prop_style);
let out_h: u32 = (size_px * 0.86).round().max(7.0) as u32;
let advance: u32 = target_adv.round().max(3.0) as u32;
let out_w: u32 = (target_adv * 0.86).round().max(3.0).min(advance as f32) as u32;
let rows: [u8; BITMAP_GLYPH_HEIGHT] = glyph_bitmap_rows(key.get_codepoint());
let out_w_usize: usize = out_w as usize;
let total_pixels: usize = out_w_usize * (out_h as usize);
let mut coverage: Vec<u8> = vec![0; total_pixels];
if rows == [0; BITMAP_GLYPH_HEIGHT] {
return RasterizedGlyph::new(out_w, out_h, advance, coverage);
}
let bold: bool = key.get_bold();
let italic: bool = key.get_italic();
let src_w: f32 = BITMAP_GLYPH_WIDTH as f32;
let src_h: f32 = BITMAP_GLYPH_HEIGHT as f32;
let dst_w: f32 = out_w as f32;
let dst_h: f32 = out_h as f32;
let max_sx: f32 = src_w - 0.01;
let max_row: usize = BITMAP_GLYPH_HEIGHT - 1;
let max_col: usize = BITMAP_GLYPH_WIDTH - 1;
if !italic && out_w_usize <= GLYPH_STACK_COL_CACHE {
let mut shift_l_buf: [u8; GLYPH_STACK_COL_CACHE] = [0; GLYPH_STACK_COL_CACHE];
let mut shift_r_buf: [u8; GLYPH_STACK_COL_CACHE] = [0; GLYPH_STACK_COL_CACHE];
for px in 0..out_w_usize {
let pxf: f32 = px as f32;
let sx_l: f32 = ((pxf + 0.25) * src_w / dst_w).clamp(0.0, max_sx);
let sx_r: f32 = ((pxf + 0.75) * src_w / dst_w).clamp(0.0, max_sx);
let col_l: usize = (sx_l.floor() as usize).min(max_col);
let col_r: usize = (sx_r.floor() as usize).min(max_col);
shift_l_buf[px] = (max_col - col_l) as u8;
shift_r_buf[px] = (max_col - col_r) as u8;
}
for py in 0..out_h {
let pyf: f32 = py as f32;
let row_top_idx: usize = (((pyf + 0.25) * src_h / dst_h).floor() as usize).min(max_row);
let row_bot_idx: usize = (((pyf + 0.75) * src_h / dst_h).floor() as usize).min(max_row);
let eff_top: u8 = dilate_row_mask(rows[row_top_idx], bold);
let eff_bot: u8 = dilate_row_mask(rows[row_bot_idx], bold);
if eff_top == 0 && eff_bot == 0 {
continue;
}
let row_off: usize = (py as usize) * out_w_usize;
let row_dst: &mut [u8] = &mut coverage[row_off..row_off + out_w_usize];
for (px, dst_px) in row_dst.iter_mut().enumerate() {
let sl: u8 = shift_l_buf[px];
let sr: u8 = shift_r_buf[px];
let hits: u8 = ((eff_top >> sl) & 1)
+ ((eff_top >> sr) & 1)
+ ((eff_bot >> sl) & 1)
+ ((eff_bot >> sr) & 1);
if hits > 0 {
*dst_px = SUBPIXEL_ALPHA_LUT[hits as usize];
}
}
}
return RasterizedGlyph::new(out_w, out_h, advance, coverage);
}
let inv_dst_h_italic: f32 = 1.0 / dst_h.max(1.0);
for py in 0..out_h {
let pyf: f32 = py as f32;
let row_top_idx: usize = (((pyf + 0.25) * src_h / dst_h).floor() as usize).min(max_row);
let row_bot_idx: usize = (((pyf + 0.75) * src_h / dst_h).floor() as usize).min(max_row);
let eff_top: u8 = dilate_row_mask(rows[row_top_idx], bold);
let eff_bot: u8 = dilate_row_mask(rows[row_bot_idx], bold);
if eff_top == 0 && eff_bot == 0 {
continue;
}
let italic_shift: f32 = if italic {
(0.5 - pyf * inv_dst_h_italic) * 0.85
} else {
0.0
};
let row_off: usize = (py as usize) * out_w_usize;
let row_dst: &mut [u8] = &mut coverage[row_off..row_off + out_w_usize];
for (px, dst_px) in row_dst.iter_mut().enumerate() {
let pxf: f32 = px as f32;
let sx_l: f32 = ((pxf + 0.25) * src_w / dst_w - italic_shift).clamp(0.0, max_sx);
let sx_r: f32 = ((pxf + 0.75) * src_w / dst_w - italic_shift).clamp(0.0, max_sx);
let sl: u8 = (max_col - (sx_l.floor() as usize).min(max_col)) as u8;
let sr: u8 = (max_col - (sx_r.floor() as usize).min(max_col)) as u8;
let hits: u8 = ((eff_top >> sl) & 1)
+ ((eff_top >> sr) & 1)
+ ((eff_bot >> sl) & 1)
+ ((eff_bot >> sr) & 1);
if hits > 0 {
*dst_px = SUBPIXEL_ALPHA_LUT[hits as usize];
}
}
}
RasterizedGlyph::new(out_w, out_h, advance, coverage)
}