diff --git a/crates/text/src/render.rs b/crates/text/src/render.rs index 47da2de811..477568c8b0 100644 --- a/crates/text/src/render.rs +++ b/crates/text/src/render.rs @@ -2,7 +2,7 @@ use photocraft_color::{Color, ColorMode, PixelFormat, SampleType}; use photocraft_doc::text::AntiAlias; -use photocraft_geom::{Affine, Rect}; +use photocraft_geom::{Affine, Rect, TILE_SIZE}; use photocraft_raster::Surface; use skrifa::instance::{LocationRef, NormalizedCoord, Size}; use skrifa::outline::DrawSettings; @@ -52,6 +52,19 @@ fn fits_raster_budget(w: usize, h: usize, stride: usize, sample: SampleType, bol peak.is_some_and(|b| b <= max_bytes) } +/// Whether the whole tiles the output surface stores for `rect` fit `max_bytes`. A thin rectangle +/// (an extreme transform of short text) crosses many tiles while holding few pixels, so the pixel +/// budget alone doesn't bound them. `held` is what the accumulator keeps alive meanwhile: the +/// tiles are allocated while it is still in use. +fn fits_tile_budget(rect: Rect, format: PixelFormat, held: u64, max_bytes: u64) -> bool { + let tile_px = (TILE_SIZE as u64).pow(2); + rect.tile_count() + .checked_mul(tile_px) + .and_then(|px| px.checked_mul(format.bytes_per_pixel() as u64)) + .and_then(|b| b.checked_add(held)) + .is_some_and(|b| b <= max_bytes) +} + /// A zeroed buffer, or `None` when its bytes cannot be reserved. fn zeroed(len: usize) -> Option> { let mut v = Vec::new(); @@ -596,7 +609,12 @@ pub(crate) fn rasterize_passes( let n = format.mode.color_channels(); let stride = n + 1; let any_bold = colors.iter().any(|(_, b, _)| *b); - if !fits_raster_budget(w, h, stride, format.sample, any_bold, MAX_RASTER_BYTES) { + // The accumulator (f32 per sample) is live while the output tiles are allocated. + // u64 arithmetic: on a 32-bit target the usize product overflows before the budget check. + let Some(acc_bytes) = (w as u64).checked_mul(h as u64).and_then(|n| n.checked_mul((stride * size_of::()) as u64)) else { + return nothing(format); + }; + if !fits_raster_budget(w, h, stride, format.sample, any_bold, MAX_RASTER_BYTES) || !fits_tile_budget(rect, format, acc_bytes, MAX_RASTER_BYTES) { return nothing(format); } // Premultiplied accumulation. @@ -810,8 +828,8 @@ pub fn outlines(layout: &TextLayout, transform: &Affine, warp: Option<&Warp>) -> #[cfg(test)] mod tests { - use super::{MAX_PIXELS, fits_raster_budget, rect_from_bounds, zeroed}; - use photocraft_color::SampleType; + use super::{MAX_PIXELS, fits_raster_budget, fits_tile_budget, rect_from_bounds, zeroed}; + use photocraft_color::{PixelFormat, SampleType}; use photocraft_geom::Rect; /// Verifies underline and strikethrough decoration drawing bounds clip strictly to the text shape's explicit path span. @@ -844,6 +862,19 @@ mod tests { } } + #[test] + fn a_thin_rectangle_is_charged_for_the_tiles_it_crosses() { + let format = PixelFormat::RGBA8; + // One pixel high, 2^24 wide: 16 Mi pixels, but 65536 tiles of 256 KiB (16 GiB). + let thin = Rect::new(0, 0, 1 << 24, 1); + assert!(!fits_tile_budget(thin, format, 0, 4 << 30)); + // The same pixels in a square fit in 16 MiB of tiles. + let square = Rect::new(0, 0, 4096, 4096); + assert!(fits_tile_budget(square, format, 0, 64 << 20)); + assert!(!fits_tile_budget(square, format, 0, (64 << 20) - 1), "the allowance is exact"); + assert!(!fits_tile_budget(square, format, 1, 64 << 20), "the accumulator is charged with the tiles"); + } + #[test] fn bounds_are_clipped_without_overflow_and_oversized_rectangles_rejected() { let min = f64::from(i32::MIN);