PDFEncodingDifferences.java
7.11 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosName
* com.adobe.internal.pdftoolkit.core.cos.CosNumeric
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.graphics.font;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.AdobeGlyphList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class PDFEncodingDifferences
extends PDFCosObject {
private final Map<Integer, ASName> diffs;
private PDFEncodingDifferences(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
this.diffs = this.initDiffs();
}
public static PDFEncodingDifferences getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFEncodingDifferences pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFEncodingDifferences.class);
if (pdfObject == null) {
pdfObject = new PDFEncodingDifferences(cosObject);
}
return pdfObject;
}
public static PDFEncodingDifferences newInstance(PDFDocument pdfDocument, Map<Integer, String> mapDifferences) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosObject = PDFCosObject.newCosArray(pdfDocument);
PDFEncodingDifferences pdfObject = new PDFEncodingDifferences((CosObject)cosObject);
for (Map.Entry<Integer, String> entry : mapDifferences.entrySet()) {
Integer charCode = entry.getKey();
ASName gNameASName = ASName.create((String)entry.getValue());
pdfObject.getCosArray().addInt(charCode.intValue());
pdfObject.getCosArray().addName(gNameASName);
pdfObject.diffs.put(charCode, gNameASName);
}
return pdfObject;
}
public void updateDifferences() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.getCosArray().clear();
for (Map.Entry<Integer, ASName> entry : this.diffs.entrySet()) {
this.getCosArray().addInt(entry.getKey().intValue());
this.getCosArray().addName(entry.getValue());
}
}
public void addDifference(Integer charCode, ASName gNameASName) throws PDFCosParseException, PDFIOException, PDFSecurityException {
this.getCosArray().addInt(charCode.intValue());
this.getCosArray().addName(gNameASName);
this.diffs.put(charCode, gNameASName);
}
public Set<Map.Entry<Integer, ASName>> entrySet() {
return this.diffs.entrySet();
}
public Set keySet() {
return this.diffs.keySet();
}
public boolean contains(int charCode) {
if (this.diffs.containsKey(charCode)) {
return true;
}
return false;
}
public boolean contains(ASName glyphName) {
if (this.diffs.containsValue((Object)glyphName)) {
return true;
}
return false;
}
public ASName toGlyphName(int charCode) {
return this.diffs.get(charCode);
}
public char[] toUnicode(int charCode) {
ASName charName = this.toGlyphName(charCode);
if (charName == null) {
char[] chars = new char[]{'\ufffd'};
return chars;
}
AdobeGlyphList glyphList = AdobeGlyphList.get();
char[] unicode = glyphList.toUnicode(charName);
if (unicode[0] == '\ufffd') {
try {
String charNameStr = charName.asString();
if (charNameStr.matches("[0-9]+")) {
Integer decVal = Integer.decode(charNameStr);
char[] directUnicode = new char[]{(char)decVal.intValue()};
return directUnicode;
}
if (charNameStr.length() > 0 && charNameStr.charAt(0) == 'n') {
String hexDigits = charNameStr.substring(1);
int len = hexDigits.length();
int size = (len + 1) / 2;
char[] directUnicode = new char[size];
int j = 0;
for (int i = 0; i < size; ++i) {
String hexStr = null;
hexStr = i == size - 1 ? hexDigits.substring(2 * i) : hexDigits.substring(2 * i, 2 * i + 2);
Integer hexVal = Integer.decode("0x" + hexStr);
directUnicode[j++] = (char)hexVal.intValue();
}
return directUnicode;
}
}
catch (NumberFormatException e) {
// empty catch block
}
}
return unicode;
}
public int fromGlyphName(ASName glyphName) {
if (this.contains(glyphName)) {
Set<Map.Entry<Integer, ASName>> entrySet = this.diffs.entrySet();
for (Map.Entry<Integer, ASName> entry : entrySet) {
ASName value = entry.getValue();
if (value != glyphName) continue;
Integer charCode = entry.getKey();
return charCode;
}
}
return 0;
}
private Map<Integer, ASName> initDiffs() {
CosArray array = this.getCosArray();
int size = array.size();
if (size == 0) {
return new LinkedHashMap<Integer, ASName>();
}
LinkedHashMap<Integer, ASName> map = new LinkedHashMap<Integer, ASName>(size - 1);
Iterator iter = array.iterator();
int charCode = 0;
while (iter.hasNext()) {
CosObject item = (CosObject)iter.next();
if (item instanceof CosNumeric) {
charCode = ((CosNumeric)item).intValue();
continue;
}
if (!(item instanceof CosName)) continue;
map.put(charCode++, ((CosName)item).nameValue());
}
return map;
}
}