View Javadoc
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  class Font
27  {
28      private int size;
29  
30      private FontMetrics metrics;
31  
32      Font( int style, int size /*pts*/ )
33          throws Exception
34      {
35          this.size = size;
36          metrics = FontMetrics.find( style );
37      }
38  
39      int ascent()
40      {
41          return toTwips( metrics.ascent );
42      }
43  
44      int descent()
45      {
46          return toTwips( metrics.descent );
47      }
48  
49      TextExtents textExtents( String text )
50      {
51          int i, n;
52          int width = 0;
53          int ascent = 0;
54          int descent = 0;
55  
56          for ( i = 0, n = text.length(); i < n; ++i )
57          {
58              char c = text.charAt( i );
59              if ( c > 255 )
60              {
61                  c = ' ';
62              }
63              FontMetrics.CharMetrics charMetrics = this.metrics.charMetrics[c];
64              width += charMetrics.wx;
65              if ( charMetrics.ury > ascent )
66              {
67                  ascent = charMetrics.ury;
68              }
69              if ( charMetrics.lly < descent )
70              {
71                  descent = charMetrics.lly;
72              }
73          }
74  
75          int height = ascent + Math.abs( descent );
76  
77          return new TextExtents( toTwips( width ), toTwips( height ), toTwips( ascent ) );
78      }
79  
80      private int toTwips( int length )
81      {
82          return (int) Math.rint( (double) length * size / 50. );
83      }
84  
85      static class TextExtents
86      {
87  
88          int width;
89  
90          int height;
91  
92          int ascent;
93  
94          TextExtents( int width, int height, int ascent )
95          {
96              this.width = width;
97              this.height = height;
98              this.ascent = ascent;
99          }
100 
101     }
102 }