FontFactory.java
8.14 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.agl.util.ULocale
* com.adobe.internal.io.ExtendedDataInputStream
* com.adobe.internal.mac.resource.ResourceParser
* com.adobe.internal.mac.resource.ResourceParser$ResourceHandler
*/
package com.adobe.fontengine.font.mac;
import com.adobe.agl.util.ULocale;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontData;
import com.adobe.fontengine.font.FontImpl;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.fontengine.font.mac.FONDResourceHandler;
import com.adobe.fontengine.font.mac.ResourceFontData;
import com.adobe.fontengine.font.mac.sfntResourceHandler;
import com.adobe.fontengine.font.mac.versResourceHandler;
import com.adobe.fontengine.fontmanagement.ResourceFont;
import com.adobe.internal.io.ExtendedDataInputStream;
import com.adobe.internal.mac.resource.ResourceParser;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
public class FontFactory {
private static final String RESOURCE_FORK_EXTENSION = "/..namedfork/rsrc";
private static final String DFONT_SUFFIX = ".dfont";
public static final int UNKNOWN = 0;
public static final int RESOURCE_FORK_FONT = 1;
public static final int DATA_FORK_FONT = 2;
private static final String[] SCRIPTID_TO_ULOCALE = new String[]{"en", "ja", "zh-Hant", "ko", "ar", "he", "el", "ru", "MacSymbol", "hi", "pa", "gu", null, null, null, null, null, null, null, null, null, "th", null, null, null, "zh-Hans", null, null, null, "MacCentralEuropean", null, null, null};
public static FontData load(URL url, int type, int fontID, int fondID) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
ResourceFontData rsrcFontData = null;
ResourceParser parser = new ResourceParser();
FONDResourceHandler handlerFOND = new FONDResourceHandler();
versResourceHandler handlervers = new versResourceHandler();
sfntResourceHandler handlersfnt = new sfntResourceHandler(fontID);
parser.addHandler((ResourceParser.ResourceHandler)handlerFOND);
parser.addHandler((ResourceParser.ResourceHandler)handlervers);
parser.addHandler((ResourceParser.ResourceHandler)handlersfnt);
try {
parser.setURL(url);
parser.parse();
}
catch (IOException e) {
throw new FontLoadingException(e);
}
Map<Integer, sfntResourceHandler.SfntResource> sfntMap = handlersfnt.getResources();
Set<FONDResourceHandler.Association> associationSet = handlerFOND.getAssociations();
for (FONDResourceHandler.Association association : associationSet) {
sfntResourceHandler.SfntResource resource;
Font font;
if (fondID != association.getFondID() || fontID != association.getFontID() || (resource = sfntMap.get(fontID)) == null || (font = resource.getFont()) == null) continue;
rsrcFontData = new ResourceFontData(((FontImpl)font).getFontData(), FontFactory.scriptCodeToCharset(resource.getScriptCode()), association.getName(), resource.getName(), association.isBold(), association.isItalic());
break;
}
return rsrcFontData;
}
public static ResourceFont[] load(URL url, int type) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
ResourceParser parser = new ResourceParser();
FONDResourceHandler handlerFOND = new FONDResourceHandler();
versResourceHandler handlervers = new versResourceHandler();
sfntResourceHandler handlersfnt = new sfntResourceHandler();
parser.addHandler((ResourceParser.ResourceHandler)handlerFOND);
parser.addHandler((ResourceParser.ResourceHandler)handlervers);
parser.addHandler((ResourceParser.ResourceHandler)handlersfnt);
try {
if (type == 1) {
url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "/..namedfork/rsrc");
}
parser.setURL(url);
parser.parse();
}
catch (IOException e) {
throw new FontLoadingException(e);
}
Map<Integer, sfntResourceHandler.SfntResource> sfntMap = handlersfnt.getResources();
Set<FONDResourceHandler.Association> associationSet = handlerFOND.getAssociations();
ArrayList<ResourceFont> fonts = new ArrayList<ResourceFont>();
for (FONDResourceHandler.Association association : associationSet) {
Font font;
int sfntID = association.getFontID();
sfntResourceHandler.SfntResource resource = sfntMap.get(sfntID);
if (resource == null || (font = resource.getFont()) == null) continue;
ResourceFontData rsrcFontData = new ResourceFontData(((FontImpl)font).getFontData(), FontFactory.scriptCodeToCharset(resource.getScriptCode()), association.getName(), resource.getName(), association.isBold(), association.isItalic());
ResourceFont rsrcFont = new ResourceFont(url, type, sfntID, association.getFondID(), rsrcFontData);
fonts.add(rsrcFont);
}
ResourceFont[] a = new ResourceFont[fonts.size()];
return fonts.toArray(a);
}
public static int getNumBytesNeededToIdentify() {
return 16;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static int isResourceFont(byte[] bytes, URL url) throws IOException {
InputStream rsrcStream = null;
int streamType = 0;
try {
long mapOffset;
long dataOffset;
long mapLength;
long dataLength;
rsrcStream = FontFactory.getDataResourceFontStream(url);
if (rsrcStream != null) {
streamType = 2;
} else {
rsrcStream = FontFactory.getResourceStream(url);
if (rsrcStream != null) {
streamType = 1;
}
}
if (rsrcStream == null) {
int n = 0;
return n;
}
try {
ExtendedDataInputStream dis = new ExtendedDataInputStream(rsrcStream);
dataOffset = dis.readUnsignedInt();
mapOffset = dis.readUnsignedInt();
dataLength = dis.readUnsignedInt();
mapLength = dis.readUnsignedInt();
}
catch (Exception e) {
int n = 0;
if (rsrcStream != null) {
rsrcStream.close();
}
return n;
}
if (mapOffset > dataOffset && mapOffset < dataOffset + dataLength || dataOffset > mapOffset && dataOffset < mapOffset + mapLength) {
int e = 0;
return e;
}
int e = streamType;
return e;
}
finally {
if (rsrcStream != null) {
rsrcStream.close();
}
}
}
private static InputStream getResourceStream(URL url) throws IOException {
URL rsrcURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "/..namedfork/rsrc");
InputStream rsrcStream = null;
try {
rsrcStream = rsrcURL.openStream();
}
catch (FileNotFoundException e) {
// empty catch block
}
return rsrcStream;
}
private static InputStream getDataResourceFontStream(URL url) throws IOException {
InputStream rsrcStream = null;
String file = url.getFile();
if (file.endsWith(".dfont")) {
rsrcStream = url.openStream();
}
return rsrcStream;
}
static final ULocale scriptCodeToCharset(int script) {
String locale = SCRIPTID_TO_ULOCALE[script = Math.min(script, SCRIPTID_TO_ULOCALE.length - 1)];
if (locale == null) {
locale = SCRIPTID_TO_ULOCALE[0];
}
return new ULocale(locale);
}
}