Font.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Font.java -- Font object
  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. import java.awt.peer.FontPeer;
  33. import java.util.StringTokenizer;
  34. /**
  35. * This class represents a windowing system font.
  36. *
  37. * @author Aaron M. Renn (arenn@urbanophile.com)
  38. * @author Warren Levy <warrenl@cygnus.com>
  39. */
  40. public class Font implements java.io.Serializable
  41. {
  42. /*
  43. * Static Variables
  44. */
  45. /**
  46. * Constant indicating a "plain" font.
  47. */
  48. public static final int PLAIN = 0;
  49. /**
  50. * Constant indicating a "bold" font.
  51. */
  52. public static final int BOLD = 1;
  53. /**
  54. * Constant indicating an "italic" font.
  55. */
  56. public static final int ITALIC = 2;
  57. public static final int ROMAN_BASELINE = 0;
  58. public static final int CENTER_BASELINE = 1;
  59. public static final int HANGING_BASELINE = 2;
  60. /**
  61. * Indicates to <code>createFont</code> that the supplied font data
  62. * is in TrueType format.
  63. *
  64. * <p><em>Specification Note:</em> The Sun JavaDoc for J2SE 1.4 does
  65. * not indicate whether this value also subsumes OpenType. OpenType
  66. * is essentially the same format as TrueType, but allows to define
  67. * glyph shapes in the same way as PostScript, using cubic bezier
  68. * curves.
  69. *
  70. * @since 1.3
  71. */
  72. public static final int TRUETYPE_FONT = 0;
  73. /**
  74. * A flag for <code>layoutGlyphVector</code>, indicating that the
  75. * orientation of a text run is from left to right.
  76. *
  77. * @since 1.4
  78. */
  79. public static final int LAYOUT_LEFT_TO_RIGHT = 0;
  80. /**
  81. * A flag for <code>layoutGlyphVector</code>, indicating that the
  82. * orientation of a text run is from right to left.
  83. *
  84. * @since 1.4
  85. */
  86. public static final int LAYOUT_RIGHT_TO_LEFT = 1;
  87. /**
  88. * A flag for <code>layoutGlyphVector</code>, indicating that the
  89. * text does not contain valid characters before the
  90. * <code>start</code> position. If this flag is set,
  91. * <code>layoutGlyphVector</code> does not examine the text before
  92. * <code>start</code>, even if this would be necessary to select the
  93. * correct glyphs (e.g., for Arabic text).
  94. *
  95. * @since 1.4
  96. */
  97. public static final int LAYOUT_NO_START_CONTEXT = 2;
  98. /**
  99. * A flag for <code>layoutGlyphVector</code>, indicating that the
  100. * text does not contain valid characters after the
  101. * <code>limit</code> position. If this flag is set,
  102. * <code>layoutGlyphVector</code> does not examine the text after
  103. * <code>limit</code>, even if this would be necessary to select the
  104. * correct glyphs (e.g., for Arabic text).
  105. *
  106. * @since 1.4
  107. */
  108. public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
  109. // Serialization constant
  110. private static final long serialVersionUID = -4206021311591459213L;
  111. /*************************************************************************/
  112. /*
  113. * Instance Variables
  114. */
  115. /**
  116. * The name of this font
  117. */
  118. protected String name;
  119. /**
  120. * The font style, which is a combination (by summing, not OR-ing) of
  121. * the font style constants in this class.
  122. */
  123. protected int style;
  124. /**
  125. * The font point size.
  126. */
  127. protected int size;
  128. protected float pointSize;
  129. // The native peer for this font
  130. private FontPeer peer;
  131. /*************************************************************************/
  132. /*
  133. * Static Methods
  134. */
  135. /**
  136. * Creates a <code>Font</code> object from the specified string, which
  137. * is in one of the following formats:
  138. * <p>
  139. * <ul>
  140. * <li>fontname-style-pointsize
  141. * <li>fontname-style
  142. * <li>fontname-pointsize
  143. * <li>fontname
  144. * </ul>
  145. * <p>
  146. * The style should be one of BOLD, ITALIC, or BOLDITALIC. The default
  147. * style if none is specified is PLAIN. The default size if none
  148. * is specified is 12.
  149. */
  150. public static Font
  151. decode(String fontspec)
  152. {
  153. String name = null;
  154. int style = PLAIN;
  155. int size = 12;
  156. StringTokenizer st = new StringTokenizer(fontspec, "-");
  157. while (st.hasMoreTokens())
  158. {
  159. String token = st.nextToken();
  160. if (name == null)
  161. {
  162. name = token;
  163. continue;
  164. }
  165. if (token.toUpperCase().equals("BOLD"))
  166. {
  167. style = BOLD;
  168. continue;
  169. }
  170. if (token.toUpperCase().equals("ITALIC"))
  171. {
  172. style = ITALIC;
  173. continue;
  174. }
  175. if (token.toUpperCase().equals("BOLDITALIC"))
  176. {
  177. style = BOLD + ITALIC;
  178. continue;
  179. }
  180. int tokenval = 0;
  181. try
  182. {
  183. tokenval = Integer.parseInt(token);
  184. }
  185. catch(NumberFormatException e) { ; }
  186. if (tokenval != 0)
  187. size = tokenval;
  188. }
  189. return(new Font(name, style, size));
  190. }
  191. /*************************************************************************/
  192. /**
  193. * Returns a <code>Font</code> object from the passed property name.
  194. *
  195. * @param propname The name of the system property.
  196. * @param default Value to use if the property is not found.
  197. *
  198. * @return The requested font, or <code>default</code> if the property
  199. * not exist or is malformed.
  200. */
  201. public static Font
  202. getFont(String propname, Font defval)
  203. {
  204. String propval = System.getProperty(propname);
  205. if (propval != null)
  206. return(decode(propval));
  207. return(defval);
  208. }
  209. /*************************************************************************/
  210. /**
  211. * Returns a <code>Font</code> object from the passed property name.
  212. *
  213. * @param propname The name of the system property.
  214. *
  215. * @return The requested font, or <code>null</code> if the property
  216. * not exist or is malformed.
  217. */
  218. public static Font
  219. getFont(String propname)
  220. {
  221. return(getFont(propname, null));
  222. }
  223. /*************************************************************************/
  224. /*
  225. * Constructors
  226. */
  227. /**
  228. * Initializes a new instance of <code>Font</code> with the specified
  229. * attributes.
  230. *
  231. * @param name The name of the font.
  232. * @param style The font style.
  233. * @param size The font point size.
  234. */
  235. public
  236. Font(String name, int style, int size)
  237. {
  238. this.name = name;
  239. this.style = style;
  240. this.size = size;
  241. this.pointSize = size;
  242. }
  243. /*************************************************************************/
  244. /*
  245. * Instance Methods
  246. */
  247. /**
  248. * Returns the name of the font.
  249. *
  250. * @return The name of the font.
  251. */
  252. public String
  253. getName()
  254. {
  255. return(name);
  256. }
  257. /*************************************************************************/
  258. /**
  259. * Returns the style of the font.
  260. *
  261. * @return The font style.
  262. */
  263. public int
  264. getSize()
  265. {
  266. return(size);
  267. }
  268. public float
  269. getSize2D()
  270. {
  271. return pointSize;
  272. }
  273. /*************************************************************************/
  274. /**
  275. * Tests whether or not this is a plain font. This will be true if
  276. * and only if neither the bold nor the italics style is set.
  277. *
  278. * @return <code>true</code> if this is a plain font, <code>false</code>
  279. * otherwise.
  280. */
  281. public boolean
  282. isPlain()
  283. {
  284. if (style == PLAIN)
  285. return(true);
  286. else
  287. return(false);
  288. }
  289. /*************************************************************************/
  290. /**
  291. * Tests whether or not this font is bold.
  292. *
  293. * @return <code>true</code> if this font is bold, <code>false</code>
  294. * otherwise.
  295. */
  296. public boolean
  297. isBold()
  298. {
  299. if ((style == BOLD) || (style == (BOLD+ITALIC)))
  300. return(true);
  301. else
  302. return(false);
  303. }
  304. /*************************************************************************/
  305. /**
  306. * Tests whether or not this font is italic.
  307. *
  308. * @return <code>true</code> if this font is italic, <code>false</code>
  309. * otherwise.
  310. */
  311. public boolean
  312. isItalic()
  313. {
  314. if ((style == ITALIC) || (style == (BOLD+ITALIC)))
  315. return(true);
  316. else
  317. return(false);
  318. }
  319. /*************************************************************************/
  320. /**
  321. * Returns the system specific font family name.
  322. *
  323. * @return The system specific font family name.
  324. */
  325. public String
  326. getFamily()
  327. {
  328. // FIXME: How do I implement this?
  329. return(name);
  330. }
  331. public int
  332. getStyle()
  333. {
  334. return style;
  335. }
  336. /*************************************************************************/
  337. /**
  338. * Returns a native peer object for this font.
  339. *
  340. * @return A native peer object for this font.
  341. */
  342. public FontPeer
  343. getPeer()
  344. {
  345. if (peer != null)
  346. return(peer);
  347. peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
  348. return(peer);
  349. }
  350. /*************************************************************************/
  351. /**
  352. * Returns a hash value for this font.
  353. *
  354. * @return A hash for this font.
  355. */
  356. public int
  357. hashCode()
  358. {
  359. return((new String(name + size + style)).hashCode());
  360. }
  361. /*************************************************************************/
  362. /**
  363. * Tests whether or not the specified object is equal to this font. This
  364. * will be true if and only if:
  365. * <P>
  366. * <ul>
  367. * <li>The object is not <code>null</code>.
  368. * <li>The object is an instance of <code>Font</code>.
  369. * <li>The object has the same name, style, and size as this object.
  370. * </ul>
  371. *
  372. * @return <code>true</code> if the specified object is equal to this
  373. * object, <code>false</code> otherwise.
  374. */
  375. public boolean
  376. equals(Object obj)
  377. {
  378. if (obj == null)
  379. return(false);
  380. if (!(obj instanceof Font))
  381. return(false);
  382. Font f = (Font)obj;
  383. if (!f.name.equals(name))
  384. return(false);
  385. if (f.size != size)
  386. return(false);
  387. if (f.style != style)
  388. return(false);
  389. return(true);
  390. }
  391. /*************************************************************************/
  392. /**
  393. * Returns a string representation of this font.
  394. *
  395. * @return A string representation of this font.
  396. */
  397. public String
  398. toString()
  399. {
  400. return(getClass().getName() + "(name=" + name + ",style=" + style +
  401. ",size=" + size + ")");
  402. }
  403. } // class Font