FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / awt / Font.java
blob3723a4528ed3ca48312282428e087063c558191d
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.awt;
41 import java.awt.peer.FontPeer;
42 import java.io.Serializable;
43 import java.util.StringTokenizer;
45 /**
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
55 * Static Variables
58 /**
59 * Constant indicating a "plain" font.
61 public static final int PLAIN = 0;
63 /**
64 * Constant indicating a "bold" font.
66 public static final int BOLD = 1;
68 /**
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;
78 /**
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
86 * curves.
88 * @since 1.3
90 public static final int TRUETYPE_FONT = 0;
93 /**
94 * A flag for <code>layoutGlyphVector</code>, indicating that the
95 * orientation of a text run is from left to right.
97 * @since 1.4
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.
106 * @since 1.4
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).
119 * @since 1.4
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).
132 * @since 1.4
134 public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
137 // Serialization constant
138 private static final long serialVersionUID = -4206021311591459213L;
140 /*************************************************************************/
143 * Instance Variables
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.
155 protected int style;
158 * The font point size.
160 protected int size;
162 protected float pointSize;
164 // The native peer for this font
165 private FontPeer peer;
167 /*************************************************************************/
170 * Static Methods
174 * Creates a <code>Font</code> object from the specified string, which
175 * is in one of the following formats:
176 * <p>
177 * <ul>
178 * <li>fontname-style-pointsize
179 * <li>fontname-style
180 * <li>fontname-pointsize
181 * <li>fontname
182 * </ul>
183 * <p>
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.
188 public static Font
189 decode(String fontspec)
191 String name = null;
192 int style = PLAIN;
193 int size = 12;
195 StringTokenizer st = new StringTokenizer(fontspec, "-");
196 while (st.hasMoreTokens())
198 String token = st.nextToken();
199 if (name == null)
201 name = token;
202 continue;
205 if (token.toUpperCase().equals("BOLD"))
207 style = BOLD;
208 continue;
210 if (token.toUpperCase().equals("ITALIC"))
212 style = ITALIC;
213 continue;
215 if (token.toUpperCase().equals("BOLDITALIC"))
217 style = BOLD + ITALIC;
218 continue;
221 int tokenval = 0;
224 tokenval = Integer.parseInt(token);
226 catch(NumberFormatException e) { ; }
228 if (tokenval != 0)
229 size = tokenval;
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.
246 public static Font
247 getFont(String propname, Font defval)
249 String propval = System.getProperty(propname);
250 if (propval != null)
251 return(decode(propval));
253 return(defval);
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.
266 public static Font
267 getFont(String propname)
269 return(getFont(propname, null));
272 /*************************************************************************/
275 * Constructors
279 * Initializes a new instance of <code>Font</code> with the specified
280 * attributes.
282 * @param name The name of the font.
283 * @param style The font style.
284 * @param size The font point size.
286 public
287 Font(String name, int style, int size)
289 this.name = name;
290 this.style = style;
291 this.size = size;
292 this.pointSize = size;
295 /*************************************************************************/
298 * Instance Methods
302 * Returns the name of the font.
304 * @return The name of the font.
306 public String
307 getName()
309 return(name);
312 /*************************************************************************/
315 * Returns the style of the font.
317 * @return The font style.
319 public int
320 getSize()
322 return(size);
325 public float
326 getSize2D()
328 return pointSize;
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>
338 * otherwise.
340 public boolean
341 isPlain()
343 if (style == PLAIN)
344 return(true);
345 else
346 return(false);
349 /*************************************************************************/
352 * Tests whether or not this font is bold.
354 * @return <code>true</code> if this font is bold, <code>false</code>
355 * otherwise.
357 public boolean
358 isBold()
360 if ((style == BOLD) || (style == (BOLD+ITALIC)))
361 return(true);
362 else
363 return(false);
366 /*************************************************************************/
369 * Tests whether or not this font is italic.
371 * @return <code>true</code> if this font is italic, <code>false</code>
372 * otherwise.
374 public boolean
375 isItalic()
377 if ((style == ITALIC) || (style == (BOLD+ITALIC)))
378 return(true);
379 else
380 return(false);
383 /*************************************************************************/
386 * Returns the system specific font family name.
388 * @return The system specific font family name.
390 public String
391 getFamily()
393 // FIXME: How do I implement this?
394 return(name);
397 public int
398 getStyle()
400 return style;
403 /*************************************************************************/
406 * Returns a native peer object for this font.
408 * @return A native peer object for this font.
410 public FontPeer
411 getPeer()
413 if (peer != null)
414 return(peer);
416 peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
417 return(peer);
420 /*************************************************************************/
423 * Returns a hash value for this font.
425 * @return A hash for this font.
427 public int
428 hashCode()
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:
438 * <P>
439 * <ul>
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.
443 * </ul>
445 * @return <code>true</code> if the specified object is equal to this
446 * object, <code>false</code> otherwise.
448 public boolean
449 equals(Object obj)
451 if (obj == null)
452 return(false);
454 if (!(obj instanceof Font))
455 return(false);
457 Font f = (Font)obj;
459 if (!f.name.equals(name))
460 return(false);
462 if (f.size != size)
463 return(false);
465 if (f.style != style)
466 return(false);
468 return(true);
471 /*************************************************************************/
474 * Returns a string representation of this font.
476 * @return A string representation of this font.
478 public String
479 toString()
481 return(getClass().getName() + "(name=" + name + ",style=" + style +
482 ",size=" + size + ")");
485 } // class Font