From d50773fb9638d2a30d630d872f3478c6289c033e Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Thu, 9 Feb 2012 18:40:44 -0800 Subject: [PATCH 01/15] Fixing ToUnicode parsing; workaround for invalid UTF16 encoding --- src/evaluator.js | 14 ++++++++++++-- src/fonts.js | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/evaluator.js b/src/evaluator.js index 1597bed11..e34787e41 100644 --- a/src/evaluator.js +++ b/src/evaluator.js @@ -620,8 +620,18 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { } else { // parsing hex UTF-16BE numbers var str = []; - for (var i = 0, ii = token.length; i < ii; i += 4) - str.push(parseInt(token.substr(i, 4), 16)); + for (var k = 0, kk = token.length; k < kk; k += 4) { + var b = parseInt(token.substr(k, 4), 16); + if (b <= 0x10) { + k += 4; + b = (b << 16) | parseInt(token.substr(k, 4), 16); + b -= 0x10000; + str.push(0xD800 | (b >> 10)); + str.push(0xDC00 | (b & 0x3FF)); + break; + } + str.push(b); + } tokens.push(String.fromCharCode.apply(String, str)); token = ''; } diff --git a/src/fonts.js b/src/fonts.js index f8ae7de4c..aadaa8d71 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -823,6 +823,13 @@ var Font = (function FontClosure() { else this.rebuildToUnicode(properties); + this.toUnicodeOriginal = this.toUnicode.slice(); + for (var i = 0, j = 0xE000; i < this.toUnicode.length; i++) { + if (typeof this.toUnicode[i] == 'number') + break; + this.toUnicode[i] = j++; + } + if (!file) { // The file data is not specified. Trying to fix the font name // to be used with the canvas.font. @@ -1778,7 +1785,7 @@ var Font = (function FontClosure() { for (var i = 1; i < numGlyphs; i++) { var cid = gidToCidMap[i] || i; var unicode = this.toUnicode[cid]; - if (!unicode || isSpecialUnicode(unicode) || + if (!unicode || typeof unicode !== 'number' || isSpecialUnicode(unicode) || unicode in usedUnicodes) { unassignedUnicodeItems.push(i); continue; @@ -1825,7 +1832,7 @@ var Font = (function FontClosure() { var usedUnicodes = [], unassignedUnicodeItems = []; for (var i = 0, ii = glyphs.length; i < ii; i++) { var unicode = toUnicode[i + 1]; - if (!unicode || unicode in usedUnicodes) { + if (!unicode || typeof unicode !== 'number' || unicode in usedUnicodes) { unassignedUnicodeItems.push(i); continue; } @@ -1972,7 +1979,7 @@ var Font = (function FontClosure() { } properties.baseEncoding = encoding; } - if (properties.subtype == 'CIDFontType0C') { + if (false && properties.subtype == 'CIDFontType0C') { var toUnicode = []; for (var i = 0; i < charstrings.length; ++i) { var charstring = charstrings[i]; @@ -2270,8 +2277,8 @@ var Font = (function FontClosure() { break; } - var unicodeChars = !('toUnicode' in this) ? charcode : - this.toUnicode[charcode] || charcode; + var unicodeChars = !('toUnicodeOriginal' in this) ? charcode : + this.toUnicodeOriginal[charcode] || charcode; if (typeof unicodeChars === 'number') unicodeChars = String.fromCharCode(unicodeChars); From 2e1a88f39e46f4131439761800b84bf5b605e83f Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Fri, 10 Feb 2012 16:06:59 +0000 Subject: [PATCH 02/15] Handle bidi ordering of PDF strings --- src/bidi.js | 368 ++++++++++++++++++++++++++++++++++++++++++++++++ src/canvas.js | 17 ++- src/fonts.js | 10 ++ web/viewer.html | 1 + 4 files changed, 393 insertions(+), 3 deletions(-) create mode 100644 src/bidi.js diff --git a/src/bidi.js b/src/bidi.js new file mode 100644 index 000000000..cbb668764 --- /dev/null +++ b/src/bidi.js @@ -0,0 +1,368 @@ +function bidi(doc, str, startLevel) { + if (str.length == 0) return str; + + var chars= new Array(str.length); + var levels = new Array(str.length); + var types = new Array(str.length); + var oldtypes = new Array(str.length); + +// get types, fill arrays + + for (var i = 0; i < str.length; ++i) { + var c = str.charAt(i); + chars[i] = c; + levels[i] = startLevel; + + var t = "L"; + if ('\u0600' <= c && c <= '\u06ff') t = "AL"; + else if ('0' <= c && c <= '9') t = (c <= 5) ? "EN" : "AN"; + else if (c == '.' || c == ',' || c == ':') t = "CS"; + else if (c == '/') t = "ES"; + else if (c == '#' || c == '$' || c == '%' || c == '+' || c == '-') t = "ET" + else if (c == '>') t = "L" + else if (c == '<') t = "R" + + oldtypes[i] = types[i] = t; + } + + var diffChars = new Array(str.length); + var diffLevels = new Array(str.length); + var diffTypes = new Array(str.length); + + showArray(doc, "Chars: ", chars, diffChars); + showArray(doc, "Types: ", types, diffTypes); + //alert("chars: " + chars.join(",")); + //alert("types: " + types.join(",")); + //alert("levels: " + levels.join(",")); + +/* +X1-X10: skip most of this, since we are NOT doing the embeddings. +*/ + + var e = isOdd(startLevel) ? "R" : "L"; + var sor = e; + var eor = sor; + +/* +W1. Examine each non-spacing mark (NSM) in the level run, and change 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. +*/ + + var lastType = sor; + for (var i = 0; i < types.length; ++i) { + if (types[i] == "NSM") types[i] = lastType; + else lastType = types[i]; + } + + showArray(doc, "W1: ", types, diffTypes); + +/* +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 the type of the European number to Arabic number. +*/ + + var lastType = sor; + for (var i = 0; i < types.length; ++i) { + var t = types[i]; + if (t == "EN") types[i] = (lastType == "AL") ? "AN" : "EN"; + else if (t == "R" || t == "L" || t == "AL") lastType = t; + } + + showArray(doc, "W2: ", types, diffTypes); + +/* +W3. Change all ALs to R. +*/ + + for (var i = 0; i < types.length; ++i) { + var t = types[i]; + if (t == "AL") types[i] = "R"; + } + + showArray(doc, "W3: ", types, diffTypes); + +/* +W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type: +*/ + + for (var i = 1; i < types.length - 1; ++i) { + 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] = types[i-1]; + } + + showArray(doc, "W4: ", types, diffTypes); + +/* +W5. A sequence of European terminators adjacent to European numbers changes to all European numbers: +*/ + + for (var i = 0; i < types.length; ++i) { + if (types[i] == "EN") { + // do before + for (j = i-1; j >= 0; --j) { + if (types[j] == "ET") types[j] = "EN"; + else break; + } + // do after + for (j = i+1; j < types.length; --j) { + if (types[j] == "ET") types[j] = "EN"; + else break; + } + } + } + + showArray(doc, "W5: ", types, diffTypes); + + +/* +W6. Otherwise, separators and terminators change to Other Neutral: +*/ + + for (var i = 0; i < types.length; ++i) { + var t = types[i]; + if (t == "ES" || t == "ET" || t == "CS") types[i] = "ON"; + } + + showArray(doc, "W6: ", types, diffTypes); + +/* +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 the type of the European number to L. +*/ + + var lastType = sor; + for (var i = 0; i < types.length; ++i) { + var t = types[i]; + if (t == "EN") types[i] = (lastType == "L") ? "L" : "EN"; + else if (t == "R" || t == "L") lastType = t; + } + + showArray(doc, "W7: ", types, diffTypes); + +/* +N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic 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 < types.length; ++i) { + if (types[i] == "ON") { + var end = findUnequal(types, i+1, "ON"); + var before = sor; + if (i > 0) before = types[i-1]; + var after = eor; + if (end+1 < types.length) after = types[end+1]; + if (before != "L") before = "R"; + if (after != "L") after = "R"; + if (before == after) setValues(types, i, end, before); + i = end - 1; // reset to end (-1 so next iteration is ok) + } + } + + showArray(doc, "N1: ", types, diffTypes); + +/* +N2. Any remaining neutrals take the embedding direction. +*/ + + for (var i = 0; i < types.length; ++i) { + if (types[i] == "ON") types[i] = e; + } + + showArray(doc, "N2: ", types, diffTypes); + +/* +I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels. +I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level. +*/ + + showArray(doc, "Levels: ", levels, diffLevels); + + for (var i = 0; i < types.length; ++i) { + var t = types[i]; + if (isEven(levels[i])) { + if (t == "R") { + levels[i] += 1; + } else if (t == "AN" || t == "EN") { + levels[i] += 2; + } + } else { // isOdd, so + if (t == "L" || t == "AN" || t == "EN") { + levels[i] += 1; + } + } + } + + showArray(doc, "I1/2: ", levels, diffLevels); + + +/* +L1. On each line, reset the embedding level of the following characters to the paragraph embedding level: + +segment separators, +paragraph separators, +any sequence of whitespace characters preceding a segment separator or paragraph separator, and +any sequence of white space characters at the end of the line. +*/ + +/* + boolean atEnd = true; + for (var i = levels.length - 1; i >= 0; --i) { + var t = oldTypes[i]; + if (t == "B" || t == "S") { + levels[i] = startLevel; + atEnd = true; + } else if (atEnd && t == "WS") { + levels[i] = startLevel; + } + } +*/ + + showArray(doc, "L1: ", levels, diffLevels); + + +/* +L2. From the highest level found in the text to the lowest odd level on each line, reverse any contiguous sequence of characters that are at that level or higher. +*/ + + // find highest level & lowest odd level + + var highestLevel = -1; + var lowestOddLevel = 99; + // alert("Levels: " + levels.join(",")); + for (var i = 0; i < levels.length; ++i) { + var level = levels[i]; + if (highestLevel < level) highestLevel = level; + if (lowestOddLevel > level && isOdd(level)) lowestOddLevel = level; + } + // alert("highestLevel: " + highestLevel + "; lowestOddLevel: " + lowestOddLevel); + + // now reverse between those limits + + for (var level = highestLevel; level >= lowestOddLevel; --level) { + // find segments to reverse + var start = -1; + for (var i = 0; i < levels.length; ++i) { + if (levels[i] < level) { + if (start >= 0) { + reverseValues(chars, start, i); + start = -1; + } + } else if (start < 0) { + start = i; + } + } + if (start >= 0) { + reverseValues(chars, start, levels.length); + } + + showArray(doc, "L2 (" + level + "):", chars, diffChars); + + } + + +/* +L3. Combining marks applied to a right-to-left base character will at this point precede their base character. If the rendering engine expects them to follow the base characters in the final display process, then the ordering of the marks and the base character must be reversed. +*/ + +// don't bother for now + + showArray(doc, "L3: ", chars, diffChars); + + +/* +L4. A character that possesses the mirrored property as specified by Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved directionality of that character is R. +*/ + + for (var i = 0; i < chars.length; ++i) { + chars[i] = mirrorGlyphs(chars[i]); + } + + showArray(doc, "L4: ", chars, diffChars); + +// Finally, return string + + var result = ""; + for (var i = 0; i < chars.length; ++i) { + var ch = chars[i]; + if (ch != '<' && ch != '>') result += ch; + } + return result; +} + + +// UTILITIES + +function isOdd(i) { + return (i & 1) != 0; +} + +function isEven(i) { + return (i & 1) == 0; +} + +function findUnequal(arr, start, value) { + var j; + for (var j = start; j < arr.length; ++j) { + if (arr[j] != value) return j; + } + return j; +} + +function setValues(arr, start, end, value) { + for (var j = start; j < end; ++j) { + arr[j] = value; + } +} + +function reverseValues(arr, start, end) { + // alert("reverse: " + arr.join(",") + "; " + start + "; " + end); + for (var i = start, j = end-1; i < j; ++i, --j) { + var temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + // alert("reverse: " + chars.join(",")); +} + +function mirrorGlyphs(c) { +/* +# BidiMirroring-1.txt +0028; 0029 # LEFT PARENTHESIS +0029; 0028 # RIGHT PARENTHESIS +003C; 003E # LESS-THAN SIGN +003E; 003C # GREATER-THAN SIGN +005B; 005D # LEFT SQUARE BRACKET +005D; 005B # RIGHT SQUARE BRACKET +007B; 007D # LEFT CURLY BRACKET +007D; 007B # RIGHT CURLY BRACKET +00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK +00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK +*/ + switch (c) { + case '(': return ')'; + case ')': return '('; + case '<': return '>'; + case '>': return '<'; + case ']': return '['; + case '[': return ']'; + case '}': return '{'; + case '{': return '}'; + case '�': return '�'; + case '�': return '�'; + default: return c; + } +} + +function showArray(doc, title, arr, diffarr) { + if (doc == null) + return; + var haveDiff = false; + for (var i = 0; i < arr.length; ++i) { + if (arr[i] != diffarr[i]) { + haveDiff = true; + diffarr[i] = arr[i]; + } + } + if (haveDiff) { + doc.writeln("", title, "", arr.join(""), ""); + } +} + + diff --git a/src/canvas.js b/src/canvas.js index f4815a655..1106ba328 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -749,8 +749,16 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { x += charWidth; - text.str += glyph.unicode === ' ' ? '\u00A0' : glyph.unicode; - text.length++; + var glyphUnicode = glyph.unicode === ' ' ? '\u00A0' : glyph.unicode; + var glyphUniLen = glyphUnicode.length; + //reverse an arabic ligature + if (glyphUniLen > 1 && isRTLRangeFor(glyphUnicode.charCodeAt(0))) + { + for (var ii = glyphUniLen - 1; ii >= 0; ii--) + text.str += glyphUnicode[ii]; + } else + text.str += glyphUnicode; + text.length += glyphUniLen; text.canvasWidth += charWidth; } current.x += x * textHScale2; @@ -823,8 +831,11 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } } - if (textSelection) + if (textSelection) { + if (isRTLRangeFor(text.str.charCodeAt(0))) //first char is rtl + text.str = bidi(null, text.str, 1); this.textLayer.appendText(text, font.loadedName, fontSize); + } }, nextLineShowText: function canvasGraphicsNextLineShowText(text) { this.nextLine(); diff --git a/src/fonts.js b/src/fonts.js index aadaa8d71..45c83b74d 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -763,6 +763,16 @@ function getUnicodeRangeFor(value) { return -1; } +function isRTLRangeFor(value) { + var range = UnicodeRanges[13]; + if (value >= range.begin && value < range.end) + return true; + range = UnicodeRanges[11]; + if (value >= range.begin && value < range.end) + return true; + return false; +} + function isSpecialUnicode(unicode) { return (unicode <= 0x1F || (unicode >= 127 && unicode < kSizeOfGlyphArea)) || (unicode >= kCmapGlyphOffset && diff --git a/web/viewer.html b/web/viewer.html index 7c55ec735..85b267516 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -27,6 +27,7 @@ + From ffa0e082a4338d3cb92bf70935dfae7d68e586e1 Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Sun, 12 Feb 2012 22:31:30 +0000 Subject: [PATCH 03/15] Scale text divs instead of letterspacing --- web/viewer.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index d5162a194..9cf97a751 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -1041,12 +1041,11 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) { textLayerDiv.appendChild(textDiv); if (textDiv.dataset.textLength > 1) { // avoid div by zero - // Adjust div width (via letterSpacing) to match canvas text + // Adjust div width to match canvas text // Due to the .offsetWidth calls, this is slow // This needs to come after appending to the DOM - textDiv.style.letterSpacing = - ((textDiv.dataset.canvasWidth - textDiv.offsetWidth) / - (textDiv.dataset.textLength - 1)) + 'px'; + textDiv.style.MozTransform = 'scale(' + textDiv.dataset.canvasWidth/textDiv.offsetWidth + ',1)'; + textDiv.style.MozTransformOrigin = '0% 0%'; } } // textLength > 0 } From 2bc708305d361b56e6e9b079c73ef199cff6c917 Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Mon, 13 Feb 2012 02:10:41 +0000 Subject: [PATCH 04/15] Fix up bidi algorithm, and set direction of div to match text direction --- src/bidi.js | 494 +++++++++++++++++++++++++++++--------------------- src/canvas.js | 7 +- web/viewer.js | 3 +- 3 files changed, 290 insertions(+), 214 deletions(-) diff --git a/src/bidi.js b/src/bidi.js index cbb668764..63ca2dde2 100644 --- a/src/bidi.js +++ b/src/bidi.js @@ -1,237 +1,327 @@ -function bidi(doc, str, startLevel) { - if (str.length == 0) return str; +var baseTypes = [ + "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "S", "B", "S", "WS", + "B", "BN", "BN", /*U+000*/ + "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "B", + "B", "B", "S", /*U+001*/ + "WS", "ON", "ON", "ET", "ET", "ET", "ON", "ON", "ON", "ON", "ON", "ON", "CS", + "ON", "CS", "ON", /*U+002*/ + "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "ON", "ON", "ON", + "ON", "ON", "ON", /*U+003*/ + "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", + "L", /*U+004*/ + "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON", + "ON", /*U+005*/ + "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", + "L", /*U+006*/ + "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON", + "BN", /*U+007*/ + "BN", "BN", "BN", "BN", "BN", "B", "BN", "BN", "BN", "BN", "BN", "BN", "BN", + "BN", "BN", "BN", /*U+008*/ + "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", + "BN", "BN", "BN", /*U+009*/ + "CS", "ON", "ET", "ET", "ET", "ET", "ON", "ON", "ON", "ON", "L", "ON", "ON", + "ON", "ON", "ON", /*U+00a*/ + "ET", "ET", "EN", "EN", "ON", "L", "ON", "ON", "ON", "EN", "L", "ON", "ON", + "ON", "ON", "ON", /*U+00b*/ + "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", + "L", /*U+00c*/ + "L", "L", "L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", + "L", /*U+00d*/ + "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", + "L", /*U+00e*/ + "L", "L", "L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", + "L" /*U+00f*/ +]; - var chars= new Array(str.length); - var levels = new Array(str.length); - var types = new Array(str.length); - var oldtypes = new Array(str.length); +var arabicTypes = [ + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "CS", + "AL", "ON", "ON", //60 + "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", "AL", //61 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //62 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //63 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "NSM", + "NSM", "NSM", "NSM", "NSM", //64 + "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", + "AL", "AL", "AL", "AL", "AL", //65 + "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "ET", "AN", "AN", + "AL", "AL", "AL", //66 + "NSM", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //67 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //68 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //69 + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //6a + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //6b + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL", //6c + "AL", "AL", "AL", "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", + "NSM", "NSM", "NSM", "NSM", //6d + "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "ON", "NSM", + "NSM", "NSM", "NSM", "AL", "AL", //6e + "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", + "AL", "AL", "AL" //6f +]; -// get types, fill arrays +function bidi(text, startLevel) { + var str = text.str; + var strLength = str.length; + if (strLength == 0) return str; - for (var i = 0; i < str.length; ++i) { + // get types, fill arrays + + var chars = new Array(strLength); + var types = new Array(strLength); + var oldtypes = new Array(strLength); + var numBidi = 0; + + for (var i = 0; i < strLength; ++i) { var c = str.charAt(i); chars[i] = c; - levels[i] = startLevel; var t = "L"; - if ('\u0600' <= c && c <= '\u06ff') t = "AL"; - else if ('0' <= c && c <= '9') t = (c <= 5) ? "EN" : "AN"; - else if (c == '.' || c == ',' || c == ':') t = "CS"; - else if (c == '/') t = "ES"; - else if (c == '#' || c == '$' || c == '%' || c == '+' || c == '-') t = "ET" - else if (c == '>') t = "L" - else if (c == '<') t = "R" + if (c <= '\u00ff') + t = baseTypes[c.charCodeAt(0)]; + else if ('\u0590' <= c && c <= '\u05f4') + t = "R"; + else if ('\u0600' <= c && c <= '\u06ff') + t = arabicTypes[c.charCodeAt(0) & 0xff]; + else if ('\u0700' <= c && c <= '\u08AC') + t = "AL"; + + if (t == "R" || t == "AL" || t == "AN") + numBidi++; oldtypes[i] = types[i] = t; } - var diffChars = new Array(str.length); - var diffLevels = new Array(str.length); - var diffTypes = new Array(str.length); + // 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) { + text.direction = 'ltr'; + return str; + } + else if (startLevel == -1) { + if ((strLength / numBidi) < 0.3) { + text.direction = 'ltr'; + startLevel = 0; + } else { + text.direction = 'rtl'; + startLevel = 1; + } + } - showArray(doc, "Chars: ", chars, diffChars); - showArray(doc, "Types: ", types, diffTypes); - //alert("chars: " + chars.join(",")); - //alert("types: " + types.join(",")); - //alert("levels: " + levels.join(",")); + var levels = new Array(strLength); -/* -X1-X10: skip most of this, since we are NOT doing the embeddings. -*/ + for (var i = 0; i < strLength; ++i) { + levels[i] = startLevel; + } + + var diffChars = new Array(strLength); + var diffLevels = new Array(strLength); + var diffTypes = new Array(strLength); + + /* + X1-X10: skip most of this, since we are NOT doing the embeddings. + */ var e = isOdd(startLevel) ? "R" : "L"; var sor = e; var eor = sor; -/* -W1. Examine each non-spacing mark (NSM) in the level run, and change 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. -*/ + /* + W1. Examine each non-spacing mark (NSM) in the level run, and change 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. + */ var lastType = sor; - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { if (types[i] == "NSM") types[i] = lastType; else lastType = types[i]; } - showArray(doc, "W1: ", types, diffTypes); - -/* -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 the type of the European number to Arabic number. -*/ + /* + 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 the type + of the European number to Arabic number. + */ var lastType = sor; - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == "EN") types[i] = (lastType == "AL") ? "AN" : "EN"; - else if (t == "R" || t == "L" || t == "AL") lastType = t; + if (t == "EN") + types[i] = (lastType == "AL") ? "AN" : "EN"; + else if (t == "R" || t == "L" || t == "AL") + lastType = t; } - showArray(doc, "W2: ", types, diffTypes); + /* + W3. Change all ALs to R. + */ -/* -W3. Change all ALs to R. -*/ - - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { var t = types[i]; if (t == "AL") types[i] = "R"; } - showArray(doc, "W3: ", types, diffTypes); + /* + W4. A single European separator between two European numbers changes to a + European number. A single common separator between two numbers of the same + type changes to that type: + */ -/* -W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type: -*/ - - for (var i = 1; i < types.length - 1; ++i) { - 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] = types[i-1]; + for (var i = 1; i < strLength - 1; ++i) { + 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] = types[i - 1]; } - showArray(doc, "W4: ", types, diffTypes); + /* + W5. A sequence of European terminators adjacent to European numbers changes + to all European numbers: + */ -/* -W5. A sequence of European terminators adjacent to European numbers changes to all European numbers: -*/ - - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { if (types[i] == "EN") { // do before - for (j = i-1; j >= 0; --j) { - if (types[j] == "ET") types[j] = "EN"; + for (j = i - 1; j >= 0; --j) { + if (types[j] == "ET") + types[j] = "EN"; else break; } // do after - for (j = i+1; j < types.length; --j) { - if (types[j] == "ET") types[j] = "EN"; + for (j = i + 1; j < strLength; --j) { + if (types[j] == "ET") + types[j] = "EN"; else break; } } } - showArray(doc, "W5: ", types, diffTypes); + /* + W6. Otherwise, separators and terminators change to Other Neutral: + */ - -/* -W6. Otherwise, separators and terminators change to Other Neutral: -*/ - - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == "ES" || t == "ET" || t == "CS") types[i] = "ON"; + if (t == "WS" || t == "ES" || t == "ET" || t == "CS") + types[i] = "ON"; } - showArray(doc, "W6: ", types, diffTypes); - -/* -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 the type of the European number to L. -*/ + /* + 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 the type + of the European number to L. + */ var lastType = sor; - for (var i = 0; i < types.length; ++i) { + 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") lastType = t; + if (t == "EN") + types[i] = (lastType == "L") ? "L" : "EN"; + else if (t == "R" || t == "L") + lastType = t; } - showArray(doc, "W7: ", types, diffTypes); + /* + N1. A sequence of neutrals takes the direction of the surrounding strong text + if the text on both sides has the same direction. European and Arabic 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. + */ -/* -N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic 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 < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { if (types[i] == "ON") { - var end = findUnequal(types, i+1, "ON"); + var end = findUnequal(types, i + 1, "ON"); var before = sor; - if (i > 0) before = types[i-1]; + if (i > 0) + before = types[i - 1]; var after = eor; - if (end+1 < types.length) after = types[end+1]; - if (before != "L") before = "R"; - if (after != "L") after = "R"; - if (before == after) setValues(types, i, end, before); + if (end + 1 < strLength) + after = types[end + 1]; + if (before != "L") + before = "R"; + if (after != "L") + after = "R"; + if (before == after) + setValues(types, i, end, before); i = end - 1; // reset to end (-1 so next iteration is ok) } } - showArray(doc, "N1: ", types, diffTypes); + /* + N2. Any remaining neutrals take the embedding direction. + */ -/* -N2. Any remaining neutrals take the embedding direction. -*/ - - for (var i = 0; i < types.length; ++i) { - if (types[i] == "ON") types[i] = e; + for (var i = 0; i < strLength; ++i) { + if (types[i] == "ON") + types[i] = e; } - showArray(doc, "N2: ", types, diffTypes); + /* + I1. For all characters with an even (left-to-right) embedding direction, + those of type R go up one level and those of type AN or EN go up two levels. + I2. For all characters with an odd (right-to-left) embedding direction, those + of type L, EN or AN go up one level. + */ -/* -I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels. -I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level. -*/ - - showArray(doc, "Levels: ", levels, diffLevels); - - for (var i = 0; i < types.length; ++i) { + for (var i = 0; i < strLength; ++i) { var t = types[i]; if (isEven(levels[i])) { if (t == "R") { levels[i] += 1; - } else if (t == "AN" || t == "EN") { + } + else if (t == "AN" || t == "EN") { levels[i] += 2; } - } else { // isOdd, so + } + else { // isOdd, so if (t == "L" || t == "AN" || t == "EN") { levels[i] += 1; } } } - showArray(doc, "I1/2: ", levels, diffLevels); + /* + L1. On each line, reset the embedding level of the following characters to + the paragraph embedding level: + segment separators, + paragraph separators, + any sequence of whitespace characters preceding a segment separator or + paragraph separator, and any sequence of white space characters at the end + of the line. + */ -/* -L1. On each line, reset the embedding level of the following characters to the paragraph embedding level: + //dont bother as text is only single line -segment separators, -paragraph separators, -any sequence of whitespace characters preceding a segment separator or paragraph separator, and -any sequence of white space characters at the end of the line. -*/ - -/* - boolean atEnd = true; - for (var i = levels.length - 1; i >= 0; --i) { - var t = oldTypes[i]; - if (t == "B" || t == "S") { - levels[i] = startLevel; - atEnd = true; - } else if (atEnd && t == "WS") { - levels[i] = startLevel; - } - } -*/ - - showArray(doc, "L1: ", levels, diffLevels); - - -/* -L2. From the highest level found in the text to the lowest odd level on each line, reverse any contiguous sequence of characters that are at that level or higher. -*/ + /* + L2. From the highest level found in the text to the lowest odd level on each + line, reverse any contiguous sequence of characters that are at that level or + higher. + */ // find highest level & lowest odd level var highestLevel = -1; var lowestOddLevel = 99; - // alert("Levels: " + levels.join(",")); for (var i = 0; i < levels.length; ++i) { var level = levels[i]; - if (highestLevel < level) highestLevel = level; - if (lowestOddLevel > level && isOdd(level)) lowestOddLevel = level; + if (highestLevel < level) + highestLevel = level; + if (lowestOddLevel > level && isOdd(level)) + lowestOddLevel = level; } - // alert("highestLevel: " + highestLevel + "; lowestOddLevel: " + lowestOddLevel); // now reverse between those limits @@ -244,49 +334,44 @@ L2. From the highest level found in the text to the lowest odd level on each lin reverseValues(chars, start, i); start = -1; } - } else if (start < 0) { + } + else if (start < 0) { start = i; } } if (start >= 0) { reverseValues(chars, start, levels.length); } - - showArray(doc, "L2 (" + level + "):", chars, diffChars); - } + /* + L3. Combining marks applied to a right-to-left base character will at this + point precede their base character. If the rendering engine expects them to + follow the base characters in the final display process, then the ordering of + the marks and the base character must be reversed. + */ -/* -L3. Combining marks applied to a right-to-left base character will at this point precede their base character. If the rendering engine expects them to follow the base characters in the final display process, then the ordering of the marks and the base character must be reversed. -*/ + // don't bother for now -// don't bother for now + /* + L4. A character that possesses the mirrored property as specified by + Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved + directionality of that character is R. + */ - showArray(doc, "L3: ", chars, diffChars); + //dont mirror as this is already in the pdf - -/* -L4. A character that possesses the mirrored property as specified by Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved directionality of that character is R. -*/ - - for (var i = 0; i < chars.length; ++i) { - chars[i] = mirrorGlyphs(chars[i]); - } - - showArray(doc, "L4: ", chars, diffChars); - -// Finally, return string + // Finally, return string var result = ""; for (var i = 0; i < chars.length; ++i) { var ch = chars[i]; - if (ch != '<' && ch != '>') result += ch; + if (ch != '<' && ch != '>') + result += ch; } return result; } - // UTILITIES function isOdd(i) { @@ -312,57 +397,50 @@ function setValues(arr, start, end, value) { } function reverseValues(arr, start, end) { - // alert("reverse: " + arr.join(",") + "; " + start + "; " + end); - for (var i = start, j = end-1; i < j; ++i, --j) { + for (var i = start, j = end - 1; i < j; ++i, --j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } - // alert("reverse: " + chars.join(",")); } function mirrorGlyphs(c) { -/* -# BidiMirroring-1.txt -0028; 0029 # LEFT PARENTHESIS -0029; 0028 # RIGHT PARENTHESIS -003C; 003E # LESS-THAN SIGN -003E; 003C # GREATER-THAN SIGN -005B; 005D # LEFT SQUARE BRACKET -005D; 005B # RIGHT SQUARE BRACKET -007B; 007D # LEFT CURLY BRACKET -007D; 007B # RIGHT CURLY BRACKET -00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK -00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK -*/ + /* + # BidiMirroring-1.txt + 0028; 0029 # LEFT PARENTHESIS + 0029; 0028 # RIGHT PARENTHESIS + 003C; 003E # LESS-THAN SIGN + 003E; 003C # GREATER-THAN SIGN + 005B; 005D # LEFT SQUARE BRACKET + 005D; 005B # RIGHT SQUARE BRACKET + 007B; 007D # LEFT CURLY BRACKET + 007D; 007B # RIGHT CURLY BRACKET + 00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK + 00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK + */ switch (c) { - case '(': return ')'; - case ')': return '('; - case '<': return '>'; - case '>': return '<'; - case ']': return '['; - case '[': return ']'; - case '}': return '{'; - case '{': return '}'; - case '�': return '�'; - case '�': return '�'; - default: return c; + case '(': + return ')'; + case ')': + return '('; + case '<': + return '>'; + case '>': + return '<'; + case ']': + return '['; + case '[': + return ']'; + case '}': + return '{'; + case '{': + return '}'; + case '�': + return '�'; + case '�': + return '�'; + default: + return c; } } -function showArray(doc, title, arr, diffarr) { - if (doc == null) - return; - var haveDiff = false; - for (var i = 0; i < arr.length; ++i) { - if (arr[i] != diffarr[i]) { - haveDiff = true; - diffarr[i] = arr[i]; - } - } - if (haveDiff) { - doc.writeln("", title, "", arr.join(""), ""); - } -} - - diff --git a/src/canvas.js b/src/canvas.js index 1106ba328..499a26446 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -824,18 +824,15 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { text.str += shownText.str; } text.canvasWidth += shownText.canvasWidth; - text.length += e.length; + text.length += shownText.length; } } else { malformed('TJ array element ' + e + ' is not string or num'); } } - if (textSelection) { - if (isRTLRangeFor(text.str.charCodeAt(0))) //first char is rtl - text.str = bidi(null, text.str, 1); + if (textSelection) this.textLayer.appendText(text, font.loadedName, fontSize); - } }, nextLineShowText: function canvasGraphicsNextLineShowText(text) { this.nextLine(); diff --git a/web/viewer.js b/web/viewer.js index 9cf97a751..bbb192a9a 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -1084,7 +1084,8 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) { textDiv.style.fontSize = fontHeight + 'px'; textDiv.style.left = text.geom.x + 'px'; textDiv.style.top = (text.geom.y - fontHeight) + 'px'; - textDiv.textContent = text.str; + textDiv.textContent = bidi(text, -1); + textDiv.dir = text.direction; textDiv.dataset.textLength = text.length; this.textDivs.push(textDiv); }; From 6902ba51fac5237d1a9bff5ad33ca3b23f093087 Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Mon, 13 Feb 2012 14:27:59 +0000 Subject: [PATCH 05/15] Properly support custom CSS properties Fix comment in bidi --- src/bidi.js | 2 +- src/util.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ web/viewer.js | 6 ++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/bidi.js b/src/bidi.js index 63ca2dde2..09f8dd7c6 100644 --- a/src/bidi.js +++ b/src/bidi.js @@ -359,7 +359,7 @@ function bidi(text, startLevel) { directionality of that character is R. */ - //dont mirror as this is already in the pdf + //dont mirror as characters are already mirrored in the pdf // Finally, return string diff --git a/src/util.js b/src/util.js index f00fcd1ce..3af6125e3 100644 --- a/src/util.js +++ b/src/util.js @@ -118,6 +118,57 @@ var Util = (function UtilClosure() { return Util; })(); +// optimised CSS custom property getter/setter +var CustomStyle = (function CustomStyleClosure() { + + // As noted on: http://www.zachstronaut.com/posts/2009/02/17/ + // animate-css-transforms-firefox-webkit.html + // in some versions of IE9 it is critical that ms appear in this list + // before Moz + var prefixes = ['ms', 'Moz', 'Webkit', 'O']; + var _cache = { }; + + function CustomStyle() { + } + + CustomStyle.getProp = function get(propName, element) { + // check cache only when no element is given + if (arguments.length == 1 && typeof _cache[propName] == 'string') { + return _cache[propName]; + } + + element = element || document.documentElement; + var style = element.style, prefixed, uPropName; + + // test standard property first + if (typeof style[propName] == 'string') { + return (_cache[propName] = propName); + } + + // capitalize + uPropName = propName.charAt(0).toUpperCase() + propName.slice(1); + + // test vendor specific properties + for (var i = 0, l = prefixes.length; i < l; i++) { + prefixed = prefixes[i] + uPropName; + if (typeof style[prefixed] == 'string') { + return (_cache[propName] = prefixed); + } + } + + //if all fails then set to undefined + return (_cache[propName] = 'undefined'); + } + + CustomStyle.setProp = function set(propName, element, str) { + var prop = this.getProp(propName); + if (prop != 'undefined') + element.style[prop] = str; + } + + return CustomStyle; +})(); + var PDFStringTranslateTable = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC, 0, 0, 0, 0, 0, 0, 0, diff --git a/web/viewer.js b/web/viewer.js index bbb192a9a..ff857a943 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -1044,8 +1044,10 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) { // Adjust div width to match canvas text // Due to the .offsetWidth calls, this is slow // This needs to come after appending to the DOM - textDiv.style.MozTransform = 'scale(' + textDiv.dataset.canvasWidth/textDiv.offsetWidth + ',1)'; - textDiv.style.MozTransformOrigin = '0% 0%'; + CustomStyle.setProp('transform' , textDiv, 'scale(' + + textDiv.dataset.canvasWidth/textDiv.offsetWidth + + ',1)'); + CustomStyle.setProp('transformOrigin' , textDiv, '0% 0%'); } } // textLength > 0 } From b50cf76ab589fa1bb4a52014470274f887e067cd Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Mon, 13 Feb 2012 14:38:44 +0000 Subject: [PATCH 06/15] Properly integrate new file bidi.js --- Makefile | 1 + src/worker_loader.js | 3 ++- test/test_slave.html | 1 + test/unit/jsTestDriver.conf | 1 + web/viewer.html | 2 +- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 043cb7fb5..f515bf717 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ PDF_JS_FILES = \ worker.js \ ../external/jpgjs/jpg.js \ jpx.js \ + bidi.js \ $(NULL) # make server diff --git a/src/worker_loader.js b/src/worker_loader.js index fc3d7a5f7..69eb1414f 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -24,7 +24,8 @@ var files = [ 'stream.js', 'worker.js', '../external/jpgjs/jpg.js', - 'jpx.js' + 'jpx.js', + 'bidi.js' ]; // Load all the files. diff --git a/test/test_slave.html b/test/test_slave.html index a2682a5a6..50bb067e1 100644 --- a/test/test_slave.html +++ b/test/test_slave.html @@ -23,6 +23,7 @@ + - + From c8e88def836588302f6ffc4742cce5d378041b5f Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 13 Feb 2012 20:28:36 -0600 Subject: [PATCH 07/15] Light bidi fixes --- src/bidi.js | 219 ++++++++++++++++++++++++---------------------------- 1 file changed, 100 insertions(+), 119 deletions(-) diff --git a/src/bidi.js b/src/bidi.js index 09f8dd7c6..025847ea9 100644 --- a/src/bidi.js +++ b/src/bidi.js @@ -1,71 +1,54 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +'use strict'; + +// Character types for symbols from 0000 to 00FF. var baseTypes = [ - "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "S", "B", "S", "WS", - "B", "BN", "BN", /*U+000*/ - "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "B", - "B", "B", "S", /*U+001*/ - "WS", "ON", "ON", "ET", "ET", "ET", "ON", "ON", "ON", "ON", "ON", "ON", "CS", - "ON", "CS", "ON", /*U+002*/ - "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "EN", "ON", "ON", "ON", - "ON", "ON", "ON", /*U+003*/ - "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", - "L", /*U+004*/ - "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON", - "ON", /*U+005*/ - "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", - "L", /*U+006*/ - "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON", "ON", "ON", - "BN", /*U+007*/ - "BN", "BN", "BN", "BN", "BN", "B", "BN", "BN", "BN", "BN", "BN", "BN", "BN", - "BN", "BN", "BN", /*U+008*/ - "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", - "BN", "BN", "BN", /*U+009*/ - "CS", "ON", "ET", "ET", "ET", "ET", "ON", "ON", "ON", "ON", "L", "ON", "ON", - "ON", "ON", "ON", /*U+00a*/ - "ET", "ET", "EN", "EN", "ON", "L", "ON", "ON", "ON", "EN", "L", "ON", "ON", - "ON", "ON", "ON", /*U+00b*/ - "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", - "L", /*U+00c*/ - "L", "L", "L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", - "L", /*U+00d*/ - "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", - "L", /*U+00e*/ - "L", "L", "L", "L", "L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", - "L" /*U+00f*/ + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'S', 'B', 'S', 'WS', + 'B', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', + 'BN', 'BN', 'B', 'B', 'B', 'S', 'WS', 'ON', 'ON', 'ET', 'ET', 'ET', 'ON', + 'ON', 'ON', 'ON', 'ON', 'ON', 'CS', 'ON', 'CS', 'ON', 'EN', 'EN', 'EN', 'EN', + 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', 'ON', 'ON', + 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', + 'ON', 'ON', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'B', 'BN', 'BN', 'BN', 'BN', + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'CS', 'ON', 'ET', 'ET', + 'ET', 'ET', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'ET', + 'ET', 'EN', 'EN', 'ON', 'L', 'ON', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', + 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L' ]; +// Character types for symbols from 0600 to 06FF var arabicTypes = [ - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "CS", - "AL", "ON", "ON", //60 - "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", "AL", //61 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //62 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //63 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "NSM", - "NSM", "NSM", "NSM", "NSM", //64 - "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", - "AL", "AL", "AL", "AL", "AL", //65 - "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "ET", "AN", "AN", - "AL", "AL", "AL", //66 - "NSM", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //67 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //68 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //69 - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //6a - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //6b - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL", //6c - "AL", "AL", "AL", "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", - "NSM", "NSM", "NSM", "NSM", //6d - "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "ON", "NSM", - "NSM", "NSM", "NSM", "AL", "AL", //6e - "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", - "AL", "AL", "AL" //6f + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'CS', + 'AL', 'ON', 'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'NSM', + 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', + 'NSM', 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AN', 'AN', 'AN', + 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'ET', 'AN', 'AN', 'AL', 'AL', 'AL', + 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'NSM', + 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', + 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'ON', 'NSM', 'NSM', 'NSM', + 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL' ]; function bidi(text, startLevel) { @@ -81,23 +64,23 @@ function bidi(text, startLevel) { var numBidi = 0; for (var i = 0; i < strLength; ++i) { - var c = str.charAt(i); - chars[i] = c; + chars[i] = str.charAt(i); - var t = "L"; - if (c <= '\u00ff') - t = baseTypes[c.charCodeAt(0)]; - else if ('\u0590' <= c && c <= '\u05f4') - t = "R"; - else if ('\u0600' <= c && c <= '\u06ff') - t = arabicTypes[c.charCodeAt(0) & 0xff]; - else if ('\u0700' <= c && c <= '\u08AC') - t = "AL"; + var charCode = str.charCodeAt(i); + var charType = 'L'; + if (charCode <= 0x00ff) + charType = baseTypes[charCode]; + else if (0x0590 <= charCode && charCode <= 0x05f4) + charType = 'R'; + else if (0x0600 <= charCode && charCode <= 0x06ff) + charType = arabicTypes[charCode & 0xff]; + else if (0x0700 <= charCode && charCode <= 0x08AC) + charType = 'AL'; - if (t == "R" || t == "AL" || t == "AN") + if (charType == 'R' || charType == 'AL' || charType == 'AN') numBidi++; - oldtypes[i] = types[i] = t; + oldtypes[i] = types[i] = charType; } // detect the bidi method @@ -132,7 +115,7 @@ function bidi(text, startLevel) { 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; @@ -144,7 +127,7 @@ function bidi(text, startLevel) { var lastType = sor; for (var i = 0; i < strLength; ++i) { - if (types[i] == "NSM") types[i] = lastType; + if (types[i] == 'NSM') types[i] = lastType; else lastType = types[i]; } @@ -157,9 +140,9 @@ function bidi(text, startLevel) { var lastType = sor; for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == "EN") - types[i] = (lastType == "AL") ? "AN" : "EN"; - else if (t == "R" || t == "L" || t == "AL") + if (t == 'EN') + types[i] = (lastType == 'AL') ? 'AN' : 'EN'; + else if (t == 'R' || t == 'L' || t == 'AL') lastType = t; } @@ -169,7 +152,7 @@ function bidi(text, startLevel) { for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == "AL") types[i] = "R"; + if (t == 'AL') types[i] = 'R'; } /* @@ -179,9 +162,9 @@ function bidi(text, startLevel) { */ for (var i = 1; i < strLength - 1; ++i) { - 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") && + 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] = types[i - 1]; } @@ -192,17 +175,17 @@ function bidi(text, startLevel) { */ for (var i = 0; i < strLength; ++i) { - if (types[i] == "EN") { + if (types[i] == 'EN') { // do before - for (j = i - 1; j >= 0; --j) { - if (types[j] == "ET") - types[j] = "EN"; + for (var j = i - 1; j >= 0; --j) { + if (types[j] == 'ET') + types[j] = 'EN'; else break; } // do after - for (j = i + 1; j < strLength; --j) { - if (types[j] == "ET") - types[j] = "EN"; + for (var j = i + 1; j < strLength; --j) { + if (types[j] == 'ET') + types[j] = 'EN'; else break; } } @@ -214,8 +197,8 @@ function bidi(text, startLevel) { for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == "WS" || t == "ES" || t == "ET" || t == "CS") - types[i] = "ON"; + if (t == 'WS' || t == 'ES' || t == 'ET' || t == 'CS') + types[i] = 'ON'; } /* @@ -227,9 +210,9 @@ function bidi(text, startLevel) { 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; } @@ -241,18 +224,18 @@ function bidi(text, startLevel) { */ for (var i = 0; i < strLength; ++i) { - if (types[i] == "ON") { - var end = findUnequal(types, i + 1, "ON"); + if (types[i] == 'ON') { + var end = findUnequal(types, i + 1, 'ON'); var before = sor; if (i > 0) before = types[i - 1]; var after = eor; if (end + 1 < strLength) after = types[end + 1]; - if (before != "L") - before = "R"; - if (after != "L") - after = "R"; + if (before != 'L') + before = 'R'; + if (after != 'L') + after = 'R'; if (before == after) setValues(types, i, end, before); i = end - 1; // reset to end (-1 so next iteration is ok) @@ -264,7 +247,7 @@ function bidi(text, startLevel) { */ for (var i = 0; i < strLength; ++i) { - if (types[i] == "ON") + if (types[i] == 'ON') types[i] = e; } @@ -278,15 +261,15 @@ function bidi(text, startLevel) { for (var i = 0; i < strLength; ++i) { var t = types[i]; if (isEven(levels[i])) { - if (t == "R") { + if (t == 'R') { levels[i] += 1; } - else if (t == "AN" || t == "EN") { + else if (t == 'AN' || t == 'EN') { levels[i] += 2; } } else { // isOdd, so - if (t == "L" || t == "AN" || t == "EN") { + if (t == 'L' || t == 'AN' || t == 'EN') { levels[i] += 1; } } @@ -303,7 +286,7 @@ function bidi(text, startLevel) { of the line. */ - //dont bother as text is only single line + // don't bother as text is only single line /* L2. From the highest level found in the text to the lowest odd level on each @@ -315,7 +298,7 @@ function bidi(text, startLevel) { var highestLevel = -1; var lowestOddLevel = 99; - for (var i = 0; i < levels.length; ++i) { + for (var i = 0, ii = levels.length; i < ii; ++i) { var level = levels[i]; if (highestLevel < level) highestLevel = level; @@ -359,12 +342,12 @@ function bidi(text, startLevel) { directionality of that character is R. */ - //dont mirror as characters are already mirrored in the pdf + // don't mirror as characters are already mirrored in the pdf // Finally, return string - var result = ""; - for (var i = 0; i < chars.length; ++i) { + var result = ''; + for (var i = 0, ii = chars.length; i < ii; ++i) { var ch = chars[i]; if (ch != '<' && ch != '>') result += ch; @@ -372,8 +355,6 @@ function bidi(text, startLevel) { return result; } -// UTILITIES - function isOdd(i) { return (i & 1) != 0; } @@ -435,10 +416,10 @@ function mirrorGlyphs(c) { return '{'; case '{': return '}'; - case '�': - return '�'; - case '�': - return '�'; + case '\u00AB': + return '\u00BB'; + case '\u00BB': + return '\u00AB'; default: return c; } From 16bd59edf0f5e34a4de6271ba070d76845188679 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 14 Feb 2012 23:00:09 -0600 Subject: [PATCH 08/15] Separating fontChar and unicode --- src/fonts.js | 77 +++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/src/fonts.js b/src/fonts.js index 45c83b74d..ea5f20d78 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -833,12 +833,7 @@ var Font = (function FontClosure() { else this.rebuildToUnicode(properties); - this.toUnicodeOriginal = this.toUnicode.slice(); - for (var i = 0, j = 0xE000; i < this.toUnicode.length; i++) { - if (typeof this.toUnicode[i] == 'number') - break; - this.toUnicode[i] = j++; - } + this.toFontChar = this.buildToFontChar(this.toUnicode); if (!file) { // The file data is not specified. Trying to fix the font name @@ -1794,7 +1789,7 @@ var Font = (function FontClosure() { var unassignedUnicodeItems = []; for (var i = 1; i < numGlyphs; i++) { var cid = gidToCidMap[i] || i; - var unicode = this.toUnicode[cid]; + var unicode = this.toFontChar[cid]; if (!unicode || typeof unicode !== 'number' || isSpecialUnicode(unicode) || unicode in usedUnicodes) { unassignedUnicodeItems.push(i); @@ -1815,7 +1810,7 @@ var Font = (function FontClosure() { if (unusedUnicode >= kCmapGlyphOffset + kSizeOfGlyphArea) break; var unicode = unusedUnicode++; - this.toUnicode[cid] = unicode; + this.toFontChar[cid] = unicode; usedUnicodes[unicode] = true; glyphs.push({ unicode: unicode, code: cid }); ids.push(i); @@ -1826,9 +1821,9 @@ var Font = (function FontClosure() { var glyphs = cmapTable.glyphs; var ids = cmapTable.ids; var hasShortCmap = !!cmapTable.hasShortCmap; - var toUnicode = this.toUnicode; + var toFontChar = this.toFontChar; - if (toUnicode && toUnicode.length > 0) { + if (toFontChar && toFontChar.length > 0) { // checking if cmap is just identity map var isIdentity = true; for (var i = 0, ii = glyphs.length; i < ii; i++) { @@ -1837,11 +1832,11 @@ var Font = (function FontClosure() { break; } } - // if it is, replacing with meaningful toUnicode values + // if it is, replacing with meaningful toFontChar values if (isIdentity) { var usedUnicodes = [], unassignedUnicodeItems = []; for (var i = 0, ii = glyphs.length; i < ii; i++) { - var unicode = toUnicode[i + 1]; + var unicode = toFontChar[i + 1]; if (!unicode || typeof unicode !== 'number' || unicode in usedUnicodes) { unassignedUnicodeItems.push(i); continue; @@ -1856,11 +1851,11 @@ var Font = (function FontClosure() { unusedUnicode++; var cid = i + 1; // override only if unicode mapping is not specified - if (!(cid in toUnicode)) - toUnicode[cid] = unusedUnicode; + if (!(cid in toFontChar)) + toFontChar[cid] = unusedUnicode; glyphs[i].unicode = unusedUnicode++; } - this.useToUnicode = true; + this.useToFontChar = true; } } @@ -1990,12 +1985,12 @@ var Font = (function FontClosure() { properties.baseEncoding = encoding; } if (false && properties.subtype == 'CIDFontType0C') { - var toUnicode = []; + var toFontChar = []; for (var i = 0; i < charstrings.length; ++i) { var charstring = charstrings[i]; - toUnicode[charstring.code] = charstring.unicode; + toFontChar[charstring.code] = charstring.unicode; } - this.toUnicode = toUnicode; + this.toFontChar = toFontChar; } var fields = { @@ -2090,6 +2085,18 @@ var Font = (function FontClosure() { return stringToArray(otf.file); }, + buildToFontChar: function font_buildToFontChar(toUnicode) { + var result = []; + var unusedUnicode = kCmapGlyphOffset; + for (var i = 0, ii = toUnicode.length; i < ii; i++) { + var unicode = toUnicode[i]; + var fontCharCode = typeof unicode === 'object' ? unusedUnicode++ : + unicode; + result.push(fontCharCode); + } + return result; + }, + rebuildToUnicode: function font_rebuildToUnicode(properties) { var firstChar = properties.firstChar, lastChar = properties.lastChar; var map = []; @@ -2222,7 +2229,7 @@ var Font = (function FontClosure() { }, charToGlyph: function fonts_charToGlyph(charcode) { - var unicode, width, codeIRQueue; + var fontCharCode, width, codeIRQueue; var width = this.widths[charcode]; @@ -2230,38 +2237,39 @@ var Font = (function FontClosure() { case 'CIDFontType0': if (this.noUnicodeAdaptation) { width = this.widths[this.unicodeToCID[charcode] || charcode]; - unicode = mapPrivateUseChars(charcode); + fontCharCode = mapPrivateUseChars(charcode); break; } - unicode = this.toUnicode[charcode] || charcode; + fontCharCode = this.toFontChar[charcode] || charcode; break; case 'CIDFontType2': if (this.noUnicodeAdaptation) { width = this.widths[this.unicodeToCID[charcode] || charcode]; - unicode = mapPrivateUseChars(charcode); + fontCharCode = mapPrivateUseChars(charcode); break; } - unicode = this.toUnicode[charcode] || charcode; + fontCharCode = this.toFontChar[charcode] || charcode; break; case 'Type1': var glyphName = this.differences[charcode] || this.encoding[charcode]; if (!isNum(width)) width = this.widths[glyphName]; if (this.noUnicodeAdaptation) { - unicode = mapPrivateUseChars(GlyphsUnicode[glyphName] || charcode); + fontCharCode = mapPrivateUseChars(GlyphsUnicode[glyphName] || + charcode); break; } - unicode = this.glyphNameMap[glyphName] || + fontCharCode = this.glyphNameMap[glyphName] || GlyphsUnicode[glyphName] || charcode; break; case 'Type3': var glyphName = this.differences[charcode] || this.encoding[charcode]; codeIRQueue = this.charProcIRQueues[glyphName]; - unicode = charcode; + fontCharCode = charcode; break; case 'TrueType': - if (this.useToUnicode) { - unicode = this.toUnicode[charcode] || charcode; + if (this.useToFontChar) { + fontCharCode = this.toFontChar[charcode] || charcode; break; } var glyphName = this.differences[charcode] || this.encoding[charcode]; @@ -2270,16 +2278,17 @@ var Font = (function FontClosure() { if (!isNum(width)) width = this.widths[glyphName]; if (this.noUnicodeAdaptation) { - unicode = GlyphsUnicode[glyphName] || charcode; + fontCharCode = GlyphsUnicode[glyphName] || charcode; break; } if (!this.hasEncoding || this.isSymbolicFont) { - unicode = this.useToUnicode ? this.toUnicode[charcode] : charcode; + fontCharCode = this.useToFontChar ? this.toFontChar[charcode] : + charcode; break; } // MacRoman encoding address by re-encoding the cmap table - unicode = glyphName in this.glyphNameMap ? + fontCharCode = glyphName in this.glyphNameMap ? this.glyphNameMap[glyphName] : GlyphsUnicode[glyphName]; break; default: @@ -2287,15 +2296,15 @@ var Font = (function FontClosure() { break; } - var unicodeChars = !('toUnicodeOriginal' in this) ? charcode : - this.toUnicodeOriginal[charcode] || charcode; + var unicodeChars = !('toUnicode' in this) ? charcode : + this.toUnicode[charcode] || charcode; if (typeof unicodeChars === 'number') unicodeChars = String.fromCharCode(unicodeChars); width = (isNum(width) ? width : this.defaultWidth) * this.widthMultiplier; return { - fontChar: String.fromCharCode(unicode), + fontChar: String.fromCharCode(fontCharCode), unicode: unicodeChars, width: width, codeIRQueue: codeIRQueue From 6c7e7df6dad89b931977f3144c2ac78d2cdb8cb8 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 14 Feb 2012 23:06:16 -0600 Subject: [PATCH 09/15] linting --- src/fonts.js | 7 ++++--- web/viewer.js | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/fonts.js b/src/fonts.js index ea5f20d78..87330756d 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -1790,8 +1790,8 @@ var Font = (function FontClosure() { for (var i = 1; i < numGlyphs; i++) { var cid = gidToCidMap[i] || i; var unicode = this.toFontChar[cid]; - if (!unicode || typeof unicode !== 'number' || isSpecialUnicode(unicode) || - unicode in usedUnicodes) { + if (!unicode || typeof unicode !== 'number' || + isSpecialUnicode(unicode) || unicode in usedUnicodes) { unassignedUnicodeItems.push(i); continue; } @@ -1837,7 +1837,8 @@ var Font = (function FontClosure() { var usedUnicodes = [], unassignedUnicodeItems = []; for (var i = 0, ii = glyphs.length; i < ii; i++) { var unicode = toFontChar[i + 1]; - if (!unicode || typeof unicode !== 'number' || unicode in usedUnicodes) { + if (!unicode || typeof unicode !== 'number' || + unicode in usedUnicodes) { unassignedUnicodeItems.push(i); continue; } diff --git a/web/viewer.js b/web/viewer.js index ff857a943..dbb72202f 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -1044,9 +1044,9 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) { // Adjust div width to match canvas text // Due to the .offsetWidth calls, this is slow // This needs to come after appending to the DOM - CustomStyle.setProp('transform' , textDiv, 'scale(' - + textDiv.dataset.canvasWidth/textDiv.offsetWidth - + ',1)'); + var textScale = textDiv.dataset.canvasWidth / textDiv.offsetWidth; + CustomStyle.setProp('transform' , textDiv, + 'scale(' + textScale + ', 1)'); CustomStyle.setProp('transformOrigin' , textDiv, '0% 0%'); } } // textLength > 0 From b4e3554af25d7dfd09a9496a515c33dbdc1010c4 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 20 Feb 2012 13:00:45 -0600 Subject: [PATCH 10/15] Remove debug code --- src/fonts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fonts.js b/src/fonts.js index 1ddf5eaa5..e67286d81 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -2040,7 +2040,7 @@ var Font = (function FontClosure() { } properties.baseEncoding = encoding; } - if (false && properties.subtype == 'CIDFontType0C') { + if (properties.subtype == 'CIDFontType0C') { var toFontChar = []; for (var i = 0; i < charstrings.length; ++i) { var charstring = charstrings[i]; From 26b58c0e4f18f3222881ae7c3c17565441308a37 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 20 Feb 2012 14:26:30 -0600 Subject: [PATCH 11/15] Properly build toFontChar --- src/fonts.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/fonts.js b/src/fonts.js index e67286d81..01e7a7c9f 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -2147,8 +2147,9 @@ var Font = (function FontClosure() { for (var i = 0, ii = toUnicode.length; i < ii; i++) { var unicode = toUnicode[i]; var fontCharCode = typeof unicode === 'object' ? unusedUnicode++ : - unicode; - result.push(fontCharCode); + unicode; + if (typeof unicode !== 'undefined') + result[i] = fontCharCode; } return result; }, From 7cad586e6315b4a88078de43d1278f0ed4805808 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 21 Feb 2012 18:27:31 -0600 Subject: [PATCH 12/15] Fix merge with symbol font fix --- src/fonts.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/fonts.js b/src/fonts.js index e0890c6fa..0651485fc 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -1957,9 +1957,10 @@ var Font = (function FontClosure() { // Moving all symbolic font glyphs into 0xF000 - 0xF0FF range. if (this.isSymbolicFont) { for (var i = 0, ii = glyphs.length; i < ii; i++) { + var cid = i + 1; var code = glyphs[i].unicode; code = kSymbolicFontGlyphOffset | (code & 0xFF); - glyphs[i].unicode = toFontChar[i] = code; + glyphs[i].unicode = toFontChar[cid] = code; } this.useToFontChar = true; } From 31d8d13ba294f2e16ca984aedd932a23748b33f5 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 21 Feb 2012 19:56:35 -0600 Subject: [PATCH 13/15] Fix merge with symbol font fix (part 2) --- src/fonts.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/fonts.js b/src/fonts.js index 0651485fc..3955f9136 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -1957,10 +1957,9 @@ var Font = (function FontClosure() { // Moving all symbolic font glyphs into 0xF000 - 0xF0FF range. if (this.isSymbolicFont) { for (var i = 0, ii = glyphs.length; i < ii; i++) { - var cid = i + 1; - var code = glyphs[i].unicode; - code = kSymbolicFontGlyphOffset | (code & 0xFF); - glyphs[i].unicode = toFontChar[cid] = code; + var code = glyphs[i].unicode & 0xFF; + var fontCharCode = kSymbolicFontGlyphOffset | code; + glyphs[i].unicode = toFontChar[code] = fontCharCode; } this.useToFontChar = true; } From e8ca7b44d9d6590bbdb3246c4c27d7c2d7c556ca Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 22 Feb 2012 17:56:47 -0600 Subject: [PATCH 14/15] Fixing review nits; moving small functions inside bidi --- src/bidi.js | 180 +++++++++++++++++++++++++------------------------- src/canvas.js | 10 +-- 2 files changed, 96 insertions(+), 94 deletions(-) diff --git a/src/bidi.js b/src/bidi.js index 025847ea9..b24721045 100644 --- a/src/bidi.js +++ b/src/bidi.js @@ -52,9 +52,81 @@ var arabicTypes = [ ]; function bidi(text, startLevel) { + function isOdd(i) { + return (i & 1) != 0; + } + + function isEven(i) { + return (i & 1) == 0; + } + + function findUnequal(arr, start, value) { + var j; + for (var j = start, jj = arr.length; j < jj; ++j) { + if (arr[j] != value) + return j; + } + return j; + } + + function setValues(arr, start, end, value) { + for (var j = start; j < end; ++j) { + arr[j] = value; + } + } + + function reverseValues(arr, start, end) { + for (var i = start, j = end - 1; i < j; ++i, --j) { + var temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + function mirrorGlyphs(c) { + /* + # BidiMirroring-1.txt + 0028; 0029 # LEFT PARENTHESIS + 0029; 0028 # RIGHT PARENTHESIS + 003C; 003E # LESS-THAN SIGN + 003E; 003C # GREATER-THAN SIGN + 005B; 005D # LEFT SQUARE BRACKET + 005D; 005B # RIGHT SQUARE BRACKET + 007B; 007D # LEFT CURLY BRACKET + 007D; 007B # RIGHT CURLY BRACKET + 00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK + 00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK + */ + switch (c) { + case '(': + return ')'; + case ')': + return '('; + case '<': + return '>'; + case '>': + return '<'; + case ']': + return '['; + case '[': + return ']'; + case '}': + return '{'; + case '{': + return '}'; + case '\u00AB': + return '\u00BB'; + case '\u00BB': + return '\u00AB'; + default: + return c; + } + } + var str = text.str; var strLength = str.length; - if (strLength == 0) return str; + if (strLength == 0) + return str; // get types, fill arrays @@ -91,7 +163,8 @@ function bidi(text, startLevel) { text.direction = 'ltr'; return str; } - else if (startLevel == -1) { + + if (startLevel == -1) { if ((strLength / numBidi) < 0.3) { text.direction = 'ltr'; startLevel = 0; @@ -127,8 +200,10 @@ function bidi(text, startLevel) { var lastType = sor; for (var i = 0; i < strLength; ++i) { - if (types[i] == 'NSM') types[i] = lastType; - else lastType = types[i]; + if (types[i] == 'NSM') + types[i] = lastType; + else + lastType = types[i]; } /* @@ -152,7 +227,8 @@ function bidi(text, startLevel) { for (var i = 0; i < strLength; ++i) { var t = types[i]; - if (t == 'AL') types[i] = 'R'; + if (t == 'AL') + types[i] = 'R'; } /* @@ -178,15 +254,15 @@ function bidi(text, startLevel) { if (types[i] == 'EN') { // do before for (var j = i - 1; j >= 0; --j) { - if (types[j] == 'ET') - types[j] = 'EN'; - else break; + if (types[j] != 'ET') + break; + types[j] = 'EN'; } // do after for (var j = i + 1; j < strLength; --j) { - if (types[j] == 'ET') - types[j] = 'EN'; - else break; + if (types[j] != 'ET') + break; + types[j] = 'EN'; } } } @@ -263,12 +339,10 @@ function bidi(text, startLevel) { if (isEven(levels[i])) { if (t == 'R') { levels[i] += 1; - } - else if (t == 'AN' || t == 'EN') { + } else if (t == 'AN' || t == 'EN') { levels[i] += 2; } - } - else { // isOdd, so + } else { // isOdd, so if (t == 'L' || t == 'AN' || t == 'EN') { levels[i] += 1; } @@ -311,14 +385,13 @@ function bidi(text, startLevel) { for (var level = highestLevel; level >= lowestOddLevel; --level) { // find segments to reverse var start = -1; - for (var i = 0; i < levels.length; ++i) { + for (var i = 0, ii = levels.length; i < ii; ++i) { if (levels[i] < level) { if (start >= 0) { reverseValues(chars, start, i); start = -1; } - } - else if (start < 0) { + } else if (start < 0) { start = i; } } @@ -354,74 +427,3 @@ function bidi(text, startLevel) { } return result; } - -function isOdd(i) { - return (i & 1) != 0; -} - -function isEven(i) { - return (i & 1) == 0; -} - -function findUnequal(arr, start, value) { - var j; - for (var j = start; j < arr.length; ++j) { - if (arr[j] != value) return j; - } - return j; -} - -function setValues(arr, start, end, value) { - for (var j = start; j < end; ++j) { - arr[j] = value; - } -} - -function reverseValues(arr, start, end) { - for (var i = start, j = end - 1; i < j; ++i, --j) { - var temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } -} - -function mirrorGlyphs(c) { - /* - # BidiMirroring-1.txt - 0028; 0029 # LEFT PARENTHESIS - 0029; 0028 # RIGHT PARENTHESIS - 003C; 003E # LESS-THAN SIGN - 003E; 003C # GREATER-THAN SIGN - 005B; 005D # LEFT SQUARE BRACKET - 005D; 005B # RIGHT SQUARE BRACKET - 007B; 007D # LEFT CURLY BRACKET - 007D; 007B # RIGHT CURLY BRACKET - 00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK - 00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK - */ - switch (c) { - case '(': - return ')'; - case ')': - return '('; - case '<': - return '>'; - case '>': - return '<'; - case ']': - return '['; - case '[': - return ']'; - case '}': - return '{'; - case '{': - return '}'; - case '\u00AB': - return '\u00BB'; - case '\u00BB': - return '\u00AB'; - default: - return c; - } -} - diff --git a/src/canvas.js b/src/canvas.js index 551148341..54ab5b4d0 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -779,15 +779,15 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { x += charWidth; var glyphUnicode = glyph.unicode === ' ' ? '\u00A0' : glyph.unicode; - var glyphUniLen = glyphUnicode.length; + var glyphUnicodeLength = glyphUnicode.length; //reverse an arabic ligature - if (glyphUniLen > 1 && isRTLRangeFor(glyphUnicode.charCodeAt(0))) - { - for (var ii = glyphUniLen - 1; ii >= 0; ii--) + if (glyphUnicodeLength > 1 && + isRTLRangeFor(glyphUnicode.charCodeAt(0))) { + for (var ii = glyphUnicodeLength - 1; ii >= 0; ii--) text.str += glyphUnicode[ii]; } else text.str += glyphUnicode; - text.length += glyphUniLen; + text.length += glyphUnicodeLength; text.canvasWidth += charWidth; } current.x += x * textHScale2; From 3b297368820b22738b5000a7ca291634209719c5 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 22 Feb 2012 18:26:43 -0600 Subject: [PATCH 15/15] create bidi closure --- src/bidi.js | 630 ++++++++++++++++++++++++++-------------------------- 1 file changed, 317 insertions(+), 313 deletions(-) diff --git a/src/bidi.js b/src/bidi.js index b24721045..a3308fa2c 100644 --- a/src/bidi.js +++ b/src/bidi.js @@ -3,55 +3,56 @@ 'use strict'; -// Character types for symbols from 0000 to 00FF. -var baseTypes = [ - 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'S', 'B', 'S', 'WS', - 'B', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', - 'BN', 'BN', 'B', 'B', 'B', 'S', 'WS', 'ON', 'ON', 'ET', 'ET', 'ET', 'ON', - 'ON', 'ON', 'ON', 'ON', 'ON', 'CS', 'ON', 'CS', 'ON', 'EN', 'EN', 'EN', 'EN', - 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', 'ON', 'ON', - 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', - 'ON', 'ON', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'B', 'BN', 'BN', 'BN', 'BN', - 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', - 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'CS', 'ON', 'ET', 'ET', - 'ET', 'ET', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'ET', - 'ET', 'EN', 'EN', 'ON', 'L', 'ON', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', - 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', - 'L', 'L', 'L', 'L', 'L', 'L' -]; +var bidi = (function bidiClosure() { + // Character types for symbols from 0000 to 00FF. + var baseTypes = [ + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'S', 'B', 'S', 'WS', + 'B', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', + 'BN', 'BN', 'B', 'B', 'B', 'S', 'WS', 'ON', 'ON', 'ET', 'ET', 'ET', 'ON', + 'ON', 'ON', 'ON', 'ON', 'ON', 'CS', 'ON', 'CS', 'ON', 'EN', 'EN', 'EN', + 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'EN', 'ON', 'ON', 'ON', 'ON', 'ON', + 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', + 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'ON', 'ON', 'ON', 'ON', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'B', 'BN', + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', + 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', 'BN', + 'BN', 'CS', 'ON', 'ET', 'ET', 'ET', 'ET', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', + 'ON', 'ON', 'ON', 'ON', 'ET', 'ET', 'EN', 'EN', 'ON', 'L', 'ON', 'ON', 'ON', + 'EN', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', + 'L', 'L', 'L', 'ON', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L' + ]; -// Character types for symbols from 0600 to 06FF -var arabicTypes = [ - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'CS', - 'AL', 'ON', 'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'NSM', - 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', - 'NSM', 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AN', 'AN', 'AN', - 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'ET', 'AN', 'AN', 'AL', 'AL', 'AL', - 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'NSM', - 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', - 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'ON', 'NSM', 'NSM', 'NSM', - 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', - 'AL', 'AL', 'AL', 'AL', 'AL', 'AL' -]; + // Character types for symbols from 0600 to 06FF + var arabicTypes = [ + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'CS', 'AL', 'ON', 'ON', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', + 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', 'AN', + 'AN', 'ET', 'AN', 'AN', 'AL', 'AL', 'AL', 'NSM', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', + 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'NSM', 'ON', 'NSM', + 'NSM', 'NSM', 'NSM', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', + 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL', 'AL' + ]; -function bidi(text, startLevel) { function isOdd(i) { return (i & 1) != 0; } @@ -123,307 +124,310 @@ function bidi(text, startLevel) { } } - var str = text.str; - var strLength = str.length; - if (strLength == 0) - return str; + return (function bidi(text, startLevel) { + var str = text.str; + var strLength = str.length; + if (strLength == 0) + return str; - // get types, fill arrays + // get types, fill arrays - var chars = new Array(strLength); - var types = new Array(strLength); - var oldtypes = new Array(strLength); - var numBidi = 0; + var chars = new Array(strLength); + var types = new Array(strLength); + var oldtypes = new Array(strLength); + var numBidi = 0; - for (var i = 0; i < strLength; ++i) { - chars[i] = str.charAt(i); + for (var i = 0; i < strLength; ++i) { + chars[i] = str.charAt(i); - var charCode = str.charCodeAt(i); - var charType = 'L'; - if (charCode <= 0x00ff) - charType = baseTypes[charCode]; - else if (0x0590 <= charCode && charCode <= 0x05f4) - charType = 'R'; - else if (0x0600 <= charCode && charCode <= 0x06ff) - charType = arabicTypes[charCode & 0xff]; - else if (0x0700 <= charCode && charCode <= 0x08AC) - charType = 'AL'; + var charCode = str.charCodeAt(i); + var charType = 'L'; + if (charCode <= 0x00ff) + charType = baseTypes[charCode]; + else if (0x0590 <= charCode && charCode <= 0x05f4) + charType = 'R'; + else if (0x0600 <= charCode && charCode <= 0x06ff) + charType = arabicTypes[charCode & 0xff]; + else if (0x0700 <= charCode && charCode <= 0x08AC) + charType = 'AL'; - if (charType == 'R' || charType == 'AL' || charType == 'AN') - numBidi++; + if (charType == 'R' || charType == 'AL' || charType == 'AN') + numBidi++; - oldtypes[i] = types[i] = charType; - } + oldtypes[i] = 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 - if (numBidi == 0) { - text.direction = 'ltr'; - return str; - } - - if (startLevel == -1) { - if ((strLength / numBidi) < 0.3) { + // 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) { text.direction = 'ltr'; - startLevel = 0; - } else { - text.direction = 'rtl'; - startLevel = 1; + return str; } - } - var levels = new Array(strLength); - - for (var i = 0; i < strLength; ++i) { - levels[i] = startLevel; - } - - var diffChars = new Array(strLength); - var diffLevels = new Array(strLength); - var diffTypes = new Array(strLength); - - /* - X1-X10: skip most of this, since we are NOT doing the embeddings. - */ - - var e = isOdd(startLevel) ? 'R' : 'L'; - var sor = e; - var eor = sor; - - /* - W1. Examine each non-spacing mark (NSM) in the level run, and change 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. - */ - - var lastType = sor; - for (var i = 0; i < strLength; ++i) { - if (types[i] == 'NSM') - types[i] = lastType; - else - lastType = types[i]; - } - - /* - 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 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') - types[i] = (lastType == 'AL') ? 'AN' : 'EN'; - 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') - types[i] = 'R'; - } - - /* - W4. A single European separator between two European numbers changes to a - 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') - types[i] = 'EN'; - if (types[i] == 'CS' && (types[i - 1] == 'EN' || types[i - 1] == 'AN') && - 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') - break; - types[j] = 'EN'; - } - // do after - for (var j = i + 1; j < strLength; --j) { - if (types[j] != 'ET') - break; - types[j] = 'EN'; + if (startLevel == -1) { + if ((strLength / numBidi) < 0.3) { + text.direction = 'ltr'; + startLevel = 0; + } else { + text.direction = 'rtl'; + startLevel = 1; } } - } - /* - W6. Otherwise, separators and terminators change to Other Neutral: - */ + var levels = new Array(strLength); - for (var i = 0; i < strLength; ++i) { - var t = types[i]; - if (t == 'WS' || t == 'ES' || t == 'ET' || t == 'CS') - types[i] = 'ON'; - } - - /* - 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 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') - lastType = t; - } - - /* - N1. A sequence of neutrals takes the direction of the surrounding strong text - if the text on both sides has the same direction. European and Arabic 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) - before = types[i - 1]; - var after = eor; - if (end + 1 < strLength) - after = types[end + 1]; - if (before != 'L') - before = 'R'; - if (after != 'L') - after = 'R'; - if (before == after) - setValues(types, i, end, before); - i = end - 1; // reset to end (-1 so next iteration is ok) + for (var i = 0; i < strLength; ++i) { + levels[i] = startLevel; } - } - /* - N2. Any remaining neutrals take the embedding direction. - */ + var diffChars = new Array(strLength); + var diffLevels = new Array(strLength); + var diffTypes = new Array(strLength); - for (var i = 0; i < strLength; ++i) { - if (types[i] == 'ON') - types[i] = e; - } + /* + X1-X10: skip most of this, since we are NOT doing the embeddings. + */ - /* - I1. For all characters with an even (left-to-right) embedding direction, - those of type R go up one level and those of type AN or EN go up two levels. - I2. For all characters with an odd (right-to-left) embedding direction, those - of type L, EN or AN go up one level. - */ + var e = isOdd(startLevel) ? 'R' : 'L'; + var sor = e; + var eor = sor; - for (var i = 0; i < strLength; ++i) { - var t = types[i]; - if (isEven(levels[i])) { - if (t == 'R') { - levels[i] += 1; - } else if (t == 'AN' || t == 'EN') { - levels[i] += 2; - } - } else { // isOdd, so - if (t == 'L' || t == 'AN' || t == 'EN') { - levels[i] += 1; - } + /* + W1. Examine each non-spacing mark (NSM) in the level run, and change 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. + */ + + var lastType = sor; + for (var i = 0; i < strLength; ++i) { + if (types[i] == 'NSM') + types[i] = lastType; + else + lastType = types[i]; } - } - /* - L1. On each line, reset the embedding level of the following characters to - the paragraph embedding level: + /* + 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 + the type of the European number to Arabic number. + */ - segment separators, - paragraph separators, - any sequence of whitespace characters preceding a segment separator or - paragraph separator, and any sequence of white space characters at the end - of the line. - */ + var lastType = sor; + for (var i = 0; i < strLength; ++i) { + var t = types[i]; + if (t == 'EN') + types[i] = (lastType == 'AL') ? 'AN' : 'EN'; + else if (t == 'R' || t == 'L' || t == 'AL') + lastType = t; + } - // don't bother as text is only single line + /* + W3. Change all ALs to R. + */ - /* - L2. From the highest level found in the text to the lowest odd level on each - line, reverse any contiguous sequence of characters that are at that level or - higher. - */ + for (var i = 0; i < strLength; ++i) { + var t = types[i]; + if (t == 'AL') + types[i] = 'R'; + } - // find highest level & lowest odd level + /* + W4. A single European separator between two European numbers changes to a + European number. A single common separator between two numbers of the same + type changes to that type: + */ - var highestLevel = -1; - var lowestOddLevel = 99; - for (var i = 0, ii = levels.length; i < ii; ++i) { - var level = levels[i]; - if (highestLevel < level) - highestLevel = level; - if (lowestOddLevel > level && isOdd(level)) - lowestOddLevel = level; - } + for (var i = 1; i < strLength - 1; ++i) { + 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] = types[i - 1]; + } - // now reverse between those limits + /* + W5. A sequence of European terminators adjacent to European numbers changes + to all European numbers: + */ - for (var level = highestLevel; level >= lowestOddLevel; --level) { - // find segments to reverse - var start = -1; - for (var i = 0, ii = levels.length; i < ii; ++i) { - if (levels[i] < level) { - if (start >= 0) { - reverseValues(chars, start, i); - start = -1; + 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') + break; + types[j] = 'EN'; + } + // do after + for (var j = i + 1; j < strLength; --j) { + if (types[j] != 'ET') + break; + types[j] = 'EN'; } - } else if (start < 0) { - start = i; } } - if (start >= 0) { - reverseValues(chars, start, levels.length); + + /* + 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') + types[i] = 'ON'; } - } - /* - L3. Combining marks applied to a right-to-left base character will at this - point precede their base character. If the rendering engine expects them to - follow the base characters in the final display process, then the ordering of - the marks and the base character must be reversed. - */ + /* + 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 + the type of the European number to L. + */ - // don't bother for now + 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') + lastType = t; + } - /* - L4. A character that possesses the mirrored property as specified by - Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved - directionality of that character is R. - */ + /* + N1. A sequence of neutrals takes the direction of the surrounding strong + text if the text on both sides has the same direction. European and Arabic + 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. + */ - // don't mirror as characters are already mirrored in the pdf + for (var i = 0; i < strLength; ++i) { + if (types[i] == 'ON') { + var end = findUnequal(types, i + 1, 'ON'); + var before = sor; + if (i > 0) + before = types[i - 1]; + var after = eor; + if (end + 1 < strLength) + after = types[end + 1]; + if (before != 'L') + before = 'R'; + if (after != 'L') + after = 'R'; + if (before == after) + setValues(types, i, end, before); + i = end - 1; // reset to end (-1 so next iteration is ok) + } + } - // Finally, return string + /* + N2. Any remaining neutrals take the embedding direction. + */ - var result = ''; - for (var i = 0, ii = chars.length; i < ii; ++i) { - var ch = chars[i]; - if (ch != '<' && ch != '>') - result += ch; - } - return result; -} + for (var i = 0; i < strLength; ++i) { + if (types[i] == 'ON') + types[i] = e; + } + + /* + I1. For all characters with an even (left-to-right) embedding direction, + those of type R go up one level and those of type AN or EN go up two + levels. + 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])) { + if (t == 'R') { + levels[i] += 1; + } else if (t == 'AN' || t == 'EN') { + levels[i] += 2; + } + } else { // isOdd, so + if (t == 'L' || t == 'AN' || t == 'EN') { + levels[i] += 1; + } + } + } + + /* + L1. On each line, reset the embedding level of the following characters to + the paragraph embedding level: + + segment separators, + paragraph separators, + any sequence of whitespace characters preceding a segment separator or + paragraph separator, and any sequence of white space characters at the end + of the line. + */ + + // don't bother as text is only single line + + /* + L2. From the highest level found in the text to the lowest odd level on + each line, reverse any contiguous sequence of characters that are at that + level or higher. + */ + + // 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) + highestLevel = 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; + for (var i = 0, ii = levels.length; i < ii; ++i) { + if (levels[i] < level) { + if (start >= 0) { + reverseValues(chars, start, i); + start = -1; + } + } else if (start < 0) { + start = i; + } + } + if (start >= 0) { + reverseValues(chars, start, levels.length); + } + } + + /* + L3. Combining marks applied to a right-to-left base character will at this + point precede their base character. If the rendering engine expects them to + follow the base characters in the final display process, then the ordering + of the marks and the base character must be reversed. + */ + + // don't bother for now + + /* + L4. A character that possesses the mirrored property as specified by + Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved + directionality of that character is R. + */ + + // 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 != '>') + result += ch; + } + return result; + }); +})();