1 51 package org.apache.fop.pdf; 52 53 import java.io.UnsupportedEncodingException ; 55 56 65 public class PDFFont extends PDFObject { 66 67 70 public static final byte TYPE0 = 0; 71 72 75 public static final byte TYPE1 = 1; 76 77 80 public static final byte MMTYPE1 = 2; 81 82 85 public static final byte TYPE3 = 3; 86 87 90 public static final byte TRUETYPE = 4; 91 92 95 protected static final String [] TYPE_NAMES = 96 new String [] { 98 "Type0", "Type1", "MMType1", "Type3", "TrueType" 99 }; 100 101 104 protected String fontname; 105 106 110 protected byte subtype; 111 112 115 protected String basefont; 116 117 124 protected Object encoding; 125 126 129 131 141 public PDFFont(int number, String fontname, byte subtype, 142 String basefont, 143 Object encoding ) { 144 145 146 super(number); 147 148 149 this.fontname = fontname; 150 this.subtype = subtype; 151 this.basefont = basefont; 152 this.encoding = encoding; 153 } 155 156 165 public static PDFFont createFont(int number, String fontname, 166 byte subtype, String basefont, 167 Object encoding) { 168 switch (subtype) { 169 case TYPE0: 170 return new PDFFontType0(number, fontname, subtype, basefont, 171 encoding); 172 case TYPE1: 173 case MMTYPE1: 174 return new PDFFontType1(number, fontname, subtype, basefont, 175 encoding); 176 180 case TRUETYPE: 181 return new PDFFontTrueType(number, fontname, subtype, basefont, 182 encoding); 183 } 184 return null; } 186 187 201 public static PDFFont createFont(int number, String fontname, 202 byte subtype, String basefont, 203 Object encoding, int firstChar, 204 int lastChar, PDFArray widths, 205 PDFFontDescriptor descriptor) { 206 207 PDFFontNonBase14 font; 208 switch (subtype) { 209 case TYPE0: 210 font = new PDFFontType0(number, fontname, subtype, basefont, 211 encoding); 212 font.setDescriptor(descriptor); 213 return font; 214 case TYPE1: 215 case MMTYPE1: 216 font = new PDFFontType1(number, fontname, subtype, basefont, 217 encoding); 218 font.setWidthMetrics(firstChar, lastChar, widths); 219 font.setDescriptor(descriptor); 220 return font; 221 case TYPE3: 222 return null; 224 case TRUETYPE: 225 font = new PDFFontTrueType(number, fontname, subtype, basefont, 226 encoding); 227 font.setWidthMetrics(firstChar, lastChar, widths); 228 font.setDescriptor(descriptor); 229 return font; 230 231 } 232 return null; } 234 235 240 public String getName() { 241 return this.fontname; 242 } 243 244 249 public byte[] toPDF() { 250 StringBuffer p = new StringBuffer (); 251 p.append(this.number + " " + this.generation 252 + " obj\n<< /Type /Font\n/Subtype /" 253 + TYPE_NAMES[this.subtype] + "\n/Name /" + this.fontname 254 + "\n/BaseFont /" + this.basefont); 255 if (encoding != null) { 256 p.append("\n/Encoding "); 257 if (encoding instanceof PDFEncoding) { 258 p.append(((PDFEncoding)this.encoding).referencePDF()); 259 } else if (encoding instanceof PDFStream) { 260 p.append(((PDFStream)this.encoding).referencePDF()); 261 } else { 262 p.append("/").append((String )encoding); 263 } 264 } 265 fillInPDF(p); 266 p.append(" >>\nendobj\n"); 267 268 try { 269 return p.toString().getBytes(PDFDocument.ENCODING); 270 } catch (UnsupportedEncodingException ue) { 271 return p.toString().getBytes(); 272 } 273 } 274 275 282 protected void fillInPDF(StringBuffer begin) {} 283 284 } 285 | Popular Tags |