Skip to content

Commit

Permalink
Merge pull request #320 from brundipub/319_NPE_font_config
Browse files Browse the repository at this point in the history
Fixes #319 - NPE prevention in case of incorrect font configuration
  • Loading branch information
danfickle authored Jan 27, 2019
2 parents 44b7fee + d6f0bff commit 1d5284e
Showing 1 changed file with 15 additions and 4 deletions.
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

0 comments on commit 1d5284e

Please sign in to comment.