1 31 package org.pdfbox.pdmodel.font; 32 33 import org.pdfbox.cos.COSDictionary; 34 import org.pdfbox.cos.COSName; 35 import org.pdfbox.cos.COSStream; 36 import org.pdfbox.pdmodel.common.PDMatrix; 37 38 import java.awt.Graphics ; 39 import java.awt.Image ; 40 41 import java.io.IOException ; 42 43 import java.util.HashMap ; 44 import java.util.Map ; 45 46 52 public class PDType3Font extends PDSimpleFont 53 { 54 private Map images = new HashMap (); 56 57 60 public PDType3Font() 61 { 62 super(); 63 font.setItem( COSName.SUBTYPE, COSName.getPDFName( "Type3" ) ); 64 } 65 66 71 public PDType3Font( COSDictionary fontDictionary ) 72 { 73 super( fontDictionary ); 74 } 75 76 82 private Image createImageIfNecessary( char character ) throws IOException 83 { 84 Character c = new Character ( character ); 85 Image retval = (Image )images.get( c ); 86 if( retval == null ) 87 { 88 COSDictionary charProcs = (COSDictionary)font.getDictionaryObject( COSName.getPDFName( "CharProcs" ) ); 89 COSStream stream = (COSStream)charProcs.getDictionaryObject( COSName.getPDFName( "" + character ) ); 90 if( stream != null ) 91 { 92 Type3StreamParser parser = new Type3StreamParser(); 93 retval = parser.createImage( stream ); 94 images.put( c, retval ); 95 } 96 else 97 { 98 } 100 } 101 return retval; 102 103 } 104 105 116 public void drawString( String string, Graphics g, float fontSize, float x, float y ) throws IOException 117 { 118 { 120 for(int i=0; i<string.length(); i++) 121 { 122 char c = string.charAt( i ); 124 Image image = createImageIfNecessary( c ); 125 if( image != null ) 126 { 127 int newWidth = (int)(.12*image.getWidth(null)); 128 int newHeight = (int)(.12*image.getHeight(null)); 129 if( newWidth > 0 && newHeight > 0 ) 130 { 131 image = image.getScaledInstance( newWidth, newHeight, Image.SCALE_SMOOTH ); 132 g.drawImage( image, (int)x, (int)y, null ); 133 x+=newWidth; 134 } 135 } 136 } 137 } 138 } 139 140 145 public void setFontMatrix( PDMatrix matrix ) 146 { 147 font.setItem( "FontMatrix", matrix ); 148 } 149 } | Popular Tags |