1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import org.openlaszlo.utils.ChainedException; 13 import java.io.Serializable ; 14 import java.util.*; 15 16 17 22 public class FontInfo implements java.io.Serializable { 23 24 public static final String NULL_FONT = null; 25 public static final int NULL_SIZE = -1; 26 public static final int NULL_STYLE = -1; 27 28 29 public final static int PLAIN = 0x0; 30 31 public final static int BOLD = 0x1; 32 33 public final static int ITALIC = 0x2; 34 35 public final static int BOLDITALIC = 0x3; 36 37 38 private String mName = null; 39 40 private int mSize; 41 42 43 private int mWidth = NULL_SIZE; 44 private int mHeight = NULL_SIZE; 45 46 47 public int styleBits = 0; 48 49 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 public int resizable = FONTINFO_NULL; 60 61 public int multiline = FONTINFO_NULL; 63 64 public String toString() { 65 return "FontInfo: name="+mName+", size="+mSize+", style="+getStyle(); 66 } 67 68 public String toStringLong() { 69 return "FontInfo: name="+mName+", size="+mSize+", style="+getStyle()+", width="+mWidth+", height="+mHeight+", resizable="+resizable+", multiline="+multiline; 70 } 71 72 73 79 public FontInfo(String name, String sz, String 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 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 name, int sz, int st) { 98 mName = name; 99 mSize = sz; 100 styleBits = st; 101 } 102 103 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 129 public void setName(String f) { 130 mName = f; 131 } 132 133 137 public void setStyle(String st) { 138 styleBits = styleBitsFromString(st); 139 } 140 141 145 public void setStyleBits(int st) { 146 styleBits = st; 147 } 148 149 153 public void setSize(String sz) { 154 mSize = Integer.parseInt(sz); 155 } 156 157 161 public void setSize(int sz) { 162 mSize = sz; 163 } 164 165 168 public int getSize() { 169 return mSize; 170 } 171 172 175 public int getStyleBits() { 176 return styleBits; 177 } 178 179 182 public String 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 193 public boolean isFullySpecified () { 194 return ((mSize != NULL_SIZE) && 195 (styleBits != NULL_STYLE) && 196 (mName != NULL_FONT) && 197 (mName.charAt(0) != '$')); 199 } 200 201 202 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 243 public final String getStyle() { 244 return styleBitsToString(styleBits); 245 } 246 247 250 public final String getStyle(boolean whitespace) { 251 return styleBitsToString(styleBits, whitespace); 252 } 253 254 255 261 public static String 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 ("Unknown style " + styleBits); 279 } 280 } 281 282 287 public static String styleBitsToString(int styleBits) { 288 return styleBitsToString(styleBits, true); 289 } 290 291 295 public static int styleBitsFromString(String name) { 296 int style = PLAIN; 297 if (name != null) { 298 StringTokenizer st = new StringTokenizer(name); 299 while (st.hasMoreTokens()) { 300 String 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 324 public static String normalizeStyleString(String style, boolean whitespace) { 325 return styleBitsToString(styleBitsFromString(style), whitespace); 326 } 327 } 328 | Popular Tags |