PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #6001: Text: admit the rasterizer's peak working bytes (#4979)
aionggbf68c22689a2
diff --cc crates/text/src/render.rsindex 7128085dc,13dccc44c..fe32982d9--- a/crates/text/src/render.rs+++ b/crates/text/src/render.rs@@@ -21,29 -18,41 +21,63 @@@ pub const FAUX_ITALIC_DEG: f32 = 12.0pub 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;+ /// Peak working bytes one rasterization may keep live. [`MAX_PIXELS`] does not bound them: the+ /// accumulator and the output surface are both `stride` deep, and a faux-bold pass keeps one more+ /// coverage buffer live, so a CMYK bold render reserves several times what a plain grey one does+ /// for the same pixels.+ const MAX_RASTER_BYTES: u64 = if cfg!(target_pointer_width = "64") { 4 << 30 } else { 1 << 30 };++ /// Whether a `w` x `h` render's peak working bytes fit `max_bytes`: the f32 accumulator and the+ /// output surface (both `stride` deep), plus the coverage buffers live at the peak. A plain pass+ /// peaks inside `Coverage::finish`, which holds its accumulator and the vector it builds beside+ /// it; a faux-bold pass peaks one buffer higher, since the resolved plain coverage is kept while+ /// the bold pass resolves into it. `false` when the arithmetic overflows, so an absurd size is+ /// refused rather than wrapping.+ fn fits_raster_budget(w: usize, h: usize, stride: usize, sample: SampleType, bold: bool, max_bytes: u64) -> bool {+ let f32s = size_of::<f32>() as u64;+ let peak = (w as u64).checked_mul(h as u64).and_then(|px| {+ let deep = px.checked_mul(stride as u64)?.checked_mul(f32s + sample.bytes() as u64)?;+ let coverage = px.checked_mul(if bold { 3 } else { 2 })?.checked_mul(f32s)?;+ deep.checked_add(coverage)+ });+ peak.is_some_and(|b| b <= max_bytes)+ }++ /// A zeroed buffer, or `None` when its bytes cannot be reserved.+ fn zeroed(len: usize) -> Option<Vec<f32>> {+ let mut v = Vec::new();+ v.try_reserve_exact(len).ok()?;+ v.resize(len, 0.0);+ Some(v)+ }++ /// What a refused or unrenderable layout returns.+ fn nothing(format: PixelFormat) -> Rendered {+ Rendered { surface: Surface::new(format), rect: Rect::new(0, 0, 0, 0) }+ }+/// 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,