zed-industries/zed · error

Canvas text raster exceeds maximum pixel count

Error message

Canvas text raster exceeds maximum pixel count {MAX_RASTER_PIXELS}: {width}x{height}

What it means

Beyond per-axis limits, `rasterize` caps total pixel count at MAX_RASTER_PIXELS (4 * 1024 * 1024, i.e. 4M pixels ≈ 16MB RGBA). This error is thrown when width * height exceeds that budget even though each dimension is within 4096. It protects memory: each raster is read back into a JS Uint8ClampedArray and then into a Rust Vec.

Solutions

  1. Break the text region into smaller rasters (per line or tile) so each stays under ~4M pixels.
  2. Lower the device pixel ratio / scale factor used to compute bounds.
  3. Reduce font size or render fewer lines per rasterize call.
  4. Check upstream bounds computation for incorrect scaling that inflates width*height.

Example fix

// before
let bounds = Bounds { size: size(px(4000), px(2000)) * scale(2.0) }; // 8000x4000 clamped to 4096x4000 = >4M px
rasterize(text, font, bounds, offset, false)?;
// after
for line_bounds in bounds.lines_within(MAX_RASTER_PIXELS) {
    rasterize(line, font, line_bounds, offset, false)?;
}
Defensive patterns

Strategy: validation

Validate before calling

const MAX_RASTER_PIXELS: usize = 4 * 1024 * 1024;
let pixels = (bounds.size.width.0 as usize).saturating_mul(bounds.size.height.0 as usize);
if pixels > MAX_RASTER_PIXELS {
    // tile the region into multiple rasters
}

Prevention

When it happens

Trigger: Calling `rasterize` with e.g. 4096x1024+ dimensions totaling more than 4,194,304 pixels — a tall/wide glyph run at high scale factor, or large multi-line text bounds passed as a single rasterize call.

Common situations: High-DPI rendering (2x-3x scale) of large text blocks; rendering an entire paragraph area instead of per-line glyph runs; a layout bug producing inflated bounds that individually pass the dimension check but multiply past the pixel cap.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of zed-industries/zed@916fc2b8cb (2026-09-19). Data as JSON: /api/errors/f6d0f467f4ebffec. Report an issue: GitHub.

Appendix: source

Thrown at crates/gpui_web/src/canvas_text.rs:142

    ensure!(
        subpixel_offset.0.is_finite() && subpixel_offset.1.is_finite(),
        "Canvas text subpixel offset must be finite"
    );
    let width = u32::try_from(bounds.size.width.0)
        .context("Canvas text raster width must be nonnegative")?;
    let height = u32::try_from(bounds.size.height.0)
        .context("Canvas text raster height must be nonnegative")?;
    if width == 0 || height == 0 {
        return Ok(Vec::new());
    }
    ensure!(
        width <= MAX_RASTER_DIMENSION && height <= MAX_RASTER_DIMENSION,
        "Canvas text raster exceeds maximum dimension {MAX_RASTER_DIMENSION}: {width}x{height}"
    );
    let pixel_count = usize::try_from(width)?
        .checked_mul(usize::try_from(height)?)
        .context("Canvas text raster pixel count overflow")?;
    ensure!(
        pixel_count <= MAX_RASTER_PIXELS,
        "Canvas text raster exceeds maximum pixel count {MAX_RASTER_PIXELS}: {width}x{height}"
    );
    let byte_count = pixel_count
        .checked_mul(4)
        .context("Canvas text raster byte count overflow")?;

    with_canvas(|canvas| {
        // Shrink before growing so intermediate canvas sizes obey the pixel cap.
        if height < canvas.canvas.height() {
            canvas.canvas.set_height(height);
        }
        if canvas.canvas.width() != width {
            canvas.canvas.set_width(width);
        }
        if canvas.canvas.height() != height {
            canvas.canvas.set_height(height);
        }

View on GitHub (pinned to 916fc2b8cb)