Merge PR #5907: Text: apply anti-alias mode shaping when rasterizing type (#5902)

This commit is contained in:
aiongg committed 2026-10-21 21:56:20 +00:00
commit 0fe1d3233d
2 files changed
+49 -3

No files matched your search

+25 -3
View File
@@ -22,6 +22,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,
@@ -288,14 +310,14 @@ pub fn rasterize_warped(layout: &TextLayout, transform: &Affine, format: PixelFo
Rendered { surface, rect }
}
/// Composites one colour's coverage over the premultiplied accumulator, resolving `AntiAlias::None`
/// to 0/1 first. A pixel's maths reads and writes only its own slots, so the rows are done in any
/// Composites one colour's coverage over the premultiplied accumulator, after [`shape_antialias_coverage`].
/// A pixel's maths reads and writes only its own slots, so the rows are done in any
/// order — one per thread — for exactly the same bytes.
fn composite(acc: &mut [f32], cov: &[f32], width: usize, stride: usize, comps: &[f32], a: f32, antialias: AntiAlias) {
let n = comps.len();
let row = |acc: &mut [f32], cov: &[f32]| {
for (cv, px) in cov.iter().zip(acc.chunks_exact_mut(stride)) {
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;
+24
View File
@@ -2180,6 +2180,30 @@ 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]