KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > font > PDType1AfmPfbFont


1 /**
2  * Copyright (c) 2004-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31
32 package org.pdfbox.pdmodel.font;
33
34 import java.io.BufferedInputStream JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
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 /**
54  * This is implementation of the Type1 Font
55  * with a afm and a pfb file.
56  *
57  * @author <a HREF="mailto:m.g.n@gmx.de">Michael Niedermair</a>
58  * @version $Revision: 1.5 $
59  */

60 public class PDType1AfmPfbFont extends PDType1Font
61 {
62     /**
63      * the buffersize.
64      */

65     private static final int BUFFERSIZE = 0xffff;
66     
67     /**
68      * The font descriptor.
69      */

70     private PDFontDescriptorDictionary fd;
71
72     /**
73      * The font metric.
74      */

75     private FontMetric metric;
76
77     /**
78      * Create a new object.
79      * @param doc The PDF document that will hold the embedded font.
80      * @param afmname The font filename.
81      * @throws IOException If there is an error loading the data.
82      */

83     public PDType1AfmPfbFont(final PDDocument doc, final String JavaDoc afmname)
84         throws IOException JavaDoc
85     {
86
87         super();
88
89         InputStream JavaDoc afmin = new BufferedInputStream JavaDoc(
90                 new FileInputStream JavaDoc(afmname), BUFFERSIZE);
91         String JavaDoc pfbname = afmname.replaceAll(".AFM", "").replaceAll(".afm", "")
92                 + ".pfb";
93         InputStream JavaDoc pfbin = new BufferedInputStream JavaDoc(
94                 new FileInputStream JavaDoc(pfbname), BUFFERSIZE);
95
96         load(doc, afmin, pfbin);
97     }
98
99     /**
100      * Create a new object.
101      * @param doc The PDF document that will hold the embedded font.
102      * @param afm The afm input.
103      * @param pfb The pfb input.
104      * @throws IOException If there is an error loading the data.
105      */

106     public PDType1AfmPfbFont(final PDDocument doc, final InputStream JavaDoc afm, final InputStream JavaDoc pfb)
107         throws IOException JavaDoc
108     {
109         super();
110
111         load(doc, afm, pfb);
112     }
113
114     /**
115      * This will load a afm and pfb to be embedding into a document.
116      *
117      * @param doc The PDF document that will hold the embedded font.
118      * @param afm The afm input.
119      * @param pfb The pfb input.
120      * @throws IOException If there is an error loading the data.
121      */

122     private void load(final PDDocument doc, final InputStream JavaDoc afm,
123             final InputStream JavaDoc pfb) throws IOException JavaDoc
124     {
125
126         fd = new PDFontDescriptorDictionary();
127         setFontDescriptor(fd);
128
129         // read the pfb
130
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         // read the afm
145
AFMParser parser = new AFMParser(afm);
146         parser.parse();
147         metric = parser.getResult();
148         setEncoding(new AFMEncoding(metric));
149
150         // set the values
151
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         // get firstchar, lastchar
165
int firstchar = 255;
166         int lastchar = 0;
167
168         // widths
169
List JavaDoc listmetric = metric.getCharMetrics();
170
171         int maxWidths = 256;
172         List JavaDoc widths = new ArrayList JavaDoc(maxWidths);
173         Integer JavaDoc zero = new Integer JavaDoc(0);
174         Iterator JavaDoc 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 JavaDoc(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     /**
201      * {@inheritDoc}
202      */

203     public PDFontDescriptor getFontDescriptor() throws IOException JavaDoc
204     {
205         return fd;
206     }
207
208 }
Popular Tags