1 /*
   2  * Copyright (c) 1996, 2014, 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 final 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     @Override
 121     public int getLeading() {
 122         return leading;
 123     }
 124 
 125     /**
 126      * Get ascent.
 127      */
 128     @Override
 129     public int getAscent() {
 130         return ascent;
 131     }
 132 
 133     /**
 134      * Get descent
 135      */
 136     @Override
 137     public int getDescent() {
 138         return descent;
 139     }
 140 
 141     /**
 142      * Get height
 143      */
 144     @Override
 145     public int getHeight() {
 146         return height;
 147     }
 148 
 149     /**
 150      * Get maxAscent
 151      */
 152     @Override
 153     public int getMaxAscent() {
 154         return maxAscent;
 155     }
 156 
 157     /**
 158      * Get maxDescent
 159      */
 160     @Override
 161     public int getMaxDescent() {
 162         return maxDescent;
 163     }
 164 
 165     /**
 166      * Get maxAdvance
 167      */
 168     @Override
 169     public int getMaxAdvance() {
 170         return maxAdvance;
 171     }
 172 
 173     /**
 174      * Return the width of the specified string in this Font.
 175      */
 176     @Override
 177     public native int stringWidth(String str);
 178 
 179     /**
 180      * Return the width of the specified char[] in this Font.
 181      */
 182     @Override
 183     public native int charsWidth(char data[], int off, int len);
 184 
 185     /**
 186      * Return the width of the specified byte[] in this Font.
 187      */
 188     @Override
 189     public native int bytesWidth(byte data[], int off, int len);
 190 
 191     /**
 192      * Get the widths of the first 256 characters in the font.
 193      */
 194     @Override
 195     public int[] getWidths() {
 196         return widths;
 197     }
 198 
 199     native void init();
 200 
 201     static Hashtable table = new Hashtable();
 202 
 203     static FontMetrics getFontMetrics(Font font) {
 204         FontMetrics fm = (FontMetrics)table.get(font);
 205         if (fm == null) {
 206             table.put(font, fm = new WFontMetrics(font));
 207         }
 208         return fm;
 209     }
 210 
 211     /**
 212      * Initialize JNI field and method IDs
 213      */
 214     private static native void initIDs();
 215 }