KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > encoding > DictionaryEncoding


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.encoding;
32
33 import java.io.IOException JavaDoc;
34
35 import org.pdfbox.cos.COSArray;
36 import org.pdfbox.cos.COSBase;
37 import org.pdfbox.cos.COSDictionary;
38 import org.pdfbox.cos.COSName;
39 import org.pdfbox.cos.COSNumber;
40
41 /**
42  * This will perform the encoding from a dictionary.
43  *
44  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
45  * @version $Revision: 1.13 $
46  */

47 public class DictionaryEncoding extends Encoding
48 {
49     private COSDictionary encoding = null;
50
51     /**
52      * Constructor.
53      *
54      * @param fontEncoding The encoding dictionary.
55      *
56      * @throws IOException If there is a problem getting the base font.
57      */

58     public DictionaryEncoding( COSDictionary fontEncoding ) throws IOException JavaDoc
59     {
60         encoding = fontEncoding;
61
62         //first set up the base encoding
63
//The previious value WinAnsiEncoding() has been changed to StandardEnding
64
//see p 389 of the PDF 1.5 reférence table 5.11 entries in a dictionary encoding
65
//"If this entry is absent, the Differences entry describes differences from an implicit
66
//base encoding. For a font program that is embedded in the PDF file, the
67
//implicit base encoding is the font program’s built-in encoding, as described
68
//above and further elaborated in the sections on specific font types below. Otherwise,
69
//for a nonsymbolic font, it is StandardEncoding, and for a symbolic font, it
70
//is the font’s built-in encoding."
71

72         //so the default base encoding is standardEncoding
73
Encoding baseEncoding = new StandardEncoding();
74         COSName baseEncodingName = (COSName)encoding.getDictionaryObject( COSName.BASE_ENCODING );
75
76         if( baseEncodingName != null )
77         {
78             EncodingManager manager = new EncodingManager();
79             baseEncoding = manager.getEncoding( baseEncodingName );
80         }
81         nameToCode.putAll( baseEncoding.nameToCode );
82         codeToName.putAll( baseEncoding.codeToName );
83
84
85         //now replace with the differences.
86
COSArray differences = (COSArray)encoding.getDictionaryObject( COSName.DIFFERENCES );
87         int currentIndex = -1;
88         for( int i=0; differences != null && i<differences.size(); i++ )
89         {
90             COSBase next = differences.getObject( i );
91             if( next instanceof COSNumber )
92             {
93                 currentIndex = ((COSNumber)next).intValue();
94             }
95             else if( next instanceof COSName )
96             {
97                 COSName name = (COSName)next;
98                 addCharacterEncoding( currentIndex++, name );
99             }
100         }
101     }
102     
103     /**
104      * Convert this standard java object to a COS object.
105      *
106      * @return The cos object that matches this Java object.
107      */

108     public COSBase getCOSObject()
109     {
110         return encoding;
111     }
112 }
113
Popular Tags