001: /**
002: * Copyright (c) 2003, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.encoding;
031:
032: import java.io.IOException;
033:
034: import org.pdfbox.cos.COSArray;
035: import org.pdfbox.cos.COSBase;
036: import org.pdfbox.cos.COSDictionary;
037: import org.pdfbox.cos.COSName;
038: import org.pdfbox.cos.COSNumber;
039:
040: /**
041: * This will perform the encoding from a dictionary.
042: *
043: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
044: * @version $Revision: 1.13 $
045: */
046: public class DictionaryEncoding extends Encoding {
047: private COSDictionary encoding = null;
048:
049: /**
050: * Constructor.
051: *
052: * @param fontEncoding The encoding dictionary.
053: *
054: * @throws IOException If there is a problem getting the base font.
055: */
056: public DictionaryEncoding(COSDictionary fontEncoding)
057: throws IOException {
058: encoding = fontEncoding;
059:
060: //first set up the base encoding
061: //The previious value WinAnsiEncoding() has been changed to StandardEnding
062: //see p 389 of the PDF 1.5 reférence table 5.11 entries in a dictionary encoding
063: //"If this entry is absent, the Differences entry describes differences from an implicit
064: //base encoding. For a font program that is embedded in the PDF file, the
065: //implicit base encoding is the font program’s built-in encoding, as described
066: //above and further elaborated in the sections on specific font types below. Otherwise,
067: //for a nonsymbolic font, it is StandardEncoding, and for a symbolic font, it
068: //is the font’s built-in encoding."
069:
070: //so the default base encoding is standardEncoding
071: Encoding baseEncoding = new StandardEncoding();
072: COSName baseEncodingName = (COSName) encoding
073: .getDictionaryObject(COSName.BASE_ENCODING);
074:
075: if (baseEncodingName != null) {
076: EncodingManager manager = new EncodingManager();
077: baseEncoding = manager.getEncoding(baseEncodingName);
078: }
079: nameToCode.putAll(baseEncoding.nameToCode);
080: codeToName.putAll(baseEncoding.codeToName);
081:
082: //now replace with the differences.
083: COSArray differences = (COSArray) encoding
084: .getDictionaryObject(COSName.DIFFERENCES);
085: int currentIndex = -1;
086: for (int i = 0; differences != null && i < differences.size(); i++) {
087: COSBase next = differences.getObject(i);
088: if (next instanceof COSNumber) {
089: currentIndex = ((COSNumber) next).intValue();
090: } else if (next instanceof COSName) {
091: COSName name = (COSName) next;
092: addCharacterEncoding(currentIndex++, name);
093: }
094: }
095: }
096:
097: /**
098: * Convert this standard java object to a COS object.
099: *
100: * @return The cos object that matches this Java object.
101: */
102: public COSBase getCOSObject() {
103: return encoding;
104: }
105: }
|