001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * FontDefinition.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.style;
030:
031: import java.awt.Font;
032: import java.io.Serializable;
033:
034: import org.jfree.util.StringUtils;
035:
036: /**
037: * The FontDefinition encapsulates all Font-Style information. The java.awt.Font class
038: * does not support extended Styles like Strikethrough or Underline or font metadata like
039: * the base encoding.
040: *
041: * @author Thomas Morgner
042: * @deprecated use single properties instead.
043: */
044: public class FontDefinition implements Serializable, Cloneable {
045: /**
046: * a constant to draw a font in bold style.
047: */
048: public static final boolean BOLD = true;
049:
050: /**
051: * a constant to draw a font in italic style.
052: */
053: public static final boolean ITALIC = true;
054:
055: /**
056: * a constant to draw a font with underline style.
057: */
058: public static final boolean UNDERLINE = true;
059:
060: /**
061: * a constant to draw a font with strikethrough style.
062: */
063: public static final boolean STRIKETHROUGH = true;
064:
065: /**
066: * a constant to draw a font in plain style.
067: */
068: public static final boolean PLAIN = false;
069:
070: /**
071: * the preferred text encoding for this font.
072: */
073: private String fontEncoding;
074:
075: /**
076: * The FontName of the font. This defines the Font-Family.
077: */
078: private String fontName;
079:
080: /**
081: * the font size in point.
082: */
083: private int fontSize;
084:
085: /**
086: * this font's bold flag.
087: */
088: private boolean isBold;
089:
090: /**
091: * this font's italic flag.
092: */
093: private boolean isItalic;
094:
095: /**
096: * this font's underline flag.
097: */
098: private boolean isUnderline;
099:
100: /**
101: * this font's strikethrough flag.
102: */
103: private boolean isStrikeThrough;
104:
105: /**
106: * the AWT-Font represented by this font definition.
107: */
108: private transient Font font;
109:
110: /**
111: * whether to embedd the font in the target documents, if supported.
112: */
113: private boolean embeddedFont;
114:
115: /**
116: * a cached hashcode.
117: */
118: private transient int hashCode;
119:
120: // color is defined elsewhere
121:
122: /**
123: * Creates a font definition using the given name and size and with the given styles
124: * defined.
125: *
126: * @param fontName the font name used in this font definition.
127: * @param fontSize the font size for the defined font.
128: * @param bold true, if the font should be bold, false otherwise
129: * @param italic true, if the font should be italic, false otherwise
130: * @param underline true, if the font should be drawn with underline style, false
131: * otherwise
132: * @param strikeThrough true, if the font should be drawn with strikethrough style,
133: * false otherwise
134: * @param encoding the default text encoding that should be used with this font.
135: * @param embedded whether this font should be embedded in the target document.
136: */
137: public FontDefinition(final String fontName, final int fontSize,
138: final boolean bold, final boolean italic,
139: final boolean underline, final boolean strikeThrough,
140: final String encoding, final boolean embedded) {
141: if (fontName == null) {
142: throw new NullPointerException("FontName must not be null");
143: }
144: if (fontSize <= 0) {
145: throw new IllegalArgumentException(
146: "FontSize must be greater than 0");
147: }
148: this .fontName = fontName;
149: this .fontSize = fontSize;
150: isBold = bold;
151: isItalic = italic;
152: isUnderline = underline;
153: isStrikeThrough = strikeThrough;
154: this .fontEncoding = encoding;
155: this .embeddedFont = embedded;
156: }
157:
158: /**
159: * Creates a font definition using the given name and size and with the given styles
160: * defined.
161: *
162: * @param fontName the font name used in this font definition.
163: * @param fontSize the font size for the defined font.
164: * @param bold true, if the font should be bold, false otherwise
165: * @param italic true, if the font should be italic, false otherwise
166: * @param underline true, if the font should be drawn with underline style, false
167: * otherwise
168: * @param strikeThrough true, if the font should be drawn with strikethrough style,
169: * false otherwise
170: */
171: public FontDefinition(final String fontName, final int fontSize,
172: final boolean bold, final boolean italic,
173: final boolean underline, final boolean strikeThrough) {
174: this (fontName, fontSize, bold, italic, underline,
175: strikeThrough, null, false);
176: }
177:
178: /**
179: * Creates a font definition using the given name and size and with no additional style
180: * enabled.
181: *
182: * @param fontName the font name used in this font definition.
183: * @param fontSize the font size for the defined font.
184: */
185: public FontDefinition(final String fontName, final int fontSize) {
186: this (fontName, fontSize, false, false, false, false, null,
187: false);
188: }
189:
190: /**
191: * Creates a font definition base on the given AWT font.
192: *
193: * @param font the awt font that should be used as definition source.
194: */
195: public FontDefinition(final Font font) {
196: this (font.getName(), font.getSize(), font.isBold(), font
197: .isItalic(), false, false, null, false);
198: }
199:
200: /**
201: * Returns the font name of this font definition. The font name is the font face name.
202: *
203: * @return the name of the font.
204: */
205: public String getFontName() {
206: return fontName;
207: }
208:
209: /**
210: * Returns the size of the defined font.
211: *
212: * @return the font size in points.
213: */
214: public int getFontSize() {
215: return fontSize;
216: }
217:
218: /**
219: * Returns whether the font should be embedded in the target document.
220: *
221: * @return true, if the font should be embedded.
222: */
223: public boolean isEmbeddedFont() {
224: return embeddedFont;
225: }
226:
227: /**
228: * Returns the bold style of this font definition.
229: *
230: * @return true, if the font should be drawn in bold style.
231: */
232: public boolean isBold() {
233: return isBold;
234: }
235:
236: /**
237: * Returns the italic style of this font definition.
238: *
239: * @return true, if the font should be drawn in italic style.
240: */
241: public boolean isItalic() {
242: return isItalic;
243: }
244:
245: /**
246: * Returns the underline style of this font definition.
247: *
248: * @return true, if the font should be drawn in underline style.
249: */
250: public boolean isUnderline() {
251: return isUnderline;
252: }
253:
254: /**
255: * Returns the strikethrough style of this font definition.
256: *
257: * @return true, if the font should be drawn in strikethrough style.
258: */
259: public boolean isStrikeThrough() {
260: return isStrikeThrough;
261: }
262:
263: /**
264: * Creates and returns a copy of this object.
265: *
266: * @return a clone of this instance.
267: *
268: * @throws CloneNotSupportedException if a error occured during cloning .
269: * @see java.lang.Cloneable
270: */
271: public Object clone() throws CloneNotSupportedException {
272: return super .clone();
273: }
274:
275: /**
276: * Returns the AWT StyleInformation, only Bold and Italic are encoding in the return
277: * value.
278: *
279: * @return the AWT-compatible style information.
280: */
281: private int getFontStyle() {
282: int fontstyle = Font.PLAIN;
283: if (isBold()) {
284: fontstyle += Font.BOLD;
285: }
286: if (isItalic()) {
287: fontstyle += Font.ITALIC;
288: }
289: return fontstyle;
290: }
291:
292: /**
293: * returns the AWT-Font defined by this FontDefinition.
294: *
295: * @return the AWT font.
296: */
297: public Font getFont() {
298: if (font == null) {
299: font = new Font(getFontName(), getFontStyle(),
300: getFontSize());
301: }
302: return font;
303: }
304:
305: /**
306: * Returns this fonts string encoding. If the font does not define an encoding, the
307: * given default encoding is returned.
308: *
309: * @param defaultEncoding the font encoding to be used if this font definition does not
310: * define an own encoding.
311: * @return the font encoding or the default encoding.
312: */
313: public String getFontEncoding(final String defaultEncoding) {
314: if (this .fontEncoding == null) {
315: return defaultEncoding;
316: } else {
317: return fontEncoding;
318: }
319: }
320:
321: /**
322: * Returns a string representation of this font definition.
323: *
324: * @return a string representation of this font definition.
325: */
326: public String toString() {
327: final StringBuffer buffer = new StringBuffer();
328: buffer.append("FontDefinition='fontname=\"");
329: buffer.append(fontName);
330: buffer.append("\"; fontSize=");
331: buffer.append(fontSize);
332: buffer.append("; bold=");
333: buffer.append(isBold);
334: buffer.append("; italic=");
335: buffer.append(isItalic);
336: buffer.append("; underline=");
337: buffer.append(isUnderline);
338: buffer.append("; strike=");
339: buffer.append(isStrikeThrough);
340: buffer.append("; embedded=");
341: buffer.append(embeddedFont);
342: buffer.append('\'');
343: return buffer.toString();
344: }
345:
346: /**
347: * Indicates whether some other object is "equal to" this one.
348: *
349: * @param o the reference object with which to compare.
350: * @return <code>true</code> if this object is the same as the obj argument;
351: * <code>false</code> otherwise.
352: *
353: * @see #hashCode()
354: * @see java.util.Hashtable
355: */
356: public boolean equals(final Object o) {
357: if (this == o) {
358: return true;
359: }
360: if (!(o instanceof FontDefinition)) {
361: return false;
362: }
363:
364: final FontDefinition definition = (FontDefinition) o;
365:
366: if (embeddedFont != definition.embeddedFont) {
367: return false;
368: }
369: if (fontSize != definition.fontSize) {
370: return false;
371: }
372: if (isBold != definition.isBold) {
373: return false;
374: }
375: if (isItalic != definition.isItalic) {
376: return false;
377: }
378: if (isStrikeThrough != definition.isStrikeThrough) {
379: return false;
380: }
381: if (isUnderline != definition.isUnderline) {
382: return false;
383: }
384: if (!fontName.equals(definition.fontName)) {
385: return false;
386: }
387: if (fontEncoding != null ? !fontEncoding
388: .equals(definition.fontEncoding)
389: : definition.fontEncoding != null) {
390: return false;
391: }
392:
393: return true;
394: }
395:
396: /**
397: * Returns a hash code value for the object. This method is supported for the benefit of
398: * hashtables such as those provided by <code>java.util.Hashtable</code>.
399: *
400: * @return a hash code value for this object.
401: *
402: * @see java.lang.Object#equals(java.lang.Object)
403: */
404: public int hashCode() {
405: if (hashCode == 0) {
406: int result = (fontEncoding != null ? fontEncoding
407: .hashCode() : 0);
408: result = 29 * result + fontName.hashCode();
409: result = 29 * result + fontSize;
410: result = 29 * result + (isBold ? 1 : 0);
411: result = 29 * result + (isItalic ? 1 : 0);
412: result = 29 * result + (isUnderline ? 1 : 0);
413: result = 29 * result + (isStrikeThrough ? 1 : 0);
414: result = 29 * result + (embeddedFont ? 1 : 0);
415: hashCode = result;
416: }
417: return hashCode;
418: }
419:
420: /**
421: * Returns true if the logical font name is equivalent to 'SansSerif', and false
422: * otherwise.
423: *
424: * @return true or false.
425: */
426: public boolean isSansSerif() {
427: return StringUtils.startsWithIgnoreCase(fontName, "SansSerif")
428: || StringUtils.startsWithIgnoreCase(fontName, "Dialog")
429: || StringUtils.startsWithIgnoreCase(fontName,
430: "SanSerif");
431: // is it a bug? Somewhere in the JDK this name is used (typo, but heck, we accept it anyway).
432: }
433:
434: /**
435: * Returns true if the logical font name is equivalent to 'Courier', and false
436: * otherwise.
437: *
438: * @return true or false.
439: */
440: public boolean isCourier() {
441: return (StringUtils.startsWithIgnoreCase(fontName,
442: "dialoginput") || StringUtils.startsWithIgnoreCase(
443: fontName, "monospaced"));
444: }
445:
446: /**
447: * Returns true if the logical font name is equivalent to 'Serif', and false otherwise.
448: *
449: * @return true or false.
450: */
451: public boolean isSerif() {
452: return (StringUtils.startsWithIgnoreCase(fontName, "serif"));
453: }
454:
455: }
|