Author: msahyoun Date: Mon Dec 23 21:40:37 2019 New Revision: 1871930 URL: http://svn.apache.org/viewvc?rev=1871930&view=rev Log: PDFBOX-3812: enhanced auto font size calculation by dannymcpherson in #78
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1871930&r1=1871929&r2=1871930&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Mon Dec 23 21:40:37 2019 @@ -82,9 +82,10 @@ class AppearanceGeneratorHelper private static final float DEFAULT_FONT_SIZE = 12; /** - * The minimum font size used for multiline text auto sizing + * The minimum/maximum font sizes used for multiline text auto sizing */ - private static final float MINIMUM_FONT_SIZE = 4; + private static final float MINIMUM_FONT_SIZE = 4; + private static final float MAXIMUM_FONT_SIZE = 300; /** * The default padding applied by Acrobat to the fields bbox. @@ -793,31 +794,31 @@ class AppearanceGeneratorHelper PlainText textContent = new PlainText(value); if (textContent.getParagraphs() != null) { - float fs = DEFAULT_FONT_SIZE; - while (fs > MINIMUM_FONT_SIZE) + float width = contentRect.getWidth() - contentRect.getLowerLeftX(); + float fs = MINIMUM_FONT_SIZE; + while (fs <= MAXIMUM_FONT_SIZE) { // determine the number of lines needed for this font and contentRect int numLines = 0; for (PlainText.Paragraph paragraph : textContent.getParagraphs()) { - numLines += paragraph.getLines(font, fs, contentRect.getWidth()).size(); + numLines += paragraph.getLines(font, fs, width).size(); } - // calculate the height using the capHeight and leading for this font size + // calculate the height required for this font size float fontScaleY = fs / FONTSCALE; - float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY; float leading = font.getBoundingBox().getHeight() * fontScaleY; - float height = fontCapAtSize + ((numLines - 1) * leading); + float height = leading * numLines; - // if within bounds, return this font size - if (height <= contentRect.getHeight()) + // if this font size didn't fit, use the prior size that did fit + if (height > contentRect.getHeight()) { - return fs; + return Math.max(fs - 1, MINIMUM_FONT_SIZE); } - - // otherwise, try again with a smaller font - fs -= 1; + fs++; } + return Math.min(fs, MAXIMUM_FONT_SIZE); } + // Acrobat defaults to 12 for multiline text with size 0 return DEFAULT_FONT_SIZE; }