flxzt/rnote · error

creating image surface with dimensions

Error message

creating image surface with dimensions ({}, {}) failed, Err: {e:?}

What it means

cairo::ImageSurface::create failed when allocating an ARgb32 surface of the scaled image dimensions in Image::gen_with_cairo. Usually the requested width/height are zero, negative, or exceed Cairo/platform limits.

Solutions

  1. Clamp width_scaled/height_scaled to at least 1 and below cairo's max surface size before calling create.
  2. Validate the zoom/scale input is finite and > 0 before generating.
  3. Tiled rendering: split very large images into tiles instead of one huge surface.
  4. Check for f64->i32 cast overflow (use saturating casts or checked_mul).

Example fix

// before
let width_scaled = (self.pixel_width as f64 * scale) as i32;
// after
let width_scaled = ((self.pixel_width as f64 * scale) as i32).clamp(1, 32767);
Defensive patterns

Strategy: validation

Validate before calling

// rust
fn dims_ok(w: i32, h: i32) -> bool {
    w > 0 && h > 0 && w <= 32767 && h <= 32767
}

Type guard

fn safe_scale(dim: u32, scale: f64) -> Option<i32> {
    if !scale.is_finite() || scale <= 0.0 { return None; }
    Some(((dim as f64 * scale) as i32).clamp(1, 32767))
}

Try / catch

match image.gen_with_cairo(scale, draw_fn) {
    Ok(tex) => tex,
    Err(e) if e.to_string().contains("creating image surface") => {
        // retry with clamped dimensions or lower zoom
    }
}

Prevention

When it happens

Trigger: Calling gen_with_cairo where width_scaled or height_scaled is <= 0 or absurdly large (integer overflow from scaling math, or cairo's size limit ~32767).

Common situations: Zoom values like 0.0 or negative from malformed settings; zoom-out values truncating a 1px image to 0; huge page sizes multiplied by high zoom causing i32 overflow.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08). Data as JSON: /api/errors/d027eb775ad57da8. Report an issue: GitHub.

Appendix: source

Thrown at crates/rnote-engine/src/image.rs:340

        image_scale: f64,
    ) -> anyhow::Result<Self>
    where
        F: FnOnce(&cairo::Context) -> anyhow::Result<()>,
    {
        bounds.ensure_positive();
        bounds.loosen(1.0);
        bounds.assert_valid()?;

        let width_scaled = ((bounds.extents()[0]) * image_scale).round() as u32;
        let height_scaled = ((bounds.extents()[1]) * image_scale).round() as u32;

        let mut image_surface = cairo::ImageSurface::create(
            cairo::Format::ARgb32,
            width_scaled as i32,
            height_scaled as i32,
        )
        .map_err(|e| {
            anyhow::anyhow!(
                "creating image surface with dimensions ({}, {}) failed, Err: {e:?}",
                width_scaled,
                height_scaled,
            )
        })?;

        {
            let cairo_cx = cairo::Context::new(&image_surface)?;
            cairo_cx.scale(image_scale, image_scale);
            cairo_cx.translate(-bounds.mins[0], -bounds.mins[1]);
            // Apply the draw function
            draw_func(&cairo_cx)?;
        }

        let data = image_surface
            .data()
            .map_err(|e| anyhow::anyhow!("accessing image surface data failed, Err: {e:?}"))?
            .to_vec();

View on GitHub (pinned to bbc5354502)