]> git.parisson.com Git - pdf.js.git/commitdiff
Fixing ToUnicode parsing; workaround for invalid UTF16 encoding
authornotmasteryet <async.processingjs@yahoo.com>
Fri, 10 Feb 2012 02:40:44 +0000 (18:40 -0800)
committernotmasteryet <async.processingjs@yahoo.com>
Fri, 10 Feb 2012 02:40:44 +0000 (18:40 -0800)
src/evaluator.js
src/fonts.js

index 1597bed11b130f72324caf72dee1cff8f64bb130..e34787e412536411a42eea4ee5d90903c6174c22 100644 (file)
@@ -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 = '';
               }
index f8ae7de4c8d842bd39ed0f88fa202c0d2eb707f8..aadaa8d7112a3b8bfaa925121a3302cd0c8d2b54 100644 (file)
@@ -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);