Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDFBOX-3812] Support auto size font for multiline PDTextField #78

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ class AppearanceGeneratorHelper
/**
* The default font size used for multiline text
*/
private static final float DEFAULT_FONT_SIZE = 12;
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.
*/
Expand Down Expand Up @@ -775,9 +776,6 @@ private void writeToStream(byte[] data, PDAppearanceStream appearanceStream) thr
}

/**
* My "not so great" method for calculating the fontsize. It does not work superb, but it
* handles ok.
*
* @return the calculated font-size
* @throws IOException If there is an error getting the font information.
*/
Expand All @@ -793,31 +791,31 @@ private float calculateFontSize(PDFont font, PDRectangle contentRect) throws IOE
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;
}
Expand Down