001: /*
002: * @(#)Font.java 1.10 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: import java.security.AccessController;
030: import sun.security.action.GetPropertyAction;
031:
032: /**
033: * A class that produces font objects.
034: *
035: * @version 1.6, 08/19/02
036: * @author Nicholas Allen
037: * @since JDK1.0
038: */
039: public class Font implements java.io.Serializable {
040: /*
041: * Constants to be used for styles. Can be combined to mix
042: * styles.
043: */
044:
045: /**
046: * The plain style constant. This style can be combined with
047: * the other style constants for mixed styles.
048: * @since JDK1.0
049: */
050: public static final int PLAIN = 0;
051: /**
052: * The bold style constant. This style can be combined with the
053: * other style constants for mixed styles.
054: * @since JDK1.0
055: */
056: public static final int BOLD = 1;
057: /**
058: * The italicized style constant. This style can be combined
059: * with the other style constants for mixed styles.
060: * @since JDK1.0
061: */
062: public static final int ITALIC = 2;
063: /**
064: * The platform specific family name of this font.
065: */
066: transient private String family;
067: /**
068: * The logical name of this font.
069: * @since JDK1.0
070: */
071: protected String name;
072: /**
073: * The style of the font. This is the sum of the
074: * constants <code>PLAIN</code>, <code>BOLD</code>,
075: * or <code>ITALIC</code>.
076: */
077: protected int style;
078: /**
079: * The point size of this font.
080: * @since JDK1.0
081: */
082: protected int size;
083: /*
084: * JDK 1.1 serialVersionUID
085: */
086: private static final long serialVersionUID = -4206021311591459213L;
087: private transient X11FontMetrics metrics;
088:
089: /**
090: * Creates a new font with the specified name, style and point size.
091: * @param name the font name
092: * @param style the constant style used
093: * @param size the point size of the font
094: * @see Toolkit#getFontList
095: * @since JDK1.0
096: */
097: public Font(String name, int style, int size) {
098: this .name = name;
099: this .style = style;
100: this .size = size;
101: }
102:
103: X11FontMetrics getX11FontMetrics() {
104: if (metrics == null)
105: metrics = X11FontMetrics.getFontMetrics(this );
106: return metrics;
107: }
108:
109: /**
110: * Gets the platform specific family name of the font. Use the
111: * <code>getName</code> method to get the logical name of the font.
112: * @return a string, the platform specific family name.
113: * @see java.awt.Font#getName
114: * @since JDK1.0
115: */
116: public String getFamily() {
117: return family;
118: }
119:
120: /**
121: * Gets the logical name of the font.
122: * @return a string, the logical name of the font.
123: * @see #getFamily
124: * @since JDK1.0
125: */
126: public String getName() {
127: return name;
128: }
129:
130: /**
131: * Gets the style of the font.
132: * @return the style of this font.
133: * @see #isPlain
134: * @see #isBold
135: * @see #isItalic
136: * @since JDK1.0
137: */
138: public int getStyle() {
139: return style;
140: }
141:
142: /**
143: * Gets the point size of the font.
144: * @return the point size of this font.
145: * @since JDK1.0
146: */
147: public int getSize() {
148: return size;
149: }
150:
151: /**
152: * Indicates whether the font's style is plain.
153: * @return <code>true</code> if the font is neither
154: * bold nor italic; <code>false</code> otherwise.
155: * @see java.awt.Font#getStyle
156: * @since JDK1.0
157: */
158: public boolean isPlain() {
159: return style == 0;
160: }
161:
162: /**
163: * Indicates whether the font's style is bold.
164: * @return <code>true</code> if the font is bold;
165: * <code>false</code> otherwise.
166: * @see java.awt.Font#getStyle
167: * @since JDK1.0
168: */
169: public boolean isBold() {
170: return (style & BOLD) != 0;
171: }
172:
173: /**
174: * Indicates whether the font's style is italic.
175: * @return <code>true</code> if the font is italic;
176: * <code>false</code> otherwise.
177: * @see java.awt.Font#getStyle
178: * @since JDK1.0
179: */
180: public boolean isItalic() {
181: return (style & ITALIC) != 0;
182: }
183:
184: /**
185: * Gets a font from the system properties list.
186: * @param nm the property name
187: * @see java.awt.Font#getFont(java.lang.String, java.awt.Font)
188: * @since JDK1.0
189: */
190: public static Font getFont(String nm) {
191: return getFont(nm, null);
192: }
193:
194: /**
195: * Gets the specified font using the name passed in.
196: * @param str the name
197: * @since JDK1.1
198: */
199: public static Font decode(String str) {
200: String fontName = str;
201: int fontSize = 12;
202: int fontStyle = Font.PLAIN;
203: int i = str.indexOf('-');
204: if (i >= 0) {
205: fontName = str.substring(0, i);
206: str = str.substring(i + 1);
207: if ((i = str.indexOf('-')) >= 0) {
208: if (str.startsWith("bold-")) {
209: fontStyle = Font.BOLD;
210: } else if (str.startsWith("italic-")) {
211: fontStyle = Font.ITALIC;
212: } else if (str.startsWith("bolditalic-")) {
213: fontStyle = Font.BOLD | Font.ITALIC;
214: }
215: str = str.substring(i + 1);
216: }
217: try {
218: fontSize = Integer.valueOf(str).intValue();
219: } catch (NumberFormatException e) {
220: }
221: }
222: return new Font(fontName, fontStyle, fontSize);
223: }
224:
225: /**
226: * Gets the specified font from the system properties list.
227: * The first argument is treated as the name of a system property to
228: * be obtained as if by the method <code>System.getProperty</code>.
229: * The string value of this property is then interpreted as a font.
230: * <p>
231: * The property value should be one of the following forms:
232: * <ul>
233: * <li><em>fontname-style-pointsize</em>
234: * <li><em>fontname-pointsize</em>
235: * <li><em>fontname-style</em>
236: * <li><em>fontname</em>
237: * </ul>
238: * where <i>style</i> is one of the three strings
239: * <code>"BOLD"</code>, <code>"BOLDITALIC"</code>, or
240: * <code>"ITALIC"</code>, and point size is a decimal
241: * representation of the point size.
242: * <p>
243: * The default style is <code>PLAIN</code>. The default point size
244: * is 12.
245: * <p>
246: * If the specified property is not found, the <code>font</code>
247: * argument is returned instead.
248: * @param nm the property name
249: * @param font a default font to return if property <code>nm</code>
250: * is not defined
251: * @return the <code>Font</code> value of the property.
252: * @since JDK1.0
253: */
254: public static Font getFont(String nm, Font font) {
255: String str = System.getProperty(nm);
256: if (str == null) {
257: return font;
258: }
259: return Font.decode(str);
260: }
261:
262: /**
263: * Returns a hashcode for this font.
264: * @return a hashcode value for this font.
265: * @since JDK1.0
266: */
267: public int hashCode() {
268: return name.hashCode() ^ style ^ size;
269: }
270:
271: /**
272: * Compares this object to the specifed object.
273: * The result is <code>true</code> if and only if the argument is not
274: * <code>null</code> and is a <code>Font</code> object with the same
275: * name, style, and point size as this font.
276: * @param obj the object to compare this font with.
277: * @return <code>true</code> if the objects are equal;
278: * <code>false</code> otherwise.
279: * @since JDK1.0
280: */
281: public boolean equals(Object obj) {
282: if (obj instanceof Font) {
283: Font font = (Font) obj;
284: return (size == font.size) && (style == font.style)
285: && name.equals(font.name);
286: }
287: return false;
288: }
289:
290: /**
291: * Converts this object to a String representation.
292: * @return a string representation of this object
293: * @since JDK1.0
294: */
295: public String toString() {
296: String strStyle;
297: if (isBold()) {
298: strStyle = isItalic() ? "bolditalic" : "bold";
299: } else {
300: strStyle = isItalic() ? "italic" : "plain";
301: }
302: return getClass().getName() + "[family=" + family + ",name="
303: + name + ",style=" + strStyle + ",size=" + size + "]";
304: }
305:
306: /* Serialization support. A readObject method is neccessary because
307: * the constructor creates the fonts peer, and we can't serialize the
308: * peer. Similarly the computed font "family" may be different
309: * at readObject time than at writeObject time. An integer version is
310: * written so that future versions of this class will be able to recognize
311: * serialized output from this one.
312: */
313:
314: private int fontSerializedDataVersion = 1;
315:
316: private void writeObject(java.io.ObjectOutputStream s)
317: throws java.lang.ClassNotFoundException,
318: java.io.IOException {
319: s.defaultWriteObject();
320: }
321:
322: private void readObject(java.io.ObjectInputStream s)
323: throws java.lang.ClassNotFoundException,
324: java.io.IOException {
325: s.defaultReadObject();
326: // initializeFont();
327: }
328: }
|