PDFText.java
10.6 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
/*
* 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);
}
}
}