home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / fontmetr.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.3 KB  |  197 lines

  1. /*
  2.  * @(#)FontMetrics.java    1.7 95/08/17 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.7 08/17/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 getMaxDecent() {
  108.     return getDescent();
  109.     }
  110.  
  111.     /**
  112.      * Gets the maximum advance width of any character in this Font. 
  113.      * @return -1 if the max advance is not known.
  114.      */
  115.     public int getMaxAdvance() {
  116.     return -1;
  117.     }
  118.  
  119.     /** 
  120.      * Returns the width of the specified character in this Font.
  121.      * @param ch the specified font
  122.      * @see #stringWidth
  123.      */
  124.     public int charWidth(int ch) {
  125.     return charWidth((char)ch);
  126.     }
  127.  
  128.     /** 
  129.      * Returns the width of the specified character in this Font.
  130.      * @param ch the specified font
  131.      * @see #stringWidth
  132.      */
  133.     public int charWidth(char ch) {
  134.     if (ch < 256) {
  135.         return getWidths()[ch];
  136.     }
  137.     char data[] = {ch};
  138.     return charsWidth(data, 0, 1);
  139.     }
  140.  
  141.     /** 
  142.      * Returns the width of the specified String in this Font.
  143.      * @param str the String to be checked
  144.      * @see #charsWidth
  145.      * @see #bytesWidth
  146.      */
  147.     public int stringWidth(String str) {
  148.     int len = str.length();
  149.     char data[] = new char[len];
  150.     str.getChars(0, len, data, 0);
  151.     return charsWidth(data, 0, len);
  152.     }
  153.  
  154.     /** 
  155.      * Returns the width of the specified character array in this Font.
  156.      * @param data the data to be checked
  157.      * @param off the start offset of the data
  158.      * @param len the maximum number of bytes checked
  159.      * @see #stringWidth
  160.      * @see #bytesWidth
  161.      */
  162.     public int charsWidth(char data[], int off, int len) {
  163.     return stringWidth(new String(data, off, len));
  164.     }
  165.  
  166.     /** 
  167.      * Returns the width of the specified array of bytes in this Font.
  168.      * @param data the data to be checked
  169.      * @param off the start offset of the data
  170.      * @param len the maximum number of bytes checked
  171.      * @see #stringWidth
  172.      * @see #charsWidth
  173.      */
  174.     public int bytesWidth(byte data[], int off, int len) {
  175.     return stringWidth(new String(data, 0, off, len));
  176.     }
  177.  
  178.     /**
  179.      * Gets the widths of the first 256 characters in the Font.
  180.      */
  181.     public int[] getWidths() {
  182.     int widths[] = new int[256];
  183.     for (char ch = 0 ; ch < 256 ; ch++) {
  184.         widths[ch] = charWidth(ch);
  185.     }
  186.     return widths;
  187.     }
  188.  
  189.     /** 
  190.      * Returns the String representation of this FontMetric's values.
  191.      */
  192.     public String toString() {
  193.     return getClass().getName() + "[font=" + getFont() + "ascent=" +
  194.         getAscent() + ", descent=" + getDescent() + ", height=" + getHeight() + "]";
  195.     }
  196. }
  197.