Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/FontMetrics.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.Graphics2D;28import java.awt.font.FontRenderContext;29import java.awt.font.LineMetrics;30import java.awt.geom.Rectangle2D;31import java.text.CharacterIterator;3233/**34* The <code>FontMetrics</code> class defines a font metrics object, which35* encapsulates information about the rendering of a particular font on a36* particular screen.37* <p>38* <b>Note to subclassers</b>: Since many of these methods form closed,39* mutually recursive loops, you must take care that you implement40* at least one of the methods in each such loop to prevent41* infinite recursion when your subclass is used.42* In particular, the following is the minimal suggested set of methods43* to override in order to ensure correctness and prevent infinite44* recursion (though other subsets are equally feasible):45* <ul>46* <li>{@link #getAscent()}47* <li>{@link #getLeading()}48* <li>{@link #getMaxAdvance()}49* <li>{@link #charWidth(char)}50* <li>{@link #charsWidth(char[], int, int)}51* </ul>52* <p>53* <img src="doc-files/FontMetrics-1.gif" alt="The letter 'p' showing its 'reference point'"54* style="border:15px; float:right; margin: 7px 10px;">55* Note that the implementations of these methods are56* inefficient, so they are usually overridden with more efficient57* toolkit-specific implementations.58* <p>59* When an application asks to place a character at the position60* (<i>x</i>, <i>y</i>), the character is placed so that its61* reference point (shown as the dot in the accompanying image) is62* put at that position. The reference point specifies a horizontal63* line called the <i>baseline</i> of the character. In normal64* printing, the baselines of characters should align.65* <p>66* In addition, every character in a font has an <i>ascent</i>, a67* <i>descent</i>, and an <i>advance width</i>. The ascent is the68* amount by which the character ascends above the baseline. The69* descent is the amount by which the character descends below the70* baseline. The advance width indicates the position at which AWT71* should place the next character.72* <p>73* An array of characters or a string can also have an ascent, a74* descent, and an advance width. The ascent of the array is the75* maximum ascent of any character in the array. The descent is the76* maximum descent of any character in the array. The advance width77* is the sum of the advance widths of each of the characters in the78* character array. The advance of a <code>String</code> is the79* distance along the baseline of the <code>String</code>. This80* distance is the width that should be used for centering or81* right-aligning the <code>String</code>.82* <p>Note that the advance of a <code>String</code> is not necessarily83* the sum of the advances of its characters measured in isolation84* because the width of a character can vary depending on its context.85* For example, in Arabic text, the shape of a character can change86* in order to connect to other characters. Also, in some scripts,87* certain character sequences can be represented by a single shape,88* called a <em>ligature</em>. Measuring characters individually does89* not account for these transformations.90* <p>Font metrics are baseline-relative, meaning that they are91* generally independent of the rotation applied to the font (modulo92* possible grid hinting effects). See {@link java.awt.Font Font}.93*94* @author Jim Graham95* @see java.awt.Font96* @since JDK1.097*/98public abstract class FontMetrics implements java.io.Serializable {99100static {101/* ensure that the necessary native libraries are loaded */102Toolkit.loadLibraries();103if (!GraphicsEnvironment.isHeadless()) {104initIDs();105}106}107108private static final FontRenderContext109DEFAULT_FRC = new FontRenderContext(null, false, false);110111/**112* The actual {@link Font} from which the font metrics are113* created.114* This cannot be null.115*116* @serial117* @see #getFont()118*/119protected Font font;120121/*122* JDK 1.1 serialVersionUID123*/124private static final long serialVersionUID = 1681126225205050147L;125126/**127* Creates a new <code>FontMetrics</code> object for finding out128* height and width information about the specified <code>Font</code>129* and specific character glyphs in that <code>Font</code>.130* @param font the <code>Font</code>131* @see java.awt.Font132*/133protected FontMetrics(Font font) {134this.font = font;135}136137/**138* Gets the <code>Font</code> described by this139* <code>FontMetrics</code> object.140* @return the <code>Font</code> described by this141* <code>FontMetrics</code> object.142*/143public Font getFont() {144return font;145}146147/**148* Gets the <code>FontRenderContext</code> used by this149* <code>FontMetrics</code> object to measure text.150* <p>151* Note that methods in this class which take a <code>Graphics</code>152* parameter measure text using the <code>FontRenderContext</code>153* of that <code>Graphics</code> object, and not this154* <code>FontRenderContext</code>155* @return the <code>FontRenderContext</code> used by this156* <code>FontMetrics</code> object.157* @since 1.6158*/159public FontRenderContext getFontRenderContext() {160return DEFAULT_FRC;161}162163/**164* Determines the <em>standard leading</em> of the165* <code>Font</code> described by this <code>FontMetrics</code>166* object. The standard leading, or167* interline spacing, is the logical amount of space to be reserved168* between the descent of one line of text and the ascent of the next169* line. The height metric is calculated to include this extra space.170* @return the standard leading of the <code>Font</code>.171* @see #getHeight()172* @see #getAscent()173* @see #getDescent()174*/175public int getLeading() {176return 0;177}178179/**180* Determines the <em>font ascent</em> of the <code>Font</code>181* described by this <code>FontMetrics</code> object. The font ascent182* is the distance from the font's baseline to the top of most183* alphanumeric characters. Some characters in the <code>Font</code>184* might extend above the font ascent line.185* @return the font ascent of the <code>Font</code>.186* @see #getMaxAscent()187*/188public int getAscent() {189return font.getSize();190}191192/**193* Determines the <em>font descent</em> of the <code>Font</code>194* described by this195* <code>FontMetrics</code> object. The font descent is the distance196* from the font's baseline to the bottom of most alphanumeric197* characters with descenders. Some characters in the198* <code>Font</code> might extend199* below the font descent line.200* @return the font descent of the <code>Font</code>.201* @see #getMaxDescent()202*/203public int getDescent() {204return 0;205}206207/**208* Gets the standard height of a line of text in this font. This209* is the distance between the baseline of adjacent lines of text.210* It is the sum of the leading + ascent + descent. Due to rounding211* this may not be the same as getAscent() + getDescent() + getLeading().212* There is no guarantee that lines of text spaced at this distance are213* disjoint; such lines may overlap if some characters overshoot214* either the standard ascent or the standard descent metric.215* @return the standard height of the font.216* @see #getLeading()217* @see #getAscent()218* @see #getDescent()219*/220public int getHeight() {221return getLeading() + getAscent() + getDescent();222}223224/**225* Determines the maximum ascent of the <code>Font</code>226* described by this <code>FontMetrics</code> object. No character227* extends further above the font's baseline than this height.228* @return the maximum ascent of any character in the229* <code>Font</code>.230* @see #getAscent()231*/232public int getMaxAscent() {233return getAscent();234}235236/**237* Determines the maximum descent of the <code>Font</code>238* described by this <code>FontMetrics</code> object. No character239* extends further below the font's baseline than this height.240* @return the maximum descent of any character in the241* <code>Font</code>.242* @see #getDescent()243*/244public int getMaxDescent() {245return getDescent();246}247248/**249* For backward compatibility only.250* @return the maximum descent of any character in the251* <code>Font</code>.252* @see #getMaxDescent()253* @deprecated As of JDK version 1.1.1,254* replaced by <code>getMaxDescent()</code>.255*/256@Deprecated257public int getMaxDecent() {258return getMaxDescent();259}260261/**262* Gets the maximum advance width of any character in this263* <code>Font</code>. The advance is the264* distance from the leftmost point to the rightmost point on the265* string's baseline. The advance of a <code>String</code> is266* not necessarily the sum of the advances of its characters.267* @return the maximum advance width of any character268* in the <code>Font</code>, or <code>-1</code> if the269* maximum advance width is not known.270*/271public int getMaxAdvance() {272return -1;273}274275/**276* Returns the advance width of the specified character in this277* <code>Font</code>. The advance is the278* distance from the leftmost point to the rightmost point on the279* character's baseline. Note that the advance of a280* <code>String</code> is not necessarily the sum of the advances281* of its characters.282*283* <p>This method doesn't validate the specified character to be a284* valid Unicode code point. The caller must validate the285* character value using {@link286* java.lang.Character#isValidCodePoint(int)287* Character.isValidCodePoint} if necessary.288*289* @param codePoint the character (Unicode code point) to be measured290* @return the advance width of the specified character291* in the <code>Font</code> described by this292* <code>FontMetrics</code> object.293* @see #charsWidth(char[], int, int)294* @see #stringWidth(String)295*/296public int charWidth(int codePoint) {297if (!Character.isValidCodePoint(codePoint)) {298codePoint = 0xffff; // substitute missing glyph width299}300301if (codePoint < 256) {302return getWidths()[codePoint];303} else {304char[] buffer = new char[2];305int len = Character.toChars(codePoint, buffer, 0);306return charsWidth(buffer, 0, len);307}308}309310/**311* Returns the advance width of the specified character in this312* <code>Font</code>. The advance is the313* distance from the leftmost point to the rightmost point on the314* character's baseline. Note that the advance of a315* <code>String</code> is not necessarily the sum of the advances316* of its characters.317*318* <p><b>Note:</b> This method cannot handle <a319* href="../lang/Character.html#supplementary"> supplementary320* characters</a>. To support all Unicode characters, including321* supplementary characters, use the {@link #charWidth(int)} method.322*323* @param ch the character to be measured324* @return the advance width of the specified character325* in the <code>Font</code> described by this326* <code>FontMetrics</code> object.327* @see #charsWidth(char[], int, int)328* @see #stringWidth(String)329*/330public int charWidth(char ch) {331if (ch < 256) {332return getWidths()[ch];333}334char data[] = {ch};335return charsWidth(data, 0, 1);336}337338/**339* Returns the total advance width for showing the specified340* <code>String</code> in this <code>Font</code>. The advance341* is the distance from the leftmost point to the rightmost point342* on the string's baseline.343* <p>344* Note that the advance of a <code>String</code> is345* not necessarily the sum of the advances of its characters.346* @param str the <code>String</code> to be measured347* @return the advance width of the specified <code>String</code>348* in the <code>Font</code> described by this349* <code>FontMetrics</code>.350* @throws NullPointerException if str is null.351* @see #bytesWidth(byte[], int, int)352* @see #charsWidth(char[], int, int)353* @see #getStringBounds(String, Graphics)354*/355public int stringWidth(String str) {356int len = str.length();357char data[] = new char[len];358str.getChars(0, len, data, 0);359return charsWidth(data, 0, len);360}361362/**363* Returns the total advance width for showing the specified array364* of characters in this <code>Font</code>. The advance is the365* distance from the leftmost point to the rightmost point on the366* string's baseline. The advance of a <code>String</code>367* is not necessarily the sum of the advances of its characters.368* This is equivalent to measuring a <code>String</code> of the369* characters in the specified range.370* @param data the array of characters to be measured371* @param off the start offset of the characters in the array372* @param len the number of characters to be measured from the array373* @return the advance width of the subarray of the specified374* <code>char</code> array in the font described by375* this <code>FontMetrics</code> object.376* @throws NullPointerException if <code>data</code> is null.377* @throws IndexOutOfBoundsException if the <code>off</code>378* and <code>len</code> arguments index characters outside379* the bounds of the <code>data</code> array.380* @see #charWidth(int)381* @see #charWidth(char)382* @see #bytesWidth(byte[], int, int)383* @see #stringWidth(String)384*/385public int charsWidth(char data[], int off, int len) {386return stringWidth(new String(data, off, len));387}388389/**390* Returns the total advance width for showing the specified array391* of bytes in this <code>Font</code>. The advance is the392* distance from the leftmost point to the rightmost point on the393* string's baseline. The advance of a <code>String</code>394* is not necessarily the sum of the advances of its characters.395* This is equivalent to measuring a <code>String</code> of the396* characters in the specified range.397* @param data the array of bytes to be measured398* @param off the start offset of the bytes in the array399* @param len the number of bytes to be measured from the array400* @return the advance width of the subarray of the specified401* <code>byte</code> array in the <code>Font</code>402* described by403* this <code>FontMetrics</code> object.404* @throws NullPointerException if <code>data</code> is null.405* @throws IndexOutOfBoundsException if the <code>off</code>406* and <code>len</code> arguments index bytes outside407* the bounds of the <code>data</code> array.408* @see #charsWidth(char[], int, int)409* @see #stringWidth(String)410*/411public int bytesWidth(byte data[], int off, int len) {412return stringWidth(new String(data, 0, off, len));413}414415/**416* Gets the advance widths of the first 256 characters in the417* <code>Font</code>. The advance is the418* distance from the leftmost point to the rightmost point on the419* character's baseline. Note that the advance of a420* <code>String</code> is not necessarily the sum of the advances421* of its characters.422* @return an array storing the advance widths of the423* characters in the <code>Font</code>424* described by this <code>FontMetrics</code> object.425*/426public int[] getWidths() {427int widths[] = new int[256];428for (char ch = 0 ; ch < 256 ; ch++) {429widths[ch] = charWidth(ch);430}431return widths;432}433434/**435* Checks to see if the <code>Font</code> has uniform line metrics. A436* composite font may consist of several different fonts to cover437* various character sets. In such cases, the438* <code>FontLineMetrics</code> objects are not uniform.439* Different fonts may have a different ascent, descent, metrics and440* so on. This information is sometimes necessary for line441* measuring and line breaking.442* @return <code>true</code> if the font has uniform line metrics;443* <code>false</code> otherwise.444* @see java.awt.Font#hasUniformLineMetrics()445*/446public boolean hasUniformLineMetrics() {447return font.hasUniformLineMetrics();448}449450/**451* Returns the {@link LineMetrics} object for the specified452* <code>String</code> in the specified {@link Graphics} context.453* @param str the specified <code>String</code>454* @param context the specified <code>Graphics</code> context455* @return a <code>LineMetrics</code> object created with the456* specified <code>String</code> and <code>Graphics</code> context.457* @see java.awt.Font#getLineMetrics(String, FontRenderContext)458*/459public LineMetrics getLineMetrics( String str, Graphics context) {460return font.getLineMetrics(str, myFRC(context));461}462463/**464* Returns the {@link LineMetrics} object for the specified465* <code>String</code> in the specified {@link Graphics} context.466* @param str the specified <code>String</code>467* @param beginIndex the initial offset of <code>str</code>468* @param limit the end offset of <code>str</code>469* @param context the specified <code>Graphics</code> context470* @return a <code>LineMetrics</code> object created with the471* specified <code>String</code> and <code>Graphics</code> context.472* @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)473*/474public LineMetrics getLineMetrics( String str,475int beginIndex, int limit,476Graphics context) {477return font.getLineMetrics(str, beginIndex, limit, myFRC(context));478}479480/**481* Returns the {@link LineMetrics} object for the specified482* character array in the specified {@link Graphics} context.483* @param chars the specified character array484* @param beginIndex the initial offset of <code>chars</code>485* @param limit the end offset of <code>chars</code>486* @param context the specified <code>Graphics</code> context487* @return a <code>LineMetrics</code> object created with the488* specified character array and <code>Graphics</code> context.489* @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)490*/491public LineMetrics getLineMetrics(char [] chars,492int beginIndex, int limit,493Graphics context) {494return font.getLineMetrics(495chars, beginIndex, limit, myFRC(context));496}497498/**499* Returns the {@link LineMetrics} object for the specified500* {@link CharacterIterator} in the specified {@link Graphics}501* context.502* @param ci the specified <code>CharacterIterator</code>503* @param beginIndex the initial offset in <code>ci</code>504* @param limit the end index of <code>ci</code>505* @param context the specified <code>Graphics</code> context506* @return a <code>LineMetrics</code> object created with the507* specified arguments.508* @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)509*/510public LineMetrics getLineMetrics(CharacterIterator ci,511int beginIndex, int limit,512Graphics context) {513return font.getLineMetrics(ci, beginIndex, limit, myFRC(context));514}515516/**517* Returns the bounds of the specified <code>String</code> in the518* specified <code>Graphics</code> context. The bounds is used519* to layout the <code>String</code>.520* <p>Note: The returned bounds is in baseline-relative coordinates521* (see {@link java.awt.FontMetrics class notes}).522* @param str the specified <code>String</code>523* @param context the specified <code>Graphics</code> context524* @return a {@link Rectangle2D} that is the bounding box of the525* specified <code>String</code> in the specified526* <code>Graphics</code> context.527* @see java.awt.Font#getStringBounds(String, FontRenderContext)528*/529public Rectangle2D getStringBounds( String str, Graphics context) {530return font.getStringBounds(str, myFRC(context));531}532533/**534* Returns the bounds of the specified <code>String</code> in the535* specified <code>Graphics</code> context. The bounds is used536* to layout the <code>String</code>.537* <p>Note: The returned bounds is in baseline-relative coordinates538* (see {@link java.awt.FontMetrics class notes}).539* @param str the specified <code>String</code>540* @param beginIndex the offset of the beginning of <code>str</code>541* @param limit the end offset of <code>str</code>542* @param context the specified <code>Graphics</code> context543* @return a <code>Rectangle2D</code> that is the bounding box of the544* specified <code>String</code> in the specified545* <code>Graphics</code> context.546* @see java.awt.Font#getStringBounds(String, int, int, FontRenderContext)547*/548public Rectangle2D getStringBounds( String str,549int beginIndex, int limit,550Graphics context) {551return font.getStringBounds(str, beginIndex, limit,552myFRC(context));553}554555/**556* Returns the bounds of the specified array of characters557* in the specified <code>Graphics</code> context.558* The bounds is used to layout the <code>String</code>559* created with the specified array of characters,560* <code>beginIndex</code> and <code>limit</code>.561* <p>Note: The returned bounds is in baseline-relative coordinates562* (see {@link java.awt.FontMetrics class notes}).563* @param chars an array of characters564* @param beginIndex the initial offset of the array of565* characters566* @param limit the end offset of the array of characters567* @param context the specified <code>Graphics</code> context568* @return a <code>Rectangle2D</code> that is the bounding box of the569* specified character array in the specified570* <code>Graphics</code> context.571* @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext)572*/573public Rectangle2D getStringBounds( char [] chars,574int beginIndex, int limit,575Graphics context) {576return font.getStringBounds(chars, beginIndex, limit,577myFRC(context));578}579580/**581* Returns the bounds of the characters indexed in the specified582* <code>CharacterIterator</code> in the583* specified <code>Graphics</code> context.584* <p>Note: The returned bounds is in baseline-relative coordinates585* (see {@link java.awt.FontMetrics class notes}).586* @param ci the specified <code>CharacterIterator</code>587* @param beginIndex the initial offset in <code>ci</code>588* @param limit the end index of <code>ci</code>589* @param context the specified <code>Graphics</code> context590* @return a <code>Rectangle2D</code> that is the bounding box of the591* characters indexed in the specified <code>CharacterIterator</code>592* in the specified <code>Graphics</code> context.593* @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext)594*/595public Rectangle2D getStringBounds(CharacterIterator ci,596int beginIndex, int limit,597Graphics context) {598return font.getStringBounds(ci, beginIndex, limit,599myFRC(context));600}601602/**603* Returns the bounds for the character with the maximum bounds604* in the specified <code>Graphics</code> context.605* @param context the specified <code>Graphics</code> context606* @return a <code>Rectangle2D</code> that is the607* bounding box for the character with the maximum bounds.608* @see java.awt.Font#getMaxCharBounds(FontRenderContext)609*/610public Rectangle2D getMaxCharBounds(Graphics context) {611return font.getMaxCharBounds(myFRC(context));612}613614private FontRenderContext myFRC(Graphics context) {615if (context instanceof Graphics2D) {616return ((Graphics2D)context).getFontRenderContext();617}618return DEFAULT_FRC;619}620621622/**623* Returns a representation of this <code>FontMetrics</code>624* object's values as a <code>String</code>.625* @return a <code>String</code> representation of this626* <code>FontMetrics</code> object.627* @since JDK1.0.628*/629public String toString() {630return getClass().getName() +631"[font=" + getFont() +632"ascent=" + getAscent() +633", descent=" + getDescent() +634", height=" + getHeight() + "]";635}636637/**638* Initialize JNI field and method IDs639*/640private static native void initIDs();641}642643644