1 31 package org.pdfbox.pdmodel.font; 32 33 import java.awt.Graphics ; 34 35 import java.io.IOException ; 36 37 import java.util.HashMap ; 38 import java.util.Map ; 39 40 import org.pdfbox.cos.COSArray; 41 import org.pdfbox.cos.COSBase; 42 import org.pdfbox.cos.COSDictionary; 43 import org.pdfbox.cos.COSInteger; 44 import org.pdfbox.cos.COSName; 45 import org.pdfbox.cos.COSNumber; 46 import org.pdfbox.pdmodel.common.PDRectangle; 47 48 54 public abstract class PDCIDFont extends PDFont 55 { 56 private Map widthCache = new HashMap (); 57 58 61 public PDCIDFont() 62 { 63 super(); 64 } 65 66 71 public PDCIDFont( COSDictionary fontDictionary ) 72 { 73 super( fontDictionary ); 74 } 75 76 81 public PDFontDescriptor getFontDescriptor() 82 { 83 PDFontDescriptor desc = null; 84 COSDictionary dict = (COSDictionary)font.getDictionaryObject( "FontDescriptor" ); 85 if( dict != null ) 86 { 87 desc = new PDFontDescriptorDictionary( dict ); 88 } 89 return desc; 90 } 91 92 97 public void setFontDescriptor( PDFontDescriptorDictionary desc ) 98 { 99 font.setItem( "FontDescriptor", desc ); 100 } 101 102 105 public void drawString( String string, Graphics g, float fontSize, 106 float xScale, float yScale, float x, float y ) 107 { 108 throw new RuntimeException ( "Not yet implemented" ); 109 } 110 111 118 public PDRectangle getFontBoundingBox() throws IOException 119 { 120 throw new RuntimeException ( "Not yet implemented" ); 121 } 122 123 128 public long getDefaultWidth() 129 { 130 long dw = 1000; 131 COSNumber number = (COSNumber)font.getDictionaryObject( COSName.getPDFName( "DW" ) ); 132 if( number != null ) 133 { 134 dw = number.intValue(); 135 } 136 return dw; 137 } 138 139 144 public void setDefaultWidth( long dw ) 145 { 146 font.setItem( COSName.getPDFName( "DW" ), new COSInteger( dw ) ); 147 } 148 149 160 public float getFontWidth( byte[] c, int offset, int length ) throws IOException 161 { 162 163 float retval = 0.0f; 164 int code = getCodeFromArray( c, offset, length ); 165 166 Float widthFloat = (Float )widthCache.get( new Integer ( code ) ); 167 if( widthFloat == null ) 168 { 169 COSArray widths = (COSArray)font.getDictionaryObject( COSName.getPDFName( "W" ) ); 170 171 if( widths != null ) 172 { 173 boolean foundWidth = false; 174 for( int i=0; !foundWidth && i<widths.size(); i++ ) 175 { 176 COSNumber firstCode = (COSNumber)widths.getObject( i++ ); 177 COSBase next = widths.getObject( i ); 178 if( next instanceof COSArray ) 179 { 180 COSArray array = (COSArray)next; 181 if( code >= firstCode.intValue() && 182 code < firstCode.intValue() + array.size() ) 183 { 184 COSNumber rangeWidth = 185 (COSNumber)array.get( code - firstCode.intValue() ); 186 retval = rangeWidth.floatValue(); 187 foundWidth = true; 188 } 189 } 190 else 191 { 192 COSNumber secondCode = (COSNumber)next; 193 i++; 194 COSNumber rangeWidth = (COSNumber)widths.getObject( i ); 195 if( code >= firstCode.intValue() && 196 code <= secondCode.intValue() ) 197 { 198 retval = rangeWidth.floatValue(); 199 foundWidth = true; 200 } 201 } 202 } 203 widthCache.put( new Integer ( code ), new Float ( retval ) ); 204 } 205 } 206 else 207 { 208 retval = widthFloat.floatValue(); 209 } 210 return retval; 211 } 212 213 224 public float getFontHeight( byte[] c, int offset, int length ) throws IOException 225 { 226 float retval = 0; 227 PDFontDescriptor desc = getFontDescriptor(); 228 float xHeight = desc.getXHeight(); 229 float capHeight = desc.getCapHeight(); 230 if( xHeight != 0f && capHeight != 0 ) 231 { 232 retval = (xHeight + capHeight)/2f; 234 } 235 else if( xHeight != 0 ) 236 { 237 retval = xHeight; 238 } 239 else if( capHeight != 0 ) 240 { 241 retval = capHeight; 242 } 243 else 244 { 245 retval = 0; 246 } 247 return retval; 248 } 249 250 257 public float getAverageFontWidth() throws IOException 258 { 259 float totalWidths = 0.0f; 260 float characterCount = 0.0f; 261 float defaultWidth = getDefaultWidth(); 262 COSArray widths = (COSArray)font.getDictionaryObject( COSName.getPDFName( "W" ) ); 263 264 if( widths != null ) 265 { 266 for( int i=0; i<widths.size(); i++ ) 267 { 268 COSNumber firstCode = (COSNumber)widths.getObject( i++ ); 269 COSBase next = widths.getObject( i ); 270 if( next instanceof COSArray ) 271 { 272 COSArray array = (COSArray)next; 273 for( int j=0; j<array.size(); j++ ) 274 { 275 COSNumber width = (COSNumber)array.get( j ); 276 totalWidths+=width.floatValue(); 277 characterCount += 1; 278 } 279 } 280 else 281 { 282 i++; 283 COSNumber rangeWidth = (COSNumber)widths.getObject( i ); 284 if( rangeWidth.floatValue() > 0 ) 285 { 286 totalWidths += rangeWidth.floatValue(); 287 characterCount += 1; 288 } 289 } 290 } 291 } 292 float average = totalWidths / characterCount; 293 if( average <= 0 ) 294 { 295 average = defaultWidth; 296 } 297 return average; 298 } 299 } | Popular Tags |