KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > FontRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 /**
26  * Title: Font Record - descrbes a font in the workbook (index = 0-3,5-infinity - skip 4)<P>
27  * Description: An element in the Font Table<P>
28  * REFERENCE: PG 315 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
29  * @author Andrew C. Oliver (acoliver at apache dot org)
30  * @version 2.0-pre
31  */

32
33 public class FontRecord
34     extends Record
35 {
36     public final static short sid =
37         0x31; // docs are wrong (0x231 Microsoft Support site article Q184647)
38
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; // in units of .05 of a point
47
private short field_2_attributes;
48
49     // 0 0x01 - Reserved bit must be 0
50
static final private BitField italic =
51         new BitField(0x02); // is this font in italics
52

53     // 2 0x04 - reserved bit must be 0
54
static final private BitField strikeout =
55         new BitField(0x08); // is this font has a line through the center
56
static final private BitField macoutline = new BitField(
57         0x10); // some weird macintosh thing....but who understands those mac people anyhow
58
static final private BitField macshadow = new BitField(
59         0x20); // some weird macintosh thing....but who understands those mac people anyhow
60

61     // 7-6 - reserved bits must be 0
62
// the rest is unused
63
private short field_3_color_palette_index;
64     private short field_4_bold_weight;
65     private short field_5_super_sub_script; // 00none/01super/02sub
66
private byte field_6_underline; // 00none/01single/02double/21singleaccounting/22doubleaccounting
67
private byte field_7_family; // ?? defined by windows api logfont structure?
68
private byte field_8_charset; // ?? defined by windows api logfont structure?
69
private byte field_9_zero = 0; // must be 0
70
private byte field_10_font_name_len; // length of the font name
71
private String JavaDoc field_11_font_name; // whoa...the font name
72

73     public FontRecord()
74     {
75     }
76
77     /**
78      * Constructs a Font record and sets its fields appropriately.
79      *
80      * @param id id must be 0x31 (NOT 0x231 see MSKB #Q184647 for an "explanation of
81      * this bug in the documentation) or an exception will be throw upon validation
82      * @param size the size of the data area of the record
83      * @param data data of the record (should not contain sid/len)
84      */

85
86     public FontRecord(short id, short size, byte [] data)
87     {
88         super(id, size, data);
89     }
90
91     /**
92      * Constructs a Font record and sets its fields appropriately.
93      *
94      * @param id id must be 0x31 (NOT 0x231 see MSKB #Q184647 for an "explanation of
95      * this bug in the documentation) or an exception will be throw upon validation
96      * @param size the size of the data area of the record
97      * @param data data of the record (should not contain sid/len)
98      * @param offset of the record's data
99      */

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             { // is compressed unicode
130
field_11_font_name = StringUtil.getFromCompressedUnicode(data, 16,
131                                                 LittleEndian.ubyteToInt(field_10_font_name_len));
132             }
133             else
134             { // is not compressed unicode
135
field_11_font_name = StringUtil.getFromUnicodeLE(data, 16,
136                         field_10_font_name_len);
137             }
138         }
139     }
140
141     /**
142      * sets the height of the font in 1/20th point units
143      *
144      * @param height fontheight (in points/20)
145      */

146
147     public void setFontHeight(short height)
148     {
149         field_1_font_height = height;
150     }
151
152     /**
153      * set the font attributes (see individual bit setters that reference this method)
154      *
155      * @param attributes the bitmask to set
156      */

157
158     public void setAttributes(short attributes)
159     {
160         field_2_attributes = attributes;
161     }
162
163     // attributes bitfields
164

165     /**
166      * set the font to be italics or not
167      *
168      * @param italics - whether the font is italics or not
169      * @see #setAttributes(short)
170      */

171
172     public void setItalic(boolean italics)
173     {
174         field_2_attributes = italic.setShortBoolean(field_2_attributes, italics);
175     }
176
177     /**
178      * set the font to be stricken out or not
179      *
180      * @param strike - whether the font is stricken out or not
181      * @see #setAttributes(short)
182      */

183
184     public void setStrikeout(boolean strike)
185     {
186         field_2_attributes = strikeout.setShortBoolean(field_2_attributes, strike);
187     }
188
189     /**
190      * whether to use the mac outline font style thing (mac only) - Some mac person
191      * should comment this instead of me doing it (since I have no idea)
192      *
193      * @param mac - whether to do that mac font outline thing or not
194      * @see #setAttributes(short)
195      */

196
197     public void setMacoutline(boolean mac)
198     {
199         field_2_attributes = macoutline.setShortBoolean(field_2_attributes, mac);
200     }
201
202     /**
203      * whether to use the mac shado font style thing (mac only) - Some mac person
204      * should comment this instead of me doing it (since I have no idea)
205      *
206      * @param mac - whether to do that mac font shadow thing or not
207      * @see #setAttributes(short)
208      */

209
210     public void setMacshadow(boolean mac)
211     {
212         field_2_attributes = macshadow.setShortBoolean(field_2_attributes, mac);
213     }
214
215     /**
216      * set the font's color palette index
217      *
218      * @param cpi - font color index
219      */

220
221     public void setColorPaletteIndex(short cpi)
222     {
223         field_3_color_palette_index = cpi;
224     }
225
226     /**
227      * set the bold weight for this font (100-1000dec or 0x64-0x3e8). Default is
228      * 0x190 for normal and 0x2bc for bold
229      *
230      * @param bw - a number between 100-1000 for the fonts "boldness"
231      */

