FontItem.java
6.62 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.fontengine.font.Font
* com.adobe.fontengine.font.LineMetrics
* com.adobe.fontengine.font.PDFFontDescription
*/
package com.adobe.xfa.font;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.LineMetrics;
import com.adobe.fontengine.font.PDFFontDescription;
import com.adobe.xfa.font.FontInfo;
import com.adobe.xfa.font.FontInstance;
import com.adobe.xfa.ut.UnitSpan;
import java.util.HashMap;
import java.util.Map;
public class FontItem
extends FontInfo {
private final Font mAFEFont;
private PDFFontDescription mAFEPDFDesc;
private double mAscent;
private double mLegacyAscent;
private double mDescent;
private double mLineGap;
private double mLegacyLineGap;
private double mSpacing;
private final Map<Integer, Integer> mGlyphMap = new HashMap<Integer, Integer>();
private final Map<InstanceKey, FontInstance> mInstances = new HashMap<InstanceKey, FontInstance>();
FontItem(FontInfo info, Font afeFont) {
super(info);
this.mAFEFont = afeFont;
}
public static double ctLegacyScale(double emScale, double advance) {
assert (emScale != 0.0);
double onePtRel = advance / emScale;
long truncated = (long)(onePtRel * 65536.0);
double result = (double)truncated / 65536.0;
return result;
}
public Font getAFEFont() {
return this.mAFEFont;
}
public double getAscent() {
return this.mAscent;
}
public double getLegacyAscent() {
return this.mLegacyAscent;
}
public double getDescent() {
return this.mDescent;
}
public double getLineGap() {
return this.mLineGap;
}
public double getLegacyLineGap() {
return this.mLegacyLineGap;
}
public double getSpacing() {
return this.mSpacing;
}
public double getCharWidth(int c, boolean useHorizontalGlyphs) {
int glyphID = this.getGlyphID(c);
if (glyphID == 0) {
return -1.0;
}
return this.getGlyphWidth(glyphID, useHorizontalGlyphs);
}
public int getNotDefGlyphID() {
return 0;
}
public int getGlyphID(int c) {
Integer value = this.mGlyphMap.get(c);
if (value == null) {
return 0;
}
return value;
}
public double getGlyphWidth(int glyphID, boolean useHorizontalGlyphs) {
double width = -1.0;
PDFFontDescription pdfDesc = this.forcePDFDesc();
if (pdfDesc != null) {
try {
width = this.mAFEPDFDesc.getAdvance(glyphID);
}
catch (Exception e) {
// empty catch block
}
}
return FontItem.ctLegacyScale(1000.0, width);
}
public boolean validateChar(int c, boolean useHorizontalGlyphs) {
return true;
}
public FontInstance reconcile(UnitSpan size) {
return this.reconcile(size, 1.0, 1.0);
}
public FontInstance reconcile(UnitSpan size, double horizontalScale, double verticalScale) {
InstanceKey key = new InstanceKey(size, horizontalScale, verticalScale);
FontInstance instance = this.lookup(key);
if (instance == null) {
instance = new FontInstance(this, size);
this.mInstances.put(key, instance);
}
return instance;
}
@Override
public boolean equals(Object object) {
return super.equals(object);
}
@Override
public int hashCode() {
int hash = 29;
hash = hash * 31 ^ super.hashCode();
return hash;
}
public static boolean match(FontItem o1, FontItem o2) {
if (o1 == o2) {
return true;
}
if (o1 == null || o2 == null) {
return false;
}
return o1.equals(o2);
}
public void addSubsettedGlyphs(int glyph, int chr) {
}
public FontItem getUnicodeFont() {
return this;
}
public boolean isPWIDApplied() {
return false;
}
public void mapGlyph(int c, int glyphID) {
this.mGlyphMap.put(c, glyphID);
}
boolean loadMetrics() {
try {
LineMetrics lineMetrics = this.mAFEFont.getCoolTypeLineMetrics();
double scale = this.mAFEFont.getUnitsPerEmY();
this.mLegacyAscent = this.mAscent = lineMetrics.ascender / scale;
this.mDescent = (- lineMetrics.descender) / scale;
if (this.mAscent + this.mDescent < 1.0) {
this.mAscent = 1.0 - this.mDescent;
}
this.mLineGap = 0.2;
this.mLegacyLineGap = lineMetrics.linegap / scale;
this.mSpacing = 1.2;
}
catch (Exception e) {
return false;
}
return true;
}
private FontInstance lookup(InstanceKey key) {
return this.mInstances.get(key);
}
private PDFFontDescription forcePDFDesc() {
if (this.mAFEPDFDesc == null) {
try {
this.mAFEPDFDesc = this.mAFEFont.getPDFFontDescription();
}
catch (Exception e) {
// empty catch block
}
}
return this.mAFEPDFDesc;
}
static class InstanceKey {
final UnitSpan mSize;
final float mHorizontalScale;
final float mVerticalScale;
InstanceKey(UnitSpan size) {
this.mSize = size;
this.mHorizontalScale = 1.0f;
this.mVerticalScale = 1.0f;
}
InstanceKey(UnitSpan size, double horizontalScale, double verticalScale) {
this.mSize = size;
this.mHorizontalScale = (float)horizontalScale;
this.mVerticalScale = (float)verticalScale;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (object.getClass() != this.getClass()) {
return false;
}
InstanceKey compare = (InstanceKey)object;
if (!UnitSpan.match(this.mSize, compare.mSize)) {
return false;
}
return this.mHorizontalScale == compare.mHorizontalScale && this.mVerticalScale == compare.mVerticalScale;
}
public int hashCode() {
int result = this.mSize.hashCode();
int f = Float.floatToIntBits(this.mHorizontalScale);
result = result * 31 ^ f;
f = Float.floatToIntBits(this.mVerticalScale);
result = result * 31 ^ f;
return result;
}
}
}