use std::sync::Arc;
use super::*;
use crate::text::Font;
const CASCADIA_BYTES: &[u8] = include_bytes!("../../../demo/assets/CascadiaCode.ttf");
const NUNITO_BYTES: &[u8] = include_bytes!("../../../demo/assets/Nunito_Regular.ttf");
fn cascadia_font() -> Arc<Font> {
Arc::new(Font::from_slice(CASCADIA_BYTES).expect("cascadia font"))
}
fn nunito_font() -> Arc<Font> {
Arc::new(Font::from_slice(NUNITO_BYTES).expect("nunito font"))
}
const DIAG_TEXT: &str = "Renders text using per-channel R/G/B coverage";
const DIAG_SIZE: f64 = 14.0;
fn reset_typography_defaults() {
crate::font_settings::set_gamma(1.0);
crate::font_settings::set_width(1.0);
crate::font_settings::set_interval(0.0);
crate::font_settings::set_faux_weight(0.0);
crate::font_settings::set_faux_italic(0.0);
crate::font_settings::set_primary_weight(1.0 / 3.0);
crate::font_settings::set_hinting_enabled(true);
}
fn lin(c: u8) -> f64 {
(c as f64 / 255.0).powf(2.2)
}
fn luma(px: &[u8]) -> f64 {
0.2126 * lin(px[0]) + 0.7152 * lin(px[1]) + 0.0722 * lin(px[2])
}
fn linear_ink(buf: &[u8]) -> f64 {
buf.chunks_exact(4).map(|p| 1.0 - luma(p)).sum()
}
fn composite_on_white(cached: &CachedLcdText, color: Color) -> Vec<u8> {
let w = cached.width;
let h = cached.height;
let mask = LcdMask {
data: (*cached.pixels).clone(),
width: w,
height: h,
};
let mut buf = vec![255u8; (w as usize) * (h as usize) * 4];
composite_lcd_mask(&mut buf, w, h, &mask, color, 0, 0);
buf
}
#[test]
fn lcd_vs_gray_ink_parity_bound() {
reset_typography_defaults();
let color = Color::rgba(0.1, 0.1, 0.1, 1.0);
for (name, f) in [("Nunito", nunito_font()), ("CascadiaCode", cascadia_font())] {
let lcd = rasterize_text_lcd_cached(&f, DIAG_TEXT, DIAG_SIZE);
let gray = rasterize_text_gray_cached(&f, DIAG_TEXT, DIAG_SIZE);
let cov_lcd: u64 = lcd.pixels.iter().map(|&b| b as u64).sum();
let cov_gray: u64 = gray.pixels.iter().map(|&b| b as u64).sum();
assert!(cov_gray > 0, "{name}: gray mask produced no coverage");
let cov_ratio = cov_lcd as f64 / cov_gray as f64;
assert!(
(cov_ratio - 1.0).abs() < 0.02,
"{name}: raw mask coverage diverged; lcd={cov_lcd} gray={cov_gray} \
(ratio {cov_ratio:.4}, want within 2% of 1.0) — the rasteriser \
changed how much ink it emits, not just how it spreads it"
);
let ink_gray = linear_ink(&composite_on_white(&gray, color));
let ink_lcd = linear_ink(&composite_on_white(&lcd, color));
assert!(ink_gray > 0.0, "{name}: gray composite deposited no ink");
let ink_ratio = ink_lcd / ink_gray;
assert!(
ink_ratio < 1.06,
"{name}: LCD text is too bold relative to grayscale; \
ink_lcd={ink_lcd:.3} ink_gray={ink_gray:.3} (ratio {ink_ratio:.4}, \
want < 1.06). Coverage ratio is {cov_ratio:.4}, so this is a \
filter/composite regression rather than a raster one."
);
}
}