FontUtils { public static boolean canDisplay(String text, PDFont font) { if (font == null) return false; try { byte[] encoded = font.encode(removeWhitespace(text)); if (font instanceof PDVectorFont) { InputStream in = new ByteArrayInputStream(encoded); while (in.available() > 0) { int code = font.readCode(in); PDVectorFont vectorFont = (PDVectorFont) font; GeneralPath path = vectorFont.getPath(code); if (path == null || path.getBounds2D().getWidth() == 0) { return false; } } } return true; } catch (IllegalArgumentException | IOException | UnsupportedOperationException | NullPointerException e) { } return false; } private  FontUtils(); static PDType1Font getStandardType1Font(StandardType1Font st1Font); static PDFont fontOrFallback(String text, PDFont font, PDDocument document); static void clearLoadedFontCache(); static void clearLoadedFontCache(PDDocument document); static PDFont loadFont(PDDocument document, FontResource font); static final PDFont findFontFor(PDDocument document, String text); static boolean isOnlyWhitespace(String text); static String removeWhitespace(String text); static boolean canDisplaySpace(PDFont font); static boolean canDisplay(String text, PDFont font); static double calculateBBoxHeight(String text, PDFont font); static boolean isBold(PDFont font); static boolean isItalic(PDFont font); static List<String> wrapLines(String rawLabel, PDFont font, float fontSize, double maxWidth,
            PDDocument document); static double getSimpleStringWidth(String text, PDFont font, double fontSize); static List<TextWithFont> resolveFonts(String label, PDFont font, PDDocument document); static String removeUnsupportedCharacters(String text, PDDocument doc); static String replaceUnsupportedCharacters(String text, PDDocument doc, String replacement); static PDFont HELVETICA; static final FontResource[] TYPE0FONTS; }
@Test public void testCanDisplay() { assertTrue(canDisplay("Chuck", getStandardType1Font(StandardType1Font.HELVETICA))); assertFalse(canDisplay("कसौटी", getStandardType1Font(StandardType1Font.HELVETICA))); assertFalse(canDisplay("Chuck", null)); }
@Test public void testCanDisplayType0FontsThatDontThrow() throws TaskIOException, IOException { PDDocument doc = getTestDoc("pdf/2-up-sample.pdf"); PDResources res = doc.getPage(0).getResources(); PDFormXObject form = (PDFormXObject) res.getXObject(COSName.getPDFName("Form2")); PDResources formRes = form.getResources(); PDFont font = formRes.getFont(COSName.getPDFName("F0")); assertThat(font.getName(), is("Arial-BoldMT")); assertThat(FontUtils.canDisplay("Redacted out :)", font), is(false)); }