1 package org.apache.maven.doxia.module.rtf;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /**
23 * A basic font descriptor using standard PostScript font metrics to compute
24 * text extents. All dimensions returned are in twips.
25 *
26 * @version $Id: Font.java 703397 2008-10-10 11:01:15Z vsiveton $
27 */
28 class Font
29 {
30 private int size;
31
32 private FontMetrics metrics;
33
34 Font( int style, int size /*pts*/ )
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 }