1. /*
  2. * @(#)FontMetrics.java 1.53 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.Graphics2D;
  9. import java.awt.font.FontRenderContext;
  10. import java.awt.font.LineMetrics;
  11. import java.awt.geom.Rectangle2D;
  12. import java.text.CharacterIterator;
  13. /**
  14. * The <code>FontMetrics</code> class defines a font metrics object, which
  15. * encapsulates information about the rendering of a particular font on a
  16. * particular screen.
  17. * <p>
  18. * <b>Note to subclassers</b>: Since many of these methods form closed,
  19. * mutually recursive loops, you must take care that you implement
  20. * at least one of the methods in each such loop to prevent
  21. * infinite recursion when your subclass is used.
  22. * In particular, the following is the minimal suggested set of methods
  23. * to override in order to ensure correctness and prevent infinite
  24. * recursion (though other subsets are equally feasible):
  25. * <ul>
  26. * <li>{@link #getAscent()}
  27. * <li>{@link #getLeading()}
  28. * <li>{@link #getMaxAdvance()}
  29. * <li>{@link #charWidth(char)}
  30. * <li>{@link #charsWidth(char[], int, int)}
  31. * </ul>
  32. * <p>
  33. * <img src="doc-files/FontMetrics-1.gif" alt="The letter 'p' showing its 'reference point'" border=15 align
  34. * ALIGN=right HSPACE=10 VSPACE=7>
  35. * Note that the implementations of these methods are
  36. * inefficient, so they are usually overridden with more efficient
  37. * toolkit-specific implementations.
  38. * <p>
  39. * When an application asks AWT to place a character at the position
  40. * (<i>x</i>, <i>y</i>), the character is placed so that its
  41. * reference point (shown as the dot in the accompanying image) is
  42. * put at that position. The reference point specifies a horizontal
  43. * line called the <i>baseline</i> of the character. In normal
  44. * printing, the baselines of characters should align.
  45. * <p>
  46. * In addition, every character in a font has an <i>ascent</i>, a
  47. * <i>descent</i>, and an <i>advance width</i>. The ascent is the
  48. * amount by which the character ascends above the baseline. The
  49. * descent is the amount by which the character descends below the
  50. * baseline. The advance width indicates the position at which AWT
  51. * should place the next character.
  52. * <p>
  53. * An array of characters or a string can also have an ascent, a
  54. * descent, and an advance width. The ascent of the array is the
  55. * maximum ascent of any character in the array. The descent is the
  56. * maximum descent of any character in the array. The advance width
  57. * is the sum of the advance widths of each of the characters in the
  58. * character array. The advance of a <code>String</code> is the
  59. * distance along the baseline of the <code>String</code>. This
  60. * distance is the width that should be used for centering or
  61. * right-aligning the <code>String</code>.
  62. * Note that the advance of a <code>String</code> is not necessarily
  63. * the sum of the advances of its characters measured in isolation
  64. * because the width of a character can vary depending on its context.
  65. * For example, in Arabic text, the shape of a character can change
  66. * in order to connect to other characters. Also, in some scripts,
  67. * certain character sequences can be represented by a single shape,
  68. * called a <em>ligature</em>. Measuring characters individually does
  69. * not account for these transformations.
  70. *
  71. * @version 1.53 05/18/04
  72. * @author Jim Graham
  73. * @see java.awt.Font
  74. * @since JDK1.0
  75. */
  76. public abstract class FontMetrics implements java.io.Serializable {
  77. static {
  78. /* ensure that the necessary native libraries are loaded */
  79. Toolkit.loadLibraries();
  80. if (!GraphicsEnvironment.isHeadless()) {
  81. initIDs();
  82. }
  83. }
  84. /**
  85. * The actual {@link Font} from which the font metrics are
  86. * created.
  87. * This cannot be null.
  88. *
  89. * @serial
  90. * @see #getFont()
  91. */
  92. protected Font font;
  93. /*
  94. * JDK 1.1 serialVersionUID
  95. */
  96. private static final long serialVersionUID = 1681126225205050147L;
  97. /**
  98. * Creates a new <code>FontMetrics</code> object for finding out
  99. * height and width information about the specified <code>Font</code>
  100. * and specific character glyphs in that <code>Font</code>.
  101. * @param font the <code>Font</code>
  102. * @see java.awt.Font
  103. */
  104. protected FontMetrics(Font font) {
  105. this.font = font;
  106. }
  107. /**
  108. * Gets the <code>Font</code> described by this
  109. * <code>FontMetrics</code> object.
  110. * @return the <code>Font</code> described by this
  111. * <code>FontMetrics</code> object.
  112. */
  113. public Font getFont() {
  114. return font;
  115. }
  116. /**
  117. * Determines the <em>standard leading</em> of the
  118. * <code>Font</code> described by this <code>FontMetrics</code>
  119. * object. The standard leading, or
  120. * interline spacing, is the logical amount of space to be reserved
  121. * between the descent of one line of text and the ascent of the next
  122. * line. The height metric is calculated to include this extra space.
  123. * @return the standard leading of the <code>Font</code>.
  124. * @see #getHeight()
  125. * @see #getAscent()
  126. * @see #getDescent()
  127. */
  128. public int getLeading() {
  129. return 0;
  130. }
  131. /**
  132. * Determines the <em>font ascent</em> of the <code>Font</code>
  133. * described by this <code>FontMetrics</code> object. The font ascent
  134. * is the distance from the font's baseline to the top of most
  135. * alphanumeric characters. Some characters in the <code>Font</code>
  136. * might extend above the font ascent line.
  137. * @return the font ascent of the <code>Font</code>.
  138. * @see #getMaxAscent()
  139. */
  140. public int getAscent() {
  141. return font.getSize();
  142. }
  143. /**
  144. * Determines the <em>font descent</em> of the <code>Font</code>
  145. * described by this
  146. * <code>FontMetrics</code> object. The font descent is the distance
  147. * from the font's baseline to the bottom of most alphanumeric
  148. * characters with descenders. Some characters in the
  149. * <code>Font</code> might extend
  150. * below the font descent line.
  151. * @return the font descent of the <code>Font</code>.
  152. * @see #getMaxDescent()
  153. */
  154. public int getDescent() {
  155. return 0;
  156. }
  157. /**
  158. * Gets the standard height of a line of text in this font. This
  159. * is the distance between the baseline of adjacent lines of text.
  160. * It is the sum of the leading + ascent + descent. Due to rounding
  161. * this may not be the same as getAscent() + getDescent() + getLeading().
  162. * There is no guarantee that lines of text spaced at this distance are
  163. * disjoint; such lines may overlap if some characters overshoot
  164. * either the standard ascent or the standard descent metric.
  165. * @return the standard height of the font.
  166. * @see #getLeading()
  167. * @see #getAscent()
  168. * @see #getDescent()
  169. */
  170. public int getHeight() {
  171. return getLeading() + getAscent() + getDescent();
  172. }
  173. /**
  174. * Determines the maximum ascent of the <code>Font</code>
  175. * described by this <code>FontMetrics</code> object. No character
  176. * extends further above the font's baseline than this height.
  177. * @return the maximum ascent of any character in the
  178. * <code>Font</code>.
  179. * @see #getAscent()
  180. */
  181. public int getMaxAscent() {
  182. return getAscent();
  183. }
  184. /**
  185. * Determines the maximum descent of the <code>Font</code>
  186. * described by this <code>FontMetrics</code> object. No character
  187. * extends further below the font's baseline than this height.
  188. * @return the maximum descent of any character in the
  189. * <code>Font</code>.
  190. * @see #getDescent()
  191. */
  192. public int getMaxDescent() {
  193. return getDescent();
  194. }
  195. /**
  196. * For backward compatibility only.
  197. * @return the maximum descent of any character in the
  198. * <code>Font</code>.
  199. * @see #getMaxDescent()
  200. * @deprecated As of JDK version 1.1.1,
  201. * replaced by <code>getMaxDescent()</code>.
  202. */
  203. @Deprecated
  204. public int getMaxDecent() {
  205. return getMaxDescent();
  206. }
  207. /**
  208. * Gets the maximum advance width of any character in this
  209. * <code>Font</code>. The advance is the
  210. * distance from the leftmost point to the rightmost point on the
  211. * string's baseline. The advance of a <code>String</code> is
  212. * not necessarily the sum of the advances of its characters.
  213. * @return the maximum advance width of any character
  214. * in the <code>Font</code>, or <code>-1</code> if the
  215. * maximum advance width is not known.
  216. */
  217. public int getMaxAdvance() {
  218. return -1;
  219. }
  220. /**
  221. * Returns the advance width of the specified character in this
  222. * <code>Font</code>. The advance is the
  223. * distance from the leftmost point to the rightmost point on the
  224. * character's baseline. Note that the advance of a
  225. * <code>String</code> is not necessarily the sum of the advances
  226. * of its characters.
  227. *
  228. * <p>This method doesn't validate the specified character to be a
  229. * valid Unicode code point. The caller must validate the
  230. * character value using {@link
  231. * java.lang.Character#isValidCodePoint(int)
  232. * Character.isValidCodePoint} if necessary.
  233. *
  234. * @param codePoint the character (Unicode code point) to be measured
  235. * @return the advance width of the specified character
  236. * in the <code>Font</code> described by this
  237. * <code>FontMetrics</code> object.
  238. * @see #charsWidth(char[], int, int)
  239. * @see #stringWidth(String)
  240. */
  241. public int charWidth(int codePoint) {
  242. if (!Character.isValidCodePoint(codePoint)) {
  243. codePoint = 0xffff; // substitute missing glyph width
  244. }
  245. if (codePoint < 256) {
  246. return getWidths()[codePoint];
  247. } else {
  248. char[] buffer = new char[2];
  249. int len = Character.toChars(codePoint, buffer, 0);
  250. return charsWidth(buffer, 0, len);
  251. }
  252. }
  253. /**
  254. * Returns the advance width of the specified character in this
  255. * <code>Font</code>. The advance is the
  256. * distance from the leftmost point to the rightmost point on the
  257. * character's baseline. Note that the advance of a
  258. * <code>String</code> is not necessarily the sum of the advances
  259. * of its characters.
  260. *
  261. * <p><b>Note:</b> This method cannot handle <a
  262. * href="../../lang/Character.html#supplementary"> supplementary
  263. * characters</a>. To support all Unicode characters, including
  264. * supplementary characters, use the {@link #charWidth(int)} method.
  265. *
  266. * @param ch the character to be measured
  267. * @return the advance width of the specified character
  268. * in the <code>Font</code> described by this
  269. * <code>FontMetrics</code> object.
  270. * @see #charsWidth(char[], int, int)
  271. * @see #stringWidth(String)
  272. */
  273. public int charWidth(char ch) {
  274. if (ch < 256) {
  275. return getWidths()[ch];
  276. }
  277. char data[] = {ch};
  278. return charsWidth(data, 0, 1);
  279. }
  280. /**
  281. * Returns the total advance width for showing the specified
  282. * <code>String</code> in this <code>Font</code>. The advance
  283. * is the distance from the leftmost point to the rightmost point
  284. * on the string's baseline.
  285. * <p>
  286. * Note that the total advance width returned from this method
  287. * does not take into account the rendering context. Therefore,
  288. * the anti-aliasing and fractional metrics hints can affect the
  289. * value of the advance. When enabling the anti-aliasing and
  290. * fractional metrics hints, use
  291. * <code>getStringBounds(String, Graphics)</code>
  292. * instead of this method. The advance of a <code>String</code> is
  293. * not necessarily the sum of the advances of its characters.
  294. * <p>
  295. * @param str the <code>String</code> to be measured
  296. * @return the advance width of the specified <code>String</code>
  297. * in the <code>Font</code> described by this
  298. * <code>FontMetrics</code>.
  299. * @see #bytesWidth(byte[], int, int)
  300. * @see #charsWidth(char[], int, int)
  301. * @see #getStringBounds(String, Graphics)
  302. */
  303. public int stringWidth(String str) {
  304. int len = str.length();
  305. char data[] = new char[len];
  306. str.getChars(0, len, data, 0);
  307. return charsWidth(data, 0, len);
  308. }
  309. /**
  310. * Returns the total advance width for showing the specified array
  311. * of characters in this <code>Font</code>. The advance is the
  312. * distance from the leftmost point to the rightmost point on the
  313. * string's baseline. The advance of a <code>String</code>
  314. * is not necessarily the sum of the advances of its characters.
  315. * This is equivalent to measuring a <code>String</code> of the
  316. * characters in the specified range.
  317. * @param data the array of characters to be measured
  318. * @param off the start offset of the characters in the array
  319. * @param len the number of characters to be measured from the array
  320. * @return the advance width of the subarray of the specified
  321. * <code>char</code> array in the font described by
  322. * this <code>FontMetrics</code> object.
  323. * @see #charWidth(int)
  324. * @see #charWidth(char)
  325. * @see #bytesWidth(byte[], int, int)
  326. * @see #stringWidth(String)
  327. */
  328. public int charsWidth(char data[], int off, int len) {
  329. return stringWidth(new String(data, off, len));
  330. }
  331. /**
  332. * Returns the total advance width for showing the specified array
  333. * of bytes in this <code>Font</code>. The advance is the
  334. * distance from the leftmost point to the rightmost point on the
  335. * string's baseline. The advance of a <code>String</code>
  336. * is not necessarily the sum of the advances of its characters.
  337. * This is equivalent to measuring a <code>String</code> of the
  338. * characters in the specified range.
  339. * @param data the array of bytes to be measured
  340. * @param off the start offset of the bytes in the array
  341. * @param len the number of bytes to be measured from the array
  342. * @return the advance width of the subarray of the specified
  343. * <code>byte</code> array in the <code>Font</code>
  344. * described by
  345. * this <code>FontMetrics</code> object.
  346. * @see #charsWidth(char[], int, int)
  347. * @see #stringWidth(String)
  348. */
  349. public int bytesWidth(byte data[], int off, int len) {
  350. return stringWidth(new String(data, 0, off, len));
  351. }
  352. /**
  353. * Gets the advance widths of the first 256 characters in the
  354. * <code>Font</code>. The advance is the
  355. * distance from the leftmost point to the rightmost point on the
  356. * character's baseline. Note that the advance of a
  357. * <code>String</code> is not necessarily the sum of the advances
  358. * of its characters.
  359. * @return an array storing the advance widths of the
  360. * characters in the <code>Font</code>
  361. * described by this <code>FontMetrics</code> object.
  362. */
  363. public int[] getWidths() {
  364. int widths[] = new int[256];
  365. for (char ch = 0 ; ch < 256 ; ch++) {
  366. widths[ch] = charWidth(ch);
  367. }
  368. return widths;
  369. }
  370. /**
  371. * Checks to see if the <code>Font</code> has uniform line metrics. A
  372. * composite font may consist of several different fonts to cover
  373. * various character sets. In such cases, the
  374. * <code>FontLineMetrics</code> objects are not uniform.
  375. * Different fonts may have a different ascent, descent, metrics and
  376. * so on. This information is sometimes necessary for line
  377. * measuring and line breaking.
  378. * @return <code>true</code> if the font has uniform line metrics;
  379. * <code>false</code> otherwise.
  380. * @see java.awt.Font#hasUniformLineMetrics()
  381. */
  382. public boolean hasUniformLineMetrics() {
  383. return font.hasUniformLineMetrics();
  384. }
  385. /**
  386. * Returns the {@link LineMetrics} object for the specified
  387. * <code>String</code> in the specified {@link Graphics} context.
  388. * @param str the specified <code>String</code>
  389. * @param context the specified <code>Graphics</code> context
  390. * @return a <code>LineMetrics</code> object created with the
  391. * specified <code>String</code> and <code>Graphics</code> context.
  392. * @see java.awt.Font#getLineMetrics(String, FontRenderContext)
  393. */
  394. public LineMetrics getLineMetrics( String str, Graphics context) {
  395. return font.getLineMetrics(str, myFRC(context));
  396. }
  397. /**
  398. * Returns the {@link LineMetrics} object for the specified
  399. * <code>String</code> in the specified {@link Graphics} context.
  400. * @param str the specified <code>String</code>
  401. * @param beginIndex the initial offset of <code>str</code>
  402. * @param limit the length of <code>str</code>
  403. * @param context the specified <code>Graphics</code> context
  404. * @return a <code>LineMetrics</code> object created with the
  405. * specified <code>String</code> and <code>Graphics</code> context.
  406. * @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)
  407. */
  408. public LineMetrics getLineMetrics( String str,
  409. int beginIndex, int limit,
  410. Graphics context) {
  411. return font.getLineMetrics(str, beginIndex, limit, myFRC(context));
  412. }
  413. /**
  414. * Returns the {@link LineMetrics} object for the specified
  415. * character array in the specified {@link Graphics} context.
  416. * @param chars the specified character array
  417. * @param beginIndex the initial offset of <code>chars</code>
  418. * @param limit the length of <code>chars</code>
  419. * @param context the specified <code>Graphics</code> context
  420. * @return a <code>LineMetrics</code> object created with the
  421. * specified character array and <code>Graphics</code> context.
  422. * @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)
  423. */
  424. public LineMetrics getLineMetrics(char [] chars,
  425. int beginIndex, int limit,
  426. Graphics context) {
  427. return font.getLineMetrics(
  428. chars, beginIndex, limit, myFRC(context));
  429. }
  430. /**
  431. * Returns the {@link LineMetrics} object for the specified
  432. * {@link CharacterIterator} in the specified {@link Graphics}
  433. * context.
  434. * @param ci the specified <code>CharacterIterator</code>
  435. * @param beginIndex the initial offset in <code>ci</code>
  436. * @param limit the end index of <code>ci</code>
  437. * @param context the specified <code>Graphics</code> context
  438. * @return a <code>LineMetrics</code> object created with the
  439. * specified arguments.
  440. * @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)
  441. */
  442. public LineMetrics getLineMetrics(CharacterIterator ci,
  443. int beginIndex, int limit,
  444. Graphics context) {
  445. return font.getLineMetrics(ci, beginIndex, limit, myFRC(context));
  446. }
  447. /**
  448. * Returns the bounds of the specified <code>String</code> in the
  449. * specified <code>Graphics</code> context. The bounds is used
  450. * to layout the <code>String</code>.
  451. * @param str the specified <code>String</code>
  452. * @param context the specified <code>Graphics</code> context
  453. * @return a {@link Rectangle2D} that is the bounding box of the
  454. * specified <code>String</code> in the specified
  455. * <code>Graphics</code> context.
  456. * @see java.awt.Font#getStringBounds(String, FontRenderContext)
  457. */
  458. public Rectangle2D getStringBounds( String str, Graphics context) {
  459. return font.getStringBounds(str, myFRC(context));
  460. }
  461. /**
  462. * Returns the bounds of the specified <code>String</code> in the
  463. * specified <code>Graphics</code> context. The bounds is used
  464. * to layout the <code>String</code>.
  465. * @param str the specified <code>String</code>
  466. * @param beginIndex the offset of the beginning of <code>str</code>
  467. * @param limit the length of <code>str</code>
  468. * @param context the specified <code>Graphics</code> context
  469. * @return a <code>Rectangle2D</code> that is the bounding box of the
  470. * specified <code>String</code> in the specified
  471. * <code>Graphics</code> context.
  472. * @see java.awt.Font#getStringBounds(String, int, int, FontRenderContext)
  473. */
  474. public Rectangle2D getStringBounds( String str,
  475. int beginIndex, int limit,
  476. Graphics context) {
  477. return font.getStringBounds(str, beginIndex, limit,
  478. myFRC(context));
  479. }
  480. /**
  481. * Returns the bounds of the specified array of characters
  482. * in the specified <code>Graphics</code> context.
  483. * The bounds is used to layout the <code>String</code>
  484. * created with the specified array of characters,
  485. * <code>beginIndex</code> and <code>limit</code>.
  486. * @param chars an array of characters
  487. * @param beginIndex the initial offset of the array of
  488. * characters
  489. * @param limit the length of the array of characters
  490. * @param context the specified <code>Graphics</code> context
  491. * @return a <code>Rectangle2D</code> that is the bounding box of the
  492. * specified character array in the specified
  493. * <code>Graphics</code> context.
  494. * @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext)
  495. */
  496. public Rectangle2D getStringBounds( char [] chars,
  497. int beginIndex, int limit,
  498. Graphics context) {
  499. return font.getStringBounds(chars, beginIndex, limit,
  500. myFRC(context));
  501. }
  502. /**
  503. * Returns the bounds of the characters indexed in the specified
  504. * <code>CharacterIterator</code> in the
  505. * specified <code>Graphics</code> context.
  506. * @param ci the specified <code>CharacterIterator</code>
  507. * @param beginIndex the initial offset in <code>ci</code>
  508. * @param limit the end index of <code>ci</code>
  509. * @param context the specified <code>Graphics</code> context
  510. * @return a <code>Rectangle2D</code> that is the bounding box of the
  511. * characters indexed in the specified <code>CharacterIterator</code>
  512. * in the specified <code>Graphics</code> context.
  513. * @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext)
  514. */
  515. public Rectangle2D getStringBounds(CharacterIterator ci,
  516. int beginIndex, int limit,
  517. Graphics context) {
  518. return font.getStringBounds(ci, beginIndex, limit,
  519. myFRC(context));
  520. }
  521. /**
  522. * Returns the bounds for the character with the maximum bounds
  523. * in the specified <code>Graphics</code> context.
  524. * @param context the specified <code>Graphics</code> context
  525. * @return a <code>Rectangle2D</code> that is the
  526. * bounding box for the character with the maximum bounds.
  527. * @see java.awt.Font#getMaxCharBounds(FontRenderContext)
  528. */
  529. public Rectangle2D getMaxCharBounds(Graphics context) {
  530. return font.getMaxCharBounds(myFRC(context));
  531. }
  532. private FontRenderContext myFRC(Graphics context) {
  533. if (context instanceof Graphics2D) {
  534. return ((Graphics2D)context).getFontRenderContext();
  535. }
  536. return new FontRenderContext(null, false, false);
  537. }
  538. /**
  539. * Returns a representation of this <code>FontMetrics</code>
  540. * object's values as a <code>String</code>.
  541. * @return a <code>String</code> representation of this
  542. * <code>FontMetrics</code> object.
  543. * @since JDK1.0.
  544. */
  545. public String toString() {
  546. return getClass().getName() +
  547. "[font=" + getFont() +
  548. "ascent=" + getAscent() +
  549. ", descent=" + getDescent() +
  550. ", height=" + getHeight() + "]";
  551. }
  552. /**
  553. * Initialize JNI field and method IDs
  554. */
  555. private static native void initIDs();
  556. }