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   {
77   
78       /**
79        * Arial font
80        */
81   
82       public final static String FONT_ARIAL          = "Arial";
83   
84       /**
85        * Normal boldness (not bold)
86        */
87   
88       public final static short  BOLDWEIGHT_NORMAL   = 190;
89   
90       /**
91        * Bold boldness (bold)
92        */
93   
94       public final static short  BOLDWEIGHT_BOLD     = 0x2bc;
95   
96       /**
97        * normal type of black color
98        */
99   
100      public final static short  COLOR_NORMAL        = 0x7fff;
101  
102      /**
103       * Dark Red color
104       */
105  
106      public final static short  COLOR_RED           = 0xa;
107  
108      /**
109       * no type offsetting (not super or subscript)
110       */
111  
112      public final static short  SS_NONE             = 0;
113  
114      /**
115       * superscript
116       */
117  
118      public final static short  SS_SUPER            = 1;
119  
120      /**
121       * subscript
122       */
123  
124      public final static short  SS_SUB              = 2;
125  
126      /**
127       * not underlined
128       */
129  
130      public final static byte   U_NONE              = 0;
131  
132      /**
133       * single (normal) underline
134       */
135  
136      public final static byte   U_SINGLE            = 1;
137  
138      /**
139       * double underlined
140       */
141  
142      public final static byte   U_DOUBLE            = 2;
143  
144      /**
145       * accounting style single underline
146       */
147  
148      public final static byte   U_SINGLE_ACCOUNTING = 0x21;
149  
150      /**
151       * accounting style double underline
152       */
153  
154      public final static byte   U_DOUBLE_ACCOUNTING = 0x22;
155      private FontRecord         font;
156      private short              index;
157  
158      /** Creates a new instance of HSSFFont */
159  
160      protected HSSFFont(short index, FontRecord rec)
161      {
162          font       = rec;
163          this.index = index;
164      }
165  
166      /**
167       * set the name for the font (i.e. Arial)
168       * @param String representing the name of the font to use
169       * @see #FONT_ARIAL
170       */
171  
172      public void setFontName(String name)
173      {
174          font.setFontName(name);
175          font.setFontNameLength(( byte ) name.length());
176      }
177  
178      /**
179       * get the name for the font (i.e. Arial)
180       * @return String representing the name of the font to use
181       * @see #FONT_ARIAL
182       */
183  
184      public String getFontName()
185      {
186          return font.getFontName();
187      }
188  
189      /**
190       * get the index within the HSSFWorkbook (sequence within the collection of Font objects)
191       * @return unique index number of the underlying record this Font represents (probably you don't care
192       *  unless you're comparing which one is which)
193       */
194  
195      public short getIndex()
196      {
197          return index;
198      }
199  
200      /**
201       * set the font height in unit's of 1/20th of a point.  Maybe you might want to
202       * use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
203       * @param short - height in 1/20ths of a point
204       * @see #setFontHeightInPoints(short)
205       */
206  
207      public void setFontHeight(short height)
208      {
209          font.setFontHeight(height);
210      }
211  
212      /**
213       * set the font height
214       * @param short - height in the familiar unit of measure - points
215       * @see #setFontHeight(short)
216       */
217  
218      public void setFontHeightInPoints(short height)
219      {
220          font.setFontHeight(( short ) (height * 20));
221      }
222  
223      /**
224       * get the font height in unit's of 1/20th of a point.  Maybe you might want to
225       * use the getFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
226       * @return short - height in 1/20ths of a point
227       * @see #getFontHeightInPoints()
228       */
229  
230      public short getFontHeight()
231      {
232          return font.getFontHeight();
233      }
234  
235      /**
236       * get the font height
237       * @return short - height in the familiar unit of measure - points
238       * @see #getFontHeight()
239       */
240  
241      public short getFontHeightInPoints()
242      {
243          return ( short ) (font.getFontHeight() / 20);
244      }
245  
246      /**
247       * set whether to use italics or not
248       * @param italics or not
249       */
250  
251      public void setItalic(boolean italic)
252      {
253          font.setItalic(italic);
254      }
255  
256      /**
257       * get whether to use italics or not
258       * @return italics or not
259       */
260  
261      public boolean getItalic()
262      {
263          return font.isItalic();
264      }
265  
266      /**
267       * set whether to use a strikeout horizontal line through the text or not
268       * @param strikeout or not
269       */
270  
271      public void setStrikeout(boolean strikeout)
272      {
273          font.setStrikeout(strikeout);
274      }
275  
276      /**
277       * get whether to use a strikeout horizontal line through the text or not
278       * @return strikeout or not
279       */
280  
281      public boolean getStrikeout()
282      {
283          return font.isStruckout();
284      }
285  
286      /**
287       * set the color for the font
288       * @param color to use
289       * @see #COLOR_NORMAL
290       * @see #COLOR_RED
291       */
292  
293      public void setColor(short color)
294      {
295          font.setColorPaletteIndex(color);
296      }
297  
298      /**
299       * get the color for the font
300       * @return color to use
301       * @see #COLOR_NORMAL
302       * @see #COLOR_RED
303       */
304  
305      public short getColor()
306      {
307          return font.getColorPaletteIndex();
308      }
309  
310      /**
311       * set the boldness to use
312       * @param boldweight
313       * @see #BOLDWEIGHT_NORMAL
314       * @see #BOLDWEIGHT_BOLD
315       */
316  
317      public void setBoldweight(short boldweight)
318      {
319          font.setBoldWeight(boldweight);
320      }
321  
322      /**
323       * get the boldness to use
324       * @return boldweight
325       * @see #BOLDWEIGHT_NORMAL
326       * @see #BOLDWEIGHT_BOLD
327       */
328  
329      public short getBoldweight()
330      {
331          return font.getBoldWeight();
332      }
333  
334      /**
335       * set normal,super or subscript.
336       * @param offset type to use (none,super,sub)
337       * @see #SS_NONE
338       * @see #SS_SUPER
339       * @see #SS_SUB
340       */
341  
342      public void setTypeOffset(short offset)
343      {
344          font.setSuperSubScript(offset);
345      }
346  
347      /**
348       * get normal,super or subscript.
349       * @return offset type to use (none,super,sub)
350       * @see #SS_NONE
351       * @see #SS_SUPER
352       * @see #SS_SUB
353       */
354  
355      public short getTypeOffset()
356      {
357          return font.getSuperSubScript();
358      }
359  
360      /**
361       * set type of text underlining to use
362       * @param underlining type
363       * @see #U_NONE
364       * @see #U_SINGLE
365       * @see #U_DOUBLE
366       * @see #U_SINGLE_ACCOUNTING
367       * @see #U_DOUBLE_ACCOUNTING
368       */
369  
370      public void setUnderline(byte underline)
371      {
372          font.setUnderline(underline);
373      }
374  
375      /**
376       * get type of text underlining to use
377       * @return underlining type
378       * @see #U_NONE
379       * @see #U_SINGLE
380       * @see #U_DOUBLE
381       * @see #U_SINGLE_ACCOUNTING
382       * @see #U_DOUBLE_ACCOUNTING
383       */
384  
385      public byte getUnderline()
386      {
387          return font.getUnderline();
388      }
389  }
390