1
24 package net.sf.jasperreports.export.pdf.classic;
25
26 import java.awt.Color;
27 import java.io.IOException;
28
29 import com.lowagie.text.DocumentException;
30 import com.lowagie.text.Font;
31 import com.lowagie.text.FontFactory;
32 import com.lowagie.text.pdf.BaseFont;
33
34 import net.sf.jasperreports.engine.JRRuntimeException;
35 import net.sf.jasperreports.export.pdf.FontRecipient;
36 import net.sf.jasperreports.export.pdf.PdfFontStyle;
37
38
42 public class ClassicFontRecipient implements FontRecipient
43 {
44
45 private Font font;
46
47 public ClassicFontRecipient()
48 {
49 }
50
51 @Override
52 public boolean hasFont()
53 {
54 return font != null;
55 }
56
57 public Font getFont()
58 {
59 return font;
60 }
61
62 @Override
63 public void setFont(String pdfFontName, String pdfEncoding, boolean isPdfEmbedded,
64 float size, PdfFontStyle pdfFontStyle, Color forecolor)
65 {
66 Font font = FontFactory.getFont(pdfFontName, pdfEncoding, isPdfEmbedded,
67 size, toITextFontStyle(pdfFontStyle), forecolor);
68
69 if (font != null && font.getBaseFont() == null && font.getFamily() == Font.UNDEFINED)
70 {
71 font = null;
72 }
73 this.font = font;
74 }
75
76 @Override
77 public void setFont(String pdfFontName, String pdfEncoding, boolean isPdfEmbedded,
78 float size, PdfFontStyle pdfFontStyle, Color forecolor,
79 byte[] fontData)
80 {
81 BaseFont baseFont;
82 try
83 {
84 baseFont = BaseFont.createFont(pdfFontName, pdfEncoding, isPdfEmbedded,
85 true, fontData, null);
86 }
87 catch(DocumentException e)
88 {
89 throw new JRRuntimeException(e);
90 }
91 catch(IOException e)
92 {
93 throw new JRRuntimeException(e);
94 }
95
96 font = new Font(baseFont, size, toITextFontStyle(pdfFontStyle), forecolor);
97 }
98
99 protected static int toITextFontStyle(PdfFontStyle pdfFontStyle)
100 {
101 return (pdfFontStyle.isBold() ? Font.BOLD : 0)
102 | (pdfFontStyle.isItalic() ? Font.ITALIC : 0)
103 | (pdfFontStyle.isUnderline() ? Font.UNDERLINE : 0)
104 | (pdfFontStyle.isStrikethrough() ? Font.STRIKETHRU : 0);
105 }
106
107 }
108