001: /*
002: * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.awt.*;
029: import java.util.Hashtable;
030: import sun.awt.PlatformFont;
031:
032: /**
033: * A font metrics object for a WServer font.
034: *
035: * @version 1.34, 05/05/07
036: * @author Jim Graham
037: */
038: public class X11FontMetrics extends FontMetrics {
039: /**
040: * The widths of the first 256 characters.
041: */
042: int widths[];
043:
044: /**
045: * The standard ascent of the font. This is the logical height
046: * above the baseline for the Alphanumeric characters and should
047: * be used for determining line spacing. Note, however, that some
048: * characters in the font may extend above this height.
049: */
050: int ascent;
051:
052: /**
053: * The standard descent of the font. This is the logical height
054: * below the baseline for the Alphanumeric characters and should
055: * be used for determining line spacing. Note, however, that some
056: * characters in the font may extend below this height.
057: */
058: int descent;
059:
060: /**
061: * The standard leading for the font. This is the logical amount
062: * of space to be reserved between the descent of one line of text
063: * and the ascent of the next line. The height metric is calculated
064: * to include this extra space.
065: */
066: int leading;
067:
068: /**
069: * The standard height of a line of text in this font. This is
070: * the distance between the baseline of adjacent lines of text.
071: * It is the sum of the ascent+descent+leading. There is no
072: * guarantee that lines of text spaced at this distance will be
073: * disjoint; such lines may overlap if some characters overshoot
074: * the standard ascent and descent metrics.
075: */
076: int height;
077:
078: /**
079: * The maximum ascent for all characters in this font. No character
080: * will extend further above the baseline than this metric.
081: */
082: int maxAscent;
083:
084: /**
085: * The maximum descent for all characters in this font. No character
086: * will descend further below the baseline than this metric.
087: */
088: int maxDescent;
089:
090: /**
091: * The maximum possible height of a line of text in this font.
092: * Adjacent lines of text spaced this distance apart will be
093: * guaranteed not to overlap. Note, however, that many paragraphs
094: * that contain ordinary alphanumeric text may look too widely
095: * spaced if this metric is used to determine line spacing. The
096: * height field should be preferred unless the text in a given
097: * line contains particularly tall characters.
098: */
099: int maxHeight;
100:
101: /**
102: * The maximum advance width of any character in this font.
103: */
104: int maxAdvance;
105:
106: static {
107: initIDs();
108: }
109:
110: /**
111: * Initialize JNI field and method IDs for fields that may be
112: accessed from C.
113: */
114: private static native void initIDs();
115:
116: /**
117: * Calculate the metrics from the given WServer and font.
118: */
119: public X11FontMetrics(Font font) {
120: super (font);
121: init();
122: }
123:
124: /**
125: * Get leading
126: */
127: public int getLeading() {
128: return leading;
129: }
130:
131: /**
132: * Get ascent.
133: */
134: public int getAscent() {
135: return ascent;
136: }
137:
138: /**
139: * Get descent
140: */
141: public int getDescent() {
142: return descent;
143: }
144:
145: /**
146: * Get height
147: */
148: public int getHeight() {
149: return height;
150: }
151:
152: /**
153: * Get maxAscent
154: */
155: public int getMaxAscent() {
156: return maxAscent;
157: }
158:
159: /**
160: * Get maxDescent
161: */
162: public int getMaxDescent() {
163: return maxDescent;
164: }
165:
166: /**
167: * Get maxAdvance
168: */
169: public int getMaxAdvance() {
170: return maxAdvance;
171: }
172:
173: /**
174: * Return the width of the specified string in this Font.
175: */
176: public int stringWidth(String string) {
177: return charsWidth(string.toCharArray(), 0, string.length());
178: }
179:
180: /**
181: * Return the width of the specified char[] in this Font.
182: */
183: public int charsWidth(char chars[], int offset, int length) {
184: Font font = getFont();
185: PlatformFont pf = ((PlatformFont) font.getPeer());
186: if (pf.mightHaveMultiFontMetrics()) {
187: return getMFCharsWidth(chars, offset, length, font);
188: } else {
189: if (widths != null) {
190: int w = 0;
191: for (int i = offset; i < offset + length; i++) {
192: int ch = chars[i];
193: if (ch < 0 || ch >= widths.length) {
194: w += maxAdvance;
195: } else {
196: w += widths[ch];
197: }
198: }
199: return w;
200: } else {
201: return maxAdvance * length;
202: }
203: }
204: }
205:
206: private native int getMFCharsWidth(char chars[], int offset,
207: int length, Font font);
208:
209: /**
210: * Return the width of the specified byte[] in this Font.
211: */
212: public native int bytesWidth(byte data[], int off, int len);
213:
214: /**
215: * Get the widths of the first 256 characters in the font.
216: */
217: public int[] getWidths() {
218: return widths;
219: }
220:
221: native void init();
222:
223: static Hashtable table = new Hashtable();
224:
225: static synchronized FontMetrics getFontMetrics(Font font) {
226: FontMetrics fm = (FontMetrics) table.get(font);
227: if (fm == null) {
228: table.put(font, fm = new X11FontMetrics(font));
229: }
230: return fm;
231: }
232: }
|