1 /*
   2  * Copyright (c) 1996, 1997, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt.windows;
  27 
  28 import java.awt.*;
  29 import java.util.Hashtable;
  30 
  31 /**
  32  * A font metrics object for a WServer font.
  33  *
  34  * @author Jim Graham
  35  */
  36 class WFontMetrics extends FontMetrics {
  37 
  38     static {
  39         initIDs();
  40     }
  41 
  42     /**
  43      * The widths of the first 256 characters.
  44      */
  45     int widths[];
  46 
  47     /**
  48      * The standard ascent of the font.  This is the logical height
  49      * above the baseline for the Alphanumeric characters and should
  50      * be used for determining line spacing.  Note, however, that some
  51      * characters in the font may extend above this height.
  52      */
  53     int ascent;
  54 
  55     /**
  56      * The standard descent of the font.  This is the logical height
  57      * below the baseline for the Alphanumeric characters and should
  58      * be used for determining line spacing.  Note, however, that some
  59      * characters in the font may extend below this height.
  60      */
  61     int descent;
  62 
  63     /**
  64      * The standard leading for the font.  This is the logical amount
  65      * of space to be reserved between the descent of one line of text
  66      * and the ascent of the next line.  The height metric is calculated
  67      * to include this extra space.
  68      */
  69     int leading;
  70 
  71     /**
  72      * The standard height of a line of text in this font.  This is
  73      * the distance between the baseline of adjacent lines of text.
  74      * It is the sum of the ascent+descent+leading.  There is no
  75      * guarantee that lines of text spaced at this distance will be
  76      * disjoint; such lines may overlap if some characters overshoot
  77      * the standard ascent and descent metrics.
  78      */
  79     int height;
  80 
  81     /**
  82      * The maximum ascent for all characters in this font.  No character
  83      * will extend further above the baseline than this metric.
  84      */
  85     int maxAscent;
  86 
  87     /**
  88      * The maximum descent for all characters in this font.  No character
  89      * will descend further below the baseline than this metric.
  90      */
  91     int maxDescent;
  92 
  93     /**
  94      * The maximum possible height of a line of text in this font.
  95      * Adjacent lines of text spaced this distance apart will be
  96      * guaranteed not to overlap.  Note, however, that many paragraphs
  97      * that contain ordinary alphanumeric text may look too widely
  98      * spaced if this metric is used to determine line spacing.  The
  99      * height field should be preferred unless the text in a given
 100      * line contains particularly tall characters.
 101      */
 102     int maxHeight;
 103 
 104     /**
 105      * The maximum advance width of any character in this font.
 106      */
 107     int maxAdvance;
 108 
 109     /**
 110      * Calculate the metrics from the given WServer and font.
 111      */
 112     public WFontMetrics(Font font) {
 113         super(font);
 114         init();
 115     }
 116 
 117     /**
 118      * Get leading
 119      */
 120     public int getLeading() {
 121         return leading;
 122     }
 123 
 124     /**
 125      * Get ascent.
 126      */
 127     public int getAscent() {
 128         return ascent;
 129     }
 130 
 131     /**
 132      * Get descent
 133      */
 134     public int getDescent() {
 135         return descent;
 136     }
 137 
 138     /**
 139      * Get height
 140      */
 141     public int getHeight() {
 142         return height;
 143     }
 144 
 145     /**
 146      * Get maxAscent
 147      */
 148     public int getMaxAscent() {
 149         return maxAscent;
 150     }
 151 
 152     /**
 153      * Get maxDescent
 154      */
 155     public int getMaxDescent() {
 156         return maxDescent;
 157     }
 158 
 159     /**
 160      * Get maxAdvance
 161      */
 162     public int getMaxAdvance() {
 163         return maxAdvance;
 164     }
 165 
 166     /**
 167      * Return the width of the specified string in this Font.
 168      */
 169     public native int stringWidth(String str);
 170 
 171     /**
 172      * Return the width of the specified char[] in this Font.
 173      */
 174     public native int charsWidth(char data[], int off, int len);
 175 
 176     /**
 177      * Return the width of the specified byte[] in this Font.
 178      */
 179     public native int bytesWidth(byte data[], int off, int len);
 180 
 181     /**
 182      * Get the widths of the first 256 characters in the font.
 183      */
 184     public int[] getWidths() {
 185         return widths;
 186     }
 187 
 188     native void init();
 189 
 190     static Hashtable table = new Hashtable();
 191 
 192     static FontMetrics getFontMetrics(Font font) {
 193         FontMetrics fm = (FontMetrics)table.get(font);
 194         if (fm == null) {
 195             table.put(font, fm = new WFontMetrics(font));
 196         }
 197         return fm;
 198     }
 199 
 200     /**
 201      * Initialize JNI field and method IDs
 202      */
 203     private static native void initIDs();
 204 }