001package org.apache.maven.doxia.module.itext; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.awt.Color; 023 024import com.lowagie.text.ExceptionConverter; 025import com.lowagie.text.Font; 026import com.lowagie.text.FontFactory; 027import com.lowagie.text.markup.MarkupTags; 028import com.lowagie.text.pdf.BaseFont; 029 030/** 031 * <code>iText</code> wrapper object for font. 032 * 033 * @see com.lowagie.text.Font 034 * 035 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 036 * @version $Id$ 037 */ 038public class ITextFont 039{ 040 /** A normal font style */ 041 public static final String NORMAL = MarkupTags.CSS_VALUE_NORMAL; 042 043 /** A bold font style */ 044 public static final String BOLD = MarkupTags.CSS_VALUE_BOLD; 045 046 /** A italic font style */ 047 public static final String ITALIC = MarkupTags.CSS_VALUE_ITALIC; 048 049 /** An underline font style */ 050 public static final String UNDERLINE = MarkupTags.CSS_VALUE_UNDERLINE; 051 052 /** A default font name */ 053 public static final String DEFAULT_FONT_NAME = FontFactory.HELVETICA; 054 055 /** A default font size */ 056 public static final float DEFAULT_FONT_SIZE = 12; 057 058 /** A default font style */ 059 public static final String DEFAULT_FONT_STYLE = NORMAL; 060 061 /** A default Black color definition */ 062 public static final int DEFAULT_FONT_COLOR_RED = Color.BLACK.getRed(); 063 064 /** A default Black color definition */ 065 public static final int DEFAULT_FONT_COLOR_GREEN = Color.BLACK.getGreen(); 066 067 /** A default Black color definition */ 068 public static final int DEFAULT_FONT_COLOR_BLUE = Color.BLACK.getBlue(); 069 070 private static final int SECTION_FONT_SIZE_0 = 24; 071 private static final int SECTION_FONT_SIZE_1 = 22; 072 private static final int SECTION_FONT_SIZE_2 = 20; 073 private static final int SECTION_FONT_SIZE_3 = 18; 074 private static final int SECTION_FONT_SIZE_4 = 16; 075 private static final int SECTION_FONT_SIZE_DEFAULT = 14; 076 077 private boolean monoSpaced = false; 078 079 private float currentSize = 12; 080 081 private int currentStyle = Font.NORMAL; 082 083 private Color currentColor = Color.BLACK; 084 085 /** 086 * Default constructor 087 */ 088 public ITextFont() 089 { 090 // nop 091 } 092 093 /** 094 * Add bold style to the current style 095 */ 096 public void addBold() 097 { 098 this.currentStyle += Font.BOLD; 099 } 100 101 /** 102 * Remove bold style to the current style 103 */ 104 public void removeBold() 105 { 106 this.currentStyle -= Font.BOLD; 107 if ( this.currentStyle < 0 ) 108 { 109 this.currentStyle = Font.NORMAL; 110 } 111 } 112 113 /** 114 * Add italic style to the current style 115 */ 116 public void addItalic() 117 { 118 this.currentStyle += Font.ITALIC; 119 } 120 121 /** 122 * Remove italic style to the current style 123 */ 124 public void removeItalic() 125 { 126 this.currentStyle -= Font.ITALIC; 127 if ( this.currentStyle < 0 ) 128 { 129 this.currentStyle = Font.NORMAL; 130 } 131 } 132 133 /** 134 * Add italic style to the current style 135 */ 136 public void addUnderlined() 137 { 138 this.currentStyle += Font.UNDERLINE; 139 } 140 141 /** 142 * Remove italic style to the current style 143 */ 144 public void removeUnderlined() 145 { 146 this.currentStyle -= Font.UNDERLINE; 147 if ( this.currentStyle < 0 ) 148 { 149 this.currentStyle = Font.NORMAL; 150 } 151 } 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 this.monoSpaced = monoSpaced; 161 } 162 163 /** 164 * Set a new font color 165 * 166 * @param color a new color 167 */ 168 public void setColor( Color color ) 169 { 170 this.currentColor = color; 171 } 172 173 /** 174 * Set a new font color 175 * 176 * @param size a new size 177 */ 178 public void setSize( float size ) 179 { 180 this.currentSize = size; 181 } 182 183 /** 184 * Return the font name 185 * 186 * @return the font name 187 */ 188 public String getFontName() 189 { 190 Font font = getCurrentFont(); 191 192 return font.getFamilyname(); 193 } 194 195 /** 196 * Return the font style 197 * 198 * @return the font style 199 */ 200 public String getFontStyle() 201 { 202 Font font = getCurrentFont(); 203 StringBuilder sb = new StringBuilder(); 204 205 if ( font.isBold() ) 206 { 207 sb.append( BOLD ); 208 } 209 210 if ( font.isItalic() ) 211 { 212 if ( sb.length() == 0 ) 213 { 214 sb.append( ITALIC ); 215 } 216 else 217 { 218 sb.append( "," ); 219 sb.append( ITALIC ); 220 } 221 } 222 223 if ( font.isUnderlined() ) 224 { 225 if ( sb.length() == 0 ) 226 { 227 sb.append( UNDERLINE ); 228 } 229 else 230 { 231 sb.append( "," ); 232 sb.append( UNDERLINE ); 233 } 234 } 235 236 if ( sb.length() == 0 ) 237 { 238 return NORMAL; 239 } 240 241 return sb.toString(); 242 } 243 244 /** 245 * Return the font name 246 * 247 * @return the font name 248 */ 249 public String getFontSize() 250 { 251 Font font = getCurrentFont(); 252 253 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 Font font = getCurrentFont(); 264 265 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 Font font = getCurrentFont(); 276 277 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 Font font = getCurrentFont(); 288 289 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 switch ( sectionNumber ) 315 { 316 case 0: 317 return SECTION_FONT_SIZE_0; 318 319 case 1: 320 return SECTION_FONT_SIZE_1; 321 322 case 2: 323 return SECTION_FONT_SIZE_2; 324 325 case 3: 326 return SECTION_FONT_SIZE_3; 327 328 case 4: 329 return SECTION_FONT_SIZE_4; 330 331 case 5: 332 default: 333 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 return new Font( BaseFont.createFont( BaseFont.COURIER, BaseFont.CP1252, false ), size, style, color ); 350 } 351 catch ( Exception e ) 352 { 353 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 Font font = new Font(); 368 font.setFamily( DEFAULT_FONT_NAME ); 369 font.setStyle( style ); 370 font.setSize( size ); 371 font.setColor( color ); 372 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 if ( this.monoSpaced ) 383 { 384 return getMonoSpacedFont( this.currentStyle, this.currentSize, this.currentColor ); 385 } 386 387 return getFont( this.currentStyle, this.currentSize, this.currentColor ); 388 } 389}