RawProcessor.java
18 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.text;
import com.adobe.xfa.font.FontInstance;
import com.adobe.xfa.font.FontItem;
import com.adobe.xfa.font.FontService;
import com.adobe.xfa.gfx.GFXGlyphOrientation;
import com.adobe.xfa.text.Composition;
import com.adobe.xfa.text.DispEmbed;
import com.adobe.xfa.text.DispLine;
import com.adobe.xfa.text.DispLineRaw;
import com.adobe.xfa.text.DispMapItem;
import com.adobe.xfa.text.DispMapSpan;
import com.adobe.xfa.text.DispPosn;
import com.adobe.xfa.text.DispRun;
import com.adobe.xfa.text.DispTab;
import com.adobe.xfa.text.FormatInfo;
import com.adobe.xfa.text.TextAttr;
import com.adobe.xfa.text.TextCharProp;
import com.adobe.xfa.text.TextContext;
import com.adobe.xfa.text.TextDisplay;
import com.adobe.xfa.text.TextEmbed;
import com.adobe.xfa.text.TextFrame;
import com.adobe.xfa.text.TextGfxSource;
import com.adobe.xfa.text.TextPosn;
import com.adobe.xfa.text.TextPosnBase;
import com.adobe.xfa.text.TextStream;
import com.adobe.xfa.ut.UnitSpan;
class RawProcessor {
private final DispLineRaw mpoLine;
private final FormatInfo moFormatInfo;
private final DispMapSpan moAnchor;
private final DispMapSpan moRun;
private TextAttr mpoPendingAttr;
private int mnVisualCharCount;
private boolean mbHasAmbiguousBreaks;
private int[] moAccentRun;
int mnAccentRunSize;
private int meAccentRunBreak;
private int meGlyphOrientation;
RawProcessor(FormatInfo oFormatInfo, DispLineRaw poLine, TextPosnBase oInitialPosition, TextAttr poInitialAttr) {
this.mpoLine = poLine;
this.moFormatInfo = oFormatInfo;
this.moAnchor = new DispMapSpan(poLine, new DispPosn(oInitialPosition));
this.moRun = new DispMapSpan(poLine, new DispRun(poLine.frame(), poInitialAttr));
this.mnVisualCharCount = 0;
this.mbHasAmbiguousBreaks = false;
this.moAccentRun = poLine.display().getContext().getAccentRun(0);
this.meAccentRunBreak = 0;
this.meGlyphOrientation = 0;
}
void processAttr(TextAttr poAttr) {
this.flushAccentRun();
poAttr = this.moFormatInfo.resolveAttr(poAttr);
this.moFormatInfo.setAttr(poAttr);
if (poAttr != null && poAttr.substituteFont()) {
this.mpoLine.display().setFontSubstitution(true);
}
DispRun oNewRun = new DispRun(this.moRun.r(), this.moFormatInfo.getAttr());
this.moRun.reset(oNewRun);
this.mpoPendingAttr = null;
}
void processChar(int c, int eBreak, TextPosnBase poPosition) {
int eGlyphOrientation;
if (this.mpoLine.frame().isOrientationVertical() && this.meGlyphOrientation != (eGlyphOrientation = RawProcessor.figureVerticalOrientationsFromChar(c))) {
this.flushAccentRun();
DispRun oNewRun = new DispRun(this.moRun.r(), eGlyphOrientation);
this.moRun.reset(oNewRun, false);
this.meGlyphOrientation = eGlyphOrientation;
}
if (c == 9) {
DispEmbed poDispEmbed;
this.syncPosition(poPosition);
DispTab poTab = new DispTab();
TextAttr poAttr = this.moRun.r().getAttr();
if (poAttr != null) {
poTab.setHeight(poAttr.size());
}
if ((poDispEmbed = this.addEmbed(poTab, 9, TextCharProp.defaultSpace)) == null) {
return;
}
DispTab poMappedTab = (DispTab)poDispEmbed.getEmbed();
this.moFormatInfo.addTab(poMappedTab);
} else if (c >= 8234 && c <= 8239) {
this.syncPosition(poPosition);
this.addChar(c, eBreak);
} else {
int newSize;
if (TextCharProp.getBreakClass(eBreak) != 8) {
this.syncPosition(poPosition);
this.meAccentRunBreak = eBreak;
}
if ((newSize = this.mnAccentRunSize + 1) >= this.moAccentRun.length) {
this.moAccentRun = this.mpoLine.display().getContext().getAccentRun(newSize);
}
this.moAccentRun[this.mnAccentRunSize] = c;
this.mnAccentRunSize = newSize;
}
}
void processObject(TextEmbed poEmbed, TextPosnBase poPosition) {
this.syncPosition(poPosition);
this.addEmbed(poEmbed);
}
void processPara(TextPosnBase poPosition) {
this.syncPosition(poPosition);
this.addChar(32, TextCharProp.defaultSpace);
this.mpoPendingAttr = null;
this.moFormatInfo.setNewPara(true);
}
void processStreamStart(TextPosnBase oPosition) {
this.processStreamChange(oPosition);
}
void processStreamEnd(TextPosnBase oPosition) {
this.processStreamChange(oPosition);
}
void finish(boolean bIsLastParaLine, boolean bIsLastLineInStream) {
this.flushAccentRun();
this.moAnchor.flush();
this.moRun.flush();
if (bIsLastParaLine) {
this.mpoLine.setLastParaLine(3);
} else {
this.mpoLine.setLastParaLine(2);
}
this.mpoLine.setLastLineInStream(bIsLastLineInStream);
this.mpoLine.setVisualCharCount(this.mnVisualCharCount);
if (this.mbHasAmbiguousBreaks) {
int eDefault = this.mpoLine.display().isIdeographic() ? 15 : 1;
int ePrev = 0;
int eNext = 0;
int nNextKnown = 0;
for (int i = 0; i < this.mpoLine.getCharCount(); ++i) {
int nData = this.mpoLine.getBreakData(i);
int eBreak = TextCharProp.getBreakClass(nData);
int eWidth = TextCharProp.getWidthClass(nData);
if (eBreak == 0) {
eBreak = TextCharProp.resolveBreakWidth(eWidth, eWidth);
}
if (eBreak == 0) {
if (nNextKnown <= i) {
eNext = 0;
for (nNextKnown = i + 1; eNext == 0 && nNextKnown < this.mpoLine.getCharCount(); ++nNextKnown) {
int eNextWidth = this.mpoLine.getWidthClass(nNextKnown);
if (TextCharProp.isAmbiguousWidth(eNextWidth)) continue;
eNext = eWidth;
break;
}
}
eBreak = TextCharProp.resolveBreakWidth(ePrev, eNext, eDefault);
this.mpoLine.setBreak(i, TextCharProp.setBreakClass(nData, eBreak));
if (eBreak == 15) {
ePrev = 320;
continue;
}
ePrev = 192;
continue;
}
if (TextCharProp.isAmbiguousWidth(eWidth)) continue;
ePrev = eWidth;
}
}
}
private void processStreamChange(TextPosnBase oPosition) {
this.flushAccentRun();
this.moAnchor.reset(new DispPosn(oPosition), true);
TextAttr poAttr = this.moAnchor.pp().attributePtr();
if (poAttr == null || poAttr.fontInstance() == null) {
TextAttr poRunAttr = this.moRun.r().getAttr();
if (poRunAttr == null) {
poRunAttr = poAttr;
}
assert (poRunAttr != null);
TextAttr poNewAttr = new TextAttr(true);
poNewAttr.fontService(poRunAttr.fontService());
poNewAttr.override(poRunAttr);
poAttr = poNewAttr;
}
this.moFormatInfo.setAttr(poAttr);
this.moRun.reset(new DispRun(this.moRun.r(), this.moFormatInfo.getAttr()));
this.mpoPendingAttr = null;
}
private void syncPosition(TextPosnBase poPosition) {
this.flushAccentRun();
if (poPosition != null && (this.moAnchor.pp().stream() != poPosition.stream() || this.moAnchor.pp().index() + this.moAnchor.length() != poPosition.index())) {
this.moAnchor.reset(new DispPosn(poPosition));
}
}
private DispEmbed addEmbed(TextEmbed poEmbed, int cInsert, int eBreak) {
this.moRun.flush();
int nCharsBefore = this.mpoLine.getCharCount();
if (cInsert == 9) {
assert (this.mnAccentRunSize == 0);
if (this.moAccentRun == null || this.moAccentRun.length < 1) {
this.moAccentRun = this.mpoLine.display().getContext().getAccentRun(1);
}
this.moAccentRun[0] = cInsert;
this.mnAccentRunSize = 1;
this.resolveAccentRunFont();
this.mnAccentRunSize = 0;
}
this.addChar(cInsert, eBreak);
DispEmbed oDispEmbed = new DispEmbed(poEmbed);
this.mpoLine.add(oDispEmbed, nCharsBefore);
this.moRun.flush();
return oDispEmbed;
}
private DispEmbed addEmbed(TextEmbed poEmbed) {
return this.addEmbed(poEmbed, 65532, TextCharProp.defaultObject);
}
private void flushAccentRun() {
if (this.mnAccentRunSize == 0) {
return;
}
int nComposed = this.resolveAccentRunFont();
int c = this.moAccentRun[0];
boolean bIsArabic = false;
boolean bIsThai = false;
if (c >= 1536 && c <= 1871 || c >= 1920 && c <= 1983 || c >= 64336 && c <= 65023 || c >= 65136 && c <= 65279) {
bIsArabic = true;
} else if (c >= 3584 && c <= 3711) {
bIsThai = true;
}
if (this.mpoLine.getCombCells() > 0) {
if (bIsArabic) {
DispRun oNewRun = new DispRun(this.moRun.r());
oNewRun.setComb(true);
this.moRun.reset(oNewRun);
}
} else {
boolean bAllowLigatures = bIsArabic;
boolean bAllowKerning = bIsThai;
TextAttr poAttr = this.moRun.r().getAttr();
if (poAttr != null) {
if (poAttr.ligatureEnable() && poAttr.ligature() == 1) {
bAllowLigatures = true;
}
if (poAttr.kerningEnable() && poAttr.kerning()) {
bAllowKerning = true;
}
}
if (bAllowLigatures != this.moRun.r().allowLigatures() || bAllowKerning != this.moRun.r().allowKerning()) {
DispRun oNewRun = new DispRun(this.moRun.r());
oNewRun.setAllowLigatures(bAllowLigatures);
oNewRun.setAllowKerning(bAllowKerning);
this.moRun.reset(oNewRun);
}
}
if (nComposed > 1) {
DispPosn p = this.moAnchor.p();
TextPosn pp = p.pp();
int nAnchorStreamCount = p.getStreamCount() == 0 ? this.moAnchor.length() : p.getStreamCount();
DispPosn oNewPosn = new DispPosn(pp.stream(), pp.index() + nAnchorStreamCount, pp.position());
oNewPosn.setStreamCount(nComposed);
this.moAnchor.reset(oNewPosn);
}
this.addChar(c, this.meAccentRunBreak);
int eClass = TextCharProp.getBreakClass(this.meAccentRunBreak);
if (eClass != 31) {
if (eClass == 0) {
this.mbHasAmbiguousBreaks = true;
}
this.mnVisualCharCount = this.mpoLine.getCharCount();
}
for (int nAccentIndex = 1; nAccentIndex < this.mnAccentRunSize; ++nAccentIndex) {
int cAccent = this.moAccentRun[nAccentIndex];
if (cAccent != 0) {
this.addChar(cAccent, TextCharProp.getCharProperty(cAccent));
}
this.mnVisualCharCount = this.mpoLine.getCharCount();
}
this.mnAccentRunSize = 0;
this.moFormatInfo.setNewPara(false);
}
private int resolveAccentRunFont() {
int nComposed = 1;
int c = this.substituteInvisibleCharacter(this.moAccentRun[0]);
if (c != this.moAccentRun[0]) {
this.moAccentRun[0] = c;
this.meAccentRunBreak = TextCharProp.getCharProperty(c);
}
if (c == 8203 || c == 8204 || c == 8205 || c == 65279 || c == 65532) {
return 1;
}
if (c == 9) {
c = 32;
}
ValidationData validation = new ValidationData();
int nSplit = 1;
TextAttr poAttr = this.moRun.r().getAttr();
boolean bInvisible = false;
if (poAttr != null && poAttr.invisibleEnable() && poAttr.invisible()) {
bInvisible = true;
}
if (this.mnAccentRunSize == 1 || bInvisible) {
this.validateBaseChar(c, validation);
if (validation.fontInstance == null) {
return 1;
}
} else {
Composition oComposition = new Composition(this.moAccentRun);
oComposition.reconcile();
do {
c = oComposition.getPrecomposedChar();
this.validateBaseChar(c, validation);
if (validation.fontInstance == null) break;
if (oComposition.getSplit() == 1) {
return 1;
}
oComposition.popCombiningMark();
} while (true);
nSplit = oComposition.getSplit();
if (oComposition.doComposition()) {
assert (nSplit > 1);
nComposed = nSplit;
for (int i = 1; i < nSplit; ++i) {
this.moAccentRun[i] = this.moAccentRun[nSplit + i - 1];
}
this.mnAccentRunSize -= nSplit - 1;
this.moAccentRun[0] = c;
this.meAccentRunBreak = TextCharProp.getCharProperty(c);
nSplit = 1;
}
}
for (int nAccentIndex = nSplit; nAccentIndex < this.mnAccentRunSize; ++nAccentIndex) {
int cAccent;
FontInstance oTestInstance;
this.moAccentRun[nAccentIndex] = cAccent = this.substituteInvisibleCharacter(this.moAccentRun[nAccentIndex]);
if (cAccent == 0 || this.validateChar(validation.fontInstance, cAccent) || (oTestInstance = this.reconcileFont(validation.fontInstance, cAccent)) == null || oTestInstance == validation.fontInstance) continue;
int i = 0;
if (this.validateChar(oTestInstance, c)) {
for (i = nSplit; i < nAccentIndex && this.validateChar(oTestInstance, this.moAccentRun[i]); ++i) {
}
}
if (i < nAccentIndex) continue;
validation.fontInstance = oTestInstance;
validation.replaceFont = true;
}
if (validation.replaceFont) {
FontItem poOriginalItem;
TextAttr poOverrideAttr = new TextAttr(validation.originalAttr);
boolean bSubstitute = false;
FontItem poFontItem = validation.fontInstance.getFontItem();
if (!poFontItem.equals(poOriginalItem = validation.originalFontInstance.getFontItem())) {
bSubstitute = true;
}
poOverrideAttr.fontInstance(validation.fontInstance, poOriginalItem.getTypeface());
if (bSubstitute) {
this.mpoLine.display().setFontSubstitution(true);
}
this.mpoPendingAttr = validation.originalAttr;
DispRun oNewRun = new DispRun(this.moRun.r(), poOverrideAttr);
this.moRun.reset(oNewRun);
}
return nComposed;
}
private void validateBaseChar(int c, ValidationData validation) {
TextAttr poAttr;
boolean bValidated = false;
if (this.mpoPendingAttr == null) {
validation.originalAttr = this.moRun.r().getAttr();
} else {
validation.originalAttr = this.mpoPendingAttr;
if (this.validateChar(this.mpoPendingAttr.fontInstance(), c)) {
DispRun oNewRun = new DispRun(this.moRun.r(), this.mpoPendingAttr);
this.moRun.reset(oNewRun);
this.mpoPendingAttr = null;
bValidated = true;
}
}
if (!bValidated) {
poAttr = this.moRun.r().getAttr();
if (poAttr == null) {
poAttr = validation.originalAttr;
}
if (poAttr != null) {
bValidated = this.validateChar(poAttr.fontInstance(), c);
}
}
if (bValidated) {
poAttr = this.moRun.r().getAttr();
if (poAttr != null) {
validation.fontInstance = validation.originalFontInstance = poAttr.fontInstance();
}
} else {
assert (validation.originalAttr != null);
validation.originalFontInstance = validation.originalAttr.fontInstance();
validation.fontInstance = this.reconcileFont(validation.originalFontInstance, c);
validation.replaceFont = true;
}
}
private boolean validateChar(FontInstance oFontInstance, int c) {
FontItem poFontItem;
if (oFontInstance != null && (poFontItem = oFontInstance.getFontItem()) != null && poFontItem.validateChar(c, GFXGlyphOrientation.usesHorizontalGlyphs(this.meGlyphOrientation))) {
return true;
}
return false;
}
private FontInstance reconcileFont(FontInstance oFontInstance, int c) {
TextAttr poAttr = this.moRun.r().getAttr();
if (poAttr == null) {
return null;
}
return poAttr.gfxSource().reconcileFont(oFontInstance, c);
}
private int substituteInvisibleCharacter(int c) {
TextAttr poAttr = this.moRun.r().getAttr();
if (poAttr != null && poAttr.invisibleEnable() && poAttr.invisible() && poAttr.invisCharEnable() && poAttr.invisChar() != '\u0000') {
c = poAttr.invisChar();
}
return c;
}
private void addChar(int c, int eBreak) {
this.mpoLine.addChar(c, eBreak);
}
private static boolean charIsRomanRotateable(int character) {
return character >= 65 && character < 90 || character >= 97 && character < 122 || character >= 64256 && character < 64263 || character >= 65377 && character <= 65439;
}
private static int figureVerticalOrientationsFromChar(int c) {
return RawProcessor.charIsRomanRotateable(c) ? 2 : 1;
}
private static class ValidationData {
TextAttr originalAttr;
FontInstance originalFontInstance;
FontInstance fontInstance;
boolean replaceFont;
private ValidationData() {
}
}
}