KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > fonts > MultiByteFont


1 /*
2  * $Id: MultiByteFont.java,v 1.4.2.3 2003/02/25 14:58:09 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.render.pdf.fonts;
52
53 import org.apache.fop.fonts.FontFileReader;
54 import org.apache.fop.fonts.TTFSubSetFile;
55 import org.apache.fop.layout.FontDescriptor;
56 import org.apache.fop.messaging.MessageHandler;
57 import org.apache.fop.pdf.PDFCIDFont;
58 import org.apache.fop.pdf.PDFStream;
59 import org.apache.fop.pdf.PDFTTFStream;
60 import org.apache.fop.pdf.PDFWArray;
61 import org.apache.fop.render.pdf.CIDFont;
62
63 import java.io.InputStream JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.util.Map JavaDoc;
66 import java.net.URL JavaDoc;
67
68 /**
69  * Generic MultiByte (CID) font
70  */

71 public class MultiByteFont extends CIDFont implements FontDescriptor {
72     public String JavaDoc fontName = null;
73     public String JavaDoc ttcName = null;
74     public String JavaDoc encoding = "Identity-H";
75
76     public int capHeight = 0;
77     public int xHeight = 0;
78     public int ascender = 0;
79     public int descender = 0;
80     public int[] fontBBox = {
81         0, 0, 0, 0
82     };
83
84     public URL JavaDoc embedFileName = null;
85     public String JavaDoc embedResourceName = null;
86     public PDFTTFStream embeddedFont = null;
87
88     public int flags = 4;
89     public int stemV = 0;
90     public int italicAngle = 0;
91     public int missingWidth = 0;
92     public int defaultWidth = 0;
93     public byte cidType = PDFCIDFont.CID_TYPE2;
94
95     public Map JavaDoc kerning = new java.util.HashMap JavaDoc();
96     public boolean useKerning = true;
97     private String JavaDoc namePrefix = null; // Quasi unique prefix
98
private static int uniqueCounter = 1;
99     public PDFWArray warray = new PDFWArray();
100     public int width[] = null;
101
102     public BFEntry[] bfentries = null;
103
104
105     /**
106      * usedGlyphs contains orginal, new glyph index
107      */

108     private Map JavaDoc usedGlyphs = new java.util.HashMap JavaDoc();
109
110     /**
111      * usedGlyphsIndex contains new glyph, original index
112      */

113     private Map JavaDoc usedGlyphsIndex = new java.util.HashMap JavaDoc();
114     int usedGlyphsCount = 0;
115
116     public MultiByteFont() {
117         // Make sure that the 3 first glyphs are included
118
usedGlyphs.put(new Integer JavaDoc(0), new Integer JavaDoc(0));
119         usedGlyphsIndex.put(new Integer JavaDoc(0), new Integer JavaDoc(0));
120         usedGlyphsCount++;
121         usedGlyphs.put(new Integer JavaDoc(1), new Integer JavaDoc(1));
122         usedGlyphsIndex.put(new Integer JavaDoc(1), new Integer JavaDoc(1));
123         usedGlyphsCount++;
124         usedGlyphs.put(new Integer JavaDoc(2), new Integer JavaDoc(2));
125         usedGlyphsIndex.put(new Integer JavaDoc(2), new Integer JavaDoc(2));
126         usedGlyphsCount++;
127
128         // Create a quasiunique prefix for fontname
129
int cnt = 0;
130         synchronized (this.getClass()) {
131             cnt = uniqueCounter++;
132         }
133         int ctm = (int)(System.currentTimeMillis() & 0xffff);
134         namePrefix = new String JavaDoc(cnt + "E" + Integer.toHexString(ctm));
135     }
136
137     public final boolean hasKerningInfo() {
138         return (useKerning & kerning.isEmpty());
139     }
140
141     public final Map JavaDoc getKerningInfo() {
142         if (useKerning)
143             return kerning;
144         else
145             return new java.util.HashMap JavaDoc();
146     }
147
148     public byte getSubType() {
149         return org.apache.fop.pdf.PDFFont.TYPE0;
150     }
151
152     public String JavaDoc getLang() {
153         return null;
154     }
155
156     public String JavaDoc getPanose() {
157         return null;
158     }
159
160     public int getAvgWidth() {
161         return -1;
162     }
163
164     public int getMinWidth() {
165         return -1;
166     }
167
168     public int getMaxWidth() {
169         return -1;
170     }
171
172     public int getleading() {
173         return -1;
174     }
175
176     public int getStemH() {
177         return 0;
178     }
179
180     public int getMissingWidth() {
181         return missingWidth;
182     }
183
184     public int getDefaultWidth() {
185         return defaultWidth;
186     }
187
188     public String JavaDoc getRegistry() {
189         return "Adobe";
190     }
191
192     public String JavaDoc getOrdering() {
193         return "UCS";
194     }
195
196     public int getSupplement() {
197         return 0;
198     }
199
200     public byte getCidType() {
201         return cidType;
202     }
203
204     public String JavaDoc getCidBaseFont() {
205         return isEmbeddable() ? namePrefix + fontName : fontName;
206     }
207
208     public String JavaDoc getCharEncoding() {
209         return "Identity-H";
210     }
211
212     public PDFWArray getWidths() {
213         if (isEmbeddable()) {
214             // Create widths for reencoded chars
215
warray = new PDFWArray();
216             int[] tmpWidth = new int[usedGlyphsCount];
217
218             for (int i = 0; i < usedGlyphsCount; i++) {
219                 Integer JavaDoc nw = (Integer JavaDoc)usedGlyphsIndex.get(new Integer JavaDoc(i));
220                 int nwx = (nw == null) ? 0 : nw.intValue();
221                 tmpWidth[i] = width[nwx];
222             }
223             warray.addEntry(0, tmpWidth);
224         }
225         return warray;
226     }
227
228     public boolean isEmbeddable() {
229         return (embedFileName == null && embedResourceName == null) ? false
230                : true;
231     }
232
233
234     public PDFStream getFontFile(int i) {
235         try {
236             InputStream JavaDoc in = embedFileName.openStream();
237             FontFileReader reader = new FontFileReader(in);
238             in.close();
239             TTFSubSetFile subset = new TTFSubSetFile();
240
241             byte[] subsetFont = subset.readFont(reader, ttcName, usedGlyphs);
242             // Only TrueType CID fonts are supported now
243

244             embeddedFont = new PDFTTFStream(i, subsetFont.length);
245             embeddedFont.addFilter("flate");
246             embeddedFont.addFilter("ascii-85");
247             embeddedFont.setData(subsetFont, subsetFont.length);
248         } catch (IOException JavaDoc ioe) {
249             MessageHandler.errorln("Failed to embed font [" + i + "] "
250                                    + fontName + ": " + ioe.getMessage());
251             return (PDFStream)null;
252         }
253
254         return (PDFStream)embeddedFont;
255     }
256
257     public String JavaDoc encoding() {
258         return encoding;
259     }
260
261     public String JavaDoc fontName() {
262         return isEmbeddable() ? namePrefix + fontName : fontName;
263     }
264
265     public int getAscender() {
266         return ascender;
267     }
268
269     public int getDescender() {
270         return descender;
271     }
272
273     public int getCapHeight() {
274         return capHeight;
275     }
276
277     public int getAscender(int size) {
278         return size * ascender;
279     }
280
281     public int getCapHeight(int size) {
282         return size * capHeight;
283     }
284
285     public int getDescender(int size) {
286         return size * descender;
287     }
288
289     public int getXHeight(int size) {
290         return size * xHeight;
291     }
292
293     public int getFlags() {
294         return flags;
295     }
296
297     public int[] getFontBBox() {
298         return fontBBox;
299     }
300
301     public int getItalicAngle() {
302         return italicAngle;
303     }
304
305     public int getStemV() {
306         return stemV;
307     }
308
309     public int getFirstChar() {
310         return 0;
311     }
312
313     public int getLastChar() {
314         return 255;
315     }
316
317     public int width(int i, int size) {
318         if (isEmbeddable()) {
319             Integer JavaDoc idx = (Integer JavaDoc)usedGlyphsIndex.get(new Integer JavaDoc(i));
320             return size * width[idx.intValue()];
321         } else {
322             return size * width[i];
323         }
324     }
325
326     public int[] getWidths(int size) {
327         int[] arr = new int[width.length];
328         System.arraycopy(width, 0, arr, 0, width.length - 1);
329         for (int i = 0; i < arr.length; i++)
330             arr[i] *= size;
331         return arr;
332     }
333
334     public Integer JavaDoc reMap(Integer JavaDoc i) {
335         if (isEmbeddable()) {
336             Integer JavaDoc ret = (Integer JavaDoc)usedGlyphsIndex.get(i);
337             if (ret == null)
338                 ret = i;
339             return ret;
340         } else {
341             return i;
342         }
343
344     }
345
346     public char mapChar(char c) {
347         int idx = (int)c;
348         int retIdx = 0;
349
350         for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) {
351             if (bfentries[i].unicodeStart <= idx
352                     && bfentries[i].unicodeEnd >= idx) {
353                 retIdx = bfentries[i].glyphStartIndex + idx
354                          - bfentries[i].unicodeStart;
355             }
356         }
357
358         if (isEmbeddable()) {
359             // Reencode to a new subset font or get
360
// the reencoded value
361
Integer JavaDoc newIdx = (Integer JavaDoc)usedGlyphs.get(new Integer JavaDoc(retIdx));
362             if (newIdx == null) {
363                 usedGlyphs.put(new Integer JavaDoc(retIdx),
364                                new Integer JavaDoc(usedGlyphsCount));
365                 usedGlyphsIndex.put(new Integer JavaDoc(usedGlyphsCount),
366                                     new Integer JavaDoc(retIdx));
367                 retIdx = usedGlyphsCount;
368                 // System.out.println(c+"("+(int)c+") = "+retIdx);
369
usedGlyphsCount++;
370             } else {
371                 retIdx = newIdx.intValue();
372             }
373         }
374
375         return (char)retIdx;
376     }
377
378 }
379
380
381
382
383
384
385
386
387
388
389
Popular Tags