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 @SuppressWarnings("serial") // JDK-implementation class 37 final class WFontMetrics extends FontMetrics { 38 39 static { 40 initIDs(); 41 } 42 43 /** 44 * The widths of the first 256 characters. 45 */ 46 int widths[]; 47 48 /** 49 * The standard ascent of the font. This is the logical height 50 * above the baseline for the Alphanumeric characters and should 51 * be used for determining line spacing. Note, however, that some 52 * characters in the font may extend above this height. 53 */ 54 int ascent; 55 56 /** 57 * The standard descent of the font. This is the logical height 58 * below the baseline for the Alphanumeric characters and should 59 * be used for determining line spacing. Note, however, that some 60 * characters in the font may extend below this height. 61 */ 62 int descent; 63 64 /** 65 * The standard leading for the font. This is the logical amount 66 * of space to be reserved between the descent of one line of text 67 * and the ascent of the next line. The height metric is calculated 68 * to include this extra space. 69 */ 70 int leading; 71 72 /** 73 * The standard height of a line of text in this font. This is 74 * the distance between the baseline of adjacent lines of text. 75 * It is the sum of the ascent+descent+leading. There is no 76 * guarantee that lines of text spaced at this distance will be 77 * disjoint; such lines may overlap if some characters overshoot 78 * the standard ascent and descent metrics. 79 */ 80 int height; 81 82 /** 83 * The maximum ascent for all characters in this font. No character 84 * will extend further above the baseline than this metric. 85 */ 86 int maxAscent; 87 88 /** 89 * The maximum descent for all characters in this font. No character 90 * will descend further below the baseline than this metric. 91 */ 92 int maxDescent; 93 94 /** 95 * The maximum possible height of a line of text in this font. 96 * Adjacent lines of text spaced this distance apart will be 97 * guaranteed not to overlap. Note, however, that many paragraphs 98 * that contain ordinary alphanumeric text may look too widely 99 * spaced if this metric is used to determine line spacing. The 100 * height field should be preferred unless the text in a given 101 * line contains particularly tall characters. 102 */ 103 int maxHeight; 104 105 /** 106 * The maximum advance width of any character in this font. 107 */ 108 int maxAdvance; 109 110 /** 111 * Calculate the metrics from the given WServer and font. 112 */ 113 public WFontMetrics(Font font) { 114 super(font); 115 init(); 116 } 117 118 /** 119 * Get leading 120 */ 121 @Override 122 public int getLeading() { 123 return leading; 124 } 125 126 /** 127 * Get ascent. 128 */ 129 @Override 130 public int getAscent() { 131 return ascent; 132 } 133 134 /** 135 * Get descent 136 */ 137 @Override 138 public int getDescent() { 139 return descent; 140 } 141 142 /** 143 * Get height 144 */ 145 @Override 146 public int getHeight() { 147 return height; 148 } 149 150 /** 151 * Get maxAscent 152 */ 153 @Override 154 public int getMaxAscent() { 155 return maxAscent; 156 } 157 158 /** 159 * Get maxDescent 160 */ 161 @Override 162 public int getMaxDescent() { 163 return maxDescent; 164 } 165 166 /** 167 * Get maxAdvance 168 */ 169 @Override 170 public int getMaxAdvance() { 171 return maxAdvance; 172 } 173 174 /** 175 * Return the width of the specified string in this Font. 176 */ 177 @Override 178 public native int stringWidth(String str); 179 180 /** 181 * Return the width of the specified char[] in this Font. 182 */ 183 @Override 184 public native int charsWidth(char data[], int off, int len); 185 186 /** 187 * Return the width of the specified byte[] in this Font. 188 */ 189 @Override 190 public native int bytesWidth(byte data[], int off, int len); 191 192 /** 193 * Get the widths of the first 256 characters in the font. 194 */ 195 @Override 196 public int[] getWidths() { 197 return widths; 198 } 199 200 native void init(); 201 202 static Hashtable<Font, FontMetrics> table = new Hashtable<>(); 203 204 static FontMetrics getFontMetrics(Font font) { 205 FontMetrics fm = table.get(font); 206 if (fm == null) { 207 table.put(font, fm = new WFontMetrics(font)); 208 } 209 return fm; 210 } 211 212 /** 213 * Initialize JNI field and method IDs 214 */ 215 private static native void initIDs(); 216 }