1 | /* Font.java -- Font object
|
---|
2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.awt;
|
---|
40 |
|
---|
41 | import java.awt.peer.FontPeer;
|
---|
42 | import 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 | */
|
---|
50 | public class Font implements java.io.Serializable
|
---|
51 | {
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Static Variables
|
---|
55 | */
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Constant indicating a "plain" font.
|
---|
59 | */
|
---|
60 | public static final int PLAIN = 0;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constant indicating a "bold" font.
|
---|
64 | */
|
---|
65 | public static final int BOLD = 1;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Constant indicating an "italic" font.
|
---|
69 | */
|
---|
70 | public static final int ITALIC = 2;
|
---|
71 |
|
---|
72 | public static final int ROMAN_BASELINE = 0;
|
---|
73 | public static final int CENTER_BASELINE = 1;
|
---|
74 | public 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
|
---|
137 | private static final long serialVersionUID = -4206021311591459213L;
|
---|
138 |
|
---|
139 | /*************************************************************************/
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Instance Variables
|
---|
143 | */
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * The name of this font
|
---|
147 | */
|
---|
148 | protected 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 | */
|
---|
154 | protected int style;
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * The font point size.
|
---|
158 | */
|
---|
159 | protected int size;
|
---|
160 |
|
---|
161 | protected float pointSize;
|
---|
162 |
|
---|
163 | // The native peer for this font
|
---|
164 | private 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 | */
|
---|
187 | public static Font
|
---|
188 | decode(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 | */
|
---|
245 | public static Font
|
---|
246 | getFont(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 | */
|
---|
265 | public static Font
|
---|
266 | getFont(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 | */
|
---|
285 | public
|
---|
286 | Font(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 | */
|
---|
305 | public String
|
---|
306 | getName()
|
---|
307 | {
|
---|
308 | return(name);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*************************************************************************/
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Returns the style of the font.
|
---|
315 | *
|
---|
316 | * @return The font style.
|
---|
317 | */
|
---|
318 | public int
|
---|
319 | getSize()
|
---|
320 | {
|
---|
321 | return(size);
|
---|
322 | }
|
---|
323 |
|
---|
324 | public float
|
---|
325 | getSize2D()
|
---|
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 | */
|
---|
339 | public boolean
|
---|
340 | isPlain()
|
---|
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 | */
|
---|
356 | public boolean
|
---|
357 | isBold()
|
---|
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 | */
|
---|
373 | public boolean
|
---|
374 | isItalic()
|
---|
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 | */
|
---|
389 | public String
|
---|
390 | getFamily()
|
---|
391 | {
|
---|
392 | // FIXME: How do I implement this?
|
---|
393 | return(name);
|
---|
394 | }
|
---|
395 |
|
---|
396 | public int
|
---|
397 | getStyle()
|
---|
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 | */
|
---|
409 | public FontPeer
|
---|
410 | getPeer()
|
---|
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 | */
|
---|
426 | public int
|
---|
427 | hashCode()
|
---|
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 | */
|
---|
447 | public boolean
|
---|
448 | equals(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 | */
|
---|
477 | public String
|
---|
478 | toString()
|
---|
479 | {
|
---|
480 | return(getClass().getName() + "(name=" + name + ",style=" + style +
|
---|
481 | ",size=" + size + ")");
|
---|
482 | }
|
---|
483 |
|
---|
484 | } // class Font
|
---|
485 |
|
---|