FontMetrics.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* FontMetrics.java -- Information about about a fonts display characteristics
  2. Copyright (C) 1999, 2002 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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. // FIXME: I leave many methods basically unimplemented. This
  33. // should be reviewed.
  34. /**
  35. * This class returns information about the display characteristics of
  36. * a font. It is abstract, and concrete subclasses should implement at
  37. * least the following methods:
  38. * <p>
  39. * <ul>
  40. * <li>getAscent
  41. * <li>getDescent
  42. * <li>getLeading()
  43. * <li>getMaxAdvance()
  44. * <li>charWidth(char)
  45. * <li>charsWidth(char[], int, int)
  46. * </ul>
  47. *
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. */
  50. public abstract class FontMetrics implements java.io.Serializable
  51. {
  52. /*
  53. * Static Variables
  54. */
  55. // Serialization constant
  56. private static final long serialVersionUID = 1681126225205050147L;
  57. /*************************************************************************/
  58. /*
  59. * Instance Variables
  60. */
  61. /**
  62. * This is the font for which metrics will be returned.
  63. */
  64. protected Font font;
  65. /*************************************************************************/
  66. /*
  67. * Constructors
  68. */
  69. /**
  70. * Initializes a new instance of <code>FontMetrics</code> for the
  71. * specified font.
  72. *
  73. * @param font The font to return metric information for.
  74. */
  75. protected
  76. FontMetrics(Font font)
  77. {
  78. this.font = font;
  79. }
  80. /*************************************************************************/
  81. /*
  82. * Instance Methods
  83. */
  84. /**
  85. * Returns the font that this object is creating metric information fo.
  86. *
  87. * @return The font for this object.
  88. */
  89. public Font
  90. getFont()
  91. {
  92. return(font);
  93. }
  94. /*************************************************************************/
  95. /**
  96. * Returns the leading, or spacing between lines, for this font.
  97. *
  98. * @return The font leading.
  99. */
  100. public int
  101. getLeading()
  102. {
  103. return(0);
  104. }
  105. /*************************************************************************/
  106. /**
  107. * Returns the ascent of the font, which is the distance from the base
  108. * to the top of the majority of characters in the set. Some characters
  109. * can exceed this value however.
  110. *
  111. * @return The font ascent.
  112. */
  113. public int
  114. getAscent()
  115. {
  116. return(1);
  117. }
  118. /*************************************************************************/
  119. /**
  120. * Returns the descent of the font, which is the distance from the base
  121. * to the bottom of the majority of characters in the set. Some characters
  122. * can exceed this value however.
  123. *
  124. * @return The font descent.
  125. */
  126. public int
  127. getDescent()
  128. {
  129. return(1);
  130. }
  131. /*************************************************************************/
  132. /**
  133. * Returns the height of a line in this font. This will be the sum
  134. * of the leading, the ascent, and the descent.
  135. *
  136. * @return The height of the font.
  137. */
  138. public int
  139. getHeight()
  140. {
  141. return(getAscent() + getDescent() + getLeading());
  142. }
  143. /*************************************************************************/
  144. /**
  145. * Returns the maximum ascent value. This is the maximum distance any
  146. * character in the font rised above the baseline.
  147. *
  148. * @return The maximum ascent for this font.
  149. */
  150. public int
  151. getMaxAscent()
  152. {
  153. return(getAscent());
  154. }
  155. /*************************************************************************/
  156. /**
  157. * Returns the maximum descent value. This is the maximum distance any
  158. * character in the font extends below the baseline.
  159. *
  160. * @return The maximum descent for this font.
  161. */
  162. public int
  163. getMaxDescent()
  164. {
  165. return(getDescent());
  166. }
  167. /*************************************************************************/
  168. /**
  169. * Returns the maximum descent value. This is the maximum distance any
  170. * character in the font extends below the baseline.
  171. *
  172. * @return The maximum descent for this font.
  173. *
  174. * @deprecated This method is deprecated in favor of
  175. * <code>getMaxDescent()</code>.
  176. */
  177. public int
  178. getMaxDecent()
  179. {
  180. return(getMaxDescent());
  181. }
  182. /*************************************************************************/
  183. /**
  184. * Returns the width of the widest character in the font.
  185. *
  186. * @return The width of the widest character in the font.
  187. */
  188. public int
  189. getMaxAdvance()
  190. {
  191. return(-1);
  192. }
  193. /*************************************************************************/
  194. /**
  195. * Returns the width of the specified character.
  196. *
  197. * @param ch The character to return the width of.
  198. *
  199. * @return The width of the specified character.
  200. */
  201. public int
  202. charWidth(int ch)
  203. {
  204. return(charWidth((char)ch));
  205. }
  206. /*************************************************************************/
  207. /**
  208. * Returns the width of the specified character.
  209. *
  210. * @param ch The character to return the width of.
  211. *
  212. * @return The width of the specified character.
  213. */
  214. public int
  215. charWidth(char ch)
  216. {
  217. return(1);
  218. }
  219. /*************************************************************************/
  220. /**
  221. * Returns the total width of the specified string
  222. *
  223. * @param str The string to return the width of.
  224. *
  225. * @return The width of the string.
  226. */
  227. public int
  228. stringWidth(String str)
  229. {
  230. char[] buf = new char[str.length()];
  231. str.getChars(0, str.length(), buf, 0);
  232. return(charsWidth(buf, 0, buf.length));
  233. }
  234. /*************************************************************************/
  235. /**
  236. * Returns the total width of the specified character array.
  237. *
  238. * @param buf The character array containing the data.
  239. * @param offset The offset into the array to start calculating from.
  240. * @param len The total number of bytes to process.
  241. *
  242. * @return The width of the requested characters.
  243. */
  244. public int
  245. charsWidth(char buf[], int offset, int len)
  246. {
  247. int total_width = 0;
  248. for (int i = offset; i < len; i++)
  249. total_width = charWidth(buf[i]);
  250. return(total_width);
  251. }
  252. /*************************************************************************/
  253. /**
  254. * Returns the total width of the specified byte array.
  255. *
  256. * @param buf The byte array containing the data.
  257. * @param offset The offset into the array to start calculating from.
  258. * @param len The total number of bytes to process.
  259. *
  260. * @return The width of the requested characters.
  261. */
  262. public int
  263. bytesWidth(byte buf[], int offset, int len)
  264. {
  265. int total_width = 0;
  266. for (int i = offset; i < len; i++)
  267. total_width = charWidth((char)buf[i]);
  268. return(total_width);
  269. }
  270. /*************************************************************************/
  271. /**
  272. * Returns the widths of the first 256 characters in the font.
  273. *
  274. * @return The widths of the first 256 characters in the font.
  275. */
  276. public int[]
  277. getWidths()
  278. {
  279. return(new int[256]);
  280. }
  281. /*************************************************************************/
  282. /**
  283. * Returns a string representation of this object.
  284. *
  285. * @return A string representation of this object.
  286. */
  287. public String
  288. toString()
  289. {
  290. return (this.getClass() + "[font=" + font + ",ascent=" + getAscent()
  291. + ",descent=" + getDescent() + ",height=" + getHeight() + "]");
  292. }
  293. } // class FontMetrics