J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1995-2004 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.awt.motif; |
| 27 | |
| 28 | import java.awt.*; |
| 29 | import java.util.Hashtable; |
| 30 | import sun.awt.PlatformFont; |
| 31 | |
| 32 | /** |
| 33 | * A font metrics object for a WServer font. |
| 34 | * |
| 35 | * @author Jim Graham |
| 36 | */ |
| 37 | public class X11FontMetrics extends FontMetrics { |
| 38 | /** |
| 39 | * The widths of the first 256 characters. |
| 40 | */ |
| 41 | int widths[]; |
| 42 | |
| 43 | /** |
| 44 | * The standard ascent of the font. This is the logical height |
| 45 | * above the baseline for the Alphanumeric characters and should |
| 46 | * be used for determining line spacing. Note, however, that some |
| 47 | * characters in the font may extend above this height. |
| 48 | */ |
| 49 | int ascent; |
| 50 | |
| 51 | /** |
| 52 | * The standard descent of the font. This is the logical height |
| 53 | * below the baseline for the Alphanumeric characters and should |
| 54 | * be used for determining line spacing. Note, however, that some |
| 55 | * characters in the font may extend below this height. |
| 56 | */ |
| 57 | int descent; |
| 58 | |
| 59 | /** |
| 60 | * The standard leading for the font. This is the logical amount |
| 61 | * of space to be reserved between the descent of one line of text |
| 62 | * and the ascent of the next line. The height metric is calculated |
| 63 | * to include this extra space. |
| 64 | */ |
| 65 | int leading; |
| 66 | |
| 67 | /** |
| 68 | * The standard height of a line of text in this font. This is |
| 69 | * the distance between the baseline of adjacent lines of text. |
| 70 | * It is the sum of the ascent+descent+leading. There is no |
| 71 | * guarantee that lines of text spaced at this distance will be |
| 72 | * disjoint; such lines may overlap if some characters overshoot |
| 73 | * the standard ascent and descent metrics. |
| 74 | */ |
| 75 | int height; |
| 76 | |
| 77 | /** |
| 78 | * The maximum ascent for all characters in this font. No character |
| 79 | * will extend further above the baseline than this metric. |
| 80 | */ |
| 81 | int maxAscent; |
| 82 | |
| 83 | /** |
| 84 | * The maximum descent for all characters in this font. No character |
| 85 | * will descend further below the baseline than this metric. |
| 86 | */ |
| 87 | int maxDescent; |
| 88 | |
| 89 | /** |
| 90 | * The maximum possible height of a line of text in this font. |
| 91 | * Adjacent lines of text spaced this distance apart will be |
| 92 | * guaranteed not to overlap. Note, however, that many paragraphs |
| 93 | * that contain ordinary alphanumeric text may look too widely |
| 94 | * spaced if this metric is used to determine line spacing. The |
| 95 | * height field should be preferred unless the text in a given |
| 96 | * line contains particularly tall characters. |
| 97 | */ |
| 98 | int maxHeight; |
| 99 | |
| 100 | /** |
| 101 | * The maximum advance width of any character in this font. |
| 102 | */ |
| 103 | int maxAdvance; |
| 104 | |
| 105 | static { |
| 106 | initIDs(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Initialize JNI field and method IDs for fields that may be |
| 111 | accessed from C. |
| 112 | */ |
| 113 | private static native void initIDs(); |
| 114 | |
| 115 | /** |
| 116 | * Calculate the metrics from the given WServer and font. |
| 117 | */ |
| 118 | public X11FontMetrics(Font font) { |
| 119 | super(font); |
| 120 | init(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get leading |
| 125 | */ |
| 126 | public int getLeading() { |
| 127 | return leading; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get ascent. |
| 132 | */ |
| 133 | public int getAscent() { |
| 134 | return ascent; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get descent |
| 139 | */ |
| 140 | public int getDescent() { |
| 141 | return descent; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get height |
| 146 | */ |
| 147 | public int getHeight() { |
| 148 | return height; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get maxAscent |
| 153 | */ |
| 154 | public int getMaxAscent() { |
| 155 | return maxAscent; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get maxDescent |
| 160 | */ |
| 161 | public int getMaxDescent() { |
| 162 | return maxDescent; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get maxAdvance |
| 167 | */ |
| 168 | public int getMaxAdvance() { |
| 169 | return maxAdvance; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return the width of the specified string in this Font. |
| 174 | */ |
| 175 | public int stringWidth(String string) { |
| 176 | return charsWidth(string.toCharArray(), 0, string.length()); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Return the width of the specified char[] in this Font. |
| 181 | */ |
| 182 | public int charsWidth(char chars[], int offset, int length) { |
| 183 | Font font = getFont(); |
| 184 | PlatformFont pf = ((PlatformFont) font.getPeer()); |
| 185 | if (pf.mightHaveMultiFontMetrics()) { |
| 186 | return getMFCharsWidth(chars, offset, length, font); |
| 187 | } else { |
| 188 | if (widths != null) { |
| 189 | int w = 0; |
| 190 | for (int i = offset; i < offset + length; i++) { |
| 191 | int ch = chars[i]; |
| 192 | if (ch < 0 || ch >= widths.length) { |
| 193 | w += maxAdvance; |
| 194 | } else { |
| 195 | w += widths[ch]; |
| 196 | } |
| 197 | } |
| 198 | return w; |
| 199 | } else { |
| 200 | return maxAdvance * length; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private native int getMFCharsWidth(char chars[], int offset, int length, Font font); |
| 206 | |
| 207 | /** |
| 208 | * Return the width of the specified byte[] in this Font. |
| 209 | */ |
| 210 | public native int bytesWidth(byte data[], int off, int len); |
| 211 | |
| 212 | /** |
| 213 | * Get the widths of the first 256 characters in the font. |
| 214 | */ |
| 215 | public int[] getWidths() { |
| 216 | return widths; |
| 217 | } |
| 218 | |
| 219 | native void init(); |
| 220 | |
| 221 | static Hashtable table = new Hashtable(); |
| 222 | |
| 223 | static synchronized FontMetrics getFontMetrics(Font font) { |
| 224 | FontMetrics fm = (FontMetrics)table.get(font); |
| 225 | if (fm == null) { |
| 226 | table.put(font, fm = new X11FontMetrics(font)); |
| 227 | } |
| 228 | return fm; |
| 229 | } |
| 230 | } |