PDFSimpleFontEncoding.java 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosName
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 */
package com.adobe.internal.pdftoolkit.pdf.graphics.font;

import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFEncodingDifferences;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.CharSetEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.Encoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.StandardCharSetEncodings;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.ZapfDingbatsEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.AdobeGlyphList;
import java.util.Map;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class PDFSimpleFontEncoding
extends PDFCosObject
implements Encoding {
    private PDFSimpleFontEncoding(CosObject cosObject) throws PDFInvalidDocumentException {
        super(cosObject);
    }

    static PDFSimpleFontEncoding getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return null;
        }
        PDFSimpleFontEncoding pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFSimpleFontEncoding.class);
        if (pdfObject == null) {
            pdfObject = new PDFSimpleFontEncoding(cosObject);
        }
        return pdfObject;
    }

    public static PDFSimpleFontEncoding newInstance(PDFDocument pdfDocument, CharSetEncoding encoding) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDocument);
        PDFSimpleFontEncoding pdfObject = new PDFSimpleFontEncoding((CosObject)cosObject);
        cosObject.put(ASName.k_Type, ASName.k_Encoding);
        if (encoding != null) {
            cosObject.put(ASName.k_BaseEncoding, encoding.getName());
        }
        return pdfObject;
    }

    public static PDFSimpleFontEncoding newInstance(PDFDocument pdfDocument, ASName encoding) throws PDFInvalidDocumentException {
        CosName cosObject = PDFCosObject.newCosName(pdfDocument, encoding);
        return new PDFSimpleFontEncoding((CosObject)cosObject);
    }

    public static PDFSimpleFontEncoding newInstance(PDFDocument pdfDocument, CharSetEncoding encoding, Map<Integer, String> differences) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFSimpleFontEncoding simpleFontEncoding = PDFSimpleFontEncoding.newInstance(pdfDocument, encoding);
        if (differences != null && !differences.isEmpty()) {
            PDFEncodingDifferences pdfEncodingDifferences = PDFEncodingDifferences.newInstance(pdfDocument, differences);
            simpleFontEncoding.setDifferences(pdfEncodingDifferences);
        }
        return simpleFontEncoding;
    }

    public CharSetEncoding getBaseEncoding() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.getCosObject() instanceof CosName) {
            try {
                return StandardCharSetEncodings.get(((CosName)this.getCosObject()).nameValue());
            }
            catch (PDFInvalidParameterException e) {
                throw new PDFInvalidDocumentException("Could not get a pre-defined character set encoding", (Throwable)e);
            }
        }
        if (this.getCosObject() instanceof CosDictionary) {
            try {
                if (((CosDictionary)this.getCosObject()).containsKey((Object)ASName.k_BaseEncoding)) {
                    ASName baseName = ((CosDictionary)this.getCosObject()).getName(ASName.k_BaseEncoding);
                    return StandardCharSetEncodings.get(baseName);
                }
            }
            catch (PDFInvalidParameterException e) {
                throw new PDFInvalidDocumentException("Could not get a pre-defined character set encoding", (Throwable)e);
            }
        }
        return null;
    }

    CharSetEncoding getBaseEncoding(boolean isSymbolic, boolean isEmbedded) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding retVal = this.getBaseEncoding();
        if (retVal != null) {
            return retVal;
        }
        if (!isSymbolic && !isEmbedded) {
            try {
                return StandardCharSetEncodings.get(ASName.k_StandardEncoding);
            }
            catch (PDFInvalidParameterException e) {
                throw new PDFInvalidDocumentException("Could not get a pre-defined character set encoding", (Throwable)e);
            }
        }
        return null;
    }

    public CharSetEncoding getBaseEncoding(ASName fontName, boolean isEmbedded) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding retVal = this.getBaseEncoding();
        if (retVal != null) {
            return retVal;
        }
        if (!isEmbedded) {
            return StandardCharSetEncodings.getDefaultCharsetForFont(fontName);
        }
        return null;
    }

    public CharSetEncoding getBaseEncoding(ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getBaseEncoding(fontName, false);
    }

    public PDFEncodingDifferences getDifferences() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFEncodingDifferences diffs = null;
        if (this.getCosObject() instanceof CosDictionary) {
            if (((CosDictionary)this.getCosObject()).containsKey((Object)ASName.k_Differences)) {
                CosArray array = ((CosDictionary)this.getCosObject()).getCosArray(ASName.k_Differences);
                return PDFEncodingDifferences.getInstance((CosObject)array);
            }
            return null;
        }
        return diffs;
    }

    public void setDifferences(PDFEncodingDifferences diffs) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ((CosDictionary)this.getCosObject()).put(ASName.k_Differences, (CosObject)diffs.getCosArray());
    }

    @Override
    public String toUnicode(ASString string) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        byte[] bytes = string.getBytes();
        return this.toUnicode(bytes, 0, bytes.length);
    }

    public String toUnicode(ASString string, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        byte[] bytes = string.getBytes();
        return this.toUnicode(bytes, 0, bytes.length, fontName);
    }

    @Override
    public String toUnicode(byte[] bytes, int start, int length) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding();
        return this.toUnicode(bytes, start, length, baseEnc);
    }

    public String toUnicode(byte[] bytes, int start, int length, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding(fontName);
        return this.toUnicode(bytes, start, length, baseEnc);
    }

    private String toUnicode(byte[] bytes, int start, int length, CharSetEncoding baseEnc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFEncodingDifferences diffs = this.getDifferences();
        if (baseEnc == null) {
            try {
                baseEnc = StandardCharSetEncodings.get(ASName.k_StandardEncoding);
            }
            catch (PDFInvalidParameterException e) {
                throw new RuntimeException("Could not get a pre-defined character set encoding", (Throwable)e);
            }
        }
        if (length > 0 && bytes != null) {
            StringBuilder strBuf = new StringBuilder(bytes.length);
            for (int i = start; i < length; ++i) {
                char[] uniChars = new char[1];
                int charCode = bytes[i] & 255;
                if (diffs != null && diffs.contains(charCode)) {
                    uniChars = diffs.toUnicode(charCode);
                } else if (baseEnc != null) {
                    uniChars = baseEnc.toUnicode(bytes[i]);
                } else {
                    uniChars[0] = 65533;
                }
                strBuf.append(uniChars);
            }
            return strBuf.toString();
        }
        return null;
    }

    @Override
    public char[] toUnicode(byte charCode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding();
        return this.toUnicode(charCode, baseEnc);
    }

    @Override
    public char[] toUnicode(byte charCode, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding(fontName);
        return this.toUnicode(charCode, baseEnc);
    }

    private char[] toUnicode(byte charCode, CharSetEncoding baseEnc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFEncodingDifferences diffs = this.getDifferences();
        char[] unicodes = new char[1];
        if (diffs != null && diffs.contains(charCode)) {
            unicodes = diffs.toUnicode(charCode);
        } else if (baseEnc != null) {
            unicodes = baseEnc.toUnicode(charCode);
        } else {
            unicodes[0] = 65533;
        }
        return unicodes;
    }

    @Override
    public int fromUnicode(char unicodeChar) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding();
        return this.fromUnicode(unicodeChar, baseEnc);
    }

    public int fromGlyphName(ASString glyphName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding();
        return this.fromGlyphName(glyphName.asString(), baseEnc);
    }

    public int fromGlyphName(String glyphName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding();
        return this.fromGlyphName(glyphName, baseEnc);
    }

    public int fromGlyphName(String glyphName, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding(fontName);
        return this.fromGlyphName(glyphName, baseEnc);
    }

    int fromUnicode(char unicodeChar, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEnc = this.getBaseEncoding(fontName);
        return this.fromUnicode(unicodeChar, baseEnc);
    }

    private int fromUnicode(char unicodeChar, CharSetEncoding baseEnc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        AdobeGlyphList glyphList;
        ASName[] glyphNames;
        PDFEncodingDifferences diffs = this.getDifferences();
        boolean found = false;
        int charCode = 0;
        if (diffs != null && (glyphNames = (glyphList = AdobeGlyphList.get()).fromUnicode(unicodeChar)).length > 0 && diffs.contains(glyphNames[0])) {
            charCode = diffs.fromGlyphName(glyphNames[0]);
            found = true;
        }
        if (!found) {
            if (baseEnc == null) {
                try {
                    baseEnc = StandardCharSetEncodings.get(ASName.k_StandardEncoding);
                }
                catch (PDFInvalidParameterException e) {
                    throw new RuntimeException("Could not get a pre-defined character set encoding", (Throwable)e);
                }
            }
            charCode = baseEnc.fromUnicode(unicodeChar);
        }
        return charCode;
    }

    private int fromGlyphName(String glyphName, CharSetEncoding baseEnc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFEncodingDifferences diffs = this.getDifferences();
        boolean found = false;
        int charCode = 0;
        ASName name = ASName.create((String)glyphName);
        if (diffs != null && diffs.contains(name)) {
            charCode = diffs.fromGlyphName(name);
            found = true;
        }
        if (!found) {
            if (baseEnc == null) {
                try {
                    baseEnc = StandardCharSetEncodings.get(ASName.k_StandardEncoding);
                }
                catch (PDFInvalidParameterException e) {
                    throw new RuntimeException("Could not get a pre-defined character set encoding", (Throwable)e);
                }
            }
            charCode = baseEnc.fromGlyphName(glyphName);
            if (diffs != null && charCode != 0 && diffs.contains(charCode)) {
                charCode = 0;
            }
        }
        return charCode;
    }

    @Override
    public String getGlyphName(int charCode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName name;
        PDFEncodingDifferences diffs = this.getDifferences();
        if (diffs != null && (name = diffs.toGlyphName(charCode)) != null) {
            return name.asString(true);
        }
        CharSetEncoding encoding = this.getBaseEncoding();
        if (encoding != null) {
            return encoding.getGlyphName(charCode);
        }
        return null;
    }

    public String getGlyphName(int charCode, boolean isSymbolic, boolean isEmbedded) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName name;
        PDFEncodingDifferences diffs = this.getDifferences();
        if (diffs != null && (name = diffs.toGlyphName(charCode)) != null) {
            return name.asString(true);
        }
        CharSetEncoding encoding = this.getBaseEncoding(isSymbolic, isEmbedded);
        if (encoding != null) {
            return encoding.getGlyphName(charCode);
        }
        return null;
    }

    public String getGlyphName(int charCode, ASName fontName, boolean isEmbedded) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding encoding;
        PDFEncodingDifferences diffs = this.getDifferences();
        if (diffs != null && diffs.contains(charCode)) {
            String name = diffs.toGlyphName(charCode).asString(true);
            if (fontName == ASName.k_ZapfDingbats) {
                name = ZapfDingbatsEncoding.zapfNameToPiName(name);
            }
            if (name != null) {
                return name;
            }
        }
        if ((encoding = this.getBaseEncoding(fontName, isEmbedded)) != null) {
            return encoding.getGlyphName(charCode);
        }
        return null;
    }

    public String getGlyphName(int charCode, ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getGlyphName(charCode, fontName, false);
    }

    public ASName getGlyphNameAsASName(int charCode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName name;
        PDFEncodingDifferences diffs = this.getDifferences();
        if (diffs != null && (name = diffs.toGlyphName(charCode)) != null) {
            return name;
        }
        CharSetEncoding encoding = this.getBaseEncoding();
        if (encoding != null) {
            return ASName.create((String)encoding.getGlyphName(charCode));
        }
        return null;
    }

    public ASName getGlyphNameAsASName(int charCode, boolean isSymbolic, boolean isEmbedded) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName name;
        PDFEncodingDifferences diffs = this.getDifferences();
        if (diffs != null && (name = diffs.toGlyphName(charCode)) != null) {
            return name;
        }
        CharSetEncoding encoding = this.getBaseEncoding(isSymbolic, isEmbedded);
        if (encoding != null) {
            return ASName.create((String)encoding.getGlyphName(charCode));
        }
        return null;
    }

    public boolean isMacRomanEncoding() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEncoding;
        if (this.getCosObject() instanceof CosName ? ASName.k_MacRomanEncoding.equals((Object)((CosName)this.getCosObject()).nameValue()) : (baseEncoding = this.getBaseEncoding()) != null && this.getDifferences() == null && ASName.k_MacRomanEncoding.equals((Object)baseEncoding.getName())) {
            return true;
        }
        return false;
    }

    public boolean isMacExpertEncoding() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEncoding;
        if (this.getCosObject() instanceof CosName ? ASName.k_MacExpertEncoding.equals((Object)((CosName)this.getCosObject()).nameValue()) : (baseEncoding = this.getBaseEncoding()) != null && this.getDifferences() == null && ASName.k_MacExpertEncoding.equals((Object)baseEncoding.getName())) {
            return true;
        }
        return false;
    }

    public boolean isWinAnsiEncoding() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CharSetEncoding baseEncoding;
        if (this.getCosObject() instanceof CosName ? ASName.k_WinAnsiEncoding.equals((Object)((CosName)this.getCosObject()).nameValue()) : (baseEncoding = this.getBaseEncoding()) != null && this.getDifferences() == null && ASName.k_WinAnsiEncoding.equals((Object)baseEncoding.getName())) {
            return true;
        }
        return false;
    }
}