home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / fontme~2.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.4 KB  |  204 lines

  1. /*
  2.  * @(#)FontMetrics.java    1.8 95/10/27 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.8 10/27/95
  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 the characters.
  66.      * @see #getMaxAscent
  67.      */
  68.     public int getAscent() {
  69.     return font.getSize();
  70.     }
  71.  
  72.     /**
  73.      * Gets the font descent. The font descent is the distance from the 
  74.      * base line to the bottom of the characters.
  75.      * @see #getMaxDescent
  76.      */
  77.     public int getDescent() {
  78.     return 0;
  79.     }
  80.  
  81.     /**
  82.      * Gets the total height of the font.  This is the distance between
  83.      * the baseline of adjacent lines of text.  It is the sum of the
  84.      * leading + ascent + descent. 
  85.      *  
  86.      */
  87.     public int getHeight() {
  88.     return getLeading() + getAscent() + getDescent();
  89.     }
  90.  
  91.     /**
  92.      * Gets the maximum ascent of all characters in this Font.
  93.      * No character will extend further above the baseline than this 
  94.      * metric.
  95.      * @see #getAscent
  96.      */
  97.     public int getMaxAscent() {
  98.     return getAscent();
  99.     }
  100.  
  101.     /**
  102.      * Gets the maximum descent of all characters.  
  103.      * No character will descend futher below the baseline than this
  104.      * metric.
  105.      * @see #getDescent
  106.      */
  107.     public int getMaxDescent() {
  108.     return getDescent();
  109.     }
  110.     /**
  111.      * For backward compatibility only.
  112.      * @see #getMaxDescent
  113.      */
  114.     public int getMaxDecent() {
  115.     return getMaxDescent();
  116.     }
  117.  
  118.     /**
  119.      * Gets the maximum advance width of any character in this Font. 
  120.      * @return -1 if the max advance is not known.
  121.      */
  122.     public int getMaxAdvance() {
  123.     return -1;
  124.     }
  125.  
  126.     /** 
  127.      * Returns the width of the specified character in this Font.
  128.      * @param ch the specified font
  129.      * @see #stringWidth
  130.      */
  131.     public int charWidth(int ch) {
  132.     return charWidth((char)ch);
  133.     }
  134.  
  135.     /** 
  136.      * Returns the width of the specified character in this Font.
  137.      * @param ch the specified font
  138.      * @see #stringWidth
  139.      */
  140.     public int charWidth(char ch) {
  141.     if (ch < 256) {
  142.         return getWidths()[ch];
  143.     }
  144.     char data[] = {ch};
  145.     return charsWidth(data, 0, 1);
  146.     }
  147.  
  148.     /** 
  149.      * Returns the width of the specified String in this Font.
  150.      * @param str the String to be checked
  151.      * @see #charsWidth
  152.      * @see #bytesWidth
  153.      */
  154.     public int stringWidth(String str) {
  155.     int len = str.length();
  156.     char data[] = new char[len];
  157.     str.getChars(0, len, data, 0);
  158.     return charsWidth(data, 0, len);
  159.     }
  160.  
  161.     /** 
  162.      * Returns the width of the specified character array in this Font.
  163.      * @param data the data to be checked
  164.      * @param off the start offset of the data
  165.      * @param len the maximum number of bytes checked
  166.      * @see #stringWidth
  167.      * @see #bytesWidth
  168.      */
  169.     public int charsWidth(char data[], int off, int len) {
  170.     return stringWidth(new String(data, off, len));
  171.     }
  172.  
  173.     /** 
  174.      * Returns the width of the specified array of bytes in this Font.
  175.      * @param data the data to be checked
  176.      * @param off the start offset of the data
  177.      * @param len the maximum number of bytes checked
  178.      * @see #stringWidth
  179.      * @see #charsWidth
  180.      */
  181.     public int bytesWidth(byte data[], int off, int len) {
  182.     return stringWidth(new String(data, 0, off, len));
  183.     }
  184.  
  185.     /**
  186.      * Gets the widths of the first 256 characters in the Font.
  187.      */
  188.     public int[] getWidths() {
  189.     int widths[] = new int[256];
  190.     for (char ch = 0 ; ch < 256 ; ch++) {
  191.         widths[ch] = charWidth(ch);
  192.     }
  193.     return widths;
  194.     }
  195.  
  196.     /** 
  197.      * Returns the String representation of this FontMetric's values.
  198.      */
  199.     public String toString() {
  200.     return getClass().getName() + "[font=" + getFont() + "ascent=" +
  201.         getAscent() + ", descent=" + getDescent() + ", height=" + getHeight() + "]";
  202.     }
  203. }
  204.