1 31 32 package org.pdfbox.pdmodel.font; 33 34 import java.io.BufferedInputStream ; 35 import java.io.FileInputStream ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.util.ArrayList ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 42 import org.fontbox.afm.AFMParser; 43 import org.fontbox.afm.CharMetric; 44 import org.fontbox.afm.FontMetric; 45 46 import org.fontbox.pfb.PfbParser; 47 48 import org.pdfbox.encoding.AFMEncoding; 49 import org.pdfbox.pdmodel.PDDocument; 50 import org.pdfbox.pdmodel.common.PDRectangle; 51 import org.pdfbox.pdmodel.common.PDStream; 52 53 60 public class PDType1AfmPfbFont extends PDType1Font 61 { 62 65 private static final int BUFFERSIZE = 0xffff; 66 67 70 private PDFontDescriptorDictionary fd; 71 72 75 private FontMetric metric; 76 77 83 public PDType1AfmPfbFont(final PDDocument doc, final String afmname) 84 throws IOException 85 { 86 87 super(); 88 89 InputStream afmin = new BufferedInputStream ( 90 new FileInputStream (afmname), BUFFERSIZE); 91 String pfbname = afmname.replaceAll(".AFM", "").replaceAll(".afm", "") 92 + ".pfb"; 93 InputStream pfbin = new BufferedInputStream ( 94 new FileInputStream (pfbname), BUFFERSIZE); 95 96 load(doc, afmin, pfbin); 97 } 98 99 106 public PDType1AfmPfbFont(final PDDocument doc, final InputStream afm, final InputStream pfb) 107 throws IOException 108 { 109 super(); 110 111 load(doc, afm, pfb); 112 } 113 114 122 private void load(final PDDocument doc, final InputStream afm, 123 final InputStream pfb) throws IOException 124 { 125 126 fd = new PDFontDescriptorDictionary(); 127 setFontDescriptor(fd); 128 129 PfbParser pfbparser = new PfbParser(pfb); 131 pfb.close(); 132 133 PDStream fontStream = new PDStream(doc, pfbparser.getInputStream(), 134 false); 135 fontStream.getStream().setInt("Length", pfbparser.size()); 136 for (int i = 0; i < pfbparser.getLengths().length; i++) 137 { 138 fontStream.getStream().setInt("Length" + (i + 1), 139 pfbparser.getLengths()[i]); 140 } 141 fontStream.addCompression(); 142 fd.setFontFile(fontStream); 143 144 AFMParser parser = new AFMParser(afm); 146 parser.parse(); 147 metric = parser.getResult(); 148 setEncoding(new AFMEncoding(metric)); 149 150 setBaseFont(metric.getFontName()); 152 fd.setFontName(metric.getFontName()); 153 fd.setFontFamily(metric.getFamilyName()); 154 fd.setNonSymbolic(true); 155 fd.setFontBoundingBox(new PDRectangle(metric.getFontBBox())); 156 fd.setItalicAngle(metric.getItalicAngle()); 157 fd.setAscent(metric.getAscender()); 158 fd.setDescent(metric.getDescender()); 159 fd.setCapHeight(metric.getCapHeight()); 160 fd.setXHeight(metric.getXHeight()); 161 fd.setAverageWidth(metric.getAverageCharacterWidth()); 162 fd.setCharacterSet(metric.getCharacterSet()); 163 164 int firstchar = 255; 166 int lastchar = 0; 167 168 List listmetric = metric.getCharMetrics(); 170 171 int maxWidths = 256; 172 List widths = new ArrayList (maxWidths); 173 Integer zero = new Integer (0); 174 Iterator iter = listmetric.iterator(); 175 while (iter.hasNext()) 176 { 177 CharMetric m = (CharMetric) iter.next(); 178 int n = m.getCharacterCode(); 179 if (n > 0) 180 { 181 firstchar = Math.min(firstchar, n); 182 lastchar = Math.max(lastchar, n); 183 if (m.getWx() > 0) 184 { 185 float width = m.getWx(); 186 widths.add(new Float (width)); 187 } 188 else 189 { 190 widths.add(zero); 191 } 192 } 193 } 194 setFirstChar(firstchar); 195 setLastChar(lastchar); 196 setWidths(widths); 197 198 } 199 200 203 public PDFontDescriptor getFontDescriptor() throws IOException 204 { 205 return fd; 206 } 207 208 } | Popular Tags |