001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.util.*;
008:
009: /**
010: * SFont is used to cache fonts.
011: *
012: * @author Robin Sharp
013: */
014:
015: public class SFont {
016: /**
017: * Style PLAIN
018: */
019: public static final int PLAIN = 0;
020: /**
021: * Style BOLD
022: */
023: public static final int BOLD = 1;
024: /**
025: * Style ITALIC
026: */
027: public static final int ITALIC = 2;
028: /**
029: * Style MONOSPACED
030: */
031: public static final int MONOSPACED = 4;
032: /**
033: * Style BIG
034: */
035: public static final int BIG = 8;
036:
037: /**
038: * Style SMALL
039: */
040: public static final int SMALL = 16;
041:
042: /**
043: * Style UNDERLINED
044: */
045: public static final int UNDERLINED = 32;
046:
047: /**
048: * Style HEADING1
049: */
050: public static final int HEADING1 = 64;
051:
052: /**
053: * Style STRIKE
054: */
055: public static final int STRIKE = 128;
056:
057: /**
058: * Get a font with the format "name:style:size" (String,int,int)
059: */
060: public synchronized static SFont getFont(String font)
061: throws IllegalArgumentException {
062: try {
063: return getFont(font.substring(0, font.indexOf(':')),
064: Integer.parseInt(font.substring(
065: font.indexOf(':') + 1, font
066: .lastIndexOf(':'))), Integer
067: .parseInt(font.substring(font
068: .lastIndexOf(':') + 1)));
069: } catch (Exception e) {
070: throw new IllegalArgumentException(
071: "Cannot parse font string " + font);
072: }
073: }
074:
075: /**
076: * Get a cached font
077: */
078: public synchronized static SFont getFont(String fontName,
079: int style, int size) {
080: if (fontName == null)
081: fontName = "";
082:
083: Integer key = new Integer(fontName.hashCode() ^ style ^ size);
084:
085: SFont font = (SFont) fonts.get(key);
086:
087: if (font == null) {
088: font = new SFont(fontName, style, size);
089: fonts.put(key, font);
090: }
091:
092: return font;
093: }
094:
095: /**
096: * Construct a fully qualified SFont.
097: */
098: protected SFont(String name, int style, int size) {
099: this .name = name;
100: this .style = style;
101: this .size = size;
102: }
103:
104: /**
105: * Get the Name.
106: */
107: public String getName() {
108: return name;
109: }
110:
111: /**
112: * Get the SFont Style.
113: */
114: public int getStyle() {
115: return style;
116: }
117:
118: /**
119: * Get the SFont Size.
120: */
121: public int getSize() {
122: return size;
123: }
124:
125: public boolean isPlain() {
126: return style == 0;
127: }
128:
129: public boolean isBold() {
130: return (style & BOLD) != 0;
131: }
132:
133: public boolean isItalic() {
134: return (style & ITALIC) != 0;
135: }
136:
137: public boolean isMonoSpaced() {
138: return (style & MONOSPACED) != 0;
139: }
140:
141: public boolean isBig() {
142: return (style & BIG) != 0;
143: }
144:
145: public boolean isSmall() {
146: return (style & SMALL) != 0;
147: }
148:
149: public boolean isUnderlined() {
150: return (style & UNDERLINED) != 0;
151: }
152:
153: public boolean isHeading1() {
154: return (style & HEADING1) != 0;
155: }
156:
157: public boolean isStrike() {
158: return (style & STRIKE) != 0;
159: }
160:
161: protected String name;
162: protected int style;
163: protected int size;
164: protected int phrase;
165:
166: protected static Hashtable fonts = new Hashtable();
167: }
|