KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > Font


1 /*
2  * $Id: Font.java 2739 2007-05-04 11:24:51Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text;
52
53 import java.awt.Color JavaDoc;
54
55 import com.lowagie.text.html.Markup;
56 import com.lowagie.text.pdf.BaseFont;
57
58 /**
59  * Contains all the specifications of a font: fontfamily, size, style and color.
60  * <P>
61  * Example: <BLOCKQUOTE>
62  *
63  * <PRE>
64  *
65  * Paragraph p = new Paragraph("This is a paragraph", <STRONG>new
66  * Font(Font.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)) </STRONG>);
67  *
68  * </PRE>
69  *
70  * </BLOCKQUOTE>
71  */

72
73 public class Font implements Comparable JavaDoc {
74
75     // static membervariables for the different families
76

77     /** a possible value of a font family. */
78     public static final int COURIER = 0;
79
80     /** a possible value of a font family. */
81     public static final int HELVETICA = 1;
82
83     /** a possible value of a font family. */
84     public static final int TIMES_ROMAN = 2;
85
86     /** a possible value of a font family. */
87     public static final int SYMBOL = 3;
88
89     /** a possible value of a font family. */
90     public static final int ZAPFDINGBATS = 4;
91
92     // static membervariables for the different styles
93

94     /** this is a possible style. */
95     public static final int NORMAL = 0;
96
97     /** this is a possible style. */
98     public static final int BOLD = 1;
99
100     /** this is a possible style. */
101     public static final int ITALIC = 2;
102
103     /** this is a possible style. */
104     public static final int UNDERLINE = 4;
105
106     /** this is a possible style. */
107     public static final int STRIKETHRU = 8;
108
109     /** this is a possible style. */
110     public static final int BOLDITALIC = BOLD | ITALIC;
111
112     // static membervariables
113

114     /** the value of an undefined attribute. */
115     public static final int UNDEFINED = -1;
116
117     /** the value of the default size. */
118     public static final int DEFAULTSIZE = 12;
119
120     // membervariables
121

122     /** the value of the fontfamily. */
123     private int family = UNDEFINED;
124
125     /** the value of the fontsize. */
126     private float size = UNDEFINED;
127
128     /** the value of the style. */
129     private int style = UNDEFINED;
130
131     /** the value of the color. */
132     private Color JavaDoc color = null;
133
134     /** the external font */
135     private BaseFont baseFont = null;
136
137     // constructors
138

139     /**
140      * Copy constructor of a Font
141      *
142      * @param other
143      * the font that has to be copied
144      */

145     public Font(Font other) {
146         this.family = other.family;
147         this.size = other.size;
148         this.style = other.style;
149         this.color = other.color;
150         this.baseFont = other.baseFont;
151     }
152
153     /**
154      * Constructs a Font.
155      *
156      * @param family
157      * the family to which this font belongs
158      * @param size
159      * the size of this font
160      * @param style
161      * the style of this font
162      * @param color
163      * the <CODE>Color</CODE> of this font.
164      */

165
166     public Font(int family, float size, int style, Color JavaDoc color) {
167         this.family = family;
168         this.size = size;
169         this.style = style;
170         this.color = color;
171     }
172
173     /**
174      * Constructs a Font.
175      *
176      * @param bf
177      * the external font
178      * @param size
179      * the size of this font
180      * @param style
181      * the style of this font
182      * @param color
183      * the <CODE>Color</CODE> of this font.
184      */

185
186     public Font(BaseFont bf, float size, int style, Color JavaDoc color) {
187         this.baseFont = bf;
188         this.size = size;
189         this.style = style;
190         this.color = color;
191     }
192
193     /**
194      * Constructs a Font.
195      *
196      * @param bf
197      * the external font
198      * @param size
199      * the size of this font
200      * @param style
201      * the style of this font
202      */

203     public Font(BaseFont bf, float size, int style) {
204         this(bf, size, style, null);
205     }
206
207     /**
208      * Constructs a Font.
209      *
210      * @param bf
211      * the external font
212      * @param size
213      * the size of this font
214      */

215     public Font(BaseFont bf, float size) {
216         this(bf, size, UNDEFINED, null);
217     }
218
219     /**
220      * Constructs a Font.
221      *
222      * @param bf
223      * the external font
224      */

225     public Font(BaseFont bf) {
226         this(bf, UNDEFINED, UNDEFINED, null);
227     }
228
229     /**
230      * Constructs a Font.
231      *
232      * @param family
233      * the family to which this font belongs
234      * @param size
235      * the size of this font
236      * @param style
237      * the style of this font
238      */

239
240     public Font(int family, float size, int style) {
241         this(family, size, style, null);
242     }
243
244     /**
245      * Constructs a Font.
246      *
247      * @param family
248      * the family to which this font belongs
249      * @param size
250      * the size of this font
251      */

252
253     public Font(int family, float size) {
254         this(family, size, UNDEFINED, null);
255     }
256
257     /**
258      * Constructs a Font.
259      *
260      * @param family
261      * the family to which this font belongs
262      */

263
264     public Font(int family) {
265         this(family, UNDEFINED, UNDEFINED, null);
266     }
267
268     /**
269      * Constructs a Font.
270      */

271
272     public Font() {
273         this(UNDEFINED, UNDEFINED, UNDEFINED, null);
274     }
275
276     // implementation of the Comparable interface
277

278     /**
279      * Compares this <CODE>Font</CODE> with another
280      *
281      * @param object
282      * the other <CODE>Font</CODE>
283      * @return a value
284      */

285     public int compareTo(Object JavaDoc object) {
286         if (object == null) {
287             return -1;
288         }
289         Font font;
290         try {
291             font = (Font) object;
292             if (baseFont != null && !baseFont.equals(font.getBaseFont())) {
293                 return -2;
294             }
295             if (this.family != font.getFamily()) {
296                 return 1;
297             }
298             if (this.size != font.getSize()) {
299                 return 2;
300             }
301             if (this.style != font.getStyle()) {
302                 return 3;
303             }
304             if (this.color == null) {
305                 if (font.color == null) {
306                     return 0;
307                 }
308                 return 4;
309             }
310             if (font.color == null) {
311                 return 4;
312             }
313             if (this.color.equals(font.getColor())) {
314                 return 0;
315             }
316             return 4;
317         } catch (ClassCastException JavaDoc cce) {
318             return -3;
319         }
320     }
321
322     // FAMILY
323

324     /**
325      * Gets the family of this font.
326      *
327      * @return the value of the family
328      */

329     public int getFamily() {
330         return family;
331     }
332
333     /**
334      * Gets the familyname as a String.
335      *
336      * @return the familyname
337      */

338     public String JavaDoc getFamilyname() {
339         String JavaDoc tmp = "unknown";
340         switch (getFamily()) {
341         case Font.COURIER:
342             return FontFactory.COURIER;
343         case Font.HELVETICA:
344             return FontFactory.HELVETICA;
345         case Font.TIMES_ROMAN:
346             return FontFactory.TIMES_ROMAN;
347         case Font.SYMBOL:
348             return FontFactory.SYMBOL;
349         case Font.ZAPFDINGBATS:
350             return FontFactory.ZAPFDINGBATS;
351         default:
352             if (baseFont != null) {
353                 String JavaDoc[][] names = baseFont.getFamilyFontName();
354                 for (int i = 0; i < names.length; i++) {
355                     if ("0".equals(names[i][2])) {
356                         return names[i][3];
357                     }
358                     if ("1033".equals(names[i][2])) {
359                         tmp = names[i][3];
360                     }
361                     if ("".equals(names[i][2])) {
362                         tmp = names[i][3];
363                     }
364                 }
365             }
366         }
367         return tmp;
368     }
369
370     /**
371      * Sets the family using a <CODE>String</CODE> ("Courier", "Helvetica",
372      * "Times New Roman", "Symbol" or "ZapfDingbats").
373      *
374      * @param family
375      * A <CODE>String</CODE> representing a certain font-family.
376      */

377     public void setFamily(String JavaDoc family) {
378         this.family = getFamilyIndex(family);
379     }
380
381     /**
382      * Translates a <CODE>String</CODE> -value of a certain family into the
383      * index that is used for this family in this class.
384      *
385      * @param family
386      * A <CODE>String</CODE> representing a certain font-family
387      * @return the corresponding index
388      */

389     public static int getFamilyIndex(String JavaDoc family) {
390         if (family.equalsIgnoreCase(FontFactory.COURIER)) {
391             return COURIER;
392         }
393         if (family.equalsIgnoreCase(FontFactory.HELVETICA)) {
394             return HELVETICA;
395         }
396         if (family.equalsIgnoreCase(FontFactory.TIMES_ROMAN)) {
397             return TIMES_ROMAN;
398         }
399         if (family.equalsIgnoreCase(FontFactory.SYMBOL)) {
400             return SYMBOL;
401         }
402         if (family.equalsIgnoreCase(FontFactory.ZAPFDINGBATS)) {
403             return ZAPFDINGBATS;
404         }
405         return UNDEFINED;
406     }
407
408     // SIZE
409

410     /**
411      * Gets the size of this font.
412      *
413      * @return a size
414      */

415     public float getSize() {
416         return size;
417     }
418
419     /**
420      * Gets the size that can be used with the calculated <CODE>BaseFont
421      * </CODE>.
422      *
423      * @return the size that can be used with the calculated <CODE>BaseFont
424      * </CODE>
425      */

426     public float getCalculatedSize() {
427         float s = this.size;
428         if (s == UNDEFINED) {
429             s = DEFAULTSIZE;
430         }
431         return s;
432     }
433
434     /**
435      * Gets the leading that can be used with this font.
436      *
437      * @param linespacing
438      * a certain linespacing
439      * @return the height of a line
440      */

441     public float getCalculatedLeading(float linespacing) {
442         return linespacing * getCalculatedSize();
443     }
444
445     /**
446      * Sets the size.
447      *
448      * @param size
449      * The new size of the font.
450      */

451     public void setSize(float size) {
452         this.size = size;
453     }
454
455     // STYLE
456

457     /**
458      * Gets the style of this font.
459      *
460      * @return a size
461      */

462     public int getStyle() {
463         return style;
464     }
465
466     /**
467      * Gets the style that can be used with the calculated <CODE>BaseFont
468      * </CODE>.
469      *
470      * @return the style that can be used with the calculated <CODE>BaseFont
471      * </CODE>
472      */

473     public int getCalculatedStyle() {
474         int style = this.style;
475         if (style == UNDEFINED) {
476             style = NORMAL;
477         }
478         if (baseFont != null)
479             return style;
480         if (family == SYMBOL || family == ZAPFDINGBATS)
481             return style;
482         else
483             return style & (~BOLDITALIC);
484     }
485
486     /**
487      * checks if this font is Bold.
488      *
489      * @return a <CODE>boolean</CODE>
490      */

491     public boolean isBold() {
492         if (style == UNDEFINED) {
493             return false;
494         }
495         return (style & BOLD) == BOLD;
496     }
497
498     /**
499      * checks if this font is Bold.
500      *
501      * @return a <CODE>boolean</CODE>
502      */

503     public boolean isItalic() {
504         if (style == UNDEFINED) {
505             return false;
506         }
507         return (style & ITALIC) == ITALIC;
508     }
509
510     /**
511      * checks if this font is underlined.
512      *
513      * @return a <CODE>boolean</CODE>
514      */

515     public boolean isUnderlined() {
516         if (style == UNDEFINED) {
517             return false;
518         }
519         return (style & UNDERLINE) == UNDERLINE;
520     }
521
522     /**
523      * checks if the style of this font is STRIKETHRU.
524      *
525      * @return a <CODE>boolean</CODE>
526      */

527     public boolean isStrikethru() {
528         if (style == UNDEFINED) {
529             return false;
530         }
531         return (style & STRIKETHRU) == STRIKETHRU;
532     }
533
534     /**
535      * Sets the style.
536      *
537      * @param style
538      * the style.
539      */

540     public void setStyle(int style) {
541         if (this.style == UNDEFINED)
542             this.style = NORMAL;
543         this.style |= style;
544     }
545
546     /**
547      * Sets the style using a <CODE>String</CODE> containing one of more of
548      * the following values: normal, bold, italic, underline, strike.
549      *
550      * @param style
551      * A <CODE>String</CODE> representing a certain style.
552      */

553     public void setStyle(String JavaDoc style) {
554         if (this.style == UNDEFINED)
555             this.style = NORMAL;
556         this.style |= getStyleValue(style);
557     }
558
559     /**
560      * Translates a <CODE>String</CODE> -value of a certain style into the
561      * index value is used for this style in this class.
562      *
563      * @param style
564      * A <CODE>String</CODE>
565      * @return the corresponding value
566      */

567     public static int getStyleValue(String JavaDoc style) {
568         int s = 0;
569         if (style.indexOf(Markup.CSS_VALUE_NORMAL) != -1) {
570             s |= NORMAL;
571         }
572         if (style.indexOf(Markup.CSS_VALUE_BOLD) != -1) {
573             s |= BOLD;
574         }
575         if (style.indexOf(Markup.CSS_VALUE_ITALIC) != -1) {
576             s |= ITALIC;
577         }
578         if (style.indexOf(Markup.CSS_VALUE_OBLIQUE) != -1) {
579             s |= ITALIC;
580         }
581         if (style.indexOf(Markup.CSS_VALUE_UNDERLINE) != -1) {
582             s |= UNDERLINE;
583         }
584         if (style.indexOf(Markup.CSS_VALUE_LINETHROUGH) != -1) {
585             s |= STRIKETHRU;
586         }
587         return s;
588     }
589
590     // COLOR
591

592     /**
593      * Gets the color of this font.
594      *
595      * @return a color
596      */

597     public Color JavaDoc getColor() {
598         return color;
599     }
600
601     /**
602      * Sets the color.
603      *
604      * @param color
605      * the new color of the font
606      */

607
608     public void setColor(Color JavaDoc color) {
609         this.color = color;
610     }
611
612     /**
613      * Sets the color.
614      *
615      * @param red
616      * the red-value of the new color
617      * @param green
618      * the green-value of the new color
619      * @param blue
620      * the blue-value of the new color
621      */

622     public void setColor(int red, int green, int blue) {
623         this.color = new Color JavaDoc(red, green, blue);
624     }
625
626     // BASEFONT
627

628     /**
629      * Gets the <CODE>BaseFont</CODE> inside this object.
630      *
631      * @return the <CODE>BaseFont</CODE>
632      */

633     public BaseFont getBaseFont() {
634         return baseFont;
635     }
636
637     /**
638      * Gets the <CODE>BaseFont</CODE> this class represents. For the built-in
639      * fonts a <CODE>BaseFont</CODE> is calculated.
640      *
641      * @param specialEncoding
642      * <CODE>true</CODE> to use the special encoding for Symbol and
643      * ZapfDingbats, <CODE>false</CODE> to always use <CODE>Cp1252
644      * </CODE>
645      * @return the <CODE>BaseFont</CODE> this class represents
646      */

647     public BaseFont getCalculatedBaseFont(boolean specialEncoding) {
648         if (baseFont != null)
649             return baseFont;
650         int style = this.style;
651         if (style == UNDEFINED) {
652             style = NORMAL;
653         }
654         String JavaDoc fontName = BaseFont.HELVETICA;
655         String JavaDoc encoding = BaseFont.WINANSI;
656         BaseFont cfont = null;
657         switch (family) {
658         case COURIER:
659             switch (style & BOLDITALIC) {
660             case BOLD:
661                 fontName = BaseFont.COURIER_BOLD;
662                 break;
663             case ITALIC:
664                 fontName = BaseFont.COURIER_OBLIQUE;
665                 break;
666             case BOLDITALIC:
667                 fontName = BaseFont.COURIER_BOLDOBLIQUE;
668                 break;
669             default:
670                 //case NORMAL:
671
fontName = BaseFont.COURIER;
672                 break;
673             }
674             break;
675         case TIMES_ROMAN:
676             switch (style & BOLDITALIC) {
677             case BOLD:
678                 fontName = BaseFont.TIMES_BOLD;
679                 break;
680             case ITALIC:
681                 fontName = BaseFont.TIMES_ITALIC;
682                 break;
683             case BOLDITALIC:
684                 fontName = BaseFont.TIMES_BOLDITALIC;
685                 break;
686             default:
687             case NORMAL:
688                 fontName = BaseFont.TIMES_ROMAN;
689                 break;
690             }
691             break;
692         case SYMBOL:
693             fontName = BaseFont.SYMBOL;
694             if (specialEncoding)
695                 encoding = BaseFont.SYMBOL;
696             break;
697         case ZAPFDINGBATS:
698             fontName = BaseFont.ZAPFDINGBATS;
699             if (specialEncoding)
700                 encoding = BaseFont.ZAPFDINGBATS;
701             break;
702         default:
703         case Font.HELVETICA:
704             switch (style & BOLDITALIC) {
705             case BOLD:
706                 fontName = BaseFont.HELVETICA_BOLD;
707                 break;
708             case ITALIC:
709                 fontName = BaseFont.HELVETICA_OBLIQUE;
710                 break;
711             case BOLDITALIC:
712                 fontName = BaseFont.HELVETICA_BOLDOBLIQUE;
713                 break;
714             default:
715             case NORMAL:
716                 fontName = BaseFont.HELVETICA;
717                 break;
718             }
719             break;
720         }
721         try {
722             cfont = BaseFont.createFont(fontName, encoding, false);
723         } catch (Exception JavaDoc ee) {
724             throw new ExceptionConverter(ee);
725         }
726         return cfont;
727     }
728     
729     
730     // Helper methods
731

732     /**
733      * Checks if the properties of this font are undefined or null.
734      * <P>
735      * If so, the standard should be used.
736      *
737      * @return a <CODE>boolean</CODE>
738      */

739     public boolean isStandardFont() {
740         return (family == UNDEFINED && size == UNDEFINED && style == UNDEFINED
741                 && color == null && baseFont == null);
742     }
743
744     /**
745      * Replaces the attributes that are equal to <VAR>null</VAR> with the
746      * attributes of a given font.
747      *
748      * @param font
749      * the font of a bigger element class
750      * @return a <CODE>Font</CODE>
751      */

752     public Font difference(Font font) {
753         // size
754
float dSize = font.size;
755         if (dSize == UNDEFINED) {
756             dSize = this.size;
757         }
758         // style
759
int dStyle = UNDEFINED;
760         int style1 = this.style;
761         int style2 = font.getStyle();
762         if (style1 != UNDEFINED || style2 != UNDEFINED) {
763             if (style1 == UNDEFINED)
764                 style1 = 0;
765             if (style2 == UNDEFINED)
766                 style2 = 0;
767             dStyle = style1 | style2;
768         }
769         // color
770
Color JavaDoc dColor = font.color;
771         if (dColor == null) {
772             dColor = this.color;
773         }
774         // family
775
if (font.baseFont != null) {
776             return new Font(font.baseFont, dSize, dStyle, dColor);
777         }
778         if (font.getFamily() != UNDEFINED) {
779             return new Font(font.family, dSize, dStyle, dColor);
780         }
781         if (this.baseFont != null) {
782             if (dStyle == style1) {
783                 return new Font(this.baseFont, dSize, dStyle, dColor);
784             } else {
785                 return FontFactory.getFont(this.getFamilyname(), dSize, dStyle,
786                         dColor);
787             }
788         }
789         return new Font(this.family, dSize, dStyle, dColor);
790     }
791
792     // methods to retrieve the membervariables
793

794     /**
795      * Gets the family of this font.
796      *
797      * @return the value of the family
798      * @deprecated Use {@link #getFamily()} instead
799      */

800     public int family() {
801         return getFamily();
802     }
803
804     /**
805      * Gets the size of this font.
806      *
807      * @return a size
808      * @deprecated Use {@link #getSize()} instead
809      */

810     public float size() {
811         return getSize();
812     }
813     
814     /**
815      * Gets the leading that can be used with this font.
816      *
817      * @param linespacing
818      * a certain linespacing
819      * @return the height of a line
820      * @deprecated Use {@link #getCalculatedLeading(float)} instead
821      */

822     public float leading(float linespacing) {
823         return getCalculatedLeading(linespacing);
824     }
825
826     /**
827      * Gets the style of this font.
828      *
829      * @return a size
830      * @deprecated Use {@link #getStyle()} instead
831      */

832     public int style() {
833         return getStyle();
834     }
835
836     /**
837      * Gets the color of this font.
838      *
839      * @return a color
840      * @deprecated Use {@link #getColor()} instead
841      */

842     public Color JavaDoc color() {
843         return getColor();
844     }
845 }
Popular Tags