• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 /**
18  * @author Ilya S. Okomin
19  * @version $Revision$
20  */
21 
22 package java.awt;
23 
24 import java.awt.font.FontRenderContext;
25 import java.awt.font.LineMetrics;
26 import java.awt.geom.Rectangle2D;
27 import java.io.Serializable;
28 import java.text.CharacterIterator;
29 
30 import org.apache.harmony.awt.internal.nls.Messages;
31 
32 /**
33  * The FontMetrics class contains information about the rendering of a
34  * particular font on a particular screen.
35  * <p>
36  * Each character in the Font has three values that help define where to place
37  * it: an ascent, a descent, and an advance. The ascent is the distance the
38  * character extends above the baseline. The descent is the distance the
39  * character extends below the baseline. The advance width defines the position
40  * at which the next character should be placed.
41  * <p>
42  * An array of characters or a string has an ascent, a descent, and an advance
43  * width too. The ascent or descent of the array is specified by the maximum
44  * ascent or descent of the characters in the array. The advance width is the
45  * sum of the advance widths of each of the characters in the character array.
46  * </p>
47  *
48  * @since Android 1.0
49  */
50 public abstract class FontMetrics implements Serializable {
51 
52     /**
53      * The Constant serialVersionUID.
54      */
55     private static final long serialVersionUID = 1681126225205050147L;
56 
57     /**
58      * The font from which the FontMetrics is created.
59      */
60     protected Font font;
61 
62     /**
63      * Instantiates a new font metrics from the specified Font.
64      *
65      * @param fnt
66      *            the Font.
67      */
FontMetrics(Font fnt)68     protected FontMetrics(Font fnt) {
69         this.font = fnt;
70     }
71 
72     /**
73      * Returns the String representation of this FontMetrics.
74      *
75      * @return the string.
76      */
77     @Override
toString()78     public String toString() {
79         return this.getClass().getName() + "[font=" + this.getFont() + //$NON-NLS-1$
80                 "ascent=" + this.getAscent() + //$NON-NLS-1$
81                 ", descent=" + this.getDescent() + //$NON-NLS-1$
82                 ", height=" + this.getHeight() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
83     }
84 
85     /**
86      * Gets the font associated with this FontMetrics.
87      *
88      * @return the font associated with this FontMetrics.
89      */
getFont()90     public Font getFont() {
91         return font;
92     }
93 
94     /**
95      * Gets the height of the text line in this Font.
96      *
97      * @return the height of the text line in this Font.
98      */
getHeight()99     public int getHeight() {
100         return this.getAscent() + this.getDescent() + this.getLeading();
101     }
102 
103     /**
104      * Gets the font ascent of the Font associated with this FontMetrics. The
105      * font ascent is the distance from the font's baseline to the top of most
106      * alphanumeric characters.
107      *
108      * @return the ascent of the Font associated with this FontMetrics.
109      */
getAscent()110     public int getAscent() {
111         return 0;
112     }
113 
114     /**
115      * Gets the font descent of the Font associated with this FontMetrics. The
116      * font descent is the distance from the font's baseline to the bottom of
117      * most alphanumeric characters with descenders.
118      *
119      * @return the descent of the Font associated with this FontMetrics.
120      */
getDescent()121     public int getDescent() {
122         return 0;
123     }
124 
125     /**
126      * Gets the leading of the Font associated with this FontMetrics.
127      *
128      * @return the leading of the Font associated with this FontMetrics.
129      */
getLeading()130     public int getLeading() {
131         return 0;
132     }
133 
134     /**
135      * Gets the LineMetrics object for the specified CharacterIterator in the
136      * specified Graphics.
137      *
138      * @param ci
139      *            the CharacterIterator.
140      * @param beginIndex
141      *            the offset.
142      * @param limit
143      *            the number of characters to be used.
144      * @param context
145      *            the Graphics.
146      * @return the LineMetrics object for the specified CharacterIterator in the
147      *         specified Graphics.
148      */
getLineMetrics(CharacterIterator ci, int beginIndex, int limit, Graphics context)149     public LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit,
150             Graphics context) {
151         return font.getLineMetrics(ci, beginIndex, limit, this.getFRCFromGraphics(context));
152     }
153 
154     /**
155      * Gets the LineMetrics object for the specified String in the specified
156      * Graphics.
157      *
158      * @param str
159      *            the String.
160      * @param context
161      *            the Graphics.
162      * @return the LineMetrics object for the specified String in the specified
163      *         Graphics.
164      */
getLineMetrics(String str, Graphics context)165     public LineMetrics getLineMetrics(String str, Graphics context) {
166         return font.getLineMetrics(str, this.getFRCFromGraphics(context));
167     }
168 
169     /**
170      * Gets the LineMetrics object for the specified character array in the
171      * specified Graphics.
172      *
173      * @param chars
174      *            the character array.
175      * @param beginIndex
176      *            the offset of array.
177      * @param limit
178      *            the number of characters to be used.
179      * @param context
180      *            the Graphics.
181      * @return the LineMetrics object for the specified character array in the
182      *         specified Graphics.
183      */
getLineMetrics(char[] chars, int beginIndex, int limit, Graphics context)184     public LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, Graphics context) {
185         return font.getLineMetrics(chars, beginIndex, limit, this.getFRCFromGraphics(context));
186     }
187 
188     /**
189      * Gets the LineMetrics object for the specified String in the specified
190      * Graphics.
191      *
192      * @param str
193      *            the String.
194      * @param beginIndex
195      *            the offset.
196      * @param limit
197      *            the number of characters to be used.
198      * @param context
199      *            the Graphics.
200      * @return the LineMetrics object for the specified String in the specified
201      *         Graphics.
202      */
getLineMetrics(String str, int beginIndex, int limit, Graphics context)203     public LineMetrics getLineMetrics(String str, int beginIndex, int limit, Graphics context) {
204         return font.getLineMetrics(str, beginIndex, limit, this.getFRCFromGraphics(context));
205     }
206 
207     /**
208      * Returns the character's maximum bounds in the specified Graphics context.
209      *
210      * @param context
211      *            the Graphics context.
212      * @return the character's maximum bounds in the specified Graphics context.
213      */
getMaxCharBounds(Graphics context)214     public Rectangle2D getMaxCharBounds(Graphics context) {
215         return this.font.getMaxCharBounds(this.getFRCFromGraphics(context));
216     }
217 
218     /**
219      * Gets the bounds of the specified CharacterIterator in the specified
220      * Graphics context.
221      *
222      * @param ci
223      *            the CharacterIterator.
224      * @param beginIndex
225      *            the begin offset of the array.
226      * @param limit
227      *            the number of characters.
228      * @param context
229      *            the Graphics.
230      * @return the bounds of the specified CharacterIterator in the specified
231      *         Graphics context.
232      */
getStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context)233     public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit,
234             Graphics context) {
235         return font.getStringBounds(ci, beginIndex, limit, this.getFRCFromGraphics(context));
236     }
237 
238     /**
239      * Gets the bounds of the specified String in the specified Graphics
240      * context.
241      *
242      * @param str
243      *            the String.
244      * @param beginIndex
245      *            the begin offset of the array.
246      * @param limit
247      *            the number of characters.
248      * @param context
249      *            the Graphics.
250      * @return the bounds of the specified String in the specified Graphics
251      *         context.
252      */
getStringBounds(String str, int beginIndex, int limit, Graphics context)253     public Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context) {
254         return font.getStringBounds(str, beginIndex, limit, this.getFRCFromGraphics(context));
255     }
256 
257     /**
258      * Gets the bounds of the specified characters array in the specified
259      * Graphics context.
260      *
261      * @param chars
262      *            the characters array.
263      * @param beginIndex
264      *            the begin offset of the array.
265      * @param limit
266      *            the number of characters.
267      * @param context
268      *            the Graphics.
269      * @return the bounds of the specified characters array in the specified
270      *         Graphics context.
271      */
getStringBounds(char[] chars, int beginIndex, int limit, Graphics context)272     public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context) {
273         return font.getStringBounds(chars, beginIndex, limit, this.getFRCFromGraphics(context));
274     }
275 
276     /**
277      * Gets the bounds of the specified String in the specified Graphics
278      * context.
279      *
280      * @param str
281      *            the String.
282      * @param context
283      *            the Graphics.
284      * @return the bounds of the specified String in the specified Graphics
285      *         context.
286      */
getStringBounds(String str, Graphics context)287     public Rectangle2D getStringBounds(String str, Graphics context) {
288         return font.getStringBounds(str, this.getFRCFromGraphics(context));
289     }
290 
291     /**
292      * Checks if the Font has uniform line metrics or not. The Font can contain
293      * characters of other fonts for covering character set. In this case the
294      * Font isn't uniform.
295      *
296      * @return true, if the Font has uniform line metrics, false otherwise.
297      */
hasUniformLineMetrics()298     public boolean hasUniformLineMetrics() {
299         return this.font.hasUniformLineMetrics();
300     }
301 
302     /**
303      * Returns the distance from the leftmost point to the rightmost point on
304      * the string's baseline showing the specified array of bytes in this Font.
305      *
306      * @param data
307      *            the array of bytes to be measured.
308      * @param off
309      *            the start offset.
310      * @param len
311      *            the number of bytes to be measured.
312      * @return the advance width of the array.
313      */
bytesWidth(byte[] data, int off, int len)314     public int bytesWidth(byte[] data, int off, int len) {
315         int width = 0;
316         if ((off >= data.length) || (off < 0)) {
317             // awt.13B=offset off is out of range
318             throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
319         }
320 
321         if ((off + len > data.length)) {
322             // awt.13C=number of elemets len is out of range
323             throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
324         }
325 
326         for (int i = off; i < off + len; i++) {
327             width += charWidth(data[i]);
328         }
329 
330         return width;
331     }
332 
333     /**
334      * Returns the distance from the leftmost point to the rightmost point on
335      * the string's baseline showing the specified array of characters in this
336      * Font.
337      *
338      * @param data
339      *            the array of characters to be measured.
340      * @param off
341      *            the start offset.
342      * @param len
343      *            the number of bytes to be measured.
344      * @return the advance width of the array.
345      */
charsWidth(char[] data, int off, int len)346     public int charsWidth(char[] data, int off, int len) {
347         int width = 0;
348         if ((off >= data.length) || (off < 0)) {
349             // awt.13B=offset off is out of range
350             throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
351         }
352 
353         if ((off + len > data.length)) {
354             // awt.13C=number of elemets len is out of range
355             throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
356         }
357 
358         for (int i = off; i < off + len; i++) {
359             width += charWidth(data[i]);
360         }
361 
362         return width;
363     }
364 
365     /**
366      * Returns the distance from the leftmost point to the rightmost point of
367      * the specified character in this Font.
368      *
369      * @param ch
370      *            the specified Unicode point code of character to be measured.
371      * @return the advance width of the character.
372      */
charWidth(int ch)373     public int charWidth(int ch) {
374         return 0;
375     }
376 
377     /**
378      * Returns the distance from the leftmost point to the rightmost point of
379      * the specified character in this Font.
380      *
381      * @param ch
382      *            the specified character to be measured.
383      * @return the advance width of the character.
384      */
charWidth(char ch)385     public int charWidth(char ch) {
386         return 0;
387     }
388 
389     /**
390      * Gets the maximum advance width of character in this Font.
391      *
392      * @return the maximum advance width of character in this Font.
393      */
getMaxAdvance()394     public int getMaxAdvance() {
395         return 0;
396     }
397 
398     /**
399      * Gets the maximum font ascent of the Font associated with this
400      * FontMetrics.
401      *
402      * @return the maximum font ascent of the Font associated with this
403      *         FontMetrics.
404      */
getMaxAscent()405     public int getMaxAscent() {
406         return 0;
407     }
408 
409     /**
410      * Gets the maximum font descent of character in this Font.
411      *
412      * @return the maximum font descent of character in this Font.
413      * @deprecated Replaced by getMaxDescent() method.
414      */
415     @Deprecated
getMaxDecent()416     public int getMaxDecent() {
417         return 0;
418     }
419 
420     /**
421      * Gets the maximum font descent of character in this Font.
422      *
423      * @return the maximum font descent of character in this Font.
424      */
getMaxDescent()425     public int getMaxDescent() {
426         return 0;
427     }
428 
429     /**
430      * Gets the advance widths of the characters in the Font.
431      *
432      * @return the advance widths of the characters in the Font.
433      */
getWidths()434     public int[] getWidths() {
435         return null;
436     }
437 
438     /**
439      * Returns the advance width for the specified String in this Font.
440      *
441      * @param str
442      *            String to be measured.
443      * @return the the advance width for the specified String in this Font.
444      */
stringWidth(String str)445     public int stringWidth(String str) {
446         return 0;
447     }
448 
449     /**
450      * Returns a FontRenderContext instance of the Graphics context specified.
451      *
452      * @param context
453      *            the specified Graphics context.
454      * @return a FontRenderContext of the specified Graphics context.
455      */
getFRCFromGraphics(Graphics context)456     private FontRenderContext getFRCFromGraphics(Graphics context) {
457         FontRenderContext frc;
458         if (context instanceof Graphics2D) {
459             frc = ((Graphics2D)context).getFontRenderContext();
460         } else {
461             frc = new FontRenderContext(null, false, false);
462         }
463 
464         return frc;
465     }
466 }
467