1 package org.apache.maven.doxia.module.rtf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 class Font
29 {
30 private int size;
31
32 private FontMetrics metrics;
33
34 Font( int style, int size )
35 throws Exception
36 {
37 this.size = size;
38 metrics = FontMetrics.find( style );
39 }
40
41 int ascent()
42 {
43 return toTwips( metrics.ascent );
44 }
45
46 int descent()
47 {
48 return toTwips( metrics.descent );
49 }
50
51 TextExtents textExtents( String text )
52 {
53 int i, n;
54 int width = 0;
55 int ascent = 0;
56 int descent = 0;
57
58 for ( i = 0, n = text.length(); i < n; ++i )
59 {
60 char c = text.charAt( i );
61 if ( c > 255 )
62 {
63 c = ' ';
64 }
65 FontMetrics.CharMetrics charMetrics = this.metrics.charMetrics[c];
66 width += charMetrics.wx;
67 if ( charMetrics.ury > ascent )
68 {
69 ascent = charMetrics.ury;
70 }
71 if ( charMetrics.lly < descent )
72 {
73 descent = charMetrics.lly;
74 }
75 }
76
77 int height = ascent + Math.abs( descent );
78
79 return new TextExtents( toTwips( width ), toTwips( height ), toTwips( ascent ) );
80 }
81
82 private int toTwips( int length )
83 {
84 return (int) Math.rint( (double) length * size / 50. );
85 }
86
87 static class TextExtents
88 {
89
90 int width;
91
92 int height;
93
94 int ascent;
95
96 TextExtents( int width, int height, int ascent )
97 {
98 this.width = width;
99 this.height = height;
100 this.ascent = ascent;
101 }
102
103 }
104 }