mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
text: apply anti-alias mode shaping when rasterizing type
Photoshop's Sharp/Crisp/Strong/Smooth settings only changed metadata; shape edge coverage per mode (fixes storytold/photocraft#5902). Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
c11ef015b7
commit
81b6cdf0d2
2 files changed
+52
-1
No files matched your search
@@ -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: PixelFo
|
||||
let 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;
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user