{"record":{"id":"a587ccfe3cc66dec","repo":"flxzt/rnote","slug":"creating-svg-surface-with-dimensions-width-he","errorCode":null,"errorMessage":"Creating svg surface with dimensions ({width}, {height}) failed, Err: {e:?}","messagePattern":"Creating svg surface with dimensions \\((.+?), (.+?)\\) failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/svg.rs","lineNumber":117,"sourceCode":"        self.svg_data = usvg_tree.to_string(&xml_options);\n        self.bounds = bounds_simplified;\n\n        Ok(())\n    }\n\n    /// Generate an Svg through cairo's SvgSurface.\n    pub fn gen_with_cairo<F>(draw_func: F, mut bounds: Aabb) -> anyhow::Result<Self>\n    where\n        F: FnOnce(&cairo::Context) -> anyhow::Result<()>,\n    {\n        bounds.ensure_positive();\n        bounds.assert_valid()?;\n\n        let width = bounds.extents()[0];\n        let height = bounds.extents()[1];\n        let mut svg_surface =\n            cairo::SvgSurface::for_stream(width, height, Vec::new()).map_err(|e| {\n                anyhow::anyhow!(\n                    \"Creating svg surface with dimensions ({width}, {height}) failed, Err: {e:?}\"\n                )\n            })?;\n        svg_surface.set_document_unit(cairo::SvgUnit::Px);\n\n        {\n            let cairo_cx = cairo::Context::new(&svg_surface)?;\n            // cairo only draws elements with positive coordinates, so we need to translate the content here\n            cairo_cx.translate(-bounds.mins[0], -bounds.mins[1]);\n            // apply the draw function\n            draw_func(&cairo_cx)?;\n        }\n\n        let content = String::from_utf8(\n            *svg_surface\n                .finish_output_stream()\n                .map_err(|e| {\n                    anyhow::anyhow!(\"Finishing Svg surface output stream failed, Err: {e:?}\")","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/svg.rs#L99-L135","documentation":"svg::gen_with_cairo creates a cairo::SvgSurface writing to an in-memory stream using the extents of the given bounds; if cairo fails to create the surface (invalid dimensions, out-of-memory, or a cairo internal error), the Result is Err and this message wraps it. Bounds are asserted valid first, so failures here are typically dimensional or cairo-level.","triggerScenarios":"Calling gen_with_cairo with bounds whose extents are zero, negative, non-finite, or exceed cairo's surface size limits (roughly >2^15-2^16 px depending on backend/version).","commonSituations":"Exporting a very large or zoomed canvas producing extents beyond cairo limits; NaN/infinite extents from degenerate geometry slipping past validation; exotic cairo builds on headless systems.","solutions":["Clamp/validate width and height to a sane range (> 0 and within cairo limits) before creating the surface","Sanity-check bounds for NaN/infinity before calling gen_with_cairo","Tile the export into multiple smaller SVG surfaces if the extents exceed limits","Inspect the wrapped {e:?} for cairo's specific status code to confirm the cause"],"exampleFix":"// before\nlet width = bounds.extents()[0];\nlet height = bounds.extents()[1];\nlet surface = cairo::SvgSurface::for_stream(width, height, Vec::new())?;\n// after\nlet (width, height) = (bounds.extents()[0], bounds.extents()[1]);\nif !(width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0 && width < 32767.0 && height < 32767.0) {\n    return Err(anyhow!(\"unsupported svg export size: {width}x{height}\"));\n}\nlet surface = cairo::SvgSurface::for_stream(width, height, Vec::new())?;","handlingStrategy":"validation","validationCode":"let (w, h) = (bounds.extents()[0], bounds.extents()[1]);\nif !w.is_finite() || !h.is_finite() || w <= 0.0 || h <= 0.0 || w > 32767.0 || h > 32767.0 {\n    return Err(anyhow!(\"svg extents {w}x{h} unsupported\"));\n}","typeGuard":"fn valid_extents(bounds: &kurbo::Rect) -> bool {\n    let (w, h) = (bounds.width(), bounds.height());\n    w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0 && w <= 32767.0 && h <= 32767.0\n}","tryCatchPattern":"match svg::gen_with_cairo(bounds, draw) {\n    Ok(svg) => svg,\n    Err(e) if e.to_string().contains(\"Creating svg surface\") => {\n        eprintln!(\"export area too large or invalid: {e}\");\n        svg::gen_with_cairo(bounds.shrink_to_limit(), draw)?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Clamp export extents before creating the surface; tile very large exports","Reject NaN/infinite bounds early (assert_valid helps but check finiteness explicitly)","Mind cairo's surface size limit (~32767 px on many backends)","Log the wrapped cairo status code to distinguish dimension vs resource failures"],"tags":["cairo","svg","export","surface"],"backgroundTag":"invalid-argument-value","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}