Making src/core/bidi.js adhere to the style guide
This commit is contained in:
parent
01b23439a9
commit
a615ccdd20
116
src/core/bidi.js
116
src/core/bidi.js
@ -79,9 +79,10 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
function findUnequal(arr, start, value) {
|
function findUnequal(arr, start, value) {
|
||||||
var j;
|
var j;
|
||||||
for (var j = start, jj = arr.length; j < jj; ++j) {
|
for (var j = start, jj = arr.length; j < jj; ++j) {
|
||||||
if (arr[j] != value)
|
if (arr[j] != value) {
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,17 +142,17 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
|
|
||||||
function BidiResult(str, isLTR, vertical) {
|
function BidiResult(str, isLTR, vertical) {
|
||||||
this.str = str;
|
this.str = str;
|
||||||
this.dir = vertical ? 'ttb' : isLTR ? 'ltr' : 'rtl';
|
this.dir = (vertical ? 'ttb' : (isLTR ? 'ltr' : 'rtl'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function bidi(str, startLevel, vertical) {
|
function bidi(str, startLevel, vertical) {
|
||||||
var isLTR = true;
|
var isLTR = true;
|
||||||
var strLength = str.length;
|
var strLength = str.length;
|
||||||
if (strLength === 0 || vertical)
|
if (strLength === 0 || vertical) {
|
||||||
return new BidiResult(str, isLTR, vertical);
|
return new BidiResult(str, isLTR, vertical);
|
||||||
|
}
|
||||||
|
|
||||||
// get types, fill arrays
|
// Get types and fill arrays
|
||||||
|
|
||||||
var chars = [];
|
var chars = [];
|
||||||
var types = [];
|
var types = [];
|
||||||
var numBidi = 0;
|
var numBidi = 0;
|
||||||
@ -161,25 +162,25 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
|
|
||||||
var charCode = str.charCodeAt(i);
|
var charCode = str.charCodeAt(i);
|
||||||
var charType = 'L';
|
var charType = 'L';
|
||||||
if (charCode <= 0x00ff)
|
if (charCode <= 0x00ff) {
|
||||||
charType = baseTypes[charCode];
|
charType = baseTypes[charCode];
|
||||||
else if (0x0590 <= charCode && charCode <= 0x05f4)
|
} else if (0x0590 <= charCode && charCode <= 0x05f4) {
|
||||||
charType = 'R';
|
charType = 'R';
|
||||||
else if (0x0600 <= charCode && charCode <= 0x06ff)
|
} else if (0x0600 <= charCode && charCode <= 0x06ff) {
|
||||||
charType = arabicTypes[charCode & 0xff];
|
charType = arabicTypes[charCode & 0xff];
|
||||||
else if (0x0700 <= charCode && charCode <= 0x08AC)
|
} else if (0x0700 <= charCode && charCode <= 0x08AC) {
|
||||||
charType = 'AL';
|
charType = 'AL';
|
||||||
|
}
|
||||||
if (charType == 'R' || charType == 'AL' || charType == 'AN')
|
if (charType == 'R' || charType == 'AL' || charType == 'AN') {
|
||||||
numBidi++;
|
numBidi++;
|
||||||
|
}
|
||||||
types[i] = charType;
|
types[i] = charType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// detect the bidi method
|
// Detect the bidi method
|
||||||
// if there are no rtl characters then no bidi needed
|
// - If there are no rtl characters then no bidi needed
|
||||||
// if less than 30% chars are rtl then string is primarily ltr
|
// - 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 more than 30% chars are rtl then string is primarily rtl
|
||||||
if (numBidi === 0) {
|
if (numBidi === 0) {
|
||||||
isLTR = true;
|
isLTR = true;
|
||||||
return new BidiResult(str, isLTR);
|
return new BidiResult(str, isLTR);
|
||||||
@ -196,7 +197,6 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var levels = [];
|
var levels = [];
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
levels[i] = startLevel;
|
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.
|
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 sor = e;
|
||||||
var eor = sor;
|
var eor = sor;
|
||||||
|
|
||||||
@ -214,71 +213,73 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
type of the NSM to the type of the previous character. If the NSM is at the
|
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.
|
start of the level run, it will get the type of sor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var lastType = sor;
|
var lastType = sor;
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
if (types[i] == 'NSM')
|
if (types[i] == 'NSM') {
|
||||||
types[i] = lastType;
|
types[i] = lastType;
|
||||||
else
|
} else {
|
||||||
lastType = types[i];
|
lastType = types[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
W2. Search backwards from each instance of a European number until the
|
W2. Search backwards from each instance of a European number until the
|
||||||
first strong type (R, L, AL, or sor) is found. If an AL is found, change
|
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.
|
the type of the European number to Arabic number.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var lastType = sor;
|
var lastType = sor;
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
var t = types[i];
|
var t = types[i];
|
||||||
if (t == 'EN')
|
if (t == 'EN') {
|
||||||
types[i] = (lastType == 'AL') ? 'AN' : '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;
|
lastType = t;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
W3. Change all ALs to R.
|
W3. Change all ALs to R.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
var t = types[i];
|
var t = types[i];
|
||||||
if (t == 'AL')
|
if (t == 'AL') {
|
||||||
types[i] = 'R';
|
types[i] = 'R';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
W4. A single European separator between two European numbers changes to a
|
W4. A single European separator between two European numbers changes to a
|
||||||
European number. A single common separator between two numbers of the same
|
European number. A single common separator between two numbers of the same
|
||||||
type changes to that type:
|
type changes to that type:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 1; i < strLength - 1; ++i) {
|
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';
|
types[i] = 'EN';
|
||||||
|
}
|
||||||
if (types[i] == 'CS' && (types[i - 1] == 'EN' || types[i - 1] == 'AN') &&
|
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];
|
types[i] = types[i - 1];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
W5. A sequence of European terminators adjacent to European numbers changes
|
W5. A sequence of European terminators adjacent to European numbers changes
|
||||||
to all European numbers:
|
to all European numbers:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
if (types[i] == 'EN') {
|
if (types[i] == 'EN') {
|
||||||
// do before
|
// do before
|
||||||
for (var j = i - 1; j >= 0; --j) {
|
for (var j = i - 1; j >= 0; --j) {
|
||||||
if (types[j] != 'ET')
|
if (types[j] != 'ET') {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
types[j] = 'EN';
|
types[j] = 'EN';
|
||||||
}
|
}
|
||||||
// do after
|
// do after
|
||||||
for (var j = i + 1; j < strLength; --j) {
|
for (var j = i + 1; j < strLength; --j) {
|
||||||
if (types[j] != 'ET')
|
if (types[j] != 'ET') {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
types[j] = 'EN';
|
types[j] = 'EN';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,27 +288,27 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
/*
|
/*
|
||||||
W6. Otherwise, separators and terminators change to Other Neutral:
|
W6. Otherwise, separators and terminators change to Other Neutral:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
var t = types[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';
|
types[i] = 'ON';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
W7. Search backwards from each instance of a European number until the
|
W7. Search backwards from each instance of a European number until the
|
||||||
first strong type (R, L, or sor) is found. If an L is found, then change
|
first strong type (R, L, or sor) is found. If an L is found, then change
|
||||||
the type of the European number to L.
|
the type of the European number to L.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var lastType = sor;
|
var lastType = sor;
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
var t = types[i];
|
var t = types[i];
|
||||||
if (t == 'EN')
|
if (t == 'EN') {
|
||||||
types[i] = (lastType == 'L') ? 'L' : 'EN';
|
types[i] = ((lastType == 'L') ? 'L' : 'EN');
|
||||||
else if (t == 'R' || t == 'L')
|
} else if (t == 'R' || t == 'L') {
|
||||||
lastType = t;
|
lastType = t;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
N1. A sequence of neutrals takes the direction of the surrounding strong
|
N1. A sequence of neutrals takes the direction of the surrounding strong
|
||||||
@ -315,22 +316,27 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
numbers are treated as though they were R. Start-of-level-run (sor) and
|
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.
|
end-of-level-run (eor) are used at level run boundaries.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
if (types[i] == 'ON') {
|
if (types[i] == 'ON') {
|
||||||
var end = findUnequal(types, i + 1, 'ON');
|
var end = findUnequal(types, i + 1, 'ON');
|
||||||
var before = sor;
|
var before = sor;
|
||||||
if (i > 0)
|
if (i > 0) {
|
||||||
before = types[i - 1];
|
before = types[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
var after = eor;
|
var after = eor;
|
||||||
if (end + 1 < strLength)
|
if (end + 1 < strLength) {
|
||||||
after = types[end + 1];
|
after = types[end + 1];
|
||||||
if (before != 'L')
|
}
|
||||||
|
if (before != 'L') {
|
||||||
before = 'R';
|
before = 'R';
|
||||||
if (after != 'L')
|
}
|
||||||
|
if (after != 'L') {
|
||||||
after = 'R';
|
after = 'R';
|
||||||
if (before == after)
|
}
|
||||||
|
if (before == after) {
|
||||||
setValues(types, i, end, before);
|
setValues(types, i, end, before);
|
||||||
|
}
|
||||||
i = end - 1; // reset to end (-1 so next iteration is ok)
|
i = end - 1; // reset to end (-1 so next iteration is ok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -338,11 +344,11 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
/*
|
/*
|
||||||
N2. Any remaining neutrals take the embedding direction.
|
N2. Any remaining neutrals take the embedding direction.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
if (types[i] == 'ON')
|
if (types[i] == 'ON') {
|
||||||
types[i] = e;
|
types[i] = e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
I1. For all characters with an even (left-to-right) embedding direction,
|
I1. For all characters with an even (left-to-right) embedding direction,
|
||||||
@ -351,7 +357,6 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
I2. For all characters with an odd (right-to-left) embedding direction,
|
I2. For all characters with an odd (right-to-left) embedding direction,
|
||||||
those of type L, EN or AN go up one level.
|
those of type L, EN or AN go up one level.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (var i = 0; i < strLength; ++i) {
|
for (var i = 0; i < strLength; ++i) {
|
||||||
var t = types[i];
|
var t = types[i];
|
||||||
if (isEven(levels[i])) {
|
if (isEven(levels[i])) {
|
||||||
@ -360,7 +365,7 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
} else if (t == 'AN' || t == 'EN') {
|
} else if (t == 'AN' || t == 'EN') {
|
||||||
levels[i] += 2;
|
levels[i] += 2;
|
||||||
}
|
}
|
||||||
} else { // isOdd, so
|
} else { // isOdd
|
||||||
if (t == 'L' || t == 'AN' || t == 'EN') {
|
if (t == 'L' || t == 'AN' || t == 'EN') {
|
||||||
levels[i] += 1;
|
levels[i] += 1;
|
||||||
}
|
}
|
||||||
@ -387,19 +392,19 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// find highest level & lowest odd level
|
// find highest level & lowest odd level
|
||||||
|
|
||||||
var highestLevel = -1;
|
var highestLevel = -1;
|
||||||
var lowestOddLevel = 99;
|
var lowestOddLevel = 99;
|
||||||
for (var i = 0, ii = levels.length; i < ii; ++i) {
|
for (var i = 0, ii = levels.length; i < ii; ++i) {
|
||||||
var level = levels[i];
|
var level = levels[i];
|
||||||
if (highestLevel < level)
|
if (highestLevel < level) {
|
||||||
highestLevel = level;
|
highestLevel = level;
|
||||||
if (lowestOddLevel > level && isOdd(level))
|
}
|
||||||
|
if (lowestOddLevel > level && isOdd(level)) {
|
||||||
lowestOddLevel = level;
|
lowestOddLevel = level;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// now reverse between those limits
|
// now reverse between those limits
|
||||||
|
|
||||||
for (var level = highestLevel; level >= lowestOddLevel; --level) {
|
for (var level = highestLevel; level >= lowestOddLevel; --level) {
|
||||||
// find segments to reverse
|
// find segments to reverse
|
||||||
var start = -1;
|
var start = -1;
|
||||||
@ -436,14 +441,13 @@ var bidi = PDFJS.bidi = (function bidiClosure() {
|
|||||||
// don't mirror as characters are already mirrored in the pdf
|
// don't mirror as characters are already mirrored in the pdf
|
||||||
|
|
||||||
// Finally, return string
|
// Finally, return string
|
||||||
|
|
||||||
var result = '';
|
var result = '';
|
||||||
for (var i = 0, ii = chars.length; i < ii; ++i) {
|
for (var i = 0, ii = chars.length; i < ii; ++i) {
|
||||||
var ch = chars[i];
|
var ch = chars[i];
|
||||||
if (ch != '<' && ch != '>')
|
if (ch != '<' && ch != '>') {
|
||||||
result += ch;
|
result += ch;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return new BidiResult(result, isLTR);
|
return new BidiResult(result, isLTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user