home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / FontMetrics.java < prev    next >
Text File  |  1997-10-27  |  8KB  |  254 lines

  1. /*
  2.  * @(#)FontMetrics.java    1.16 97/02/20
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt;
  24.  
  25. /** 
  26.  * A font metrics object. Note that the implementations of these
  27.  * methods are inefficient, they are usually overridden with more
  28.  * efficient toolkit specific implementations.
  29.  * <p>
  30.  * <b>Note to subclassers</b>: Since many of these methods form closed
  31.  * mutually recursive loops, you must take care that you implement
  32.  * at least one of the methods in each such loop in order to prevent
  33.  * infinite recursion when your subclass is used.
  34.  * In particular, the following is the minimal suggested set of methods
  35.  * to override in order to ensure correctness and prevent infinite
  36.  * recursion (though other subsets are equally feasible):
  37.  * <ul>
  38.  * <li><a href=#getAscent>getAscent</a>()
  39.  * <li><a href=#getAscent>getDescent</a>()
  40.  * <li><a href=#getLeading>getLeading</a>()
  41.  * <li><a href=#getMaxAdvance>getMaxAdvance</a>()
  42.  * <li><a href="#charWidth(char)">charWidth</a>(char ch)
  43.  * <li><a href="#charsWidth(char[], int, int)">charsWidth</a>(char data[], int off, int len)
  44.  * </ul>
  45.  * 
  46.  * @version     1.16 02/20/97
  47.  * @author     Jim Graham
  48.  */
  49. public abstract class FontMetrics implements java.io.Serializable {
  50.     /**
  51.      * The actual font.
  52.      * @see #getFont
  53.      */
  54.     protected Font font;
  55.  
  56.     /*
  57.      * JDK 1.1 serialVersionUID 
  58.      */
  59.     private static final long serialVersionUID = 1681126225205050147L;
  60.  
  61.     /**
  62.      * Creates a new FontMetrics object with the specified font.
  63.      * @param font the font
  64.      * @see Font
  65.      */
  66.     protected FontMetrics(Font font) {
  67.     this.font = font;
  68.     }
  69.  
  70.     /**
  71.      * Gets the font.
  72.      */
  73.     public Font getFont() {
  74.     return font;
  75.     }
  76.  
  77.     /**
  78.      * Gets the standard leading, or line spacing, for the font.  
  79.      * This is the logical amount of space to be reserved between the
  80.      * descent of one line of text and the ascent of the next line.
  81.      * The height metric is calculated to include this extra space.
  82.      */
  83.     public int getLeading() {
  84.     return 0;
  85.     }
  86.  
  87.     /**
  88.      * Gets the font ascent. The font ascent is the distance from the 
  89.      * base line to the top of most Alphanumeric characters.  Note,
  90.      * however, that some characters in the font may extend above
  91.      * this height.
  92.      * @see #getMaxAscent
  93.      */
  94.     public int getAscent() {
  95.     return font.getSize();
  96.     }
  97.  
  98.     /**
  99.      * Gets the font descent. The font descent is the distance from the 
  100.      * base line to the bottom of most Alphanumeric characters.  Note,
  101.      * however, that some characters in the font may extend below this
  102.      * height.
  103.      * @see #getMaxDescent
  104.      */
  105.     public int getDescent() {
  106.     return 0;
  107.     }
  108.  
  109.     /**
  110.      * Gets the standard height of a line of text in this font.  This
  111.      * is the distance between the baseline of adjacent lines of text.
  112.      * It is the sum of the leading + ascent + descent.  There is no
  113.      * guarantee that lines of text spaced at this distance will be
  114.      * disjoint; such lines may overlap if some characters overshoot
  115.      * either the standard ascent or the standard descent metric.
  116.      */
  117.     public int getHeight() {
  118.     return getLeading() + getAscent() + getDescent();
  119.     }
  120.  
  121.     /**
  122.      * Gets the maximum ascent of all characters in this Font.
  123.      * No character will extend further above the baseline than this 
  124.      * distance.
  125.      * @see #getAscent
  126.      */
  127.     public int getMaxAscent() {
  128.     return getAscent();
  129.     }
  130.  
  131.     /**
  132.      * Gets the maximum descent of all characters in this Font.
  133.      * No character will descend futher below the baseline than this
  134.      * distance.
  135.      * @see #getDescent
  136.      */
  137.     public int getMaxDescent() {
  138.     return getDescent();
  139.     }
  140.  
  141.     /**
  142.      * For backward compatibility only.
  143.      * @see #getMaxDescent
  144.      * @deprecated As of JDK version 1.1.1,
  145.      * replaced by getMaxDescent().
  146.      */
  147.     public int getMaxDecent() {
  148.     return getMaxDescent();
  149.     }
  150.  
  151.     /**
  152.      * Gets the maximum advance width of any character in this Font. 
  153.      * The advance width is the amount by which the current point is
  154.      * moved from one character to the next in a line of text.
  155.      * @return -1 if the max advance is not known.
  156.      */
  157.     public int getMaxAdvance() {
  158.     return -1;
  159.     }
  160.  
  161.     /** 
  162.      * Returns the advance width of the specified character in this Font.
  163.      * The advance width is the amount by which the current point is
  164.      * moved from one character to the next in a line of text.
  165.      * @param ch the character to be measured
  166.      * @see #stringWidth
  167.      */
  168.     public int charWidth(int ch) {
  169.     return charWidth((char)ch);
  170.     }
  171.  
  172.     /** 
  173.      * Returns the advance width of the specified character in this Font.
  174.      * The advance width is the amount by which the current point is
  175.      * moved from one character to the next in a line of text.
  176.      * @param ch the character to be measured
  177.      * @see #stringWidth
  178.      */
  179.     public int charWidth(char ch) {
  180.     if (ch < 256) {
  181.         return getWidths()[ch];
  182.     }
  183.     char data[] = {ch};
  184.     return charsWidth(data, 0, 1);
  185.     }
  186.  
  187.     /** 
  188.      * Returns the total advance width for showing the specified String
  189.      * in this Font.
  190.      * The advance width is the amount by which the current point is
  191.      * moved from one character to the next in a line of text.
  192.      * @param str the String to be measured
  193.      * @see #charsWidth
  194.      * @see #bytesWidth
  195.      */
  196.     public int stringWidth(String str) {
  197.     int len = str.length();
  198.     char data[] = new char[len];
  199.     str.getChars(0, len, data, 0);
  200.     return charsWidth(data, 0, len);
  201.     }
  202.  
  203.     /** 
  204.      * Returns the total advance width for showing the specified array
  205.      * of characters in this Font.
  206.      * The advance width is the amount by which the current point is
  207.      * moved from one character to the next in a line of text.
  208.      * @param data the array of characters to be measured
  209.      * @param off the start offset of the characters in the array
  210.      * @param len the number of characters to be measured from the array
  211.      * @see #stringWidth
  212.      * @see #bytesWidth
  213.      */
  214.     public int charsWidth(char data[], int off, int len) {
  215.     return stringWidth(new String(data, off, len));
  216.     }
  217.  
  218.     /** 
  219.      * Returns the total advance width for showing the specified array
  220.      * of bytes in this Font.
  221.      * The advance width is the amount by which the current point is
  222.      * moved from one character to the next in a line of text.
  223.      * @param data the array of bytes to be measured
  224.      * @param off the start offset of the bytes in the array
  225.      * @param len the number of bytes to be measured from the array
  226.      * @see #stringWidth
  227.      * @see #charsWidth
  228.      */
  229.     public int bytesWidth(byte data[], int off, int len) {
  230.     return stringWidth(new String(data, 0, off, len));
  231.     }
  232.  
  233.     /**
  234.      * Gets the advance widths of the first 256 characters in the Font.
  235.      * The advance width is the amount by which the current point is
  236.      * moved from one character to the next in a line of text.
  237.      */
  238.     public int[] getWidths() {
  239.     int widths[] = new int[256];
  240.     for (char ch = 0 ; ch < 256 ; ch++) {
  241.         widths[ch] = charWidth(ch);
  242.     }
  243.     return widths;
  244.     }
  245.  
  246.     /** 
  247.      * Returns the String representation of this FontMetric's values.
  248.      */
  249.     public String toString() {
  250.     return getClass().getName() + "[font=" + getFont() + "ascent=" +
  251.         getAscent() + ", descent=" + getDescent() + ", height=" + getHeight() + "]";
  252.     }
  253. }
  254.