Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / Font.java
blobc43297759caa974d2eeb70f68f227bc26d106348
1 /* Font.java -- Font object
2 Copyright (C) 1999, 2002, 2004, 2005 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 gnu.java.awt.ClasspathToolkit;
42 import gnu.java.awt.peer.ClasspathFontPeer;
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;
61 /**
62 * This class represents a windowing system font.
64 * @author Aaron M. Renn (arenn@urbanophile.com)
65 * @author Warren Levy (warrenl@cygnus.com)
66 * @author Graydon Hoare (graydon@redhat.com)
68 public class Font implements Serializable
72 * Static Variables
75 /**
76 * Constant indicating a "plain" font.
78 public static final int PLAIN = 0;
80 /**
81 * Constant indicating a "bold" font.
83 public static final int BOLD = 1;
85 /**
86 * Constant indicating an "italic" font.
88 public static final int ITALIC = 2;
90 /**
91 * Constant indicating the baseline mode characteristic of Roman.
93 public static final int ROMAN_BASELINE = 0;
95 /**
96 * Constant indicating the baseline mode characteristic of Chinese.
98 public static final int CENTER_BASELINE = 1;
101 * Constant indicating the baseline mode characteristic of Devanigri.
103 public static final int HANGING_BASELINE = 2;
107 * Indicates to <code>createFont</code> that the supplied font data
108 * is in TrueType format.
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.
116 * @since 1.3
118 public static final int TRUETYPE_FONT = 0;
122 * A flag for <code>layoutGlyphVector</code>, indicating that the
123 * orientation of a text run is from left to right.
125 * @since 1.4
127 public static final int LAYOUT_LEFT_TO_RIGHT = 0;
131 * A flag for <code>layoutGlyphVector</code>, indicating that the
132 * orientation of a text run is from right to left.
134 * @since 1.4
136 public static final int LAYOUT_RIGHT_TO_LEFT = 1;
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).
147 * @since 1.4
149 public static final int LAYOUT_NO_START_CONTEXT = 2;
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).
160 * @since 1.4
162 public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
165 * The logical name of this font.
167 * @since 1.0
169 protected String name;
172 * The size of this font in pixels.
174 * @since 1.0
176 protected int size;
179 * The style of this font -- PLAIN, BOLD, ITALIC or BOLD+ITALIC.
181 * @since 1.0
183 protected int style;
185 // Serialization constant
186 private static final long serialVersionUID = -4206021311591459213L;
189 // The ClasspathToolkit-provided peer which implements this font
190 private ClasspathFontPeer peer;
192 /*************************************************************************/
195 * Static Methods
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.
213 public static Font decode (String fontspec)
215 String name = null;
216 int style = PLAIN;
217 int size = 12;
219 StringTokenizer st = new StringTokenizer(fontspec, "-");
220 while (st.hasMoreTokens())
222 String token = st.nextToken();
223 if (name == null)
225 name = token;
226 continue;
229 if (token.toUpperCase().equals("BOLD"))
231 style = BOLD;
232 continue;
234 if (token.toUpperCase().equals("ITALIC"))
236 style = ITALIC;
237 continue;
239 if (token.toUpperCase().equals("BOLDITALIC"))
241 style = BOLD | ITALIC;
242 continue;
245 int tokenval = 0;
248 tokenval = Integer.parseInt(token);
250 catch(NumberFormatException e)
252 // Ignored.
255 if (tokenval != 0)
256 size = tokenval;
259 HashMap attrs = new HashMap();
260 ClasspathFontPeer.copyStyleToAttrs (style, attrs);
261 ClasspathFontPeer.copySizeToAttrs (size, attrs);
263 return getFontFromToolkit (name, attrs);
266 /* These methods delegate to the toolkit. */
268 protected static ClasspathToolkit tk ()
270 return (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
273 /* Every factory method in Font should eventually call this. */
274 protected static Font getFontFromToolkit (String name, Map attribs)
276 return tk ().getFont (name, attribs);
279 /* Every Font constructor should eventually call this. */
280 protected static ClasspathFontPeer getPeerFromToolkit (String name, Map attrs)
282 return tk ().getClasspathFontPeer (name, attrs);
286 /*************************************************************************/
289 * Returns a <code>Font</code> object from the passed property name.
291 * @param propname The name of the system property.
292 * @param defval Value to use if the property is not found.
294 * @return The requested font, or <code>default</code> if the property
295 * not exist or is malformed.
297 public static Font getFont (String propname, Font defval)
299 String propval = System.getProperty(propname);
300 if (propval != null)
301 return decode (propval);
302 return defval;
305 /*************************************************************************/
308 * Returns a <code>Font</code> object from the passed property name.
310 * @param propname The name of the system property.
312 * @return The requested font, or <code>null</code> if the property
313 * not exist or is malformed.
315 public static Font getFont (String propname)
317 return getFont (propname, (Font)null);
320 /*************************************************************************/
323 * Constructors
327 * Initializes a new instance of <code>Font</code> with the specified
328 * attributes.
330 * @param name The name of the font.
331 * @param style The font style.
332 * @param size The font point size.
335 public Font (String name, int style, int size)
337 HashMap attrs = new HashMap();
338 ClasspathFontPeer.copyStyleToAttrs (style, attrs);
339 ClasspathFontPeer.copySizeToAttrs (size, attrs);
340 this.peer = getPeerFromToolkit (name, attrs);
343 public Font (Map attrs)
345 this(null, attrs);
348 /* This extra constructor is here to permit ClasspathToolkit and to build
349 a font with a "logical name" as well as attrs. */
350 public Font (String name, Map attrs)
352 // If attrs is null, setting it to an empty HashMap will give this
353 // Font default attributes.
354 if (attrs == null)
355 attrs = new HashMap();
356 this.peer = getPeerFromToolkit (name, attrs);
359 /*************************************************************************/
362 * Instance Methods
366 * Returns the logical name of the font. A logical name is the name the
367 * font was constructed with. It may be the name of a logical font (one
368 * of 6 required names in all java environments) or it may be a face
369 * name.
371 * @return The logical name of the font.
373 * @see getFamily()
374 * @see getFontName()
376 public String getName ()
378 return peer.getName (this);
381 /*************************************************************************/
384 * Returns the style of the font.
386 * @return The font style.
388 public int getSize ()
390 return (int) peer.getSize (this);
393 public float getSize2D ()
395 return peer.getSize (this);
398 /*************************************************************************/
401 * Tests whether or not this is a plain font. This will be true if
402 * and only if neither the bold nor the italics style is set.
404 * @return <code>true</code> if this is a plain font, <code>false</code>
405 * otherwise.
407 public boolean isPlain ()
409 return peer.isPlain (this);
412 /*************************************************************************/
415 * Tests whether or not this font is bold.
417 * @return <code>true</code> if this font is bold, <code>false</code>
418 * otherwise.
420 public boolean isBold ()
422 return peer.isBold (this);
425 /*************************************************************************/
428 * Tests whether or not this font is italic.
430 * @return <code>true</code> if this font is italic, <code>false</code>
431 * otherwise.
433 public boolean isItalic ()
435 return peer.isItalic (this);
438 /*************************************************************************/
441 * Returns the family name of this font. A family name describes a design
442 * or "brand name" (such as Helvetica or Palatino). It is less specific
443 * than a font face name (such as Helvetica Bold).
445 * @return A string containing the font family name.
447 * @since 1.2
449 * @see getName()
450 * @see getFontName()
451 * @see GraphicsEnvironment.getAvailableFontFamilyNames()
453 public String getFamily ()
455 return peer.getFamily (this);
459 * Returns integer code representing the sum of style flags of this font, a
460 * combination of either {@link PLAIN}, {@link BOLD}, or {@link ITALIC}.
462 * @return code representing the style of this font.
464 * @see isPlain()
465 * @see isBold()
466 * @see isItalic()
468 public int getStyle ()
470 return peer.getStyle (this);
474 * Checks if specified character maps to a glyph in this font.
476 * @param c The character to check.
478 * @return Whether the character has a corresponding glyph in this font.
480 * @since 1.2
482 public boolean canDisplay (char c)
484 return peer.canDisplay (this, c);
488 * Checks how much of a given string can be mapped to glyphs in
489 * this font.
491 * @param s The string to check.
493 * @return The index of the first character in <code>s</code> which cannot
494 * be converted to a glyph by this font, or <code>-1</code> if all
495 * characters can be mapped to glyphs.
497 * @since 1.2
499 public int canDisplayUpTo (String s)
501 return peer.canDisplayUpTo (this, new StringCharacterIterator (s),
502 0, s.length () - 1);
506 * Checks how much of a given sequence of text can be mapped to glyphs in
507 * this font.
509 * @param text Array containing the text to check.
510 * @param start Position of first character to check in <code>text</code>.
511 * @param limit Position of last character to check in <code>text</code>.
513 * @return The index of the first character in the indicated range which
514 * cannot be converted to a glyph by this font, or <code>-1</code> if all
515 * characters can be mapped to glyphs.
517 * @since 1.2
519 * @throws IndexOutOfBoundsException if the range [start, limit] is
520 * invalid in <code>text</code>.
522 public int canDisplayUpTo (char[] text, int start, int limit)
524 return peer.canDisplayUpTo
525 (this, new StringCharacterIterator (new String (text)), start, limit);
529 * Checks how much of a given sequence of text can be mapped to glyphs in
530 * this font.
532 * @param i Iterator over the text to check.
533 * @param start Position of first character to check in <code>i</code>.
534 * @param limit Position of last character to check in <code>i</code>.
536 * @return The index of the first character in the indicated range which
537 * cannot be converted to a glyph by this font, or <code>-1</code> if all
538 * characters can be mapped to glyphs.
540 * @since 1.2
542 * @throws IndexOutOfBoundsException if the range [start, limit] is
543 * invalid in <code>i</code>.
545 public int canDisplayUpTo (CharacterIterator i, int start, int limit)
547 return peer.canDisplayUpTo (this, i, start, limit);
551 * Creates a new font with point size 1 and {@link PLAIN} style,
552 * reading font data from the provided input stream. The resulting font
553 * can have further fonts derived from it using its
554 * <code>deriveFont</code> method.
556 * @param fontFormat Integer code indicating the format the font data is
557 * in.Currently this can only be {@link TRUETYPE_FONT}.
558 * @param is {@link InputStream} from which font data will be read. This
559 * stream is not closed after font data is extracted.
561 * @return A new {@link Font} of the format indicated.
563 * @throws IllegalArgumentException if <code>fontType</code> is not
564 * recognized.
565 * @throws FontFormatException if data in InputStream is not of format
566 * indicated.
567 * @throws IOException if insufficient data is present on InputStream.
569 * @since 1.3
571 public static Font createFont (int fontFormat, InputStream is)
572 throws FontFormatException, IOException
574 return tk().createFont (fontFormat, is);
578 * Maps characters to glyphs in a one-to-one relationship, returning a new
579 * {@link GlyphVector} with a mapped glyph for each input character. This
580 * sort of mapping is often sufficient for some scripts such as Roman, but
581 * is inappropriate for scripts with special shaping or contextual layout
582 * requirements such as Arabic, Indic, Hebrew or Thai.
584 * @param ctx The rendering context used for precise glyph placement.
585 * @param str The string to convert to Glyphs.
587 * @return A new {@link GlyphVector} containing glyphs mapped from str,
588 * through the font's cmap table.
590 * @see layoutGlyphVector()
592 public GlyphVector createGlyphVector (FontRenderContext ctx, String str)
594 return peer.createGlyphVector (this, ctx, new StringCharacterIterator (str));
598 * Maps characters to glyphs in a one-to-one relationship, returning a new
599 * {@link GlyphVector} with a mapped glyph for each input character. This
600 * sort of mapping is often sufficient for some scripts such as Roman, but
601 * is inappropriate for scripts with special shaping or contextual layout
602 * requirements such as Arabic, Indic, Hebrew or Thai.
604 * @param ctx The rendering context used for precise glyph placement.
605 * @param i Iterator over the text to convert to glyphs.
607 * @return A new {@link GlyphVector} containing glyphs mapped from str,
608 * through the font's cmap table.
610 * @see layoutGlyphVector()
612 public GlyphVector createGlyphVector (FontRenderContext ctx, CharacterIterator i)
614 return peer.createGlyphVector (this, ctx, i);
618 * Maps characters to glyphs in a one-to-one relationship, returning a new
619 * {@link GlyphVector} with a mapped glyph for each input character. This
620 * sort of mapping is often sufficient for some scripts such as Roman, but
621 * is inappropriate for scripts with special shaping or contextual layout
622 * requirements such as Arabic, Indic, Hebrew or Thai.
624 * @param ctx The rendering context used for precise glyph placement.
625 * @param chars Array of characters to convert to glyphs.
627 * @return A new {@link GlyphVector} containing glyphs mapped from str,
628 * through the font's cmap table.
630 * @see layoutGlyphVector()
632 public GlyphVector createGlyphVector (FontRenderContext ctx, char[] chars)
634 return peer.createGlyphVector
635 (this, ctx, new StringCharacterIterator (new String (chars)));
639 * Extracts a sequence of glyphs from a font, returning a new {@link
640 * GlyphVector} with a mapped glyph for each input glyph code.
642 * @param ctx The rendering context used for precise glyph placement.
643 * @param glyphCodes Array of characters to convert to glyphs.
645 * @return A new {@link GlyphVector} containing glyphs mapped from str,
646 * through the font's cmap table.
648 * @see layoutGlyphVector()
650 * @specnote This method is documented to perform character-to-glyph
651 * conversions, in the Sun documentation, but its second parameter name is
652 * "glyphCodes" and it is not clear to me why it would exist if its
653 * purpose was to transport character codes inside integers. I assume it
654 * is mis-documented in the Sun documentation.
657 public GlyphVector createGlyphVector (FontRenderContext ctx, int[] glyphCodes)
659 return peer.createGlyphVector (this, ctx, glyphCodes);
663 * Produces a new {@link Font} based on the current font, adjusted to a
664 * new size and style.
666 * @param style The style of the newly created font.
667 * @param size The size of the newly created font.
669 * @return A clone of the current font, with the specified size and style.
671 * @since 1.2
673 public Font deriveFont (int style, float size)
675 return peer.deriveFont (this, style, size);
679 * Produces a new {@link Font} based on the current font, adjusted to a
680 * new size.
682 * @param size The size of the newly created font.
684 * @return A clone of the current font, with the specified size.
686 * @since 1.2
688 public Font deriveFont (float size)
690 return peer.deriveFont (this, size);
694 * Produces a new {@link Font} based on the current font, adjusted to a
695 * new style.
697 * @param style The style of the newly created font.
699 * @return A clone of the current font, with the specified style.
701 * @since 1.2
703 public Font deriveFont (int style)
705 return peer.deriveFont (this, style);
709 * Produces a new {@link Font} based on the current font, adjusted to a
710 * new style and subjected to a new affine transformation.
712 * @param style The style of the newly created font.
713 * @param a The transformation to apply.
715 * @return A clone of the current font, with the specified style and
716 * transform.
718 * @throws IllegalArgumentException If transformation is
719 * <code>null</code>.
721 * @since 1.2
723 public Font deriveFont (int style, AffineTransform a)
725 if (a == null)
726 throw new IllegalArgumentException ("Affine transformation is null");
728 return peer.deriveFont (this, style, a);
732 * Produces a new {@link Font} based on the current font, subjected
733 * to a new affine transformation.
735 * @param a The transformation to apply.
737 * @return A clone of the current font, with the specified transform.
739 * @throws IllegalArgumentException If transformation is
740 * <code>null</code>.
742 * @since 1.2
744 public Font deriveFont (AffineTransform a)
746 if (a == null)
747 throw new IllegalArgumentException ("Affine transformation is null");
749 return peer.deriveFont (this, a);
753 * Produces a new {@link Font} based on the current font, adjusted to a
754 * new set of attributes.
756 * @param attributes Attributes of the newly created font.
758 * @return A clone of the current font, with the specified attributes.
760 * @since 1.2
762 public Font deriveFont (Map attributes)
764 return peer.deriveFont (this, attributes);
768 * Returns a map of chracter attributes which this font currently has set.
770 * @return A map of chracter attributes which this font currently has set.
772 * @see getAvailableAttributes()
773 * @see java.text.AttributedCharacterIterator.Attribute
774 * @see java.awt.font.TextAttribute
776 public Map getAttributes ()
778 return peer.getAttributes (this);
782 * Returns an array of chracter attribute keys which this font understands.
784 * @return An array of chracter attribute keys which this font understands.
786 * @see getAttributes()
787 * @see java.text.AttributedCharacterIterator.Attribute
788 * @see java.awt.font.TextAttribute
790 public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
792 return peer.getAvailableAttributes (this);
796 * Returns a baseline code (one of {@link ROMAN_BASELINE}, {@link
797 * CENTER_BASELINE} or {@link HANGING_BASELINE}) indicating which baseline
798 * this font will measure baseline offsets for, when presenting glyph
799 * metrics for a given character.
801 * Baseline offsets describe the position of a glyph relative to an
802 * invisible line drawn under, through the center of, or over a line of
803 * rendered text, respectively. Different scripts use different baseline
804 * modes, so clients should not assume all baseline offsets in a glyph
805 * vector are from a common baseline.
807 * @param c The character code to select a baseline mode for.
809 * @return The baseline mode which would be used in a glyph associated
810 * with the provided character.
812 * @since 1.2
814 * @see LineMetrics.getBaselineOffsets()
816 public byte getBaselineFor (char c)
818 return peer.getBaselineFor (this, c);
822 * Returns the family name of this font. A family name describes a
823 * typographic style (such as Helvetica or Palatino). It is more specific
824 * than a logical font name (such as Sans Serif) but less specific than a
825 * font face name (such as Helvetica Bold).
827 * @param lc The locale in which to describe the name of the font family.
829 * @return A string containing the font family name, localized for the
830 * provided locale.
832 * @since 1.2
834 * @see getName()
835 * @see getFontName()
836 * @see GraphicsEnvironment.getAvailableFontFamilyNames()
837 * @see Locale
839 public String getFamily (Locale lc)
841 return peer.getFamily (this, lc);
845 * Returns a font appropriate for the given attribute set.
847 * @param attributes The attributes required for the new font.
849 * @return A new Font with the given attributes.
851 * @since 1.2
853 * @see TextAttribure
855 public static Font getFont (Map attributes)
857 return getFontFromToolkit (null, attributes);
861 * Returns the font face name of the font. A font face name describes a
862 * specific variant of a font family (such as Helvetica Bold). It is more
863 * specific than both a font family name (such as Helvetica) and a logical
864 * font name (such as Sans Serif).
866 * @return The font face name of the font.
868 * @since 1.2
870 * @see getName()
871 * @see getFamily()
873 public String getFontName ()
875 return peer.getFontName (this);
879 * Returns the font face name of the font. A font face name describes a
880 * specific variant of a font family (such as Helvetica Bold). It is more
881 * specific than both a font family name (such as Helvetica).
883 * @param lc The locale in which to describe the name of the font face.
885 * @return A string containing the font face name, localized for the
886 * provided locale.
888 * @since 1.2
890 * @see getName()
891 * @see getFamily()
893 public String getFontName (Locale lc)
895 return peer.getFontName (this, lc);
899 * Returns the italic angle of this font, a measurement of its slant when
900 * style is {@link ITALIC}. The precise meaning is the inverse slope of a
901 * caret line which "best measures" the font's italic posture.
903 * @return The italic angle.
905 * @see TextAttribute.POSTURE
907 public float getItalicAngle ()
909 return peer.getItalicAngle (this);
913 * Returns a {@link LineMetrics} object constructed with the specified
914 * text and {@link FontRenderContext}.
916 * @param text The string to calculate metrics from.
917 * @param begin Index of first character in <code>text</code> to measure.
918 * @param limit Index of last character in <code>text</code> to measure.
919 * @param rc Context for calculating precise glyph placement and hints.
921 * @return A new {@link LineMetrics} object.
923 * @throws IndexOutOfBoundsException if the range [begin, limit] is
924 * invalid in <code>text</code>.
926 public LineMetrics getLineMetrics(String text, int begin,
927 int limit, FontRenderContext rc)
929 return peer.getLineMetrics (this, new StringCharacterIterator (text),
930 begin, limit, rc);
934 * Returns a {@link LineMetrics} object constructed with the specified
935 * text and {@link FontRenderContext}.
937 * @param chars The string to calculate metrics from.
938 * @param begin Index of first character in <code>text</code> to measure.
939 * @param limit Index of last character in <code>text</code> to measure.
940 * @param rc Context for calculating precise glyph placement and hints.
942 * @return A new {@link LineMetrics} object.
944 * @throws IndexOutOfBoundsException if the range [begin, limit] is
945 * invalid in <code>chars</code>.
947 public LineMetrics getLineMetrics(char[] chars, int begin,
948 int limit, FontRenderContext rc)
950 return peer.getLineMetrics (this, new StringCharacterIterator (new String(chars)),
951 begin, limit, rc);
955 * Returns a {@link LineMetrics} object constructed with the specified
956 * text and {@link FontRenderContext}.
958 * @param ci The string to calculate metrics from.
959 * @param begin Index of first character in <code>text</code> to measure.
960 * @param limit Index of last character in <code>text</code> to measure.
961 * @param rc Context for calculating precise glyph placement and hints.
963 * @return A new {@link LineMetrics} object.
965 * @throws IndexOutOfBoundsException if the range [begin, limit] is
966 * invalid in <code>ci</code>.
968 public LineMetrics getLineMetrics (CharacterIterator ci, int begin,
969 int limit, FontRenderContext rc)
971 return peer.getLineMetrics (this, ci, begin, limit, rc);
975 * Returns the maximal bounding box of all the bounding boxes in this
976 * font, when the font's bounding boxes are evaluated in a given {@link
977 * FontRenderContext}
979 * @param rc Context in which to evaluate bounding boxes.
981 * @return The maximal bounding box.
983 public Rectangle2D getMaxCharBounds (FontRenderContext rc)
985 return peer.getMaxCharBounds (this, rc);
989 * Returns the glyph code this font uses to represent missing glyphs. This
990 * code will be present in glyph vectors when the font was unable to
991 * locate a glyph to represent a particular character code.
993 * @return The missing glyph code.
995 * @since 1.2
997 public int getMissingGlyphCode ()
999 return peer.getMissingGlyphCode (this);
1003 * Returns the overall number of glyphs in this font. This number is one
1004 * more than the greatest glyph code used in any glyph vectors this font
1005 * produces. In other words, glyph codes are taken from the range
1006 * <code>[ 0, getNumGlyphs() - 1 ]</code>.
1008 * @return The number of glyphs in this font.
1010 * @since 1.2
1012 public int getNumGlyphs ()
1014 return peer.getMissingGlyphCode (this);
1018 * Returns the PostScript Name of this font.
1020 * @return The PostScript Name of this font.
1022 * @since 1.2
1024 * @see getName()
1025 * @see getFamily()
1026 * @see getFontName()
1028 public String getPSName ()
1030 return peer.getPostScriptName (this);
1034 * Returns the logical bounds of the specified string when rendered with this
1035 * font in the specified {@link FontRenderContext}. This box will include the
1036 * glyph origin, ascent, advance, height, and leading, but may not include all
1037 * diacritics or accents. To get the complete visual bounding box of all the
1038 * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1039 * {@link TextLayout}.
1041 * @param str The string to measure.
1042 * @param frc The context in which to make the precise glyph measurements.
1044 * @return A bounding box covering the logical bounds of the specified text.
1046 * @see createGlyphVector()
1048 public Rectangle2D getStringBounds (String str, FontRenderContext frc)
1050 return getStringBounds (str, 0, str.length () - 1, frc);
1054 * Returns the logical bounds of the specified string when rendered with this
1055 * font in the specified {@link FontRenderContext}. This box will include the
1056 * glyph origin, ascent, advance, height, and leading, but may not include all
1057 * diacritics or accents. To get the complete visual bounding box of all the
1058 * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1059 * {@link TextLayout}.
1061 * @param str The string to measure.
1062 * @param begin Index of the first character in <code>str</code> to measure.
1063 * @param limit Index of the last character in <code>str</code> to measure.
1064 * @param frc The context in which to make the precise glyph measurements.
1066 * @return A bounding box covering the logical bounds of the specified text.
1068 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1069 * invalid in <code>str</code>.
1071 * @since 1.2
1073 * @see createGlyphVector()
1075 public Rectangle2D getStringBounds (String str, int begin,
1076 int limit, FontRenderContext frc)
1078 return peer.getStringBounds (this, new StringCharacterIterator(str), begin, limit, frc);
1082 * Returns the logical bounds of the specified string when rendered with this
1083 * font in the specified {@link FontRenderContext}. This box will include the
1084 * glyph origin, ascent, advance, height, and leading, but may not include all
1085 * diacritics or accents. To get the complete visual bounding box of all the
1086 * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1087 * {@link TextLayout}.
1089 * @param ci The text to measure.
1090 * @param begin Index of the first character in <code>ci</code> to measure.
1091 * @param limit Index of the last character in <code>ci</code> to measure.
1092 * @param frc The context in which to make the precise glyph measurements.
1094 * @return A bounding box covering the logical bounds of the specified text.
1096 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1097 * invalid in <code>ci</code>.
1099 * @since 1.2
1101 * @see createGlyphVector()
1103 public Rectangle2D getStringBounds (CharacterIterator ci, int begin,
1104 int limit, FontRenderContext frc)
1106 return peer.getStringBounds (this, ci, begin, limit, frc);
1110 * Returns the logical bounds of the specified string when rendered with this
1111 * font in the specified {@link FontRenderContext}. This box will include the
1112 * glyph origin, ascent, advance, height, and leading, but may not include all
1113 * diacritics or accents. To get the complete visual bounding box of all the
1114 * glyphs in a run of text, use the {@link TextLayout#getBounds} method of
1115 * {@link TextLayout}.
1117 * @param chars The text to measure.
1118 * @param begin Index of the first character in <code>ci</code> to measure.
1119 * @param limit Index of the last character in <code>ci</code> to measure.
1120 * @param frc The context in which to make the precise glyph measurements.
1122 * @return A bounding box covering the logical bounds of the specified text.
1124 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1125 * invalid in <code>chars</code>.
1127 * @since 1.2
1129 * @see createGlyphVector()
1131 public Rectangle2D getStringBounds (char[] chars, int begin,
1132 int limit, FontRenderContext frc)
1134 return peer.getStringBounds (this, new StringCharacterIterator (new String (chars)),
1135 begin, limit, frc);
1139 * Returns a copy of the affine transformation this font is currently
1140 * subject to, if any.
1142 * @return The current transformation.
1144 public AffineTransform getTransform ()
1146 return peer.getTransform (this);
1150 * Indicates whether this font's line metrics are uniform. A font may be
1151 * composed of several "subfonts", each covering a different code range,
1152 * and each with their own line metrics. A font with no subfonts, or
1153 * subfonts with identical line metrics, is said to have "uniform" line
1154 * metrics.
1156 * @return Whether this font has uniform line metrics.
1158 * @see LineMetrics
1159 * @see getLineMetrics()
1161 public boolean hasUniformLineMetrics ()
1163 return peer.hasUniformLineMetrics (this);
1167 * Indicates whether this font is subject to a non-identity affine
1168 * transformation.
1170 * @return <code>true</code> iff the font has a non-identity affine
1171 * transformation applied to it.
1173 public boolean isTransformed ()
1175 return peer.isTransformed (this);
1179 * Produces a glyph vector representing a full layout fo the specified
1180 * text in this font. Full layouts may include complex shaping and
1181 * reordering operations, for scripts such as Arabic or Hindi.
1183 * Bidirectional (bidi) layout is not performed in this method; text
1184 * should have its bidi direction specified with one of the flags {@link
1185 * LAYOUT_LEFT_TO_RIGHT} or {@link LAYOUT_RIGHT_TO_LEFT}.
1187 * Some types of layout (notably Arabic glyph shaping) may examine context
1188 * characters beyond the bounds of the indicated range, in order to select
1189 * an appropriate shape. The flags {@link LAYOUT_NO_START_CONTEXT} and
1190 * {@link LAYOUT_NO_LIMIT_CONTEXT} can be provided to prevent these extra
1191 * context areas from being examined, for instance if they contain invalid
1192 * characters.
1194 * @param frc Context in which to perform the layout.
1195 * @param chars Text to perform layout on.
1196 * @param start Index of first character to perform layout on.
1197 * @param limit Index of last character to perform layout on.
1198 * @param flags Combination of flags controlling layout.
1200 * @return A new {@link GlyphVector} representing the specified text.
1202 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1203 * invalid in <code>chars</code>.
1205 public GlyphVector layoutGlyphVector (FontRenderContext frc,
1206 char[] chars, int start,
1207 int limit, int flags)
1209 return peer.layoutGlyphVector (this, frc, chars, start, limit, flags);
1214 * Returns a native peer object for this font.
1216 * @return A native peer object for this font.
1218 * @deprecated
1220 public FontPeer getPeer ()
1222 return peer;
1227 * Returns a hash value for this font.
1229 * @return A hash for this font.
1231 public int hashCode()
1233 return this.toString().hashCode();
1238 * Tests whether or not the specified object is equal to this font. This
1239 * will be true if and only if:
1240 * <P>
1241 * <ul>
1242 * <li>The object is not <code>null</code>.
1243 * <li>The object is an instance of <code>Font</code>.
1244 * <li>The object has the same names, style, size, and transform as this object.
1245 * </ul>
1247 * @return <code>true</code> if the specified object is equal to this
1248 * object, <code>false</code> otherwise.
1250 public boolean
1251 equals(Object obj)
1253 if (obj == null)
1254 return(false);
1256 if (!(obj instanceof Font))
1257 return(false);
1259 Font f = (Font)obj;
1261 return (f.getName ().equals (this.getName ()) &&
1262 f.getFamily ().equals (this.getFamily ()) &&
1263 f.getFontName ().equals (this.getFontName ()) &&
1264 f.getTransform ().equals (this.getTransform ()) &&
1265 f.getSize() == this.getSize() &&
1266 f.getStyle() == this.getStyle());
1269 /*************************************************************************/
1272 * Returns a string representation of this font.
1274 * @return A string representation of this font.
1276 public String
1277 toString()
1279 String styleString = "";
1281 switch (getStyle ())
1283 case 0:
1284 styleString = "plain";
1285 break;
1286 case 1:
1287 styleString = "bold";
1288 break;
1289 case 2:
1290 styleString = "italic";
1291 break;
1292 default:
1293 styleString = "unknown";
1296 return getClass ().getName ()
1297 + "[family=" + getFamily ()
1298 + ",name=" + getFontName ()
1299 + ",style=" + styleString
1300 + ",size=" + getSize () + "]";
1305 * Determines the line metrics for a run of text.
1307 * @param str the text run to be measured.
1309 * @param frc the font rendering parameters that are used for the
1310 * measurement. The exact placement and size of text slightly
1311 * depends on device-specific characteristics, for instance
1312 * the device resolution or anti-aliasing. For this reason,
1313 * the returned measurement will only be accurate if the
1314 * passed <code>FontRenderContext</code> correctly reflects
1315 * the relevant parameters. Hence, <code>frc</code> should be
1316 * obtained from the same <code>Graphics2D</code> that will
1317 * be used for drawing, and any rendering hints should be set
1318 * to the desired values before obtaining <code>frc</code>.
1320 * @see java.awt.Graphics2D#getFontRenderContext()
1322 public LineMetrics getLineMetrics(String str, FontRenderContext frc)
1324 return getLineMetrics (str, 0, str.length () - 1, frc);
1327 } // class Font