001: /**
002: * Copyright (c) 2004-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.font;
031:
032: import java.awt.Font;
033: import java.awt.Graphics;
034: import java.awt.Graphics2D;
035: import java.awt.RenderingHints;
036: import java.awt.geom.AffineTransform;
037: import java.io.IOException;
038: import java.util.HashMap;
039: import java.util.Map;
040:
041: import org.pdfbox.cos.COSDictionary;
042: import org.pdfbox.cos.COSName;
043:
044: /**
045: * This is implementation of the Type1 Font.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.11 $
049: */
050: public class PDType1Font extends PDSimpleFont {
051: /**
052: * Standard Base 14 Font.
053: */
054: public static final PDType1Font TIMES_ROMAN = new PDType1Font(
055: "Times-Roman");
056: /**
057: * Standard Base 14 Font.
058: */
059: public static final PDType1Font TIMES_BOLD = new PDType1Font(
060: "Times-Bold");
061: /**
062: * Standard Base 14 Font.
063: */
064: public static final PDType1Font TIMES_ITALIC = new PDType1Font(
065: "Times-Italic");
066: /**
067: * Standard Base 14 Font.
068: */
069: public static final PDType1Font TIMES_BOLD_ITALIC = new PDType1Font(
070: "Times-BoldItalic");
071: /**
072: * Standard Base 14 Font.
073: */
074: public static final PDType1Font HELVETICA = new PDType1Font(
075: "Helvetica");
076: /**
077: * Standard Base 14 Font.
078: */
079: public static final PDType1Font HELVETICA_BOLD = new PDType1Font(
080: "Helvetica-Bold");
081: /**
082: * Standard Base 14 Font.
083: */
084: public static final PDType1Font HELVETICA_OBLIQUE = new PDType1Font(
085: "Helvetica-Oblique");
086: /**
087: * Standard Base 14 Font.
088: */
089: public static final PDType1Font HELVETICA_BOLD_OBLIQUE = new PDType1Font(
090: "Helvetica-BoldOblique");
091: /**
092: * Standard Base 14 Font.
093: */
094: public static final PDType1Font COURIER = new PDType1Font("Courier");
095: /**
096: * Standard Base 14 Font.
097: */
098: public static final PDType1Font COURIER_BOLD = new PDType1Font(
099: "Courier-Bold");
100: /**
101: * Standard Base 14 Font.
102: */
103: public static final PDType1Font COURIER_OBLIQUE = new PDType1Font(
104: "Courier-Oblique");
105: /**
106: * Standard Base 14 Font.
107: */
108: public static final PDType1Font COURIER_BOLD_OBLIQUE = new PDType1Font(
109: "Courier-BoldOblique");
110: /**
111: * Standard Base 14 Font.
112: */
113: public static final PDType1Font SYMBOL = new PDType1Font("Symbol");
114: /**
115: * Standard Base 14 Font.
116: */
117: public static final PDType1Font ZAPF_DINGBATS = new PDType1Font(
118: "ZapfDingbats");
119:
120: private static final Map STANDARD_14 = new HashMap();
121: static {
122: STANDARD_14.put(TIMES_ROMAN.getBaseFont(), TIMES_ROMAN);
123: STANDARD_14.put(TIMES_BOLD.getBaseFont(), TIMES_BOLD);
124: STANDARD_14.put(TIMES_ITALIC.getBaseFont(), TIMES_ITALIC);
125: STANDARD_14.put(TIMES_BOLD_ITALIC.getBaseFont(),
126: TIMES_BOLD_ITALIC);
127: STANDARD_14.put(HELVETICA.getBaseFont(), HELVETICA);
128: STANDARD_14.put(HELVETICA_BOLD.getBaseFont(), HELVETICA_BOLD);
129: STANDARD_14.put(HELVETICA_OBLIQUE.getBaseFont(),
130: HELVETICA_OBLIQUE);
131: STANDARD_14.put(HELVETICA_BOLD_OBLIQUE.getBaseFont(),
132: HELVETICA_BOLD_OBLIQUE);
133: STANDARD_14.put(COURIER.getBaseFont(), COURIER);
134: STANDARD_14.put(COURIER_BOLD.getBaseFont(), COURIER_BOLD);
135: STANDARD_14.put(COURIER_OBLIQUE.getBaseFont(), COURIER_OBLIQUE);
136: STANDARD_14.put(COURIER_BOLD_OBLIQUE.getBaseFont(),
137: COURIER_BOLD_OBLIQUE);
138: STANDARD_14.put(SYMBOL.getBaseFont(), SYMBOL);
139: STANDARD_14.put(ZAPF_DINGBATS.getBaseFont(), ZAPF_DINGBATS);
140: }
141:
142: private Font awtFont = null;
143:
144: /**
145: * Constructor.
146: */
147: public PDType1Font() {
148: super ();
149: font.setItem(COSName.SUBTYPE, COSName.getPDFName("Type1"));
150: }
151:
152: /**
153: * Constructor.
154: *
155: * @param fontDictionary The font dictionary according to the PDF specification.
156: */
157: public PDType1Font(COSDictionary fontDictionary) {
158: super (fontDictionary);
159: }
160:
161: /**
162: * Constructor.
163: *
164: * @param baseFont The base font for this font.
165: */
166: public PDType1Font(String baseFont) {
167: this ();
168: setBaseFont(baseFont);
169: }
170:
171: /**
172: * A convenience method to get one of the standard 14 font from name.
173: *
174: * @param name The name of the font to get.
175: *
176: * @return The font that matches the name or null if it does not exist.
177: */
178: public static PDType1Font getStandardFont(String name) {
179: return (PDType1Font) STANDARD_14.get(name);
180: }
181:
182: /**
183: * This will get the names of the standard 14 fonts.
184: *
185: * @return An array of the names of the standard 14 fonts.
186: */
187: public static String[] getStandard14Names() {
188: return (String[]) STANDARD_14.keySet().toArray(new String[14]);
189: }
190:
191: /**
192: * {@inheritDoc}
193: */
194: public void drawString(String string, Graphics g, float fontSize,
195: float xScale, float yScale, float x, float y)
196: throws IOException {
197: if (awtFont == null) {
198: String baseFont = this .getBaseFont();
199: if (baseFont.equals(TIMES_ROMAN.getBaseFont())) {
200: awtFont = new Font("Times New Roman", Font.PLAIN, 1);
201: } else if (baseFont.equals(TIMES_ITALIC.getBaseFont())) {
202: awtFont = new Font("Times New Roman", Font.ITALIC, 1);
203: } else if (baseFont.equals(TIMES_BOLD.getBaseFont())) {
204: awtFont = new Font("Times New Roman", Font.BOLD, 1);
205: } else if (baseFont.equals(TIMES_BOLD_ITALIC.getBaseFont())) {
206: awtFont = new Font("Times New Roman", Font.BOLD
207: | Font.ITALIC, 1);
208: } else if (baseFont.equals(HELVETICA.getBaseFont())) {
209: awtFont = new Font("Helvetica", Font.PLAIN, 1);
210: } else if (baseFont.equals(HELVETICA_BOLD.getBaseFont())) {
211: awtFont = new Font("Helvetica", Font.BOLD, 1);
212: } else if (baseFont.equals(HELVETICA_BOLD_OBLIQUE
213: .getBaseFont())) {
214: awtFont = new Font("Helvetica",
215: Font.BOLD | Font.ITALIC, 1);
216: } else if (baseFont.equals(HELVETICA_OBLIQUE.getBaseFont())) {
217: awtFont = new Font("Helvetica", Font.ITALIC, 1);
218: } else if (baseFont.equals(COURIER.getBaseFont())) {
219: awtFont = new Font("Courier", Font.PLAIN, 1);
220: } else if (baseFont.equals(COURIER_BOLD.getBaseFont())) {
221: awtFont = new Font("Courier", Font.BOLD, 1);
222: } else if (baseFont.equals(COURIER_BOLD_OBLIQUE
223: .getBaseFont())) {
224: awtFont = new Font("Courier", Font.BOLD | Font.ITALIC,
225: 1);
226: } else if (baseFont.equals(COURIER_OBLIQUE.getBaseFont())) {
227: awtFont = new Font("Courier", Font.ITALIC, 1);
228: } else if (baseFont.equals(SYMBOL.getBaseFont())) {
229: awtFont = new Font("Symbol", Font.PLAIN, 1);
230: } else if (baseFont.equals(ZAPF_DINGBATS.getBaseFont())) {
231: awtFont = new Font("ZapfDingbats", Font.PLAIN, 1);
232: } else {
233: awtFont = new Font("Arial", Font.PLAIN, 1);
234: //throw new IOException( "Not yet implemented:" + getClass().getName() + " " +
235: //this.getBaseFont() +
236: //" " + this + " " + TIMES_ROMAN );
237: }
238: }
239: AffineTransform at = new AffineTransform();
240: at.scale(xScale, yScale);
241:
242: Graphics2D g2d = (Graphics2D) g;
243: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
244: RenderingHints.VALUE_ANTIALIAS_ON);
245: g2d.setFont(awtFont.deriveFont(at).deriveFont(fontSize));
246: //g2d.getFontRenderContext().getTransform().scale( xScale, yScale );
247:
248: g2d.drawString(string, (int) x, (int) y);
249: }
250: }
|