1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

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 /**
42  * @author Teodor Danciu (teodord@users.sourceforge.net)
43  */

44 public class SimpleFontFace implements FontFace, JRCloneable
45 {
46
47     private static final Log log = LogFactory.getLog(SimpleFontFace.class);
48     
49     /**
50      * 
51      */

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     /**
71      * @deprecated Replaced by {@link #SimpleFontFace(JasperReportsContext)} and {@link #setTtf(String)}.
72      */

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     /**
88      * @deprecated Replaced by #{@link #SimpleFontFace(JasperReportsContext)} and {@link #setTtf(String)}.
89      */

90     public SimpleFontFace(JasperReportsContext jasperReportsContext, String ttf)
91     {
92         this(jasperReportsContext);
93         setTtf(ttf);
94     }
95     
96
97     /**
98      * @deprecated Replaced by #{@link #SimpleFontFace(JasperReportsContext)} and {@link #setTtf(String)}.
99      */

100     public SimpleFontFace(String file)
101     {
102         this(DefaultJasperReportsContext.getInstance());
103         setTtf(file);
104     }
105
106     
107     /**
108      * @deprecated To be removed.
109      */

110     public SimpleFontFace(Font font)
111     {
112         this.font = font;
113     }
114
115     
116     /**
117      * 
118      */

119     public SimpleFontFace(JasperReportsContext jasperReportsContext)
120     {
121         this.jasperReportsContext = jasperReportsContext;
122     }
123
124     
125     @Override
126     public String getName()
127     {
128         //(String)font.getAttributes().get(TextAttribute.FAMILY);
129         return font == null ? null : font.getName();
130     }
131     
132     /**
133      * @deprecated Replaced by {@link #getTtf()}.
134      */

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     /**
148      * 
149      */

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     /**
205      * 
206      */

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     /**
219      * 
220      */

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     /**
233      * 
234      */

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     /**
247      * 
248      */

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