KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > Type3Font


1 /*
2  * Copyright 2005 by Paulo Soares.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47
48 package com.lowagie.text.pdf;
49
50 import java.util.HashMap JavaDoc;
51
52 import com.lowagie.text.DocumentException;
53
54 /**
55  * A class to support Type3 fonts.
56  */

57 public class Type3Font extends BaseFont {
58     
59     private boolean[] usedSlot;
60     private IntHashtable widths3 = new IntHashtable();
61     private HashMap JavaDoc char2glyph = new HashMap JavaDoc();
62     private PdfWriter writer;
63     private float llx = Float.NaN, lly, urx, ury;
64     private PageResources pageResources = new PageResources();
65     private boolean colorized;
66     
67     /**
68      * Creates a Type3 font.
69      * @param writer the writer
70      * @param chars an array of chars corresponding to the glyphs used (not used, prisent for compability only)
71      * @param colorized if <CODE>true</CODE> the font may specify color, if <CODE>false</CODE> no color commands are allowed
72      * and only images as masks can be used
73      */

74     public Type3Font(PdfWriter writer, char[] chars, boolean colorized) {
75         this(writer, colorized);
76     }
77     
78     /**
79      * Creates a Type3 font. This implementation assumes that the /FontMatrix is
80      * [0.001 0 0 0.001 0 0] or a 1000-unit glyph coordinate system.
81      * <p>
82      * An example:
83      * <p>
84      * <pre>
85      * Document document = new Document(PageSize.A4);
86      * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("type3.pdf"));
87      * document.open();
88      * Type3Font t3 = new Type3Font(writer, false);
89      * PdfContentByte g = t3.defineGlyph('a', 1000, 0, 0, 750, 750);
90      * g.rectangle(0, 0, 750, 750);
91      * g.fill();
92      * g = t3.defineGlyph('b', 1000, 0, 0, 750, 750);
93      * g.moveTo(0, 0);
94      * g.lineTo(375, 750);
95      * g.lineTo(750, 0);
96      * g.fill();
97      * Font f = new Font(t3, 12);
98      * document.add(new Paragraph("ababab", f));
99      * document.close();
100      * </pre>
101      * @param writer the writer
102      * @param colorized if <CODE>true</CODE> the font may specify color, if <CODE>false</CODE> no color commands are allowed
103      * and only images as masks can be used
104      */

105     public Type3Font(PdfWriter writer, boolean colorized) {
106         this.writer = writer;
107         this.colorized = colorized;
108         fontType = FONT_TYPE_T3;
109         usedSlot = new boolean[256];
110     }
111     
112     /**
113      * Defines a glyph. If the character was already defined it will return the same content
114      * @param c the character to match this glyph.
115      * @param wx the advance this character will have
116      * @param llx the X lower left corner of the glyph bounding box. If the <CODE>colorize</CODE> option is
117      * <CODE>true</CODE> the value is ignored
118      * @param lly the Y lower left corner of the glyph bounding box. If the <CODE>colorize</CODE> option is
119      * <CODE>true</CODE> the value is ignored
120      * @param urx the X upper right corner of the glyph bounding box. If the <CODE>colorize</CODE> option is
121      * <CODE>true</CODE> the value is ignored
122      * @param ury the Y upper right corner of the glyph bounding box. If the <CODE>colorize</CODE> option is
123      * <CODE>true</CODE> the value is ignored
124      * @return a content where the glyph can be defined
125      */

126     public PdfContentByte defineGlyph(char c, float wx, float llx, float lly, float urx, float ury) {
127         if (c == 0 || c > 255)
128             throw new IllegalArgumentException JavaDoc("The char " + (int)c + " doesn't belong in this Type3 font");
129         usedSlot[c] = true;
130         Integer JavaDoc ck = new Integer JavaDoc((int)c);
131         Type3Glyph glyph = (Type3Glyph)char2glyph.get(ck);
132         if (glyph != null)
133             return glyph;
134         widths3.put(c, (int)wx);
135         if (!colorized) {
136             if (Float.isNaN(this.llx)) {
137                 this.llx = llx;
138                 this.lly = lly;
139                 this.urx = urx;
140                 this.ury = ury;
141             }
142             else {
143                 this.llx = Math.min(this.llx, llx);
144                 this.lly = Math.min(this.lly, lly);
145                 this.urx = Math.max(this.urx, urx);
146                 this.ury = Math.max(this.ury, ury);
147             }
148         }
149         glyph = new Type3Glyph(writer, pageResources, wx, llx, lly, urx, ury, colorized);
150         char2glyph.put(ck, glyph);
151         return glyph;
152     }
153     
154     public String JavaDoc[][] getFamilyFontName() {
155         return new String JavaDoc[0][];
156     }
157     
158     public float getFontDescriptor(int key, float fontSize) {
159         return 0;
160     }
161     
162     public String JavaDoc[][] getFullFontName() {
163         return new String JavaDoc[0][];
164     }
165     
166     public int getKerning(char char1, char char2) {
167         return 0;
168     }
169     
170     public String JavaDoc getPostscriptFontName() {
171         return "";
172     }
173     
174     protected int[] getRawCharBBox(int c, String JavaDoc name) {
175         return null;
176     }
177     
178     int getRawWidth(int c, String JavaDoc name) {
179         return 0;
180     }
181     
182     public boolean hasKernPairs() {
183         return false;
184     }
185     
186     public boolean setKerning(char char1, char char2, int kern) {
187         return false;
188     }
189     
190     public void setPostscriptFontName(String JavaDoc name) {
191     }
192     
193     void writeFont(PdfWriter writer, PdfIndirectReference ref, Object JavaDoc[] params) throws com.lowagie.text.DocumentException, java.io.IOException JavaDoc {
194         if (this.writer != writer)
195             throw new IllegalArgumentException JavaDoc("Type3 font used with the wrong PdfWriter");
196         
197         // Get first & lastchar ...
198
int firstChar = 0;
199         while( firstChar < usedSlot.length && !usedSlot[firstChar] ) firstChar++;
200         
201         if ( firstChar == usedSlot.length ) {
202             throw new DocumentException( "No glyphs defined for Type3 font" );
203         }
204         int lastChar = usedSlot.length - 1;
205         while( lastChar >= firstChar && !usedSlot[lastChar] ) lastChar--;
206         
207         int[] widths = new int[lastChar - firstChar + 1];
208         int[] invOrd = new int[lastChar - firstChar + 1];
209         
210         int invOrdIndx = 0, w = 0;
211         for( int u = firstChar; u<=lastChar; u++, w++ ) {
212             if ( usedSlot[u] ) {
213                 invOrd[invOrdIndx++] = u;
214                 widths[w] = widths3.get(u);
215             }
216         }
217         PdfArray diffs = new PdfArray();
218         PdfDictionary charprocs = new PdfDictionary();
219         int last = -1;
220         for (int k = 0; k < invOrdIndx; ++k) {
221             int c = invOrd[k];
222             if (c > last) {
223                 last = c;
224                 diffs.add(new PdfNumber(last));
225             }
226             ++last;
227             int c2 = invOrd[k];
228             String JavaDoc s = GlyphList.unicodeToName(c2);
229             if (s == null)
230                 s = "a" + c2;
231             PdfName n = new PdfName(s);
232             diffs.add(n);
233             Type3Glyph glyph = (Type3Glyph)char2glyph.get(new Integer JavaDoc(c2));
234             PdfStream stream = new PdfStream(glyph.toPdf(null));
235             stream.flateCompress();
236             PdfIndirectReference refp = writer.addToBody(stream).getIndirectReference();
237             charprocs.put(n, refp);
238         }
239         PdfDictionary font = new PdfDictionary(PdfName.FONT);
240         font.put(PdfName.SUBTYPE, PdfName.TYPE3);
241         if (colorized)
242             font.put(PdfName.FONTBBOX, new PdfRectangle(0, 0, 0, 0));
243         else
244             font.put(PdfName.FONTBBOX, new PdfRectangle(llx, lly, urx, ury));
245         font.put(PdfName.FONTMATRIX, new PdfArray(new float[]{0.001f, 0, 0, 0.001f, 0, 0}));
246         font.put(PdfName.CHARPROCS, writer.addToBody(charprocs).getIndirectReference());
247         PdfDictionary encoding = new PdfDictionary();
248         encoding.put(PdfName.DIFFERENCES, diffs);
249         font.put(PdfName.ENCODING, writer.addToBody(encoding).getIndirectReference());
250         font.put(PdfName.FIRSTCHAR, new PdfNumber(firstChar));
251         font.put(PdfName.LASTCHAR, new PdfNumber(lastChar));
252         font.put(PdfName.WIDTHS, writer.addToBody(new PdfArray(widths)).getIndirectReference());
253         if (pageResources.hasResources())
254             font.put(PdfName.RESOURCES, writer.addToBody(pageResources.getResources()).getIndirectReference());
255         writer.addToBody(font, ref);
256     }
257     
258     
259     byte[] convertToBytes(String JavaDoc text) {
260         char[] cc = text.toCharArray();
261         byte[] b = new byte[cc.length];
262         int p = 0;
263         for (int k = 0; k < cc.length; ++k) {
264             char c = cc[k];
265             if (charExists(c))
266                 b[p++] = (byte)c;
267         }
268         if (b.length == p)
269             return b;
270         byte[] b2 = new byte[p];
271         System.arraycopy(b, 0, b2, 0, p);
272         return b2;
273     }
274     
275     public int getWidth(char char1) {
276         if (!widths3.containsKey(char1))
277             throw new IllegalArgumentException JavaDoc("The char " + (int)char1 + " is not defined in a Type3 font");
278         return widths3.get(char1);
279     }
280     
281     public int getWidth(String JavaDoc text) {
282         char[] c = text.toCharArray();
283         int total = 0;
284         for (int k = 0; k < c.length; ++k)
285             total += getWidth(c[k]);
286         return total;
287     }
288     
289     public int[] getCharBBox(char c) {
290         return null;
291     }
292     
293     public boolean charExists(char c) {
294         if (c > 0 && c < 256) {
295             return usedSlot[c];
296         } else {
297             return false;
298         }
299     }
300     
301     public boolean setCharAdvance(char c, int advance) {
302         return false;
303     }
304     
305 }
306
Popular Tags