FontUtils { public static List<TextWithFont> resolveFonts(String label, PDFont font, PDDocument document) { PDFont currentFont = font; StringBuilder currentString = new StringBuilder(); List<TextWithFont> result = new ArrayList<>(); Iterator<Integer> codePointIterator = visualToLogical(label).codePoints().iterator(); while (codePointIterator.hasNext()) { int codePoint = codePointIterator.next(); String s = new String(Character.toChars(codePoint)); PDFont f = fontOrFallback(s, font, document); if (s.equals(" ")) { if (!FontUtils.canDisplaySpace(f)) { f = FontUtils.getStandardType1Font(StandardType1Font.HELVETICA); } if (f != currentFont) { if (currentString.length() > 0) { result.add(new TextWithFont(currentString.toString(), currentFont)); } result.add(new TextWithFont(" ", f)); currentString = new StringBuilder(); currentFont = f; } else { currentString.append(s); } } else if (currentFont == f) { currentString.append(s); } else { if (currentString.length() > 0) { result.add(new TextWithFont(currentString.toString(), currentFont)); } currentString = new StringBuilder(s); currentFont = f; } } result.add(new TextWithFont(currentString.toString(), currentFont)); for (TextWithFont each : result) { LOG.trace("Will write '{}' with {}", each.getText(), each.getFont()); } return result; } 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 resolveFontsWhenTextRepeats() { PDDocument doc = new PDDocument(); List<TextWithFont> textAndFonts = FontUtils.resolveFonts("123α456α789", HELVETICA, doc); assertThat(textAndFonts.get(0).getFont().getName(), is("Helvetica")); assertThat(textAndFonts.get(0).getText(), is("123")); assertThat(textAndFonts.get(1).getFont().getName(), is(not("Helvetica"))); assertThat(textAndFonts.get(1).getText(), is("α")); assertThat(textAndFonts.get(2).getFont().getName(), is("Helvetica")); assertThat(textAndFonts.get(2).getText(), is("456")); assertThat(textAndFonts.get(3).getFont().getName(), is(not("Helvetica"))); assertThat(textAndFonts.get(3).getText(), is("α")); }
@Test public void resolveFontsWhenSpaceIsNotSeparately() { PDDocument doc = new PDDocument(); List<TextWithFont> textAndFonts = FontUtils.resolveFonts("ab cd", HELVETICA, doc); assertThat(textAndFonts.get(0).getFont().getName(), is("Helvetica")); assertThat(textAndFonts.get(0).getText(), is("ab cd")); }