Debian Codegraph @

dojox/string/BidiEngine.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Stateful"],
2 function(lang,declare,Stateful) {
3 lang.getObject("string", true, dojox);
4
5 var BidiEngine = declare("dojox.string.BidiEngine", Stateful, {
6 // summary:
7 // This class provides a bidi transformation engine, i.e.
8 // functions for reordering and shaping bidi text.
9 // description:
10 // Bidi stands for support for languages with a bidirectional script.
11 //
12 // Usually Unicode Bidi Algorithm used by OS platform (and web browsers) is capable of properly
13 // transforming Bidi text and as a result it is adequately displayed on the screen.
14 // However, in some situations, Unicode Bidi Algorithm is not invoked or is not properly applied.
15 // This may occur in situation in which software responsible for rendering the text is not leveraging
16 // Unicode Bidi Algorithm implemented by OS (e.g. GFX renderers).
17 //
18 // Bidi engine provided in this class implements Unicode Bidi Algorithm as specified at
19 // http://www.unicode.org/reports/tr9/.
20 //
21 // For more information on basic Bidi concepts please read
22 // "Bidirectional script support - A primer" available from
23 // http://www.ibm.com/developerworks/websphere/library/techarticles/bidi/bidigen.html.
24 //
25 // As of February 2011, Bidi engine has following limitations:
26 //
27 // 1. No support for following numeric shaping options:
28 // - H - Hindi,
29 // - C - Contextual,
30 // - N - Nominal.
31 // 2. No support for following shaping options:
32 // - I - Initial shaping,
33 // - M - Middle shaping,
34 // - F - Final shaping,
35 // - B - Isolated shaping.
36 // 3. No support for LRE/RLE/LRO/RLO/PDF (they are handled like neutrals).
37 // 4. No support for Windows compatibility.
38 // 5. No support for insert/remove marks.
39 // 6. No support for code pages.
40 //
41
42 // Input Bidi layout in which inputText is passed to the function.
43 inputFormat: "ILYNN",
44
45 // Output Bidi layout to which inputText should be transformed.
46 outputFormat: "VLNNN",
47
48 // Array, containing positions of each character from the source text in the resulting text.
49 sourceToTarget: [],
50
51 // Array, containing positions of each character from the resulting text in the source text.
52 targetToSource: [],
53
54 // Array, containing bidi level of each character from the source text
55 levels: [],
56
57 bidiTransform: function (/*String*/text, /*String*/formatIn, /*String*/formatOut) {
58 // summary:
59 // Central public API for Bidi engine. Transforms the text according to formatIn, formatOut
60 // parameters. If formatIn or formatOut parametrs are not valid throws an exception.
61 // inputText:
62 // Input text subject to application of Bidi transformation.
63 // formatIn:
64 // Input Bidi layout in which inputText is passed to the function.
65 // formatOut:
66 // Output Bidi layout to which inputText should be transformed.
67 // description:
68 // Both formatIn and formatOut parameters are 5 letters long strings.
69 // For example - "ILYNN". Each letter is associated with specific attribute of Bidi layout.
70 // Possible and default values for each one of the letters are provided below:
71 //
72 // First letter:
73 //
74 // - Letter position/index:
75 // 1
76 // - Letter meaning:
77 // Ordering Schema.
78 // - Possible values:
79 // - I - Implicit (Logical).
80 // - V - Visual.
81 // - Default value:
82 // I
83 //
84 // Second letter:
85 //
86 // - Letter position/index:
87 // 2
88 // - Letter meaning:
89 // Orientation.
90 // - Possible values:
91 // - L - Left To Right.
92 // - R - Right To Left.
93 // - C - Contextual Left to Right.
94 // - D - Contextual Right to Left.
95 // - Default value:
96 // L
97 //
98 // Third letter:
99 //
100 // - Letter position/index:
101 // 3
102 // - Letter meaning:
103 // Symmetric Swapping.
104 // - Possible values:
105 // - Y - Symmetric swapping is on.
106 // - N - Symmetric swapping is off.
107 // - Default value:
108 // Y
109 //
110 // Fourth letter:
111 //
112 // - Letter position/index:
113 // 4
114 // - Letter meaning:
115 // Shaping.
116 // - Possible values:
117 // - S - Text is shaped.
118 // - N - Text is not shaped.
119 // - Default value:
120 // N
121 //
122 // Fifth letter:
123 //
124 // - Letter position/index:
125 // 5
126 // - Letter meaning:
127 // Numeric Shaping.
128 // - Possible values:
129 // - N - Nominal.
130 // - Default value:
131 // N
132 //
133 // The output of this function is original text (passed via first argument) transformed from
134 // input Bidi layout (second argument) to output Bidi layout (last argument).
135 //
136 // Sample call:
137 // | mytext = bidiTransform("HELLO WORLD", "ILYNN", "VLYNN");
138 // In this case, "HELLO WORLD" text is transformed from Logical - LTR to Visual - LTR Bidi layout
139 // with default values for symmetric swapping (Yes), shaping (Not shaped) and numeric shaping
140 // (Nominal).
141 // returns: String
142 // Original text transformed from input Bidi layout (second argument)
143 // to output Bidi layout (last argument).
144 // Throws an exception if the bidi layout strings are not valid.
145 // tags:
146 // public
147
148 this.sourceToTarget = [];
149 this.targetToSource = [];
150 if (!text) {
151 return "";
152 }
153 initMaps(this.sourceToTarget, this.targetToSource, text.length);
154 if (!this.checkParameters(formatIn, formatOut)) {
155 return text;
156 }
157
158 formatIn = this.inputFormat;
159 formatOut = this.outputFormat;
160 var result = text;
161 var bdx = BDX;
162 var orientIn = getOrientation(formatIn.charAt(1)),
163 orientOut = getOrientation(formatOut.charAt(1)),
164 osIn = (formatIn.charAt(0) === "I") ? "L" : formatIn.charAt(0),
165 osOut = (formatOut.charAt(0) === "I") ? "L" : formatOut.charAt(0),
166 inFormat = osIn + orientIn,
167 outFormat = osOut + orientOut,
168 swap = formatIn.charAt(2) + formatOut.charAt(2);
169
170 bdx.defInFormat = inFormat;
171 bdx.defOutFormat = outFormat;
172 bdx.defSwap = swap;
173
174 var stage1Text = doBidiReorder(text, inFormat, outFormat, swap, bdx),
175 isRtl = false;
176
177 if (formatOut.charAt(1) === "R") {
178 isRtl = true;
179 } else if (formatOut.charAt(1) === "C" || formatOut.charAt(1) === "D") {
180 isRtl = this.checkContextual(stage1Text);
181 }
182
183 this.sourceToTarget = stMap;
184 this.targetToSource = reverseMap(this.sourceToTarget);
185 tsMap = this.targetToSource;
186
187 if (formatIn.charAt(3) === formatOut.charAt(3)) {
188 result = stage1Text;
189 } else if (formatOut.charAt(3) === "S") {
190 result = shape(isRtl, stage1Text, true);
191 } else { //formatOut.charAt(3) === "N"
192 result = deshape(stage1Text, isRtl, true);
193 }
194 this.sourceToTarget = stMap;
195 this.targetToSource = tsMap;
196 this.levels = lvMap;
197 return result;
198 },
199
200 _inputFormatSetter: function (format) {
201 if (!validFormat.test(format)) {
202 throw new Error("dojox/string/BidiEngine: the bidi layout string is wrong!");
203 }
204 this.inputFormat = format;
205 },
206
207 _outputFormatSetter: function (format) {
208 if (!validFormat.test(format)) {
209 throw new Error("dojox/string/BidiEngine: the bidi layout string is wrong!");
210 }
211 this.outputFormat = format;
212 },
213
214 checkParameters: function (/*String*/formatIn, /*String*/formatOut) {
215 // summary:
216 // Checks layout parameters.
217 // formatIn:
218 // Input Bidi layout in which inputText is passed to the function.
219 // formatOut:
220 // Output Bidi layout to which inputText should be transformed.
221 // description:
222 // Checks, that layout parameters are different and contain allowed values.
223 // Allowed values for format string are:
224 // - 1st letter: I, V
225 // - 2nd letter: L, R, C, D
226 // - 3rd letter: Y, N
227 // - 4th letter: S, N
228 // - 5th letter: N
229 // returns: /*Boolean*/
230 // true - if layout parameters are valid.
231 // false - otherwise.
232 // tags:
233 // private
234
235 if (!formatIn) {
236 formatIn = this.inputFormat;
237 } else {
238 this.set("inputFormat", formatIn);
239 }
240 if (!formatOut) {
241 formatOut = this.outputFormat;
242 } else {
243 this.set("outputFormat", formatOut);
244 }
245 if (formatIn === formatOut) {
246 return false;
247 }
248 return true;
249 },
250
251 checkContextual: function (/*String*/text) {
252 // summary:
253 // Determine the base direction of a bidi text according
254 // to its first strong directional character.
255 // text:
256 // The text to check.
257 // returns: /*String*/
258 // "ltr" or "rtl" according to the first strong character.
259 // If there is no strong character, returns the value of the
260 // document dir property.
261 // tags:
262 // public
263 var dir = firstStrongDir(text);
264 if (dir !== "ltr" && dir !== "rtl") {
265 try {
266 dir = document.dir.toLowerCase();
267 } catch (e) {
268 }
269 if (dir !== "ltr" && dir !== "rtl") {
270 dir = "ltr";
271 }
272 }
273 return dir;
274 },
275
276 hasBidiChar: function (/*String*/text) {
277 // summary:
278 // Return true if text contains RTL directed character.
279 // text:
280 // The source string.
281 // description:
282 // Searches for RTL directed character.
283 // Returns true if found, else returns false.
284 // returns: /*Boolean*/
285 // true - if text has a RTL directed character.
286 // false - otherwise.
287 // tags:
288 // public
289
290 return bidiChars.test(text);
291 }
292 });
293
294 function doBidiReorder(/*String*/text, /*String*/inFormat,
295 /*String*/outFormat, /*String*/swap, /*Object*/bdx) {
296 // summary:
297 // Reorder the source text according to the bidi attributes
298 // of source and result.
299 // text:
300 // The text to reorder.
301 // inFormat:
302 // Ordering scheme and base direction of the source text.
303 // Can be "LLTR", "LRTL", "LCLR", "LCRL", "VLTR", "VRTL",
304 // "VCLR", "VCRL".
305 // The first letter is "L" for logical ordering scheme,
306 // "V" for visual ordering scheme.
307 // The other letters specify the base direction.
308 // "CLR" means contextual direction defaulting to LTR if
309 // there is no strong letter.
310 // "CRL" means contextual direction defaulting to RTL if
311 // there is no strong letter.
312 // The initial value is "LLTR", if none, the initial value is used.
313 // outFormat:
314 // Required ordering scheme and base direction of the
315 // result. Has the same format as inFormat.
316 // If none, the initial value "VLTR" is used.
317 // swap:
318 // Symmetric swapping attributes of source and result.
319 // The allowed values can be "YN", "NY", "YY" and "NN".
320 // The first letter reflects the symmetric swapping attribute
321 // of the source, the second letter that of the result.
322 // bdx: Object
323 // Used for intermediate data storage
324 // returns:
325 // Text reordered according to source and result attributes.
326
327 var params = prepareReorderingParameters(text, {inFormat: inFormat, outFormat: outFormat, swap: swap}, bdx);
328 if (params.inFormat === params.outFormat) {
329 return text;
330 }
331 inFormat = params.inFormat;
332 outFormat = params.outFormat;
333 swap = params.swap;
334 var inOrdering = inFormat.substring(0, 1),
335 inOrientation = inFormat.substring(1, 4),
336 outOrdering = outFormat.substring(0, 1),
337 outOrientation = outFormat.substring(1, 4);
338 bdx.inFormat = inFormat;
339 bdx.outFormat = outFormat;
340 bdx.swap = swap;
341 if ((inOrdering === "L") && (outFormat === "VLTR")) { //core cases
342 //cases: LLTR->VLTR, LRTL->VLTR
343 if (inOrientation === "LTR") {
344 bdx.dir = LTR;
345 return doReorder(text, bdx);
346 }
347 if (inOrientation === "RTL") {
348 bdx.dir = RTL;
349 return doReorder(text, bdx);
350 }
351 }
352 if ((inOrdering === "V") && (outOrdering === "V")) {
353 //inOrientation != outOrientation
354 //cases: VRTL->VLTR, VLTR->VRTL
355 bdx.dir = inOrientation === "RTL"? RTL : LTR;
356 return invertStr(text, bdx);
357 }
358 if ((inOrdering === "L") && (outFormat === "VRTL")) {
359 //cases: LLTR->VRTL, LRTL->VRTL
360 if (inOrientation === "LTR") {
361 bdx.dir = LTR;
362 text = doReorder(text, bdx);
363 } else {
364 //inOrientation == RTL
365 bdx.dir = RTL;
366 text = doReorder(text, bdx);
367 }
368 return invertStr(text);
369 }
370 if ((inFormat === "VLTR") && (outFormat === "LLTR")) {
371 //case: VLTR->LLTR
372 bdx.dir = LTR;
373 return doReorder(text, bdx);
374 }
375 if ((inOrdering === "V") && (outOrdering === "L") && (inOrientation !== outOrientation)) {
376 //cases: VLTR->LRTL, VRTL->LLTR
377 text = invertStr(text);
378 return (inOrientation === "RTL") ? doBidiReorder(text, "LLTR", "VLTR", swap, bdx) :
379 doBidiReorder(text, "LRTL", "VRTL", swap, bdx);
380 }
381 if ((inFormat === "VRTL") && (outFormat === "LRTL")) {
382 //case VRTL->LRTL
383 return doBidiReorder(text, "LRTL", "VRTL", swap, bdx);
384 }
385 if ((inOrdering === "L") && (outOrdering === "L")) {
386 //inOrientation != outOrientation
387 //cases: LRTL->LLTR, LLTR->LRTL
388 var saveSwap = bdx.swap;
389 bdx.swap = saveSwap.substr(0, 1) + "N";
390 if (inOrientation === "RTL") {
391 //LRTL->LLTR
392 bdx.dir = RTL;
393 text = doReorder(text, bdx);
394 bdx.swap = "N" + saveSwap.substr(1, 2);
395 bdx.dir = LTR;
396 text = doReorder(text, bdx);
397 } else { //LLTR->LRTL
398 bdx.dir = LTR;
399 text = doReorder(text, bdx);
400 bdx.swap = "N" + saveSwap.substr(1, 2);
401 text = doBidiReorder(text, "VLTR", "LRTL", bdx.swap, bdx);
402 }
403 return text;
404 }
405 }
406
407 function prepareReorderingParameters(/*String*/text, /*Object*/params, /*Object*/bdx) {
408 // summary:
409 // Prepare reordering parameters
410 // text:
411 // The text to reorder.
412 // params:
413 // Object, containing reordering parameters:
414 // - inFormat: Ordering scheme and base direction of the source text.
415 // - outFormat: Required ordering scheme and base direction of the result.
416 // - swap: Symmetric swapping attributes of source and result.
417 // bdx: Object
418 // Used for intermediate data storage
419 // tags:
420 // private
421
422 if (params.inFormat === undefined) {
423 params.inFormat = bdx.defInFormat;
424 }
425 if (params.outFormat === undefined) {
426 params.outFormat = bdx.defOutFormat;
427 }
428 if (params.swap === undefined) {
429 params.swap = bdx.defSwap;
430 }
431 if (params.inFormat === params.outFormat) {
432 return params;
433 }
434 var dir, inOrdering = params.inFormat.substring(0, 1),
435 inOrientation = params.inFormat.substring(1, 4),
436 outOrdering = params.outFormat.substring(0, 1),
437 outOrientation = params.outFormat.substring(1, 4);
438 if (inOrientation.charAt(0) === "C") {
439 dir = firstStrongDir(text);
440 if (dir === "ltr" || dir === "rtl") {
441 inOrientation = dir.toUpperCase();
442 } else {
443 inOrientation = params.inFormat.charAt(2) === "L" ? "LTR" : "RTL";
444 }
445 params.inFormat = inOrdering + inOrientation;
446 }
447 if (outOrientation.charAt(0) === "C") {
448 dir = firstStrongDir(text);
449 if (dir === "rtl") {
450 outOrientation = "RTL";
451 } else if (dir === "ltr") {
452 dir = lastStrongDir(text);
453 outOrientation = dir.toUpperCase();
454 } else {
455 outOrientation = params.outFormat.charAt(2) === "L" ? "LTR" : "RTL";
456 }
457 params.outFormat = outOrdering + outOrientation;
458 }
459 return params;
460 }
461
462 function shape(/*boolean*/rtl, /*String*/text, /*boolean*/compress) {
463 // summary:
464 // Shape the source text.
465 // rtl:
466 // Flag indicating if the text is in RTL direction (logical
467 // direction for Arabic words).
468 // text:
469 // The text to shape.
470 // compress:
471 // A flag indicates to insert extra space after the lam alef compression
472 // to preserve the buffer size or not insert an extra space which will lead
473 // to decrease the buffer size. This option can be:
474 //
475 // - true (default) to not insert extra space after compressing Lam+Alef into one character Lamalef
476 // - false to insert an extra space after compressed Lamalef to preserve the buffer size
477 // returns:
478 // text shaped.
479 // tags:
480 // private.
481
482 if (text.length === 0) {
483 return;
484 }
485 if (rtl === undefined) {
486 rtl = true;
487 }
488 if (compress === undefined) {
489 compress = true;
490 }
491 text = String(text);
492
493 var str06 = text.split(""),
494 Ix = 0,
495 step = +1,
496 nIEnd = str06.length;
497 if (!rtl) {
498 Ix = str06.length - 1;
499 step = -1;
500 nIEnd = 1;
501 }
502 var compressArray = doShape(str06, Ix, step, nIEnd, compress);
503 var outBuf = "";
504 for (var idx = 0; idx < str06.length; idx++) {
505 if (!(compress && indexOf(compressArray, compressArray.length, idx) > -1)) {
506 outBuf += str06[idx];
507 } else {
508 updateMap(tsMap, idx, !rtl, -1);
509 stMap.splice(idx, 1);
510 }
511 }
512 return outBuf;
513 }
514
515 function doShape(str06, Ix, step, nIEnd, compress) {
516 // summary:
517 // Shape the source text.
518 // str06:
519 // Array containing source text
520 // Ix:
521 // Index of the first handled element
522 // step:
523 // direction of the process
524 // nIEnd:
525 // Index of the last handled element
526 // compress:
527 // A flag indicates to insert extra space after the lam alef compression
528 // to preserve the buffer size or not insert an extra space which will lead
529 // to decrease the buffer size.
530 // returns:
531 // Array, contained shaped text.
532 // tags:
533 // private.
534
535 var previousCursive = 0, compressArray = [], compressArrayIndx = 0;
536 for (var index = Ix; index * step < nIEnd; index = index + step) {
537 if (isArabicAlefbet(str06[index]) || isArabicDiacritics(str06[index])) {
538 // Arabic letter Lam
539 if (str06[index] === "\u0644" && isNextAlef(str06, (index + step), step, nIEnd)) {
540 str06[index] = (previousCursive === 0) ?
541 getLamAlefFE(str06[index + step], LamAlefInialTableFE) :
542 getLamAlefFE(str06[index + step], LamAlefMedialTableFE);
543 index += step;
544 setAlefToSpace(str06, index, step, nIEnd);
545 if (compress) {
546 compressArray[compressArrayIndx] = index;
547 compressArrayIndx++;
548 }
549 previousCursive = 0;
550 continue;
551 }
552 var currentChr = str06[index];
553 if (previousCursive === 1) {
554 // if next is Arabic
555 // Character is in medial form
556 // else character is in final form
557 str06[index] = (isNextArabic(str06, (index + step), step, nIEnd)) ?
558 getMedialFormCharacterFE(str06[index]) : getFormCharacterFE(str06[index], FinalForm);
559 } else {
560 if (isNextArabic(str06, (index + step), step, nIEnd) === true) {
561 //character is in Initial form
562 str06[index] = getFormCharacterFE(str06[index], InitialForm);
563 } else {
564 str06[index] = getFormCharacterFE(str06[index], IsolatedForm);
565 }
566 }
567 //exam if the current character is cursive
568 if (!isArabicDiacritics(currentChr)) {
569 previousCursive = 1;
570 }
571 if (isStandAlonCharacter(currentChr) === true) {
572 previousCursive = 0;
573 }
574 } else {
575 previousCursive = 0;
576 }
577 }
578 return compressArray;
579 }
580
581 function firstStrongDir(/*String*/text) {
582 // summary:
583 // Return the first strong character direction
584 // text:
585 // The source string.
586 // description:
587 // Searches for first "strong" character.
588 // Returns if strong character was found with the direction defined by this
589 // character, if no strong character was found returns an empty string.
590 // returns: String
591 // "ltr" - if the first strong character is Latin.
592 // "rtl" - if the first strong character is RTL directed character.
593 // "" - if the strong character wasn't found.
594 // tags:
595 // private
596
597 var fdc = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(text);
598 // if found return the direction that defined by the character
599 return fdc ? (fdc[0] <= "z" ? "ltr" : "rtl") : "";
600 }
601
602 function lastStrongDir(text) {
603 // summary:
604 // Return the last strong character direction
605 // text:
606 // The source string.
607 // description:
608 // Searches for first (from the end) "strong" character.
609 // Returns if strong character was found with the direction defined by this
610 // character, if no strong character was found returns an empty string.
611 // tags:
612 // private
613 var chars = text.split("");
614 chars.reverse();
615 return firstStrongDir(chars.join(""));
616 }
617
618 function deshape(/*String*/text, /*boolean*/rtl, /*boolean*/consumeNextSpace) {
619 // summary:
620 // deshape the source text.
621 // text:
622 // the text to be deshape.
623 // rtl:
624 // flag indicating if the text is in RTL direction (logical
625 // direction for Arabic words).
626 // consumeNextSpace:
627 // flag indicating whether to consume the space next to the
628 // the lam alef if there is a space followed the Lamalef character to preserve the buffer size.
629 // In case there is no space next to the lam alef the buffer size will be increased due to the
630 // expansion of the lam alef one character into lam+alef two characters
631 // returns:
632 // text deshaped.
633 if (text.length === 0) {
634 return;
635 }
636 if (consumeNextSpace === undefined) {
637 consumeNextSpace = true;
638 }
639 if (rtl === undefined) {
640 rtl = true;
641 }
642 text = String(text);
643
644 var outBuf = "", strFE = [];
645 strFE = text.split("");
646 for (var i = 0; i < text.length; i++) {
647 var increase = false;
648 if (strFE[i] >= "\uFE70" && strFE[i] < "\uFEFF") {
649 var chNum = text.charCodeAt(i);
650 if (strFE[i] >= "\uFEF5" && strFE[i] <= "\uFEFC") {
651 //expand the LamAlef
652 if (rtl) {
653 //Lam + Alef
654 if (i > 0 && consumeNextSpace && strFE[i - 1] === " ") {
655 outBuf = outBuf.substring(0, outBuf.length - 1) + "\u0644";
656 } else {
657 outBuf += "\u0644";
658 increase = true;
659 }
660 outBuf += AlefTable[(chNum - 65269) / 2];
661 } else {
662 outBuf += AlefTable[(chNum - 65269) / 2];
663 outBuf += "\u0644";
664 if (i + 1 < text.length && consumeNextSpace && strFE[i + 1] === " ") {
665 i++;
666 } else {
667 increase = true;
668 }
669 }
670 if (increase) {
671 updateMap(tsMap, i, true, 1);
672 stMap.splice(i, 0, stMap[i]);
673 }
674 } else {
675 outBuf += FETo06Table[chNum - 65136];
676 }
677 } else {
678 outBuf += strFE[i];
679 }
680 }
681 return outBuf;
682 }
683
684 function doReorder(str, bdx) {
685 // summary:
686 // Helper to the doBidiReorder. Manages the UBA.
687 // str:
688 // the string to reorder.
689 // bdx: Object
690 // Used for intermediate data storage
691 // returns:
692 // text reordered according to source and result attributes.
693 // tags:
694 // private
695 var chars = str.split(""), levels = [];
696
697 computeLevels(chars, levels, bdx);
698 swapChars(chars, levels, bdx);
699 invertLevel(2, chars, levels, bdx);
700 invertLevel(1, chars, levels, bdx);
701 lvMap = levels;
702 return chars.join("");
703 }
704
705 function computeLevels(chars, levels, bdx) {
706 var len = chars.length,
707 impTab = bdx.dir ? impTabRtl : impTabLtr,
708 prevState = null, newClass = null, newLevel = null, newState = 0,
709 action = null, cond = null, condPos = -1, i = null, ix = null,
710 types = [],
711 classes = [];
712 bdx.hiLevel = bdx.dir;
713 bdx.lastArabic = false;
714 bdx.hasUbatAl = false;
715 bdx.hasUbatB = false;
716 bdx.hasUbatS = false;
717 for (i = 0; i < len; i++) {
718 types[i] = getCharacterType(chars[i]);
719 }
720 for (ix = 0; ix < len; ix++) {
721 prevState = newState;
722 classes[ix] = newClass = getCharClass(chars, types, classes, ix, bdx);
723 newState = impTab[prevState][newClass];
724 action = newState & 0xF0;
725 newState &= 0x0F;
726 levels[ix] = newLevel = impTab[newState][ITIL];
727 if (action > 0) {
728 if (action === 0x10) { // set conditional run to level 1
729 for (i = condPos; i < ix; i++) {
730 levels[i] = 1;
731 }
732 condPos = -1;
733 } else { // 0x20 confirm the conditional run
734 condPos = -1;
735 }
736 }
737 cond = impTab[newState][ITCOND];
738 if (cond) {
739 if (condPos === -1) {
740 condPos = ix;
741 }
742 } else { // unconditional level
743 if (condPos > -1) {
744 for (i = condPos; i < ix; i++) {
745 levels[i] = newLevel;
746 }
747 condPos = -1;
748 }
749 }
750 if (types[ix] === UBAT_B) {
751 levels[ix] = 0;
752 }
753 bdx.hiLevel |= newLevel;
754 }
755 if (bdx.hasUbatS) {
756 handleUbatS(types, levels, len, bdx);
757 }
758 }
759
760 function handleUbatS(types, levels, len, bdx) {
761 for (var i = 0; i < len; i++) {
762 if (types[i] === UBAT_S) {
763 levels[i] = bdx.dir;
764 for (var j = i - 1; j >= 0; j--) {
765 if (types[j] === UBAT_WS) {
766 levels[j] = bdx.dir;
767 } else {
768 break;
769 }
770 }
771 }
772 }
773 }
774
775 function swapChars(chars, levels, bdx) {
776 // summary:
777 // Swap characters with symmetrical mirroring as all kinds of parenthesis.
778 // (When needed).
779 // chars:
780 // The source string as Array of characters.
781 // levels:
782 // An array (like hash) of flags for each character in the source string,
783 // that defines if swapping should be applied on the following character.
784 // bdx: Object
785 // Used for intermediate data storage
786 // tags:
787 // private
788
789 if (bdx.hiLevel === 0 || bdx.swap.substr(0, 1) === bdx.swap.substr(1, 2)) {
790 return;
791 }
792 for (var i = 0; i < chars.length; i++) {
793 if (levels[i] === 1) {
794 chars[i] = getMirror(chars[i]);
795 }
796 }
797 }
798
799 function getCharacterType(ch) {
800 // summary:
801 // Return the type of the character.
802 // ch:
803 // The character to be checked.
804
805 // description:
806 // Check the type of the character according to MasterTable,
807 // type = LTR, RTL, neutral,Arabic-Indic digit etc.
808 // tags:
809 // private
810 var uc = ch.charCodeAt(0),
811 hi = MasterTable[uc >> 8];
812 return (hi < TBBASE) ? hi : UnicodeTable[hi - TBBASE][uc & 0xFF];
813 }
814
815 function invertStr(str, bdx) {
816 // summary:
817 // Return the reversed string.
818 // str:
819 // The string to be reversed.
820 // description:
821 // Reverse the string str.
822 // tags:
823 // private
824 var chars = str.split("");
825 if (bdx) {
826 var levels = [];
827 computeLevels(chars, levels, bdx);
828 lvMap = levels;
829 }
830 chars.reverse();
831 stMap.reverse();
832 return chars.join("");
833 }
834
835 function indexOf(cArray, cLength, idx) {
836 for (var i = 0; i < cLength; i++) {
837 if (cArray[i] === idx) {
838 return i;
839 }
840 }
841 return -1;
842 }
843
844 function isArabicAlefbet(c) {
845 for (var i = 0; i < ArabicAlefBetIntervalsBegine.length; i++) {
846 if (c >= ArabicAlefBetIntervalsBegine[i] && c <= ArabicAlefBetIntervalsEnd[i]) {
847 return true;
848 }
849 }
850 return false;
851 }
852
853 function isNextArabic(str06, index, step, nIEnd) {
854 while (((index) * step) < nIEnd && isArabicDiacritics(str06[index])) {
855 index += step;
856 }
857 if (((index) * step) < nIEnd && isArabicAlefbet(str06[index])) {
858 return true;
859 }
860 return false;
861 }
862
863 function isNextAlef(str06, index, step, nIEnd) {
864 while (((index) * step) < nIEnd && isArabicDiacritics(str06[index])) {
865 index += step;
866 }
867 var c = " ";
868 if (((index) * step) < nIEnd) {
869 c = str06[index];
870 } else {
871 return false;
872 }
873 for (var i = 0; i < AlefTable.length; i++) {
874 if (AlefTable[i] === c) {
875 return true;
876 }
877 }
878 return false;
879 }
880
881 function invertLevel(lev, chars, levels, bdx) {
882 if (bdx.hiLevel < lev) {
883 return;
884 }
885 if (lev === 1 && bdx.dir === RTL && !bdx.hasUbatB) {
886 chars.reverse();
887 stMap.reverse();
888 return;
889 }
890 var len = chars.length, start = 0, end, lo, hi, tmp;
891 while (start < len) {
892 if (levels[start] >= lev) {
893 end = start + 1;
894 while (end < len && levels[end] >= lev) {
895 end++;
896 }
897 for (lo = start, hi = end - 1 ; lo < hi; lo++, hi--) {
898 tmp = chars[lo];
899 chars[lo] = chars[hi];
900 chars[hi] = tmp;
901 tmp = stMap[lo];
902 stMap[lo] = stMap[hi];
903 stMap[hi] = tmp;
904 }
905 start = end;
906 }
907 start++;
908 }
909 }
910
911 function getCharClass(chars, types, classes, ix, bdx) {
912 // summary:
913 // Return the class if ix character in chars.
914 // chars:
915 // The source string as Array of characters.
916 // types:
917 // Array of types, for each character in chars.
918 // classes:
919 // Array of classes that already been solved.
920 // ix:
921 // the index of checked character.
922 // bdx: Object
923 // Used for intermediate data storage
924 // tags:
925 // private
926 var cType = types[ix],
927 results = {
928 UBAT_L : function () { bdx.lastArabic = false; return UBAT_L; },
929 UBAT_R : function () { bdx.lastArabic = false; return UBAT_R; },
930 UBAT_ON : function () { return UBAT_ON; },
931 UBAT_AN : function () { return UBAT_AN; },
932 UBAT_EN : function () { return bdx.lastArabic ? UBAT_AN : UBAT_EN; },
933 UBAT_AL : function () { bdx.lastArabic = true; bdx.hasUbatAl = true; return UBAT_R; },
934 UBAT_WS : function () { return UBAT_ON; },
935 UBAT_CS : function () {
936 var wType, nType;
937 if (ix < 1 || (ix + 1) >= types.length ||
938 ((wType = classes[ix - 1]) !== UBAT_EN && wType !== UBAT_AN) ||
939 ((nType = types[ix + 1]) !== UBAT_EN && nType !== UBAT_AN)) {
940 return UBAT_ON;
941 }
942 if (bdx.lastArabic) {
943 nType = UBAT_AN;
944 }
945 return nType === wType ? nType : UBAT_ON;
946 },
947 UBAT_ES : function () {
948 var wType = ix > 0 ? classes[ix - 1] : UBAT_B;
949 if (wType === UBAT_EN && (ix + 1) < types.length && types[ix + 1] === UBAT_EN) {
950 return UBAT_EN;
951 }
952 return UBAT_ON;
953 },
954 UBAT_ET : function () {
955 if (ix > 0 && classes[ix - 1] === UBAT_EN) {
956 return UBAT_EN;
957 }
958 if (bdx.lastArabic) {
959 return UBAT_ON;
960 }
961 var i = ix + 1,
962 len = types.length;
963 while (i < len && types[i] === UBAT_ET) {
964 i++;
965 }
966 if (i < len && types[i] === UBAT_EN) {
967 return UBAT_EN;
968 }
969 return UBAT_ON;
970 },
971 UBAT_NSM : function () {
972 if (bdx.inFormat === "VLTR") { // visual to implicit transformation
973 var len = types.length,
974 i = ix + 1;
975 while (i < len && types[i] === UBAT_NSM) {
976 i++;
977 }
978 if (i < len) {
979 var c = chars[ix],
980 rtlCandidate = (c >= 0x0591 && c <= 0x08FF) || c === 0xFB1E,
981 wType = types[i];
982 if (rtlCandidate && (wType === UBAT_R || wType === UBAT_AL)) {
983 return UBAT_R;
984 }
985 }
986 }
987 if (ix < 1 || types[ix - 1] === UBAT_B) {
988 return UBAT_ON;
989 }
990 return classes[ix - 1];
991 },
992 UBAT_B : function () { bdx.lastArabic = true; bdx.hasUbatB = true; return bdx.dir; },
993 UBAT_S : function () { bdx.hasUbatS = true; return UBAT_ON; },
994 UBAT_LRE : function () { bdx.lastArabic = false; return UBAT_ON; },
995 UBAT_RLE : function () { bdx.lastArabic = false; return UBAT_ON; },
996 UBAT_LRO : function () { bdx.lastArabic = false; return UBAT_ON; },
997 UBAT_RLO : function () { bdx.lastArabic = false; return UBAT_ON; },
998 UBAT_PDF : function () { bdx.lastArabic = false; return UBAT_ON; },
999 UBAT_BN : function () { return UBAT_ON; }
1000 };
1001 return results[TYPES_NAMES[cType]]();
1002 }
1003
1004 function getMirror(c) {
1005 // summary:
1006 // Calculates the mirrored character of c
1007 // c:
1008 // The character to be mirrored.
1009 // tags:
1010 // private
1011 var mid, low = 0, high = SwapTable.length - 1;
1012
1013 while (low <= high) {
1014 mid = Math.floor((low + high) / 2);
1015 if (c < SwapTable[mid][0]) {
1016 high = mid - 1;
1017 } else if (c > SwapTable[mid][0]) {
1018 low = mid + 1;
1019 } else {
1020 return SwapTable[mid][1];
1021 }
1022 }
1023 return c;
1024 }
1025
1026 function isStandAlonCharacter(c) {
1027 for (var i = 0; i < StandAlonForm.length; i++) {
1028 if (StandAlonForm[i] === c) {
1029 return true;
1030 }
1031 }
1032 return false;
1033 }
1034
1035 function getMedialFormCharacterFE(c) {
1036 for (var i = 0; i < BaseForm.length; i++) {
1037 if (c === BaseForm[i]) {
1038 return MedialForm[i];
1039 }
1040 }
1041 return c;
1042 }
1043
1044 function getFormCharacterFE(/*char*/ c, /*char[]*/formArr) {
1045 for (var i = 0; i < BaseForm.length; i++) {
1046 if (c === BaseForm[i]) {
1047 return formArr[i];
1048 }
1049 }
1050 return c;
1051 }
1052
1053 function isArabicDiacritics(c) {
1054 return (c >= "\u064b" && c <= "\u0655") ? true : false;
1055 }
1056
1057 function getOrientation(/*Char*/ oc) {
1058 if (oc === "L") {
1059 return "LTR";
1060 }
1061 if (oc === "R") {
1062 return "RTL";
1063 }
1064 if (oc === "C") {
1065 return "CLR";
1066 }
1067 if (oc === "D") {
1068 return "CRL";
1069 }
1070 }
1071
1072 function setAlefToSpace(str06, index, step, nIEnd) {
1073 while (((index) * step) < nIEnd && isArabicDiacritics(str06[index])) {
1074 index += step;
1075 }
1076 if (((index) * step) < nIEnd) {
1077 str06[index] = " ";
1078 return true;
1079 }
1080 return false;
1081 }
1082
1083 function getLamAlefFE(alef06, LamAlefForm) {
1084 for (var i = 0; i < AlefTable.length; i++) {
1085 if (alef06 === AlefTable[i]) {
1086 return LamAlefForm[i];
1087 }
1088 }
1089 return alef06;
1090 }
1091
1092 function initMaps(map1, map2, length) {
1093 stMap = [];
1094 lvMap = [];
1095 for (var i = 0; i < length; i++) {
1096 map1[i] = i;
1097 map2[i] = i;
1098 stMap[i] = i;
1099 }
1100 }
1101
1102 function reverseMap(sourceMap) {
1103 var map = new Array(sourceMap.length);
1104 for (var i = 0; i < sourceMap.length; i++) {
1105 map[sourceMap[i]] = i;
1106 }
1107 return map;
1108 }
1109
1110 function updateMap(map, value, isGreater, update) {
1111 for (var i = 0; i < map.length; i++) {
1112 if (map[i] > value || (!isGreater && map[i] === value)) {
1113 map[i] += update;
1114 }
1115 }
1116 }
1117
1118 var stMap = [];
1119 var tsMap = [];
1120 var lvMap = [];
1121
1122 var BDX = {
1123 dir: 0,
1124 defInFormat: "LLTR",
1125 defoutFormat: "VLTR",
1126 defSwap: "YN",
1127 inFormat: "LLTR",
1128 outFormat: "VLTR",
1129 swap: "YN",
1130 hiLevel: 0,
1131 lastArabic: false,
1132 hasUbatAl: false,
1133 hasBlockSep: false,
1134 hasSegSep: false
1135 };
1136
1137 var ITIL = 5;
1138
1139 var ITCOND = 6;
1140
1141 var LTR = 0;
1142
1143 var RTL = 1;
1144
1145 var validFormat = /^[(I|V)][(L|R|C|D)][(Y|N)][(S|N)][N]$/;
1146
1147 var bidiChars = /[\u0591-\u06ff\ufb1d-\ufefc]/;
1148
1149 /****************************************************************************/
1150 /* Array in which directional characters are replaced by their symmetric. */
1151 /****************************************************************************/
1152 var SwapTable = [
1153 [ "\u0028", "\u0029" ], /* Round brackets */
1154 [ "\u0029", "\u0028" ],
1155 [ "\u003C", "\u003E" ], /* Less than/greater than */
1156 [ "\u003E", "\u003C" ],
1157 [ "\u005B", "\u005D" ], /* Square brackets */
1158 [ "\u005D", "\u005B" ],
1159 [ "\u007B", "\u007D" ], /* Curly brackets */
1160 [ "\u007D", "\u007B" ],
1161 [ "\u00AB", "\u00BB" ], /* Double angle quotation marks */
1162 [ "\u00BB", "\u00AB" ],
1163 [ "\u2039", "\u203A" ], /* single angle quotation mark */
1164 [ "\u203A", "\u2039" ],
1165 [ "\u207D", "\u207E" ], /* Superscript parentheses */
1166 [ "\u207E", "\u207D" ],
1167 [ "\u208D", "\u208E" ], /* Subscript parentheses */
1168 [ "\u208E", "\u208D" ],
1169 [ "\u2264", "\u2265" ], /* Less/greater than or equal */
1170 [ "\u2265", "\u2264" ],
1171 [ "\u2329", "\u232A" ], /* Angle brackets */
1172 [ "\u232A", "\u2329" ],
1173 [ "\uFE59", "\uFE5A" ], /* Small round brackets */
1174 [ "\uFE5A", "\uFE59" ],
1175 [ "\uFE5B", "\uFE5C" ], /* Small curly brackets */
1176 [ "\uFE5C", "\uFE5B" ],
1177 [ "\uFE5D", "\uFE5E" ], /* Small tortoise shell brackets */
1178 [ "\uFE5E", "\uFE5D" ],
1179 [ "\uFE64", "\uFE65" ], /* Small less than/greater than */
1180 [ "\uFE65", "\uFE64" ]
1181 ];
1182 var AlefTable = ["\u0622", "\u0623", "\u0625", "\u0627"];
1183
1184 var LamAlefInialTableFE = ["\ufef5", "\ufef7", "\ufef9", "\ufefb"];
1185
1186 var LamAlefMedialTableFE = ["\ufef6", "\ufef8", "\ufefa", "\ufefc"];
1187 /**
1188 * Arabic Characters in the base form
1189 */
1190 var BaseForm = ["\u0627", "\u0628", "\u062A", "\u062B", "\u062C", "\u062D", "\u062E", "\u062F", "\u0630", "\u0631",
1191 "\u0632", "\u0633", "\u0634", "\u0635", "\u0636", "\u0637", "\u0638", "\u0639", "\u063A", "\u0641",
1192 "\u0642", "\u0643", "\u0644", "\u0645", "\u0646", "\u0647", "\u0648", "\u064A", "\u0625", "\u0623",
1193 "\u0622", "\u0629", "\u0649", "\u0644", "\u0645", "\u0646", "\u0647", "\u0648", "\u064A", "\u0625",
1194 "\u0623", "\u0622", "\u0629", "\u0649", "\u06CC", "\u0626", "\u0624"];
1195
1196 /**
1197 * Arabic shaped characters in Isolated form
1198 */
1199 var IsolatedForm = ["\uFE8D", "\uFE8F", "\uFE95", "\uFE99", "\uFE9D", "\uFEA1", "\uFEA5", "\uFEA9", "\uFEAB",
1200 "\uFEAD", "\uFEAF", "\uFEB1", "\uFEB5", "\uFEB9", "\uFEBD", "\uFEC1", "\uFEC5", "\uFEC9",
1201 "\uFECD", "\uFED1", "\uFED5", "\uFED9", "\uFEDD", "\uFEE1", "\uFEE5", "\uFEE9", "\uFEED",
1202 "\uFEF1", "\uFE87", "\uFE83", "\uFE81", "\uFE93", "\uFEEF", "\uFBFC", "\uFE89", "\uFE85",
1203 "\uFE70", "\uFE72", "\uFE74", "\uFE76", "\uFE78", "\uFE7A", "\uFE7C", "\uFE7E", "\uFE80",
1204 "\uFE89", "\uFE85"];
1205
1206 /**
1207 * Arabic shaped characters in Final form
1208 */
1209 var FinalForm = ["\uFE8E", "\uFE90", "\uFE96", "\uFE9A", "\uFE9E", "\uFEA2", "\uFEA6", "\uFEAA", "\uFEAC", "\uFEAE",
1210 "\uFEB0", "\uFEB2", "\uFEB6", "\uFEBA", "\uFEBE", "\uFEC2", "\uFEC6", "\uFECA", "\uFECE", "\uFED2",
1211 "\uFED6", "\uFEDA", "\uFEDE", "\uFEE2", "\uFEE6", "\uFEEA", "\uFEEE", "\uFEF2", "\uFE88", "\uFE84",
1212 "\uFE82", "\uFE94", "\uFEF0", "\uFBFD", "\uFE8A", "\uFE86", "\uFE70", "\uFE72", "\uFE74", "\uFE76",
1213 "\uFE78", "\uFE7A", "\uFE7C", "\uFE7E", "\uFE80", "\uFE8A", "\uFE86"];
1214
1215 /**
1216 * Arabic shaped characters in Media form
1217 */
1218 var MedialForm = ["\uFE8E", "\uFE92", "\uFE98", "\uFE9C", "\uFEA0", "\uFEA4", "\uFEA8", "\uFEAA", "\uFEAC",
1219 "\uFEAE", "\uFEB0", "\uFEB4", "\uFEB8", "\uFEBC", "\uFEC0", "\uFEC4", "\uFEC8", "\uFECC",
1220 "\uFED0", "\uFED4", "\uFED8", "\uFEDC", "\uFEE0", "\uFEE4", "\uFEE8", "\uFEEC", "\uFEEE",
1221 "\uFEF4", "\uFE88", "\uFE84", "\uFE82", "\uFE94", "\uFEF0", "\uFBFF", "\uFE8C", "\uFE86",
1222 "\uFE71", "\uFE72", "\uFE74", "\uFE77", "\uFE79", "\uFE7B", "\uFE7D", "\uFE7F", "\uFE80",
1223 "\uFE8C", "\uFE86"];
1224
1225 /**
1226 * Arabic shaped characters in Initial form
1227 */
1228 var InitialForm = ["\uFE8D", "\uFE91", "\uFE97", "\uFE9B", "\uFE9F", "\uFEA3", "\uFEA7", "\uFEA9", "\uFEAB",
1229 "\uFEAD", "\uFEAF", "\uFEB3", "\uFEB7", "\uFEBB", "\uFEBF", "\uFEC3", "\uFEC7", "\uFECB",
1230 "\uFECF", "\uFED3", "\uFED7", "\uFEDB", "\uFEDF", "\uFEE3", "\uFEE7", "\uFEEB", "\uFEED",
1231 "\uFEF3", "\uFE87", "\uFE83", "\uFE81", "\uFE93", "\uFEEF", "\uFBFE", "\uFE8B", "\uFE85",
1232 "\uFE70", "\uFE72", "\uFE74", "\uFE76", "\uFE78", "\uFE7A", "\uFE7C", "\uFE7E", "\uFE80",
1233 "\uFE8B", "\uFE85"];
1234
1235 /**
1236 * Arabic characters that couldn't join to the next character
1237 */
1238 var StandAlonForm = ["\u0621", "\u0622", "\u0623", "\u0624", "\u0625", "\u0627", "\u0629", "\u062F", "\u0630",
1239 "\u0631", "\u0632", "\u0648", "\u0649"];
1240
1241 var FETo06Table = ["\u064B", "\u064B", "\u064C", "\u061F", "\u064D", "\u061F", "\u064E", "\u064E", "\u064F",
1242 "\u064F", "\u0650", "\u0650", "\u0651", "\u0651", "\u0652", "\u0652", "\u0621", "\u0622",
1243 "\u0622", "\u0623", "\u0623", "\u0624", "\u0624", "\u0625", "\u0625", "\u0626", "\u0626",
1244 "\u0626", "\u0626", "\u0627", "\u0627", "\u0628", "\u0628", "\u0628", "\u0628", "\u0629",
1245 "\u0629", "\u062A", "\u062A", "\u062A", "\u062A", "\u062B", "\u062B", "\u062B", "\u062B",
1246 "\u062C", "\u062C", "\u062C", "\u062c", "\u062D", "\u062D", "\u062D", "\u062D", "\u062E",
1247 "\u062E", "\u062E", "\u062E", "\u062F", "\u062F", "\u0630", "\u0630", "\u0631", "\u0631",
1248 "\u0632", "\u0632", "\u0633", "\u0633", "\u0633", "\u0633", "\u0634", "\u0634", "\u0634",
1249 "\u0634", "\u0635", "\u0635", "\u0635", "\u0635", "\u0636", "\u0636", "\u0636", "\u0636",
1250 "\u0637", "\u0637", "\u0637", "\u0637", "\u0638", "\u0638", "\u0638", "\u0638", "\u0639",
1251 "\u0639", "\u0639", "\u0639", "\u063A", "\u063A", "\u063A", "\u063A", "\u0641", "\u0641",
1252 "\u0641", "\u0641", "\u0642", "\u0642", "\u0642", "\u0642", "\u0643", "\u0643", "\u0643",
1253 "\u0643", "\u0644", "\u0644", "\u0644", "\u0644", "\u0645", "\u0645", "\u0645", "\u0645",
1254 "\u0646", "\u0646", "\u0646", "\u0646", "\u0647", "\u0647", "\u0647", "\u0647", "\u0648",
1255 "\u0648", "\u0649", "\u0649", "\u064A", "\u064A", "\u064A", "\u064A", "\uFEF5", "\uFEF6",
1256 "\uFEF7", "\uFEF8", "\uFEF9", "\uFEFA", "\uFEFB", "\uFEFC", "\u061F", "\u061F", "\u061F"];
1257
1258 var ArabicAlefBetIntervalsBegine = ["\u0621", "\u0641"];
1259
1260 var ArabicAlefBetIntervalsEnd = ["\u063A", "\u064a"];
1261
1262 var impTabLtr = [
1263 /* L, R, EN, AN, N, IL, Cond */
1264 [ 0, 3, 0, 1, 0, 0, 0 ], /* 0 LTR text */
1265 [ 0, 3, 0, 1, 2, 2, 0 ], /* 1 LTR+AN */
1266 [ 0, 3, 0, 0x11, 2, 0, 1 ], /* 2 LTR+AN+N */
1267 [ 0, 3, 5, 5, 4, 1, 0 ], /* 3 RTL text */
1268 [ 0, 3, 0x15, 0x15, 4, 0, 1 ], /* 4 RTL cont */
1269 [ 0, 3, 5, 5, 4, 2, 0 ] /* 5 RTL+EN/AN */
1270 ];
1271 var impTabRtl = [
1272 /* L, R, EN, AN, N, IL, Cond */
1273 [ 2, 0, 1, 1, 0, 1, 0 ], /* 0 RTL text */
1274 [ 2, 0, 1, 1, 0, 2, 0 ], /* 1 RTL+EN/AN */
1275 [ 2, 0, 2, 1, 3, 2, 0 ], /* 2 LTR text */
1276 [ 2, 0, 2, 0x21, 3, 1, 1 ] /* 3 LTR+cont */
1277 ];
1278
1279 var UBAT_L = 0; /* left to right */
1280 var UBAT_R = 1; /* right to left */
1281 var UBAT_EN = 2; /* European digit */
1282 var UBAT_AN = 3; /* Arabic-Indic digit */
1283 var UBAT_ON = 4; /* neutral */
1284 var UBAT_B = 5; /* block separator */
1285 var UBAT_S = 6; /* segment separator */
1286 var UBAT_AL = 7; /* Arabic Letter */
1287 var UBAT_WS = 8; /* white space */
1288 var UBAT_CS = 9; /* common digit separator */
1289 var UBAT_ES = 10; /* European digit separator */
1290 var UBAT_ET = 11; /* European digit terminator */
1291 var UBAT_NSM = 12; /* Non Spacing Mark */
1292 var UBAT_LRE = 13; /* LRE */
1293 var UBAT_RLE = 14; /* RLE */
1294 var UBAT_PDF = 15; /* PDF */
1295 var UBAT_LRO = 16; /* LRO */
1296 var UBAT_RLO = 17; /* RLO */
1297 var UBAT_BN = 18; /* Boundary Neutral */
1298
1299 var TYPES_NAMES = [ "UBAT_L", "UBAT_R", "UBAT_EN", "UBAT_AN", "UBAT_ON", "UBAT_B", "UBAT_S", "UBAT_AL", "UBAT_WS",
1300 "UBAT_CS", "UBAT_ES", "UBAT_ET", "UBAT_NSM", "UBAT_LRE", "UBAT_RLE", "UBAT_PDF", "UBAT_LRO",
1301 "UBAT_RLO", "UBAT_BN" ];
1302 var TBBASE = 100;
1303
1304 var TB00 = TBBASE + 0;
1305 var TB05 = TBBASE + 1;
1306 var TB06 = TBBASE + 2;
1307 var TB07 = TBBASE + 3;
1308 var TB20 = TBBASE + 4;
1309 var TBFB = TBBASE + 5;
1310 var TBFE = TBBASE + 6;
1311 var TBFF = TBBASE + 7;
1312
1313 var L = UBAT_L;
1314 var R = UBAT_R;
1315 var EN = UBAT_EN;
1316 var AN = UBAT_AN;
1317 var ON = UBAT_ON;
1318 var B = UBAT_B;
1319 var S = UBAT_S;
1320 var AL = UBAT_AL;
1321 var WS = UBAT_WS;
1322 var CS = UBAT_CS;
1323 var ES = UBAT_ES;
1324 var ET = UBAT_ET;
1325 var NSM = UBAT_NSM;
1326 var LRE = UBAT_LRE;
1327 var RLE = UBAT_RLE;
1328 var PDF = UBAT_PDF;
1329 var LRO = UBAT_LRO;
1330 var RLO = UBAT_RLO;
1331 var BN = UBAT_BN;
1332
1333 var MasterTable = [
1334 /*******************************************************************************************************/
1335 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1336 /*******************************************************************************************************/
1337 /*0-*/ TB00, L, L, L, L, TB05, TB06, TB07, R, L, L, L, L, L, L, L,
1338 /*1-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1339 /*2-*/ TB20, ON, ON, ON, L, ON, L, ON, L, ON, ON, ON, L, L, ON, ON,
1340 /*3-*/ L, L, L, L, L, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1341 /*4-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, L, L, ON,
1342 /*5-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1343 /*6-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1344 /*7-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1345 /*8-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1346 /*9-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, L,
1347 /*A-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON,
1348 /*B-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1349 /*C-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1350 /*D-*/ ON, ON, ON, ON, ON, ON, ON, L, L, ON, ON, L, L, ON, ON, L,
1351 /*E-*/ L, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1352 /*F-*/ ON, ON, ON, ON, ON, ON, ON, ON, L, L, L, TBFB, AL, AL, TBFE, TBFF
1353 ];
1354
1355 var UnicodeTable = [
1356 [ /* Table 00: Unicode 00xx */
1357 /****************************************************************************************/
1358 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1359 /****************************************************************************************/
1360 /*0-*/ BN, BN, BN, BN, BN, BN, BN, BN, BN, S, B, S, WS, B, BN, BN,
1361 /*1-*/ BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, B, B, B, S,
1362 /*2-*/ WS, ON, ON, ET, ET, ET, ON, ON, ON, ON, ON, ES, CS, ES, CS, CS,
1363 /*3-*/ EN, EN, EN, EN, EN, EN, EN, EN, EN, EN, CS, ON, ON, ON, ON, ON,
1364 /*4-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1365 /*5-*/ L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON, ON, ON,
1366 /*6-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1367 /*7-*/ L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON, ON, BN,
1368 /*8-*/ BN, BN, BN, BN, BN, B, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN,
1369 /*9-*/ BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN, BN,
1370 /*A-*/ CS, ON, ET, ET, ET, ET, ON, ON, ON, ON, L, ON, ON, BN, ON, ON,
1371 /*B-*/ ET, ET, EN, EN, ON, L, ON, ON, ON, EN, L, ON, ON, ON, ON, ON,
1372 /*C-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1373 /*D-*/ L, L, L, L, L, L, L, ON, L, L, L, L, L, L, L, L,
1374 /*E-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1375 /*F-*/ L, L, L, L, L, L, L, ON, L, L, L, L, L, L, L, L
1376 ],
1377 [ /* Table 01: Unicode 05xx */
1378 /****************************************************************************************/
1379 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1380 /****************************************************************************************/
1381 /*0-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1382 /*1-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1383 /*2-*/ L, L, L, L, L, L, L, L, ON, ON, ON, ON, ON, ON, ON, ON,
1384 /*3-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1385 /*4-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1386 /*5-*/ L, L, L, L, L, L, L, ON, ON, L, L, L, L, L, L, L,
1387 /*6-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1388 /*7-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1389 /*8-*/ L, L, L, L, L, L, L, L, ON, L, ON, ON, ON, ON, ON, ON,
1390 /*9-*/ ON, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1391 /*A-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1392 /*B-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, R, NSM,
1393 /*C-*/ R, NSM, NSM, R, NSM, NSM, R, NSM, ON, ON, ON, ON, ON, ON, ON, ON,
1394 /*D-*/ R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
1395 /*E-*/ R, R, R, R, R, R, R, R, R, R, R, ON, ON, ON, ON, ON,
1396 /*F-*/ R, R, R, R, R, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON
1397 ],
1398 [ /* Table 02: Unicode 06xx */
1399 /****************************************************************************************/
1400 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1401 /****************************************************************************************/
1402 /*0-*/ AN, AN, AN, AN, ON, ON, ON, ON, AL, ET, ET, AL, CS, AL, ON, ON,
1403 /*1-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, AL, ON, ON, AL, AL,
1404 /*2-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1405 /*3-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1406 /*4-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, NSM, NSM, NSM, NSM, NSM,
1407 /*5-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1408 /*6-*/ AN, AN, AN, AN, AN, AN, AN, AN, AN, AN, ET, AN, AN, AL, AL, AL,
1409 /*7-*/ NSM, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1410 /*8-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1411 /*9-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1412 /*A-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1413 /*B-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1414 /*C-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1415 /*D-*/ AL, AL, AL, AL, AL, AL, NSM, NSM, NSM, NSM, NSM, NSM, NSM, AN, ON, NSM,
1416 /*E-*/ NSM, NSM, NSM, NSM, NSM, AL, AL, NSM, NSM, ON, NSM, NSM, NSM, NSM, AL, AL,
1417 /*F-*/ EN, EN, EN, EN, EN, EN, EN, EN, EN, EN, AL, AL, AL, AL, AL, AL
1418 ],
1419 [ /* Table 03: Unicode 07xx */
1420 /****************************************************************************************/
1421 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1422 /****************************************************************************************/
1423 /*0-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, ON, AL,
1424 /*1-*/ AL, NSM, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1425 /*2-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1426 /*3-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1427 /*4-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, ON, ON, AL, AL, AL,
1428 /*5-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1429 /*6-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1430 /*7-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1431 /*8-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1432 /*9-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1433 /*A-*/ AL, AL, AL, AL, AL, AL, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1434 /*B-*/ NSM, AL, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1435 /*C-*/ R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
1436 /*D-*/ R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
1437 /*E-*/ R, R, R, R, R, R, R, R, R, R, R, NSM, NSM, NSM, NSM, NSM,
1438 /*F-*/ NSM, NSM, NSM, NSM, R, R, ON, ON, ON, ON, R, ON, ON, ON, ON, ON
1439 ],
1440 [ /* Table 04: Unicode 20xx */
1441 /****************************************************************************************/
1442 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1443 /****************************************************************************************/
1444 /*0-*/ WS, WS, WS, WS, WS, WS, WS, WS, WS, WS, WS, BN, BN, BN, L, R,
1445 /*1-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1446 /*2-*/ ON, ON, ON, ON, ON, ON, ON, ON, WS, B, LRE, RLE, PDF, LRO, RLO, CS,
1447 /*3-*/ ET, ET, ET, ET, ET, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1448 /*4-*/ ON, ON, ON, ON, CS, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1449 /*5-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, WS,
1450 /*6-*/ BN, BN, BN, BN, BN, ON, ON, ON, ON, ON, BN, BN, BN, BN, BN, BN,
1451 /*7-*/ EN, L, ON, ON, EN, EN, EN, EN, EN, EN, ES, ES, ON, ON, ON, L,
1452 /*8-*/ EN, EN, EN, EN, EN, EN, EN, EN, EN, EN, ES, ES, ON, ON, ON, ON,
1453 /*9-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON,
1454 /*A-*/ ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ET,
1455 /*B-*/ ET, ET, ET, ET, ET, ET, ET, ET, ET, ET, ON, ON, ON, ON, ON, ON,
1456 /*C-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1457 /*D-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1458 /*E-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1459 /*F-*/ NSM, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON
1460 ],
1461 [ /* Table 05: Unicode FBxx */
1462 /****************************************************************************************/
1463 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1464 /****************************************************************************************/
1465 /*0-*/ L, L, L, L, L, L, L, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1466 /*1-*/ ON, ON, ON, L, L, L, L, L, ON, ON, ON, ON, ON, R, NSM, R,
1467 /*2-*/ R, R, R, R, R, R, R, R, R, ES, R, R, R, R, R, R,
1468 /*3-*/ R, R, R, R, R, R, R, ON, R, R, R, R, R, ON, R, ON,
1469 /*4-*/ R, R, ON, R, R, ON, R, R, R, R, R, R, R, R, R, R,
1470 /*5-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1471 /*6-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1472 /*7-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1473 /*8-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1474 /*9-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1475 /*A-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1476 /*B-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1477 /*C-*/ AL, AL, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1478 /*D-*/ ON, ON, ON, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1479 /*E-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1480 /*F-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL
1481 ],
1482 [ /* Table 06: Unicode FExx */
1483 /****************************************************************************************/
1484 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1485 /****************************************************************************************/
1486 /*0-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM, NSM,
1487 /*1-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1488 /*2-*/ NSM, NSM, NSM, NSM, NSM, NSM, NSM, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1489 /*3-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1490 /*4-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1491 /*5-*/ CS, ON, CS, ON, ON, CS, ON, ON, ON, ON, ON, ON, ON, ON, ON, ET,
1492 /*6-*/ ON, ON, ES, ES, ON, ON, ON, ON, ON, ET, ET, ON, ON, ON, ON, ON,
1493 /*7-*/ AL, AL, AL, AL, AL, ON, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1494 /*8-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1495 /*9-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1496 /*A-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1497 /*B-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1498 /*C-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1499 /*D-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1500 /*E-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
1501 /*F-*/ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, ON, ON, BN
1502 ],
1503 [ /* Table 07: Unicode FFxx */
1504 /****************************************************************************************/
1505 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
1506 /****************************************************************************************/
1507 /*0-*/ ON, ON, ON, ET, ET, ET, ON, ON, ON, ON, ON, ES, CS, ES, CS, CS,
1508 /*1-*/ EN, EN, EN, EN, EN, EN, EN, EN, EN, EN, CS, ON, ON, ON, ON, ON,
1509 /*2-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1510 /*3-*/ L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON, ON, ON,
1511 /*4-*/ ON, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1512 /*5-*/ L, L, L, L, L, L, L, L, L, L, L, ON, ON, ON, ON, ON,
1513 /*6-*/ ON, ON, ON, ON, ON, ON, L, L, L, L, L, L, L, L, L, L,
1514 /*7-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1515 /*8-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1516 /*9-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1517 /*A-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
1518 /*B-*/ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, ON,
1519 /*C-*/ ON, ON, L, L, L, L, L, L, ON, ON, L, L, L, L, L, L,
1520 /*D-*/ ON, ON, L, L, L, L, L, L, ON, ON, L, L, L, ON, ON, ON,
1521 /*E-*/ ET, ET, ON, ON, ON, ET, ET, ON, ON, ON, ON, ON, ON, ON, ON, ON,
1522 /*F-*/ ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON
1523 ]
1524 ];
1525
1526 return BidiEngine;
1527 });