KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-2004, 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 org.pdfbox.cos.COSDictionary;
34 import org.pdfbox.cos.COSName;
35 import org.pdfbox.cos.COSStream;
36 import org.pdfbox.pdmodel.common.PDMatrix;
37
38 import java.awt.Graphics JavaDoc;
39 import java.awt.Image JavaDoc;
40
41 import java.io.IOException JavaDoc;
42
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * This is implementation of the Type3 Font.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.8 $
51  */

52 public class PDType3Font extends PDSimpleFont
53 {
54     //A map of character code to java.awt.Image for the glyph
55
private Map JavaDoc images = new HashMap JavaDoc();
56
57     /**
58      * Constructor.
59      */

60     public PDType3Font()
61     {
62         super();
63         font.setItem( COSName.SUBTYPE, COSName.getPDFName( "Type3" ) );
64     }
65
66     /**
67      * Constructor.
68      *
69      * @param fontDictionary The font dictionary according to the PDF specification.
70      */

71     public PDType3Font( COSDictionary fontDictionary )
72     {
73         super( fontDictionary );
74     }
75
76     /**
77      * Type3 fonts have their glyphs defined as a content stream. This
78      * will create the image that represents that character
79      *
80      * @throws IOException If there is an error creating the image.
81      */

82     private Image JavaDoc createImageIfNecessary( char character ) throws IOException JavaDoc
83     {
84         Character JavaDoc c = new Character JavaDoc( character );
85         Image JavaDoc retval = (Image JavaDoc)images.get( c );
86         if( retval == null )
87         {
88             COSDictionary charProcs = (COSDictionary)font.getDictionaryObject( COSName.getPDFName( "CharProcs" ) );
89             COSStream stream = (COSStream)charProcs.getDictionaryObject( COSName.getPDFName( "" + character ) );
90             if( stream != null )
91             {
92                 Type3StreamParser parser = new Type3StreamParser();
93                 retval = parser.createImage( stream );
94                 images.put( c, retval );
95             }
96             else
97             {
98                 //stream should not be null!!
99
}
100         }
101         return retval;
102
103     }
104
105     /**
106      * This will draw a string on a canvas using the font.
107      *
108      * @param string The string to draw.
109      * @param g The graphics to draw onto.
110      * @param fontSize The size of the font to draw.
111      * @param x The x coordinate to draw at.
112      * @param y The y coordinate to draw at.
113      *
114      * @throws IOException If there is an error drawing the image on the screen.
115      */

116     public void drawString( String JavaDoc string, Graphics JavaDoc g, float fontSize, float x, float y ) throws IOException JavaDoc
117     {
118         //if( string.equals( "V" )|| string.equals( "o" ) )
119
{
120             for(int i=0; i<string.length(); i++)
121             {
122                 //todo need to use image observers and such
123
char c = string.charAt( i );
124                 Image JavaDoc image = createImageIfNecessary( c );
125                 if( image != null )
126                 {
127                     int newWidth = (int)(.12*image.getWidth(null));
128                     int newHeight = (int)(.12*image.getHeight(null));
129                     if( newWidth > 0 && newHeight > 0 )
130                     {
131                         image = image.getScaledInstance( newWidth, newHeight, Image.SCALE_SMOOTH );
132                         g.drawImage( image, (int)x, (int)y, null );
133                         x+=newWidth;
134                     }
135                 }
136             }
137         }
138     }
139     
140     /**
141      * Set the font matrix for this type3 font.
142      *
143      * @param matrix The font matrix for this type3 font.
144      */

145     public void setFontMatrix( PDMatrix matrix )
146     {
147         font.setItem( "FontMatrix", matrix );
148     }
149 }
Popular Tags