1
24 package net.sf.jasperreports.engine.fonts;
25
26 import java.awt.Font;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import net.sf.jasperreports.engine.DefaultJasperReportsContext;
32 import net.sf.jasperreports.engine.JRCloneable;
33 import net.sf.jasperreports.engine.JRFont;
34 import net.sf.jasperreports.engine.JRPropertiesUtil;
35 import net.sf.jasperreports.engine.JRRuntimeException;
36 import net.sf.jasperreports.engine.JasperReportsContext;
37 import net.sf.jasperreports.engine.util.JRStyledText;
38
39
40
41
44 public class SimpleFontFace implements FontFace, JRCloneable
45 {
46
47 private static final Log log = LogFactory.getLog(SimpleFontFace.class);
48
49
52 private JasperReportsContext jasperReportsContext;
53 private String ttf;
54 private Font font;
55 private String pdf;
56 private String eot;
57 private String svg;
58 private String woff;
59
60 @Override
61 public Object clone() {
62 try
63 {
64 return super.clone();
65 } catch (CloneNotSupportedException e)
66 {
67 throw new JRRuntimeException(e);
68 }
69 }
70
73 public static SimpleFontFace getInstance(JasperReportsContext jasperReportsContext, String fontName)
74 {
75 SimpleFontFace fontFace = null;
76
77 if (fontName != null)
78 {
79 fontFace = new SimpleFontFace(jasperReportsContext);
80 fontFace.setTtf(fontName);
81 }
82
83 return fontFace;
84 }
85
86
87
90 public SimpleFontFace(JasperReportsContext jasperReportsContext, String ttf)
91 {
92 this(jasperReportsContext);
93 setTtf(ttf);
94 }
95
96
97
100 public SimpleFontFace(String file)
101 {
102 this(DefaultJasperReportsContext.getInstance());
103 setTtf(file);
104 }
105
106
107
110 public SimpleFontFace(Font font)
111 {
112 this.font = font;
113 }
114
115
116
119 public SimpleFontFace(JasperReportsContext jasperReportsContext)
120 {
121 this.jasperReportsContext = jasperReportsContext;
122 }
123
124
125 @Override
126 public String getName()
127 {
128
129 return font == null ? null : font.getName();
130 }
131
132
135 @Override
136 public String getFile()
137 {
138 return getTtf();
139 }
140
141 @Override
142 public String getTtf()
143 {
144 return ttf;
145 }
146
147
150 public void setTtf(String ttf)
151 {
152 setTtf(ttf, true);
153 }
154
155 public void setTtf(String ttf, boolean load)
156 {
157 this.ttf = ttf;
158 this.font = null;
159
160 if (load)
161 {
162 loadFont();
163 }
164 }
165
166 public void loadFont() throws InvalidFontException
167 {
168 if (ttf != null && font == null)
169 {
170 if (log.isDebugEnabled())
171 {
172 log.debug("Loading font " + ttf);
173 }
174
175 String upperCaseTtf = ttf.trim().toUpperCase();
176 if (
177 upperCaseTtf.endsWith(".TTF")
178 || upperCaseTtf.endsWith(".OTF")
179 )
180 {
181 font = AwtFontManager.instance().getAwtFont(jasperReportsContext, ttf);
182 }
183 else
184 {
185 FontUtil.getInstance(jasperReportsContext).checkAwtFont(ttf, JRPropertiesUtil.getInstance(jasperReportsContext).getBooleanProperty(JRStyledText.PROPERTY_AWT_IGNORE_MISSING_FONT));
186
187 font = new Font(ttf, Font.PLAIN, JRPropertiesUtil.getInstance(jasperReportsContext).getIntegerProperty(JRFont.DEFAULT_FONT_SIZE));
188 }
189 }
190 }
191
192 @Override
193 public Font getFont()
194 {
195 return font;
196 }
197
198 @Override
199 public String getPdf()
200 {
201 return pdf;
202 }
203
204
207 public void setPdf(String pdf)
208 {
209 this.pdf = pdf;
210 }
211
212 @Override
213 public String getEot()
214 {
215 return eot;
216 }
217
218
221 public void setEot(String eot)
222 {
223 this.eot = eot;
224 }
225
226 @Override
227 public String getSvg()
228 {
229 return svg;
230 }
231
232
235 public void setSvg(String svg)
236 {
237 this.svg = svg;
238 }
239
240 @Override
241 public String getWoff()
242 {
243 return woff;
244 }
245
246
249 public void setWoff(String woff)
250 {
251 this.woff = woff;
252 }
253
254 @Override
255 protected void finalize()
256 {
257 AwtFontManager.instance().purgeFontFiles();
258 }
259
260
261 }
262