Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/FontMetrics.java
41152 views
1
/*
2
* Copyright (c) 1995, 2021, 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 java.awt;
27
28
import java.awt.font.FontRenderContext;
29
import java.awt.font.LineMetrics;
30
import java.awt.geom.Rectangle2D;
31
import java.io.Serial;
32
import java.text.CharacterIterator;
33
34
/**
35
* The {@code FontMetrics} class defines a font metrics object, which
36
* encapsulates information about the rendering of a particular font on a
37
* particular screen.
38
* <p>
39
* <b>Note to subclassers</b>: Since many of these methods form closed,
40
* mutually recursive loops, you must take care that you implement
41
* at least one of the methods in each such loop to prevent
42
* infinite recursion when your subclass is used.
43
* In particular, the following is the minimal suggested set of methods
44
* to override in order to ensure correctness and prevent infinite
45
* recursion (though other subsets are equally feasible):
46
* <ul>
47
* <li>{@link #getAscent()}
48
* <li>{@link #getLeading()}
49
* <li>{@link #getMaxAdvance()}
50
* <li>{@link #charWidth(char)}
51
* <li>{@link #charsWidth(char[], int, int)}
52
* </ul>
53
* <p>
54
* <img src="doc-files/FontMetrics-1.gif" alt="The letter 'p' showing its 'reference point'"
55
* style="border:15px; float:right; margin: 7px 10px;">
56
* Note that the implementations of these methods are
57
* inefficient, so they are usually overridden with more efficient
58
* toolkit-specific implementations.
59
* <p>
60
* When an application asks to place a character at the position
61
* (<i>x</i>,&nbsp;<i>y</i>), the character is placed so that its
62
* reference point (shown as the dot in the accompanying image) is
63
* put at that position. The reference point specifies a horizontal
64
* line called the <i>baseline</i> of the character. In normal
65
* printing, the baselines of characters should align.
66
* <p>
67
* In addition, every character in a font has an <i>ascent</i>, a
68
* <i>descent</i>, and an <i>advance width</i>. The ascent is the
69
* amount by which the character ascends above the baseline. The
70
* descent is the amount by which the character descends below the
71
* baseline. The advance width indicates the position at which AWT
72
* should place the next character.
73
* <p>
74
* An array of characters or a string can also have an ascent, a
75
* descent, and an advance width. The ascent of the array is the
76
* maximum ascent of any character in the array. The descent is the
77
* maximum descent of any character in the array. The advance width
78
* is the sum of the advance widths of each of the characters in the
79
* character array. The advance of a {@code String} is the
80
* distance along the baseline of the {@code String}. This
81
* distance is the width that should be used for centering or
82
* right-aligning the {@code String}.
83
* <p>Note that the advance of a {@code String} is not necessarily
84
* the sum of the advances of its characters measured in isolation
85
* because the width of a character can vary depending on its context.
86
* For example, in Arabic text, the shape of a character can change
87
* in order to connect to other characters. Also, in some scripts,
88
* certain character sequences can be represented by a single shape,
89
* called a <em>ligature</em>. Measuring characters individually does
90
* not account for these transformations.
91
* <p>Font metrics are baseline-relative, meaning that they are
92
* generally independent of the rotation applied to the font (modulo
93
* possible grid hinting effects). See {@link java.awt.Font Font}.
94
*
95
* @author Jim Graham
96
* @see java.awt.Font
97
* @since 1.0
98
*/
99
public abstract class FontMetrics implements java.io.Serializable {
100
101
static {
102
/* ensure that the necessary native libraries are loaded */
103
Toolkit.loadLibraries();
104
if (!GraphicsEnvironment.isHeadless()) {
105
initIDs();
106
}
107
}
108
109
private static final FontRenderContext
110
DEFAULT_FRC = new FontRenderContext(null, false, false);
111
112
/**
113
* The actual {@link Font} from which the font metrics are
114
* created.
115
* This cannot be null.
116
*
117
* @serial
118
* @see #getFont()
119
*/
120
protected Font font;
121
122
/**
123
* Use serialVersionUID from JDK 1.1 for interoperability.
124
*/
125
@Serial
126
private static final long serialVersionUID = 1681126225205050147L;
127
128
/**
129
* Creates a new {@code FontMetrics} object for finding out
130
* height and width information about the specified {@code Font}
131
* and specific character glyphs in that {@code Font}.
132
* @param font the {@code Font}
133
* @see java.awt.Font
134
*/
135
protected FontMetrics(Font font) {
136
this.font = font;
137
}
138
139
/**
140
* Gets the {@code Font} described by this
141
* {@code FontMetrics} object.
142
* @return the {@code Font} described by this
143
* {@code FontMetrics} object.
144
*/
145
public Font getFont() {
146
return font;
147
}
148
149
/**
150
* Gets the {@code FontRenderContext} used by this
151
* {@code FontMetrics} object to measure text.
152
* <p>
153
* Note that methods in this class which take a {@code Graphics}
154
* parameter measure text using the {@code FontRenderContext}
155
* of that {@code Graphics} object, and not this
156
* {@code FontRenderContext}
157
* @return the {@code FontRenderContext} used by this
158
* {@code FontMetrics} object.
159
* @since 1.6
160
*/
161
public FontRenderContext getFontRenderContext() {
162
return DEFAULT_FRC;
163
}
164
165
/**
166
* Determines the <em>standard leading</em> of the
167
* {@code Font} described by this {@code FontMetrics}
168
* object. The standard leading, or
169
* interline spacing, is the logical amount of space to be reserved
170
* between the descent of one line of text and the ascent of the next
171
* line. The height metric is calculated to include this extra space.
172
* @return the standard leading of the {@code Font}.
173
* @see #getHeight()
174
* @see #getAscent()
175
* @see #getDescent()
176
*/
177
public int getLeading() {
178
return 0;
179
}
180
181
/**
182
* Determines the <em>font ascent</em> of the {@code Font}
183
* described by this {@code FontMetrics} object. The font ascent
184
* is the distance from the font's baseline to the top of most
185
* alphanumeric characters. Some characters in the {@code Font}
186
* might extend above the font ascent line.
187
* @return the font ascent of the {@code Font}.
188
* @see #getMaxAscent()
189
*/
190
public int getAscent() {
191
return font.getSize();
192
}
193
194
/**
195
* Determines the <em>font descent</em> of the {@code Font}
196
* described by this
197
* {@code FontMetrics} object. The font descent is the distance
198
* from the font's baseline to the bottom of most alphanumeric
199
* characters with descenders. Some characters in the
200
* {@code Font} might extend
201
* below the font descent line.
202
* @return the font descent of the {@code Font}.
203
* @see #getMaxDescent()
204
*/
205
public int getDescent() {
206
return 0;
207
}
208
209
/**
210
* Gets the standard height of a line of text in this font. This
211
* is the distance between the baseline of adjacent lines of text.
212
* It is the sum of the leading + ascent + descent. Due to rounding
213
* this may not be the same as getAscent() + getDescent() + getLeading().
214
* There is no guarantee that lines of text spaced at this distance are
215
* disjoint; such lines may overlap if some characters overshoot
216
* either the standard ascent or the standard descent metric.
217
* @return the standard height of the font.
218
* @see #getLeading()
219
* @see #getAscent()
220
* @see #getDescent()
221
*/
222
public int getHeight() {
223
return getLeading() + getAscent() + getDescent();
224
}
225
226
/**
227
* Determines the maximum ascent of the {@code Font}
228
* described by this {@code FontMetrics} object. No character
229
* extends further above the font's baseline than this height.
230
* @return the maximum ascent of any character in the
231
* {@code Font}.
232
* @see #getAscent()
233
*/
234
public int getMaxAscent() {
235
return getAscent();
236
}
237
238
/**
239
* Determines the maximum descent of the {@code Font}
240
* described by this {@code FontMetrics} object. No character
241
* extends further below the font's baseline than this height.
242
* @return the maximum descent of any character in the
243
* {@code Font}.
244
* @see #getDescent()
245
*/
246
public int getMaxDescent() {
247
return getDescent();
248
}
249
250
/**
251
* For backward compatibility only.
252
* @return the maximum descent of any character in the
253
* {@code Font}.
254
* @see #getMaxDescent()
255
* @deprecated As of JDK version 1.1.1,
256
* replaced by {@code getMaxDescent()}.
257
*/
258
@Deprecated
259
public int getMaxDecent() {
260
return getMaxDescent();
261
}
262
263
/**
264
* Returns an estimate of the maximum advance width of any character
265
* in the {@code Font} described by this {@code FontMetrics} object,
266
* with important caveats, enumerated below.
267
* <p>
268
* The advance is the distance from the leftmost point used to position
269
* the character to the rightmost point along the baseline.
270
* This is not the same thing as the visible width of the glyph image
271
* representing the character.
272
* <p>
273
* The advance of a {@code String} is not necessarily the sum of the
274
* advances of its characters. It may differ substantially if
275
* complex text layout is required for proper rendering.
276
* <p>
277
* Some of the caveats of the reported value include
278
* <ul>
279
* <li> The returned value is relying upon information from some
280
* underlying system font, and the correctness of that information
281
* is outside of AWT's control.
282
* <li> When specific characters are mapped into glyphs
283
* in some rendering context, instructions in the font itself
284
* together with the rasterization process may cause some glyph
285
* to have a wider advance than reported.
286
* <li> When a font is requested in some style, eg {@code Font.BOLD},
287
* for which no exact match is available, then techniques to satisfy
288
* the requested rendering may similarly result in glyphs that are
289
* wider than the reported maximum.
290
* <li> Depending on the implementation, an AWT logical font or
291
* physical font may need to locate some characters from one or more
292
* "fall back" fonts, when the primary underlying physical font does not
293
* support the character. These fonts may not all be known or considered
294
* in the calculation of the reported maximum advance. It is common
295
* for the design center of such fall back fonts to be for a different
296
* script than the design center of the primary font, so their
297
* advances can be quite different. This can also lead to the
298
* unexpected result that a font such as {@code Font.MONOSPACED} can
299
* render glyphs that are not all the same width.
300
* </ul>
301
* None of these caveats are exposed as they are all implementation details,
302
* and there is no practical way to determine when these are in effect.
303
* An application which needs a better estimate of the maximum advance,
304
* and knows the subset of characters it expects to display can query
305
* the advance of each such character to find the widest, however,
306
* as discussed above, since the displayed width of a {@code String}
307
* is not necessarily the sum of the advances the value still needs
308
* to be used with caution.
309
* <p>
310
* In summary, this method makes no absolute guarantee, nor can
311
* it even make a guarantee to be correct within some margin of error.
312
* So it should be used at most only for estimating the total space
313
* sufficient to display some number of as yet unknown characters from
314
* the font. And that might be either an overestimate, or an
315
* underestimate depending on the specific text and rendering conext.
316
* @return an estimate of the maximum advance width of any character
317
* in the {@code Font}, or {@code -1} if the
318
* maximum advance width is not known.
319
*/
320
public int getMaxAdvance() {
321
return -1;
322
}
323
324
/**
325
* Returns the advance width of the specified character in this
326
* {@code Font}. The advance is the
327
* distance from the leftmost point to the rightmost point on the
328
* character's baseline. Note that the advance of a
329
* {@code String} is not necessarily the sum of the advances
330
* of its characters.
331
*
332
* <p>This method doesn't validate the specified character to be a
333
* valid Unicode code point. The caller must validate the
334
* character value using {@link
335
* java.lang.Character#isValidCodePoint(int)
336
* Character.isValidCodePoint} if necessary.
337
*
338
* @param codePoint the character (Unicode code point) to be measured
339
* @return the advance width of the specified character
340
* in the {@code Font} described by this
341
* {@code FontMetrics} object.
342
* @see #charsWidth(char[], int, int)
343
* @see #stringWidth(String)
344
*/
345
public int charWidth(int codePoint) {
346
if (!Character.isValidCodePoint(codePoint)) {
347
codePoint = 0xffff; // substitute missing glyph width
348
}
349
350
if (codePoint < 256) {
351
return getWidths()[codePoint];
352
} else {
353
char[] buffer = new char[2];
354
int len = Character.toChars(codePoint, buffer, 0);
355
return charsWidth(buffer, 0, len);
356
}
357
}
358
359
/**
360
* Returns the advance width of the specified character in this
361
* {@code Font}. The advance is the
362
* distance from the leftmost point to the rightmost point on the
363
* character's baseline. Note that the advance of a
364
* {@code String} is not necessarily the sum of the advances
365
* of its characters.
366
*
367
* <p><b>Note:</b> This method cannot handle <a
368
* href="../../../java.base/java/lang/Character.html#supplementary">
369
* supplementary characters</a>.
370
* To support all Unicode characters, including
371
* supplementary characters, use the {@link #charWidth(int)} method.
372
*
373
* @param ch the character to be measured
374
* @return the advance width of the specified character
375
* in the {@code Font} described by this
376
* {@code FontMetrics} object.
377
* @see #charsWidth(char[], int, int)
378
* @see #stringWidth(String)
379
*/
380
public int charWidth(char ch) {
381
if (ch < 256) {
382
return getWidths()[ch];
383
}
384
char[] data = {ch};
385
return charsWidth(data, 0, 1);
386
}
387
388
/**
389
* Returns the total advance width for showing the specified
390
* {@code String} in this {@code Font}. The advance
391
* is the distance from the leftmost point to the rightmost point
392
* on the string's baseline.
393
* <p>
394
* Note that the advance of a {@code String} is
395
* not necessarily the sum of the advances of its characters.
396
* @param str the {@code String} to be measured
397
* @return the advance width of the specified {@code String}
398
* in the {@code Font} described by this
399
* {@code FontMetrics}.
400
* @throws NullPointerException if str is null.
401
* @see #bytesWidth(byte[], int, int)
402
* @see #charsWidth(char[], int, int)
403
* @see #getStringBounds(String, Graphics)
404
*/
405
public int stringWidth(String str) {
406
int len = str.length();
407
char[] data = new char[len];
408
str.getChars(0, len, data, 0);
409
return charsWidth(data, 0, len);
410
}
411
412
/**
413
* Returns the total advance width for showing the specified array
414
* of characters in this {@code Font}. The advance is the
415
* distance from the leftmost point to the rightmost point on the
416
* string's baseline. The advance of a {@code String}
417
* is not necessarily the sum of the advances of its characters.
418
* This is equivalent to measuring a {@code String} of the
419
* characters in the specified range.
420
* @param data the array of characters to be measured
421
* @param off the start offset of the characters in the array
422
* @param len the number of characters to be measured from the array
423
* @return the advance width of the subarray of the specified
424
* {@code char} array in the font described by
425
* this {@code FontMetrics} object.
426
* @throws NullPointerException if {@code data} is null.
427
* @throws IndexOutOfBoundsException if the {@code off}
428
* and {@code len} arguments index characters outside
429
* the bounds of the {@code data} array.
430
* @see #charWidth(int)
431
* @see #charWidth(char)
432
* @see #bytesWidth(byte[], int, int)
433
* @see #stringWidth(String)
434
*/
435
public int charsWidth(char[] data, int off, int len) {
436
return stringWidth(new String(data, off, len));
437
}
438
439
/**
440
* Returns the total advance width for showing the specified array
441
* of bytes in this {@code Font}. The advance is the
442
* distance from the leftmost point to the rightmost point on the
443
* string's baseline. The advance of a {@code String}
444
* is not necessarily the sum of the advances of its characters.
445
* This is equivalent to measuring a {@code String} of the
446
* characters in the specified range.
447
* @param data the array of bytes to be measured
448
* @param off the start offset of the bytes in the array
449
* @param len the number of bytes to be measured from the array
450
* @return the advance width of the subarray of the specified
451
* {@code byte} array in the {@code Font}
452
* described by
453
* this {@code FontMetrics} object.
454
* @throws NullPointerException if {@code data} is null.
455
* @throws IndexOutOfBoundsException if the {@code off}
456
* and {@code len} arguments index bytes outside
457
* the bounds of the {@code data} array.
458
* @see #charsWidth(char[], int, int)
459
* @see #stringWidth(String)
460
*/
461
@SuppressWarnings("deprecation")
462
public int bytesWidth(byte[] data, int off, int len) {
463
return stringWidth(new String(data, 0, off, len));
464
}
465
466
/**
467
* Gets the advance widths of the first 256 characters in the
468
* {@code Font}. The advance is the
469
* distance from the leftmost point to the rightmost point on the
470
* character's baseline. Note that the advance of a
471
* {@code String} is not necessarily the sum of the advances
472
* of its characters.
473
* @return an array storing the advance widths of the
474
* characters in the {@code Font}
475
* described by this {@code FontMetrics} object.
476
*/
477
public int[] getWidths() {
478
int[] widths = new int[256];
479
for (char ch = 0 ; ch < 256 ; ch++) {
480
widths[ch] = charWidth(ch);
481
}
482
return widths;
483
}
484
485
/**
486
* Checks to see if the {@code Font} has uniform line metrics. A
487
* composite font may consist of several different fonts to cover
488
* various character sets. In such cases, the
489
* {@code FontLineMetrics} objects are not uniform.
490
* Different fonts may have a different ascent, descent, metrics and
491
* so on. This information is sometimes necessary for line
492
* measuring and line breaking.
493
* @return {@code true} if the font has uniform line metrics;
494
* {@code false} otherwise.
495
* @see java.awt.Font#hasUniformLineMetrics()
496
*/
497
public boolean hasUniformLineMetrics() {
498
return font.hasUniformLineMetrics();
499
}
500
501
/**
502
* Returns the {@link LineMetrics} object for the specified
503
* {@code String} in the specified {@link Graphics} context.
504
* @param str the specified {@code String}
505
* @param context the specified {@code Graphics} context
506
* @return a {@code LineMetrics} object created with the
507
* specified {@code String} and {@code Graphics} context.
508
* @see java.awt.Font#getLineMetrics(String, FontRenderContext)
509
*/
510
public LineMetrics getLineMetrics( String str, Graphics context) {
511
return font.getLineMetrics(str, myFRC(context));
512
}
513
514
/**
515
* Returns the {@link LineMetrics} object for the specified
516
* {@code String} in the specified {@link Graphics} context.
517
* @param str the specified {@code String}
518
* @param beginIndex the initial offset of {@code str}
519
* @param limit the end offset of {@code str}
520
* @param context the specified {@code Graphics} context
521
* @return a {@code LineMetrics} object created with the
522
* specified {@code String} and {@code Graphics} context.
523
* @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)
524
*/
525
public LineMetrics getLineMetrics( String str,
526
int beginIndex, int limit,
527
Graphics context) {
528
return font.getLineMetrics(str, beginIndex, limit, myFRC(context));
529
}
530
531
/**
532
* Returns the {@link LineMetrics} object for the specified
533
* character array in the specified {@link Graphics} context.
534
* @param chars the specified character array
535
* @param beginIndex the initial offset of {@code chars}
536
* @param limit the end offset of {@code chars}
537
* @param context the specified {@code Graphics} context
538
* @return a {@code LineMetrics} object created with the
539
* specified character array and {@code Graphics} context.
540
* @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)
541
*/
542
public LineMetrics getLineMetrics(char [] chars,
543
int beginIndex, int limit,
544
Graphics context) {
545
return font.getLineMetrics(
546
chars, beginIndex, limit, myFRC(context));
547
}
548
549
/**
550
* Returns the {@link LineMetrics} object for the specified
551
* {@link CharacterIterator} in the specified {@link Graphics}
552
* context.
553
* @param ci the specified {@code CharacterIterator}
554
* @param beginIndex the initial offset in {@code ci}
555
* @param limit the end index of {@code ci}
556
* @param context the specified {@code Graphics} context
557
* @return a {@code LineMetrics} object created with the
558
* specified arguments.
559
* @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)
560
*/
561
public LineMetrics getLineMetrics(CharacterIterator ci,
562
int beginIndex, int limit,
563
Graphics context) {
564
return font.getLineMetrics(ci, beginIndex, limit, myFRC(context));
565
}
566
567
/**
568
* Returns the bounds of the specified {@code String} in the
569
* specified {@code Graphics} context. The bounds is used
570
* to layout the {@code String}.
571
* <p>Note: The returned bounds is in baseline-relative coordinates
572
* (see {@link java.awt.FontMetrics class notes}).
573
* @param str the specified {@code String}
574
* @param context the specified {@code Graphics} context
575
* @return a {@link Rectangle2D} that is the bounding box of the
576
* specified {@code String} in the specified
577
* {@code Graphics} context.
578
* @see java.awt.Font#getStringBounds(String, FontRenderContext)
579
*/
580
public Rectangle2D getStringBounds( String str, Graphics context) {
581
return font.getStringBounds(str, myFRC(context));
582
}
583
584
/**
585
* Returns the bounds of the specified {@code String} in the
586
* specified {@code Graphics} context. The bounds is used
587
* to layout the {@code String}.
588
* <p>Note: The returned bounds is in baseline-relative coordinates
589
* (see {@link java.awt.FontMetrics class notes}).
590
* @param str the specified {@code String}
591
* @param beginIndex the offset of the beginning of {@code str}
592
* @param limit the end offset of {@code str}
593
* @param context the specified {@code Graphics} context
594
* @return a {@code Rectangle2D} that is the bounding box of the
595
* specified {@code String} in the specified
596
* {@code Graphics} context.
597
* @see java.awt.Font#getStringBounds(String, int, int, FontRenderContext)
598
*/
599
public Rectangle2D getStringBounds( String str,
600
int beginIndex, int limit,
601
Graphics context) {
602
return font.getStringBounds(str, beginIndex, limit,
603
myFRC(context));
604
}
605
606
/**
607
* Returns the bounds of the specified array of characters
608
* in the specified {@code Graphics} context.
609
* The bounds is used to layout the {@code String}
610
* created with the specified array of characters,
611
* {@code beginIndex} and {@code limit}.
612
* <p>Note: The returned bounds is in baseline-relative coordinates
613
* (see {@link java.awt.FontMetrics class notes}).
614
* @param chars an array of characters
615
* @param beginIndex the initial offset of the array of
616
* characters
617
* @param limit the end offset of the array of characters
618
* @param context the specified {@code Graphics} context
619
* @return a {@code Rectangle2D} that is the bounding box of the
620
* specified character array in the specified
621
* {@code Graphics} context.
622
* @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext)
623
*/
624
public Rectangle2D getStringBounds( char [] chars,
625
int beginIndex, int limit,
626
Graphics context) {
627
return font.getStringBounds(chars, beginIndex, limit,
628
myFRC(context));
629
}
630
631
/**
632
* Returns the bounds of the characters indexed in the specified
633
* {@code CharacterIterator} in the
634
* specified {@code Graphics} context.
635
* <p>Note: The returned bounds is in baseline-relative coordinates
636
* (see {@link java.awt.FontMetrics class notes}).
637
* @param ci the specified {@code CharacterIterator}
638
* @param beginIndex the initial offset in {@code ci}
639
* @param limit the end index of {@code ci}
640
* @param context the specified {@code Graphics} context
641
* @return a {@code Rectangle2D} that is the bounding box of the
642
* characters indexed in the specified {@code CharacterIterator}
643
* in the specified {@code Graphics} context.
644
* @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext)
645
*/
646
public Rectangle2D getStringBounds(CharacterIterator ci,
647
int beginIndex, int limit,
648
Graphics context) {
649
return font.getStringBounds(ci, beginIndex, limit,
650
myFRC(context));
651
}
652
653
/**
654
* Returns the bounds for the character with the maximum bounds
655
* in the specified {@code Graphics} context.
656
* @param context the specified {@code Graphics} context
657
* @return a {@code Rectangle2D} that is the
658
* bounding box for the character with the maximum bounds.
659
* @see java.awt.Font#getMaxCharBounds(FontRenderContext)
660
*/
661
public Rectangle2D getMaxCharBounds(Graphics context) {
662
return font.getMaxCharBounds(myFRC(context));
663
}
664
665
private FontRenderContext myFRC(Graphics context) {
666
if (context instanceof Graphics2D) {
667
return ((Graphics2D)context).getFontRenderContext();
668
}
669
return DEFAULT_FRC;
670
}
671
672
673
/**
674
* Returns a representation of this {@code FontMetrics}
675
* object's values as a {@code String}.
676
* @return a {@code String} representation of this
677
* {@code FontMetrics} object.
678
*/
679
public String toString() {
680
return getClass().getName() +
681
"[font=" + getFont() +
682
"ascent=" + getAscent() +
683
", descent=" + getDescent() +
684
", height=" + getHeight() + "]";
685
}
686
687
/**
688
* Initialize JNI field and method IDs
689
*/
690
private static native void initIDs();
691
}
692
693