1. /*
  2. * @(#)FontMetrics.java 1.49 03/01/23
  3. *
  4. * Copyright 2003 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.49 01/23/03
  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. public int getMaxDecent() {
  204. return getMaxDescent();
  205. }
  206. /**
  207. * Gets the maximum advance width of any character in this
  208. * <code>Font</code>. The advance is the
  209. * distance from the leftmost point to the rightmost point on the
  210. * string's baseline. The advance of a <code>String</code> is
  211. * not necessarily the sum of the advances of its characters.
  212. * @return the maximum advance width of any character
  213. * in the <code>Font</code>, or <code>-1</code> if the
  214. * maximum advance width is not known.
  215. */
  216. public int getMaxAdvance() {
  217. return -1;
  218. }
  219. /**
  220. * Returns the advance width of the specified character in this
  221. * <code>Font</code>. The advance is the
  222. * distance from the leftmost point to the rightmost point on the
  223. * character's baseline. Note that the advance of a
  224. * <code>String</code> is not necessarily the sum of the advances
  225. * of its characters.
  226. * @param ch the character to be measured
  227. * @return the advance width of the specified <code>char</code>
  228. * in the <code>Font</code> described by this
  229. * <code>FontMetrics</code> object.
  230. * @see #charsWidth(char[], int, int)
  231. * @see #stringWidth(String)
  232. */
  233. public int charWidth(int ch) {
  234. return charWidth((char)ch);
  235. }
  236. /**
  237. * Returns the advance width of the specified character in this
  238. * <code>Font</code>. The advance is the
  239. * distance from the leftmost point to the rightmost point on the
  240. * character's baseline. Note that the advance of a
  241. * <code>String</code> is not necessarily the sum of the advances
  242. * of its characters.
  243. * @param ch the character to be measured
  244. * @return the advance width of the specified <code>char</code>
  245. * in the <code>Font</code> described by this
  246. * <code>FontMetrics</code> object.
  247. * @see #charsWidth(char[], int, int)
  248. * @see #stringWidth(String)
  249. */
  250. public int charWidth(char ch) {
  251. if (ch < 256) {
  252. return getWidths()[ch];
  253. }
  254. char data[] = {ch};
  255. return charsWidth(data, 0, 1);
  256. }
  257. /**
  258. * Returns the total advance width for showing the specified
  259. * <code>String</code> in this <code>Font</code>. The advance
  260. * is the distance from the leftmost point to the rightmost point
  261. * on the string's baseline.
  262. * <p>
  263. * Note that the total advance width returned from this method
  264. * does not take into account the rendering context. Therefore,
  265. * the anti-aliasing and fractional metrics hints can affect the
  266. * value of the advance. When enabling the anti-aliasing and
  267. * fractional metrics hints, use
  268. * <code>getStringBounds(String, Graphics)</code>
  269. * instead of this method. The advance of a <code>String</code> is
  270. * not necessarily the sum of the advances of its characters.
  271. * <p>
  272. * @param str the <code>String</code> to be measured
  273. * @return the advance width of the specified <code>String</code>
  274. * in the <code>Font</code> described by this
  275. * <code>FontMetrics</code>.
  276. * @see #bytesWidth(byte[], int, int)
  277. * @see #charsWidth(char[], int, int)
  278. * @see #getStringBounds(String, Graphics)
  279. */
  280. public int stringWidth(String str) {
  281. int len = str.length();
  282. char data[] = new char[len];
  283. str.getChars(0, len, data, 0);
  284. return charsWidth(data, 0, len);
  285. }
  286. /**
  287. * Returns the total advance width for showing the specified array
  288. * of characters in this <code>Font</code>. The advance is the
  289. * distance from the leftmost point to the rightmost point on the
  290. * string's baseline. The advance of a <code>String</code>
  291. * is not necessarily the sum of the advances of its characters.
  292. * This is equivalent to measuring a <code>String</code> of the
  293. * characters in the specified range.
  294. * @param data the array of characters to be measured
  295. * @param off the start offset of the characters in the array
  296. * @param len the number of characters to be measured from the array
  297. * @return the advance width of the subarray of the specified
  298. * <code>char</code> array in the font described by
  299. * this <code>FontMetrics</code> object.
  300. * @see #charWidth(int)
  301. * @see #charWidth(char)
  302. * @see #bytesWidth(byte[], int, int)
  303. * @see #stringWidth(String)
  304. */
  305. public int charsWidth(char data[], int off, int len) {
  306. return stringWidth(new String(data, off, len));
  307. }
  308. /**
  309. * Returns the total advance width for showing the specified array
  310. * of bytes in this <code>Font</code>. The advance is the
  311. * distance from the leftmost point to the rightmost point on the
  312. * string's baseline. The advance of a <code>String</code>
  313. * is not necessarily the sum of the advances of its characters.
  314. * This is equivalent to measuring a <code>String</code> of the
  315. * characters in the specified range.
  316. * @param data the array of bytes to be measured
  317. * @param off the start offset of the bytes in the array
  318. * @param len the number of bytes to be measured from the array
  319. * @return the advance width of the subarray of the specified
  320. * <code>byte</code> array in the <code>Font</code>
  321. * described by
  322. * this <code>FontMetrics</code> object.
  323. * @see #charsWidth(char[], int, int)
  324. * @see #stringWidth(String)
  325. */
  326. public int bytesWidth(byte data[], int off, int len) {
  327. return stringWidth(new String(data, 0, off, len));
  328. }
  329. /**
  330. * Gets the advance widths of the first 256 characters in the
  331. * <code>Font</code>. The advance is the
  332. * distance from the leftmost point to the rightmost point on the
  333. * character's baseline. Note that the advance of a
  334. * <code>String</code> is not necessarily the sum of the advances
  335. * of its characters.
  336. * @return an array storing the advance widths of the
  337. * characters in the <code>Font</code>
  338. * described by this <code>FontMetrics</code> object.
  339. */
  340. public int[] getWidths() {
  341. int widths[] = new int[256];
  342. for (char ch = 0 ; ch < 256 ; ch++) {
  343. widths[ch] = charWidth(ch);
  344. }
  345. return widths;
  346. }
  347. /**
  348. * Checks to see if the <code>Font</code> has uniform line metrics. A
  349. * composite font may consist of several different fonts to cover
  350. * various character sets. In such cases, the
  351. * <code>FontLineMetrics</code> objects are not uniform.
  352. * Different fonts may have a different ascent, descent, metrics and
  353. * so on. This information is sometimes necessary for line
  354. * measuring and line breaking.
  355. * @return <code>true</code> if the font has uniform line metrics;
  356. * <code>false</code> otherwise.
  357. * @see java.awt.Font#hasUniformLineMetrics()
  358. */
  359. public boolean hasUniformLineMetrics() {
  360. return font.hasUniformLineMetrics();
  361. }
  362. /**
  363. * Returns the {@link LineMetrics} object for the specified
  364. * <code>String</code> in the specified {@link Graphics} context.
  365. * @param str the specified <code>String</code>
  366. * @param context the specified <code>Graphics</code> context
  367. * @return a <code>LineMetrics</code> object created with the
  368. * specified <code>String</code> and <code>Graphics</code> context.
  369. * @see java.awt.Font#getLineMetrics(String, FontRenderContext)
  370. */
  371. public LineMetrics getLineMetrics( String str, Graphics context) {
  372. return font.getLineMetrics(str, myFRC(context));
  373. }
  374. /**
  375. * Returns the {@link LineMetrics} object for the specified
  376. * <code>String</code> in the specified {@link Graphics} context.
  377. * @param str the specified <code>String</code>
  378. * @param beginIndex the initial offset of <code>str</code>
  379. * @param limit the length of <code>str</code>
  380. * @param context the specified <code>Graphics</code> context
  381. * @return a <code>LineMetrics</code> object created with the
  382. * specified <code>String</code> and <code>Graphics</code> context.
  383. * @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)
  384. */
  385. public LineMetrics getLineMetrics( String str,
  386. int beginIndex, int limit,
  387. Graphics context) {
  388. return font.getLineMetrics(str, beginIndex, limit, myFRC(context));
  389. }
  390. /**
  391. * Returns the {@link LineMetrics} object for the specified
  392. * character array in the specified {@link Graphics} context.
  393. * @param chars the specified character array
  394. * @param beginIndex the initial offset of <code>chars</code>
  395. * @param limit the length of <code>chars</code>
  396. * @param context the specified <code>Graphics</code> context
  397. * @return a <code>LineMetrics</code> object created with the
  398. * specified character array and <code>Graphics</code> context.
  399. * @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)
  400. */
  401. public LineMetrics getLineMetrics(char [] chars,
  402. int beginIndex, int limit,
  403. Graphics context) {
  404. return font.getLineMetrics(
  405. chars, beginIndex, limit, myFRC(context));
  406. }
  407. /**
  408. * Returns the {@link LineMetrics} object for the specified
  409. * {@link CharacterIterator} in the specified {@link Graphics}
  410. * context.
  411. * @param ci the specified <code>CharacterIterator</code>
  412. * @param beginIndex the initial offset in <code>ci</code>
  413. * @param limit the end index of <code>ci</code>
  414. * @param context the specified <code>Graphics</code> context
  415. * @return a <code>LineMetrics</code> object created with the
  416. * specified arguments.
  417. * @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)
  418. */
  419. public LineMetrics getLineMetrics(CharacterIterator ci,
  420. int beginIndex, int limit,
  421. Graphics context) {
  422. return font.getLineMetrics(ci, beginIndex, limit, myFRC(context));
  423. }
  424. /**
  425. * Returns the bounds of the specified <code>String</code> in the
  426. * specified <code>Graphics</code> context. The bounds is used
  427. * to layout the <code>String</code>.
  428. * @param str the specified <code>String</code>
  429. * @param context the specified <code>Graphics</code> context
  430. * @return a {@link Rectangle2D} that is the bounding box of the
  431. * specified <code>String</code> in the specified
  432. * <code>Graphics</code> context.
  433. * @see java.awt.Font#getStringBounds(String, FontRenderContext)
  434. */
  435. public Rectangle2D getStringBounds( String str, Graphics context) {
  436. return font.getStringBounds(str, myFRC(context));
  437. }
  438. /**
  439. * Returns the bounds of the specified <code>String</code> in the
  440. * specified <code>Graphics</code> context. The bounds is used
  441. * to layout the <code>String</code>.
  442. * @param str the specified <code>String</code>
  443. * @param beginIndex the offset of the beginning of <code>str</code>
  444. * @param limit the length of <code>str</code>
  445. * @param context the specified <code>Graphics</code> context
  446. * @return a <code>Rectangle2D</code> that is the bounding box of the
  447. * specified <code>String</code> in the specified
  448. * <code>Graphics</code> context.
  449. * @see java.awt.Font#getStringBounds(String, int, int, FontRenderContext)
  450. */
  451. public Rectangle2D getStringBounds( String str,
  452. int beginIndex, int limit,
  453. Graphics context) {
  454. return font.getStringBounds(str, beginIndex, limit,
  455. myFRC(context));
  456. }
  457. /**
  458. * Returns the bounds of the specified array of characters
  459. * in the specified <code>Graphics</code> context.
  460. * The bounds is used to layout the <code>String</code>
  461. * created with the specified array of characters,
  462. * <code>beginIndex</code> and <code>limit</code>.
  463. * @param chars an array of characters
  464. * @param beginIndex the initial offset of the array of
  465. * characters
  466. * @param limit the length of the array of characters
  467. * @param context the specified <code>Graphics</code> context
  468. * @return a <code>Rectangle2D</code> that is the bounding box of the
  469. * specified character array in the specified
  470. * <code>Graphics</code> context.
  471. * @see java.awt.Font#getStringBounds(char[], int, int, FontRenderContext)
  472. */
  473. public Rectangle2D getStringBounds( char [] chars,
  474. int beginIndex, int limit,
  475. Graphics context) {
  476. return font.getStringBounds(chars, beginIndex, limit,
  477. myFRC(context));
  478. }
  479. /**
  480. * Returns the bounds of the characters indexed in the specified
  481. * <code>CharacterIterator</code> in the
  482. * specified <code>Graphics</code> context.
  483. * @param ci the specified <code>CharacterIterator</code>
  484. * @param beginIndex the initial offset in <code>ci</code>
  485. * @param limit the end index of <code>ci</code>
  486. * @param context the specified <code>Graphics</code> context
  487. * @return a <code>Rectangle2D</code> that is the bounding box of the
  488. * characters indexed in the specified <code>CharacterIterator</code>
  489. * in the specified <code>Graphics</code> context.
  490. * @see java.awt.Font#getStringBounds(CharacterIterator, int, int, FontRenderContext)
  491. */
  492. public Rectangle2D getStringBounds(CharacterIterator ci,
  493. int beginIndex, int limit,
  494. Graphics context) {
  495. return font.getStringBounds(ci, beginIndex, limit,
  496. myFRC(context));
  497. }
  498. /**
  499. * Returns the bounds for the character with the maximum bounds
  500. * in the specified <code>Graphics</code> context.
  501. * @param context the specified <code>Graphics</code> context
  502. * @return a <code>Rectangle2D</code> that is the
  503. * bounding box for the character with the maximum bounds.
  504. * @see java.awt.Font#getMaxCharBounds(FontRenderContext)
  505. */
  506. public Rectangle2D getMaxCharBounds(Graphics context) {
  507. return font.getMaxCharBounds(myFRC(context));
  508. }
  509. private FontRenderContext myFRC(Graphics context) {
  510. if (context instanceof Graphics2D) {
  511. return ((Graphics2D)context).getFontRenderContext();
  512. }
  513. return new FontRenderContext(null, false, false);
  514. }
  515. /**
  516. * Returns a representation of this <code>FontMetrics</code>
  517. * object's values as a <code>String</code>.
  518. * @return a <code>String</code> representation of this
  519. * <code>FontMetrics</code> object.
  520. * @since JDK1.0.
  521. */
  522. public String toString() {
  523. return getClass().getName() +
  524. "[font=" + getFont() +
  525. "ascent=" + getAscent() +
  526. ", descent=" + getDescent() +
  527. ", height=" + getHeight() + "]";
  528. }
  529. /**
  530. * Initialize JNI field and method IDs
  531. */
  532. private static native void initIDs();
  533. }