KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > IReportFont


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * IReportFont.java
28  *
29  * Created on 6 giugno 2003, 23.23
30  *
31  */

32
33 package it.businesslogic.ireport;
34
35 import java.awt.font.TextAttribute JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  *
42  * @author Administrator
43  */

44 public class IReportFont extends IReportHashMapBean {
45     
46     static public final String JavaDoc IS_DEFAULT_FONT = "defaultFont";
47     static public final String JavaDoc REPORT_FONT = "reportFont";
48     static public final String JavaDoc FONT_NAME = "fontName";
49     static public final String JavaDoc PDF_FONT_NAME = "PDFFontName";
50     static public final String JavaDoc FONT_SIZE = "fontSize";
51     static public final String JavaDoc TTF_FONT_NAME = "PDFFontName";
52     static public final String JavaDoc IS_BOLD = "bold";
53     static public final String JavaDoc IS_UNDERLINE = "underline";
54     static public final String JavaDoc IS_ITALIC = "italic";
55     static public final String JavaDoc IS_STRIKETROUGHT = "strikeTrought";
56     static public final String JavaDoc IS_PDF_EMBEDDED = "pdfEmbedded";
57     static public final String JavaDoc PDF_ENCODING = "pdfEncoding";
58     
59     static public final boolean DEFAULT_IS_DEFAULT_FONT = false;
60     static public final String JavaDoc DEFAULT_REPORT_FONT = "";
61     static public final String JavaDoc DEFAULT_FONT_NAME = "SansSerif";
62     static public final String JavaDoc DEFAULT_PDF_FONT_NAME = "Helvetica";
63     static public final int DEFAULT_FONT_SIZE = 10;
64     static public final String JavaDoc DEFAULT_TTF_FONT_NAME = "";
65     static public final boolean DEFAULT_IS_BOLD = false;
66     static public final boolean DEFAULT_IS_UNDERLINE = false;
67     static public final boolean DEFAULT_IS_ITALIC = false;
68     static public final boolean DEFAULT_IS_STRIKETROUGHT = false;
69     static public final boolean DEFAULT_IS_PDF_EMBEDDED = false;
70     static public final String JavaDoc DEFAULT_PDF_ENCODING = "Cp1252";
71     
72     
73     
74     /** Creates a new instance of IReportFont */
75     public IReportFont() {
76     }
77     
78     public Object JavaDoc clone()
79     {
80             IReportFont newBean = new IReportFont();
81             return super.clone(newBean);
82     }
83         
84     /** Getter for property bold.
85      * @return Value of property bold.
86      *
87      */

88     public boolean isBold() {
89         return getBooleanValue( IS_BOLD, DEFAULT_IS_BOLD );
90     }
91     
92     public boolean isSet(String JavaDoc property)
93     {
94         return (getBeanProperties().get(property) != null);
95     }
96     
97     /** Setter for property bold.
98      * @param bold New value of property bold.
99      *
100      */

101     public void setBold(boolean bold) {
102         getBeanProperties().put(IS_BOLD, ""+bold);
103     }
104     
105     /** Getter for property fontName.
106      * @return Value of property fontName.
107      *
108      */

109     public java.lang.String JavaDoc getFontName() {
110         return getStringValue(FONT_NAME, DEFAULT_FONT_NAME);
111     }
112     
113     /** Setter for property fontName.
114      * @param fontName New value of property fontName.
115      *
116      */

117     public void setFontName(java.lang.String JavaDoc fontName) {
118         getBeanProperties().put(FONT_NAME, fontName);
119     }
120     
121     /** Getter for property fontSize.
122      * @return Value of property fontSize.
123      *
124      */

125     public int getFontSize() {
126         return getIntValue(FONT_SIZE, DEFAULT_FONT_SIZE);
127     }
128     
129     /** Setter for property fontSize.
130      * @param fontSize New value of property fontSize.
131      *
132      */

133     public void setFontSize(int fontSize) {
134         getBeanProperties().put(FONT_SIZE, ""+fontSize);
135     }
136     
137     /** Getter for property italic.
138      * @return Value of property italic.
139      *
140      */

141     public boolean isItalic() {
142         return getBooleanValue( IS_ITALIC, DEFAULT_IS_ITALIC );
143     }
144     
145     /** Setter for property italic.
146      * @param italic New value of property italic.
147      *
148      */

149     public void setItalic(boolean italic) {
150        getBeanProperties().put(IS_ITALIC, ""+italic);
151     }
152     
153     /** Getter for property pdfEmbedded.
154      * @return Value of property pdfEmbedded.
155      *
156      */

157     public boolean isPdfEmbedded() {
158         return getBooleanValue( IS_PDF_EMBEDDED, DEFAULT_IS_PDF_EMBEDDED );
159     }
160     
161     /** Setter for property pdfEmbedded.
162      * @param pdfEmbedded New value of property pdfEmbedded.
163      *
164      */

165     public void setPdfEmbedded(boolean pdfEmbedded) {
166         getBeanProperties().put(IS_PDF_EMBEDDED, ""+pdfEmbedded);
167     }
168     
169     /** Getter for property pdfEncoding.
170      * @return Value of property pdfEncoding.
171      *
172      */

173     public java.lang.String JavaDoc getPdfEncoding() {
174         return getStringValue(PDF_ENCODING, DEFAULT_PDF_ENCODING);
175     }
176     
177     /** Setter for property pdfEncoding.
178      * @param pdfEncoding New value of property pdfEncoding.
179      *
180      */

181     public void setPdfEncoding(java.lang.String JavaDoc pdfEncoding) {
182         getBeanProperties().put(PDF_ENCODING, ""+pdfEncoding);
183     }
184     
185     /** Getter for property PDFFontName.
186      * @return Value of property PDFFontName.
187      *
188      */

189     public java.lang.String JavaDoc getPDFFontName() {
190         return getStringValue(PDF_FONT_NAME, DEFAULT_PDF_FONT_NAME);
191     }
192     
193     /** Setter for property PDFFontName.
194      * @param PDFFontName New value of property PDFFontName.
195      *
196      */

197     public void setPDFFontName(java.lang.String JavaDoc PDFFontName) {
198         getBeanProperties().put(PDF_FONT_NAME, ""+PDFFontName);
199     //see it.bussinesslogic.ireport.Report.java line 2041
200
if(PDFFontName != null && PDFFontName.toUpperCase().endsWith(".TTF")){
201                 getBeanProperties().put(TTF_FONT_NAME, ""+PDFFontName);
202     }
203     }
204     
205     /** Getter for property reportFont.
206      * @return Value of property reportFont.
207      *
208      */

209     public java.lang.String JavaDoc getReportFont() {
210         return getStringValue(REPORT_FONT, DEFAULT_REPORT_FONT);
211     }
212     
213     /** Setter for property reportFont.
214      * @param reportFont New value of property reportFont.
215      *
216      */

217     public void setReportFont(java.lang.String JavaDoc reportFont) {
218         getBeanProperties().put(REPORT_FONT, ""+reportFont);
219     }
220     
221     /** Getter for property strikeTrought.
222      * @return Value of property strikeTrought.
223      *
224      */

225     public boolean isStrikeTrought() {
226         return getBooleanValue( IS_STRIKETROUGHT, DEFAULT_IS_STRIKETROUGHT );
227     }
228     
229     /** Setter for property strikeTrought.
230      * @param strikeTrought New value of property strikeTrought.
231      *
232      */

233     public void setStrikeTrought(boolean strikeTrought) {
234         getBeanProperties().put(IS_STRIKETROUGHT, ""+strikeTrought);
235     }
236     
237     /** Getter for property TTFFont.
238      * @return Value of property TTFFont.
239      *
240      */

241     public java.lang.String JavaDoc getTTFFont() {
242         return getStringValue(TTF_FONT_NAME, DEFAULT_TTF_FONT_NAME);
243     }
244     
245     /** Setter for property TTFFont.
246      * @param TTFFont New value of property TTFFont.
247      *
248      */

249     public void setTTFFont(java.lang.String JavaDoc TTFFont) {
250         getBeanProperties().put(TTF_FONT_NAME, ""+TTFFont);
251     }
252     
253     /** Getter for property underline.
254      * @return Value of property underline.
255      *
256      */

257     public boolean isUnderline() {
258         return getBooleanValue( IS_UNDERLINE, DEFAULT_IS_UNDERLINE );
259     }
260     
261     /** Setter for property underline.
262      * @param underline New value of property underline.
263      *
264      */

265     public void setUnderline(boolean underline) {
266         getBeanProperties().put(IS_UNDERLINE, ""+underline);
267     }
268         
269     public String JavaDoc toString()
270     {
271         return this.getReportFont();
272     }
273     
274     public String JavaDoc getDescription()
275     {
276         return this.getFontName()+" "+this.getFontSize();
277     }
278     
279     /** Getter for property defaultFont.
280      * @return Value of property defaultFont.
281      *
282      */

283     public boolean isDefaultFont() {
284         return getBooleanValue( IS_DEFAULT_FONT, DEFAULT_IS_DEFAULT_FONT );
285     }
286     
287     /** Setter for property defaultFont.
288      * @param defaultFont New value of property defaultFont.
289      *
290      */

291     public void setDefaultFont(boolean defaultFont) {
292         getBeanProperties().put(IS_DEFAULT_FONT, ""+defaultFont);
293     }
294     
295     
296     /** Convert to java.awt.Font.
297     */

298     public java.awt.Font JavaDoc getJavaAWTFont(){
299
300              int style = java.awt.Font.PLAIN;
301              style = style & (isBold() ? java.awt.Font.BOLD : 0);
302              style = style & (isItalic() ? java.awt.Font.ITALIC : 0);
303
304              java.awt.Font JavaDoc font = new java.awt.Font JavaDoc(getFontName(), style, getFontSize());
305
306              Map JavaDoc fontAttributes = font.getAttributes();
307              if (isUnderline()){
308                     fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
309              }
310              if (isStrikeTrought()){
311                     fontAttributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
312              }
313              return new java.awt.Font JavaDoc(fontAttributes);
314
315     }
316
317     /** Copy java.awt.Font common attributes.
318     */

319     public void setJavaAWTFont(java.awt.Font JavaDoc font){
320         
321         //not logical name
322
setFontName( font.getFontName() );
323         
324         setFontSize( font.getSize() );
325     
326         int style = font.getStyle();
327         setBold( (style & java.awt.Font.BOLD) > 0 );
328         setItalic( (style & java.awt.Font.ITALIC) > 0);
329     
330         Map JavaDoc fontAttributes = font.getAttributes();
331         setUnderline( (fontAttributes.containsKey(TextAttribute.UNDERLINE) && fontAttributes.get(TextAttribute.UNDERLINE).equals(TextAttribute.UNDERLINE_ON)));
332         setStrikeTrought( (fontAttributes.containsKey(TextAttribute.STRIKETHROUGH) && fontAttributes.get(TextAttribute.STRIKETHROUGH).equals(TextAttribute.STRIKETHROUGH_ON)));
333     
334     }
335         
336         
337         public static boolean isTTFFont(String JavaDoc fontName)
338         {
339             if (fontName.equals("Helvetica")) return false;
340             if (fontName.equals("Helvetica-Bold")) return false;
341             if (fontName.equals("Helvetica-BoldOblique")) return false;
342             if (fontName.equals("Helvetica-Oblique")) return false;
343             if (fontName.equals("Courier")) return false;
344             if (fontName.equals("Courier-Bold")) return false;
345             if (fontName.equals("Courier-BoldOblique")) return false;
346             if (fontName.equals("Courier-Oblique")) return false;
347             if (fontName.equals("Symbol")) return false;
348             if (fontName.equals("Times-Roman")) return false;
349             if (fontName.equals("Times-Bold")) return false;
350             if (fontName.equals("Times-BoldItalic")) return false;
351             if (fontName.equals("Times-Italic")) return false;
352             if (fontName.equals("ZapfDingbats")) return false;
353             if (fontName.equals("STSong-Light")) return false;
354             if (fontName.equals("MHei-Medium")) return false;
355             if (fontName.equals("MSung-Light")) return false;
356             if (fontName.equals("HeiseiKakuGo-W5")) return false;
357             if (fontName.equals("HeiseiMin-W3")) return false;
358             if (fontName.equals("HYGoThic-Medium")) return false;
359             if (fontName.equals("HYSMyeongJo-Medium")) return false;
360             return true;
361         }
362         
363         
364         /**
365          * Try to match the most appropriate pdf font
366          *
367          */

368         public static String JavaDoc matchFont(String JavaDoc fontName, boolean bold, boolean italic)
369         {
370             if ( !isTTFFont( fontName ))
371             {
372                 if (fontName.startsWith("Helvetica") && !bold && !italic) return "Helvetica";
373                 if (fontName.startsWith("Helvetica") && bold && !italic) return "Helvetica-Bold";
374                 if (fontName.startsWith("Helvetica") && bold && italic) return "Helvetica-BoldOblique";
375                 if (fontName.startsWith("Helvetica") && !bold && italic) return "Helvetica-Oblique";
376                 
377                 if (fontName.startsWith("Courier") && !bold && !italic) return "Courier";
378                 if (fontName.startsWith("Courier") && bold && !italic) return "Courier-Bold";
379                 if (fontName.startsWith("Courier") && bold && italic) return "Courier-BoldOblique";
380                 if (fontName.startsWith("Courier") && !bold && italic) return "Courier-Oblique";
381                 
382                 if (fontName.startsWith("Times") && !bold && !italic) return "Times-Roman";
383                 if (fontName.startsWith("Times") && bold && !italic) return "Times-Bold";
384                 if (fontName.startsWith("Times") && bold && italic) return "Times-BoldItalic";
385                 if (fontName.startsWith("Times") && !bold && italic) return "Times-Italic";
386                 
387             }
388             return fontName;
389        }
390        
391         
392         /**
393         * Set the more appropriate pdf font name reading the bold and italic properties
394         * of element.
395         */

396         public static boolean adjustPdfFontName(TextReportElement element)
397         {
398                String JavaDoc newPdfFontName = matchFont(element.getPDFFontName(), element.isBold(), element.isItalic());
399                if (newPdfFontName != null && !newPdfFontName.equals(element.getPDFFontName()))
400                {
401                    element.setPDFFontName(newPdfFontName);
402                    return true;
403                }
404                
405                return false;
406         }
407 }
408
Popular Tags