summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/FontMetrics.java
blob: 90826265a7f15262f128e52d873a57702413593b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Ilya S. Okomin
 * @version $Revision$
 */

package java.awt;

import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.text.CharacterIterator;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 * The FontMetrics class contains information about the rendering of a
 * particular font on a particular screen.
 * <p>
 * Each character in the Font has three values that help define where to place
 * it: an ascent, a descent, and an advance. The ascent is the distance the
 * character extends above the baseline. The descent is the distance the
 * character extends below the baseline. The advance width defines the position
 * at which the next character should be placed.
 * <p>
 * An array of characters or a string has an ascent, a descent, and an advance
 * width too. The ascent or descent of the array is specified by the maximum
 * ascent or descent of the characters in the array. The advance width is the
 * sum of the advance widths of each of the characters in the character array.
 * </p>
 * 
 * @since Android 1.0
 */
public abstract class FontMetrics implements Serializable {

    /**
     * The Constant serialVersionUID.
     */
    private static final long serialVersionUID = 1681126225205050147L;

    /**
     * The font from which the FontMetrics is created.
     */
    protected Font font;

    /**
     * Instantiates a new font metrics from the specified Font.
     * 
     * @param fnt
     *            the Font.
     */
    protected FontMetrics(Font fnt) {
        this.font = fnt;
    }

