{"record":{"id":"3848c8abd5843575","repo":"zed-industries/zed","slug":"invalid-canvas-font-size","errorCode":null,"errorMessage":"invalid Canvas font size","messagePattern":"invalid Canvas font size","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui_web/src/text_system.rs","lineNumber":449,"sourceCode":"    }\n\n    fn recommended_rendering_mode(&self, font_id: FontId, font_size: Pixels) -> TextRenderingMode {\n        if self.canvas_font(font_id).is_some() {\n            TextRenderingMode::Grayscale\n        } else {\n            self.native.recommended_rendering_mode(font_id, font_size)\n        }\n    }\n\n    fn glyph_dilation_for_color(&self, color: Hsla) -> u8 {\n        self.native.glyph_dilation_for_color(color)\n    }\n}\n\nimpl CanvasFont {\n    fn css_font(&self, font_size: Pixels, color: bool) -> Result<String> {\n        let font_size = f32::from(font_size);\n        ensure!(\n            font_size.is_finite() && font_size > 0.,\n            \"invalid Canvas font size\"\n        );\n        let style = match self.descriptor.style {\n            FontStyle::Normal => \"normal\",\n            FontStyle::Italic => \"italic\",\n            FontStyle::Oblique => \"oblique\",\n        };\n        let mut families = Vec::new();\n        if !color && !self.descriptor.family.starts_with('.') {\n            families.push(quote_family(&self.descriptor.family));\n        }\n        if let Some(fallbacks) = &self.descriptor.fallbacks {\n            families.extend(\n                fallbacks\n                    .fallback_list()\n                    .iter()\n                    .map(|family| quote_family(family)),","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/zed-industries/zed/blob/916fc2b8cb3a815cbef4a3b40e13081be72036b6/crates/gpui_web/src/text_system.rs#L431-L467","documentation":"CanvasFont::css_font builds the CSS font shorthand string (e.g. \"italic 700 16px family\") that is passed to the browser's canvas text APIs. Canvas requires a finite, positive font size; if the incoming Pixels value is NaN, infinite, zero, or negative, the method throws this error instead of producing an invalid CSS font string that the browser would silently reject.","triggerScenarios":"Calling css_font (via measure, typographic_bounds, advance, or glyph rasterization paths on a Canvas font) with a font_size that is non-finite or <= 0 — e.g. NaN/inf from a failed layout computation, a zero size from an empty window/viewport, or negative sizes from sign errors in scaling code.","commonSituations":"A text style whose font_size was divided by a zero scale factor (NaN), a zoom/animation tween passing through 0, a viewport measurement of 0 before layout settles, or a misconfigured theme providing a non-positive default size.","solutions":["Clamp font_size at the call site: if !size.is_finite() || size <= 0.0, substitute a sane default before invoking text measurement","Trace where the NaN/zero/negative size originates (often a division by zero scale or an unmeasured parent)","Give text styles a positive default font size in config/theme instead of 0.0","Defer measurement until layout has produced real dimensions (e.g. skip measuring while viewport size is 0)"],"exampleFix":"// before\nlet font_size = style.font_size; // may be 0.0 or NaN\nlet metrics = text_system.layout_line(...)?;\n// after\nlet font_size = if style.font_size.is_finite() && style.font_size > px(0.0) {\n    style.font_size\n} else {\n    px(16.0) // sane default instead of invalid Canvas font size\n};\nlet metrics = text_system.layout_line(...)?;","handlingStrategy":"validation","validationCode":"fn is_valid_canvas_font_size(size: Pixels) -> bool {\n    size.0.is_finite() && size.0 > 0.0\n}\n// guard before any text measurement call\nif !is_valid_canvas_font_size(style.font_size) { style.font_size = px(16.0); }","typeGuard":"fn valid_font_size(size: Pixels) -> Option<Pixels> {\n    (size.0.is_finite() && size.0 > 0.0).then_some(size)\n}","tryCatchPattern":"match text_system.layout_line(line, font_size, ...) {\n    Ok(result) => result,\n    Err(err) if err.to_string().contains(\"invalid Canvas font size\") => {\n        // retry with a safe default size\n        text_system.layout_line(line, px(16.0), ...)?\n    }\n    Err(err) => return Err(err),\n}","preventionTips":["Never initialize text styles with font_size 0.0 or a computed value that can be NaN","Clamp scale/zoom-derived font sizes: size = raw_size.max(px(1.0))","Defer text measurement until parent layout has real (non-zero) dimensions","Unit-test theme/config parsing so a non-positive default size is caught early"],"tags":["fonts","canvas","validation","text-layout"],"backgroundTag":"invalid-argument-value","analyzedSha":"916fc2b8cb3a815cbef4a3b40e13081be72036b6","analyzedAt":"2026-09-19T19:09:50.599Z","contentChangedAt":"2026-09-19T19:09:50.599Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}