OSDN Git Service

2004-11-30 Thomas Fitzsimmons <fitzsim@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / Font.java
1 /* Font.java -- Font object
2    Copyright (C) 1999, 2002, 2004 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 gnu.java.awt.ClasspathToolkit;
42 import gnu.java.awt.peer.ClasspathFontPeer;
43
44 import java.awt.font.FontRenderContext;
45 import java.awt.font.GlyphVector;
46 import java.awt.font.LineMetrics;
47 import java.awt.geom.AffineTransform;
48 import java.awt.geom.Rectangle2D;
49 import java.awt.peer.FontPeer;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.Serializable;
53 import java.text.AttributedCharacterIterator;
54 import java.text.CharacterIterator;
55 import java.text.StringCharacterIterator;
56 import java.util.HashMap;
57 import java.util.Locale;
58 import java.util.Map;
59 import java.util.StringTokenizer;
60
61 /**
62  * This class represents a windowing system font.
63  *
64  * @author Aaron M. Renn (arenn@urbanophile.com)
65  * @author Warren Levy (warrenl@cygnus.com)
66  * @author Graydon Hoare (graydon@redhat.com)
67  */
68 public class Font implements Serializable
69 {
70
71 /*
72  * Static Variables
73  */
74
75 /**
76   * Constant indicating a "plain" font.
77   */
78 public static final int PLAIN = 0;
79
80 /**
81   * Constant indicating a "bold" font.
82   */
83 public static final int BOLD = 1;
84
85 /**
86   * Constant indicating an "italic" font.
87   */
88 public static final int ITALIC = 2;
89
90 /**
91  * Constant indicating the baseline mode characteristic of Roman.
92  */
93 public static final int ROMAN_BASELINE = 0;
94
95 /**
96  * Constant indicating the baseline mode characteristic of Chinese.
97  */
98 public static final int CENTER_BASELINE = 1;
99
100 /**
101  * Constant indicating the baseline mode characteristic of Devanigri.
102  */
103 public static final int HANGING_BASELINE = 2;  
104
105
106   /**
107    * Indicates to <code>createFont</code> that the supplied font data
108    * is in TrueType format.
109    *
110    * <p><em>Specification Note:</em> The Sun JavaDoc for J2SE 1.4 does
111    * not indicate whether this value also subsumes OpenType. OpenType
112    * is essentially the same format as TrueType, but allows to define
113    * glyph shapes in the same way as PostScript, using cubic bezier
114    * curves.
115    *
116    * @since 1.3
117    */
118   public static final int TRUETYPE_FONT = 0;
119
120
121   /**
122    * A flag for <code>layoutGlyphVector</code>, indicating that the
123    * orientation of a text run is from left to right.
124    *
125    * @since 1.4
126    */
127   public static final int LAYOUT_LEFT_TO_RIGHT = 0;
128
129
130   /**
131    * A flag for <code>layoutGlyphVector</code>, indicating that the
132    * orientation of a text run is from right to left.
133    *
134    * @since 1.4
135    */
136   public static final int LAYOUT_RIGHT_TO_LEFT = 1;
137
138
139   /**
140    * A flag for <code>layoutGlyphVector</code>, indicating that the
141    * text does not contain valid characters before the
142    * <code>start</code> position.  If this flag is set,
143    * <code>layoutGlyphVector</code> does not examine the text before
144    * <code>start</code>, even if this would be necessary to select the
145    * correct glyphs (e.g., for Arabic text).
146    *
147    * @since 1.4
148    */
149   public static final int LAYOUT_NO_START_CONTEXT = 2;
150
151
152   /**
153    * A flag for <code>layoutGlyphVector</code>, indicating that the
154    * text does not contain valid characters after the
155    * <code>limit</code> position.  If this flag is set,
156    * <code>layoutGlyphVector</code> does not examine the text after
157    * <code>limit</code>, even if this would be necessary to select the
158    * correct glyphs (e.g., for Arabic text).
159    *
160    * @since 1.4
161    */
162   public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
163
164   /**
165    * The logical name of this font.
166    *
167    * @since 1.0
168    */
169   protected String name;
170
171   /**
172    * The size of this font in pixels.
173    *
174    * @since 1.0
175    */
176   protected int size;
177
178   /**
179    * The style of this font -- PLAIN, BOLD, ITALIC or BOLD+ITALIC.
180    *
181    * @since 1.0
182    */
183   protected int style;
184
185 // Serialization constant
186 private static final long serialVersionUID = -4206021311591459213L;
187
188
189   // The ClasspathToolkit-provided peer which implements this font
190   private ClasspathFontPeer peer;
191
192 /*************************************************************************/
193
194 /*
195  * Static Methods
196  */
197
198 /**
199   * Creates a <code>Font</code> object from the specified string, which
200   * is in one of the following formats:
201   * <p>
202   * <ul>
203   * <li>fontname-style-pointsize
204   * <li>fontname-style
205   * <li>fontname-pointsize
206   * <li>fontname
207   * </ul>
208   * <p>
209   * The style should be one of BOLD, ITALIC, or BOLDITALIC.  The default
210   * style if none is specified is PLAIN.  The default size if none
211   * is specified is 12.
212   */
213   public static Font decode (String fontspec)
214 {
215   String name = null;
216   int style = PLAIN;
217   int size = 12;
218
219   StringTokenizer st = new StringTokenizer(fontspec, "-");
220   while (st.hasMoreTokens())
221     {
222       String token = st.nextToken();
223       if (name == null)
224         {
225           name = token;
226           continue;
227         }
228
229       if (token.toUpperCase().equals("BOLD"))
230         {
231           style = BOLD;
232           continue;
233         }
234       if (token.toUpperCase().equals("ITALIC"))
235         {
236           style = ITALIC;
237           continue;
238         }
239       if (token.toUpperCase().equals("BOLDITALIC"))
240         {
241             style = BOLD | ITALIC;
242           continue;
243         }
244
245       int tokenval = 0;
246       try
247         {
248           tokenval = Integer.parseInt(token);
249         }
250       catch(NumberFormatException e) { ; }
251
252       if (tokenval != 0)
253         size = tokenval;
254     }
255
256     HashMap attrs = new HashMap();
257     ClasspathFontPeer.copyStyleToAttrs (style, attrs);
258     ClasspathFontPeer.copySizeToAttrs (size, attrs);
259
260     return getFontFromToolkit (name, attrs);
261 }
262
263   /* These methods delegate to the toolkit. */
264
265   protected static ClasspathToolkit tk ()
266   {
267     return (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
268   }
269
270   /* Every factory method in Font should eventually call this. */
271   protected static Font getFontFromToolkit (String name, Map attribs)
272   {
273     return tk ().getFont (name, attribs);
274   }
275
276   /* Every Font constructor should eventually call this. */
277   protected static ClasspathFontPeer getPeerFromToolkit (String name, Map attrs)
278   {
279     return tk ().getClasspathFontPeer (name, attrs);
280   }
281
282
283 /*************************************************************************/
284
285 /**
286   * Returns a <code>Font</code> object from the passed property name.
287   *
288   * @param propname The name of the system property.
289   * @param defval Value to use if the property is not found.
290   *
291   * @return The requested font, or <code>default</code> if the property 
292   * not exist or is malformed.
293   */
294   public static Font getFont (String propname, Font defval)
295 {
296   String propval = System.getProperty(propname);
297   if (propval != null)
298       return decode (propval);
299     return defval;
300 }
301
302 /*************************************************************************/
303
304 /**
305   * Returns a <code>Font</code> object from the passed property name.
306   *
307   * @param propname The name of the system property.
308   *
309   * @return The requested font, or <code>null</code> if the property 
310   * not exist or is malformed.
311   */
312   public static Font getFont (String propname)
313 {
314     return getFont (propname, (Font)null);
315 }
316
317 /*************************************************************************/
318
319 /*
320  * Constructors
321  */
322
323 /**
324   * Initializes a new instance of <code>Font</code> with the specified
325   * attributes.
326   *
327   * @param name The name of the font.
328   * @param style The font style.
329   * @param size The font point size.
330   */
331
332   public Font (String name, int style, int size)
333   {
334     HashMap attrs = new HashMap();
335     ClasspathFontPeer.copyStyleToAttrs (style, attrs);
336     ClasspathFontPeer.copySizeToAttrs (size, attrs);
337     this.peer = getPeerFromToolkit (name, attrs);
338   }
339
340   public Font (Map attrs)
341   {
342     this(null, attrs);
343   }
344
345   /* This extra constructor is here to permit ClasspathToolkit and to build
346      a font with a "logical name" as well as attrs.  */
347   public Font (String name, Map attrs)
348   {
349     // If attrs is null, setting it to an empty HashMap will give this
350     // Font default attributes.
351     if (attrs == null)
352       attrs = new HashMap();
353     this.peer = getPeerFromToolkit (name, attrs);
354   }
355
356 /*************************************************************************/
357
358 /*
359  * Instance Methods
360  */
361
362 /**
363    * Returns the logical name of the font.  A logical name is the name the
364    * font was constructed with. It may be the name of a logical font (one
365    * of 6 required names in all java environments) or it may be a face
366    * name.
367   *
368   * @return The logical name of the font.
369   *
370   * @see getFamily()
371   * @see getFontName()
372   */
373   public String getName ()
374 {
375     return peer.getName (this);
376 }
377
378 /*************************************************************************/
379
380 /**
381   * Returns the style of the font.
382   * 
383   * @return The font style.
384   */
385   public int getSize ()
386 {
387     return (int) peer.getSize (this);
388 }
389
390   public float getSize2D ()
391 {
392     return peer.getSize (this);
393 }
394
395 /*************************************************************************/
396
397 /**
398   * Tests whether or not this is a plain font.  This will be true if
399   * and only if neither the bold nor the italics style is set.
400   *
401   * @return <code>true</code> if this is a plain font, <code>false</code>
402   * otherwise.
403   */
404   public boolean isPlain ()
405 {
406     return peer.isPlain (this); 
407 }
408
409 /*************************************************************************/
410
411 /**
412   * Tests whether or not this font is bold.
413   *
414   * @return <code>true</code> if this font is bold, <code>false</code>
415   * otherwise.
416   */
417   public boolean isBold ()
418 {
419     return peer.isBold (this);
420 }
421
422 /*************************************************************************/
423
424 /**
425   * Tests whether or not this font is italic.
426   *
427   * @return <code>true</code> if this font is italic, <code>false</code>
428   * otherwise.
429   */
430   public boolean isItalic ()
431 {
432     return peer.isItalic (this);
433 }
434
435 /*************************************************************************/
436
437 /**
438    * Returns the family name of this font. A family name describes a design
439    * or "brand name" (such as Helvetica or Palatino). It is less specific
440    * than a font face name (such as Helvetica Bold).
441   *
442   * @return A string containing the font family name.
443   *
444   * @since 1.2
445   *
446   * @see getName()
447   * @see getFontName()
448   * @see GraphicsEnvironment.getAvailableFontFamilyNames()
449   */
450   public String getFamily ()
451 {
452     return peer.getFamily (this);
453 }
454
455 /**
456   * Returns integer code representing the sum of style flags of this font, a
457   * combination of either {@link PLAIN}, {@link BOLD}, or {@link ITALIC}.
458   *
459   * @return code representing the style of this font.
460   *
461   * @see isPlain()
462   * @see isBold()
463   * @see isItalic()
464   */
465   public int getStyle ()
466 {
467     return peer.getStyle (this);
468 }
469
470 /**
471   * Checks if specified character maps to a glyph in this font.
472   *
473   * @param c The character to check.
474   *
475   * @return Whether the character has a corresponding glyph in this font.
476   *
477   * @since 1.2
478   */
479   public boolean canDisplay (char c)
480 {
481     return peer.canDisplay (this, c);    
482 }
483
484 /**
485   * Checks how much of a given string can be mapped to glyphs in 
486   * this font.
487   *
488   * @param s The string to check.
489   *
490   * @return The index of the first character in <code>s</code> which cannot
491   * be converted to a glyph by this font, or <code>-1</code> if all
492   * characters can be mapped to glyphs.
493   *
494   * @since 1.2
495   */
496   public int canDisplayUpTo (String s)
497 {
498     return peer.canDisplayUpTo (this, new StringCharacterIterator (s), 
499                                 0, s.length () - 1);
500 }
501
502 /**
503   * Checks how much of a given sequence of text can be mapped to glyphs in
504   * this font.
505   *
506   * @param text Array containing the text to check.
507   * @param start Position of first character to check in <code>text</code>.
508   * @param limit Position of last character to check in <code>text</code>.
509   *
510   * @return The index of the first character in the indicated range which
511   * cannot be converted to a glyph by this font, or <code>-1</code> if all
512   * characters can be mapped to glyphs.
513   *
514   * @since 1.2
515   *
516   * @throws IndexOutOfBoundsException if the range [start, limit] is
517   * invalid in <code>text</code>.
518   */
519   public int canDisplayUpTo (char[] text, int start, int limit)
520 {
521     return peer.canDisplayUpTo 
522       (this, new StringCharacterIterator (new String (text)), start, limit);
523 }
524
525 /**
526   * Checks how much of a given sequence of text can be mapped to glyphs in
527   * this font.
528   *
529   * @param i Iterator over the text to check.
530   * @param start Position of first character to check in <code>i</code>.
531   * @param limit Position of last character to check in <code>i</code>.
532   *
533   * @return The index of the first character in the indicated range which
534   * cannot be converted to a glyph by this font, or <code>-1</code> if all
535   * characters can be mapped to glyphs.
536   *
537   * @since 1.2
538   *
539   * @throws IndexOutOfBoundsException if the range [start, limit] is
540   * invalid in <code>i</code>.
541   */
542   public int canDisplayUpTo (CharacterIterator i, int start, int limit)
543 {
544     return peer.canDisplayUpTo (this, i, start, limit);    
545 }
546
547 /**
548   * Creates a new font with point size 1 and {@link PLAIN} style,
549   * reading font data from the provided input stream. The resulting font
550   * can have further fonts derived from it using its
551   * <code>deriveFont</code> method.
552   *
553   * @param fontFormat Integer code indicating the format the font data is
554   * in.Currently this can only be {@link TRUETYPE_FONT}.
555   * @param is {@link InputStream} from which font data will be read. This
556   * stream is not closed after font data is extracted.
557   *
558   * @return A new {@link Font} of the format indicated.
559   *
560   * @throws IllegalArgumentException if <code>fontType</code> is not
561   * recognized.
562   * @throws FontFormatException if data in InputStream is not of format
563   * indicated.
564   * @throws IOException if insufficient data is present on InputStream.
565   *
566   * @since 1.3
567   */
568   public static Font createFont (int fontFormat, InputStream is) 
569   throws FontFormatException, IOException
570 {
571     return tk().createFont (fontFormat, is);
572 }
573
574 /**
575   * Maps characters to glyphs in a one-to-one relationship, returning a new
576   * {@link GlyphVector} with a mapped glyph for each input character. This
577   * sort of mapping is often sufficient for some scripts such as Roman, but
578   * is inappropriate for scripts with special shaping or contextual layout
579   * requirements such as Arabic, Indic, Hebrew or Thai.
580   *
581   * @param ctx The rendering context used for precise glyph placement.
582   * @param str The string to convert to Glyphs.
583   *
584   * @return A new {@link GlyphVector} containing glyphs mapped from str,
585   * through the font's cmap table.
586   *
587   * @see layoutGlyphVector()
588   */
589   public GlyphVector createGlyphVector (FontRenderContext ctx, String str)
590 {
591     return peer.createGlyphVector (this, ctx, new StringCharacterIterator (str));
592 }
593
594 /**
595   * Maps characters to glyphs in a one-to-one relationship, returning a new
596   * {@link GlyphVector} with a mapped glyph for each input character. This
597   * sort of mapping is often sufficient for some scripts such as Roman, but
598   * is inappropriate for scripts with special shaping or contextual layout
599   * requirements such as Arabic, Indic, Hebrew or Thai.
600   *
601   * @param ctx The rendering context used for precise glyph placement.
602   * @param i Iterator over the text to convert to glyphs.
603   *
604   * @return A new {@link GlyphVector} containing glyphs mapped from str,
605   * through the font's cmap table.
606   *
607   * @see layoutGlyphVector()
608   */
609   public GlyphVector createGlyphVector (FontRenderContext ctx, CharacterIterator i)
610 {
611     return peer.createGlyphVector (this, ctx, i);
612 }
613
614 /**
615   * Maps characters to glyphs in a one-to-one relationship, returning a new
616   * {@link GlyphVector} with a mapped glyph for each input character. This
617   * sort of mapping is often sufficient for some scripts such as Roman, but
618   * is inappropriate for scripts with special shaping or contextual layout
619   * requirements such as Arabic, Indic, Hebrew or Thai.
620   *
621   * @param ctx The rendering context used for precise glyph placement.
622   * @param chars Array of characters to convert to glyphs.
623   *
624   * @return A new {@link GlyphVector} containing glyphs mapped from str,
625   * through the font's cmap table.
626   *
627   * @see layoutGlyphVector()
628   */
629   public GlyphVector createGlyphVector (FontRenderContext ctx, char[] chars)
630 {
631     return peer.createGlyphVector 
632       (this, ctx, new StringCharacterIterator (new String (chars)));
633 }
634
635 /**
636   * Extracts a sequence of glyphs from a font, returning a new {@link
637   * GlyphVector} with a mapped glyph for each input glyph code. 
638   *
639   * @param ctx The rendering context used for precise glyph placement.
640   * @param glyphCodes Array of characters to convert to glyphs.
641   *
642   * @return A new {@link GlyphVector} containing glyphs mapped from str,
643   * through the font's cmap table.
644   *
645   * @see layoutGlyphVector()
646   *
647   * @specnote This method is documented to perform character-to-glyph
648   * conversions, in the Sun documentation, but its second parameter name is
649   * "glyphCodes" and it is not clear to me why it would exist if its
650   * purpose was to transport character codes inside integers. I assume it
651   * is mis-documented in the Sun documentation.
652   */
653
654   public GlyphVector createGlyphVector (FontRenderContext ctx, int[] glyphCodes)
655 {
656     return peer.createGlyphVector (this, ctx, glyphCodes);
657 }
658
659 /**
660   * Produces a new {@link Font} based on the current font, adjusted to a
661   * new size and style.
662   *
663   * @param style The style of the newly created font.
664   * @param size The size of the newly created font.
665   *
666   * @return A clone of the current font, with the specified size and style.
667   *
668   * @since 1.2
669   */
670   public Font deriveFont (int style, float size)
671 {
672     return peer.deriveFont (this, style, size);
673 }
674
675 /**
676   * Produces a new {@link Font} based on the current font, adjusted to a
677   * new size.
678   *
679   * @param size The size of the newly created font.
680   *
681   * @return A clone of the current font, with the specified size.
682   *
683   * @since 1.2
684   */
685   public Font deriveFont (float size)
686 {
687     return peer.deriveFont (this, size);
688 }
689
690 /**
691   * Produces a new {@link Font} based on the current font, adjusted to a
692   * new style.
693   *
694   * @param style The style of the newly created font.
695   *
696   * @return A clone of the current font, with the specified style.
697   *
698   * @since 1.2
699   */
700   public Font deriveFont (int style)
701 {
702     return peer.deriveFont (this, style);
703 }
704
705 /**
706   * Produces a new {@link Font} based on the current font, adjusted to a
707   * new style and subjected to a new affine transformation.
708   *
709   * @param style The style of the newly created font.
710   * @param a The transformation to apply.
711   *
712   * @return A clone of the current font, with the specified style and
713   * transform.
714   *
715   * @throws IllegalArgumentException If transformation is
716   * <code>null</code>.
717   *
718   * @since 1.2
719   */
720   public Font deriveFont (int style, AffineTransform a)
721 {
722     if (a == null)
723       throw new IllegalArgumentException ("Affine transformation is null");
724
725     return peer.deriveFont (this, style, a);
726 }
727
728 /**
729   * Produces a new {@link Font} based on the current font, subjected
730   * to a new affine transformation.
731   *
732   * @param a The transformation to apply.
733   *
734   * @return A clone of the current font, with the specified transform.
735   *
736   * @throws IllegalArgumentException If transformation is
737   * <code>null</code>.
738   *
739   * @since 1.2
740   */
741   public Font deriveFont (AffineTransform a)
742 {
743     if (a == null)
744       throw new IllegalArgumentException ("Affine transformation is null");
745
746     return peer.deriveFont (this, a);
747 }
748
749 /**
750   * Produces a new {@link Font} based on the current font, adjusted to a
751   * new set of attributes.
752   *
753   * @param attributes Attributes of the newly created font.
754   *
755   * @return A clone of the current font, with the specified attributes.
756   *
757   * @since 1.2
758   */
759   public Font deriveFont (Map attributes)
760 {
761     return peer.deriveFont (this, attributes);
762 }
763
764 /**
765   * Returns a map of chracter attributes which this font currently has set.
766   *
767   * @return A map of chracter attributes which this font currently has set.
768   *
769   * @see getAvailableAttributes()
770   * @see java.text.AttributedCharacterIterator.Attribute
771   * @see java.awt.font.TextAttribute
772   */
773   public Map getAttributes ()
774 {
775     return peer.getAttributes (this);
776 }
777
778 /**
779   * Returns an array of chracter attribute keys which this font understands. 
780   *
781   * @return An array of chracter attribute keys which this font understands.
782   *
783   * @see getAttributes()
784   * @see java.text.AttributedCharacterIterator.Attribute
785   * @see java.awt.font.TextAttribute
786   */
787   public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
788 {
789     return peer.getAvailableAttributes (this);
790 }
791
792 /**
793   * Returns a baseline code (one of {@link ROMAN_BASELINE}, {@link
794   * CENTER_BASELINE} or {@link HANGING_BASELINE}) indicating which baseline
795   * this font will measure baseline offsets for, when presenting glyph
796   * metrics for a given character.
797   *
798   * Baseline offsets describe the position of a glyph relative to an
799   * invisible line drawn under, through the center of, or over a line of
800   * rendered text, respectively. Different scripts use different baseline
801   * modes, so clients should not assume all baseline offsets in a glyph
802   * vector are from a common baseline.
803   *
804   * @param c The character code to select a baseline mode for.
805   *
806   * @return The baseline mode which would be used in a glyph associated
807   * with the provided character.
808   *
809   * @since 1.2
810   *
811   * @see LineMetrics.getBaselineOffsets()
812   */
813   public byte getBaselineFor (char c)
814 {
815     return peer.getBaselineFor (this, c);
816 }
817
818 /**
819   * Returns the family name of this font. A family name describes a
820   * typographic style (such as Helvetica or Palatino). It is more specific
821   * than a logical font name (such as Sans Serif) but less specific than a
822   * font face name (such as Helvetica Bold).
823   *
824   * @param lc The locale in which to describe the name of the font family.
825   *
826   * @return A string containing the font family name, localized for the
827   * provided locale.
828   *
829   * @since 1.2
830   *
831   * @see getName()
832   * @see getFontName()
833   * @see GraphicsEnvironment.getAvailableFontFamilyNames()
834   * @see Locale
835   */
836   public String getFamily (Locale lc)
837 {
838     return peer.getFamily (this, lc); 
839 }
840
841 /**
842   * Returns a font appropriate for the given attribute set.
843   *
844   * @param attributes The attributes required for the new font.
845   *
846   * @return A new Font with the given attributes.
847   *
848   * @since 1.2
849   *
850   * @see TextAttribure  
851   */
852   public static Font getFont (Map attributes)
853 {
854     return getFontFromToolkit (null, attributes);
855 }
856
857 /**
858   * Returns the font face name of the font.  A font face name describes a
859   * specific variant of a font family (such as Helvetica Bold). It is more
860   * specific than both a font family name (such as Helvetica) and a logical
861   * font name (such as Sans Serif).
862   *
863   * @return The font face name of the font.
864   *
865   * @since 1.2
866   *
867   * @see getName()
868   * @see getFamily()
869   */
870   public String getFontName ()
871 {
872     return peer.getFontName (this);
873 }
874
875 /**
876   * Returns the font face name of the font.  A font face name describes a
877   * specific variant of a font family (such as Helvetica Bold). It is more
878    * specific than both a font family name (such as Helvetica).
879   *
880   * @param lc The locale in which to describe the name of the font face.
881   *
882   * @return A string containing the font face name, localized for the
883   * provided locale.
884   *
885   * @since 1.2
886   *
887   * @see getName()
888   * @see getFamily()
889   */
890   public String getFontName (Locale lc)
891 {
892     return peer.getFontName (this, lc);
893 }
894
895 /**
896   * Returns the italic angle of this font, a measurement of its slant when
897   * style is {@link ITALIC}. The precise meaning is the inverse slope of a
898   * caret line which "best measures" the font's italic posture.
899   *
900   * @return The italic angle.
901   *
902   * @see TextAttribute.POSTURE
903   */
904   public float getItalicAngle ()
905 {
906     return peer.getItalicAngle (this);
907 }
908
909 /**
910   * Returns a {@link LineMetrics} object constructed with the specified
911   * text and {@link FontRenderContext}. 
912   *
913   * @param text The string to calculate metrics from.
914   * @param begin Index of first character in <code>text</code> to measure.
915   * @param limit Index of last character in <code>text</code> to measure.
916   * @param rc Context for calculating precise glyph placement and hints.
917   *
918   * @return A new {@link LineMetrics} object.
919   *
920   * @throws IndexOutOfBoundsException if the range [begin, limit] is
921   * invalid in <code>text</code>.
922   */
923   public LineMetrics getLineMetrics(String text, int begin, 
924                                     int limit, FontRenderContext rc)
925 {
926     return peer.getLineMetrics (this, new StringCharacterIterator (text), 
927                                 begin, limit, rc);
928 }
929
930 /**
931   * Returns a {@link LineMetrics} object constructed with the specified
932   * text and {@link FontRenderContext}. 
933   *
934   * @param chars The string to calculate metrics from.
935   * @param begin Index of first character in <code>text</code> to measure.
936   * @param limit Index of last character in <code>text</code> to measure.
937   * @param rc Context for calculating precise glyph placement and hints.
938   *
939   * @return A new {@link LineMetrics} object.
940   *
941   * @throws IndexOutOfBoundsException if the range [begin, limit] is
942   * invalid in <code>chars</code>.
943   */
944   public LineMetrics getLineMetrics(char[] chars, int begin, 
945                                     int limit, FontRenderContext rc)
946 {
947     return peer.getLineMetrics (this, new StringCharacterIterator (new String(chars)), 
948                                 begin, limit, rc);
949 }
950
951 /**
952   * Returns a {@link LineMetrics} object constructed with the specified
953   * text and {@link FontRenderContext}. 
954   *
955   * @param ci The string to calculate metrics from.
956   * @param begin Index of first character in <code>text</code> to measure.
957   * @param limit Index of last character in <code>text</code> to measure.
958   * @param rc Context for calculating precise glyph placement and hints.
959   *
960   * @return A new {@link LineMetrics} object.
961   *
962   * @throws IndexOutOfBoundsException if the range [begin, limit] is
963   * invalid in <code>ci</code>.
964   */
965   public LineMetrics getLineMetrics (CharacterIterator ci, int begin, 
966                                      int limit, FontRenderContext rc)
967 {
968     return peer.getLineMetrics (this, ci, begin, limit, rc);
969 }
970
971 /**
972   * Returns the maximal bounding box of all the bounding boxes in this
973   * font, when the font's bounding boxes are evaluated in a given {@link
974   * FontRenderContext}
975   *
976   * @param rc Context in which to evaluate bounding boxes.
977   *
978   * @return The maximal bounding box.
979   */
980   public Rectangle2D getMaxCharBounds (FontRenderContext rc)
981 {
982     return peer.getMaxCharBounds (this, rc);
983 }
984
985 /**
986   * Returns the glyph code this font uses to represent missing glyphs. This
987   * code will be present in glyph vectors when the font was unable to
988   * locate a glyph to represent a particular character code.
989   *
990   * @return The missing glyph code.
991   *
992   * @since 1.2
993   */
994   public int getMissingGlyphCode ()
995 {
996     return peer.getMissingGlyphCode (this);
997 }
998
999 /**
1000   * Returns the overall number of glyphs in this font. This number is one
1001   * more than the greatest glyph code used in any glyph vectors this font
1002   * produces. In other words, glyph codes are taken from the range
1003   * <code>[ 0, getNumGlyphs() - 1 ]</code>.
1004   *
1005   * @return The number of glyphs in this font.
1006   * 
1007   * @since 1.2
1008   */
1009   public int getNumGlyphs ()
1010 {
1011     return peer.getMissingGlyphCode (this);
1012 }
1013
1014 /**
1015   * Returns the PostScript Name of this font.   
1016   *
1017   * @return The PostScript Name of this font.
1018   *
1019   * @since 1.2
1020   *
1021   * @see getName()
1022   * @see getFamily()
1023   * @see getFontName()
1024   */
1025   public String getPSName ()
1026 {
1027     return peer.getPostScriptName (this);
1028 }
1029
1030 /**
1031   * Returns the logical bounds of the specified string when rendered with this
1032   * font in the specified {@link FontRenderContext}. This box will include the
1033   * glyph origin, ascent, advance, height, and leading, but may not include all
1034   * diacritics or accents. To get the complete visual bounding box of all the
1035   * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1036   * {@link TextLayout}.
1037   *
1038   * @param str The string to measure.
1039   * @param frc The context in which to make the precise glyph measurements.
1040   * 
1041   * @return A bounding box covering the logical bounds of the specified text.
1042   *
1043   * @see createGlyphVector()
1044   */
1045   public Rectangle2D getStringBounds (String str, FontRenderContext frc)
1046 {
1047     return getStringBounds (str, 0, str.length () - 1, frc);
1048 }
1049
1050 /**
1051   * Returns the logical bounds of the specified string when rendered with this
1052   * font in the specified {@link FontRenderContext}. This box will include the
1053   * glyph origin, ascent, advance, height, and leading, but may not include all
1054   * diacritics or accents. To get the complete visual bounding box of all the
1055   * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1056   * {@link TextLayout}.
1057   *
1058   * @param str The string to measure.
1059   * @param begin Index of the first character in <code>str</code> to measure.
1060   * @param limit Index of the last character in <code>str</code> to measure.
1061   * @param frc The context in which to make the precise glyph measurements.
1062   * 
1063   * @return A bounding box covering the logical bounds of the specified text.
1064   *
1065   * @throws IndexOutOfBoundsException if the range [begin, limit] is
1066   * invalid in <code>str</code>.
1067   *
1068   * @since 1.2
1069   *
1070   * @see createGlyphVector()
1071   */
1072   public Rectangle2D getStringBounds (String str, int begin, 
1073                                       int limit, FontRenderContext frc)
1074 {
1075     return peer.getStringBounds (this, new StringCharacterIterator(str), begin, limit, frc);
1076 }
1077
1078 /**
1079   * Returns the logical bounds of the specified string when rendered with this
1080   * font in the specified {@link FontRenderContext}. This box will include the
1081   * glyph origin, ascent, advance, height, and leading, but may not include all
1082   * diacritics or accents. To get the complete visual bounding box of all the
1083   * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1084   * {@link TextLayout}.
1085   *
1086   * @param ci The text to measure.
1087   * @param begin Index of the first character in <code>ci</code> to measure.
1088   * @param limit Index of the last character in <code>ci</code> to measure.
1089   * @param frc The context in which to make the precise glyph measurements.
1090   * 
1091   * @return A bounding box covering the logical bounds of the specified text.
1092   *
1093   * @throws IndexOutOfBoundsException if the range [begin, limit] is
1094   * invalid in <code>ci</code>.
1095   *
1096   * @since 1.2
1097   *
1098   * @see createGlyphVector()
1099   */
1100   public Rectangle2D getStringBounds (CharacterIterator ci, int begin, 
1101                                       int limit, FontRenderContext frc)
1102 {
1103     return peer.getStringBounds (this, ci, begin, limit, frc);
1104 }
1105
1106 /**
1107   * Returns the logical bounds of the specified string when rendered with this
1108   * font in the specified {@link FontRenderContext}. This box will include the
1109   * glyph origin, ascent, advance, height, and leading, but may not include all
1110   * diacritics or accents. To get the complete visual bounding box of all the
1111   * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1112   * {@link TextLayout}.
1113   *
1114   * @param chars The text to measure.
1115   * @param begin Index of the first character in <code>ci</code> to measure.
1116   * @param limit Index of the last character in <code>ci</code> to measure.
1117   * @param frc The context in which to make the precise glyph measurements.
1118   * 
1119   * @return A bounding box covering the logical bounds of the specified text.
1120   *
1121   * @throws IndexOutOfBoundsException if the range [begin, limit] is
1122   * invalid in <code>chars</code>.
1123   *
1124   * @since 1.2
1125   *
1126   * @see createGlyphVector()
1127   */
1128   public Rectangle2D getStringBounds (char[] chars, int begin, 
1129                                       int limit, FontRenderContext frc)
1130 {
1131     return peer.getStringBounds (this, new StringCharacterIterator (new String (chars)), 
1132                                  begin, limit, frc);
1133 }
1134
1135 /**
1136   * Returns a copy of the affine transformation this font is currently
1137   * subject to, if any.
1138   *
1139   * @return The current transformation.
1140  */
1141   public AffineTransform getTransform ()
1142 {
1143     return peer.getTransform (this);
1144 }
1145
1146 /**
1147   * Indicates whether this font's line metrics are uniform. A font may be
1148   * composed of several "subfonts", each covering a different code range,
1149   * and each with their own line metrics. A font with no subfonts, or
1150   * subfonts with identical line metrics, is said to have "uniform" line
1151   * metrics.
1152   *
1153   * @return Whether this font has uniform line metrics.
1154   *
1155   * @see LineMetrics
1156   * @see getLineMetrics()
1157   */
1158   public boolean hasUniformLineMetrics ()
1159 {
1160     return peer.hasUniformLineMetrics (this);
1161 }
1162
1163 /**
1164   * Indicates whether this font is subject to a non-identity affine
1165   * transformation.
1166   *
1167   * @return <code>true</code> iff the font has a non-identity affine
1168   * transformation applied to it.
1169   */
1170   public boolean isTransformed ()
1171 {
1172     return peer.isTransformed (this);
1173 }
1174
1175 /**
1176   * Produces a glyph vector representing a full layout fo the specified
1177   * text in this font. Full layouts may include complex shaping and
1178   * reordering operations, for scripts such as Arabic or Hindi.
1179   *
1180   * Bidirectional (bidi) layout is not performed in this method; text
1181   * should have its bidi direction specified with one of the flags {@link
1182   * LAYOUT_LEFT_TO_RIGHT} or {@link LAYOUT_RIGHT_TO_LEFT}.
1183   *
1184   * Some types of layout (notably Arabic glyph shaping) may examine context
1185   * characters beyond the bounds of the indicated range, in order to select
1186   * an appropriate shape. The flags {@link LAYOUT_NO_START_CONTEXT} and
1187   * {@link LAYOUT_NO_LIMIT_CONTEXT} can be provided to prevent these extra
1188   * context areas from being examined, for instance if they contain invalid
1189   * characters.
1190   *
1191   * @param frc Context in which to perform the layout.
1192   * @param chars Text to perform layout on.
1193   * @param start Index of first character to perform layout on.
1194   * @param limit Index of last character to perform layout on.
1195   * @param flags Combination of flags controlling layout.
1196   *
1197   * @return A new {@link GlyphVector} representing the specified text.
1198   *
1199   * @throws IndexOutOfBoundsException if the range [begin, limit] is
1200   * invalid in <code>chars</code>. 
1201   */
1202   public GlyphVector layoutGlyphVector (FontRenderContext frc, 
1203                                         char[] chars, int start, 
1204                                         int limit, int flags)
1205 {
1206     return peer.layoutGlyphVector (this, frc, chars, start, limit, flags);
1207 }
1208
1209
1210 /**
1211   * Returns a native peer object for this font.
1212   *
1213   * @return A native peer object for this font.
1214   *
1215   * @deprecated
1216   */
1217   public FontPeer getPeer ()
1218 {
1219     return peer;
1220 }
1221
1222
1223 /**
1224   * Returns a hash value for this font.
1225   * 
1226   * @return A hash for this font.
1227   */
1228   public int hashCode()
1229 {
1230     return this.toString().hashCode();
1231 }
1232
1233
1234 /**
1235   * Tests whether or not the specified object is equal to this font.  This
1236   * will be true if and only if:
1237   * <P>
1238   * <ul>
1239   * <li>The object is not <code>null</code>.
1240   * <li>The object is an instance of <code>Font</code>.
1241   * <li>The object has the same names, style, size, and transform as this object.
1242   * </ul>
1243   *
1244   * @return <code>true</code> if the specified object is equal to this
1245   * object, <code>false</code> otherwise.
1246   */
1247 public boolean
1248 equals(Object obj)
1249 {
1250   if (obj == null)
1251     return(false);
1252
1253   if (!(obj instanceof Font))
1254     return(false);
1255
1256   Font f = (Font)obj;
1257
1258   return (f.getName ().equals (this.getName ()) &&
1259           f.getFamily ().equals (this.getFamily ()) &&
1260           f.getFontName ().equals (this.getFontName ()) &&
1261           f.getTransform ().equals (this.getTransform ()) &&
1262           f.getSize() == this.getSize() &&
1263           f.getStyle() == this.getStyle());
1264
1265
1266 /*************************************************************************/
1267
1268 /**
1269   * Returns a string representation of this font.
1270   *
1271   * @return A string representation of this font.
1272   */
1273 public String
1274 toString()
1275 {
1276   String styleString = "";
1277
1278   switch (getStyle ())
1279     {
1280     case 0:
1281       styleString = "plain";
1282       break;
1283     case 1:
1284       styleString = "bold";
1285       break;
1286     case 2:
1287       styleString = "italic";
1288       break;
1289     default:
1290       styleString = "unknown";
1291     }
1292
1293   return getClass ().getName () 
1294     + "[family=" + getFamily ()
1295     + ",name=" + getFontName ()
1296     + ",style=" + styleString
1297     + ",size=" + getSize () + "]";
1298 }
1299
1300
1301   /**
1302    * Determines the line metrics for a run of text.
1303    *
1304    * @param str the text run to be measured.
1305    *
1306    * @param frc the font rendering parameters that are used for the
1307    *        measurement. The exact placement and size of text slightly
1308    *        depends on device-specific characteristics, for instance
1309    *        the device resolution or anti-aliasing.  For this reason,
1310    *        the returned measurement will only be accurate if the
1311    *        passed <code>FontRenderContext</code> correctly reflects
1312    *        the relevant parameters. Hence, <code>frc</code> should be
1313    *        obtained from the same <code>Graphics2D</code> that will
1314    *        be used for drawing, and any rendering hints should be set
1315    *        to the desired values before obtaining <code>frc</code>.
1316    *
1317    * @see java.awt.Graphics2D#getFontRenderContext()
1318    */
1319   public LineMetrics getLineMetrics(String str, FontRenderContext frc)
1320   {
1321     return getLineMetrics (str, 0, str.length () - 1, frc);
1322   }
1323
1324 } // class Font 
1325