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 39 import org.fontbox.afm.FontMetric; 40 41 import org.pdfbox.cos.COSArray; 42 import org.pdfbox.cos.COSDictionary; 43 import org.pdfbox.cos.COSName; 44 import org.pdfbox.cos.COSNumber; 45 import org.pdfbox.cos.COSInteger; 46 import org.pdfbox.encoding.Encoding; 47 48 import org.pdfbox.pdmodel.common.PDRectangle; 49 import org.pdfbox.pdmodel.common.PDStream; 50 51 57 public abstract class PDSimpleFont extends PDFont 58 { 59 private HashMap mFontSizes = new HashMap (128); 60 private float avgFontWidth = 0.0f; 61 62 65 public PDSimpleFont() 66 { 67 super(); 68 } 69 70 75 public PDSimpleFont( COSDictionary fontDictionary ) 76 { 77 super( fontDictionary ); 78 } 79 80 83 public void drawString( String string, Graphics g, float fontSize, 84 float xScale, float yScale, float x, float y ) throws IOException 85 { 86 System.err.println( "Not yet implemented:" + getClass().getName() ); 87 } 88 89 100 public float getFontHeight( byte[] c, int offset, int length ) throws IOException 101 { 102 float retval = 0; 103 int code = getCodeFromArray( c, offset, length ); 104 FontMetric metric = getAFM(); 105 if( metric != null ) 106 { 107 Encoding encoding = getEncoding(); 108 COSName characterName = encoding.getName( code ); 109 retval = metric.getCharacterHeight( characterName.getName() ); 110 } 111 else 112 { 113 PDFontDescriptor desc = getFontDescriptor(); 114 if( desc != null ) 115 { 116 float xHeight = desc.getXHeight(); 117 float capHeight = desc.getCapHeight(); 118 if( xHeight != 0f && capHeight != 0 ) 119 { 120 retval = (xHeight + capHeight)/2f; 122 } 123 else if( xHeight != 0 ) 124 { 125 retval = xHeight; 126 } 127 else if( capHeight != 0 ) 128 { 129 retval = capHeight; 130 } 131 else 132 { 133 retval = 0; 134 } 135 } 136 } 137 return retval; 138 } 139 140 151 public float getFontWidth( byte[] c, int offset, int length ) throws IOException 152 { 153 float fontWidth = 0; 154 int code = getCodeFromArray( c, offset, length ); 155 156 Integer codeI = new Integer (code); 157 if (mFontSizes.containsKey(codeI)) 158 { 159 Float fontWidthF = (Float ) mFontSizes.get(codeI); 160 fontWidth = fontWidthF.floatValue(); 161 } 162 else 163 { 164 COSInteger firstChar = (COSInteger)font.getDictionaryObject( COSName.FIRST_CHAR ); 166 COSInteger lastChar = (COSInteger)font.getDictionaryObject( COSName.LAST_CHAR ); 167 if( firstChar != null && lastChar != null ) 168 { 169 long first = firstChar.intValue(); 170 long last = lastChar.intValue(); 171 if( code >= first && code <= last && font.getDictionaryObject( COSName.WIDTHS ) != null ) 172 { 173 COSArray widthArray = (COSArray)font.getDictionaryObject( COSName.WIDTHS ); 174 COSNumber fontWidthObject = (COSNumber)widthArray.getObject( (int)(code - first) ); 175 fontWidth = fontWidthObject.floatValue(); 176 } 177 else 178 { 179 fontWidth = getFontWidthFromAFMFile( code ); 180 } 181 } 182 else 183 { 184 fontWidth = getFontWidthFromAFMFile( code ); 185 } 186 mFontSizes.put(codeI, new Float (fontWidth)); 187 } 188 return fontWidth; 189 } 190 191 198 public float getAverageFontWidth() throws IOException 199 { 200 float average = 0.0f; 201 202 if (avgFontWidth != 0.0f) 204 { 205 average = avgFontWidth; 206 } 207 else 208 { 209 float totalWidth = 0.0f; 210 float characterCount = 0.0f; 211 COSArray widths = (COSArray)font.getDictionaryObject( COSName.WIDTHS ); 212 if( widths != null ) 213 { 214 for( int i=0; i<widths.size(); i++ ) 215 { 216 COSNumber fontWidth = (COSNumber)widths.getObject( i ); 217 if( fontWidth.floatValue() > 0 ) 218 { 219 totalWidth += fontWidth.floatValue(); 220 characterCount += 1; 221 } 222 } 223 } 224 225 if( totalWidth > 0 ) 226 { 227 average = totalWidth / characterCount; 228 } 229 else 230 { 231 average = getAverageFontWidthFromAFMFile(); 232 } 233 avgFontWidth = average; 234 } 235 return average; 236 } 237 238 246 public PDFontDescriptor getFontDescriptor() throws IOException 247 { 248 PDFontDescriptor retval = null; 249 COSDictionary fd = (COSDictionary)font.getDictionaryObject( COSName.getPDFName( "FontDescriptor" ) ); 250 if( fd == null ) 251 { 252 FontMetric afm = getAFM(); 253 if( afm != null ) 254 { 255 retval = new PDFontDescriptorAFM( afm ); 256 } 257 } 258 else 259 { 260 retval = new PDFontDescriptorDictionary( fd ); 261 } 262 263 return retval; 264 } 265 266 271 public void setFontDescriptor( PDFontDescriptorDictionary fontDescriptor ) 272 { 273 COSDictionary dic = null; 274 if( fontDescriptor != null ) 275 { 276 dic = fontDescriptor.getCOSDictionary(); 277 } 278 font.setItem( COSName.getPDFName( "FontDescriptor" ), dic ); 279 } 280 281 287 public PDStream getToUnicode() throws IOException 288 { 289 return PDStream.createFromCOS( font.getDictionaryObject( "ToUnicode" ) ); 290 } 291 292 297 public void setToUnicode( PDStream unicode ) 298 { 299 font.setItem( "ToUnicode", unicode ); 300 } 301 302 309 public PDRectangle getFontBoundingBox() throws IOException 310 { 311 return getFontDescriptor().getFontBoundingBox(); 312 } 313 } | Popular Tags |