{"record":{"id":"17dae4c5bf4a2931","repo":"openjdk/jdk","slug":"error-cannot-fit-text-with-the-current-font-size","errorCode":null,"errorMessage":"ERROR: Cannot fit text with the current font size. Resize the window or use smaller font size.","messagePattern":"ERROR: Cannot fit text with the current font size\\. Resize the window or use smaller font size\\.","errorType":"exception","errorClass":"CannotDrawException","httpStatus":null,"severity":"warning","filePath":"src/demo/share/jfc/Font2DTest/FontPanel.java","lineNumber":784,"sourceCode":"            maxDescent = fm.getMaxDescent();\n            if (maxAscent == 0) maxAscent = 10;\n            if (maxDescent == 0) maxDescent = 5;\n            if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {\n                /// Give slight extra room for each character\n                maxAscent += 3;\n                maxDescent += 3;\n                gridWidth = fm.getMaxAdvance() + 6;\n                gridHeight = maxAscent + maxDescent;\n                if ( force16Cols )\n                  numCharAcross = 16;\n                else\n                  numCharAcross = ( w - 10 ) / gridWidth;\n                numCharDown = ( h - 10 ) / gridHeight;\n\n                canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;\n                canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;\n                if ( numCharDown == 0 || numCharAcross == 0 )\n                  throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );\n\n                if ( !isPrinting )\n                  resetScrollbar( verticalBar.getValue() * numCharAcross );\n            }\n            else {\n                maxDescent += fm.getLeading();\n                canvasInset_X = 5;\n                canvasInset_Y = 5;\n                /// gridWidth and numCharAcross will not be used in this mode...\n                gridHeight = maxAscent + maxDescent;\n                numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;\n\n                if ( numCharDown == 0 )\n                  throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );\n                /// If this is text loaded from file, prepares the LineBreak'ed\n                /// text layout at this point\n                if ( textToUse == FILE_TEXT ) {\n                    if ( !isPrinting )","sourceCodeStart":766,"sourceCodeEnd":802,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/demo/share/jfc/Font2DTest/FontPanel.java#L766-L802","documentation":"CannotDrawException (CANT_FIT_DRAW variant) from FontPanel's layout math: with the current font metrics, the drawing canvas is too small to fit even one column or one row of the character grid (numCharAcross or numCharDown computed as 0), so the panel cannot render the range.","triggerScenarios":"Range/grid mode with a very large font size, a very small window, or a font whose getMaxAdvance() makes gridWidth exceed the panel width — (w - 10) / gridWidth truncates to 0.","commonSituations":"Enlarging the font size slider in Font2DTest without resizing the window; maximizing font size in a small embedded frame; high max-advance fonts (e.g. wide CJK glyphs) in a narrow window.","solutions":["Enlarge the Font2DTest window so at least one glyph cell fits horizontally and vertically.","Reduce the font size in the demo UI.","Turn off 'Force 16 columns' if enabled, so numCharAcross adapts to the width."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before drawing the grid: verify one cell fits the canvas\nint gridW = fm.getMaxAdvance() + 6;\nint gridH = fm.getMaxAscent() + fm.getMaxDescent() + 3;\nif (w - 10 < gridW || h - 10 < gridH)\n    throw new CannotDrawException(CANT_FIT_DRAW); // fail before doing layout work","typeGuard":null,"tryCatchPattern":"try {\n    drawGrid(g2, w, h);\n} catch (CannotDrawException e) {\n    // display the reason in the status area; do not crash the paint loop\n    statusLabel.setText(e.getMessage());\n    g2.clearRect(0, 0, w, h);\n}","preventionTips":["Clamp the selectable font size to the current window dimensions in the UI.","Recompute layout on componentResized so the grid refits instead of throwing.","Catch CannotDrawException inside paint/print overrides — paint() must never throw."],"tags":["swing","font2dtest","layout","font-size","demo"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}