Coverage Report - org.apache.maven.doxia.module.rtf.Font
 
Classes in this File Line Coverage Branch Coverage Complexity
Font
86%
19/22
87%
7/8
1,667
Font$TextExtents
100%
5/5
N/A
1,667
 
 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  14
     {
 37  14
         this.size = size;
 38  14
         metrics = FontMetrics.find( style );
 39  14
     }
 40  
 
 41  
     int ascent()
 42  
     {
 43  0
         return toTwips( metrics.ascent );
 44  
     }
 45  
 
 46  
     int descent()
 47  
     {
 48  0
         return toTwips( metrics.descent );
 49  
     }
 50  
 
 51  
     TextExtents textExtents( String text )
 52  
     {
 53  
         int i, n;
 54  86
         int width = 0;
 55  86
         int ascent = 0;
 56  86
         int descent = 0;
 57  
 
 58  612
         for ( i = 0, n = text.length(); i < n; ++i )
 59  
         {
 60  526
             char c = text.charAt( i );
 61  526
             if ( c > 255 )
 62  
             {
 63  0
                 c = ' ';
 64  
             }
 65  526
             FontMetrics.CharMetrics charMetrics = this.metrics.charMetrics[c];
 66  526
             width += charMetrics.wx;
 67  526
             if ( charMetrics.ury > ascent )
 68  
             {
 69  146
                 ascent = charMetrics.ury;
 70  
             }
 71  526
             if ( charMetrics.lly < descent )
 72  
             {
 73  104
                 descent = charMetrics.lly;
 74  
             }
 75  
         }
 76  
 
 77  86
         int height = ascent + Math.abs( descent );
 78  
 
 79  86
         return new TextExtents( toTwips( width ), toTwips( height ), toTwips( ascent ) );
 80  
     }
 81  
 
 82  
     private int toTwips( int length )
 83  
     {
 84  258
         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  86
         {
 98  86
             this.width = width;
 99  86
             this.height = height;
 100  86
             this.ascent = ascent;
 101  86
         }
 102  
 
 103  
     }
 104  
 }