home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / FontMetrics.java < prev    next >
Text File  |  1996-05-03  |  7KB  |  228 lines

  1. /*
  2.  * @(#)FontMetrics.java    1.10 96/02/28 Jim Graham
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt;
  21.  
  22. /** 
  23.  * A font metrics object. Note that the implementations of these
  24.  * methods are inefficient, they are usually overridden with more
  25.  * efficient toolkit specific implementations.
  26.  * 
  27.  * @version     1.10 28 Feb 1996
  28.  * @author     Jim Graham
  29.  */
  30. public abstract class FontMetrics {
  31.     /**
  32.      * The actual font.
  33.      * @see #getFont
  34.      */
  35.     protected Font font;
  36.  
  37.     /**
  38.      * Creates a new FontMetrics object with the specified font.
  39.      * @param font the font
  40.      * @see Font
  41.      */
  42.     protected FontMetrics(Font font) {
  43.     this.font = font;
  44.     }
  45.  
  46.     /**
  47.      * Gets the font.
  48.      */
  49.     public Font getFont() {
  50.     return font;
  51.     }
  52.  
  53.     /**
  54.      * Gets the standard leading, or line spacing, for the font.  
  55.      * This is the logical amount of space to be reserved between the
  56.      * descent of one line of text and the ascent of the next line.
  57.      * The height metric is calculated to include this extra space.
  58.      */
  59.     public int getLeading() {
  60.     return 0;
  61.     }
  62.  
  63.     /**
  64.      * Gets the font ascent. The font ascent is the distance from the 
  65.      * base line to the top of most Alphanumeric characters.  Note,
  66.      * however, that some characters in the font may extend above
  67.      * this height.
  68.      * @see #getMaxAscent
  69.      */
  70.     public int getAscent() {
  71.     return font.getSize();
  72.     }
  73.  
  74.     /**
  75.      * Gets the font descent. The font descent is the distance from the 
  76.      * base line to the bottom of most Alphanumeric characters.  Note,
  77.      * however, that some characters in the font may extend below this
  78.      * height.
  79.      * @see #getMaxDescent
  80.      */
  81.     public int getDescent() {
  82.     return 0;
  83.     }
  84.  
  85.     /**
  86.      * Gets the standard height of a line of text in this font.  This
  87.      * is the distance between the baseline of adjacent lines of text.
  88.      * It is the sum of the leading + ascent + descent.  There is no
  89.      * guarantee that lines of text spaced at this distance will be
  90.      * disjoint; such lines may overlap if some characters overshoot
  91.      * either the standard ascent or the standard descent metric.
  92.      */
  93.     public int getHeight() {
  94.     return getLeading() + getAscent() + getDescent();
  95.     }
  96.  
  97.     /**
  98.      * Gets the maximum ascent of all characters in this Font.
  99.      * No character will extend further above the baseline than this 
  100.      * distance.
  101.      * @see #getAscent
  102.      */
  103.     public int getMaxAscent() {
  104.     return getAscent();
  105.     }
  106.  
  107.     /**
  108.      * Gets the maximum descent of all characters in this Font.
  109.      * No character will descend futher below the baseline than this
  110.      * distance.
  111.      * @see #getDescent
  112.      */
  113.     public int getMaxDescent() {
  114.     return getDescent();
  115.     }
  116.  
  117.     /**
  118.      * For backward compatibility only.
  119.      * @see #getMaxDescent
  120.      */
  121.     public int getMaxDecent() {
  122.     return getMaxDescent();
  123.     }
  124.  
  125.     /**
  126.      * Gets the maximum advance width of any character in this Font. 
  127.      * The advance width is the amount by which the current point is
  128.      * moved from one character to the next in a line of text.
  129.      * @return -1 if the max advance is not known.
  130.      */
  131.     public int getMaxAdvance() {
  132.     return -1;
  133.     }
  134.  
  135.     /** 
  136.      * Returns the advance width of the specified character in this Font.
  137.      * The advance width is the amount by which the current point is
  138.      * moved from one character to the next in a line of text.
  139.      * @param ch the character to be measured
  140.      * @see #stringWidth
  141.      */
  142.     public int charWidth(int ch) {
  143.     return charWidth((char)ch);
  144.     }
  145.  
  146.     /** 
  147.      * Returns the advance width of the specified character in this Font.
  148.      * The advance width is the amount by which the current point is
  149.      * moved from one character to the next in a line of text.
  150.      * @param ch the character to be measured
  151.      * @see #stringWidth
  152.      */
  153.     public int charWidth(char ch) {
  154.     if (ch < 256) {
  155.         return getWidths()[ch];
  156.     }
  157.     char data[] = {ch};
  158.     return charsWidth(data, 0, 1);
  159.     }
  160.  
  161.     /** 
  162.      * Returns the total advance width for showing the specified String
  163.      * in this Font.
  164.      * The advance width is the amount by which the current point is
  165.      * moved from one character to the next in a line of text.
  166.      * @param str the String to be measured
  167.      * @see #charsWidth
  168.      * @see #bytesWidth
  169.      */
  170.     public int stringWidth(String str) {
  171.     int len = str.length();
  172.     char data[] = new char[len];
  173.     str.getChars(0, len, data, 0);
  174.     return charsWidth(data, 0, len);
  175.     }
  176.  
  177.     /** 
  178.      * Returns the total advance width for showing the specified array
  179.      * of characters in this Font.
  180.      * The advance width is the amount by which the current point is
  181.      * moved from one character to the next in a line of text.
  182.      * @param data the array of characters to be measured
  183.      * @param off the start offset of the characters in the array
  184.      * @param len the number of characters to be measured from the array
  185.      * @see #stringWidth
  186.      * @see #bytesWidth
  187.      */
  188.     public int charsWidth(char data[], int off, int len) {
  189.     return stringWidth(new String(data, off, len));
  190.     }
  191.  
  192.     /** 
  193.      * Returns the total advance width for showing the specified array
  194.      * of bytes in this Font.
  195.      * The advance width is the amount by which the current point is
  196.      * moved from one character to the next in a line of text.
  197.      * @param data the array of bytes to be measured
  198.      * @param off the start offset of the bytes in the array
  199.      * @param len the number of bytes to be measured from the array
  200.      * @see #stringWidth
  201.      * @see #charsWidth
  202.      */
  203.     public int bytesWidth(byte data[], int off, int len) {
  204.     return stringWidth(new String(data, 0, off, len));
  205.     }
  206.  
  207.     /**
  208.      * Gets the advance widths of the first 256 characters in the Font.
  209.      * The advance width is the amount by which the current point is
  210.      * moved from one character to the next in a line of text.
  211.      */
  212.     public int[] getWidths() {
  213.     int widths[] = new int[256];
  214.     for (char ch = 0 ; ch < 256 ; ch++) {
  215.         widths[ch] = charWidth(ch);
  216.     }
  217.     return widths;
  218.     }
  219.  
  220.     /** 
  221.      * Returns the String representation of this FontMetric's values.
  222.      */
  223.     public String toString() {
  224.     return getClass().getName() + "[font=" + getFont() + "ascent=" +
  225.         getAscent() + ", descent=" + getDescent() + ", height=" + getHeight() + "]";
  226.     }
  227. }
  228.