diff --git a/src/core/bidi.js b/src/core/bidi.js index 01a5aa373..6c28dc4b3 100644 --- a/src/core/bidi.js +++ b/src/core/bidi.js @@ -79,8 +79,9 @@ var bidi = PDFJS.bidi = (function bidiClosure() { function findUnequal(arr, start, value) { var j; for (var j = start, jj = arr.length; j < jj; ++j) { - if (arr[j] != value) + if (arr[j] != value) { return j; + } } return j; } @@ -141,17 +142,17 @@ var bidi = PDFJS.bidi = (function bidiClosure() { function BidiResult(str, isLTR, vertical) { this.str = str; - this.dir = vertical ? 'ttb' : isLTR ? 'ltr' : 'rtl'; + this.dir = (vertical ? 'ttb' : (isLTR ? 'ltr' : 'rtl')); } function bidi(str, startLevel, vertical) { var isLTR = true; var strLength = str.length; - if (strLength === 0 || vertical) + if (strLength === 0 || vertical) { return new BidiResult(str, isLTR, vertical); + } - // get types, fill arrays - + // Get types and fill arrays var chars = []; var types = []; var numBidi = 0; @@ -161,25 +162,25 @@ var bidi = PDFJS.bidi = (function bidiClosure() { var charCode = str.charCodeAt(i); var charType = 'L'; - if (charCode <= 0x00ff) + if (charCode <= 0x00ff) { charType = baseTypes[charCode]; - else if (0x0590 <= charCode && charCode <= 0x05f4) + } else if (0x0590 <= charCode && charCode <= 0x05f4) { charType = 'R'; - else if (0x0600 <= charCode && charCode <= 0x06ff) + } else if (0x0600 <= charCode && charCode <= 0x06ff) { charType = arabicTypes[charCode & 0xff]; - else if (0x0700 <= charCode && charCode <= 0x08AC) + } else if (0x0700 <= charCode && charCode <= 0x08AC) { charType = 'AL'; - - if (charType == 'R' || charType == 'AL' || charType == 'AN') + } + if (charType == 'R' || charType == 'AL' || charType == 'AN') { numBidi++; - + } types[i] = charType; } - // detect the bidi method - // if there are no rtl characters then no bidi needed - // if less than 30% chars are rtl then string is primarily ltr - // if more than 30% chars are rtl then string is primarily rtl + // Detect the bidi method + // - If there are no rtl characters then no bidi needed + // - If less than 30% chars are rtl then string is primarily ltr + // - If more than 30% chars are rtl then string is primarily rtl if (numBidi === 0) { isLTR = true; return new BidiResult(str, isLTR); @@ -196,7 +197,6 @@ var bidi = PDFJS.bidi = (function bidiClosure() { } var levels = []; - for (var i = 0; i < strLength; ++i) { levels[i] = startLevel; } @@ -204,8 +204,7 @@ var bidi = PDFJS.bidi = (function bidiClosure() { /* X1-X10: skip most of this, since we are NOT doing the embeddings. */ - - var e = isOdd(startLevel) ? 'R' : 'L'; + var e = (isOdd(startLevel) ? 'R' : 'L'); var sor = e; var eor = sor; @@ -214,13 +213,13 @@ var bidi = PDFJS.bidi = (function bidiClosure() { type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor. */ - var lastType = sor; for (var i = 0; i < strLength; ++i) { - if (types[i] == 'NSM') + if (types[i] == 'NSM') { types[i] = lastType; - else + } else { lastType = types[i]; + } } /* @@ -228,24 +227,24 @@ var bidi = PDFJS.bidi = (function bidiClosure() { first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number. */ - var lastType = sor; for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == 'EN') + if (t == 'EN') { types[i] = (lastType == 'AL') ? 'AN' : 'EN'; - else if (t == 'R' || t == 'L' || t == 'AL') + } else if (t == 'R' || t == 'L' || t == 'AL') { lastType = t; + } } /* W3. Change all ALs to R. */ - for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == 'AL') + if (t == 'AL') { types[i] = 'R'; + } } /* @@ -253,32 +252,34 @@ var bidi = PDFJS.bidi = (function bidiClosure() { European number. A single common separator between two numbers of the same type changes to that type: */ - for (var i = 1; i < strLength - 1; ++i) { - if (types[i] == 'ES' && types[i - 1] == 'EN' && types[i + 1] == 'EN') + if (types[i] == 'ES' && types[i - 1] == 'EN' && types[i + 1] == 'EN') { types[i] = 'EN'; + } if (types[i] == 'CS' && (types[i - 1] == 'EN' || types[i - 1] == 'AN') && - types[i + 1] == types[i - 1]) + types[i + 1] == types[i - 1]) { types[i] = types[i - 1]; + } } /* W5. A sequence of European terminators adjacent to European numbers changes to all European numbers: */ - for (var i = 0; i < strLength; ++i) { if (types[i] == 'EN') { // do before for (var j = i - 1; j >= 0; --j) { - if (types[j] != 'ET') + if (types[j] != 'ET') { break; + } types[j] = 'EN'; } // do after for (var j = i + 1; j < strLength; --j) { - if (types[j] != 'ET') + if (types[j] != 'ET') { break; + } types[j] = 'EN'; } } @@ -287,11 +288,11 @@ var bidi = PDFJS.bidi = (function bidiClosure() { /* W6. Otherwise, separators and terminators change to Other Neutral: */ - for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == 'WS' || t == 'ES' || t == 'ET' || t == 'CS') + if (t == 'WS' || t == 'ES' || t == 'ET' || t == 'CS') { types[i] = 'ON'; + } } /* @@ -299,14 +300,14 @@ var bidi = PDFJS.bidi = (function bidiClosure() { first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L. */ - var lastType = sor; for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == 'EN') - types[i] = (lastType == 'L') ? 'L' : 'EN'; - else if (t == 'R' || t == 'L') + if (t == 'EN') { + types[i] = ((lastType == 'L') ? 'L' : 'EN'); + } else if (t == 'R' || t == 'L') { lastType = t; + } } /* @@ -315,22 +316,27 @@ var bidi = PDFJS.bidi = (function bidiClosure() { numbers are treated as though they were R. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries. */ - for (var i = 0; i < strLength; ++i) { if (types[i] == 'ON') { var end = findUnequal(types, i + 1, 'ON'); var before = sor; - if (i > 0) + if (i > 0) { before = types[i - 1]; + } + var after = eor; - if (end + 1 < strLength) + if (end + 1 < strLength) { after = types[end + 1]; - if (before != 'L') + } + if (before != 'L') { before = 'R'; - if (after != 'L') + } + if (after != 'L') { after = 'R'; - if (before == after) + } + if (before == after) { setValues(types, i, end, before); + } i = end - 1; // reset to end (-1 so next iteration is ok) } } @@ -338,10 +344,10 @@ var bidi = PDFJS.bidi = (function bidiClosure() { /* N2. Any remaining neutrals take the embedding direction. */ - for (var i = 0; i < strLength; ++i) { - if (types[i] == 'ON') + if (types[i] == 'ON') { types[i] = e; + } } /* @@ -351,7 +357,6 @@ var bidi = PDFJS.bidi = (function bidiClosure() { I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level. */ - for (var i = 0; i < strLength; ++i) { var t = types[i]; if (isEven(levels[i])) { @@ -360,7 +365,7 @@ var bidi = PDFJS.bidi = (function bidiClosure() { } else if (t == 'AN' || t == 'EN') { levels[i] += 2; } - } else { // isOdd, so + } else { // isOdd if (t == 'L' || t == 'AN' || t == 'EN') { levels[i] += 1; } @@ -387,19 +392,19 @@ var bidi = PDFJS.bidi = (function bidiClosure() { */ // find highest level & lowest odd level - var highestLevel = -1; var lowestOddLevel = 99; for (var i = 0, ii = levels.length; i < ii; ++i) { var level = levels[i]; - if (highestLevel < level) + if (highestLevel < level) { highestLevel = level; - if (lowestOddLevel > level && isOdd(level)) + } + if (lowestOddLevel > level && isOdd(level)) { lowestOddLevel = level; + } } // now reverse between those limits - for (var level = highestLevel; level >= lowestOddLevel; --level) { // find segments to reverse var start = -1; @@ -436,14 +441,13 @@ var bidi = PDFJS.bidi = (function bidiClosure() { // don't mirror as characters are already mirrored in the pdf // Finally, return string - var result = ''; for (var i = 0, ii = chars.length; i < ii; ++i) { var ch = chars[i]; - if (ch != '<' && ch != '>') + if (ch != '<' && ch != '>') { result += ch; + } } - return new BidiResult(result, isLTR); }