    /**
     * Returns the String representation of this FontMetrics.
     * 
     * @return the string.
     */
    @Override
    public String toString() {
        return this.getClass().getName() + "[font=" + this.getFont() + //$NON-NLS-1$
                "ascent=" + this.getAscent() + //$NON-NLS-1$
                ", descent=" + this.getDescent() + //$NON-NLS-1$
                ", height=" + this.getHeight() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Gets the font associated with this FontMetrics.
     * 
     * @return the font associated with this FontMetrics.
     */
    public Font getFont() {
        return font;
    }

    /**
     * Gets the height of the text line in this Font.
     * 
     * @return the height of the text line in this Font.
     */
    public int getHeight() {
        return this.getAscent() + this.getDescent() + this.getLeading();
    }

    /**
     * Gets the font ascent of the Font associated with this FontMetrics. The
     * font ascent is the distance from the font's baseline to the top of most
     * alphanumeric characters.
     * 
     * @return the ascent of the Font associated with this FontMetrics.
     */
    public int getAscent() {
        return 0;
    }

    /**
     * Gets the font descent of the Font associated with this FontMetrics. The
     * font descent is the distance from the font's baseline to the bottom of
     * most alphanumeric characters with descenders.
     * 
     * @return the descent of the Font associated with this FontMetrics.
     */
    public int getDescent() {
        return 0;
    }

    /**
     * Gets the leading of the Font associated with this FontMetrics.
     * 
     * @return the leading of the Font associated with this FontMetrics.
     */
    public int getLeading() {
        return 0;
    }

    /**
     * Gets the LineMetrics object for the specified CharacterIterator in the
     * specified Graphics.
     * 
     * @param ci
     *            the CharacterIterator.
     * @param beginIndex
     *            the offset.
     * @param limit
     *            the number of characters to be used.
     * @param context
     *            the Graphics.
     * @return the LineMetrics object for the specified CharacterIterator in the
     *         specified Graphics.
     */
    public LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit,
            Graphics context) {
        return font.getLineMetrics(ci, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the LineMetrics object for the specified String in the specified
     * Graphics.
     * 
     * @param str
     *            the String.
     * @param context
     *            the Graphics.
     * @return the LineMetrics object for the specified String in the specified
     *         Graphics.
     */
    public LineMetrics getLineMetrics(String str, Graphics context) {
        return font.getLineMetrics(str, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the LineMetrics object for the specified character array in the
     * specified Graphics.
     * 
     * @param chars
     *            the character array.
     * @param beginIndex
     *            the offset of array.
     * @param limit
     *            the number of characters to be used.
     * @param context
     *            the Graphics.
     * @return the LineMetrics object for the specified character array in the
     *         specified Graphics.
     */
    public LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, Graphics context) {
        return font.getLineMetrics(chars, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the LineMetrics object for the specified String in the specified
     * Graphics.
     * 
     * @param str
     *            the String.
     * @param beginIndex
     *            the offset.
     * @param limit
     *            the number of characters to be used.
     * @param context
     *            the Graphics.
     * @return the LineMetrics object for the specified String in the specified
     *         Graphics.
     */
    public LineMetrics getLineMetrics(String str, int beginIndex, int limit, Graphics context) {
        return font.getLineMetrics(str, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Returns the character's maximum bounds in the specified Graphics context.
     * 
     * @param context
     *            the Graphics context.
     * @return the character's maximum bounds in the specified Graphics context.
     */
    public Rectangle2D getMaxCharBounds(Graphics context) {
        return this.font.getMaxCharBounds(this.getFRCFromGraphics(context));
    }

    /**
     * Gets the bounds of the specified CharacterIterator in the specified
     * Graphics context.
     * 
     * @param ci
     *            the CharacterIterator.
     * @param beginIndex
     *            the begin offset of the array.
     * @param limit
     *            the number of characters.
     * @param context
     *            the Graphics.
     * @return the bounds of the specified CharacterIterator in the specified
     *         Graphics context.
     */
    public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit,
            Graphics context) {
        return font.getStringBounds(ci, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the bounds of the specified String in the specified Graphics
     * context.
     * 
     * @param str
     *            the String.
     * @param beginIndex
     *            the begin offset of the array.
     * @param limit
     *            the number of characters.
     * @param context
     *            the Graphics.
     * @return the bounds of the specified String in the specified Graphics
     *         context.
     */
    public Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context) {
        return font.getStringBounds(str, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the bounds of the specified characters array in the specified
     * Graphics context.
     * 
     * @param chars
     *            the characters array.
     * @param beginIndex
     *            the begin offset of the array.
     * @param limit
     *            the number of characters.
     * @param context
     *            the Graphics.
     * @return the bounds of the specified characters array in the specified
     *         Graphics context.
     */
    public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context) {
        return font.getStringBounds(chars, beginIndex, limit, this.getFRCFromGraphics(context));
    }

    /**
     * Gets the bounds of the specified String in the specified Graphics
     * context.
     * 
     * @param str
     *            the String.
     * @param context
     *            the Graphics.
     * @return the bounds of the specified String in the specified Graphics
     *         context.
     */
    public Rectangle2D getStringBounds(String str, Graphics context) {
        return font.getStringBounds(str, this.getFRCFromGraphics(context));
    }

    /**
     * Checks if the Font has uniform line metrics or not. The Font can contain
     * characters of other fonts for covering character set. In this case the
     * Font isn't uniform.
     * 
     * @return true, if the Font has uniform line metrics, false otherwise.
     */
    public boolean hasUniformLineMetrics() {
        return this.font.hasUniformLineMetrics();
    }

    /**
     * Returns the distance from the leftmost point to the rightmost point on
     * the string's baseline showing the specified array of bytes in this Font.
     * 
     * @param data
     *            the array of bytes to be measured.
     * @param off
     *            the start offset.
     * @param len
     *            the number of bytes to be measured.
     * @return the advance width of the array.
     */
    public int bytesWidth(byte[] data, int off, int len) {
        int width = 0;
        if ((off >= data.length) || (off < 0)) {
            // awt.13B=offset off is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
        }

        if ((off + len > data.length)) {
            // awt.13C=number of elemets len is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
        }

        for (int i = off; i < off + len; i++) {
            width += charWidth(data[i]);
        }

        return width;
    }

    /**
     * Returns the distance from the leftmost point to the rightmost point on
     * the string's baseline showing the specified array of characters in this
     * Font.
     * 
     * @param data
     *            the array of characters to be measured.
     * @param off
     *            the start offset.
     * @param len
     *            the number of bytes to be measured.
     * @return the advance width of the array.
     */
    public int charsWidth(char[] data, int off, int len) {
        int width = 0;
        if ((off >= data.length) || (off < 0)) {
            // awt.13B=offset off is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13B")); //$NON-NLS-1$
        }

        if ((off + len > data.length)) {
            // awt.13C=number of elemets len is out of range
            throw new IllegalArgumentException(Messages.getString("awt.13C")); //$NON-NLS-1$
        }

        for (int i = off; i < off + len; i++) {
            width += charWidth(data[i]);
        }

        return width;
    }

    /**
     * Returns the distance from the leftmost point to the rightmost point of
     * the specified character in this Font.
     * 
     * @param ch
     *            the specified Unicode point code of character to be measured.
     * @return the advance width of the character.
     */
    public int charWidth(int ch) {
        return 0;
    }

    /**
     * Returns the distance from the leftmost point to the rightmost point of
     * the specified character in this Font.
     * 
     * @param ch
     *            the specified character to be measured.
     * @return the advance width of the character.
     */
    public int charWidth(char ch) {
        return 0;
    }

    /**
     * Gets the maximum advance width of character in this Font.
     * 
     * @return the maximum advance width of character in this Font.
     */
    public int getMaxAdvance() {
        return 0;
    }

    /**
     * Gets the maximum font ascent of the Font associated with this
     * FontMetrics.
     * 
     * @return the maximum font ascent of the Font associated with this
     *         FontMetrics.
     */
    public int getMaxAscent() {
        return 0;
    }

    /**
     * Gets the maximum font descent of character in this Font.
     * 
     * @return the maximum font descent of character in this Font.
     * @deprecated Replaced by getMaxDescent() method.
     */
    @Deprecated
    public int getMaxDecent() {
        return 0;
    }

    /**
     * Gets the maximum font descent of character in this Font.
     * 
     * @return the maximum font descent of character in this Font.
     */
    public int getMaxDescent() {
        return 0;
    }

    /**
     * Gets the advance widths of the characters in the Font.
     * 
     * @return the advance widths of the characters in the Font.
     */
    public int[] getWidths() {
        return null;
    }

    /**
     * Returns the advance width for the specified String in this Font.
     * 
     * @param str
     *            String to be measured.
     * @return the the advance width for the specified String in this Font.
     */
    public int stringWidth(String str) {
        return 0;
    }

    /**
     * Returns a FontRenderContext instance of the Graphics context specified.
     * 
     * @param context
     *            the specified Graphics context.
     * @return a FontRenderContext of the specified Graphics context.
     */
    private FontRenderContext getFRCFromGraphics(Graphics context) {
        FontRenderContext frc;
        if (context instanceof Graphics2D) {
            frc = ((Graphics2D)context).getFontRenderContext();
        } else {
            frc = new FontRenderContext(null, false, false);
        }

        return frc;
    }
}