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 | 2 | public static final int DEFAULT_FONT_COLOR_RED = Color.BLACK.getRed(); |
63 | |
|
64 | |
|
65 | 2 | public static final int DEFAULT_FONT_COLOR_GREEN = Color.BLACK.getGreen(); |
66 | |
|
67 | |
|
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 | |
|
87 | |
|
88 | |
public ITextFont() |
89 | 8 | { |
90 | |
|
91 | 8 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void addBold() |
97 | |
{ |
98 | 16 | this.currentStyle += Font.BOLD; |
99 | 16 | } |
100 | |
|
101 | |
|
102 | |
|
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 | |
|
115 | |
|
116 | |
public void addItalic() |
117 | |
{ |
118 | 2 | this.currentStyle += Font.ITALIC; |
119 | 2 | } |
120 | |
|
121 | |
|
122 | |
|
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 | |
|
135 | |
|
136 | |
public void addUnderlined() |
137 | |
{ |
138 | 12 | this.currentStyle += Font.UNDERLINE; |
139 | 12 | } |
140 | |
|
141 | |
|
142 | |
|
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 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void setMonoSpaced( boolean monoSpaced ) |
159 | |
{ |
160 | 4 | this.monoSpaced = monoSpaced; |
161 | 4 | } |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public void setColor( Color color ) |
169 | |
{ |
170 | 34 | this.currentColor = color; |
171 | 34 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public void setSize( float size ) |
179 | |
{ |
180 | 28 | this.currentSize = size; |
181 | 28 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public String getFontName() |
189 | |
{ |
190 | 230 | Font font = getCurrentFont(); |
191 | |
|
192 | 230 | return font.getFamilyname(); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
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 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public String getFontSize() |
250 | |
{ |
251 | 230 | Font font = getCurrentFont(); |
252 | |
|
253 | 230 | return String.valueOf( font.getCalculatedSize() ); |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public String getFontColorBlue() |
262 | |
{ |
263 | 224 | Font font = getCurrentFont(); |
264 | |
|
265 | 224 | return String.valueOf( font.color().getBlue() ); |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public String getFontColorGreen() |
274 | |
{ |
275 | 224 | Font font = getCurrentFont(); |
276 | |
|
277 | 224 | return String.valueOf( font.color().getGreen() ); |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
public String getFontColorRed() |
286 | |
{ |
287 | 224 | Font font = getCurrentFont(); |
288 | |
|
289 | 224 | 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 | 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 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
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 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
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 | |
|
377 | |
|
378 | |
|
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 | |
} |