PDFText.java 10.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.IO
 *  com.adobe.internal.io.stream.InputByteStream
 *  com.adobe.internal.io.stream.StreamManager
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.cos.CosStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosString
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 *  com.adobe.internal.pdftoolkit.core.util.PDFDocEncoding
 */
package com.adobe.internal.pdftoolkit.pdf.document;

import com.adobe.internal.io.stream.IO;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.core.util.PDFDocEncoding;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFStream;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilter;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilterFlate;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilterList;
import java.io.IOException;

public class PDFText
extends PDFCosObject {
    private static final char UTF16BE_BOM = '\ufeff';
    private static final int BADSEQUENCE = -1;

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

    private PDFText(CosObject cosObj) throws PDFInvalidDocumentException {
        super(cosObj);
        int cosType = cosObj.getType();
        if (cosType != 7 && cosType != 4) {
            throw new PDFInvalidDocumentException("attempt to construct PDF text from non-text object");
        }
    }

    public static PDFText newInstance(PDFDocument pdfDocument, ASString text) throws PDFInvalidDocumentException {
        CosString string = PDFCosObject.newCosString(pdfDocument, text);
        return new PDFText((CosObject)string);
    }

    public static PDFText newInstance(PDFDocument doc, String textString, boolean asStream, boolean forceUTF16) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        try {
            forceUTF16 |= !PDFDocEncoding.isPDFDocEncoding((String)textString);
            if (asStream) {
                InputByteStream byteStream = doc.getStreamManager().getInputByteStream(PDFDocEncoding.fromUnicodeString((String)textString, (boolean)forceUTF16));
                return new PDFText((CosObject)PDFCosObject.newCosStream(doc, byteStream));
            }
            return new PDFText((CosObject)PDFCosObject.newCosString(doc, PDFDocEncoding.fromUnicodeString((String)textString, (boolean)forceUTF16)));
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    public static PDFText newInstance(String textString, boolean isUTF16, PDFDocument doc) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        if (textString.length() > 32000) {
            return PDFText.newInstance(doc, textString, true, isUTF16);
        }
        return PDFText.newInstance(doc, textString, false, isUTF16);
    }

    public static PDFText newInstance(PDFDocument doc, String textString) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFText.newInstance(textString, false, doc);
    }

    public static PDFText createString(PDFDocument doc, String textString) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFText.newInstance(doc, textString, false, false);
    }

    public static PDFText createStream(PDFDocument doc, String textString) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFText.newInstance(doc, textString, true, false);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public String stringValue() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        try {
            int cosType = this.getCosObject().getType();
            byte[] bytes = null;
            if (cosType == 4) {
                bytes = ((CosString)this.getCosObject()).byteArrayValue();
            } else if (cosType == 7) {
                CosStream stream = (CosStream)this.getCosObject();
                InputByteStream byteStream = null;
                try {
                    byteStream = stream.getStreamDecoded();
                    bytes = IO.inputByteStreamToArray((InputByteStream)byteStream);
                }
                finally {
                    if (byteStream != null) {
                        byteStream.close();
                    }
                }
            }
            try {
                if (bytes == null) {
                    return null;
                }
                return PDFDocEncoding.toUnicodeString((byte[])bytes, (int)0, (int)bytes.length);
            }
            catch (PDFParseException e) {
                throw new PDFInvalidDocumentException((Throwable)e);
            }
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    public byte[] byteArrayValue() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        try {
            int cosType = this.getCosObject().getType();
            byte[] bytes = null;
            if (cosType == 4) {
                bytes = ((CosString)this.getCosObject()).byteArrayValue();
            } else if (cosType == 7) {
                bytes = IO.inputByteStreamToArray((InputByteStream)((CosStream)this.getCosObject()).getStreamDecoded());
            }
            return bytes;
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    public boolean isUTF16() throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        return PDFText.isUTF16(this.stringValue());
    }

    public static boolean isUTF16(String stringValue) {
        return stringValue.length() > 0 && stringValue.charAt(0) == '\ufeff';
    }

    public static boolean isValidUTF8(byte[] data) {
        long thisChar = 0;
        int curPos = 0;
        int bytesAvail = data.length;
        do {
            int nextNextByte;
            int nextByte;
            short firstByte;
            if ((firstByte = (short)(255 & data[curPos++])) == 0) {
                thisChar = 0;
            } else if (firstByte <= 127) {
                thisChar = firstByte;
            } else if ((firstByte & 224) == 192) {
                nextByte = bytesAvail > 1 ? (short)(255 & data[curPos]) : 0;
                thisChar = -1;
                if ((nextByte & 192) == 128) {
                    ++curPos;
                    thisChar = (firstByte & 31) << 6 | nextByte & 63;
                    if (thisChar < 128) {
                        thisChar = -1;
                    }
                }
            } else if ((firstByte & 240) == 224) {
                nextByte = bytesAvail > 1 ? (short)(255 & data[curPos]) : 0;
                thisChar = -1;
                if ((nextByte & 192) == 128) {
                    nextNextByte = bytesAvail > 2 ? (short)(255 & data[++curPos]) : 0;
                    short s = (short)nextNextByte;
                    if ((nextNextByte & 192) == 128) {
                        ++curPos;
                        thisChar = (firstByte & 15) << 12 | (nextByte & 63) << 6 | nextNextByte & 63;
                        if (thisChar < 2048) {
                            thisChar = -1;
                        }
                    }
                }
            } else if ((firstByte & 248) == 240) {
                nextByte = bytesAvail > 1 ? (short)(255 & data[curPos]) : 0;
                thisChar = -1;
                if ((nextByte & 192) == 128) {
                    nextNextByte = bytesAvail > 2 ? (short)(255 & data[++curPos]) : 0;
                    short s = (short)nextNextByte;
                    if ((nextNextByte & 192) == 128) {
                        int nextNextNextByte = bytesAvail > 3 ? (short)(255 & data[++curPos]) : 0;
                        short s2 = (short)nextNextNextByte;
                        if ((nextNextNextByte & 192) == 128) {
                            ++curPos;
                            long high = (firstByte & 7) << 8 | (nextByte & 63) << 2 | (nextNextByte & 48) >> 4;
                            long low = (nextNextByte & 15) << 6 | nextNextNextByte & 63;
                            thisChar = (high -= 64) * 1024 + low + 65536;
                            if (thisChar < 65536) {
                                thisChar = -1;
                            }
                        }
                    }
                }
            } else {
                thisChar = -1;
            }
            if (thisChar > 0 && thisChar != -1) {
                if (thisChar >= 55296 && thisChar <= 57343) {
                    thisChar = -1;
                } else if (thisChar > 1114111) {
                    thisChar = -1;
                }
            }
            bytesAvail = data.length - curPos;
        } while (thisChar != -1 && thisChar != 0 && bytesAvail > 0);
        return thisChar != -1 && thisChar != 0;
    }

    public void setFilter(PDFFilterFlate filter) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        CosObject pdfTextCosObject = this.getCosObject();
        if (pdfTextCosObject instanceof CosStream) {
            PDFStream pdfTextStream = PDFStream.getInstance(pdfTextCosObject);
            PDFFilterList filterList = pdfTextStream.procureOutputFilters();
            filterList.add(filter);
            pdfTextStream.setOutputFilters(filterList);
        }
    }
}