jdk/src/share/classes/java/awt/FontMetrics.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 20451 4cedf4e1560a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.font.LineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The <code>FontMetrics</code> class defines a font metrics object, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * encapsulates information about the rendering of a particular font on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * particular screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <b>Note to subclassers</b>: Since many of these methods form closed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * mutually recursive loops, you must take care that you implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * at least one of the methods in each such loop to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * infinite recursion when your subclass is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * In particular, the following is the minimal suggested set of methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to override in order to ensure correctness and prevent infinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * recursion (though other subsets are equally feasible):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>{@link #getAscent()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <li>{@link #getLeading()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>{@link #getMaxAdvance()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li>{@link #charWidth(char)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>{@link #charsWidth(char[], int, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <img src="doc-files/FontMetrics-1.gif" alt="The letter 'p' showing its 'reference point'" border=15 align
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * ALIGN=right HSPACE=10 VSPACE=7>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Note that the implementations of these methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * inefficient, so they are usually overridden with more efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * toolkit-specific implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * When an application asks to place a character at the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * (<i>x</i>,&nbsp;<i>y</i>), the character is placed so that its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * reference point (shown as the dot in the accompanying image) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * put at that position. The reference point specifies a horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * line called the <i>baseline</i> of the character. In normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * printing, the baselines of characters should align.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * In addition, every character in a font has an <i>ascent</i>, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <i>descent</i>, and an <i>advance width</i>. The ascent is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * amount by which the character ascends above the baseline. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * descent is the amount by which the character descends below the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * baseline. The advance width indicates the position at which AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * should place the next character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * An array of characters or a string can also have an ascent, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * descent, and an advance width. The ascent of the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * maximum ascent of any character in the array. The descent is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * maximum descent of any character in the array. The advance width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * is the sum of the advance widths of each of the characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * character array.  The advance of a <code>String</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * distance along the baseline of the <code>String</code>.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * distance is the width that should be used for centering or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * right-aligning the <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>Note that the advance of a <code>String</code> is not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * the sum of the advances of its characters measured in isolation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * because the width of a character can vary depending on its context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * For example, in Arabic text, the shape of a character can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * in order to connect to other characters.  Also, in some scripts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * certain character sequences can be represented by a single shape,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * called a <em>ligature</em>.  Measuring characters individually does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * not account for these transformations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>Font metrics are baseline-relative, meaning that they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * generally independent of the rotation applied to the font (modulo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * possible grid hinting effects).  See {@link java.awt.Font Font}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @see         java.awt.Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
public abstract class FontMetrics implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        DEFAULT_FRC = new FontRenderContext(null, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The actual {@link Font} from which the font metrics are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see #getFont()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final long serialVersionUID = 1681126225205050147L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Creates a new <code>FontMetrics</code> object for finding out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * height and width information about the specified <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * and specific character glyphs in that <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param     font the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see       java.awt.Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected FontMetrics(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Gets the <code>Font</code> described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return    the <code>Font</code> described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Gets the <code>FontRenderContext</code> used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>FontMetrics</code> object to measure text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Note that methods in this class which take a <code>Graphics</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * parameter measure text using the <code>FontRenderContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * of that <code>Graphics</code> object, and not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>FontRenderContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return    the <code>FontRenderContext</code> used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public FontRenderContext getFontRenderContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return DEFAULT_FRC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Determines the <em>standard leading</em> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>Font</code> described by this <code>FontMetrics</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * object.  The standard leading, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * interline spacing, is the logical amount of space to be reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * between the descent of one line of text and the ascent of the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * line. The height metric is calculated to include this extra space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return    the standard leading of the <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see   #getHeight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see   #getAscent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see   #getDescent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int getLeading() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Determines the <em>font ascent</em> of the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * described by this <code>FontMetrics</code> object. The font ascent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * is the distance from the font's baseline to the top of most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * alphanumeric characters. Some characters in the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * might extend above the font ascent line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return     the font ascent of the <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see        #getMaxAscent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public int getAscent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return font.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Determines the <em>font descent</em> of the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <code>FontMetrics</code> object. The font descent is the distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * from the font's baseline to the bottom of most alphanumeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * characters with descenders. Some characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <code>Font</code> might extend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * below the font descent line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return     the font descent of the <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see        #getMaxDescent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public int getDescent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Gets the standard height of a line of text in this font.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * is the distance between the baseline of adjacent lines of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * It is the sum of the leading + ascent + descent. Due to rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * this may not be the same as getAscent() + getDescent() + getLeading().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * There is no guarantee that lines of text spaced at this distance are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * disjoint; such lines may overlap if some characters overshoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * either the standard ascent or the standard descent metric.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return    the standard height of the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see       #getLeading()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see       #getAscent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see       #getDescent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public int getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return getLeading() + getAscent() + getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Determines the maximum ascent of the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * described by this <code>FontMetrics</code> object.  No character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * extends further above the font's baseline than this height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return    the maximum ascent of any character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see       #getAscent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public int getMaxAscent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Determines the maximum descent of the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * described by this <code>FontMetrics</code> object.  No character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * extends further below the font's baseline than this height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return    the maximum descent of any character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see       #getDescent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public int getMaxDescent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * For backward compatibility only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return    the maximum descent of any character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #getMaxDescent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @deprecated As of JDK version 1.1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * replaced by <code>getMaxDescent()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int getMaxDecent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return getMaxDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Gets the maximum advance width of any character in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * string's baseline.  The advance of a <code>String</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * not necessarily the sum of the advances of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return    the maximum advance width of any character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *            in the <code>Font</code>, or <code>-1</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *            maximum advance width is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public int getMaxAdvance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns the advance width of the specified character in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * character's baseline.  Note that the advance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <code>String</code> is not necessarily the sum of the advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>This method doesn't validate the specified character to be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * valid Unicode code point. The caller must validate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * character value using {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * java.lang.Character#isValidCodePoint(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Character.isValidCodePoint} if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param codePoint the character (Unicode code point) to be measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return    the advance width of the specified character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *            in the <code>Font</code> described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *            <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see   #charsWidth(char[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see   #stringWidth(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public int charWidth(int codePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!Character.isValidCodePoint(codePoint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            codePoint = 0xffff; // substitute missing glyph width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (codePoint < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return getWidths()[codePoint];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            char[] buffer = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            int len = Character.toChars(codePoint, buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return charsWidth(buffer, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the advance width of the specified character in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * character's baseline.  Note that the advance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <code>String</code> is not necessarily the sum of the advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p><b>Note:</b> This method cannot handle <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * href="../lang/Character.html#supplementary"> supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * characters</a>. To support all Unicode characters, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * supplementary characters, use the {@link #charWidth(int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param ch the character to be measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return     the advance width of the specified character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                  in the <code>Font</code> described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *                  <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see        #charsWidth(char[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @see        #stringWidth(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public int charWidth(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (ch < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return getWidths()[ch];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        char data[] = {ch};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return charsWidth(data, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns the total advance width for showing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <code>String</code> in this <code>Font</code>.  The advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * is the distance from the leftmost point to the rightmost point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * on the string's baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Note that the advance of a <code>String</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * not necessarily the sum of the advances of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param str the <code>String</code> to be measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return    the advance width of the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *                  in the <code>Font</code> described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *                  <code>FontMetrics</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws NullPointerException if str is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see       #bytesWidth(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see       #charsWidth(char[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see       #getStringBounds(String, Graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public int stringWidth(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int len = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        char data[] = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        str.getChars(0, len, data, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return charsWidth(data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns the total advance width for showing the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * of characters in this <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * string's baseline.  The advance of a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * is not necessarily the sum of the advances of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * This is equivalent to measuring a <code>String</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * characters in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param data the array of characters to be measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param off the start offset of the characters in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param len the number of characters to be measured from the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return    the advance width of the subarray of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *               <code>char</code> array in the font described by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *               this <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws    NullPointerException if <code>data</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws    IndexOutOfBoundsException if the <code>off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *            and <code>len</code> arguments index characters outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *            the bounds of the <code>data</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see       #charWidth(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see       #charWidth(char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see       #bytesWidth(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see       #stringWidth(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int charsWidth(char data[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return stringWidth(new String(data, off, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the total advance width for showing the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * of bytes in this <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * string's baseline.  The advance of a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * is not necessarily the sum of the advances of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * This is equivalent to measuring a <code>String</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * characters in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param data the array of bytes to be measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param off the start offset of the bytes in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param len the number of bytes to be measured from the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return    the advance width of the subarray of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *               <code>byte</code> array in the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *                  described by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *               this <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws    NullPointerException if <code>data</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws    IndexOutOfBoundsException if the <code>off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *            and <code>len</code> arguments index bytes outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *            the bounds of the <code>data</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see       #charsWidth(char[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see       #stringWidth(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public int bytesWidth(byte data[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return stringWidth(new String(data, 0, off, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Gets the advance widths of the first 256 characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <code>Font</code>.  The advance is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * distance from the leftmost point to the rightmost point on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * character's baseline.  Note that the advance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <code>String</code> is not necessarily the sum of the advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * of its characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return    an array storing the advance widths of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *                 characters in the <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *                 described by this <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public int[] getWidths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int widths[] = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        for (char ch = 0 ; ch < 256 ; ch++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            widths[ch] = charWidth(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return widths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Checks to see if the <code>Font</code> has uniform line metrics.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * composite font may consist of several different fonts to cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * various character sets.  In such cases, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <code>FontLineMetrics</code> objects are not uniform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Different fonts may have a different ascent, descent, metrics and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * so on.  This information is sometimes necessary for line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * measuring and line breaking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return <code>true</code> if the font has uniform line metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see java.awt.Font#hasUniformLineMetrics()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public boolean hasUniformLineMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return font.hasUniformLineMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Returns the {@link LineMetrics} object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * <code>String</code> in the specified {@link Graphics} context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param str the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return a <code>LineMetrics</code> object created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * specified <code>String</code> and <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see java.awt.Font#getLineMetrics(String, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public LineMetrics getLineMetrics( String str, Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return font.getLineMetrics(str, myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Returns the {@link LineMetrics} object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <code>String</code> in the specified {@link Graphics} context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @param str the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param beginIndex the initial offset of <code>str</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param limit the end offset of <code>str</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return a <code>LineMetrics</code> object created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * specified <code>String</code> and <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public LineMetrics getLineMetrics( String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                            int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                            Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return font.getLineMetrics(str, beginIndex, limit, myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Returns the {@link LineMetrics} object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * character array in the specified {@link Graphics} context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param chars the specified character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param beginIndex the initial offset of <code>chars</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param limit the end offset of <code>chars</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return a <code>LineMetrics</code> object created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * specified character array and <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public LineMetrics getLineMetrics(char [] chars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                            int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                            Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return font.getLineMetrics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                chars, beginIndex, limit, myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Returns the {@link LineMetrics} object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * {@link CharacterIterator} in the specified {@link Graphics}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param ci the specified <code>CharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param beginIndex the initial offset in <code>ci</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param limit the end index of <code>ci</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return a <code>LineMetrics</code> object created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public LineMetrics getLineMetrics(CharacterIterator ci,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                            int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                            Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return font.getLineMetrics(ci, beginIndex, limit, myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Returns the bounds of the specified <code>String</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * specified <code>Graphics</code> context.  The bounds is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * to layout the <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * (see {@link java.awt.FontMetrics class notes}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param str the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return a {@link Rectangle2D} that is the bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * specified <code>String</code> in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @see java.awt.Font#getStringBounds(String, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public Rectangle2D getStringBounds( String str, Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return font.getStringBounds(str, myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Returns the bounds of the specified <code>String</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * specified <code>Graphics</code> context.  The bounds is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * to layout the <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * (see {@link java.awt.FontMetrics class notes}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param str the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param beginIndex the offset of the beginning of <code>str</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param limit the end offset of <code>str</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return a <code>Rectangle2D</code> that is the bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * specified <code>String</code> in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @see java.awt.Font#getStringBounds(String, int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public Rectangle2D getStringBounds( String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                        int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                        Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return font.getStringBounds(str, beginIndex, limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                        myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Returns the bounds of the specified array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * in the specified <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * The bounds is used to layout the <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * created with the specified array of characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * <code>beginIndex</code> and <code>limit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * (see {@link java.awt.FontMetrics class notes}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param chars an array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param beginIndex the initial offset of the array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param limit the end offset of the array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return a <code>Rectangle2D</code> that is the bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * specified character array in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public Rectangle2D getStringBounds( char [] chars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                        int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                        Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return font.getStringBounds(chars, beginIndex, limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                        myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Returns the bounds of the characters indexed in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>CharacterIterator</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * specified <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * (see {@link java.awt.FontMetrics class notes}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @param ci the specified <code>CharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param beginIndex the initial offset in <code>ci</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param limit the end index of <code>ci</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return a <code>Rectangle2D</code> that is the bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * characters indexed in the specified <code>CharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * in the specified <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public Rectangle2D getStringBounds(CharacterIterator ci,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                        int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                        Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return font.getStringBounds(ci, beginIndex, limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                        myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Returns the bounds for the character with the maximum bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * in the specified <code>Graphics</code> context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param context the specified <code>Graphics</code> context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @return a <code>Rectangle2D</code> that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * bounding box for the character with the maximum bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see java.awt.Font#getMaxCharBounds(FontRenderContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public Rectangle2D getMaxCharBounds(Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return font.getMaxCharBounds(myFRC(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    private FontRenderContext myFRC(Graphics context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (context instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return ((Graphics2D)context).getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return DEFAULT_FRC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns a representation of this <code>FontMetrics</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * object's values as a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @return    a <code>String</code> representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <code>FontMetrics</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @since     JDK1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            "[font=" + getFont() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            "ascent=" + getAscent() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            ", descent=" + getDescent() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            ", height=" + getHeight() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
}