KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > Font


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31 import java.io.Serializable JavaDoc;
32
33 /**
34  * An immutable representation of a text font, including typeface, size, and
35  * style.
36  */

37 public class Font
38 implements Serializable JavaDoc {
39     
40     /**
41      * An immutable representation of a type face.
42      * A typeface may specify an alternate <code>TypeFace</code> object
43      * in the event that the primary one is not available on a given
44      * client. In this way, a chain of alternates may be created for
45      * a very specific face, e.g.:
46      * &quot;Verdana-&gt;Arial-&gt;Helvetica-&gt;Sans-Serif&quot;
47      */

48     public static class Typeface
49     implements Serializable JavaDoc {
50         
51         private String JavaDoc name;
52         private Typeface alternate;
53         
54         /**
55          * Creates a new <code>Typeface</code>.
56          *
57          * @param name the typeface name
58          */

59         public Typeface(String JavaDoc name) {
60             this(name, null);
61         }
62
63         /**
64          * Creates a new <code>Typeface</code>.
65          *
66          * @param name the typeface name
67          * @param alternate the alternate typeface which should be used, in
68          * case the client does not support the specified typeface
69          */

70         public Typeface(String JavaDoc name, Typeface alternate) {
71             super();
72             if (name == null) {
73                 throw new IllegalArgumentException JavaDoc("'name' argument cannot be null.");
74             }
75             this.name = name;
76             this.alternate = alternate;
77         }
78         
79         /**
80          * @see java.lang.Object#equals(java.lang.Object)
81          */

82         public boolean equals(Object JavaDoc o) {
83             if (this == o) {
84                 return true;
85             }
86             if (!(o instanceof Typeface)) {
87                 return false;
88             }
89             Typeface that = (Typeface) o;
90             if (!this.name.equals(that.name)) {
91                 return false;
92             }
93             if (this.alternate == null) {
94                 return that.alternate == null;
95             }
96             return this.alternate.equals(that.alternate);
97         }
98             
99         /**
100          * Returns the alternate typeface.
101          * This method should be queried recursively until it returns null
102          * in order to determine all alternate typefaces.
103          *
104          * @return the alternate <code>Typeface</code>
105          */

106         public Typeface getAlternate() {
107             return alternate;
108         }
109         
110         /**
111          * Returns the name of the typeface.
112          *
113          * @return the name of the typeface, e.g., 'Helvetica'
114          */

115         public String JavaDoc getName() {
116             return name;
117         }
118         
119         /**
120          * Renders a debug representation of the object.
121          *
122          * @see java.lang.Object#toString()
123          */

124         public String JavaDoc toString() {
125             return alternate == null ? name : name + ", " + alternate;
126         }
127     }
128     
129     public static final Typeface SANS_SERIF = new Typeface("Sans-Serif");
130     public static final Typeface SERIF = new Typeface("Serif");
131     public static final Typeface MONOSPACE = new Typeface("Monospace");
132     public static final Typeface HELVETICA = new Typeface("Helvetica", SANS_SERIF);
133     public static final Typeface ARIAL = new Typeface("Arial", HELVETICA);
134     public static final Typeface VERDANA = new Typeface("Verdana", ARIAL);
135     public static final Typeface TIMES = new Typeface("Times", SERIF);
136     public static final Typeface TIMES_ROMAN = new Typeface("Times Roman", TIMES);
137     public static final Typeface TIMES_NEW_ROMAN = new Typeface("Times New Roman", TIMES_ROMAN);
138     public static final Typeface COURIER = new Typeface("Courier", MONOSPACE);
139     public static final Typeface COURIER_NEW = new Typeface("Courier New", COURIER);
140
141     /**
142      * A style value indicating no text attributes.
143      */

144     public static final int PLAIN = 0x0;
145     
146     /**
147      * A style value indicating bold.
148      */

149     public static final int BOLD = 0x1;
150
151     /**
152      * A style value indicating bold.
153      */

154     public static final int ITALIC = 0x2;
155
156     /**
157      * A style value indicating underline.
158      */

159     public static final int UNDERLINE = 0x4;
160     
161     /**
162      * A style value indicating overline.
163      */

164     public static final int OVERLINE = 0x8;
165     
166     /**
167      * A style value indicating line-through.
168      */

169     public static final int LINE_THROUGH = 0x10;
170     
171     private Typeface typeface;
172     private Extent size;
173     private int style;
174     
175     /**
176      * Creates a new <code>Font</code> with the specified <code>Typeface</code>,
177      * size, and style.
178      *
179      * @param typeface a <code>Typeface</code> describing the typeface of the font.
180      * @param style the style of the font, one or more of the following values:
181      * <ul>
182      * <li>PLAIN</li>
183      * <li>BOLD</li>
184      * <li>ITALIC</li>
185      * <li>OVERLINE</li>
186      * <li>LINE_THROUGH</li>
187      * <li>UNDERLINE</li>
188      * </ul>
189      * If it is necessary create a font with multiple style attributes,
190      * they should be bitwise-ORed together, using an expression such as
191      * <code>BOLD | UNDERLINE</code>.
192      * @param size the size of the font as a <code>Extent</code>
193      */

194     public Font(Typeface typeface, int style, Extent size) {
195         super();
196         this.typeface = typeface;
197         this.style = style;
198         this.size = size;
199     }
200     
201     /**
202      * @see java.lang.Object#equals(java.lang.Object)
203      */

204     public boolean equals(Object JavaDoc o) {
205         if (this == o) {
206             return true;
207         }
208         if (!(o instanceof Font)) {
209             return false;
210         }
211         Font that = (Font) o;
212         if (this.style != that.style) {
213             return false;
214         }
215         if (typeface == null) {
216             if (that.typeface != null) {
217                 return false;
218             }
219         } else {
220             if (!this.typeface.equals(that.typeface)) {
221                 return false;
222             }
223         }
224         if (size == null) {
225             if (that.size != null) {
226                 return false;
227             }
228         } else {
229             if (!this.size.equals(that.size)) {
230                 return false;
231             }
232         }
233         return true;
234     }
235     
236     /**
237      * Returns the size of the font.
238      *
239      * @return the size of the font
240      */

241     public Extent getSize() {
242         return size;
243     }
244     
245     /**
246      * Returns the typeface of the font.
247      *
248      * @return the typeface of the font
249      */

250     public Typeface getTypeface() {
251         return typeface;
252     }
253     
254     /**
255      * Determines whether the font is bold.
256      *
257      * @return true if the font is bold
258      */

259     public boolean isBold() {
260         return (style & BOLD) != 0;
261     }
262     
263     /**
264      * Determines whether the font is italicized.
265      *
266      * @return true if the font is italicized
267      */

268     public boolean isItalic() {
269         return (style & ITALIC) != 0;
270     }
271     
272     /**
273      * Determines whether the font has line-through enabled.
274      *
275      * @return true if the font has line-through enabled
276      */

277     public boolean isLineThrough() {
278         return (style & LINE_THROUGH) != 0;
279     }
280     
281     /**
282      * Determines whether the font is plain (i.e., the font has no style
283      * attributes set).
284      *
285      * @return true if the font is plain
286      */

287     public boolean isPlain() {
288         return style == 0;
289     }
290     
291     /**
292      * Determines whether the font has an overline.
293      *
294      * @return true if the font has an overline
295      */

296     public boolean isOverline() {
297         return (style & OVERLINE) != 0;
298     }
299     
300     /**
301      * Determines whether the font is underlined.
302      *
303      * @return true if the font is underlined
304      */

305     public boolean isUnderline() {
306         return (style & UNDERLINE) != 0;
307     }
308     
309     /**
310      * Renders a debug representation of the object.
311      *
312      * @see java.lang.Object#toString()
313      */

314     public String JavaDoc toString() {
315         StringBuffer JavaDoc out = new StringBuffer JavaDoc(Font.class.getName());
316         out.append(" (");
317         out.append(getTypeface());
318         out.append(" /");
319         if (isPlain()) {
320             out.append(" Plain");
321         }
322         if (isBold()) {
323             out.append(" Bold");
324         }
325         if (isItalic()) {
326             out.append(" Italic");
327         }
328         if (isLineThrough()) {
329             out.append(" LineThrough");
330         }
331         if (isOverline()) {
332             out.append(" Overline");
333         }
334         if (isUnderline()) {
335             out.append(" Underline");
336         }
337         out.append(" / ");
338         out.append(getSize());
339         out.append(")");
340         return out.toString();
341     }
342 }
343
Popular Tags