1:
37:
38:
39: package ;
40:
41:
46: public class FontSize
47: {
48:
49:
52: private String value;
53:
54:
57: private int size;
58:
59:
65: private int sizeIndex;
66:
67:
70: private boolean isRelative;
71:
72:
76: public static final int DEFAULT_FONT_SIZE = 12;
77:
78:
81: private static final double[] SCALE = {0.8, 0.9, 1.0, 1.2, 1.4, 1.6, 1.8 };
82:
83:
88: public FontSize(String val)
89: {
90: value = val;
91: sizeIndex = -1;
92: isRelative = false;
93: size = mapValue();
94: }
95:
96:
101: public int getValue(int p)
102: {
103: if (isRelative)
104: mapRelative(p);
105: return size;
106: }
107:
108: public int getValue()
109: {
110: assert ! isRelative;
111: return size;
112: }
113:
114:
119: private int mapValue()
120: {
121: int intVal;
122: if (value.contains("pt"))
123: intVal = mapPoints();
124: else if (value.contains("px"))
125: intVal = mapPixels();
126: else if (value.contains("em") || value.contains("%")
127: || value.contains("larger") || value.contains("smaller"))
128: {
129: intVal = -1;
130: isRelative = true;
131: }
132: else
133: intVal = mapAbsolute();
134: return intVal;
135: }
136:
137:
142: private int mapPoints()
143: {
144: int end = value.indexOf("pt");
145: String number = value.substring(0, end);
146: int intVal = (int) Double.parseDouble(number);
147: return intVal;
148: }
149:
150:
155: private int mapPixels()
156: {
157: int end = value.indexOf("px");
158: if (end == -1)
159: end = value.length();
160: String number = value.substring(0, end);
161: try
162: {
163: int intVal = (int) Double.parseDouble(number);
164: return intVal;
165: }
166: catch (NumberFormatException ex)
167: {
168: return DEFAULT_FONT_SIZE;
169: }
170: }
171:
172: private int mapPercent(int par)
173: {
174: int end = value.indexOf("%");
175: if (end == -1)
176: end = value.length();
177: String number = value.substring(0, end);
178: try
179: {
180: int intVal = (int) Double.parseDouble(number);
181: return intVal * par / 100;
182: }
183: catch (NumberFormatException ex)
184: {
185: System.err.println("couldn't map value: '" + value + "'");
186: return DEFAULT_FONT_SIZE;
187: }
188: }
189:
190: private int mapEM(int par)
191: {
192: int end = value.indexOf("em");
193: if (end == -1)
194: end = value.length();
195: String number = value.substring(0, end);
196: try
197: {
198: float factor = Float.parseFloat(number);
199:
200: return (int) (factor * par);
201: }
202: catch (NumberFormatException ex)
203: {
204: return DEFAULT_FONT_SIZE;
205: }
206: }
207:
208: private int mapSmaller(int par)
209: {
210: return (int) (par * 0.9);
211: }
212:
213: private int mapLarger(int par)
214: {
215: return (int) (par * 0.9);
216: }
217:
218:
223: private int mapAbsolute()
224: {
225: int index;
226: if (value.equals("xx-small") || value.equals("x-small"))
227: index = 0;
228: else if (value.equals("small"))
229: index = 1;
230: else if (value.equals("medium"))
231: index = 2;
232: else if (value.equals("large"))
233: index = 3;
234: else if (value.equals("x-large"))
235: index = 4;
236: else if (value.equals("xx-large"))
237: index = 5;
238: else
239: index = 2;
240: double scale = SCALE[index];
241:
242:
243: int intVal = (int) (scale * DEFAULT_FONT_SIZE);
244: sizeIndex = index;
245: return intVal;
246: }
247:
248:
251: public String toString()
252: {
253: return value;
254: }
255:
256: private int mapRelative(int par)
257: {
258: if (value.indexOf('%') != -1)
259: size = mapPercent(par);
260: else if (value.indexOf("em") != -1)
261: size = mapEM(par);
262: else if (value.indexOf("larger") != -1)
263: size = mapLarger(par);
264: else if (value.indexOf("smaller") != -1)
265: size = mapSmaller(par);
266: return size;
267: }
268:
269: public boolean isRelative()
270: {
271: return isRelative;
272: }
273: }