1 /* Font.java -- Font object
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.peer
.FontPeer
;
42 import java
.io
.Serializable
;
43 import java
.util
.StringTokenizer
;
46 * This class represents a windowing system font.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
49 * @author Warren Levy <warrenl@cygnus.com>
51 public class Font
implements Serializable
59 * Constant indicating a "plain" font.
61 public static final int PLAIN
= 0;
64 * Constant indicating a "bold" font.
66 public static final int BOLD
= 1;
69 * Constant indicating an "italic" font.
71 public static final int ITALIC
= 2;
73 public static final int ROMAN_BASELINE
= 0;
74 public static final int CENTER_BASELINE
= 1;
75 public static final int HANGING_BASELINE
= 2;
79 * Indicates to <code>createFont</code> that the supplied font data
80 * is in TrueType format.
82 * <p><em>Specification Note:</em> The Sun JavaDoc for J2SE 1.4 does
83 * not indicate whether this value also subsumes OpenType. OpenType
84 * is essentially the same format as TrueType, but allows to define
85 * glyph shapes in the same way as PostScript, using cubic bezier
90 public static final int TRUETYPE_FONT
= 0;
94 * A flag for <code>layoutGlyphVector</code>, indicating that the
95 * orientation of a text run is from left to right.
99 public static final int LAYOUT_LEFT_TO_RIGHT
= 0;
103 * A flag for <code>layoutGlyphVector</code>, indicating that the
104 * orientation of a text run is from right to left.
108 public static final int LAYOUT_RIGHT_TO_LEFT
= 1;
112 * A flag for <code>layoutGlyphVector</code>, indicating that the
113 * text does not contain valid characters before the
114 * <code>start</code> position. If this flag is set,
115 * <code>layoutGlyphVector</code> does not examine the text before
116 * <code>start</code>, even if this would be necessary to select the
117 * correct glyphs (e.g., for Arabic text).
121 public static final int LAYOUT_NO_START_CONTEXT
= 2;
125 * A flag for <code>layoutGlyphVector</code>, indicating that the
126 * text does not contain valid characters after the
127 * <code>limit</code> position. If this flag is set,
128 * <code>layoutGlyphVector</code> does not examine the text after
129 * <code>limit</code>, even if this would be necessary to select the
130 * correct glyphs (e.g., for Arabic text).
134 public static final int LAYOUT_NO_LIMIT_CONTEXT
= 4;
137 // Serialization constant
138 private static final long serialVersionUID
= -4206021311591459213L;
140 /*************************************************************************/
147 * The name of this font
149 protected String name
;
152 * The font style, which is a combination (by summing, not OR-ing) of
153 * the font style constants in this class.
158 * The font point size.
162 protected float pointSize
;
164 // The native peer for this font
165 private FontPeer peer
;
167 /*************************************************************************/
174 * Creates a <code>Font</code> object from the specified string, which
175 * is in one of the following formats:
178 * <li>fontname-style-pointsize
180 * <li>fontname-pointsize
184 * The style should be one of BOLD, ITALIC, or BOLDITALIC. The default
185 * style if none is specified is PLAIN. The default size if none
186 * is specified is 12.
189 decode(String fontspec
)
195 StringTokenizer st
= new StringTokenizer(fontspec
, "-");
196 while (st
.hasMoreTokens())
198 String token
= st
.nextToken();
205 if (token
.toUpperCase().equals("BOLD"))
210 if (token
.toUpperCase().equals("ITALIC"))
215 if (token
.toUpperCase().equals("BOLDITALIC"))
217 style
= BOLD
+ ITALIC
;
224 tokenval
= Integer
.parseInt(token
);
226 catch(NumberFormatException e
) { ; }
232 return(new Font(name
, style
, size
));
235 /*************************************************************************/
238 * Returns a <code>Font</code> object from the passed property name.
240 * @param propname The name of the system property.
241 * @param default Value to use if the property is not found.
243 * @return The requested font, or <code>default</code> if the property
244 * not exist or is malformed.
247 getFont(String propname
, Font defval
)
249 String propval
= System
.getProperty(propname
);
251 return(decode(propval
));
256 /*************************************************************************/
259 * Returns a <code>Font</code> object from the passed property name.
261 * @param propname The name of the system property.
263 * @return The requested font, or <code>null</code> if the property
264 * not exist or is malformed.
267 getFont(String propname
)
269 return(getFont(propname
, null));
272 /*************************************************************************/
279 * Initializes a new instance of <code>Font</code> with the specified
282 * @param name The name of the font.
283 * @param style The font style.
284 * @param size The font point size.
287 Font(String name
, int style
, int size
)
292 this.pointSize
= size
;
295 /*************************************************************************/
302 * Returns the name of the font.
304 * @return The name of the font.
312 /*************************************************************************/
315 * Returns the style of the font.
317 * @return The font style.
331 /*************************************************************************/
334 * Tests whether or not this is a plain font. This will be true if
335 * and only if neither the bold nor the italics style is set.
337 * @return <code>true</code> if this is a plain font, <code>false</code>
349 /*************************************************************************/
352 * Tests whether or not this font is bold.
354 * @return <code>true</code> if this font is bold, <code>false</code>
360 if ((style
== BOLD
) || (style
== (BOLD
+ITALIC
)))
366 /*************************************************************************/
369 * Tests whether or not this font is italic.
371 * @return <code>true</code> if this font is italic, <code>false</code>
377 if ((style
== ITALIC
) || (style
== (BOLD
+ITALIC
)))
383 /*************************************************************************/
386 * Returns the system specific font family name.
388 * @return The system specific font family name.
393 // FIXME: How do I implement this?
403 /*************************************************************************/
406 * Returns a native peer object for this font.
408 * @return A native peer object for this font.
416 peer
= Toolkit
.getDefaultToolkit().getFontPeer(name
, style
);
420 /*************************************************************************/
423 * Returns a hash value for this font.
425 * @return A hash for this font.
430 return((new String(name
+ size
+ style
)).hashCode());
433 /*************************************************************************/
436 * Tests whether or not the specified object is equal to this font. This
437 * will be true if and only if:
440 * <li>The object is not <code>null</code>.
441 * <li>The object is an instance of <code>Font</code>.
442 * <li>The object has the same name, style, and size as this object.
445 * @return <code>true</code> if the specified object is equal to this
446 * object, <code>false</code> otherwise.
454 if (!(obj
instanceof Font
))
459 if (!f
.name
.equals(name
))
465 if (f
.style
!= style
)
471 /*************************************************************************/
474 * Returns a string representation of this font.
476 * @return A string representation of this font.
481 return(getClass().getName() + "(name=" + name
+ ",style=" + style
+
482 ",size=" + size
+ ")");