KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFFont


1 /*
2  * $Id: PDFFont.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
3  *
4  * $Date: 2001/10/29 19:51:08 $
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package gnu.jpdf;
21
22 import java.awt.Font JavaDoc;
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * This class defines a font within a PDF document.
28  *
29  * @author Peter T Mount,http://www.retep.org.uk/pdf/
30  * @author Eric Z. Beard, ericzbeard@hotmail.com
31  * @author $Author: ezb $
32  * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
33  */

34 public class PDFFont extends PDFObject implements Serializable
35 {
36
37   /*
38    * NOTE: The original class is the work of Peter T. Mount, who released it
39    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
40    * follows:
41    * The package name was changed to gnu.pdf.
42    * The formatting was changed a little bit
43    * It is still licensed under the LGPL.
44    */

45   
46   /**
47    * The PDF document name of the font
48    */

49   private String JavaDoc name;
50   
51   /**
52    * The PDF type of the font, usually /Type1
53    */

54   private String JavaDoc type;
55   
56   /**
57    * The font's real name
58    */

59   private String JavaDoc font;
60   
61   /**
62    * The name of the equivalent Java font
63    */

64   private String JavaDoc javaFont;
65   
66   /**
67    * The PDF Style, ie: BOLD, ITALIC, etc
68    */

69   private int style;
70   
71   /**
72    * This constructs a default PDFFont. In this case Helvetica
73    */

74   protected PDFFont() {
75     this("/F1","/Type1","Helvetica",Font.PLAIN);
76   }
77   
78   /**
79    * Constructs a PDFFont. This will attempt to map the font from a known
80    * Java font name to that in PDF, defaulting to Helvetica if not possible.
81    *
82    * @param name The document name, ie /F1
83    * @param type The pdf type, ie /Type1
84    * @param font The font name, ie Helvetica
85    * @param style The java.awt.Font style, ie: Font.PLAIN
86    */

87   public PDFFont(String JavaDoc name,String JavaDoc type,String JavaDoc font,int style) {
88     super("/Font");
89     this.name = name;
90     this.type = type;
91     this.style = style;
92     
93     String JavaDoc f = font.toLowerCase();
94     
95     // default PDF Font name
96
this.font = base14[0][1];
97     this.javaFont = base14[0][0];
98     
99     // attempt to translate the font name from Java to PDF
100
for(int i=0;i<base14.length;i++) {
101       if(base14[i][0].equals(f)) {
102         this.javaFont = base14[i][0];
103         this.font = base14[i][1+style];
104         //System.out.println("Setting a font style to: " + this.font);
105
break;
106       }
107     }
108   }
109   
110   /**
111    * This is the most common method to use.
112    * @return the Font name within the PDF document.
113    */

114   public String JavaDoc getName() {
115     return name;
116   }
117   
118   /**
119    * @return the Font's PDF type
120    */

121   public String JavaDoc getType() {
122     return type;
123   }
124   
125   /**
126    * @return The PDF Font name
127    */

128   public String JavaDoc getFont() {
129     return font;
130   }
131   
132   /**
133    * @return the font style.
134    * @see java.awt.Font
135    */

136   public int getStyle() {
137     return style;
138   }
139   
140   /**
141    * @param os OutputStream to send the object to
142    * @exception IOException on error
143    */

144   public void write(OutputStream os) throws IOException {
145     // Write the object header
146
writeStart(os);
147     
148     // now the objects body
149
os.write("/Subtype ".getBytes());
150     os.write(type.getBytes());
151     os.write("\n/Name ".getBytes());
152     os.write(name.getBytes());
153     os.write("\n/BaseFont ".getBytes());
154     os.write(font.getBytes());
155     // The performance problem in Bug#106693 comments out the
156
// encoding line, and removes the /WinAnsiEncoding. I'm going
157
// to leave them in, as the Encoding fixes another problem.
158
os.write("\n/Encoding ".getBytes());
159     os.write("/WinAnsiEncoding".getBytes());
160     //os.write(encoding.getBytes());
161
os.write("\n".getBytes());
162     
163     // finish off with its footer
164
writeEnd(os);
165   }
166   
167   /**
168    * This is used by the PDF and PDFPage classes to compare font names
169    *
170    * @param type The pdf type, ie /Type1
171    * @param font The font name, ie Helvetica
172    * @param style The java.awt.Font style, ie: Font.PLAIN
173    * @return true if this object is identical to this font's spec
174    */

175   protected boolean equals(String JavaDoc type,String JavaDoc font,int style) {
176     return this.type.equals(type)
177       && (this.font.equals(font)
178           && this.javaFont.equalsIgnoreCase(font));
179     // new styles not being picked up - ezb june 6 2001
180
// || this.javaFont.equalsIgnoreCase(font));
181

182     // Removed in fix for Bug#106693
183
//why? - ezb - can't find bug in bug tracker
184
//&& this.style==style;
185
}
186   
187   /**
188    * This maps the standard JDK1.1 font names and styles to
189    * the base 14 PDF fonts
190    */

191   private static String JavaDoc[][] base14 = {
192     // java name
193
// NORMAL
194
// BOLD
195
// ITALIC
196
// BOLD+ITALIC
197
{"sansserif",
198      "/Helvetica",
199      "/Helvetica-Bold",
200      "/Helvetica-Oblique",
201      "/Helvetica-BoldOblique"},
202     {"monospaced",
203      "/Courier",
204      "/Courier-Bold",
205      "/Courier-Oblique",
206      "/Courier-BoldOblique"},
207     {"timesroman",
208      "/Times-Roman",
209      "/Times-Bold",
210      "/Times-Italic",
211      "/Times-BoldItalic"},
212     {"courier",
213      "/Courier",
214      "/Courier-Bold",
215      "/Courier-Oblique",
216      "/Courier-BoldOblique"},
217     {"helvetica",
218      "/Helvetica",
219      "/Helvetica-Bold",
220      "/Helvetica-Oblique",
221      "/Helvetica-BoldOblique"},
222     {"dialog",
223      "/Courier",
224      "/Courier-Bold",
225      "/Courier-Oblique",
226      "/Courier-BoldOblique"},
227     {"dialoginput",
228      "/Courier",
229      "/Courier-Bold",
230      "/Courier-Oblique",
231      "/Courier-BoldOblique"},
232   };
233   
234 }
235
236
Popular Tags