* @param setFontLines whether to set underline and strikethrough as font style
* @return the PDF font for the specified attributes
*/
protected Font getFont(Map attributes, Locale locale, boolean setFontLines)
{
JRFont jrFont = new JRBaseFont(attributes);
Exception initialException = null;
Color forecolor = (Color)attributes.get(TextAttribute.FOREGROUND);
// use the same font scale ratio as in JRStyledText.getAwtAttributedString
float fontSizeScale = 1f;
Integer scriptStyle = (Integer) attributes.get(TextAttribute.SUPERSCRIPT);
if (scriptStyle != null && (
TextAttribute.SUPERSCRIPT_SUB.equals(scriptStyle)
|| TextAttribute.SUPERSCRIPT_SUPER.equals(scriptStyle)))
{
fontSizeScale = 2f / 3;
}
Font font = null;
PdfFont pdfFont = null;
FontKey key = new FontKey(jrFont.getFontName(), jrFont.isBold(), jrFont.isItalic());
if (fontMap != null && fontMap.containsKey(key))
{
pdfFont = (PdfFont) fontMap.get(key);
}
else
{
FontInfo fontInfo = JRFontUtil.getFontInfo(jrFont.getFontName(), locale);
if (fontInfo == null)
{
//fontName NOT found in font extensions
pdfFont = new PdfFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded());
}
else
{
//fontName found in font extensions
FontFamily family = fontInfo.getFontFamily();
FontFace face = fontInfo.getFontFace();
int faceStyle = java.awt.Font.PLAIN;
if (face == null)
{
//fontName matches family name in font extension
if (jrFont.isBold() && jrFont.isItalic())
{
face = family.getBoldItalicFace();
faceStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
}
if (face == null && jrFont.isBold())
{
face = family.getBoldFace();
faceStyle = java.awt.Font.BOLD;
}
if (face == null && jrFont.isItalic())
{
face = family.getItalicFace();
faceStyle = java.awt.Font.ITALIC;
}
if (face == null)
{
face = family.getNormalFace();
faceStyle = java.awt.Font.PLAIN;
}
// if (face == null)
// {
// throw new JRRuntimeException("Font family '" + family.getName() + "' does not have the normal font face.");
// }
}
else
{
//fontName matches face name in font extension; not family name
faceStyle = fontInfo.getStyle();
}
String pdfFontName = null;
int pdfFontStyle = java.awt.Font.PLAIN;
if (jrFont.isBold() && jrFont.isItalic())
{
pdfFontName = family.getBoldItalicPdfFont();
pdfFontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
}
if (pdfFontName == null && jrFont.isBold())
{
pdfFontName = family.getBoldPdfFont();
pdfFontStyle = java.awt.Font.BOLD;
}
if (pdfFontName == null && jrFont.isItalic())
{
pdfFontName = family.getItalicPdfFont();
pdfFontStyle = java.awt.Font.ITALIC;
}
if (pdfFontName == null)
{
pdfFontName = family.getNormalPdfFont();
pdfFontStyle = java.awt.Font.PLAIN;
}
if (pdfFontName == null)
{
//in theory, face file cannot be null here
pdfFontName = (face == null || face.getFile() == null ? jrFont.getPdfFontName() : face.getFile());
pdfFontStyle = faceStyle;//FIXMEFONT not sure this is correct, in case we inherit pdfFontName from default properties
}
// String ttf = face.getFile();
// if (ttf == null)
// {
// throw new JRRuntimeException("The '" + face.getName() + "' font face in family '" + family.getName() + "' returns a null file.");
// }
pdfFont =
new PdfFont(
pdfFontName,
family.getPdfEncoding() == null ? jrFont.getPdfEncoding() : family.getPdfEncoding(),
family.isPdfEmbedded() == null ? jrFont.isPdfEmbedded() : family.isPdfEmbedded().booleanValue(),
jrFont.isBold() && ((pdfFontStyle & java.awt.Font.BOLD) == 0),
jrFont.isItalic() && ((pdfFontStyle & java.awt.Font.ITALIC) == 0)
);
}
}
int pdfFontStyle = (pdfFont.isPdfSimulatedBold() ? Font.BOLD : 0)
| (pdfFont.isPdfSimulatedItalic() ? Font.ITALIC : 0);
if (setFontLines)
{
pdfFontStyle |= (jrFont.isUnderline() ? Font.UNDERLINE : 0)
| (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0);
}
try
{
font = FontFactory.getFont(
pdfFont.getPdfFontName(),
pdfFont.getPdfEncoding(),
pdfFont.isPdfEmbedded(),
jrFont.getFontSize() * fontSizeScale,
pdfFontStyle,
forecolor
);
// check if FontFactory didn't find the font
if (font.getBaseFont() == null && font.getFamily() == Font.UNDEFINED)
{
font = null;
}
}
catch(Exception e)
{
initialException = e;
}
if (font == null)
{
byte[] bytes = null;
try
{
bytes = JRLoader.loadBytesFromLocation(pdfFont.getPdfFontName(), classLoader, urlHandlerFactory, fileResolver);
}
catch(JRException e)
{
throw //NOPMD
new JRRuntimeException(
"Could not load the following font : "
+ "\npdfFontName : " + pdfFont.getPdfFontName()
+ "\npdfEncoding : " + pdfFont.getPdfEncoding()
+ "\nisPdfEmbedded : " + pdfFont.isPdfEmbedded(),
initialException
);
}
BaseFont baseFont = null;
try
{
baseFont =
BaseFont.createFont(
pdfFont.getPdfFontName(),
pdfFont.getPdfEncoding(),
pdfFont.isPdfEmbedded(),
true,
bytes,
null
);
}
catch(DocumentException e)
{
throw new JRRuntimeException(e);
}
catch(IOException e)
{
throw new JRRuntimeException(e);
}
font =
new Font(
baseFont,
jrFont.getFontSize() * fontSizeScale,
pdfFontStyle,
forecolor
);
}