001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * IReportFont.java
028: *
029: * Created on 6 giugno 2003, 23.23
030: *
031: */
032:
033: package it.businesslogic.ireport;
034:
035: import java.awt.font.TextAttribute;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.Map;
039:
040: /**
041: *
042: * @author Administrator
043: */
044: public class IReportFont extends IReportHashMapBean {
045:
046: static public final String IS_DEFAULT_FONT = "defaultFont";
047: static public final String REPORT_FONT = "reportFont";
048: static public final String FONT_NAME = "fontName";
049: static public final String PDF_FONT_NAME = "PDFFontName";
050: static public final String FONT_SIZE = "fontSize";
051: static public final String TTF_FONT_NAME = "PDFFontName";
052: static public final String IS_BOLD = "bold";
053: static public final String IS_UNDERLINE = "underline";
054: static public final String IS_ITALIC = "italic";
055: static public final String IS_STRIKETROUGHT = "strikeTrought";
056: static public final String IS_PDF_EMBEDDED = "pdfEmbedded";
057: static public final String PDF_ENCODING = "pdfEncoding";
058:
059: static public final boolean DEFAULT_IS_DEFAULT_FONT = false;
060: static public final String DEFAULT_REPORT_FONT = "";
061: static public final String DEFAULT_FONT_NAME = "SansSerif";
062: static public final String DEFAULT_PDF_FONT_NAME = "Helvetica";
063: static public final int DEFAULT_FONT_SIZE = 10;
064: static public final String DEFAULT_TTF_FONT_NAME = "";
065: static public final boolean DEFAULT_IS_BOLD = false;
066: static public final boolean DEFAULT_IS_UNDERLINE = false;
067: static public final boolean DEFAULT_IS_ITALIC = false;
068: static public final boolean DEFAULT_IS_STRIKETROUGHT = false;
069: static public final boolean DEFAULT_IS_PDF_EMBEDDED = false;
070: static public final String DEFAULT_PDF_ENCODING = "Cp1252";
071:
072: /** Creates a new instance of IReportFont */
073: public IReportFont() {
074: }
075:
076: public Object clone() {
077: IReportFont newBean = new IReportFont();
078: return super .clone(newBean);
079: }
080:
081: /** Getter for property bold.
082: * @return Value of property bold.
083: *
084: */
085: public boolean isBold() {
086: return getBooleanValue(IS_BOLD, DEFAULT_IS_BOLD);
087: }
088:
089: public boolean isSet(String property) {
090: return (getBeanProperties().get(property) != null);
091: }
092:
093: /** Setter for property bold.
094: * @param bold New value of property bold.
095: *
096: */
097: public void setBold(boolean bold) {
098: getBeanProperties().put(IS_BOLD, "" + bold);
099: }
100:
101: /** Getter for property fontName.
102: * @return Value of property fontName.
103: *
104: */
105: public java.lang.String getFontName() {
106: return getStringValue(FONT_NAME, DEFAULT_FONT_NAME);
107: }
108:
109: /** Setter for property fontName.
110: * @param fontName New value of property fontName.
111: *
112: */
113: public void setFontName(java.lang.String fontName) {
114: getBeanProperties().put(FONT_NAME, fontName);
115: }
116:
117: /** Getter for property fontSize.
118: * @return Value of property fontSize.
119: *
120: */
121: public int getFontSize() {
122: return getIntValue(FONT_SIZE, DEFAULT_FONT_SIZE);
123: }
124:
125: /** Setter for property fontSize.
126: * @param fontSize New value of property fontSize.
127: *
128: */
129: public void setFontSize(int fontSize) {
130: getBeanProperties().put(FONT_SIZE, "" + fontSize);
131: }
132:
133: /** Getter for property italic.
134: * @return Value of property italic.
135: *
136: */
137: public boolean isItalic() {
138: return getBooleanValue(IS_ITALIC, DEFAULT_IS_ITALIC);
139: }
140:
141: /** Setter for property italic.
142: * @param italic New value of property italic.
143: *
144: */
145: public void setItalic(boolean italic) {
146: getBeanProperties().put(IS_ITALIC, "" + italic);
147: }
148:
149: /** Getter for property pdfEmbedded.
150: * @return Value of property pdfEmbedded.
151: *
152: */
153: public boolean isPdfEmbedded() {
154: return getBooleanValue(IS_PDF_EMBEDDED, DEFAULT_IS_PDF_EMBEDDED);
155: }
156:
157: /** Setter for property pdfEmbedded.
158: * @param pdfEmbedded New value of property pdfEmbedded.
159: *
160: */
161: public void setPdfEmbedded(boolean pdfEmbedded) {
162: getBeanProperties().put(IS_PDF_EMBEDDED, "" + pdfEmbedded);
163: }
164:
165: /** Getter for property pdfEncoding.
166: * @return Value of property pdfEncoding.
167: *
168: */
169: public java.lang.String getPdfEncoding() {
170: return getStringValue(PDF_ENCODING, DEFAULT_PDF_ENCODING);
171: }
172:
173: /** Setter for property pdfEncoding.
174: * @param pdfEncoding New value of property pdfEncoding.
175: *
176: */
177: public void setPdfEncoding(java.lang.String pdfEncoding) {
178: getBeanProperties().put(PDF_ENCODING, "" + pdfEncoding);
179: }
180:
181: /** Getter for property PDFFontName.
182: * @return Value of property PDFFontName.
183: *
184: */
185: public java.lang.String getPDFFontName() {
186: return getStringValue(PDF_FONT_NAME, DEFAULT_PDF_FONT_NAME);
187: }
188:
189: /** Setter for property PDFFontName.
190: * @param PDFFontName New value of property PDFFontName.
191: *
192: */
193: public void setPDFFontName(java.lang.String PDFFontName) {
194: getBeanProperties().put(PDF_FONT_NAME, "" + PDFFontName);
195: //see it.bussinesslogic.ireport.Report.java line 2041
196: if (PDFFontName != null
197: && PDFFontName.toUpperCase().endsWith(".TTF")) {
198: getBeanProperties().put(TTF_FONT_NAME, "" + PDFFontName);
199: }
200: }
201:
202: /** Getter for property reportFont.
203: * @return Value of property reportFont.
204: *
205: */
206: public java.lang.String getReportFont() {
207: return getStringValue(REPORT_FONT, DEFAULT_REPORT_FONT);
208: }
209:
210: /** Setter for property reportFont.
211: * @param reportFont New value of property reportFont.
212: *
213: */
214: public void setReportFont(java.lang.String reportFont) {
215: getBeanProperties().put(REPORT_FONT, "" + reportFont);
216: }
217:
218: /** Getter for property strikeTrought.
219: * @return Value of property strikeTrought.
220: *
221: */
222: public boolean isStrikeTrought() {
223: return getBooleanValue(IS_STRIKETROUGHT,
224: DEFAULT_IS_STRIKETROUGHT);
225: }
226:
227: /** Setter for property strikeTrought.
228: * @param strikeTrought New value of property strikeTrought.
229: *
230: */
231: public void setStrikeTrought(boolean strikeTrought) {
232: getBeanProperties().put(IS_STRIKETROUGHT, "" + strikeTrought);
233: }
234:
235: /** Getter for property TTFFont.
236: * @return Value of property TTFFont.
237: *
238: */
239: public java.lang.String getTTFFont() {
240: return getStringValue(TTF_FONT_NAME, DEFAULT_TTF_FONT_NAME);
241: }
242:
243: /** Setter for property TTFFont.
244: * @param TTFFont New value of property TTFFont.
245: *
246: */
247: public void setTTFFont(java.lang.String TTFFont) {
248: getBeanProperties().put(TTF_FONT_NAME, "" + TTFFont);
249: }
250:
251: /** Getter for property underline.
252: * @return Value of property underline.
253: *
254: */
255: public boolean isUnderline() {
256: return getBooleanValue(IS_UNDERLINE, DEFAULT_IS_UNDERLINE);
257: }
258:
259: /** Setter for property underline.
260: * @param underline New value of property underline.
261: *
262: */
263: public void setUnderline(boolean underline) {
264: getBeanProperties().put(IS_UNDERLINE, "" + underline);
265: }
266:
267: public String toString() {
268: return this .getReportFont();
269: }
270:
271: public String getDescription() {
272: return this .getFontName() + " " + this .getFontSize();
273: }
274:
275: /** Getter for property defaultFont.
276: * @return Value of property defaultFont.
277: *
278: */
279: public boolean isDefaultFont() {
280: return getBooleanValue(IS_DEFAULT_FONT, DEFAULT_IS_DEFAULT_FONT);
281: }
282:
283: /** Setter for property defaultFont.
284: * @param defaultFont New value of property defaultFont.
285: *
286: */
287: public void setDefaultFont(boolean defaultFont) {
288: getBeanProperties().put(IS_DEFAULT_FONT, "" + defaultFont);
289: }
290:
291: /** Convert to java.awt.Font.
292: */
293: public java.awt.Font getJavaAWTFont() {
294:
295: int style = java.awt.Font.PLAIN;
296: style = style & (isBold() ? java.awt.Font.BOLD : 0);
297: style = style & (isItalic() ? java.awt.Font.ITALIC : 0);
298:
299: java.awt.Font font = new java.awt.Font(getFontName(), style,
300: getFontSize());
301:
302: Map fontAttributes = font.getAttributes();
303: if (isUnderline()) {
304: fontAttributes.put(TextAttribute.UNDERLINE,
305: TextAttribute.UNDERLINE_ON);
306: }
307: if (isStrikeTrought()) {
308: fontAttributes.put(TextAttribute.STRIKETHROUGH,
309: TextAttribute.STRIKETHROUGH_ON);
310: }
311: return new java.awt.Font(fontAttributes);
312:
313: }
314:
315: /** Copy java.awt.Font common attributes.
316: */
317: public void setJavaAWTFont(java.awt.Font font) {
318:
319: //not logical name
320: setFontName(font.getFontName());
321:
322: setFontSize(font.getSize());
323:
324: int style = font.getStyle();
325: setBold((style & java.awt.Font.BOLD) > 0);
326: setItalic((style & java.awt.Font.ITALIC) > 0);
327:
328: Map fontAttributes = font.getAttributes();
329: setUnderline((fontAttributes
330: .containsKey(TextAttribute.UNDERLINE) && fontAttributes
331: .get(TextAttribute.UNDERLINE).equals(
332: TextAttribute.UNDERLINE_ON)));
333: setStrikeTrought((fontAttributes
334: .containsKey(TextAttribute.STRIKETHROUGH) && fontAttributes
335: .get(TextAttribute.STRIKETHROUGH).equals(
336: TextAttribute.STRIKETHROUGH_ON)));
337:
338: }
339:
340: public static boolean isTTFFont(String fontName) {
341: if (fontName.equals("Helvetica"))
342: return false;
343: if (fontName.equals("Helvetica-Bold"))
344: return false;
345: if (fontName.equals("Helvetica-BoldOblique"))
346: return false;
347: if (fontName.equals("Helvetica-Oblique"))
348: return false;
349: if (fontName.equals("Courier"))
350: return false;
351: if (fontName.equals("Courier-Bold"))
352: return false;
353: if (fontName.equals("Courier-BoldOblique"))
354: return false;
355: if (fontName.equals("Courier-Oblique"))
356: return false;
357: if (fontName.equals("Symbol"))
358: return false;
359: if (fontName.equals("Times-Roman"))
360: return false;
361: if (fontName.equals("Times-Bold"))
362: return false;
363: if (fontName.equals("Times-BoldItalic"))
364: return false;
365: if (fontName.equals("Times-Italic"))
366: return false;
367: if (fontName.equals("ZapfDingbats"))
368: return false;
369: if (fontName.equals("STSong-Light"))
370: return false;
371: if (fontName.equals("MHei-Medium"))
372: return false;
373: if (fontName.equals("MSung-Light"))
374: return false;
375: if (fontName.equals("HeiseiKakuGo-W5"))
376: return false;
377: if (fontName.equals("HeiseiMin-W3"))
378: return false;
379: if (fontName.equals("HYGoThic-Medium"))
380: return false;
381: if (fontName.equals("HYSMyeongJo-Medium"))
382: return false;
383: return true;
384: }
385:
386: /**
387: * Try to match the most appropriate pdf font
388: *
389: */
390: public static String matchFont(String fontName, boolean bold,
391: boolean italic) {
392: if (!isTTFFont(fontName)) {
393: if (fontName.startsWith("Helvetica") && !bold && !italic)
394: return "Helvetica";
395: if (fontName.startsWith("Helvetica") && bold && !italic)
396: return "Helvetica-Bold";
397: if (fontName.startsWith("Helvetica") && bold && italic)
398: return "Helvetica-BoldOblique";
399: if (fontName.startsWith("Helvetica") && !bold && italic)
400: return "Helvetica-Oblique";
401:
402: if (fontName.startsWith("Courier") && !bold && !italic)
403: return "Courier";
404: if (fontName.startsWith("Courier") && bold && !italic)
405: return "Courier-Bold";
406: if (fontName.startsWith("Courier") && bold && italic)
407: return "Courier-BoldOblique";
408: if (fontName.startsWith("Courier") && !bold && italic)
409: return "Courier-Oblique";
410:
411: if (fontName.startsWith("Times") && !bold && !italic)
412: return "Times-Roman";
413: if (fontName.startsWith("Times") && bold && !italic)
414: return "Times-Bold";
415: if (fontName.startsWith("Times") && bold && italic)
416: return "Times-BoldItalic";
417: if (fontName.startsWith("Times") && !bold && italic)
418: return "Times-Italic";
419:
420: }
421: return fontName;
422: }
423:
424: /**
425: * Set the more appropriate pdf font name reading the bold and italic properties
426: * of element.
427: */
428: public static boolean adjustPdfFontName(TextReportElement element) {
429: String newPdfFontName = matchFont(element.getPDFFontName(),
430: element.isBold(), element.isItalic());
431: if (newPdfFontName != null
432: && !newPdfFontName.equals(element.getPDFFontName())) {
433: element.setPDFFontName(newPdfFontName);
434: return true;
435: }
436:
437: return false;
438: }
439: }
|