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
37
38 public class ITextFont
39 {
40
41 public static final String NORMAL = MarkupTags.CSS_VALUE_NORMAL;
42
43
44 public static final String BOLD = MarkupTags.CSS_VALUE_BOLD;
45
46
47 public static final String ITALIC = MarkupTags.CSS_VALUE_ITALIC;
48
49
50 public static final String UNDERLINE = MarkupTags.CSS_VALUE_UNDERLINE;
51
52
53 public static final String DEFAULT_FONT_NAME = FontFactory.HELVETICA;
54
55
56 public static final float DEFAULT_FONT_SIZE = 12;
57
58
59 public static final String DEFAULT_FONT_STYLE = NORMAL;
60
61
62 public static final int DEFAULT_FONT_COLOR_RED = Color.BLACK.getRed();
63
64
65 public static final int DEFAULT_FONT_COLOR_GREEN = Color.BLACK.getGreen();
66
67
68 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 private boolean monoSpaced = false;
78
79 private float currentSize = 12;
80
81 private int currentStyle = Font.NORMAL;
82
83 private Color currentColor = Color.BLACK;
84
85
86
87
88 public ITextFont()
89 {
90
91 }
92
93
94
95
96 public void addBold()
97 {
98 this.currentStyle += Font.BOLD;
99 }
100
101
102
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
115
116 public void addItalic()
117 {
118 this.currentStyle += Font.ITALIC;
119 }
120
121
122
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
135
136 public void addUnderlined()
137 {
138 this.currentStyle += Font.UNDERLINE;
139 }
140
141
142
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
155
156
157
158 public void setMonoSpaced( boolean monoSpaced )
159 {
160 this.monoSpaced = monoSpaced;
161 }
162
163
164
165
166
167
168 public void setColor( Color color )
169 {
170 this.currentColor = color;
171 }
172
173
174
175
176
177
178 public void setSize( float size )
179 {
180 this.currentSize = size;
181 }
182
183
184
185
186
187
188 public String getFontName()
189 {
190 Font font = getCurrentFont();
191
192 return font.getFamilyname();
193 }
194
195
196
197
198
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
246
247
248
249 public String getFontSize()
250 {
251 Font font = getCurrentFont();
252
253 return String.valueOf( font.getCalculatedSize() );
254 }
255
256
257
258
259
260
261 public String getFontColorBlue()
262 {
263 Font font = getCurrentFont();
264
265 return String.valueOf( font.color().getBlue() );
266 }
267
268
269
270
271
272
273 public String getFontColorGreen()
274 {
275 Font font = getCurrentFont();
276
277 return String.valueOf( font.color().getGreen() );
278 }
279
280
281
282
283
284
285 public String getFontColorRed()
286 {
287 Font font = getCurrentFont();
288
289 return String.valueOf( font.color().getRed() );
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
339
340
341
342
343
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
359
360
361
362
363
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
377
378
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 }