1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * HSSFFont.java
58    *
59    * Created on December 9, 2001, 10:34 AM
60    */
61   package org.apache.poi.hssf.usermodel;
62   
63   import org.apache.poi.hssf.record.FontRecord;
64   
65   /**
66    * Represents a Font used in a workbook.
67    *
68    * @version 1.0-pre
69    * @author  Andrew C. Oliver
70    * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
71    * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
72    * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont)
73    */
74   
75   public class HSSFFont
76       implements HSSFColorConstants
77   {
78   
79       /**
80        * Arial font
81        */
82   
83       public final static String FONT_ARIAL          = "Arial";
84   
85       /**
86        * Normal boldness (not bold)
87        */
88   
89       public final static short  BOLDWEIGHT_NORMAL   = 190;
90   
91       /**
92        * Bold boldness (bold)
93        */
94   
95       public final static short  BOLDWEIGHT_BOLD     = 0x2bc;
96   
97       /**
98        * normal type of black color
99        */
100  
101      public final static short  COLOR_NORMAL        = 0x7fff;
102  
103      /**
104       * Dark Red color
105       */
106  
107      public final static short  COLOR_RED           = 0xa;
108  
109      /**
110       * no type offsetting (not super or subscript)
111       */
112  
113      public final static short  SS_NONE             = 0;
114  
115      /**
116       * superscript
117       */
118  
119      public final static short  SS_SUPER            = 1;
120  
121      /**
122       * subscript
123       */
124  
125      public final static short  SS_SUB              = 2;
126  
127      /**
128       * not underlined
129       */
130  
131      public final static byte   U_NONE              = 0;
132  
133      /**
134       * single (normal) underline
135       */
136  
137      public final static byte   U_SINGLE            = 1;
138  
139      /**
140       * double underlined
141       */
142  
143      public final static byte   U_DOUBLE            = 2;
144  
145      /**
146       * accounting style single underline
147       */
148  
149      public final static byte   U_SINGLE_ACCOUNTING = 0x21;
150  
151      /**
152       * accounting style double underline
153       */
154  
155      public final static byte   U_DOUBLE_ACCOUNTING = 0x22;
156      private FontRecord         font;
157      private short              index;
158  
159      /** Creates a new instance of HSSFFont */
160  
161      protected HSSFFont(short index, FontRecord rec)
162      {
163          font       = rec;
164          this.index = index;
165      }
166  
167      /**
168       * set the name for the font (i.e. Arial)
169       * @param String representing the name of the font to use
170       * @see #FONT_ARIAL
171       */
172  
173      public void setFontName(String name)
174      {
175          font.setFontName(name);
176          font.setFontNameLength(( byte ) name.length());
177      }
178  
179      /**
180       * get the name for the font (i.e. Arial)
181       * @return String representing the name of the font to use
182       * @see #FONT_ARIAL
183       */
184  
185      public String getFontName()
186      {
187          return font.getFontName();
188      }
189  
190      /**
191       * get the index within the HSSFWorkbook (sequence within the collection of Font objects)
192       * @return unique index number of the underlying record this Font represents (probably you don't care
193       *  unless you're comparing which one is which)
194       */
195  
196      public short getIndex()
197      {
198          return index;
199      }
200  
201      /**
202       * set the font height in unit's of 1/20th of a point.  Maybe you might want to
203       * use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
204       * @param short - height in 1/20ths of a point
205       * @see #setFontHeightInPoints(short)
206       */
207  
208      public void setFontHeight(short height)
209      {
210          font.setFontHeight(height);
211      }
212  
213      /**
214       * set the font height
215       * @param short - height in the familiar unit of measure - points
216       * @see #setFontHeight(short)
217       */
218  
219      public void setFontHeightInPoints(short height)
220      {
221          font.setFontHeight(( short ) (height * 20));
222      }
223  
224      /**
225       * get the font height in unit's of 1/20th of a point.  Maybe you might want to
226       * use the getFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
227       * @return short - height in 1/20ths of a point
228       * @see #getFontHeightInPoints()
229       */
230  
231      public short getFontHeight()
232      {
233          return font.getFontHeight();
234      }
235  
236      /**
237       * get the font height
238       * @return short - height in the familiar unit of measure - points
239       * @see #getFontHeight()
240       */
241  
242      public short getFontHeightInPoints()
243      {
244          return ( short ) (font.getFontHeight() / 20);
245      }
246  
247      /**
248       * set whether to use italics or not
249       * @param italics or not
250       */
251  
252      public void setItalic(boolean italic)
253      {
254          font.setItalic(italic);
255      }
256  
257      /**
258       * get whether to use italics or not
259       * @return italics or not
260       */
261  
262      public boolean getItalic()
263      {
264          return font.isItalic();
265      }
266  
267      /**
268       * set whether to use a strikeout horizontal line through the text or not
269       * @param strikeout or not
270       */
271  
272      public void setStrikeout(boolean strikeout)
273      {
274          font.setStrikeout(strikeout);
275      }
276  
277      /**
278       * get whether to use a strikeout horizontal line through the text or not
279       * @return strikeout or not
280       */
281  
282      public boolean getStrikeout()
283      {
284          return font.isStruckout();
285      }
286  
287      /**
288       * set the color for the font
289       * @param color to use
290       * @see #COLOR_NORMAL
291       * @see #COLOR_RED
292       */
293  
294      public void setColor(short color)
295      {
296          font.setColorPaletteIndex(color);
297      }
298  
299      /**
300       * get the color for the font
301       * @return color to use
302       * @see #COLOR_NORMAL
303       * @see #COLOR_RED
304       */
305  
306      public short getColor()
307      {
308          return font.getColorPaletteIndex();
309      }
310  
311      /**
312       * set the boldness to use
313       * @param boldweight
314       * @see #BOLDWEIGHT_NORMAL
315       * @see #BOLDWEIGHT_BOLD
316       */
317  
318      public void setBoldweight(short boldweight)
319      {
320          font.setBoldWeight(boldweight);
321      }
322  
323      /**
324       * get the boldness to use
325       * @return boldweight
326       * @see #BOLDWEIGHT_NORMAL
327       * @see #BOLDWEIGHT_BOLD
328       */
329  
330      public short getBoldweight()
331      {
332          return font.getBoldWeight();
333      }
334  
335      /**
336       * set normal,super or subscript.
337       * @param offset type to use (none,super,sub)
338       * @see #SS_NONE
339       * @see #SS_SUPER
340       * @see #SS_SUB
341       */
342  
343      public void setTypeOffset(short offset)
344      {
345          font.setSuperSubScript(offset);
346      }
347  
348      /**
349       * get normal,super or subscript.
350       * @return offset type to use (none,super,sub)
351       * @see #SS_NONE
352       * @see #SS_SUPER
353       * @see #SS_SUB
354       */
355  
356      public short getTypeOffset()
357      {
358          return font.getSuperSubScript();
359      }
360  
361      /**
362       * set type of text underlining to use
363       * @param underlining type
364       * @see #U_NONE
365       * @see #U_SINGLE
366       * @see #U_DOUBLE
367       * @see #U_SINGLE_ACCOUNTING
368       * @see #U_DOUBLE_ACCOUNTING
369       */
370  
371      public void setUnderline(byte underline)
372      {
373          font.setUnderline(underline);
374      }
375  
376      /**
377       * get type of text underlining to use
378       * @return underlining type
379       * @see #U_NONE
380       * @see #U_SINGLE
381       * @see #U_DOUBLE
382       * @see #U_SINGLE_ACCOUNTING
383       * @see #U_DOUBLE_ACCOUNTING
384       */
385  
386      public byte getUnderline()
387      {
388          return font.getUnderline();
389      }
390  }
391