home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / FontMetrics.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  2.7 KB  |  88 lines

  1. package java.awt;
  2.  
  3. public abstract class FontMetrics {
  4.    protected Font font;
  5.  
  6.    protected FontMetrics(Font font) {
  7.       this.font = font;
  8.    }
  9.  
  10.    public Font getFont() {
  11.       return this.font;
  12.    }
  13.  
  14.    public int getLeading() {
  15.       return 0;
  16.    }
  17.  
  18.    public int getAscent() {
  19.       return this.font.getSize();
  20.    }
  21.  
  22.    public int getDescent() {
  23.       return 0;
  24.    }
  25.  
  26.    public int getHeight() {
  27.       return this.getLeading() + this.getAscent() + this.getDescent();
  28.    }
  29.  
  30.    public int getMaxAscent() {
  31.       return this.getAscent();
  32.    }
  33.  
  34.    public int getMaxDescent() {
  35.       return this.getDescent();
  36.    }
  37.  
  38.    public int getMaxDecent() {
  39.       return this.getMaxDescent();
  40.    }
  41.  
  42.    public int getMaxAdvance() {
  43.       return -1;
  44.    }
  45.  
  46.    public int charWidth(int ch) {
  47.       return this.charWidth((char)ch);
  48.    }
  49.  
  50.    public int charWidth(char ch) {
  51.       if (ch < 256) {
  52.          return this.getWidths()[ch];
  53.       } else {
  54.          char[] data = new char[]{ch};
  55.          return this.charsWidth(data, 0, 1);
  56.       }
  57.    }
  58.  
  59.    public int stringWidth(String str) {
  60.       int len = str.length();
  61.       char[] data = new char[len];
  62.       str.getChars(0, len, data, 0);
  63.       return this.charsWidth(data, 0, len);
  64.    }
  65.  
  66.    public int charsWidth(char[] data, int off, int len) {
  67.       return this.stringWidth(new String(data, off, len));
  68.    }
  69.  
  70.    public int bytesWidth(byte[] data, int off, int len) {
  71.       return this.stringWidth(new String(data, 0, off, len));
  72.    }
  73.  
  74.    public int[] getWidths() {
  75.       int[] widths = new int[256];
  76.  
  77.       for(char ch = 0; ch < 256; ++ch) {
  78.          widths[ch] = this.charWidth(ch);
  79.       }
  80.  
  81.       return widths;
  82.    }
  83.  
  84.    public String toString() {
  85.       return this.getClass().getName() + "[font=" + this.getFont() + "ascent=" + this.getAscent() + ", descent=" + this.getDescent() + ", height=" + this.getHeight() + "]";
  86.    }
  87. }
  88.