PhotoCraft: An open-source, photo editor written by scratch in pure Rust
text: apply anti-alias mode shaping when rasterizing type
nrq6993-a11y81b6cdf0d2fc
diff --git a/crates/text/src/render.rs b/crates/text/src/render.rsindex af29dd10d..7fff345a6 100644--- a/crates/text/src/render.rs+++ b/crates/text/src/render.rs@@ -19,6 +19,28 @@ pub const FAUX_BOLD_RADIUS: f32 = 0.036;/// Largest raster we produce (pixels), as a guard against absurd sizes.const MAX_PIXELS: u64 = 256 * 1024 * 1024;+/// Maps raw area-coverage to the opacity Photoshop's anti-alias modes imply. Exponents above 1 thin+/// the fringe (sharper); below 1 widen it (stronger smoothing).+fn shape_antialias_coverage(cv: f32, mode: AntiAlias) -> f32 {+ if !cv.is_finite() {+ return 0.0;+ }+ let cv = cv.clamp(0.0, 1.0);+ match mode {+ AntiAlias::None => {+ if cv >= 0.5 {+ 1.0+ } else {+ 0.0+ }+ }+ AntiAlias::Smooth => cv,+ AntiAlias::Strong => cv.powf(0.55),+ AntiAlias::Crisp => cv.powf(1.35),+ AntiAlias::Sharp | AntiAlias::Windows | AntiAlias::WindowsLcd => cv.powf(2.2),+ }+}+/// Rendered text: pixels in document space plus the covered rectangle.pub struct Rendered {pub surface: Surface,@@ -277,7 +299,7 @@ pub fn rasterize_warped(layout: &TextLayout, transform: &Affine, format: PixelFolet comps = color_in(&format, c);let a = c.alpha.clamp(0.0, 1.0);for (i, &cv) in cov.iter().enumerate() {- let cv = if antialias == AntiAlias::None { if cv >= 0.5 { 1.0 } else { 0.0 } } else { cv };+ let cv = shape_antialias_coverage(cv, antialias);let s = cv * a;if s <= 0.0 {continue;diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex 13de665cc..c6cc6b2ce 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -1930,6 +1930,35 @@ fn engine_data_antialias_numbers_match_photoshop() {}}+/// Anti-alias modes must change edge coverage (storytold/photocraft#5902).+#[test]+fn antialias_modes_change_edge_fringe() {+ use photocraft_doc::text::AntiAlias;++ let fringe_mass = |mode: AntiAlias| {+ let mut e = TextEngine::new();+ let layer = TextLayer { antialias: mode, ..point("O", 48.0) };+ let (_, r) = e.render(&layer, 72.0, PixelFormat::RGBA8);+ let n = r.surface.channels();+ r.surface+ .read_region(r.rect)+ .chunks_exact(n)+ .map(|p| f64::from(p[n - 1]))+ .filter(|&a| a > 0.02 && a < 0.98)+ .sum::<f64>()+ };++ let none = fringe_mass(AntiAlias::None);+ let sharp = fringe_mass(AntiAlias::Sharp);+ let smooth = fringe_mass(AntiAlias::Smooth);+ let strong = fringe_mass(AntiAlias::Strong);++ assert_eq!(none, 0.0, "aliased-off edges are hard");+ assert!(strong > smooth + 1.0, "strong smooths more than smooth: {strong} vs {smooth}");+ assert!(smooth > sharp + 0.5, "sharp thins the fringe vs smooth: {sharp} vs {smooth}");+ assert!(sharp > 0.0);+}+/// Type on a path (#218): along a vertical line the type runs top to bottom with its glyphs/// turned, along a curve it follows the curve, and glyphs beyond the end are not drawn.#[test]