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