]> git.parisson.com Git - pdf.js.git/commitdiff
Initial ToUnicode modifications
authornotmasteryet <async.processingjs@yahoo.com>
Thu, 24 Nov 2011 15:38:09 +0000 (09:38 -0600)
committernotmasteryet <async.processingjs@yahoo.com>
Thu, 24 Nov 2011 15:38:09 +0000 (09:38 -0600)
src/canvas.js
src/evaluator.js
src/fonts.js

index 9b3ed0ba9ad66d8e8d9347570ccb7ea074ff6497..8d6fb046de2718303c27bf618731851dfbbe13ac 100644 (file)
@@ -474,13 +474,11 @@ var CanvasGraphics = (function canvasGraphics() {
             continue;
           }
 
-          var unicode = glyph.unicode;
-          var char = (unicode >= 0x10000) ?
-            String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
-            0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
-
+          var char = glyph.fontChar;
           ctx.fillText(char, width, 0);
           width += glyph.width * fontSize * 0.001 + charSpacing;
+
+          // TODO actual characters can be extracted from the glyph.unicode
         }
         current.x += width;
 
index a863a531ec57b17a14a8d9153b32f17b9c5a915d..03fce2d9aa6544ba3c21e4e3e91cd189700cf3e1 100644 (file)
@@ -512,6 +512,7 @@ var PartialEvaluator = (function partialEvaluator() {
           error('Encoding is not a Name nor a Dict');
         }
       }
+
       properties.differences = differences;
       properties.baseEncoding = baseEncoding;
       properties.hasEncoding = hasEncoding;
@@ -595,9 +596,18 @@ var PartialEvaluator = (function partialEvaluator() {
             }
           } else if (byte == 0x3E) {
             if (token.length) {
-              // parsing hex number
-              tokens.push(parseInt(token, 16));
-              token = '';
+              if (token.length <= 4) {
+                // parsing hex number
+                tokens.push(parseInt(token, 16));
+                token = '';
+              } 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));
+                tokens.push(String.fromCharCode.apply(String, str));
+                token = '';
+              }
             }
           } else {
             token += String.fromCharCode(byte);
index 116bb4dfc71064a1e8f86d092d852628751117ec..d028a9786ab0b91ca386438ae292fd1a1d35f5c1 100644 (file)
@@ -771,7 +771,6 @@ var Font = (function Font() {
     this.widths = properties.widths;
     this.defaultWidth = properties.defaultWidth;
     this.composite = properties.composite;
-    this.toUnicode = properties.toUnicode;
     this.hasEncoding = properties.hasEncoding;
 
     this.fontMatrix = properties.fontMatrix;
@@ -781,6 +780,11 @@ var Font = (function Font() {
     // Trying to fix encoding using glyph CIDSystemInfo.
     this.loadCidToUnicode(properties);
 
+    if (properties.toUnicode)
+      this.toUnicode = properties.toUnicode;
+    else
+      this.rebuildToUnicode(properties);
+
     if (!file) {
       // The file data is not specified. Trying to fix the font name
       // to be used with the canvas.font.
@@ -1898,6 +1902,29 @@ var Font = (function Font() {
       return stringToArray(otf.file);
     },
 
+    rebuildToUnicode: function font_rebuildToUnicode(properties) {
+      var map = [];
+      if (properties.composite) {
+        for (var i = properties.firstChar, ii = properties.lastChar; i <= ii; i++) {
+          // TODO missing map the character according font's CMap
+          var cid = i;
+          map[i] = this.cidToUnicode[cid];
+        }
+      } else {
+        for (var i = properties.firstChar, ii = properties.lastChar; i <= ii; i++) {
+          var glyph = properties.differences[i];
+          if (!glyph)
+            glyph = properties.baseEncoding[i];
+          if (!!glyph && (glyph in GlyphsUnicode))
+            map[i] = GlyphsUnicode[glyph]
+        }
+      }
+      this.toUnicode = map;
+      this.refreshToUnicode = function refreshToUnicode() {
+        this.font_rebuildToUnicode(properties);
+      };
+    },
+
     loadCidToUnicode: function font_loadCidToUnicode(properties) {
       if (properties.cidToGidMap) {
         this.cidToUnicode = properties.cidToGidMap;
@@ -2039,8 +2066,18 @@ var Font = (function Font() {
           warn('Unsupported font type: ' + this.type);
           break;
       }
+
+      var unicodeChars = this.toUnicode ? this.toUnicode[charcode] : charcode;
+      if (typeof unicodeChars === 'number') {
+        unicodeChars = (unicodeChars >= 0x10000) ?
+            String.fromCharCode(0xD800 | ((unicodeChars - 0x10000) >> 10),
+            0xDC00 | (unicodeChars & 0x3FF)) : String.fromCharCode(unicodeChars);
+        // TODO we probably don't need convert high/low surrogate... keeping for now
+      }
+
       return {
-        unicode: unicode,
+        fontChar: String.fromCharCode(unicode),
+        unicode: unicodeChars,
         width: isNum(width) ? width : this.defaultWidth,
         codeIRQueue: codeIRQueue
       };