< prev index next >

src/java.desktop/share/classes/java/awt/FontMetrics.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 315      * character's baseline.  Note that the advance of a
 316      * {@code String} is not necessarily the sum of the advances
 317      * of its characters.
 318      *
 319      * <p><b>Note:</b> This method cannot handle <a
 320      * href="../lang/Character.html#supplementary"> supplementary
 321      * characters</a>. To support all Unicode characters, including
 322      * supplementary characters, use the {@link #charWidth(int)} method.
 323      *
 324      * @param ch the character to be measured
 325      * @return     the advance width of the specified character
 326      *                  in the {@code Font} described by this
 327      *                  {@code FontMetrics} object.
 328      * @see        #charsWidth(char[], int, int)
 329      * @see        #stringWidth(String)
 330      */
 331     public int charWidth(char ch) {
 332         if (ch < 256) {
 333             return getWidths()[ch];
 334         }
 335         char data[] = {ch};
 336         return charsWidth(data, 0, 1);
 337     }
 338 
 339     /**
 340      * Returns the total advance width for showing the specified
 341      * {@code String} in this {@code Font}.  The advance
 342      * is the distance from the leftmost point to the rightmost point
 343      * on the string's baseline.
 344      * <p>
 345      * Note that the advance of a {@code String} is
 346      * not necessarily the sum of the advances of its characters.
 347      * @param str the {@code String} to be measured
 348      * @return    the advance width of the specified {@code String}
 349      *                  in the {@code Font} described by this
 350      *                  {@code FontMetrics}.
 351      * @throws NullPointerException if str is null.
 352      * @see       #bytesWidth(byte[], int, int)
 353      * @see       #charsWidth(char[], int, int)
 354      * @see       #getStringBounds(String, Graphics)
 355      */
 356     public int stringWidth(String str) {
 357         int len = str.length();
 358         char data[] = new char[len];
 359         str.getChars(0, len, data, 0);
 360         return charsWidth(data, 0, len);
 361     }
 362 
 363     /**
 364      * Returns the total advance width for showing the specified array
 365      * of characters in this {@code Font}.  The advance is the
 366      * distance from the leftmost point to the rightmost point on the
 367      * string's baseline.  The advance of a {@code String}
 368      * is not necessarily the sum of the advances of its characters.
 369      * This is equivalent to measuring a {@code String} of the
 370      * characters in the specified range.
 371      * @param data the array of characters to be measured
 372      * @param off the start offset of the characters in the array
 373      * @param len the number of characters to be measured from the array
 374      * @return    the advance width of the subarray of the specified
 375      *               {@code char} array in the font described by
 376      *               this {@code FontMetrics} object.
 377      * @throws    NullPointerException if {@code data} is null.
 378      * @throws    IndexOutOfBoundsException if the {@code off}
 379      *            and {@code len} arguments index characters outside
 380      *            the bounds of the {@code data} array.
 381      * @see       #charWidth(int)
 382      * @see       #charWidth(char)
 383      * @see       #bytesWidth(byte[], int, int)
 384      * @see       #stringWidth(String)
 385      */
 386     public int charsWidth(char data[], int off, int len) {
 387         return stringWidth(new String(data, off, len));
 388     }
 389 
 390     /**
 391      * Returns the total advance width for showing the specified array
 392      * of bytes in this {@code Font}.  The advance is the
 393      * distance from the leftmost point to the rightmost point on the
 394      * string's baseline.  The advance of a {@code String}
 395      * is not necessarily the sum of the advances of its characters.
 396      * This is equivalent to measuring a {@code String} of the
 397      * characters in the specified range.
 398      * @param data the array of bytes to be measured
 399      * @param off the start offset of the bytes in the array
 400      * @param len the number of bytes to be measured from the array
 401      * @return    the advance width of the subarray of the specified
 402      *               {@code byte} array in the {@code Font}
 403      *                  described by
 404      *               this {@code FontMetrics} object.
 405      * @throws    NullPointerException if {@code data} is null.
 406      * @throws    IndexOutOfBoundsException if the {@code off}
 407      *            and {@code len} arguments index bytes outside
 408      *            the bounds of the {@code data} array.
 409      * @see       #charsWidth(char[], int, int)
 410      * @see       #stringWidth(String)
 411      */
 412     @SuppressWarnings("deprecation")
 413     public int bytesWidth(byte data[], int off, int len) {
 414         return stringWidth(new String(data, 0, off, len));
 415     }
 416 
 417     /**
 418      * Gets the advance widths of the first 256 characters in the
 419      * {@code Font}.  The advance is the
 420      * distance from the leftmost point to the rightmost point on the
 421      * character's baseline.  Note that the advance of a
 422      * {@code String} is not necessarily the sum of the advances
 423      * of its characters.
 424      * @return    an array storing the advance widths of the
 425      *                 characters in the {@code Font}
 426      *                 described by this {@code FontMetrics} object.
 427      */
 428     public int[] getWidths() {
 429         int widths[] = new int[256];
 430         for (char ch = 0 ; ch < 256 ; ch++) {
 431             widths[ch] = charWidth(ch);
 432         }
 433         return widths;
 434     }
 435 
 436     /**
 437      * Checks to see if the {@code Font} has uniform line metrics.  A
 438      * composite font may consist of several different fonts to cover
 439      * various character sets.  In such cases, the
 440      * {@code FontLineMetrics} objects are not uniform.
 441      * Different fonts may have a different ascent, descent, metrics and
 442      * so on.  This information is sometimes necessary for line
 443      * measuring and line breaking.
 444      * @return {@code true} if the font has uniform line metrics;
 445      * {@code false} otherwise.
 446      * @see java.awt.Font#hasUniformLineMetrics()
 447      */
 448     public boolean hasUniformLineMetrics() {
 449         return font.hasUniformLineMetrics();


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 315      * character's baseline.  Note that the advance of a
 316      * {@code String} is not necessarily the sum of the advances
 317      * of its characters.
 318      *
 319      * <p><b>Note:</b> This method cannot handle <a
 320      * href="../lang/Character.html#supplementary"> supplementary
 321      * characters</a>. To support all Unicode characters, including
 322      * supplementary characters, use the {@link #charWidth(int)} method.
 323      *
 324      * @param ch the character to be measured
 325      * @return     the advance width of the specified character
 326      *                  in the {@code Font} described by this
 327      *                  {@code FontMetrics} object.
 328      * @see        #charsWidth(char[], int, int)
 329      * @see        #stringWidth(String)
 330      */
 331     public int charWidth(char ch) {
 332         if (ch < 256) {
 333             return getWidths()[ch];
 334         }
 335         char[] data = {ch};
 336         return charsWidth(data, 0, 1);
 337     }
 338 
 339     /**
 340      * Returns the total advance width for showing the specified
 341      * {@code String} in this {@code Font}.  The advance
 342      * is the distance from the leftmost point to the rightmost point
 343      * on the string's baseline.
 344      * <p>
 345      * Note that the advance of a {@code String} is
 346      * not necessarily the sum of the advances of its characters.
 347      * @param str the {@code String} to be measured
 348      * @return    the advance width of the specified {@code String}
 349      *                  in the {@code Font} described by this
 350      *                  {@code FontMetrics}.
 351      * @throws NullPointerException if str is null.
 352      * @see       #bytesWidth(byte[], int, int)
 353      * @see       #charsWidth(char[], int, int)
 354      * @see       #getStringBounds(String, Graphics)
 355      */
 356     public int stringWidth(String str) {
 357         int len = str.length();
 358         char[] data = new char[len];
 359         str.getChars(0, len, data, 0);
 360         return charsWidth(data, 0, len);
 361     }
 362 
 363     /**
 364      * Returns the total advance width for showing the specified array
 365      * of characters in this {@code Font}.  The advance is the
 366      * distance from the leftmost point to the rightmost point on the
 367      * string's baseline.  The advance of a {@code String}
 368      * is not necessarily the sum of the advances of its characters.
 369      * This is equivalent to measuring a {@code String} of the
 370      * characters in the specified range.
 371      * @param data the array of characters to be measured
 372      * @param off the start offset of the characters in the array
 373      * @param len the number of characters to be measured from the array
 374      * @return    the advance width of the subarray of the specified
 375      *               {@code char} array in the font described by
 376      *               this {@code FontMetrics} object.
 377      * @throws    NullPointerException if {@code data} is null.
 378      * @throws    IndexOutOfBoundsException if the {@code off}
 379      *            and {@code len} arguments index characters outside
 380      *            the bounds of the {@code data} array.
 381      * @see       #charWidth(int)
 382      * @see       #charWidth(char)
 383      * @see       #bytesWidth(byte[], int, int)
 384      * @see       #stringWidth(String)
 385      */
 386     public int charsWidth(char[] data, int off, int len) {
 387         return stringWidth(new String(data, off, len));
 388     }
 389 
 390     /**
 391      * Returns the total advance width for showing the specified array
 392      * of bytes in this {@code Font}.  The advance is the
 393      * distance from the leftmost point to the rightmost point on the
 394      * string's baseline.  The advance of a {@code String}
 395      * is not necessarily the sum of the advances of its characters.
 396      * This is equivalent to measuring a {@code String} of the
 397      * characters in the specified range.
 398      * @param data the array of bytes to be measured
 399      * @param off the start offset of the bytes in the array
 400      * @param len the number of bytes to be measured from the array
 401      * @return    the advance width of the subarray of the specified
 402      *               {@code byte} array in the {@code Font}
 403      *                  described by
 404      *               this {@code FontMetrics} object.
 405      * @throws    NullPointerException if {@code data} is null.
 406      * @throws    IndexOutOfBoundsException if the {@code off}
 407      *            and {@code len} arguments index bytes outside
 408      *            the bounds of the {@code data} array.
 409      * @see       #charsWidth(char[], int, int)
 410      * @see       #stringWidth(String)
 411      */
 412     @SuppressWarnings("deprecation")
 413     public int bytesWidth(byte[] data, int off, int len) {
 414         return stringWidth(new String(data, 0, off, len));
 415     }
 416 
 417     /**
 418      * Gets the advance widths of the first 256 characters in the
 419      * {@code Font}.  The advance is the
 420      * distance from the leftmost point to the rightmost point on the
 421      * character's baseline.  Note that the advance of a
 422      * {@code String} is not necessarily the sum of the advances
 423      * of its characters.
 424      * @return    an array storing the advance widths of the
 425      *                 characters in the {@code Font}
 426      *                 described by this {@code FontMetrics} object.
 427      */
 428     public int[] getWidths() {
 429         int[] widths = new int[256];
 430         for (char ch = 0 ; ch < 256 ; ch++) {
 431             widths[ch] = charWidth(ch);
 432         }
 433         return widths;
 434     }
 435 
 436     /**
 437      * Checks to see if the {@code Font} has uniform line metrics.  A
 438      * composite font may consist of several different fonts to cover
 439      * various character sets.  In such cases, the
 440      * {@code FontLineMetrics} objects are not uniform.
 441      * Different fonts may have a different ascent, descent, metrics and
 442      * so on.  This information is sometimes necessary for line
 443      * measuring and line breaking.
 444      * @return {@code true} if the font has uniform line metrics;
 445      * {@code false} otherwise.
 446      * @see java.awt.Font#hasUniformLineMetrics()
 447      */
 448     public boolean hasUniformLineMetrics() {
 449         return font.hasUniformLineMetrics();


< prev index next >