KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > FontRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

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 /**
33  * A record containing the necessary data for the font information
34  */

35 public class FontRecord extends WritableRecordData implements Font
36 {
37   /**
38    * The logger
39    */

40   private static Logger logger = Logger.getLogger(FontRecord.class);
41
42   /**
43    * The point height of this font
44    */

45   private int pointHeight;
46   /**
47    * The index into the colour palette
48    */

49   private int colourIndex;
50   /**
51    * The bold weight for this font (normal or bold)
52    */

53   private int boldWeight;
54   /**
55    * The style of the script (italic or normal)
56    */

57   private int scriptStyle;
58   /**
59    * The underline style for this font (none, single, double etc)
60    */

61   private int underlineStyle;
62   /**
63    * The font family
64    */

65   private byte fontFamily;
66   /**
67    * The character set
68    */

69   private byte characterSet;
70
71   /**
72    * Indicates whether or not this font is italic
73    */

74   private boolean italic;
75   /**
76    * Indicates whether or not this font is struck out
77    */

78   private boolean struckout;
79   /**
80    * The name of this font
81    */

82   private String JavaDoc name;
83   /**
84    * Flag to indicate whether the derived data (such as the font index) has
85    * been initialized or not
86    */

87   private boolean initialized;
88
89   /**
90    * The index of this font in the font list
91    */

92   private int fontIndex;
93
94   /**
95    * Dummy indicators for overloading the constructor
96    */

97   private static class Biff7 {};
98   public static final Biff7 biff7 = new Biff7();
99
100   /**
101    * The conversion factor between microsoft internal units and point size
102    */

103   private static final int EXCEL_UNITS_PER_POINT = 20;
104
105   /**
106    * Constructor, used when creating a new font for writing out.
107    *
108    * @param bold the bold indicator
109    * @param ps the point size
110    * @param us the underline style
111    * @param fn the name
112    * @param it italicised indicator
113    * @param ss the script style
114    * @param ci the colour index
115    */

116   protected FontRecord(String JavaDoc 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   /**
132    * Constructs this object from the raw data. Used when reading in a
133    * format record
134    *
135    * @param t the raw data
136    * @param ws the workbook settings
137    */

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       // Some font names don't have the unicode indicator
176
name = StringHelper.getString(data, numChars, 15, ws);
177     }
178   }
179
180   /**
181    * Constructs this object from the raw data. Used when reading in a
182    * format record
183    *
184    * @param t the raw data
185    * @param ws the workbook settings
186    * @param dummy dummy overload
187    */

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   /**
218    * Publicly available copy constructor
219    *
220    * @param f the font to copy
221    */

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   /**
240    * Gets the byte data for writing out
241    *
242    * @return the raw data
243    */

244   public byte[] getData()
245   {
246     byte[] data = new byte[16 + name.length() * 2];
247
248     // Excel expects font heights in 1/20ths of a point
249
IntegerHelper.getTwoBytes(pointHeight * EXCEL_UNITS_PER_POINT, data, 0);
250
251     // Set the font attributes to be zero for now
252
if (italic)
253     {
254       data[2] |= 0x2;
255     }
256
257     if (struckout)
258     {
259       data[2] |= 0x08;
260     }
261
262     // Set the index to the colour palette
263
IntegerHelper.getTwoBytes(colourIndex, data, 4);
264
265     // Bold style
266
IntegerHelper.getTwoBytes(boldWeight, data, 6);
267
268     // Script style
269
IntegerHelper.getTwoBytes(scriptStyle, data, 8);
270
271     // Underline style
272
data[10] = (byte) underlineStyle;
273
274     // Set the font family to be 0
275
data[11] = fontFamily;
276
277     // Set the character set to be zero
278
data[12] = characterSet;
279
280     // Set the reserved bit to be zero
281
data[13] = 0;
282
283     // Set the length of the font name
284
data[14] = (byte) name.length();
285
286     data[15] = (byte) 1;
287
288     // Copy in the string
289
StringHelper.getUnicodeBytes(name, data, 16);
290
291     return data;
292   }
293
294   /**
295    * Accessor to see whether this object is initialized or not.
296    *
297    * @return TRUE if this font record has been initialized, FALSE otherwise
298    */

299   public final boolean isInitialized()
300   {
301     return initialized;
302   }
303
304   /**
305    * Sets the font index of this record. Called from the FormattingRecords
306    * object
307    *
308    * @param pos the position of this font in the workbooks font list
309    */

