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

#319 - NPE prevention in case of incorrect font configuration #320

Merged
merged 2 commits into from
Jan 27, 2019
Merged
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 @@ -296,14 +296,25 @@ public int getWidth(FontContext context, FSFont font, String string) {
float result = 0f;

try {
// First try using the first given font in the list.
result = ((PdfBoxFSFont) font).getFontDescription().get(0).getFont().getStringWidth(string) / 1000f * font.getSize2D();
if (((PdfBoxFSFont) font).getFontDescription() == null
|| ((PdfBoxFSFont) font).getFontDescription().isEmpty()) {
XRLog.render(Level.WARNING, "Font list is empty.");
} else {
// Go through the list of font descriptions
for (FontDescription fd : ((PdfBoxFSFont) font).getFontDescription()) {
if (fd.getFont() != null) {
result = fd.getFont().getStringWidth(string) / 1000f * font.getSize2D();
break;
} else {
XRLog.render(Level.WARNING, "Font is null.");
}
}
}
} catch (IllegalArgumentException e2) {
// PDFont::getStringWidth throws an IllegalArgumentException if the character doesn't exist in the font.
// So we do it one character by character instead.
result = getStringWidthSlow(font, string) / 1000f * font.getSize2D();
}
catch (IOException e) {
} catch (IOException e) {
throw new PdfContentStreamAdapter.PdfException("getWidth", e);
}

Expand Down