openjdk/jdk · warning · CannotDrawException

ERROR: Cannot fit text with the current font size. Resize th

Error message

ERROR: Cannot fit text with the current font size. Resize the window or use smaller font size.

What it means

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.

Source

Thrown at src/demo/share/jfc/Font2DTest/FontPanel.java:784

            maxDescent = fm.getMaxDescent();
            if (maxAscent == 0) maxAscent = 10;
            if (maxDescent == 0) maxDescent = 5;
            if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
                /// Give slight extra room for each character
                maxAscent += 3;
                maxDescent += 3;
                gridWidth = fm.getMaxAdvance() + 6;
                gridHeight = maxAscent + maxDescent;
                if ( force16Cols )
                  numCharAcross = 16;
                else
                  numCharAcross = ( w - 10 ) / gridWidth;
                numCharDown = ( h - 10 ) / gridHeight;

                canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
                canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
                if ( numCharDown == 0 || numCharAcross == 0 )
                  throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );

                if ( !isPrinting )
                  resetScrollbar( verticalBar.getValue() * numCharAcross );
            }
            else {
                maxDescent += fm.getLeading();
                canvasInset_X = 5;
                canvasInset_Y = 5;
                /// gridWidth and numCharAcross will not be used in this mode...
                gridHeight = maxAscent + maxDescent;
                numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;

                if ( numCharDown == 0 )
                  throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
                /// If this is text loaded from file, prepares the LineBreak'ed
                /// text layout at this point
                if ( textToUse == FILE_TEXT ) {
                    if ( !isPrinting )

View on GitHub (pinned to 88dfb74bbe)

Solutions

  1. Enlarge the Font2DTest window so at least one glyph cell fits horizontally and vertically.
  2. Reduce the font size in the demo UI.
  3. Turn off 'Force 16 columns' if enabled, so numCharAcross adapts to the width.
Defensive patterns

Strategy: validation

Validate before calling

// before drawing the grid: verify one cell fits the canvas
int gridW = fm.getMaxAdvance() + 6;
int gridH = fm.getMaxAscent() + fm.getMaxDescent() + 3;
if (w - 10 < gridW || h - 10 < gridH)
    throw new CannotDrawException(CANT_FIT_DRAW); // fail before doing layout work

Try / catch

try {
    drawGrid(g2, w, h);
} catch (CannotDrawException e) {
    // display the reason in the status area; do not crash the paint loop
    statusLabel.setText(e.getMessage());
    g2.clearRect(0, 0, w, h);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of openjdk/jdk@88dfb74bbe (2026-08-14). Data as JSON: /api/errors/17dae4c5bf4a2931. Report an issue: GitHub.