View Javadoc
1   package org.apache.maven.doxia.module.itext;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.awt.Color;
23  
24  import com.lowagie.text.ExceptionConverter;
25  import com.lowagie.text.Font;
26  import com.lowagie.text.FontFactory;
27  import com.lowagie.text.markup.MarkupTags;
28  import com.lowagie.text.pdf.BaseFont;
29  
30  /**
31   * <code>iText</code> wrapper object for font.
32   *
33   * @see com.lowagie.text.Font
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   */
36  public class ITextFont
37  {
38      /** A normal font style */
39      public static final String NORMAL = MarkupTags.CSS_VALUE_NORMAL;
40  
41      /** A bold font style */
42      public static final String BOLD = MarkupTags.CSS_VALUE_BOLD;
43  
44      /** A italic font style */
45      public static final String ITALIC = MarkupTags.CSS_VALUE_ITALIC;
46  
47      /** An underline font style */
48      public static final String UNDERLINE = MarkupTags.CSS_VALUE_UNDERLINE;
49  
50      /** A default font name */
51      public static final String DEFAULT_FONT_NAME = FontFactory.HELVETICA;
52  
53      /** A default font size */
54      public static final float DEFAULT_FONT_SIZE = 12;
55  
56      /** A default font style */
57      public static final String DEFAULT_FONT_STYLE = NORMAL;
58  
59      /** A default Black color definition */
60      public static final int DEFAULT_FONT_COLOR_RED = Color.BLACK.getRed();
61  
62      /** A default Black color definition */
63      public static final int DEFAULT_FONT_COLOR_GREEN = Color.BLACK.getGreen();
64  
65      /** A default Black color definition */
66      public static final int DEFAULT_FONT_COLOR_BLUE = Color.BLACK.getBlue();
67  
68      private static final int SECTION_FONT_SIZE_0 = 24;
69      private static final int SECTION_FONT_SIZE_1 = 22;
70      private static final int SECTION_FONT_SIZE_2 = 20;
71      private static final int SECTION_FONT_SIZE_3 = 18;
72      private static final int SECTION_FONT_SIZE_4 = 16;
73      private static final int SECTION_FONT_SIZE_DEFAULT = 14;
74  
75      private boolean monoSpaced = false;
76  
77      private float currentSize = 12;
78  
79      private int currentStyle = Font.NORMAL;
80  
81      private Color currentColor = Color.BLACK;
82  
83      /**
84       * Default constructor
85       */
86      public ITextFont()
87      {
88          // nop
89      }
90  
91      /**
92       * Add bold style to the current style
93       */
94      public void addBold()
95      {
96          this.currentStyle += Font.BOLD;
97      }
98  
99      /**
100      * Remove bold style to the current style
101      */
102     public void removeBold()
103     {
104         this.currentStyle -= Font.BOLD;
105         if ( this.currentStyle < 0 )
106         {
107             this.currentStyle = Font.NORMAL;
108         }
109     }
110 
111     /**
112      * Add italic style to the current style
113      */
114     public void addItalic()
115     {
116         this.currentStyle += Font.ITALIC;
117     }
118 
119     /**
120      * Remove italic style to the current style
121      */
122     public void removeItalic()
123     {
124         this.currentStyle -= Font.ITALIC;
125         if ( this.currentStyle < 0 )
126         {
127             this.currentStyle = Font.NORMAL;
128         }
129     }
130 
131     /**
132      * Add italic style to the current style
133      */
134     public void addUnderlined()
135     {
136         this.currentStyle += Font.UNDERLINE;
137     }
138 
139     /**
140      * Remove italic style to the current style
141      */
142     public void removeUnderlined()
143     {
144         this.currentStyle -= Font.UNDERLINE;
145         if ( this.currentStyle < 0 )
146         {
147             this.currentStyle = Font.NORMAL;
148         }
149     }
150 
151     /**
152      * Add monospaced style to the current style
153      *
154      * @param monoSpaced true for monospaced style
155      */
156     public void setMonoSpaced( boolean monoSpaced )
157     {
158         this.monoSpaced = monoSpaced;
159     }
160 
161     /**
162      * Set a new font color
163      *
164      * @param color a new color
165      */
166     public void setColor( Color color )
167     {
168         this.currentColor = color;
169     }
170 
171     /**
172      * Set a new font color
173      *
174      * @param size a new size
175      */
176     public void setSize( float size )
177     {
178         this.currentSize = size;
179     }
180 
181     /**
182      * Return the font name
183      *
184      * @return the font name
185      */
186     public String getFontName()
187     {
188         Font font = getCurrentFont();
189 
190         return font.getFamilyname();
191     }
192 
193     /**
194      * Return the font style
195      *
196      * @return the font style
197      */
198     public String getFontStyle()
199     {
200         Font font = getCurrentFont();
201         StringBuilder sb = new StringBuilder();
202 
203         if ( font.isBold() )
204         {
205             sb.append( BOLD );
206         }
207 
208         if ( font.isItalic() )
209         {
210             if ( sb.length() == 0 )
211             {
212                 sb.append( ITALIC );
213             }
214             else
215             {
216                 sb.append( "," );
217                 sb.append( ITALIC );
218             }
219         }
220 
221         if ( font.isUnderlined() )
222         {
223             if ( sb.length() == 0 )
224             {
225                 sb.append( UNDERLINE );
226             }
227             else
228             {
229                 sb.append( "," );
230                 sb.append( UNDERLINE );
231             }
232         }
233 
234         if ( sb.length() == 0 )
235         {
236             return NORMAL;
237         }
238 
239         return sb.toString();
240     }
241 
242     /**
243      * Return the font name
244      *
245      * @return the font name
246      */
247     public String getFontSize()
248     {
249         Font font = getCurrentFont();
250 
251         return String.valueOf( font.getCalculatedSize() );
252     }
253 
254     /**
255      * Return the font color blue
256      *
257      * @return the font color blue
258      */
259     public String getFontColorBlue()
260     {
261         Font font = getCurrentFont();
262 
263         return String.valueOf( font.color().getBlue() );
264     }
265 
266     /**
267      * Return the font color green
268      *
269      * @return the font color green
270      */
271     public String getFontColorGreen()
272     {
273         Font font = getCurrentFont();
274 
275         return String.valueOf( font.color().getGreen() );
276     }
277 
278     /**
279      * Return the font color red
280      *
281      * @return the font color red
282      */
283     public String getFontColorRed()
284     {
285         Font font = getCurrentFont();
286 
287         return String.valueOf( font.color().getRed() );
288     }
289 
290     /**
291      * Get a section font size depending the section number.
292      * <dl>
293      * <dt>0</dt>
294      * <dd>Chapter: font size = 24</dd>
295      * <dt>1</dt>
296      * <dd>Section 1: font size = 22</dd>
297      * <dt>2</dt>
298      * <dd>Section 2: font size = 20</dd>
299      * <dt>3</dt>
300      * <dd>Section 3: font size = 18</dd>
301      * <dt>4</dt>
302      * <dd>Section 4: font size = 16</dd>
303      * <dt>5 ot otherwise</dt>
304      * <dd>Section 5: font size = 14</dd>
305      * </dl>
306      *
307      * @param sectionNumber a section number
308      * @return a font size.
309      */
310     public static int getSectionFontSize( int sectionNumber )
311     {
312         switch ( sectionNumber )
313         {
314             case 0:
315                 return SECTION_FONT_SIZE_0;
316 
317             case 1:
318                 return SECTION_FONT_SIZE_1;
319 
320             case 2:
321                 return SECTION_FONT_SIZE_2;
322 
323             case 3:
324                 return SECTION_FONT_SIZE_3;
325 
326             case 4:
327                 return SECTION_FONT_SIZE_4;
328 
329             case 5:
330             default:
331                 return SECTION_FONT_SIZE_DEFAULT;
332         }
333     }
334 
335     /**
336      * Convenience method to get a defined MonoSpaced font depending the wanted style and size.
337      *
338      * @param style the font style.
339      * @param size the font size.
340      * @param color the font color.
341      * @return a font the font.
342      */
343     public static Font getMonoSpacedFont( int style, float size, Color color )
344     {
345         try
346         {
347             return new Font( BaseFont.createFont( BaseFont.COURIER, BaseFont.CP1252, false ), size, style, color );
348         }
349         catch ( Exception e )
350         {
351             throw new ExceptionConverter( e );
352         }
353     }
354 
355     /**
356      * Convenience method to get a defined font depending the wanted style and size.
357      *
358      * @param style the font style.
359      * @param size the font size.
360      * @param color the font color.
361      * @return a font the font.
362      */
363     public static Font getFont( int style, float size, Color color )
364     {
365         Font font = new Font();
366         font.setFamily( DEFAULT_FONT_NAME );
367         font.setStyle( style );
368         font.setSize( size );
369         font.setColor( color );
370         return font;
371     }
372 
373     /**
374      * Convenience method to return the current font
375      *
376      * @return the current font
377      */
378     private Font getCurrentFont()
379     {
380         if ( this.monoSpaced )
381         {
382             return getMonoSpacedFont( this.currentStyle, this.currentSize, this.currentColor );
383         }
384 
385         return getFont( this.currentStyle, this.currentSize, this.currentColor );
386     }
387 }