source: trunk/gcc/libjava/java/awt/Font.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.0 KB
Line 
1/* Font.java -- Font object
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.FontPeer;
42import java.util.StringTokenizer;
43
44/**
45 * This class represents a windowing system font.
46 *
47 * @author Aaron M. Renn (arenn@urbanophile.com)
48 * @author Warren Levy <warrenl@cygnus.com>
49 */
50public class Font implements java.io.Serializable
51{
52
53/*
54 * Static Variables
55 */
56
57/**
58 * Constant indicating a "plain" font.
59 */
60public static final int PLAIN = 0;
61
62/**
63 * Constant indicating a "bold" font.
64 */
65public static final int BOLD = 1;
66
67/**
68 * Constant indicating an "italic" font.
69 */
70public static final int ITALIC = 2;
71
72public static final int ROMAN_BASELINE = 0;
73public static final int CENTER_BASELINE = 1;
74public static final int HANGING_BASELINE = 2;
75
76
77 /**
78 * Indicates to <code>createFont</code> that the supplied font data
79 * is in TrueType format.
80 *
81 * <p><em>Specification Note:</em> The Sun JavaDoc for J2SE 1.4 does
82 * not indicate whether this value also subsumes OpenType. OpenType
83 * is essentially the same format as TrueType, but allows to define
84 * glyph shapes in the same way as PostScript, using cubic bezier
85 * curves.
86 *
87 * @since 1.3
88 */
89 public static final int TRUETYPE_FONT = 0;
90
91
92 /**
93 * A flag for <code>layoutGlyphVector</code>, indicating that the
94 * orientation of a text run is from left to right.
95 *
96 * @since 1.4
97 */
98 public static final int LAYOUT_LEFT_TO_RIGHT = 0;
99
100
101 /**
102 * A flag for <code>layoutGlyphVector</code>, indicating that the
103 * orientation of a text run is from right to left.
104 *
105 * @since 1.4
106 */
107 public static final int LAYOUT_RIGHT_TO_LEFT = 1;
108
109
110 /**
111 * A flag for <code>layoutGlyphVector</code>, indicating that the
112 * text does not contain valid characters before the
113 * <code>start</code> position. If this flag is set,
114 * <code>layoutGlyphVector</code> does not examine the text before
115 * <code>start</code>, even if this would be necessary to select the
116 * correct glyphs (e.g., for Arabic text).
117 *
118 * @since 1.4
119 */
120 public static final int LAYOUT_NO_START_CONTEXT = 2;
121
122
123 /**
124 * A flag for <code>layoutGlyphVector</code>, indicating that the
125 * text does not contain valid characters after the
126 * <code>limit</code> position. If this flag is set,
127 * <code>layoutGlyphVector</code> does not examine the text after
128 * <code>limit</code>, even if this would be necessary to select the
129 * correct glyphs (e.g., for Arabic text).
130 *
131 * @since 1.4
132 */
133 public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
134
135
136// Serialization constant
137private static final long serialVersionUID = -4206021311591459213L;
138
139/*************************************************************************/
140
141/*
142 * Instance Variables
143 */
144
145/**
146 * The name of this font
147 */
148protected String name;
149
150/**
151 * The font style, which is a combination (by summing, not OR-ing) of
152 * the font style constants in this class.
153 */
154protected int style;
155
156/**
157 * The font point size.
158 */
159protected int size;
160
161protected float pointSize;
162
163// The native peer for this font
164private FontPeer peer;
165
166/*************************************************************************/
167
168/*
169 * Static Methods
170 */
171
172/**
173 * Creates a <code>Font</code> object from the specified string, which
174 * is in one of the following formats:
175 * <p>
176 * <ul>
177 * <li>fontname-style-pointsize
178 * <li>fontname-style
179 * <li>fontname-pointsize
180 * <li>fontname
181 * </ul>
182 * <p>
183 * The style should be one of BOLD, ITALIC, or BOLDITALIC. The default
184 * style if none is specified is PLAIN. The default size if none
185 * is specified is 12.
186 */
187public static Font
188decode(String fontspec)
189{
190 String name = null;
191 int style = PLAIN;
192 int size = 12;
193
194 StringTokenizer st = new StringTokenizer(fontspec, "-");
195 while (st.hasMoreTokens())
196 {
197 String token = st.nextToken();
198 if (name == null)
199 {
200 name = token;
201 continue;
202 }
203
204 if (token.toUpperCase().equals("BOLD"))
205 {
206 style = BOLD;
207 continue;
208 }
209 if (token.toUpperCase().equals("ITALIC"))
210 {
211 style = ITALIC;
212 continue;
213 }
214 if (token.toUpperCase().equals("BOLDITALIC"))
215 {
216 style = BOLD + ITALIC;
217 continue;
218 }
219
220 int tokenval = 0;
221 try
222 {
223 tokenval = Integer.parseInt(token);
224 }
225 catch(NumberFormatException e) { ; }
226
227 if (tokenval != 0)
228 size = tokenval;
229 }
230
231 return(new Font(name, style, size));
232}
233
234/*************************************************************************/
235
236/**
237 * Returns a <code>Font</code> object from the passed property name.
238 *
239 * @param propname The name of the system property.
240 * @param default Value to use if the property is not found.
241 *
242 * @return The requested font, or <code>default</code> if the property
243 * not exist or is malformed.
244 */
245public static Font
246getFont(String propname, Font defval)
247{
248 String propval = System.getProperty(propname);
249 if (propval != null)
250 return(decode(propval));
251
252 return(defval);
253}
254
255/*************************************************************************/
256
257/**
258 * Returns a <code>Font</code> object from the passed property name.
259 *
260 * @param propname The name of the system property.
261 *
262 * @return The requested font, or <code>null</code> if the property
263 * not exist or is malformed.
264 */
265public static Font
266getFont(String propname)
267{
268 return(getFont(propname, null));
269}
270
271/*************************************************************************/
272
273/*
274 * Constructors
275 */
276
277/**
278 * Initializes a new instance of <code>Font</code> with the specified
279 * attributes.
280 *
281 * @param name The name of the font.
282 * @param style The font style.
283 * @param size The font point size.
284 */
285public
286Font(String name, int style, int size)
287{
288 this.name = name;
289 this.style = style;
290 this.size = size;
291 this.pointSize = size;
292}
293
294/*************************************************************************/
295
296/*
297 * Instance Methods
298 */
299
300/**
301 * Returns the name of the font.
302 *
303 * @return The name of the font.
304 */
305public String
306getName()
307{
308 return(name);
309}
310
311/*************************************************************************/
312
313/**
314 * Returns the style of the font.
315 *
316 * @return The font style.
317 */
318public int
319getSize()
320{
321 return(size);
322}
323
324public float
325getSize2D()
326{
327 return pointSize;
328}
329
330/*************************************************************************/
331
332/**
333 * Tests whether or not this is a plain font. This will be true if
334 * and only if neither the bold nor the italics style is set.
335 *
336 * @return <code>true</code> if this is a plain font, <code>false</code>
337 * otherwise.
338 */
339public boolean
340isPlain()
341{
342 if (style == PLAIN)
343 return(true);
344 else
345 return(false);
346}
347
348/*************************************************************************/
349
350/**
351 * Tests whether or not this font is bold.
352 *
353 * @return <code>true</code> if this font is bold, <code>false</code>
354 * otherwise.
355 */
356public boolean
357isBold()
358{
359 if ((style == BOLD) || (style == (BOLD+ITALIC)))
360 return(true);
361 else
362 return(false);
363}
364
365/*************************************************************************/
366
367/**
368 * Tests whether or not this font is italic.
369 *
370 * @return <code>true</code> if this font is italic, <code>false</code>
371 * otherwise.
372 */
373public boolean
374isItalic()
375{
376 if ((style == ITALIC) || (style == (BOLD+ITALIC)))
377 return(true);
378 else
379 return(false);
380}
381
382/*************************************************************************/
383
384/**
385 * Returns the system specific font family name.
386 *
387 * @return The system specific font family name.
388 */
389public String
390getFamily()
391{
392 // FIXME: How do I implement this?
393 return(name);
394}
395
396public int
397getStyle()
398{
399 return style;
400}
401
402/*************************************************************************/
403
404/**
405 * Returns a native peer object for this font.
406 *
407 * @return A native peer object for this font.
408 */
409public FontPeer
410getPeer()
411{
412 if (peer != null)
413 return(peer);
414
415 peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
416 return(peer);
417}
418
419/*************************************************************************/
420
421/**
422 * Returns a hash value for this font.
423 *
424 * @return A hash for this font.
425 */
426public int
427hashCode()
428{
429 return((new String(name + size + style)).hashCode());
430}
431
432/*************************************************************************/
433
434/**
435 * Tests whether or not the specified object is equal to this font. This
436 * will be true if and only if:
437 * <P>
438 * <ul>
439 * <li>The object is not <code>null</code>.
440 * <li>The object is an instance of <code>Font</code>.
441 * <li>The object has the same name, style, and size as this object.
442 * </ul>
443 *
444 * @return <code>true</code> if the specified object is equal to this
445 * object, <code>false</code> otherwise.
446 */
447public boolean
448equals(Object obj)
449{
450 if (obj == null)
451 return(false);
452
453 if (!(obj instanceof Font))
454 return(false);
455
456 Font f = (Font)obj;
457
458 if (!f.name.equals(name))
459 return(false);
460
461 if (f.size != size)
462 return(false);
463
464 if (f.style != style)
465 return(false);
466
467 return(true);
468}
469
470/*************************************************************************/
471
472/**
473 * Returns a string representation of this font.
474 *
475 * @return A string representation of this font.
476 */
477public String
478toString()
479{
480 return(getClass().getName() + "(name=" + name + ",style=" + style +
481 ",size=" + size + ")");
482}
483
484} // class Font
485
Note: See TracBrowser for help on using the repository browser.