{"record":{"id":"3e04d75bf5c385c8","repo":"openjdk/jdk","slug":"error-cannot-print-with-the-current-font-size-us","errorCode":null,"errorMessage":"ERROR: Cannot print with the current font size. Use smaller font size.","messagePattern":"ERROR: Cannot print with the current font size\\. 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_PRINT variant) from the same grid-fitting check as [18], but on the printing path: the printable page area cannot fit a single glyph cell at the current font size, so printing the range is impossible. Selected by the isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ternary.","triggerScenarios":"Printing from Font2DTest while the font size is too large for the page dimensions, or the printer/page setup (small paper, landscape/portrait mismatch) yields a page too narrow for one column or too short for one row.","commonSituations":"Printing a test range at 72pt+ font sizes; page setup changed to a small paper size; printer margins consuming nearly the whole page.","solutions":["Reduce the font size before printing.","In the print/page setup dialog, choose larger paper or the orientation that maximizes usable area.","Print in a mode other than the full grid (e.g. the one-glyph view) if the range must stay large."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before printing: check the page fits at least one glyph cell\nPrinterJob job = PrinterJob.getPrinterJob();\nPageFormat pf = job.defaultPage();\nint usableW = (int) pf.getImageableWidth();\nint usableH = (int) pf.getImageableHeight();\nint cellW = fm.getMaxAdvance() + 6, cellH = fm.getMaxAscent() + fm.getMaxDescent() + 3;\nif (usableW < cellW || usableH < cellH)\n    JOptionPane.showMessageDialog(frame, \"Font size too large for the page; reduce it before printing.\");","typeGuard":null,"tryCatchPattern":"try {\n    fontPanel.print();\n} catch (CannotDrawException e) {\n    // printing path: abort the print job cleanly with a user-facing message\n    job.cancel();\n    JOptionPane.showMessageDialog(frame, e.getMessage());\n} catch (PrinterException e) {\n    logger.error(\"print failed\", e);\n}","preventionTips":["Derive the print font size from PageFormat.getImageableWidth()/Height(), not from the screen canvas.","Offer a print preview so oversize fonts are caught before the print dialog.","Handle CannotDrawException in Printable.print() implementations and return Printable.NO_SUCH_PAGE instead of throwing."],"tags":["swing","font2dtest","printing","font-size","demo"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}