1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndian; 22 import org.apache.poi.util.StringUtil; 23 import org.apache.poi.util.BitField; 24 25 32 33 public class FontRecord 34 extends Record 35 { 36 public final static short sid = 37 0x31; public final static short SS_NONE = 0; 39 public final static short SS_SUPER = 1; 40 public final static short SS_SUB = 2; 41 public final static byte U_NONE = 0; 42 public final static byte U_SINGLE = 1; 43 public final static byte U_DOUBLE = 2; 44 public final static byte U_SINGLE_ACCOUNTING = 0x21; 45 public final static byte U_DOUBLE_ACCOUNTING = 0x22; 46 private short field_1_font_height; private short field_2_attributes; 48 49 static final private BitField italic = 51 new BitField(0x02); 53 static final private BitField strikeout = 55 new BitField(0x08); static final private BitField macoutline = new BitField( 57 0x10); static final private BitField macshadow = new BitField( 59 0x20); 61 private short field_3_color_palette_index; 64 private short field_4_bold_weight; 65 private short field_5_super_sub_script; private byte field_6_underline; private byte field_7_family; private byte field_8_charset; private byte field_9_zero = 0; private byte field_10_font_name_len; private String field_11_font_name; 73 public FontRecord() 74 { 75 } 76 77 85 86 public FontRecord(short id, short size, byte [] data) 87 { 88 super(id, size, data); 89 } 90 91 100 101 public FontRecord(short id, short size, byte [] data, int offset) 102 { 103 super(id, size, data, offset); 104 } 105 106 protected void validateSid(short id) 107 { 108 if (id != sid) 109 { 110 throw new RecordFormatException("NOT A FONT RECORD"); 111 } 112 } 113 114 protected void fillFields(byte [] data, short size, int offset) 115 { 116 field_1_font_height = LittleEndian.getShort(data, 0 + offset); 117 field_2_attributes = LittleEndian.getShort(data, 2 + offset); 118 field_3_color_palette_index = LittleEndian.getShort(data, 4 + offset); 119 field_4_bold_weight = LittleEndian.getShort(data, 6 + offset); 120 field_5_super_sub_script = LittleEndian.getShort(data, 8 + offset); 121 field_6_underline = data[ 10 + offset ]; 122 field_7_family = data[ 11 + offset ]; 123 field_8_charset = data[ 12 + offset ]; 124 field_9_zero = data[ 13 + offset ]; 125 field_10_font_name_len = data[ 14 + offset ]; 126 if (field_10_font_name_len > 0) 127 { 128 if (data[ 15 ] == 0) 129 { field_11_font_name = StringUtil.getFromCompressedUnicode(data, 16, 131 LittleEndian.ubyteToInt(field_10_font_name_len)); 132 } 133 else 134 { field_11_font_name = StringUtil.getFromUnicodeLE(data, 16, 136 field_10_font_name_len); 137 } 138 } 139 } 140 141 146 147 public void setFontHeight(short height) 148 { 149 field_1_font_height = height; 150 } 151 152 157 158 public void setAttributes(short attributes) 159 { 160 field_2_attributes = attributes; 161 } 162 163 165 171 172 public void setItalic(boolean italics) 173 { 174 field_2_attributes = italic.setShortBoolean(field_2_attributes, italics); 175 } 176 177 183 184 public void setStrikeout(boolean strike) 185 { 186 field_2_attributes = strikeout.setShortBoolean(field_2_attributes, strike); 187 } 188 189 196 197 public void setMacoutline(boolean mac) 198 { 199 field_2_attributes = macoutline.setShortBoolean(field_2_attributes, mac); 200 } 201 202 209 210 public void setMacshadow(boolean mac) 211 { 212 field_2_attributes = macshadow.setShortBoolean(field_2_attributes, mac); 213 } 214 215 220 221 public void setColorPaletteIndex(short cpi) 222 { 223 field_3_color_palette_index = cpi; 224 } 225 226 232 233 public void setBoldWeight(short bw) 234 { 235 field_4_bold_weight = bw; 236 } 237 238 246 247 public void setSuperSubScript(short sss) 248 { 249 field_5_super_sub_script = sss; 250 } 251 252 263 264 public void setUnderline(byte u) 265 { 266 field_6_underline = u; 267 } 268 269 274 275 public void setFamily(byte f) 276 { 277 field_7_family = f; 278 } 279 280 285 286 public void setCharset(byte charset) 287 { 288 field_8_charset = charset; 289 } 290 291 297 298 public void setFontNameLength(byte len) 299 { 300 field_10_font_name_len = len; 301 } 302 303 308 309 public void setFontName(String fn) 310 { 311 field_11_font_name = fn; 312 } 313 314 319 320 public short getFontHeight() 321 { 322 return field_1_font_height; 323 } 324 325 330 331 public short getAttributes() 332 { 333 return field_2_attributes; 334 } 335 336 342 343 public boolean isItalic() 344 { 345 return italic.isSet(field_2_attributes); 346 } 347 348 354 355 public boolean isStruckout() 356 { 357 return strikeout.isSet(field_2_attributes); 358 } 359 360 367 368 public boolean isMacoutlined() 369 { 370 return macoutline.isSet(field_2_attributes); 371 } 372 373 380 381 public boolean isMacshadowed() 382 { 383 return macshadow.isSet(field_2_attributes); 384 } 385 386 391 392 public short getColorPaletteIndex() 393 { 394 return field_3_color_palette_index; 395 } 396 397 403 404 public short getBoldWeight() 405 { 406 return field_4_bold_weight; 407 } 408 409 417 418 public short getSuperSubScript() 419 { 420 return field_5_super_sub_script; 421 } 422 423 434 435 public byte getUnderline() 436 { 437 return field_6_underline; 438 } 439 440 445 446 public byte getFamily() 447 { 448 return field_7_family; 449 } 450 451 456 457 public byte getCharset() 458 { 459 return field_8_charset; 460 } 461 462 468 469 public byte getFontNameLength() 470 { 471 return field_10_font_name_len; 472 } 473 474 479 480 public String getFontName() 481 { 482 return field_11_font_name; 483 } 484 485 public String toString() 486 { 487 StringBuffer buffer = new StringBuffer (); 488 489 buffer.append("[FONT]\n"); 490 buffer.append(" .fontheight = ") 491 .append(Integer.toHexString(getFontHeight())).append("\n"); 492 buffer.append(" .attributes = ") 493 .append(Integer.toHexString(getAttributes())).append("\n"); 494 buffer.append(" .italic = ").append(isItalic()) 495 .append("\n"); 496 buffer.append(" .strikout = ").append(isStruckout()) 497 .append("\n"); 498 buffer.append(" .macoutlined= ").append(isMacoutlined()) 499 .append("\n"); 500 buffer.append(" .macshadowed= ").append(isMacshadowed()) 501 .append("\n"); 502 buffer.append(" .colorpalette = ") 503 .append(Integer.toHexString(getColorPaletteIndex())).append("\n"); 504 buffer.append(" .boldweight = ") 505 .append(Integer.toHexString(getBoldWeight())).append("\n"); 506 buffer.append(" .supersubscript = ") 507 .append(Integer.toHexString(getSuperSubScript())).append("\n"); 508 buffer.append(" .underline = ") 509 .append(Integer.toHexString(getUnderline())).append("\n"); 510 buffer.append(" .family = ") 511 .append(Integer.toHexString(getFamily())).append("\n"); 512 buffer.append(" .charset = ") 513 .append(Integer.toHexString(getCharset())).append("\n"); 514 buffer.append(" .namelength = ") 515 .append(Integer.toHexString(getFontNameLength())).append("\n"); 516 buffer.append(" .fontname = ").append(getFontName()) 517 .append("\n"); 518 buffer.append("[/FONT]\n"); 519 return buffer.toString(); 520 } 521 522 public int serialize(int offset, byte [] data) 523 { 524 int realflen = getFontNameLength() * 2; 525 526 LittleEndian.putShort(data, 0 + offset, sid); 527 LittleEndian.putShort( 528 data, 2 + offset, 529 ( short ) (15 + realflen 530 + 1)); 532 LittleEndian.putShort(data, 4 + offset, getFontHeight()); 534 LittleEndian.putShort(data, 6 + offset, getAttributes()); 535 LittleEndian.putShort(data, 8 + offset, getColorPaletteIndex()); 536 LittleEndian.putShort(data, 10 + offset, getBoldWeight()); 537 LittleEndian.putShort(data, 12 + offset, getSuperSubScript()); 538 data[ 14 + offset ] = getUnderline(); 539 data[ 15 + offset ] = getFamily(); 540 data[ 16 + offset ] = getCharset(); 541 data[ 17 + offset ] = (( byte ) 0); 542 data[ 18 + offset ] = getFontNameLength(); 543 data[ 19 + offset ] = ( byte ) 1; 544 if (getFontName() != null) { 545 StringUtil.putUnicodeLE(getFontName(), data, 20 + offset); 546 } 547 return getRecordSize(); 548 } 549 550 public int getRecordSize() 551 { 552 return (getFontNameLength() * 2) + 20; 553 } 554 555 public short getSid() 556 { 557 return this.sid; 558 } 559 } 560 | Popular Tags |