Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Text: count both coverage buffers on the plain path (#4979)

Muhammad Saadf13731a51a74
diff --git a/crates/text/src/render.rs b/crates/text/src/render.rs
index 51b9e1e5d..13dccc44c 100644
--- a/crates/text/src/render.rs
+++ b/crates/text/src/render.rs
@@ -19,20 +19,22 @@ 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;
/// 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 holds a second
-/// coverage buffer, so a CMYK bold render reserves several times what a plain grey one does for
-/// the same pixels.
+/// 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), the finished coverage of the colour being drawn and, with
-/// faux bold, the second coverage buffer and the vector its `finish` builds beside it. `false`
-/// when the arithmetic overflows, so an absurd size is refused rather than wrapping.
+/// 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 { 1 })?.checked_mul(f32s)?;
+ 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)
@@ -470,17 +472,17 @@ mod tests {
/// #4979: the pixel cap says nothing about bytes, so the peak is admitted from the channel
/// depth of the accumulator and surface, the sample size and the faux-bold coverage buffers.
- /// A 100 x 100 grey+alpha render at 8-bit keeps 2 x (4 + 1) + 4 = 14 bytes a pixel live.
+ /// A 100 x 100 grey+alpha render at 8-bit keeps 2 x (4 + 1) + 2 x 4 = 18 bytes a pixel live.
#[test]
fn the_byte_budget_follows_channels_depth_and_faux_bold() {
let px = 100 * 100;
- assert!(fits_raster_budget(100, 100, 2, SampleType::U8, false, px * 14));
- assert!(!fits_raster_budget(100, 100, 2, SampleType::U8, false, px * 14 - 1), "the allowance is exact");
+ assert!(fits_raster_budget(100, 100, 2, SampleType::U8, false, px * 18));
+ assert!(!fits_raster_budget(100, 100, 2, SampleType::U8, false, px * 18 - 1), "the allowance is exact");
// The same pixels cost more with more channels, a wider sample, or a bold pass.
- assert!(!fits_raster_budget(100, 100, 5, SampleType::U8, false, px * 14), "CMYK is deeper");
- assert!(!fits_raster_budget(100, 100, 2, SampleType::F32, false, px * 14), "f32 output is wider");
- assert!(!fits_raster_budget(100, 100, 2, SampleType::U8, true, px * 14), "bold holds two more buffers");
+ assert!(!fits_raster_budget(100, 100, 5, SampleType::U8, false, px * 18), "CMYK is deeper");
+ assert!(!fits_raster_budget(100, 100, 2, SampleType::F32, false, px * 18), "f32 output is wider");
+ assert!(!fits_raster_budget(100, 100, 2, SampleType::U8, true, px * 18), "bold holds one more buffer");
assert!(fits_raster_budget(100, 100, 2, SampleType::U8, true, px * 22), "2 x 5 + 3 x 4 bytes a pixel");
// A size whose peak overflows is refused rather than wrapping to a small number.