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

Print this page




  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
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.Graphics2D;
  29 import java.awt.font.FontRenderContext;
  30 import java.awt.font.LineMetrics;
  31 import java.awt.geom.Rectangle2D;


  32 import java.text.CharacterIterator;
  33 
  34 /**
  35  * The <code>FontMetrics</code> class defines a font metrics object, which
  36  * encapsulates information about the rendering of a particular font on a
  37  * particular screen.
  38  * <p>
  39  * <b>Note to subclassers</b>: Since many of these methods form closed,
  40  * mutually recursive loops, you must take care that you implement
  41  * at least one of the methods in each such loop to prevent
  42  * infinite recursion when your subclass is used.
  43  * In particular, the following is the minimal suggested set of methods
  44  * to override in order to ensure correctness and prevent infinite
  45  * recursion (though other subsets are equally feasible):
  46  * <ul>
  47  * <li>{@link #getAscent()}
  48  * <li>{@link #getLeading()}
  49  * <li>{@link #getMaxAdvance()}
  50  * <li>{@link #charWidth(char)}
  51  * <li>{@link #charsWidth(char[], int, int)}


 393      * distance from the leftmost point to the rightmost point on the
 394      * string's baseline.  The advance of a <code>String</code>
 395      * is not necessarily the sum of the advances of its characters.
 396      * This is equivalent to measuring a <code>String</code> 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</code> array in the <code>Font</code>
 403      *                  described by
 404      *               this <code>FontMetrics</code> object.
 405      * @throws    NullPointerException if <code>data</code> is null.
 406      * @throws    IndexOutOfBoundsException if the <code>off</code>
 407      *            and <code>len</code> arguments index bytes outside
 408      *            the bounds of the <code>data</code> array.
 409      * @see       #charsWidth(char[], int, int)
 410      * @see       #stringWidth(String)
 411      */
 412     public int bytesWidth(byte data[], int off, int len) {
 413         return stringWidth(new String(data, 0, off, len));
 414     }
 415 
 416     /**
 417      * Gets the advance widths of the first 256 characters in the
 418      * <code>Font</code>.  The advance is the
 419      * distance from the leftmost point to the rightmost point on the
 420      * character's baseline.  Note that the advance of a
 421      * <code>String</code> is not necessarily the sum of the advances
 422      * of its characters.
 423      * @return    an array storing the advance widths of the
 424      *                 characters in the <code>Font</code>
 425      *                 described by this <code>FontMetrics</code> object.
 426      */
 427     public int[] getWidths() {
 428         int widths[] = new int[256];
 429         for (char ch = 0 ; ch < 256 ; ch++) {
 430             widths[ch] = charWidth(ch);
 431         }
 432         return widths;
 433     }




  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
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.Graphics2D;
  29 import java.awt.font.FontRenderContext;
  30 import java.awt.font.LineMetrics;
  31 import java.awt.geom.Rectangle2D;
  32 import java.nio.charset.Charset;
  33 import java.nio.charset.StandardCharsets;
  34 import java.text.CharacterIterator;
  35 
  36 /**
  37  * The <code>FontMetrics</code> class defines a font metrics object, which
  38  * encapsulates information about the rendering of a particular font on a
  39  * particular screen.
  40  * <p>
  41  * <b>Note to subclassers</b>: Since many of these methods form closed,
  42  * mutually recursive loops, you must take care that you implement
  43  * at least one of the methods in each such loop to prevent
  44  * infinite recursion when your subclass is used.
  45  * In particular, the following is the minimal suggested set of methods
  46  * to override in order to ensure correctness and prevent infinite
  47  * recursion (though other subsets are equally feasible):
  48  * <ul>
  49  * <li>{@link #getAscent()}
  50  * <li>{@link #getLeading()}
  51  * <li>{@link #getMaxAdvance()}
  52  * <li>{@link #charWidth(char)}
  53  * <li>{@link #charsWidth(char[], int, int)}


 395      * distance from the leftmost point to the rightmost point on the
 396      * string's baseline.  The advance of a <code>String</code>
 397      * is not necessarily the sum of the advances of its characters.
 398      * This is equivalent to measuring a <code>String</code> of the
 399      * characters in the specified range.
 400      * @param data the array of bytes to be measured
 401      * @param off the start offset of the bytes in the array
 402      * @param len the number of bytes to be measured from the array
 403      * @return    the advance width of the subarray of the specified
 404      *               <code>byte</code> array in the <code>Font</code>
 405      *                  described by
 406      *               this <code>FontMetrics</code> object.
 407      * @throws    NullPointerException if <code>data</code> is null.
 408      * @throws    IndexOutOfBoundsException if the <code>off</code>
 409      *            and <code>len</code> arguments index bytes outside
 410      *            the bounds of the <code>data</code> array.
 411      * @see       #charsWidth(char[], int, int)
 412      * @see       #stringWidth(String)
 413      */
 414     public int bytesWidth(byte data[], int off, int len) {
 415         return stringWidth(new String(data, off, len, StandardCharsets.ISO_8859_1));
 416     }
 417 
 418     /**
 419      * Gets the advance widths of the first 256 characters in the
 420      * <code>Font</code>.  The advance is the
 421      * distance from the leftmost point to the rightmost point on the
 422      * character's baseline.  Note that the advance of a
 423      * <code>String</code> is not necessarily the sum of the advances
 424      * of its characters.
 425      * @return    an array storing the advance widths of the
 426      *                 characters in the <code>Font</code>
 427      *                 described by this <code>FontMetrics</code> object.
 428      */
 429     public int[] getWidths() {
 430         int widths[] = new int[256];
 431         for (char ch = 0 ; ch < 256 ; ch++) {
 432             widths[ch] = charWidth(ch);
 433         }
 434         return widths;
 435     }