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