use crate::types::GlyphBounds;
pub const MAX_DILATION: u8 = 4;
#[inline]
pub fn compute_dilation(rgb: [f32; 3]) -> u8 {
#[cfg(target_os = "macos")]
{
let luminance = 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2];
let dilation = 4.0 * (0.75 - luminance).max(0.0);
dilation.min(MAX_DILATION as f32).round() as u8
}
#[cfg(not(target_os = "macos"))]
{
let _ = rgb;
0
}
}
#[inline]
pub fn dilate_bounds(bounds: GlyphBounds, dilation: u8) -> GlyphBounds {
if dilation == 0 {
return bounds;
}
let d = dilation as u32;
GlyphBounds {
width: bounds.width.saturating_add(2 * d),
height: bounds.height.saturating_add(2 * d),
}
}
#[inline]
pub fn compute_dilation_srgb(rgb: [u8; 3]) -> u8 {
let r = (rgb[0] as f32 / 255.0).powf(2.2);
let g = (rgb[1] as f32 / 255.0).powf(2.2);
let b = (rgb[2] as f32 / 255.0).powf(2.2);
compute_dilation([r, g, b])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn white_text_gets_no_dilation() {
let dilation = compute_dilation([1.0, 1.0, 1.0]);
#[cfg(target_os = "macos")]
assert_eq!(dilation, 0, "White text has high luminance, no dilation");
#[cfg(not(target_os = "macos"))]
assert_eq!(dilation, 0, "Non-macOS always returns 0");
}
#[test]
fn black_text_gets_dilation() {
let dilation = compute_dilation([0.0, 0.0, 0.0]);
#[cfg(target_os = "macos")]
assert_eq!(dilation, 3, "Black text gets dilation");
#[cfg(not(target_os = "macos"))]
assert_eq!(dilation, 0, "Non-macOS always returns 0");
}
#[test]
fn dilate_bounds_expands_correctly() {
let bounds = GlyphBounds {
width: 10,
height: 12,
};
let dilated = dilate_bounds(bounds, 2);
assert_eq!(dilated.width, 14);
assert_eq!(dilated.height, 16);
}
#[test]
fn dilate_bounds_zero_is_noop() {
let bounds = GlyphBounds {
width: 10,
height: 12,
};
let dilated = dilate_bounds(bounds, 0);
assert_eq!(dilated, bounds);
}
#[test]
fn compute_dilation_srgb_converts_correctly() {
let dilation = compute_dilation_srgb([255, 255, 255]);
#[cfg(target_os = "macos")]
assert_eq!(dilation, 0);
#[cfg(not(target_os = "macos"))]
assert_eq!(dilation, 0);
}
}