232
233     public void setBoldWeight(short bw)
234     {
235         field_4_bold_weight = bw;
236     }
237
238     /**
239      * set the type of super or subscript for the font
240      *
241      * @param sss super or subscript option
242      * @see #SS_NONE
243      * @see #SS_SUPER
244      * @see #SS_SUB
245      */

246
247     public void setSuperSubScript(short sss)
248     {
249         field_5_super_sub_script = sss;
250     }
251
252     /**
253      * set the type of underlining for the font
254      *
255      * @param u super or subscript option
256      *
257      * @see #U_NONE
258      * @see #U_SINGLE
259      * @see #U_DOUBLE
260      * @see #U_SINGLE_ACCOUNTING
261      * @see #U_DOUBLE_ACCOUNTING
262      */

263
264     public void setUnderline(byte u)
265     {
266         field_6_underline = u;
267     }
268
269     /**
270      * set the font family (TODO)
271      *
272      * @param f family
273      */

274
275     public void setFamily(byte f)
276     {
277         field_7_family = f;
278     }
279
280     /**
281      * set the character set
282      *
283      * @param charset - characterset
284      */

285
286     public void setCharset(byte charset)
287     {
288         field_8_charset = charset;
289     }
290
291     /**
292      * set the length of the fontname string
293      *
294      * @param len length of the font name
295      * @see #setFontName(String)
296      */

297
298     public void setFontNameLength(byte len)
299     {
300         field_10_font_name_len = len;
301     }
302
303     /**
304      * set the name of the font
305      *
306      * @param fn - name of the font (i.e. "Arial")
307      */

308
309     public void setFontName(String JavaDoc fn)
310     {
311         field_11_font_name = fn;
312     }
313
314     /**
315      * gets the height of the font in 1/20th point units
316      *
317      * @return fontheight (in points/20)
318      */

319
320     public short getFontHeight()
321     {
322         return field_1_font_height;
323     }
324
325     /**
326      * get the font attributes (see individual bit getters that reference this method)
327      *
328      * @return attribute - the bitmask
329      */

330
331     public short getAttributes()
332     {
333         return field_2_attributes;
334     }
335
336     /**
337      * get whether the font is to be italics or not
338      *
339      * @return italics - whether the font is italics or not
340      * @see #getAttributes()
341      */

342
343     public boolean isItalic()
344     {
345         return italic.isSet(field_2_attributes);
346     }
347
348     /**
349      * get whether the font is to be stricken out or not
350      *
351      * @return strike - whether the font is stricken out or not
352      * @see #getAttributes()
353      */

354
355     public boolean isStruckout()
356     {
357         return strikeout.isSet(field_2_attributes);
358     }
359
360     /**
361      * whether to use the mac outline font style thing (mac only) - Some mac person
362      * should comment this instead of me doing it (since I have no idea)
363      *
364      * @return mac - whether to do that mac font outline thing or not
365      * @see #getAttributes()
366      */

367
368     public boolean isMacoutlined()
369     {
370         return macoutline.isSet(field_2_attributes);
371     }
372
373     /**
374      * whether to use the mac shado font style thing (mac only) - Some mac person
375      * should comment this instead of me doing it (since I have no idea)
376      *
377      * @return mac - whether to do that mac font shadow thing or not
378      * @see #getAttributes()
379      */

380
381     public boolean isMacshadowed()
382     {
383         return macshadow.isSet(field_2_attributes);
384     }
385
386     /**
387      * get the font's color palette index
388      *
389      * @return cpi - font color index
390      */

391
392     public short getColorPaletteIndex()
393     {
394         return field_3_color_palette_index;
395     }
396
397     /**
398      * get the bold weight for this font (100-1000dec or 0x64-0x3e8). Default is
399      * 0x190 for normal and 0x2bc for bold
400      *
401      * @return bw - a number between 100-1000 for the fonts "boldness"
402      */

403
404     public short getBoldWeight()
405     {
406         return field_4_bold_weight;
407     }
408
409     /**
410      * get the type of super or subscript for the font
411      *
412      * @return super or subscript option
413      * @see #SS_NONE
414      * @see #SS_SUPER
415      * @see #SS_SUB
416      */

417
418     public short getSuperSubScript()
419     {
420         return field_5_super_sub_script;
421     }
422
423     /**
424      * get the type of underlining for the font
425      *
426      * @return super or subscript option
427      *
428      * @see #U_NONE
429      * @see #U_SINGLE
430      * @see #U_DOUBLE
431      * @see #U_SINGLE_ACCOUNTING
432      * @see #U_DOUBLE_ACCOUNTING
433      */

434
435     public byte getUnderline()
436     {
437         return field_6_underline;
438     }
439
440     /**
441      * get the font family (TODO)
442      *
443      * @return family
444      */

445
446     public byte getFamily()
447     {
448         return field_7_family;
449     }
450
451     /**
452      * get the character set
453      *
454      * @return charset - characterset
455      */

456
457     public byte getCharset()
458     {
459         return field_8_charset;
460     }
461
462     /**
463      * get the length of the fontname string
464      *
465      * @return length of the font name
466      * @see #getFontName()
467      */

468
469     public byte getFontNameLength()
470     {
471         return field_10_font_name_len;
472     }
473
474     /**
475      * get the name of the font
476      *
477      * @return fn - name of the font (i.e. "Arial")
478      */

479
480     public String JavaDoc getFontName()
481     {
482         return field_11_font_name;
483     }
484
485     public String JavaDoc toString()
486     {
487         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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)); // 19 - 4 (sid/len) + font name length = datasize
531

532         // undocumented single byte (1)
533
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