1 19 20 package jxl.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.WorkbookSettings; 26 import jxl.read.biff.Record; 27 import jxl.format.Font; 28 import jxl.format.UnderlineStyle; 29 import jxl.format.ScriptStyle; 30 import jxl.format.Colour; 31 32 35 public class FontRecord extends WritableRecordData implements Font 36 { 37 40 private static Logger logger = Logger.getLogger(FontRecord.class); 41 42 45 private int pointHeight; 46 49 private int colourIndex; 50 53 private int boldWeight; 54 57 private int scriptStyle; 58 61 private int underlineStyle; 62 65 private byte fontFamily; 66 69 private byte characterSet; 70 71 74 private boolean italic; 75 78 private boolean struckout; 79 82 private String name; 83 87 private boolean initialized; 88 89 92 private int fontIndex; 93 94 97 private static class Biff7 {}; 98 public static final Biff7 biff7 = new Biff7(); 99 100 103 private static final int EXCEL_UNITS_PER_POINT = 20; 104 105 116 protected FontRecord(String fn, int ps, int bold, boolean it, 117 int us, int ci, int ss) 118 { 119 super(Type.FONT); 120 boldWeight = bold; 121 underlineStyle = us; 122 name = fn; 123 pointHeight = ps; 124 italic = it; 125 scriptStyle = ss; 126 colourIndex = ci; 127 initialized = false; 128 struckout = false; 129 } 130 131 138 public FontRecord(Record t, WorkbookSettings ws) 139 { 140 super(t); 141 142 byte[] data = getRecord().getData(); 143 144 pointHeight = IntegerHelper.getInt(data[0], data[1]) / 145 EXCEL_UNITS_PER_POINT; 146 colourIndex = IntegerHelper.getInt(data[4], data[5]); 147 boldWeight = IntegerHelper.getInt(data[6], data[7]); 148 scriptStyle = IntegerHelper.getInt(data[8], data[9]); 149 underlineStyle = data[10]; 150 fontFamily = data[11]; 151 characterSet = data[12]; 152 initialized = false; 153 154 if ((data[2] & 0x02) != 0) 155 { 156 italic = true; 157 } 158 159 if ((data[2] & 0x08) != 0) 160 { 161 struckout = true; 162 } 163 164 int numChars = data[14]; 165 if (data[15] == 0) 166 { 167 name = StringHelper.getString(data, numChars, 16, ws); 168 } 169 else if (data[15] == 1) 170 { 171 name = StringHelper.getUnicodeString(data, numChars, 16); 172 } 173 else 174 { 175 name = StringHelper.getString(data, numChars, 15, ws); 177 } 178 } 179 180 188 public FontRecord(Record t, WorkbookSettings ws, Biff7 dummy) 189 { 190 super(t); 191 192 byte[] data = getRecord().getData(); 193 194 pointHeight = IntegerHelper.getInt(data[0], data[1]) / 195 EXCEL_UNITS_PER_POINT; 196 colourIndex = IntegerHelper.getInt(data[4], data[5]); 197 boldWeight = IntegerHelper.getInt(data[6], data[7]); 198 scriptStyle = IntegerHelper.getInt(data[8], data[9]); 199 underlineStyle = data[10]; 200 fontFamily = data[11]; 201 initialized = false; 202 203 if ((data[2] & 0x02) != 0) 204 { 205 italic = true; 206 } 207 208 if ((data[2] & 0x08) != 0) 209 { 210 struckout = true; 211 } 212 213 int numChars = data[14]; 214 name = StringHelper.getString(data, numChars, 15, ws); 215 } 216 217 222 protected FontRecord(Font f) 223 { 224 super(Type.FONT); 225 226 Assert.verify(f != null); 227 228 pointHeight = f.getPointSize(); 229 colourIndex = f.getColour().getValue(); 230 boldWeight = f.getBoldWeight(); 231 scriptStyle = f.getScriptStyle().getValue(); 232 underlineStyle = f.getUnderlineStyle().getValue(); 233 italic = f.isItalic(); 234 name = f.getName(); 235 struckout = false; 236 initialized = false; 237 } 238 239 244 public byte[] getData() 245 { 246 byte[] data = new byte[16 + name.length() * 2]; 247 248 IntegerHelper.getTwoBytes(pointHeight * EXCEL_UNITS_PER_POINT, data, 0); 250 251 if (italic) 253 { 254 data[2] |= 0x2; 255 } 256 257 if (struckout) 258 { 259 data[2] |= 0x08; 260 } 261 262 IntegerHelper.getTwoBytes(colourIndex, data, 4); 264 265 IntegerHelper.getTwoBytes(boldWeight, data, 6); 267 268 IntegerHelper.getTwoBytes(scriptStyle, data, 8); 270 271 data[10] = (byte) underlineStyle; 273 274 data[11] = fontFamily; 276 277 data[12] = characterSet; 279 280 data[13] = 0; 282 283 data[14] = (byte) name.length(); 285 286 data[15] = (byte) 1; 287 288 StringHelper.getUnicodeBytes(name, data, 16); 290 291 return data; 292 } 293 294 299 public final boolean isInitialized() 300 { 301 return initialized; 302 } 303 304 310 public final void initialize(int pos) 311 { 312 fontIndex = pos; 313 initialized = true; 314 } 315 316 320 public final void uninitialize() 321 { 322 initialized = false; 323 } 324 325 330 public final int getFontIndex() 331 { 332 return fontIndex; 333 } 334 335 340 protected void setFontPointSize(int ps) 341 { 342 Assert.verify(!initialized); 343 344 pointHeight = ps; 345 } 346 347 352 public int getPointSize() 353 { 354 return pointHeight; 355 } 356 357 362 protected void setFontBoldStyle(int bs) 363 { 364 Assert.verify(!initialized); 365 366 boldWeight = bs; 367 } 368 369 374 public int getBoldWeight() 375 { 376 return boldWeight; 377 } 378 379 385 protected void setFontItalic(boolean i) 386 { 387 Assert.verify(!initialized); 388 389 italic = i; 390 } 391 392 397 public boolean isItalic() 398 { 399 return italic; 400 } 401 402 408 protected void setFontUnderlineStyle(int us) 409 { 410 Assert.verify(!initialized); 411 412 underlineStyle = us; 413 } 414 415 420 public UnderlineStyle getUnderlineStyle() 421 { 422 return UnderlineStyle.getStyle(underlineStyle); 423 } 424 425 431 protected void setFontColour(int c) 432 { 433 Assert.verify(!initialized); 434 435 colourIndex = c; 436 } 437 438 443 public Colour getColour() 444 { 445 return Colour.getInternalColour(colourIndex); 446 } 447 448 454 protected void setFontScriptStyle(int ss) 455 { 456 Assert.verify(!initialized); 457 458 scriptStyle = ss; 459 } 460 461 466 public ScriptStyle getScriptStyle() 467 { 468 return ScriptStyle.getStyle(scriptStyle); 469 } 470 471 476 public String getName() 477 { 478 return name; 479 } 480 481 485 public int hashCode() 486 { 487 return name.hashCode(); 488 } 489 490 495 public boolean equals(Object o) 496 { 497 if (o == this) 498 { 499 return true; 500 } 501 502 if (!(o instanceof FontRecord)) 503 { 504 return false; 505 } 506 507 FontRecord font = (FontRecord) o; 508 509 if (pointHeight == font.pointHeight && 510 colourIndex == font.colourIndex && 511 boldWeight == font.boldWeight && 512 scriptStyle == font.scriptStyle && 513 underlineStyle == font.underlineStyle && 514 italic == font.italic && 515 struckout == font.struckout && 516 fontFamily == font.fontFamily && 517 characterSet == font.characterSet && 518 name.equals(font.name)) 519 { 520 return true; 521 } 522 523 return false; 524 } 525 526 531 public boolean isStruckout() 532 { 533 return struckout; 534 } 535 536 541 protected void setFontStruckout(boolean os) 542 { 543 struckout = os; 544 } 545 } 546 547 548 | Popular Tags |