PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/java/awt/FontMetrics.java

https://github.com/penberg/classpath
Java | 448 lines | 167 code | 38 blank | 243 comment | 8 complexity | 64182d9361813de3e4db8b563dbc1c23 MD5 | raw file
  1. /* FontMetrics.java -- Information about about a fonts display characteristics
  2. Copyright (C) 1999, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.font.FontRenderContext;
  33. import java.awt.font.LineMetrics;
  34. import java.awt.geom.Rectangle2D;
  35. import java.text.CharacterIterator;
  36. // FIXME: I leave many methods basically unimplemented. This
  37. // should be reviewed.
  38. /**
  39. * This class returns information about the display characteristics of
  40. * a font. It is abstract, and concrete subclasses should implement at
  41. * least the following methods:
  42. *
  43. * <ul>
  44. * <li>getAscent()</li>
  45. * <li>getDescent()</li>
  46. * <li>getLeading()</li>
  47. * <li>getMaxAdvance()</li>
  48. * <li>charWidth(char)</li>
  49. * <li>charsWidth(char[], int, int)</li>
  50. * </ul>
  51. *
  52. * @author Aaron M. Renn (arenn@urbanophile.com)
  53. */
  54. public abstract class FontMetrics implements java.io.Serializable
  55. {
  56. // Serialization constant.
  57. private static final long serialVersionUID = 1681126225205050147L;
  58. /**
  59. * This is the font for which metrics will be returned.
  60. */
  61. protected Font font;
  62. /**
  63. * Initializes a new instance of <code>FontMetrics</code> for the
  64. * specified font.
  65. *
  66. * @param font The font to return metric information for.
  67. */
  68. protected FontMetrics(Font font)
  69. {
  70. this.font = font;
  71. }
  72. /**
  73. * Returns the font that this object is creating metric information fo.
  74. *
  75. * @return The font for this object.
  76. */
  77. public Font getFont()
  78. {
  79. return font;
  80. }
  81. /**
  82. * Returns the leading, or spacing between lines, for this font.
  83. *
  84. * @return The font leading.
  85. */
  86. public int getLeading()
  87. {
  88. return 0;
  89. }
  90. /**
  91. * Returns the ascent of the font, which is the distance from the base
  92. * to the top of the majority of characters in the set. Some characters
  93. * can exceed this value however.
  94. *
  95. * @return The font ascent.
  96. */
  97. public int getAscent()
  98. {
  99. return 1;
  100. }
  101. /**
  102. * Returns the descent of the font, which is the distance from the base
  103. * to the bottom of the majority of characters in the set. Some characters
  104. * can exceed this value however.
  105. *
  106. * @return The font descent.
  107. */
  108. public int getDescent()
  109. {
  110. return 1;
  111. }
  112. /**
  113. * Returns the height of a line in this font. This will be the sum
  114. * of the leading, the ascent, and the descent.
  115. *
  116. * @return The height of the font.
  117. */
  118. public int getHeight()
  119. {
  120. return getAscent() + getDescent() + getLeading();
  121. }
  122. /**
  123. * Returns the maximum ascent value. This is the maximum distance any
  124. * character in the font rised above the baseline.
  125. *
  126. * @return The maximum ascent for this font.
  127. */
  128. public int getMaxAscent()
  129. {
  130. return getAscent();
  131. }
  132. /**
  133. * Returns the maximum descent value. This is the maximum distance any
  134. * character in the font extends below the baseline.
  135. *
  136. * @return The maximum descent for this font.
  137. */
  138. public int getMaxDescent()
  139. {
  140. return getMaxDecent();
  141. }
  142. /**
  143. * Returns the maximum descent value. This is the maximum distance any
  144. * character in the font extends below the baseline.
  145. *
  146. * @return The maximum descent for this font.
  147. *
  148. * @deprecated This method is deprecated in favor of
  149. * <code>getMaxDescent()</code>.
  150. */
  151. public int getMaxDecent()
  152. {
  153. return getDescent();
  154. }
  155. /**
  156. * Returns the width of the widest character in the font.
  157. *
  158. * @return The width of the widest character in the font.
  159. */
  160. public int getMaxAdvance()
  161. {
  162. return -1;
  163. }
  164. /**
  165. * Returns the width of the specified character.
  166. *
  167. * @param ch The character to return the width of.
  168. *
  169. * @return The width of the specified character.
  170. */
  171. public int charWidth(int ch)
  172. {
  173. char[] chars = Character.toChars(ch);
  174. return charsWidth(chars, 0, chars.length);
  175. }
  176. /**
  177. * Returns the width of the specified character.
  178. *
  179. * @param ch The character to return the width of.
  180. *
  181. * @return The width of the specified character.
  182. */
  183. public int charWidth(char ch)
  184. {
  185. return 1;
  186. }
  187. /**
  188. * Returns the total width of the specified string
  189. *
  190. * @param str The string to return the width of.
  191. *
  192. * @return The width of the string.
  193. */
  194. public int stringWidth(String str)
  195. {
  196. char[] buf = new char[str.length()];
  197. str.getChars(0, str.length(), buf, 0);
  198. return charsWidth(buf, 0, buf.length);
  199. }
  200. /**
  201. * Returns the total width of the specified character array.
  202. *
  203. * @param buf The character array containing the data.
  204. * @param offset The offset into the array to start calculating from.
  205. * @param len The total number of bytes to process.
  206. *
  207. * @return The width of the requested characters.
  208. */
  209. public int charsWidth(char[] buf, int offset, int len)
  210. {
  211. int total_width = 0;
  212. int endOffset = offset + len;
  213. for (int i = offset; i < endOffset; i++)
  214. total_width += charWidth(buf[i]);
  215. return total_width;
  216. }
  217. /**
  218. * Returns the total width of the specified byte array.
  219. *
  220. * @param buf The byte array containing the data.
  221. * @param offset The offset into the array to start calculating from.
  222. * @param len The total number of bytes to process.
  223. *
  224. * @return The width of the requested characters.
  225. */
  226. public int bytesWidth(byte[] buf, int offset, int len)
  227. {
  228. int total_width = 0;
  229. for (int i = offset; i < len; i++)
  230. total_width = charWidth((char) buf[i]);
  231. return total_width;
  232. }
  233. /**
  234. * Returns the widths of the first 256 characters in the font.
  235. *
  236. * @return The widths of the first 256 characters in the font.
  237. */
  238. public int[] getWidths()
  239. {
  240. int[] result = new int[256];
  241. for (char i = 0; i < 256; i++)
  242. result[i] = charWidth(i);
  243. return result;
  244. }
  245. /**
  246. * Returns a string representation of this object.
  247. *
  248. * @return A string representation of this object.
  249. */
  250. public String toString()
  251. {
  252. return (this.getClass() + "[font=" + font + ",ascent=" + getAscent()
  253. + ",descent=" + getDescent() + ",height=" + getHeight() + "]");
  254. }
  255. // Generic FontRenderContext used when getLineMetrics is called with a
  256. // plain Graphics object.
  257. private static final FontRenderContext gRC = new FontRenderContext(null,
  258. false,
  259. false);
  260. /**
  261. * Returns a {@link LineMetrics} object constructed with the
  262. * specified text and the {@link FontRenderContext} of the Graphics
  263. * object when it is an instance of Graphics2D or a generic
  264. * FontRenderContext with a null transform, not anti-aliased and not
  265. * using fractional metrics.
  266. *
  267. * @param text The string to calculate metrics from.
  268. * @param g The Graphics object that will be used.
  269. *
  270. * @return A new {@link LineMetrics} object.
  271. */
  272. public LineMetrics getLineMetrics(String text, Graphics g)
  273. {
  274. return getLineMetrics(text, 0, text.length(), g);
  275. }
  276. /**
  277. * Returns a {@link LineMetrics} object constructed with the
  278. * specified text and the {@link FontRenderContext} of the Graphics
  279. * object when it is an instance of Graphics2D or a generic
  280. * FontRenderContext with a null transform, not anti-aliased and not
  281. * using fractional metrics.
  282. *
  283. * @param text The string to calculate metrics from.
  284. * @param begin Index of first character in <code>text</code> to measure.
  285. * @param limit Index of last character in <code>text</code> to measure.
  286. * @param g The Graphics object that will be used.
  287. *
  288. * @return A new {@link LineMetrics} object.
  289. *
  290. * @throws IndexOutOfBoundsException if the range [begin, limit] is
  291. * invalid in <code>text</code>.
  292. */
  293. public LineMetrics getLineMetrics(String text, int begin, int limit,
  294. Graphics g)
  295. {
  296. FontRenderContext rc;
  297. if (g instanceof Graphics2D)
  298. rc = ((Graphics2D) g).getFontRenderContext();
  299. else
  300. rc = gRC;
  301. return font.getLineMetrics(text, begin, limit, rc);
  302. }
  303. /**
  304. * Returns a {@link LineMetrics} object constructed with the
  305. * specified text and the {@link FontRenderContext} of the Graphics
  306. * object when it is an instance of Graphics2D or a generic
  307. * FontRenderContext with a null transform, not anti-aliased and not
  308. * using fractional metrics.
  309. *
  310. * @param chars The string to calculate metrics from.
  311. * @param begin Index of first character in <code>text</code> to measure.
  312. * @param limit Index of last character in <code>text</code> to measure.
  313. * @param g The Graphics object that will be used.
  314. *
  315. * @return A new {@link LineMetrics} object.
  316. *
  317. * @throws IndexOutOfBoundsException if the range [begin, limit] is
  318. * invalid in <code>text</code>.
  319. */
  320. public LineMetrics getLineMetrics(char[] chars, int begin, int limit,
  321. Graphics g)
  322. {
  323. FontRenderContext rc;
  324. if (g instanceof Graphics2D)
  325. rc = ((Graphics2D) g).getFontRenderContext();
  326. else
  327. rc = gRC;
  328. return font.getLineMetrics(chars, begin, limit, rc);
  329. }
  330. /**
  331. * Returns the bounds of the largest character in a Graphics context.
  332. * @param context the Graphics context object.
  333. * @return a <code>Rectangle2D</code> representing the bounds
  334. */
  335. public Rectangle2D getMaxCharBounds(Graphics context)
  336. {
  337. if( context instanceof Graphics2D )
  338. return font.getMaxCharBounds(((Graphics2D)context).getFontRenderContext());
  339. return font.getMaxCharBounds( gRC );
  340. }
  341. /**
  342. * Returns a {@link LineMetrics} object constructed with the
  343. * specified text and the {@link FontRenderContext} of the Graphics
  344. * object when it is an instance of Graphics2D or a generic
  345. * FontRenderContext with a null transform, not anti-aliased and not
  346. * using fractional metrics.
  347. *
  348. * @param ci An iterator over the string to calculate metrics from.
  349. * @param begin Index of first character in <code>text</code> to measure.
  350. * @param limit Index of last character in <code>text</code> to measure.
  351. * @param g The Graphics object that will be used.
  352. *
  353. * @return A new {@link LineMetrics} object.
  354. *
  355. * @throws IndexOutOfBoundsException if the range [begin, limit] is
  356. * invalid in <code>text</code>.
  357. */
  358. public LineMetrics getLineMetrics(CharacterIterator ci, int begin,
  359. int limit, Graphics g)
  360. {
  361. FontRenderContext rc;
  362. if (g instanceof Graphics2D)
  363. rc = ((Graphics2D) g).getFontRenderContext();
  364. else
  365. rc = gRC;
  366. return font.getLineMetrics(ci, begin, limit, rc);
  367. }
  368. public Rectangle2D getStringBounds(String str, Graphics context)
  369. {
  370. return font.getStringBounds(str, getFontRenderContext(context));
  371. }
  372. public Rectangle2D getStringBounds(String str, int beginIndex, int limit,
  373. Graphics context)
  374. {
  375. return font.getStringBounds(str, beginIndex, limit,
  376. getFontRenderContext(context));
  377. }
  378. public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit,
  379. Graphics context)
  380. {
  381. return font.getStringBounds(chars, beginIndex, limit,
  382. getFontRenderContext(context));
  383. }
  384. public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex,
  385. int limit, Graphics context)
  386. {
  387. return font.getStringBounds(ci, beginIndex, limit,
  388. getFontRenderContext(context));
  389. }
  390. private FontRenderContext getFontRenderContext(Graphics context)
  391. {
  392. if (context instanceof Graphics2D)
  393. return ((Graphics2D) context).getFontRenderContext();
  394. return gRC;
  395. }
  396. /**
  397. * Returns if the font has uniform line metrics.
  398. * @see Font#hasUniformLineMetrics()
  399. */
  400. public boolean hasUniformLineMetrics()
  401. {
  402. return font.hasUniformLineMetrics();
  403. }
  404. }