KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > font > PDType1Font


1 /**
2  * Copyright (c) 2004-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.font;
32
33 import java.awt.Font JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Graphics2D JavaDoc;
36 import java.awt.RenderingHints JavaDoc;
37 import java.awt.geom.AffineTransform JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import org.pdfbox.cos.COSDictionary;
43 import org.pdfbox.cos.COSName;
44
45 /**
46  * This is implementation of the Type1 Font.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.11 $
50  */

51 public class PDType1Font extends PDSimpleFont
52 {
53     /**
54      * Standard Base 14 Font.
55      */

56     public static final PDType1Font TIMES_ROMAN = new PDType1Font( "Times-Roman" );
57     /**
58      * Standard Base 14 Font.
59      */

60     public static final PDType1Font TIMES_BOLD = new PDType1Font( "Times-Bold" );
61     /**
62      * Standard Base 14 Font.
63      */

64     public static final PDType1Font TIMES_ITALIC = new PDType1Font( "Times-Italic" );
65     /**
66      * Standard Base 14 Font.
67      */

68     public static final PDType1Font TIMES_BOLD_ITALIC = new PDType1Font( "Times-BoldItalic" );
69     /**
70      * Standard Base 14 Font.
71      */

72     public static final PDType1Font HELVETICA = new PDType1Font( "Helvetica" );
73     /**
74      * Standard Base 14 Font.
75      */

76     public static final PDType1Font HELVETICA_BOLD = new PDType1Font( "Helvetica-Bold" );
77     /**
78      * Standard Base 14 Font.
79      */

80     public static final PDType1Font HELVETICA_OBLIQUE = new PDType1Font( "Helvetica-Oblique" );
81     /**
82      * Standard Base 14 Font.
83      */

84     public static final PDType1Font HELVETICA_BOLD_OBLIQUE = new PDType1Font( "Helvetica-BoldOblique" );
85     /**
86      * Standard Base 14 Font.
87      */

88     public static final PDType1Font COURIER = new PDType1Font( "Courier" );
89     /**
90      * Standard Base 14 Font.
91      */

92     public static final PDType1Font COURIER_BOLD = new PDType1Font( "Courier-Bold" );
93     /**
94      * Standard Base 14 Font.
95      */

96     public static final PDType1Font COURIER_OBLIQUE = new PDType1Font( "Courier-Oblique" );
97     /**
98      * Standard Base 14 Font.
99      */

100     public static final PDType1Font COURIER_BOLD_OBLIQUE = new PDType1Font( "Courier-BoldOblique" );
101     /**
102      * Standard Base 14 Font.
103      */

104     public static final PDType1Font SYMBOL = new PDType1Font( "Symbol" );
105     /**
106      * Standard Base 14 Font.
107      */

108     public static final PDType1Font ZAPF_DINGBATS = new PDType1Font( "ZapfDingbats" );
109     
110     
111     private static final Map JavaDoc STANDARD_14 = new HashMap JavaDoc();
112     static
113     {
114         STANDARD_14.put( TIMES_ROMAN.getBaseFont(), TIMES_ROMAN );
115         STANDARD_14.put( TIMES_BOLD.getBaseFont(), TIMES_BOLD );
116         STANDARD_14.put( TIMES_ITALIC.getBaseFont(), TIMES_ITALIC );
117         STANDARD_14.put( TIMES_BOLD_ITALIC.getBaseFont(), TIMES_BOLD_ITALIC );
118         STANDARD_14.put( HELVETICA.getBaseFont(), HELVETICA );
119         STANDARD_14.put( HELVETICA_BOLD.getBaseFont(), HELVETICA_BOLD );
120         STANDARD_14.put( HELVETICA_OBLIQUE.getBaseFont(), HELVETICA_OBLIQUE );
121         STANDARD_14.put( HELVETICA_BOLD_OBLIQUE.getBaseFont(), HELVETICA_BOLD_OBLIQUE );
122         STANDARD_14.put( COURIER.getBaseFont(), COURIER );
123         STANDARD_14.put( COURIER_BOLD.getBaseFont(), COURIER_BOLD );
124         STANDARD_14.put( COURIER_OBLIQUE.getBaseFont(), COURIER_OBLIQUE );
125         STANDARD_14.put( COURIER_BOLD_OBLIQUE.getBaseFont(), COURIER_BOLD_OBLIQUE );
126         STANDARD_14.put( SYMBOL.getBaseFont(), SYMBOL );
127         STANDARD_14.put( ZAPF_DINGBATS.getBaseFont(), ZAPF_DINGBATS );
128     }
129     
130     private Font JavaDoc awtFont = null;
131     
132     /**
133      * Constructor.
134      */

135     public PDType1Font()
136     {
137         super();
138         font.setItem( COSName.SUBTYPE, COSName.getPDFName( "Type1" ) );
139     }
140
141     /**
142      * Constructor.
143      *
144      * @param fontDictionary The font dictionary according to the PDF specification.
145      */

146     public PDType1Font( COSDictionary fontDictionary )
147     {
148         super( fontDictionary );
149     }
150     
151     /**
152      * Constructor.
153      *
154      * @param baseFont The base font for this font.
155      */

156     public PDType1Font( String JavaDoc baseFont )
157     {
158         this();
159         setBaseFont( baseFont );
160     }
161     
162     /**
163      * A convenience method to get one of the standard 14 font from name.
164      *
165      * @param name The name of the font to get.
166      *
167      * @return The font that matches the name or null if it does not exist.
168      */

169     public static PDType1Font getStandardFont( String JavaDoc name )
170     {
171         return (PDType1Font)STANDARD_14.get( name );
172     }
173     
174     /**
175      * This will get the names of the standard 14 fonts.
176      *
177      * @return An array of the names of the standard 14 fonts.
178      */

179     public static String JavaDoc[] getStandard14Names()
180     {
181         return (String JavaDoc[])STANDARD_14.keySet().toArray( new String JavaDoc[14] );
182     }
183     
184     /**
185      * {@inheritDoc}
186      */

187     public void drawString( String JavaDoc string, Graphics JavaDoc g, float fontSize,
188         float xScale, float yScale, float x, float y ) throws IOException JavaDoc
189     {
190         if( awtFont == null )
191         {
192             String JavaDoc baseFont = this.getBaseFont();
193             if( baseFont.equals( TIMES_ROMAN.getBaseFont() ) )
194             {
195                 awtFont = new Font JavaDoc( "Times New Roman", Font.PLAIN, 1 );
196             }
197             else if( baseFont.equals( TIMES_ITALIC.getBaseFont() ) )
198             {
199                 awtFont = new Font JavaDoc( "Times New Roman", Font.ITALIC, 1 );
200             }
201             else if( baseFont.equals( TIMES_BOLD.getBaseFont() ) )
202             {
203                 awtFont = new Font JavaDoc( "Times New Roman", Font.BOLD, 1 );
204             }
205             else if( baseFont.equals( TIMES_BOLD_ITALIC.getBaseFont() ) )
206             {
207                 awtFont = new Font JavaDoc( "Times New Roman", Font.BOLD | Font.ITALIC, 1 );
208             }
209             else if( baseFont.equals( HELVETICA.getBaseFont() ) )
210             {
211                 awtFont = new Font JavaDoc( "Helvetica", Font.PLAIN, 1 );
212             }
213             else if( baseFont.equals( HELVETICA_BOLD.getBaseFont() ) )
214             {
215                 awtFont = new Font JavaDoc( "Helvetica", Font.BOLD, 1 );
216             }
217             else if( baseFont.equals( HELVETICA_BOLD_OBLIQUE.getBaseFont() ) )
218             {
219                 awtFont = new Font JavaDoc( "Helvetica", Font.BOLD | Font.ITALIC, 1 );
220             }
221             else if( baseFont.equals( HELVETICA_OBLIQUE.getBaseFont() ) )
222             {
223                 awtFont = new Font JavaDoc( "Helvetica", Font.ITALIC, 1 );
224             }
225             else if( baseFont.equals( COURIER.getBaseFont() ) )
226             {
227                 awtFont = new Font JavaDoc( "Courier", Font.PLAIN, 1 );
228             }
229             else if( baseFont.equals( COURIER_BOLD.getBaseFont() ) )
230             {
231                 awtFont = new Font JavaDoc( "Courier", Font.BOLD, 1 );
232             }
233             else if( baseFont.equals( COURIER_BOLD_OBLIQUE.getBaseFont() ) )
234             {
235                 awtFont = new Font JavaDoc( "Courier", Font.BOLD | Font.ITALIC, 1 );
236             }
237             else if( baseFont.equals( COURIER_OBLIQUE.getBaseFont() ) )
238             {
239                 awtFont = new Font JavaDoc( "Courier", Font.ITALIC, 1 );
240             }
241             else if( baseFont.equals( SYMBOL.getBaseFont() ) )
242             {
243                 awtFont = new Font JavaDoc( "Symbol", Font.PLAIN, 1 );
244             }
245             else if( baseFont.equals( ZAPF_DINGBATS.getBaseFont() ) )
246             {
247                 awtFont = new Font JavaDoc( "ZapfDingbats", Font.PLAIN, 1 );
248             }
249             else
250             {
251                 awtFont = new Font JavaDoc( "Arial", Font.PLAIN, 1 );
252                 //throw new IOException( "Not yet implemented:" + getClass().getName() + " " +
253
//this.getBaseFont() +
254
//" " + this + " " + TIMES_ROMAN );
255
}
256         }
257         AffineTransform JavaDoc at = new AffineTransform JavaDoc();
258         at.scale( xScale, yScale );
259         
260         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
261         g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
262         g2d.setFont( awtFont.deriveFont( at ).deriveFont( fontSize ) );
263         //g2d.getFontRenderContext().getTransform().scale( xScale, yScale );
264

265         g2d.drawString( string, (int)x, (int)y );
266     }
267 }
Popular Tags