ArabicFormatter.java
10.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.agl.lang.UCharacter
* com.adobe.agl.util.ULocale
*/
package com.adobe.fontengine.inlineformatting.infontformatting;
import com.adobe.agl.lang.UCharacter;
import com.adobe.agl.util.ULocale;
import com.adobe.fontengine.CharUtil;
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.opentype.Gdef;
import com.adobe.fontengine.font.opentype.Gpos;
import com.adobe.fontengine.font.opentype.Gsub;
import com.adobe.fontengine.font.opentype.OTSelector;
import com.adobe.fontengine.font.opentype.OTSelectorOnElementAttribute;
import com.adobe.fontengine.font.opentype.OTSelectors;
import com.adobe.fontengine.font.opentype.OpenTypeFont;
import com.adobe.fontengine.font.opentype.Tag;
import com.adobe.fontengine.inlineformatting.AttributedRun;
import com.adobe.fontengine.inlineformatting.ElementAttribute;
import com.adobe.fontengine.inlineformatting.InterElementAttribute;
import com.adobe.fontengine.inlineformatting.LigatureLevel;
import com.adobe.fontengine.inlineformatting.infontformatting.BaseFormatter;
import com.adobe.fontengine.inlineformatting.infontformatting.InFontFormatter;
import com.adobe.fontengine.inlineformatting.infontformatting.LookupsCache;
final class ArabicFormatter
extends BaseFormatter {
private static final ElementAttribute joiningShape = new ElementAttribute("joiningShape");
private static final OTSelector initSelector = new OTSelectorOnElementAttribute(joiningShape, JoiningShapes.init, true);
private static final OTSelector mediSelector = new OTSelectorOnElementAttribute(joiningShape, JoiningShapes.medi, true);
private static final OTSelector finaSelector = new OTSelectorOnElementAttribute(joiningShape, JoiningShapes.fina, true);
private static final OTSelector isolSelector = new OTSelectorOnElementAttribute(joiningShape, JoiningShapes.isol, true);
private static final int[] gsubFeatures = new int[]{Tag.feature_ccmp, Tag.feature_locl, Tag.feature_init, Tag.feature_medi, Tag.feature_fina, Tag.feature_isol, Tag.feature_rtla, Tag.feature_rlig, Tag.feature_liga};
private static final int[] gposFeatures = new int[]{Tag.feature_kern, Tag.feature_mark, Tag.feature_mkmk};
ArabicFormatter() {
}
protected void setJoiningShape(AttributedRun run, int start, int limit) {
int previousIndex = Integer.MIN_VALUE;
boolean previousJoinsItsRight = false;
int previousType = 0;
for (int i = start; i < limit + 1; ++i) {
JoiningShapes previousShape;
int thisType = i == limit ? 0 : (run.getElementStyle(i, ElementAttribute.isGlyph) != Boolean.TRUE ? UCharacter.getIntPropertyValue((int)run.elementAt(i), (int)4103) : (Integer)run.getElementStyle(i, ElementAttribute.joiningType));
if (thisType == 5) continue;
if (!(thisType != 1 && thisType != 2 && thisType != 4 || previousType != 1 && previousType != 2 && previousType != 3)) {
previousShape = previousJoinsItsRight ? JoiningShapes.medi : JoiningShapes.init;
previousJoinsItsRight = true;
} else {
previousShape = previousJoinsItsRight ? JoiningShapes.fina : JoiningShapes.isol;
previousJoinsItsRight = false;
}
if (previousIndex != Integer.MIN_VALUE && previousType != 1 && previousType != 0) {
run.setElementStyle(previousIndex, joiningShape, previousShape);
}
previousIndex = i;
previousType = thisType;
}
}
public int firstPass(AttributedRun run, int start, int limit) {
while (start < limit) {
int bidiRunLimit = run.getSubrunLimit(start, limit, ElementAttribute.bidiLevel);
this.setJoiningShape(run, start, bidiRunLimit);
start = bidiRunLimit;
}
return super.firstPass(run, start, limit);
}
protected int setGlyphs(AttributedRun run, int start, int limit) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
Font font = (Font)run.getElementStyle(start, ElementAttribute.font);
OpenTypeFont otFont = (OpenTypeFont)((FontImpl)font).getFontData();
while (start < limit) {
int graphemeLimit;
int i;
int usvc;
int gid;
if (run.getElementStyle(start, ElementAttribute.isGlyph) == Boolean.TRUE) {
++start;
continue;
}
int usv = run.elementAt(start);
if (usv == 8205 || usv == 8204) {
run.remove(start);
--limit;
run.setInterElementStyleBefore(start, InterElementAttribute.ligatureLevel, LigatureLevel.NONE);
continue;
}
if (CharUtil.isControl(usv)) {
run.remove(start);
--limit;
continue;
}
if ((Integer)run.getElementStyle(start, ElementAttribute.bidiLevel) % 2 == 1) {
usv = UCharacter.getMirror((int)usv);
}
if (!CharUtil.isBase(usv) || start + 1 == limit || run.getElementStyle(start + 1, ElementAttribute.isGlyph) == Boolean.TRUE || !CharUtil.isCombining(run.elementAt(start + 1))) {
int gid2 = otFont.getGlyphForChar(usv);
run.replace(start, gid2);
run.setElementStyle(start, ElementAttribute.isGlyph, Boolean.TRUE);
++start;
continue;
}
for (graphemeLimit = start + 1; graphemeLimit < limit && run.getElementStyle(graphemeLimit, ElementAttribute.isGlyph) != Boolean.TRUE && CharUtil.isCombining(run.elementAt(graphemeLimit)); ++graphemeLimit) {
}
boolean directMappingHasNotdef = false;
int[] usvs = new int[graphemeLimit - start];
int[] gids = new int[graphemeLimit - start];
for (i = start; i < graphemeLimit; ++i) {
usvs[i - start] = run.elementAt(i);
gid = otFont.getGlyphForChar(usvs[i - start]);
if (gid == 0) {
directMappingHasNotdef = true;
}
gids[i - start] = gid;
}
if (directMappingHasNotdef && (usvc = CharUtil.compose(usvs, 0, graphemeLimit - start)) != -1 && (gid = otFont.getGlyphForChar(usvc)) != 0) {
run.replace(start, graphemeLimit, gid);
run.setElementStyle(start, ElementAttribute.isGlyph, Boolean.TRUE);
limit -= graphemeLimit - start - 1;
++start;
continue;
}
for (i = start; i < graphemeLimit; ++i) {
run.replace(i, gids[i - start]);
run.setElementStyle(start, ElementAttribute.isGlyph, Boolean.TRUE);
}
start = graphemeLimit;
}
return limit;
}
protected boolean canFormatOT() {
return true;
}
protected int formatOT(OpenTypeFont otFont, AttributedRun run, int start, int limit, boolean shouldKern) throws InvalidFontException, UnsupportedFontException, FontLoadingException {
Integer bidiLevel = (Integer)run.getElementStyle(start, ElementAttribute.bidiLevel);
int scriptTag = ArabicFormatter.getOTScriptTag((Integer)run.getElementStyle(start, InFontFormatter.scriptAttribute));
int langTag = ArabicFormatter.getOTLanguageTag((ULocale)run.getElementStyle(start, ElementAttribute.locale));
limit = this.setGlyphs(run, start, limit);
if (otFont.gsub != null) {
int[][] gsubLookups = LookupsCache.resolveFeatureTag(otFont.gsub, scriptTag, langTag, gsubFeatures);
limit = otFont.gsub.applyLookups(gsubLookups[0], run, start, limit, OTSelectors.everywhere, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[1], run, start, limit, OTSelectors.everywhere, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[2], run, start, limit, initSelector, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[3], run, start, limit, mediSelector, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[4], run, start, limit, finaSelector, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[5], run, start, limit, isolSelector, otFont.gdef);
if (bidiLevel % 2 == 1) {
limit = otFont.gsub.applyLookups(gsubLookups[6], run, start, limit, OTSelectors.everywhere, otFont.gdef);
}
limit = otFont.gsub.applyLookups(gsubLookups[7], run, start, limit, OTSelectors.minimumLigatures, otFont.gdef);
limit = otFont.gsub.applyLookups(gsubLookups[8], run, start, limit, OTSelectors.commonLigatures, otFont.gdef);
}
this.posFromAdvanceWidth(run, otFont, start, limit);
if (otFont.gpos != null) {
int[][] gposLookups = LookupsCache.resolveFeatureTag(otFont.gpos, scriptTag, langTag, gposFeatures);
if (shouldKern) {
if (gposLookups[0].length != 0) {
limit = otFont.gpos.applyLookups(gposLookups[0], run, start, limit, OTSelectors.everywhere, otFont.gdef);
} else {
this.applyKernTable(otFont, run, start, limit);
}
}
limit = otFont.gpos.applyLookups(gposLookups[1], run, start, limit, OTSelectors.everywhere, otFont.gdef);
limit = otFont.gpos.applyLookups(gposLookups[2], run, start, limit, OTSelectors.everywhere, otFont.gdef);
} else {
this.applyKernTable(otFont, run, start, limit);
}
return limit;
}
private static class JoiningShapes {
private String name;
public static final JoiningShapes init = new JoiningShapes("init");
public static final JoiningShapes isol = new JoiningShapes("isol");
public static final JoiningShapes medi = new JoiningShapes("medi");
public static final JoiningShapes fina = new JoiningShapes("fina");
private JoiningShapes(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
}