forked from louy/Javascript-Arabic-Reshaper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
285 lines (256 loc) · 10 KB
/
index.js
File metadata and controls
285 lines (256 loc) · 10 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
/**
* Javascript Arabic Reshaper by Louy Alakkad
* https://github.com/louy/Javascript-Arabic-Reshaper
* Based on (http://git.io/vsnAd)
*/
(function(root, factory) {
var name = 'ArabicReshaper';
/* global define, module */
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
// Browser globals
root[name] = factory();
}
}(this, function() {
var charsMap = [
/* code,isolated,initial, medial, final */
[0x0621, 0xFE80, null, null, null], /* HAMZA */
[0x0622, 0xFE81, null, null, 0xFE82], /* ALEF_MADDA */
[0x0623, 0xFE83, null, null, 0xFE84], /* ALEF_HAMZA_ABOVE */
[0x0624, 0xFE85, null, null, 0xFE86], /* WAW_HAMZA */
[0x0625, 0xFE87, null, null, 0xFE88], /* ALEF_HAMZA_BELOW */
[0x0626, 0xFE89, 0xFE8B, 0xFE8C, 0xFE8A], /* YEH_HAMZA */
[0x0627, 0xFE8D, null, null, 0xFE8E], /* ALEF */
[0x0628, 0xFE8F, 0xFE91, 0xFE92, 0xFE90], /* BEH */
[0x0629, 0xFE93, null, null, 0xFE94], /* TEH_MARBUTA */
[0x062A, 0xFE95, 0xFE97, 0xFE98, 0xFE96], /* TEH */
[0x062B, 0xFE99, 0xFE9B, 0xFE9C, 0xFE9A], /* THEH */
[0x062C, 0xFE9D, 0xFE9F, 0xFEA0, 0xFE9E], /* JEEM */
[0x062D, 0xFEA1, 0xFEA3, 0xFEA4, 0xFEA2], /* HAH */
[0x062E, 0xFEA5, 0xFEA7, 0xFEA8, 0xFEA6], /* KHAH */
[0x062F, 0xFEA9, null, null, 0xFEAA], /* DAL */
[0x0630, 0xFEAB, null, null, 0xFEAC], /* THAL */
[0x0631, 0xFEAD, null, null, 0xFEAE], /* REH */
[0x0632, 0xFEAF, null, null, 0xFEB0], /* ZAIN */
[0x0633, 0xFEB1, 0xFEB3, 0xFEB4, 0xFEB2], /* SEEN */
[0x0634, 0xFEB5, 0xFEB7, 0xFEB8, 0xFEB6], /* SHEEN */
[0x0635, 0xFEB9, 0xFEBB, 0xFEBC, 0xFEBA], /* SAD */
[0x0636, 0xFEBD, 0xFEBF, 0xFEC0, 0xFEBE], /* DAD */
[0x0637, 0xFEC1, 0xFEC3, 0xFEC4, 0xFEC2], /* TAH */
[0x0638, 0xFEC5, 0xFEC7, 0xFEC8, 0xFEC6], /* ZAH */
[0x0639, 0xFEC9, 0xFECB, 0xFECC, 0xFECA], /* AIN */
[0x063A, 0xFECD, 0xFECF, 0xFED0, 0xFECE], /* GHAIN */
[0x0640, 0x0640, 0x0640, 0x0640, 0x0640], /* TATWEEL */
[0x0641, 0xFED1, 0xFED3, 0xFED4, 0xFED2], /* FEH */
[0x0642, 0xFED5, 0xFED7, 0xFED8, 0xFED6], /* QAF */
[0x0643, 0xFED9, 0xFEDB, 0xFEDC, 0xFEDA], /* KAF */
[0x0644, 0xFEDD, 0xFEDF, 0xFEE0, 0xFEDE], /* LAM */
[0x0645, 0xFEE1, 0xFEE3, 0xFEE4, 0xFEE2], /* MEEM */
[0x0646, 0xFEE5, 0xFEE7, 0xFEE8, 0xFEE6], /* NOON */
[0x0647, 0xFEE9, 0xFEEB, 0xFEEC, 0xFEEA], /* HEH */
[0x0648, 0xFEED, null, null, 0xFEEE], /* WAW */
[0x0649, 0xFEEF, null, null, 0xFEF0], /* ALEF_MAKSURA */
[0x064A, 0xFEF1, 0xFEF3, 0xFEF4, 0xFEF2], /* YEH */
[0x067E, 0xFB56, 0xFB58, 0xFB59, 0xFB57], /* PEH */
[0x06CC, 0xFBFC, 0xFBFE, 0xFBFF, 0xFBFD], /* Farsi Yeh */
[0x0686, 0xFB7A, 0xFB7C, 0xFB7D, 0xFB7B], /* Tcheh */
[0x06A9, 0xFB8E, 0xFB90, 0xFB91, 0xFB8F], /* Keheh */
[0x06AF, 0xFB92, 0xFB94, 0xFB95, 0xFB93], /* Gaf */
[0x0698, 0xFB8A, null, null, 0xFB8B], /* Jeh */
],
combCharsMap = [
[[0x0644, 0x0622], 0xFEF5, null, null, 0xFEF6], /* LAM_ALEF_MADDA */
[[0x0644, 0x0623], 0xFEF7, null, null, 0xFEF8], /* LAM_ALEF_HAMZA_ABOVE */
[[0x0644, 0x0625], 0xFEF9, null, null, 0xFEFA], /* LAM_ALEF_HAMZA_BELOW */
[[0x0644, 0x0627], 0xFEFB, null, null, 0xFEFC], /* LAM_ALEF */
],
transChars = [
0x0610, /* ARABIC SIGN SALLALLAHOU ALAYHE WASSALLAM */
0x0612, /* ARABIC SIGN ALAYHE ASSALLAM */
0x0613, /* ARABIC SIGN RADI ALLAHOU ANHU */
0x0614, /* ARABIC SIGN TAKHALLUS */
0x0615, /* ARABIC SMALL HIGH TAH */
0x064B, /* ARABIC FATHATAN */
0x064C, /* ARABIC DAMMATAN */
0x064D, /* ARABIC KASRATAN */
0x064E, /* ARABIC FATHA */
0x064F, /* ARABIC DAMMA */
0x0650, /* ARABIC KASRA */
0x0651, /* ARABIC SHADDA */
0x0652, /* ARABIC SUKUN */
0x0653, /* ARABIC MADDAH ABOVE */
0x0654, /* ARABIC HAMZA ABOVE */
0x0655, /* ARABIC HAMZA BELOW */
0x0656, /* ARABIC SUBSCRIPT ALEF */
0x0657, /* ARABIC INVERTED DAMMA */
0x0658, /* ARABIC MARK NOON GHUNNA */
0x0670, /* ARABIC LETTER SUPERSCRIPT ALEF */
0x06D6, /* ARABIC SMALL HIGH LIGATURE SAD WITH LAM WITH ALEF MAKSURA */
0x06D7, /* ARABIC SMALL HIGH LIGATURE QAF WITH LAM WITH ALEF MAKSURA */
0x06D8, /* ARABIC SMALL HIGH MEEM INITIAL FORM */
0x06D9, /* ARABIC SMALL HIGH LAM ALEF */
0x06DA, /* ARABIC SMALL HIGH JEEM */
0x06DB, /* ARABIC SMALL HIGH THREE DOTS */
0x06DC, /* ARABIC SMALL HIGH SEEN */
0x06DF, /* ARABIC SMALL HIGH ROUNDED ZERO */
0x06E0, /* ARABIC SMALL HIGH UPRIGHT RECTANGULAR ZERO */
0x06E1, /* ARABIC SMALL HIGH DOTLESS HEAD OF KHAH */
0x06E2, /* ARABIC SMALL HIGH MEEM ISOLATED FORM */
0x06E3, /* ARABIC SMALL LOW SEEN */
0x06E4, /* ARABIC SMALL HIGH MADDA */
0x06E7, /* ARABIC SMALL HIGH YEH */
0x06E8, /* ARABIC SMALL HIGH NOON */
0x06EA, /* ARABIC EMPTY CENTRE LOW STOP */
0x06EB, /* ARABIC EMPTY CENTRE HIGH STOP */
0x06EC, /* ARABIC ROUNDED HIGH STOP WITH FILLED CENTRE */
0x06ED, /* ARABIC SMALL LOW MEEM */
];
function characterMapContains(c) {
for (var i = 0; i < charsMap.length; ++i) {
if (charsMap[ i ][0] === c) {
return true;
}
}
return false;
}
function getCharRep(c) {
for (var i = 0; i < charsMap.length; ++i) {
if (charsMap[ i ][0] === c) {
return charsMap[i];
}
}
return false;
}
function getCombCharRep(c1, c2) {
for (var i = 0; i < combCharsMap.length; ++i) {
if (combCharsMap[i][0][0] === c1 && combCharsMap[i][0][1] === c2) {
return combCharsMap[i];
}
}
// We should never reach here...
// istanbul ignore next
return false;
}
function isTransparent(c) {
for (var i = 0; i < transChars.length; ++i) {
if (transChars[i] === c) {
return true;
}
}
return false;
}
function getOriginalCharsFromCode(code) {
var j;
for (j = 0; j < charsMap.length; ++j) {
if (charsMap[j].indexOf(code) > -1) {
return String.fromCharCode(charsMap[j][0]);
}
}
for (j = 0; j < combCharsMap.length; ++j) {
if (combCharsMap[j].indexOf(code) > -1) {
return String.fromCharCode(combCharsMap[j][0][0]) +
String.fromCharCode(combCharsMap[j][0][1]);
}
}
return String.fromCharCode(code);
}
return {
convertArabic: function(normal, optUseOriginalInsteadOfIsolated) {
var crep,
combcrep,
shaped = '';
for (var i = 0; i < normal.length; ++i) {
var current = normal.charCodeAt(i);
if (characterMapContains(current)) {
var prev = null,
next = null,
prevID = i - 1,
nextID = i + 1;
/*
Transparent characters have no effect in the shaping process.
So, ignore all the transparent characters that are BEFORE the
current character.
*/
for (; prevID >= 0; --prevID) {
if (!isTransparent(normal.charCodeAt(prevID))) {
break;
}
}
prev = (prevID >= 0) ? normal.charCodeAt(prevID) : null;
crep = prev ? getCharRep(prev) : false;
if (!crep || crep[2] == null && crep[3] == null) {
prev = null; // prev character doesn’t connect with its successor
}
/*
Transparent characters have no effect in the shaping process.
So, ignore all the transparent characters that are AFTER the
current character.
*/
for (; nextID < normal.length; ++nextID) {
if (!isTransparent(normal.charCodeAt(nextID))) {
break;
}
}
next = (nextID < normal.length) ? normal.charCodeAt(nextID) : null;
crep = next ? getCharRep(next) : false;
if (!crep || crep[3] == null && crep[4] == null) {
next = null; // next character doesn’t connect with its predecessor
}
/* Combinations */
if (current === 0x0644 && next != null &&
(next === 0x0622 || next === 0x0623 || next === 0x0625 || next === 0x0627)) {
combcrep = getCombCharRep(current, next);
if (prev != null) {
shaped += String.fromCharCode(combcrep[4]);
} else {
shaped += String.fromCharCode(combcrep[1]);
}
++i;
continue;
}
crep = getCharRep(current);
/* Medial */
if (prev != null && next != null && crep[3] != null) {
shaped += String.fromCharCode(crep[3]);
continue;
} else /* Final */
if (prev != null && crep[4] != null) {
shaped += String.fromCharCode(crep[4]);
continue;
} else /* Initial */
if (next != null && crep[2] != null) {
shaped += String.fromCharCode(crep[2]);
continue;
} else /* Isolated */ {
// A lot of Arabic font files are missing glyphs in the isolated block
// this allows the shaper to just use the original characters instead
// which should not mess up the shaping process.
shaped += optUseOriginalInsteadOfIsolated
? String.fromCharCode(crep[0])
: String.fromCharCode(crep[1]);
}
} else {
shaped += String.fromCharCode(current);
}
}
return shaped;
},
// convert from Arabic Presentation Forms B
convertArabicBack: function(apfb) {
var toReturn = '',
selectedChar;
var i;
theLoop:
for (i = 0; i < apfb.length; ++i) {
selectedChar = apfb.charCodeAt(i);
toReturn += getOriginalCharsFromCode(selectedChar);
}
return toReturn;
},
};
}));