1 31 package org.pdfbox.pdmodel.font; 32 33 import java.awt.Font ; 34 import java.awt.Graphics ; 35 import java.awt.Graphics2D ; 36 import java.awt.RenderingHints ; 37 import java.awt.geom.AffineTransform ; 38 import java.io.IOException ; 39 import java.util.HashMap ; 40 import java.util.Map ; 41 42 import org.pdfbox.cos.COSDictionary; 43 import org.pdfbox.cos.COSName; 44 45 51 public class PDType1Font extends PDSimpleFont 52 { 53 56 public static final PDType1Font TIMES_ROMAN = new PDType1Font( "Times-Roman" ); 57 60 public static final PDType1Font TIMES_BOLD = new PDType1Font( "Times-Bold" ); 61 64 public static final PDType1Font TIMES_ITALIC = new PDType1Font( "Times-Italic" ); 65 68 public static final PDType1Font TIMES_BOLD_ITALIC = new PDType1Font( "Times-BoldItalic" ); 69 72 public static final PDType1Font HELVETICA = new PDType1Font( "Helvetica" ); 73 76 public static final PDType1Font HELVETICA_BOLD = new PDType1Font( "Helvetica-Bold" ); 77 80 public static final PDType1Font HELVETICA_OBLIQUE = new PDType1Font( "Helvetica-Oblique" ); 81 84 public static final PDType1Font HELVETICA_BOLD_OBLIQUE = new PDType1Font( "Helvetica-BoldOblique" ); 85 88 public static final PDType1Font COURIER = new PDType1Font( "Courier" ); 89 92 public static final PDType1Font COURIER_BOLD = new PDType1Font( "Courier-Bold" ); 93 96 public static final PDType1Font COURIER_OBLIQUE = new PDType1Font( "Courier-Oblique" ); 97 100 public static final PDType1Font COURIER_BOLD_OBLIQUE = new PDType1Font( "Courier-BoldOblique" ); 101 104 public static final PDType1Font SYMBOL = new PDType1Font( "Symbol" ); 105 108 public static final PDType1Font ZAPF_DINGBATS = new PDType1Font( "ZapfDingbats" ); 109 110 111 private static final Map STANDARD_14 = new HashMap (); 112 static 113 { 114 STANDARD_14.put( TIMES_ROMAN.getBaseFont(), TIMES_ROMAN ); 115 STANDARD_14.put( TIMES_BOLD.getBaseFont(), TIMES_BOLD ); 116 STANDARD_14.put( TIMES_ITALIC.getBaseFont(), TIMES_ITALIC ); 117 STANDARD_14.put( TIMES_BOLD_ITALIC.getBaseFont(), TIMES_BOLD_ITALIC ); 118 STANDARD_14.put( HELVETICA.getBaseFont(), HELVETICA ); 119 STANDARD_14.put( HELVETICA_BOLD.getBaseFont(), HELVETICA_BOLD ); 120 STANDARD_14.put( HELVETICA_OBLIQUE.getBaseFont(), HELVETICA_OBLIQUE ); 121 STANDARD_14.put( HELVETICA_BOLD_OBLIQUE.getBaseFont(), HELVETICA_BOLD_OBLIQUE ); 122 STANDARD_14.put( COURIER.getBaseFont(), COURIER ); 123 STANDARD_14.put( COURIER_BOLD.getBaseFont(), COURIER_BOLD ); 124 STANDARD_14.put( COURIER_OBLIQUE.getBaseFont(), COURIER_OBLIQUE ); 125 STANDARD_14.put( COURIER_BOLD_OBLIQUE.getBaseFont(), COURIER_BOLD_OBLIQUE ); 126 STANDARD_14.put( SYMBOL.getBaseFont(), SYMBOL ); 127 STANDARD_14.put( ZAPF_DINGBATS.getBaseFont(), ZAPF_DINGBATS ); 128 } 129 130 private Font awtFont = null; 131 132 135 public PDType1Font() 136 { 137 super(); 138 font.setItem( COSName.SUBTYPE, COSName.getPDFName( "Type1" ) ); 139 } 140 141 146 public PDType1Font( COSDictionary fontDictionary ) 147 { 148 super( fontDictionary ); 149 } 150 151 156 public PDType1Font( String baseFont ) 157 { 158 this(); 159 setBaseFont( baseFont ); 160 } 161 162 169 public static PDType1Font getStandardFont( String name ) 170 { 171 return (PDType1Font)STANDARD_14.get( name ); 172 } 173 174 179 public static String [] getStandard14Names() 180 { 181 return (String [])STANDARD_14.keySet().toArray( new String [14] ); 182 } 183 184 187 public void drawString( String string, Graphics g, float fontSize, 188 float xScale, float yScale, float x, float y ) throws IOException 189 { 190 if( awtFont == null ) 191 { 192 String baseFont = this.getBaseFont(); 193 if( baseFont.equals( TIMES_ROMAN.getBaseFont() ) ) 194 { 195 awtFont = new Font ( "Times New Roman", Font.PLAIN, 1 ); 196 } 197 else if( baseFont.equals( TIMES_ITALIC.getBaseFont() ) ) 198 { 199 awtFont = new Font ( "Times New Roman", Font.ITALIC, 1 ); 200 } 201 else if( baseFont.equals( TIMES_BOLD.getBaseFont() ) ) 202 { 203 awtFont = new Font ( "Times New Roman", Font.BOLD, 1 ); 204 } 205 else if( baseFont.equals( TIMES_BOLD_ITALIC.getBaseFont() ) ) 206 { 207 awtFont = new Font ( "Times New Roman", Font.BOLD | Font.ITALIC, 1 ); 208 } 209 else if( baseFont.equals( HELVETICA.getBaseFont() ) ) 210 { 211 awtFont = new Font ( "Helvetica", Font.PLAIN, 1 ); 212 } 213 else if( baseFont.equals( HELVETICA_BOLD.getBaseFont() ) ) 214 { 215 awtFont = new Font ( "Helvetica", Font.BOLD, 1 ); 216 } 217 else if( baseFont.equals( HELVETICA_BOLD_OBLIQUE.getBaseFont() ) ) 218 { 219 awtFont = new Font ( "Helvetica", Font.BOLD | Font.ITALIC, 1 ); 220 } 221 else if( baseFont.equals( HELVETICA_OBLIQUE.getBaseFont() ) ) 222 { 223 awtFont = new Font ( "Helvetica", Font.ITALIC, 1 ); 224 } 225 else if( baseFont.equals( COURIER.getBaseFont() ) ) 226 { 227 awtFont = new Font ( "Courier", Font.PLAIN, 1 ); 228 } 229 else if( baseFont.equals( COURIER_BOLD.getBaseFont() ) ) 230 { 231 awtFont = new Font ( "Courier", Font.BOLD, 1 ); 232 } 233 else if( baseFont.equals( COURIER_BOLD_OBLIQUE.getBaseFont() ) ) 234 { 235 awtFont = new Font ( "Courier", Font.BOLD | Font.ITALIC, 1 ); 236 } 237 else if( baseFont.equals( COURIER_OBLIQUE.getBaseFont() ) ) 238 { 239 awtFont = new Font ( "Courier", Font.ITALIC, 1 ); 240 } 241 else if( baseFont.equals( SYMBOL.getBaseFont() ) ) 242 { 243 awtFont = new Font ( "Symbol", Font.PLAIN, 1 ); 244 } 245 else if( baseFont.equals( ZAPF_DINGBATS.getBaseFont() ) ) 246 { 247 awtFont = new Font ( "ZapfDingbats", Font.PLAIN, 1 ); 248 } 249 else 250 { 251 awtFont = new Font ( "Arial", Font.PLAIN, 1 ); 252 } 256 } 257 AffineTransform at = new AffineTransform (); 258 at.scale( xScale, yScale ); 259 260 Graphics2D g2d = (Graphics2D )g; 261 g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 262 g2d.setFont( awtFont.deriveFont( at ).deriveFont( fontSize ) ); 263 265 g2d.drawString( string, (int)x, (int)y ); 266 } 267 } | Popular Tags |