001: /*
002: * $Id: PDFFont.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
003: *
004: * $Date: 2001/10/29 19:51:08 $
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package gnu.jpdf;
021:
022: import java.awt.Font;
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * This class defines a font within a PDF document.
028: *
029: * @author Peter T Mount,http://www.retep.org.uk/pdf/
030: * @author Eric Z. Beard, ericzbeard@hotmail.com
031: * @author $Author: ezb $
032: * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
033: */
034: public class PDFFont extends PDFObject implements Serializable {
035:
036: /*
037: * NOTE: The original class is the work of Peter T. Mount, who released it
038: * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
039: * follows:
040: * The package name was changed to gnu.pdf.
041: * The formatting was changed a little bit
042: * It is still licensed under the LGPL.
043: */
044:
045: /**
046: * The PDF document name of the font
047: */
048: private String name;
049:
050: /**
051: * The PDF type of the font, usually /Type1
052: */
053: private String type;
054:
055: /**
056: * The font's real name
057: */
058: private String font;
059:
060: /**
061: * The name of the equivalent Java font
062: */
063: private String javaFont;
064:
065: /**
066: * The PDF Style, ie: BOLD, ITALIC, etc
067: */
068: private int style;
069:
070: /**
071: * This constructs a default PDFFont. In this case Helvetica
072: */
073: protected PDFFont() {
074: this ("/F1", "/Type1", "Helvetica", Font.PLAIN);
075: }
076:
077: /**
078: * Constructs a PDFFont. This will attempt to map the font from a known
079: * Java font name to that in PDF, defaulting to Helvetica if not possible.
080: *
081: * @param name The document name, ie /F1
082: * @param type The pdf type, ie /Type1
083: * @param font The font name, ie Helvetica
084: * @param style The java.awt.Font style, ie: Font.PLAIN
085: */
086: public PDFFont(String name, String type, String font, int style) {
087: super ("/Font");
088: this .name = name;
089: this .type = type;
090: this .style = style;
091:
092: String f = font.toLowerCase();
093:
094: // default PDF Font name
095: this .font = base14[0][1];
096: this .javaFont = base14[0][0];
097:
098: // attempt to translate the font name from Java to PDF
099: for (int i = 0; i < base14.length; i++) {
100: if (base14[i][0].equals(f)) {
101: this .javaFont = base14[i][0];
102: this .font = base14[i][1 + style];
103: //System.out.println("Setting a font style to: " + this.font);
104: break;
105: }
106: }
107: }
108:
109: /**
110: * This is the most common method to use.
111: * @return the Font name within the PDF document.
112: */
113: public String getName() {
114: return name;
115: }
116:
117: /**
118: * @return the Font's PDF type
119: */
120: public String getType() {
121: return type;
122: }
123:
124: /**
125: * @return The PDF Font name
126: */
127: public String getFont() {
128: return font;
129: }
130:
131: /**
132: * @return the font style.
133: * @see java.awt.Font
134: */
135: public int getStyle() {
136: return style;
137: }
138:
139: /**
140: * @param os OutputStream to send the object to
141: * @exception IOException on error
142: */
143: public void write(OutputStream os) throws IOException {
144: // Write the object header
145: writeStart(os);
146:
147: // now the objects body
148: os.write("/Subtype ".getBytes());
149: os.write(type.getBytes());
150: os.write("\n/Name ".getBytes());
151: os.write(name.getBytes());
152: os.write("\n/BaseFont ".getBytes());
153: os.write(font.getBytes());
154: // The performance problem in Bug#106693 comments out the
155: // encoding line, and removes the /WinAnsiEncoding. I'm going
156: // to leave them in, as the Encoding fixes another problem.
157: os.write("\n/Encoding ".getBytes());
158: os.write("/WinAnsiEncoding".getBytes());
159: //os.write(encoding.getBytes());
160: os.write("\n".getBytes());
161:
162: // finish off with its footer
163: writeEnd(os);
164: }
165:
166: /**
167: * This is used by the PDF and PDFPage classes to compare font names
168: *
169: * @param type The pdf type, ie /Type1
170: * @param font The font name, ie Helvetica
171: * @param style The java.awt.Font style, ie: Font.PLAIN
172: * @return true if this object is identical to this font's spec
173: */
174: protected boolean equals(String type, String font, int style) {
175: return this .type.equals(type)
176: && (this .font.equals(font) && this .javaFont
177: .equalsIgnoreCase(font));
178: // new styles not being picked up - ezb june 6 2001
179: // || this.javaFont.equalsIgnoreCase(font));
180:
181: // Removed in fix for Bug#106693
182: //why? - ezb - can't find bug in bug tracker
183: //&& this.style==style;
184: }
185:
186: /**
187: * This maps the standard JDK1.1 font names and styles to
188: * the base 14 PDF fonts
189: */
190: private static String[][] base14 = {
191: // java name
192: // NORMAL
193: // BOLD
194: // ITALIC
195: // BOLD+ITALIC
196: { "sansserif", "/Helvetica", "/Helvetica-Bold",
197: "/Helvetica-Oblique", "/Helvetica-BoldOblique" },
198: { "monospaced", "/Courier", "/Courier-Bold",
199: "/Courier-Oblique", "/Courier-BoldOblique" },
200: { "timesroman", "/Times-Roman", "/Times-Bold",
201: "/Times-Italic", "/Times-BoldItalic" },
202: { "courier", "/Courier", "/Courier-Bold",
203: "/Courier-Oblique", "/Courier-BoldOblique" },
204: { "helvetica", "/Helvetica", "/Helvetica-Bold",
205: "/Helvetica-Oblique", "/Helvetica-BoldOblique" },
206: { "dialog", "/Courier", "/Courier-Bold",
207: "/Courier-Oblique", "/Courier-BoldOblique" },
208: { "dialoginput", "/Courier", "/Courier-Bold",
209: "/Courier-Oblique", "/Courier-BoldOblique" }, };
210:
211: }
|