1 /* Font.java -- Font object
2 Copyright (C) 1999, 2002, 2004 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)
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
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
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. */
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;
59 import java.util.StringTokenizer;
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
76 * Constant indicating a "plain" font.
78 public static final int PLAIN = 0;
81 * Constant indicating a "bold" font.
83 public static final int BOLD = 1;
86 * Constant indicating an "italic" font.
88 public static final int ITALIC = 2;
91 * Constant indicating the baseline mode characteristic of Roman.
93 public static final int ROMAN_BASELINE = 0;
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
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.
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.
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).
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).
162 public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
165 * The logical name of this font.
169 protected String name;
172 * The size of this font in pixels.
179 * The style of this font -- PLAIN, BOLD, ITALIC or BOLD+ITALIC.
185 // Serialization constant
186 private static final long serialVersionUID = -4206021311591459213L;
189 // The ClasspathToolkit-provided peer which implements this font
190 private ClasspathFontPeer peer;
192 /*************************************************************************/
199 * Creates a <code>Font</code> object from the specified string, which
200 * is in one of the following formats:
203 * <li>fontname-style-pointsize
205 * <li>fontname-pointsize
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)
219 StringTokenizer st = new StringTokenizer(fontspec, "-");
220 while (st.hasMoreTokens())
222 String token = st.nextToken();
229 if (token.toUpperCase().equals("BOLD"))
234 if (token.toUpperCase().equals("ITALIC"))
239 if (token.toUpperCase().equals("BOLDITALIC"))
241 style = BOLD | ITALIC;
248 tokenval = Integer.parseInt(token);
250 catch(NumberFormatException e) { ; }
256 HashMap attrs = new HashMap();
257 ClasspathFontPeer.copyStyleToAttrs (style, attrs);
258 ClasspathFontPeer.copySizeToAttrs (size, attrs);
260 return getFontFromToolkit (name, attrs);
263 /* These methods delegate to the toolkit. */
265 protected static ClasspathToolkit tk ()
267 return (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
270 /* Every factory method in Font should eventually call this. */
271 protected static Font getFontFromToolkit (String name, Map attribs)
273 return tk ().getFont (name, attribs);
276 /* Every Font constructor should eventually call this. */
277 protected static ClasspathFontPeer getPeerFromToolkit (String name, Map attrs)
279 return tk ().getClasspathFontPeer (name, attrs);
283 /*************************************************************************/
286 * Returns a <code>Font</code> object from the passed property name.
288 * @param propname The name of the system property.
289 * @param defval Value to use if the property is not found.
291 * @return The requested font, or <code>default</code> if the property
292 * not exist or is malformed.
294 public static Font getFont (String propname, Font defval)
296 String propval = System.getProperty(propname);
298 return decode (propval);
302 /*************************************************************************/
305 * Returns a <code>Font</code> object from the passed property name.
307 * @param propname The name of the system property.
309 * @return The requested font, or <code>null</code> if the property
310 * not exist or is malformed.
312 public static Font getFont (String propname)
314 return getFont (propname, (Font)null);
317 /*************************************************************************/
324 * Initializes a new instance of <code>Font</code> with the specified
327 * @param name The name of the font.
328 * @param style The font style.
329 * @param size The font point size.
332 public Font (String name, int style, int size)
334 HashMap attrs = new HashMap();
335 ClasspathFontPeer.copyStyleToAttrs (style, attrs);
336 ClasspathFontPeer.copySizeToAttrs (size, attrs);
337 this.peer = getPeerFromToolkit (name, attrs);
340 public Font (Map attrs)
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)
349 // If attrs is null, setting it to an empty HashMap will give this
350 // Font default attributes.
352 attrs = new HashMap();
353 this.peer = getPeerFromToolkit (name, attrs);
356 /*************************************************************************/
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
368 * @return The logical name of the font.
373 public String getName ()
375 return peer.getName (this);
378 /*************************************************************************/
381 * Returns the style of the font.
383 * @return The font style.
385 public int getSize ()
387 return (int) peer.getSize (this);
390 public float getSize2D ()
392 return peer.getSize (this);
395 /*************************************************************************/
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.
401 * @return <code>true</code> if this is a plain font, <code>false</code>
404 public boolean isPlain ()
406 return peer.isPlain (this);
409 /*************************************************************************/
412 * Tests whether or not this font is bold.
414 * @return <code>true</code> if this font is bold, <code>false</code>
417 public boolean isBold ()
419 return peer.isBold (this);
422 /*************************************************************************/
425 * Tests whether or not this font is italic.
427 * @return <code>true</code> if this font is italic, <code>false</code>
430 public boolean isItalic ()
432 return peer.isItalic (this);
435 /*************************************************************************/
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).
442 * @return A string containing the font family name.
448 * @see GraphicsEnvironment.getAvailableFontFamilyNames()
450 public String getFamily ()
452 return peer.getFamily (this);
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}.
459 * @return code representing the style of this font.
465 public int getStyle ()
467 return peer.getStyle (this);
471 * Checks if specified character maps to a glyph in this font.
473 * @param c The character to check.
475 * @return Whether the character has a corresponding glyph in this font.
479 public boolean canDisplay (char c)
481 return peer.canDisplay (this, c);
485 * Checks how much of a given string can be mapped to glyphs in
488 * @param s The string to check.
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.
496 public int canDisplayUpTo (String s)
498 return peer.canDisplayUpTo (this, new StringCharacterIterator (s),
503 * Checks how much of a given sequence of text can be mapped to glyphs in
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>.
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.
516 * @throws IndexOutOfBoundsException if the range [start, limit] is
517 * invalid in <code>text</code>.
519 public int canDisplayUpTo (char[] text, int start, int limit)
521 return peer.canDisplayUpTo
522 (this, new StringCharacterIterator (new String (text)), start, limit);
526 * Checks how much of a given sequence of text can be mapped to glyphs in
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>.
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.
539 * @throws IndexOutOfBoundsException if the range [start, limit] is
540 * invalid in <code>i</code>.
542 public int canDisplayUpTo (CharacterIterator i, int start, int limit)
544 return peer.canDisplayUpTo (this, i, start, limit);
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.
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.
558 * @return A new {@link Font} of the format indicated.
560 * @throws IllegalArgumentException if <code>fontType</code> is not
562 * @throws FontFormatException if data in InputStream is not of format
564 * @throws IOException if insufficient data is present on InputStream.
568 public static Font createFont (int fontFormat, InputStream is)
569 throws FontFormatException, IOException
571 return tk().createFont (fontFormat, is);
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.
581 * @param ctx The rendering context used for precise glyph placement.
582 * @param str The string to convert to Glyphs.
584 * @return A new {@link GlyphVector} containing glyphs mapped from str,
585 * through the font's cmap table.
587 * @see layoutGlyphVector()
589 public GlyphVector createGlyphVector (FontRenderContext ctx, String str)
591 return peer.createGlyphVector (this, ctx, new StringCharacterIterator (str));
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.
601 * @param ctx The rendering context used for precise glyph placement.
602 * @param i Iterator over the text to convert to glyphs.
604 * @return A new {@link GlyphVector} containing glyphs mapped from str,
605 * through the font's cmap table.
607 * @see layoutGlyphVector()
609 public GlyphVector createGlyphVector (FontRenderContext ctx, CharacterIterator i)
611 return peer.createGlyphVector (this, ctx, i);
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.
621 * @param ctx The rendering context used for precise glyph placement.
622 * @param chars Array of characters to convert to glyphs.
624 * @return A new {@link GlyphVector} containing glyphs mapped from str,
625 * through the font's cmap table.
627 * @see layoutGlyphVector()
629 public GlyphVector createGlyphVector (FontRenderContext ctx, char[] chars)
631 return peer.createGlyphVector
632 (this, ctx, new StringCharacterIterator (new String (chars)));
636 * Extracts a sequence of glyphs from a font, returning a new {@link
637 * GlyphVector} with a mapped glyph for each input glyph code.
639 * @param ctx The rendering context used for precise glyph placement.
640 * @param glyphCodes Array of characters to convert to glyphs.
642 * @return A new {@link GlyphVector} containing glyphs mapped from str,
643 * through the font's cmap table.
645 * @see layoutGlyphVector()
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.
654 public GlyphVector createGlyphVector (FontRenderContext ctx, int[] glyphCodes)
656 return peer.createGlyphVector (this, ctx, glyphCodes);
660 * Produces a new {@link Font} based on the current font, adjusted to a
661 * new size and style.
663 * @param style The style of the newly created font.
664 * @param size The size of the newly created font.
666 * @return A clone of the current font, with the specified size and style.
670 public Font deriveFont (int style, float size)
672 return peer.deriveFont (this, style, size);
676 * Produces a new {@link Font} based on the current font, adjusted to a
679 * @param size The size of the newly created font.
681 * @return A clone of the current font, with the specified size.
685 public Font deriveFont (float size)
687 return peer.deriveFont (this, size);
691 * Produces a new {@link Font} based on the current font, adjusted to a
694 * @param style The style of the newly created font.
696 * @return A clone of the current font, with the specified style.
700 public Font deriveFont (int style)
702 return peer.deriveFont (this, style);
706 * Produces a new {@link Font} based on the current font, adjusted to a
707 * new style and subjected to a new affine transformation.
709 * @param style The style of the newly created font.
710 * @param a The transformation to apply.
712 * @return A clone of the current font, with the specified style and
715 * @throws IllegalArgumentException If transformation is
720 public Font deriveFont (int style, AffineTransform a)
723 throw new IllegalArgumentException ("Affine transformation is null");
725 return peer.deriveFont (this, style, a);
729 * Produces a new {@link Font} based on the current font, subjected
730 * to a new affine transformation.
732 * @param a The transformation to apply.
734 * @return A clone of the current font, with the specified transform.
736 * @throws IllegalArgumentException If transformation is
741 public Font deriveFont (AffineTransform a)
744 throw new IllegalArgumentException ("Affine transformation is null");
746 return peer.deriveFont (this, a);
750 * Produces a new {@link Font} based on the current font, adjusted to a
751 * new set of attributes.
753 * @param attributes Attributes of the newly created font.
755 * @return A clone of the current font, with the specified attributes.
759 public Font deriveFont (Map attributes)
761 return peer.deriveFont (this, attributes);
765 * Returns a map of chracter attributes which this font currently has set.
767 * @return A map of chracter attributes which this font currently has set.
769 * @see getAvailableAttributes()
770 * @see java.text.AttributedCharacterIterator.Attribute
771 * @see java.awt.font.TextAttribute
773 public Map getAttributes ()
775 return peer.getAttributes (this);
779 * Returns an array of chracter attribute keys which this font understands.
781 * @return An array of chracter attribute keys which this font understands.
783 * @see getAttributes()
784 * @see java.text.AttributedCharacterIterator.Attribute
785 * @see java.awt.font.TextAttribute
787 public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
789 return peer.getAvailableAttributes (this);
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.
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.
804 * @param c The character code to select a baseline mode for.
806 * @return The baseline mode which would be used in a glyph associated
807 * with the provided character.
811 * @see LineMetrics.getBaselineOffsets()
813 public byte getBaselineFor (char c)
815 return peer.getBaselineFor (this, c);
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).
824 * @param lc The locale in which to describe the name of the font family.
826 * @return A string containing the font family name, localized for the
833 * @see GraphicsEnvironment.getAvailableFontFamilyNames()
836 public String getFamily (Locale lc)
838 return peer.getFamily (this, lc);
842 * Returns a font appropriate for the given attribute set.
844 * @param attributes The attributes required for the new font.
846 * @return A new Font with the given attributes.
852 public static Font getFont (Map attributes)
854 return getFontFromToolkit (null, attributes);
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).
863 * @return The font face name of the font.
870 public String getFontName ()
872 return peer.getFontName (this);
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).
880 * @param lc The locale in which to describe the name of the font face.
882 * @return A string containing the font face name, localized for the
890 public String getFontName (Locale lc)
892 return peer.getFontName (this, lc);
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.
900 * @return The italic angle.
902 * @see TextAttribute.POSTURE
904 public float getItalicAngle ()
906 return peer.getItalicAngle (this);
910 * Returns a {@link LineMetrics} object constructed with the specified
911 * text and {@link FontRenderContext}.
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.
918 * @return A new {@link LineMetrics} object.
920 * @throws IndexOutOfBoundsException if the range [begin, limit] is
921 * invalid in <code>text</code>.
923 public LineMetrics getLineMetrics(String text, int begin,
924 int limit, FontRenderContext rc)
926 return peer.getLineMetrics (this, new StringCharacterIterator (text),
931 * Returns a {@link LineMetrics} object constructed with the specified
932 * text and {@link FontRenderContext}.
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.
939 * @return A new {@link LineMetrics} object.
941 * @throws IndexOutOfBoundsException if the range [begin, limit] is
942 * invalid in <code>chars</code>.
944 public LineMetrics getLineMetrics(char[] chars, int begin,
945 int limit, FontRenderContext rc)
947 return peer.getLineMetrics (this, new StringCharacterIterator (new String(chars)),
952 * Returns a {@link LineMetrics} object constructed with the specified
953 * text and {@link FontRenderContext}.
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.
960 * @return A new {@link LineMetrics} object.
962 * @throws IndexOutOfBoundsException if the range [begin, limit] is
963 * invalid in <code>ci</code>.
965 public LineMetrics getLineMetrics (CharacterIterator ci, int begin,
966 int limit, FontRenderContext rc)
968 return peer.getLineMetrics (this, ci, begin, limit, rc);
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
976 * @param rc Context in which to evaluate bounding boxes.
978 * @return The maximal bounding box.
980 public Rectangle2D getMaxCharBounds (FontRenderContext rc)
982 return peer.getMaxCharBounds (this, rc);
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.
990 * @return The missing glyph code.
994 public int getMissingGlyphCode ()
996 return peer.getMissingGlyphCode (this);
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>.
1005 * @return The number of glyphs in this font.
1009 public int getNumGlyphs ()
1011 return peer.getMissingGlyphCode (this);
1015 * Returns the PostScript Name of this font.
1017 * @return The PostScript Name of this font.
1023 * @see getFontName()
1025 public String getPSName ()
1027 return peer.getPostScriptName (this);
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}.
1038 * @param str The string to measure.
1039 * @param frc The context in which to make the precise glyph measurements.
1041 * @return A bounding box covering the logical bounds of the specified text.
1043 * @see createGlyphVector()
1045 public Rectangle2D getStringBounds (String str, FontRenderContext frc)
1047 return getStringBounds (str, 0, str.length () - 1, frc);
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}.
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.
1063 * @return A bounding box covering the logical bounds of the specified text.
1065 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1066 * invalid in <code>str</code>.
1070 * @see createGlyphVector()
1072 public Rectangle2D getStringBounds (String str, int begin,
1073 int limit, FontRenderContext frc)
1075 return peer.getStringBounds (this, new StringCharacterIterator(str), begin, limit, frc);
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}.
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.
1091 * @return A bounding box covering the logical bounds of the specified text.
1093 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1094 * invalid in <code>ci</code>.
1098 * @see createGlyphVector()
1100 public Rectangle2D getStringBounds (CharacterIterator ci, int begin,
1101 int limit, FontRenderContext frc)
1103 return peer.getStringBounds (this, ci, begin, limit, frc);
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}.
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.
1119 * @return A bounding box covering the logical bounds of the specified text.
1121 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1122 * invalid in <code>chars</code>.
1126 * @see createGlyphVector()
1128 public Rectangle2D getStringBounds (char[] chars, int begin,
1129 int limit, FontRenderContext frc)
1131 return peer.getStringBounds (this, new StringCharacterIterator (new String (chars)),
1136 * Returns a copy of the affine transformation this font is currently
1137 * subject to, if any.
1139 * @return The current transformation.
1141 public AffineTransform getTransform ()
1143 return peer.getTransform (this);
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
1153 * @return Whether this font has uniform line metrics.
1156 * @see getLineMetrics()
1158 public boolean hasUniformLineMetrics ()
1160 return peer.hasUniformLineMetrics (this);
1164 * Indicates whether this font is subject to a non-identity affine
1167 * @return <code>true</code> iff the font has a non-identity affine
1168 * transformation applied to it.
1170 public boolean isTransformed ()
1172 return peer.isTransformed (this);
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.
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}.
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
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.
1197 * @return A new {@link GlyphVector} representing the specified text.
1199 * @throws IndexOutOfBoundsException if the range [begin, limit] is
1200 * invalid in <code>chars</code>.
1202 public GlyphVector layoutGlyphVector (FontRenderContext frc,
1203 char[] chars, int start,
1204 int limit, int flags)
1206 return peer.layoutGlyphVector (this, frc, chars, start, limit, flags);
1211 * Returns a native peer object for this font.
1213 * @return A native peer object for this font.
1217 public FontPeer getPeer ()
1224 * Returns a hash value for this font.
1226 * @return A hash for this font.
1228 public int hashCode()
1230 return this.toString().hashCode();
1235 * Tests whether or not the specified object is equal to this font. This
1236 * will be true if and only if:
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.
1244 * @return <code>true</code> if the specified object is equal to this
1245 * object, <code>false</code> otherwise.
1253 if (!(obj instanceof Font))
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());
1266 /*************************************************************************/
1269 * Returns a string representation of this font.
1271 * @return A string representation of this font.
1276 String styleString = "";
1278 switch (getStyle ())
1281 styleString = "plain";
1284 styleString = "bold";
1287 styleString = "italic";
1290 styleString = "unknown";
1293 return getClass ().getName ()
1294 + "[family=" + getFamily ()
1295 + ",name=" + getFontName ()
1296 + ",style=" + styleString
1297 + ",size=" + getSize () + "]";
1302 * Determines the line metrics for a run of text.
1304 * @param str the text run to be measured.
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>.
1317 * @see java.awt.Graphics2D#getFontRenderContext()
1319 public LineMetrics getLineMetrics(String str, FontRenderContext frc)
1321 return getLineMetrics (str, 0, str.length () - 1, frc);