KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > FontInfo


1 /* ****************************************************************************
2  * FontInfo.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11
12 import org.openlaszlo.utils.ChainedException;
13 import java.io.Serializable JavaDoc;
14 import java.util.*;
15
16
17 /**
18  * Font information used for measuring text.
19  *
20  * @author <a HREF="mailto:bloch@laszlosystems.com">Eric Bloch</a>
21  */

22 public class FontInfo implements java.io.Serializable JavaDoc {
23
24     public static final String JavaDoc NULL_FONT = null;
25     public static final int NULL_SIZE = -1;
26     public static final int NULL_STYLE = -1;
27
28     /** bit fields for text style */
29     public final static int PLAIN = 0x0;
30     /** bit fields for text style */
31     public final static int BOLD = 0x1;
32     /** bit fields for text style */
33     public final static int ITALIC = 0x2;
34     /** bit fields for text style */
35     public final static int BOLDITALIC = 0x3;
36
37     /** font face */
38     private String JavaDoc mName = null;
39     /** font size */
40     private int mSize;
41
42     /** used for compile-time width/height cascading */
43     private int mWidth = NULL_SIZE;
44     private int mHeight = NULL_SIZE;
45
46     /** font style bits */
47     public int styleBits = 0;
48
49     /** This can have three values:
50         -1 = unset
51         0 = false
52         1 = true
53     */

54     public final static int FONTINFO_NULL = -1;
55     public final static int FONTINFO_FALSE = 0;
56     public final static int FONTINFO_TRUE = 1;
57
58     // resizable defaults to false
59
public int resizable = FONTINFO_NULL;
60
61     // multiline default to false
62
public int multiline = FONTINFO_NULL;
63
64     public String JavaDoc toString() {
65         return "FontInfo: name="+mName+", size="+mSize+", style="+getStyle();
66     }
67
68     public String JavaDoc toStringLong() {
69         return "FontInfo: name="+mName+", size="+mSize+", style="+getStyle()+", width="+mWidth+", height="+mHeight+", resizable="+resizable+", multiline="+multiline;
70     }
71
72
73     /**
74      * Create a new FontInfo
75      * @param name name
76      * @param sz size
77      * @param st style
78      */

79     public FontInfo(String JavaDoc name, String JavaDoc sz, String JavaDoc st) {
80         mName = name;
81         setSize(sz);
82         setStyle(st);
83     }
84
85     public FontInfo(FontInfo i) {
86         this.mName = i.mName;
87         this.mSize = i.mSize;
88         this.styleBits = i.styleBits;
89
90         // static text optimization params
91
this.resizable = i.resizable;
92         this.multiline = i.multiline;
93         this.mWidth = i.getWidth();
94         this.mHeight = i.getHeight();
95     }
96
97     public FontInfo(String JavaDoc name, int sz, int st) {
98         mName = name;
99         mSize = sz;
100         styleBits = st;
101     }
102
103     /** Extra params for static textfield optimization.
104         Tracks width and height of a view.
105      */

106     public int getWidth() {
107         return mWidth;
108     }
109
110     public int getHeight() {
111         return mHeight;
112     }
113
114
115     public void setWidth (int w) {
116         mWidth = w;
117     }
118
119     public void setHeight (int w) {
120         mHeight = w;
121     }
122
123
124
125     /**
126      * Set the name
127      * @param f the name
128      */

129     public void setName(String JavaDoc f) {
130         mName = f;
131     }
132
133     /**
134      * Set the style
135      * @param st style
136      */

137     public void setStyle(String JavaDoc st) {
138         styleBits = styleBitsFromString(st);
139     }
140
141     /**
142      * Set the style bits directly
143      * @param st stylebits
144      */

145     public void setStyleBits(int st) {
146         styleBits = st;
147     }
148
149     /**
150      * Set the size
151      * @param sz the size
152      */

153     public void setSize(String JavaDoc sz) {
154         mSize = Integer.parseInt(sz);
155     }
156
157     /**
158      * Set the size
159      * @param sz the size
160      */

161     public void setSize(int sz) {
162         mSize = sz;
163     }
164
165     /**
166      * @return the size
167      */

168     public int getSize() {
169         return mSize;
170     }
171
172     /**
173      * @return the stylebits
174      */

175     public int getStyleBits() {
176         return styleBits;
177     }
178
179     /**
180      * @return the name
181      */

182     public String JavaDoc getName() {
183         return mName;
184     }
185
186     public static FontInfo blankFontInfo() {
187         FontInfo fi = new FontInfo(FontInfo.NULL_FONT, FontInfo.NULL_SIZE, FontInfo.NULL_STYLE);
188         return fi;
189     }
190
191     /** Does this font spec contain a known font name,size,style ?
192      */

193     public boolean isFullySpecified () {
194         return ((mSize != NULL_SIZE) &&
195                 (styleBits != NULL_STYLE) &&
196                 (mName != NULL_FONT) &&
197                 // we don't understand constraint expressions, just string literals
198
(mName.charAt(0) != '$'));
199     }
200
201
202     /** If OTHER has non-null fields, copy
203         them from OTHER to us.
204
205         null fields are indicated by:
206         name == null,
207         size == -1,
208         stylebits == -1,
209      */

210     public void mergeFontInfoFrom(FontInfo other) {
211         if (other.getSize()!= NULL_SIZE) {
212             mSize = other.getSize();
213         }
214
215         if (other.getStyleBits() != NULL_STYLE) {
216             styleBits = other.getStyleBits();
217         }
218
219         if (other.getName()!= NULL_FONT) {
220             mName = other.getName();
221         }
222
223         if (other.resizable != FONTINFO_NULL) {
224             resizable = other.resizable;
225         }
226
227         if (other.multiline != FONTINFO_NULL) {
228             multiline = other.multiline;
229         }
230
231         if (other.getWidth() != NULL_SIZE) {
232             mWidth = other.getWidth();
233         }
234
235         if (other.getHeight() != NULL_SIZE) {
236             mHeight = other.getHeight();
237         }
238     }
239
240     /**
241      * @return the name
242      */

243     public final String JavaDoc getStyle() {
244         return styleBitsToString(styleBits);
245     }
246
247     /**
248      * @return the name
249      */

250     public final String JavaDoc getStyle(boolean whitespace) {
251         return styleBitsToString(styleBits, whitespace);
252     }
253
254
255     /**
256      * Return the string representation of the style.
257      *
258      * @param styleBits an <code>int</code> encoding the style
259      * @param whitespace whether to separate style names by spaces; e.g. true for "bold italic", false for "bolditalic"
260      */

261     public static String JavaDoc styleBitsToString(int styleBits, boolean whitespace) {
262         switch (styleBits) {
263             case PLAIN:
264                 return "plain";
265             case BOLD:
266                 return "bold";
267             case ITALIC:
268                 return "italic";
269             case BOLDITALIC:
270                 if (whitespace) {
271                     return "bold italic";
272                 } else {
273                     return "bolditalic";
274                 }
275           case NULL_STYLE:
276             return "UNDEFINED STYLE";
277           default:
278                 throw new RuntimeException JavaDoc("Unknown style " + styleBits);
279         }
280     }
281
282     /**
283      * Return the string representation of the style, e.g. "bold italic".
284      *
285      * @param styleBits an <code>int</code> value
286      */

287     public static String JavaDoc styleBitsToString(int styleBits) {
288         return styleBitsToString(styleBits, true);
289     }
290     
291     /**
292     /**
293      * @return the bits for a style
294      */

295     public static int styleBitsFromString(String JavaDoc name) {
296         int style = PLAIN;
297         if (name != null) {
298             StringTokenizer st = new StringTokenizer(name);
299             while (st.hasMoreTokens()) {
300                 String JavaDoc token = st.nextToken();
301                 if (token.equals("bold")) {
302                     style |= BOLD;
303                 } else if (token.equals("italic")) {
304                     style |= ITALIC;
305                 } else if (token.equals("plain")) {
306                     style |= PLAIN;
307                 } else if (token.equals("bolditalic")) {
308                     style |= ITALIC | BOLD;
309                 } else {
310                     throw new CompilationError("Unknown style " + name);
311                 }
312             }
313         }
314         return style;
315     }
316
317     /**
318      * "bold italic", "italic bold" -> "bold italic" or "bolditalic"
319      * (depending on the value of <code>whitespace</code>
320      *
321      * @param style a <code>String</code> value
322      * @return a <code>String</code> value
323      */

324     public static String JavaDoc normalizeStyleString(String JavaDoc style, boolean whitespace) {
325         return styleBitsToString(styleBitsFromString(style), whitespace);
326     }
327 }
328
Popular Tags