310   public final void initialize(int pos)
311   {
312     fontIndex = pos;
313     initialized = true;
314   }
315
316   /**
317    * Resets the initialize flag. This is called by the constructor of
318    * WritableWorkbookImpl to reset the statically declared fonts
319    */

320   public final void uninitialize()
321   {
322     initialized = false;
323   }
324
325   /**
326    * Accessor for the font index
327    *
328    * @return the font index
329    */

330   public final int getFontIndex()
331   {
332     return fontIndex;
333   }
334
335   /**
336    * Sets the point size for this font, if the font hasn't been initialized
337    *
338    * @param ps the point size
339    */

340   protected void setFontPointSize(int ps)
341   {
342     Assert.verify(!initialized);
343
344     pointHeight = ps;
345   }
346
347   /**
348    * Gets the point size for this font, if the font hasn't been initialized
349    *
350    * @return the point size
351    */

352   public int getPointSize()
353   {
354     return pointHeight;
355   }
356
357   /**
358    * Sets the bold style for this font, if the font hasn't been initialized
359    *
360    * @param bs the bold style
361    */

362   protected void setFontBoldStyle(int bs)
363   {
364     Assert.verify(!initialized);
365
366     boldWeight = bs;
367   }
368
369   /**
370    * Gets the bold weight for this font
371    *
372    * @return the bold weight for this font
373    */

374   public int getBoldWeight()
375   {
376     return boldWeight;
377   }
378
379   /**
380    * Sets the italic indicator for this font, if the font hasn't been
381    * initialized
382    *
383    * @param i the italic flag
384    */

385   protected void setFontItalic(boolean i)
386   {
387     Assert.verify(!initialized);
388
389     italic = i;
390   }
391
392   /**
393    * Returns the italic flag
394    *
395    * @return TRUE if this font is italic, FALSE otherwise
396    */

397   public boolean isItalic()
398   {
399     return italic;
400   }
401
402   /**
403    * Sets the underline style for this font, if the font hasn't been
404    * initialized
405    *
406    * @param us the underline style
407    */

408   protected void setFontUnderlineStyle(int us)
409   {
410     Assert.verify(!initialized);
411
412     underlineStyle = us;
413   }
414
415   /**
416    * Gets the underline style for this font
417    *
418    * @return the underline style
419    */

420   public UnderlineStyle getUnderlineStyle()
421   {
422     return UnderlineStyle.getStyle(underlineStyle);
423   }
424
425   /**
426    * Sets the colour for this font, if the font hasn't been
427    * initialized
428    *
429    * @param c the colour
430    */

431   protected void setFontColour(int c)
432   {
433     Assert.verify(!initialized);
434
435     colourIndex = c;
436   }
437
438   /**
439    * Gets the colour for this font
440    *
441    * @return the colour
442    */

443   public Colour getColour()
444   {
445     return Colour.getInternalColour(colourIndex);
446   }
447
448   /**
449    * Sets the script style (eg. superscript, subscript) for this font,
450    * if the font hasn't been initialized
451    *
452    * @param ss the colour
453    */

454   protected void setFontScriptStyle(int ss)
455   {
456     Assert.verify(!initialized);
457
458     scriptStyle = ss;
459   }
460
461   /**
462    * Gets the script style
463    *
464    * @return the script style
465    */

466   public ScriptStyle getScriptStyle()
467   {
468     return ScriptStyle.getStyle(scriptStyle);
469   }
470
471   /**
472    * Gets the name of this font
473    *
474    * @return the name of this font
475    */

476   public String JavaDoc getName()
477   {
478     return name;
479   }
480
481   /**
482    * Standard hash code method
483    * @return the hash code for this object
484    */

485   public int hashCode()
486   {
487     return name.hashCode();
488   }
489
490   /**
491    * Standard equals method
492    * @param o the object to compare
493    * @return TRUE if the objects are equal, FALSE otherwise
494    */

495   public boolean equals(Object JavaDoc 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   /**
527    * Accessor for the strike out flag
528    *
529    * @return TRUE if this font is struck out, FALSE otherwise
530    */

531   public boolean isStruckout()
532   {
533     return struckout;
534   }
535
536   /**
537    * Sets the struck out flag
538    *
539    * @param os TRUE if the font is struck out, false otherwise
540    */

541   protected void setFontStruckout(boolean os)
542   {
543     struckout = os;
544   }
545 }
546
547
548
Popular Tags