1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.encoding; 18 19 import java.io.IOException; 20 21 import org.apache.pdfbox.cos.COSArray; 22 import org.apache.pdfbox.cos.COSBase; 23 import org.apache.pdfbox.cos.COSDictionary; 24 import org.apache.pdfbox.cos.COSName; 25 import org.apache.pdfbox.cos.COSNumber; 26 27 /** 28 * This will perform the encoding from a dictionary. 29 * 30 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 31 * @version $Revision: 1.13 $ 32 */ 33 public class DictionaryEncoding extends Encoding 34 { 35 private COSDictionary encoding = null; 36 37 /** 38 * Constructor. 39 * 40 * @param fontEncoding The encoding dictionary. 41 * 42 * @throws IOException If there is a problem getting the base font. 43 */ 44 public DictionaryEncoding( COSDictionary fontEncoding ) throws IOException 45 { 46 encoding = fontEncoding; 47 48 //first set up the base encoding 49 //The previious value WinAnsiEncoding() has been changed to StandardEnding 50 //see p 389 of the PDF 1.5 ref?rence table 5.11 entries in a dictionary encoding 51 //"If this entry is absent, the Differences entry describes differences from an implicit 52 //base encoding. For a font program that is embedded in the PDF file, the 53 //implicit base encoding is the font program?s built-in encoding, as described 54 //above and further elaborated in the sections on specific font types below. Otherwise, 55 //for a nonsymbolic font, it is StandardEncoding, and for a symbolic font, it 56 //is the font?s built-in encoding." 57 58 //so the default base encoding is standardEncoding 59 Encoding baseEncoding = new StandardEncoding(); 60 COSName baseEncodingName = (COSName)encoding.getDictionaryObject( COSName.BASE_ENCODING ); 61 62 if( baseEncodingName != null ) 63 { 64 EncodingManager manager = new EncodingManager(); 65 baseEncoding = manager.getEncoding( baseEncodingName ); 66 } 67 nameToCode.putAll( baseEncoding.nameToCode ); 68 codeToName.putAll( baseEncoding.codeToName ); 69 70 71 //now replace with the differences. 72 COSArray differences = (COSArray)encoding.getDictionaryObject( COSName.DIFFERENCES ); 73 int currentIndex = -1; 74 for( int i=0; differences != null && i<differences.size(); i++ ) 75 { 76 COSBase next = differences.getObject( i ); 77 if( next instanceof COSNumber ) 78 { 79 currentIndex = ((COSNumber)next).intValue(); 80 } 81 else if( next instanceof COSName ) 82 { 83 COSName name = (COSName)next; 84 addCharacterEncoding( currentIndex++, name ); 85 } 86 } 87 } 88 89 /** 90 * Convert this standard java object to a COS object. 91 * 92 * @return The cos object that matches this Java object. 93 */ 94 public COSBase getCOSObject() 95 { 96 return encoding; 97 } 98 }