TextMeasurement.java
6.37 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.text;
import com.adobe.xfa.font.FontInstance;
import com.adobe.xfa.text.Units;
import com.adobe.xfa.ut.UnitSpan;
public class TextMeasurement
implements Comparable {
public static final int TYPE_LENGTH = 0;
public static final int TYPE_EM = 1;
public static final int TYPE_PERCENT = 2;
public static final int DEFAULT_PRECISION = 6;
public static final TextMeasurement ZERO = new TextMeasurement();
private final int meType;
private final UnitSpan moLength;
private final double mdScale;
private boolean mbUseHorzScalingForFlatten;
static UnitSpan moBaseUnitForCompare = new UnitSpan(1.0, 19);
public TextMeasurement() {
this.meType = 0;
this.moLength = UnitSpan.ZERO;
this.mdScale = 0.0;
this.mbUseHorzScalingForFlatten = true;
}
public TextMeasurement(UnitSpan oLength) {
this.meType = 0;
this.moLength = oLength == null ? UnitSpan.ZERO : oLength;
this.mdScale = 0.0;
this.mbUseHorzScalingForFlatten = true;
}
public TextMeasurement(int eType, double dScale) {
this.meType = eType;
this.moLength = null;
this.mdScale = dScale;
this.mbUseHorzScalingForFlatten = false;
}
public TextMeasurement(int eType, double dScale, boolean bUseHorzScalingForFlatten) {
this.meType = eType;
this.moLength = null;
this.mdScale = dScale;
this.mbUseHorzScalingForFlatten = bUseHorzScalingForFlatten;
}
public UnitSpan flatten(FontInstance oFontInstance) {
double dWidth;
if (this.meType == 0) {
return this.moLength;
}
if (this.mdScale == 0.0) {
return UnitSpan.ZERO;
}
assert (oFontInstance != null);
UnitSpan oResult = oFontInstance.getSize();
if (this.mbUseHorzScalingForFlatten) {
oResult = oResult.multiply(oFontInstance.getHorizontalScale());
}
if (this.meType == 2 && (dWidth = oFontInstance.getDoubleCharWidth(32, true)) > 0.0) {
oResult = new UnitSpan(dWidth, 19);
}
return oResult.multiply(this.mdScale);
}
public UnitSpan flatten(UnitSpan oBaseValue) {
return this.meType == 0 ? this.moLength : oBaseValue.multiply(this.mdScale);
}
public int getType() {
return this.meType;
}
public UnitSpan getLength() {
return this.moLength;
}
public int getLengthValue() {
return this.moLength.value();
}
public double getScale() {
return this.mdScale;
}
public boolean isZero() {
return this.meType == 0 ? this.moLength.value() == 0 : this.mdScale == 0.0;
}
public static TextMeasurement fromString(String sValue, int eDefaultUnits, boolean bValuePerUnit) {
int eType;
UnitSpan.ParseData oParseData = UnitSpan.validatingParse(sValue, eDefaultUnits, true, bValuePerUnit, true);
if (oParseData == null) {
return null;
}
if (oParseData.mbValuePerUnit) {
return null;
}
if (oParseData.meUnits != 255) {
return new TextMeasurement(new UnitSpan(oParseData.meUnits, oParseData.mnValue));
}
double dValue = oParseData.mnValue;
if (oParseData.mnFraction != 0) {
dValue += (double)oParseData.mnFraction / (double)oParseData.mnFractionScale;
}
if (oParseData.mbPercent) {
eType = 2;
dValue /= 100.0;
} else {
if (oParseData.mcUnit2 != '\u0000') {
return null;
}
eType = 1;
char c0 = oParseData.mcUnit0;
char c1 = oParseData.mcUnit1;
if (c0 != 'e' && c0 != 'E' || c1 != 'm' && c1 != 'M') {
return null;
}
}
return new TextMeasurement(eType, dValue);
}
public static TextMeasurement fromString(String sValue, int eDefaultUnits) {
return TextMeasurement.fromString(sValue, eDefaultUnits, false);
}
public static TextMeasurement fromString(String sValue) {
return TextMeasurement.fromString(sValue, 255, false);
}
public String toString(int nPrecision) {
StringBuilder sResult = new StringBuilder();
switch (this.meType) {
case 1: {
sResult.append(Units.doubleToString(this.mdScale, nPrecision));
sResult.append('e');
sResult.append('m');
break;
}
case 2: {
sResult.append(Units.doubleToString(this.mdScale * 100.0, nPrecision));
sResult.append('%');
break;
}
default: {
sResult.append(this.moLength.text(nPrecision, true, false));
}
}
return sResult.toString();
}
public static boolean match(TextMeasurement m1, TextMeasurement m2) {
if (m1 == m2) {
return true;
}
if (m1 == null || m2 == null) {
return false;
}
return m1.equals(m2);
}
public String toString() {
return this.toString(6);
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (object.getClass() != this.getClass()) {
return false;
}
TextMeasurement compare = (TextMeasurement)object;
if (this.meType != compare.meType) {
return false;
}
if (this.meType == 0) {
return UnitSpan.match(this.moLength, compare.moLength);
}
return this.mdScale == compare.mdScale;
}
public int hashCode() {
int hash = Integer.valueOf(this.meType).hashCode();
if (this.meType == 0) {
hash = hash * 31 ^ this.moLength.hashCode();
}
long bits = Double.doubleToLongBits(this.mdScale);
hash = hash * 31 ^ (int)(bits ^ bits >>> 32);
return hash;
}
public static TextMeasurement zero() {
return ZERO;
}
public int compareTo(Object object) {
if (object == null) {
throw new NullPointerException();
}
TextMeasurement compare = (TextMeasurement)object;
return this.flatten(moBaseUnitForCompare).compareTo(compare.flatten(moBaseUnitForCompare));
}
}