1 package org.apache.maven.doxia.module.itext;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
35
36 public class ITextFont
37 {
38
39 public static final String NORMAL = MarkupTags.CSS_VALUE_NORMAL;
40
41
42 public static final String BOLD = MarkupTags.CSS_VALUE_BOLD;
43
44
45 public static final String ITALIC = MarkupTags.CSS_VALUE_ITALIC;
46
47
48 public static final String UNDERLINE = MarkupTags.CSS_VALUE_UNDERLINE;
49
50
51 public static final String DEFAULT_FONT_NAME = FontFactory.HELVETICA;
52
53
54 public static final float DEFAULT_FONT_SIZE = 12;
55
56
57 public static final String DEFAULT_FONT_STYLE = NORMAL;
58
59
60 public static final int DEFAULT_FONT_COLOR_RED = Color.BLACK.getRed();
61
62
63 public static final int DEFAULT_FONT_COLOR_GREEN = Color.BLACK.getGreen();
64
65
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
85
86 public ITextFont()
87 {
88
89 }
90
91
92
93
94 public void addBold()
95 {
96 this.currentStyle += Font.BOLD;
97 }
98
99
100
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
113
114 public void addItalic()
115 {
116 this.currentStyle += Font.ITALIC;
117 }
118
119
120
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
133
134 public void addUnderlined()
135 {
136 this.currentStyle += Font.UNDERLINE;
137 }
138
139
140
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
153
154
155
156 public void setMonoSpaced( boolean monoSpaced )
157 {
158 this.monoSpaced = monoSpaced;
159 }
160
161
162
163
164
165
166 public void setColor( Color color )
167 {
168 this.currentColor = color;
169 }
170
171
172
173
174
175
176 public void setSize( float size )
177 {
178 this.currentSize = size;
179 }
180
181
182
183
184
185
186 public String getFontName()
187 {
188 Font font = getCurrentFont();
189
190 return font.getFamilyname();
191 }
192
193
194
195
196
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
244
245
246
247 public String getFontSize()
248 {
249 Font font = getCurrentFont();
250
251 return String.valueOf( font.getCalculatedSize() );
252 }
253
254
255
256
257
258
259 public String getFontColorBlue()
260 {
261 Font font = getCurrentFont();
262
263 return String.valueOf( font.color().getBlue() );
264 }
265
266
267
268
269
270
271 public String getFontColorGreen()
272 {
273 Font font = getCurrentFont();
274
275 return String.valueOf( font.color().getGreen() );
276 }
277
278
279
280
281
282
283 public String getFontColorRed()
284 {
285 Font font = getCurrentFont();
286
287 return String.valueOf( font.color().getRed() );
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
337
338
339
340
341
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
357
358
359
360
361
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
375
376
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 }