KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003, 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.io.IOException JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.pdfbox.cos.COSDictionary;
37 import org.pdfbox.cos.COSName;
38
39 /**
40  * This will create the correct type of font based on information in the dictionary.
41  *
42  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
43  * @version $Revision: 1.6 $
44  */

45 public class PDFontFactory
46 {
47     /**
48      * private constructor, should only use static methods in this class.
49      */

50     private PDFontFactory()
51     {
52     }
53     
54     /**
55      * Create a font from the dictionary. Use the fontCache to get the existing
56      * object instead of creating it.
57      *
58      * @param dic The font dictionary.
59      * @param fontCache The font cache.
60      * @return The PDModel object for the cos dictionary.
61      * @throws IOException If there is an error creating the font.
62      */

63     public static PDFont createFont( COSDictionary dic, Map JavaDoc fontCache ) throws IOException JavaDoc
64     {
65         PDFont font = null;
66         if( fontCache != null )
67         {
68             font = (PDFont)fontCache.get( dic );
69         }
70         if( font == null )
71         {
72             font = createFont( dic );
73             if( fontCache != null )
74             {
75                 fontCache.put( dic, font );
76             }
77         }
78         return font;
79     }
80
81     /**
82      * This will create the correct font based on information in the dictionary.
83      *
84      * @param dic The populated dictionary.
85      *
86      * @return The corrent implementation for the font.
87      *
88      * @throws IOException If the dictionary is not valid.
89      */

90     public static PDFont createFont( COSDictionary dic ) throws IOException JavaDoc
91     {
92         PDFont retval = null;
93
94         COSName type = (COSName)dic.getDictionaryObject( COSName.TYPE );
95         if( !type.equals( COSName.FONT ) )
96         {
97             throw new IOException JavaDoc( "Cannot create font if /Type is not /Font. Actual=" +type );
98         }
99
100         COSName subType = (COSName)dic.getDictionaryObject( COSName.SUBTYPE );
101         if( subType.equals( COSName.getPDFName( "Type1" ) ) )
102         {
103             retval = new PDType1Font( dic );
104         }
105         else if( subType.equals( COSName.getPDFName( "MMType1" ) ) )
106         {
107             retval = new PDMMType1Font( dic );
108         }
109         else if( subType.equals( COSName.getPDFName( "TrueType" ) ) )
110         {
111             retval = new PDTrueTypeFont( dic );
112         }
113         else if( subType.equals( COSName.getPDFName( "Type3" ) ) )
114         {
115             retval = new PDType3Font( dic );
116         }
117         else if( subType.equals( COSName.getPDFName( "Type0" ) ) )
118         {
119             retval = new PDType0Font( dic );
120         }
121         else if( subType.equals( COSName.getPDFName( "CIDFontType0" ) ) )
122         {
123             retval = new PDCIDFontType0Font( dic );
124         }
125         else if( subType.equals( COSName.getPDFName( "CIDFontType2" ) ) )
126         {
127             retval = new PDCIDFontType2Font( dic );
128         }
129         else
130         {
131             throw new IOException JavaDoc( "Unknown font subtype=" + subType );
132         }
133
134         return retval;
135     }
136 }
Popular Tags