]> git.parisson.com Git - pdf.js.git/commitdiff
Refactoring charsToUnicode into charsToGlyphs
authornotmasteryet <async.processingjs@yahoo.com>
Sun, 11 Sep 2011 01:21:20 +0000 (20:21 -0500)
committernotmasteryet <async.processingjs@yahoo.com>
Sun, 11 Sep 2011 01:21:20 +0000 (20:21 -0500)
fonts.js
pdf.js

index cc353c03b3d5cca1ea2a0f13628fa0f57269b95f..ac070ed182fc199171113ab511bad1a024eff030 100755 (executable)
--- a/fonts.js
+++ b/fonts.js
@@ -1296,7 +1296,7 @@ var Font = (function Font() {
       return rule;
     },
 
-    charsToUnicode: function fonts_chars2Unicode(chars) {
+    charsToGlyphs: function fonts_chars2Glyphs(chars) {
       var charsCache = this.charsCache;
       var str;
 
@@ -1315,7 +1315,7 @@ var Font = (function Font() {
       var encoding = this.encoding;
       if (!encoding)
         return chars;
-      str = '';
+      var glyphs = [];
 
       if (this.composite) {
         // composite fonts have multi-byte strings convert the string from
@@ -1329,11 +1329,9 @@ var Font = (function Font() {
           var unicode = encoding[charcode];
           if ('undefined' == typeof(unicode)) {
             warn('Unencoded charcode ' + charcode);
-            unicode = charcode;
-          } else {
-            unicode = unicode.unicode;
+            unicode = { unicode: charcode };
           }
-          str += String.fromCharCode(unicode);
+          glyphs.push(unicode);
         }
       }
       else {
@@ -1342,22 +1340,14 @@ var Font = (function Font() {
           var unicode = encoding[charcode];
           if ('undefined' == typeof(unicode)) {
             warn('Unencoded charcode ' + charcode);
-            unicode = charcode;
-          } else {
-            unicode = unicode.unicode;
-          }
-
-          // Handle surrogate pairs
-          if (unicode > 0xFFFF) {
-            str += String.fromCharCode(unicode & 0xFFFF);
-            unicode >>= 16;
+            unicode = { unicode: charcode };
           }
-          str += String.fromCharCode(unicode);
+          glyphs.push(unicode);
         }
       }
 
       // Enter the translated string into the cache
-      return charsCache[chars] = str;
+      return charsCache[chars] = glyphs;
     }
   };
 
diff --git a/pdf.js b/pdf.js
index 3e824952411ab9a446d6552d2d047608c8637049..17ef1f3ca0c1bf809b45f2b841366eb4d3d66d10 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -4954,7 +4954,6 @@ var CanvasGraphics = (function() {
     showText: function(text) {
       var ctx = this.ctx;
       var current = this.current;
-      var originalText = text;
 
       ctx.save();
       ctx.transform.apply(ctx, current.textMatrix);
@@ -4963,9 +4962,15 @@ var CanvasGraphics = (function() {
       ctx.translate(current.x, -1 * current.y);
 
       var font = current.font;
+      var glyphs = [];
       if (font) {
         ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX);
-        text = font.charsToUnicode(text);
+        glyphs = font.charsToGlyphs(text);
+      } else {
+        // fallback to simple glyphs
+        glyphs = [];
+        for (var i = 0; i < text.length; ++i)
+          glyphs.push({unicode: text.charCodeAt(i)});
       }
 
       var composite = font.composite;
@@ -4977,21 +4982,19 @@ var CanvasGraphics = (function() {
       ctx.scale(1 / textHScale, 1);
 
       var width = 0;
-      for (var i = 0; i < text.length; i++) {
-        if (composite) {
-          var position = i * 2 + 1;
-          var charcode = (originalText.charCodeAt(position - 1) << 8) +
-                          originalText.charCodeAt(position);
-        } else {
-          var charcode = originalText.charCodeAt(i);
-        }
-
-        var charWidth = font.encoding[charcode].width * fontSize * 0.001;
+      for (var i = 0; i < glyphs.length; i++) {
+        var glyph = glyphs[i];
+        var unicode = glyph.unicode;
+        var char = unicode >= 0x10000 ?
+          String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
+          0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
+
+        var charWidth = glyph.width * fontSize * 0.001;
         charWidth += charSpacing;
-        if (charcode == 32)
+        if (unicode == 32)
           charWidth += wordSpacing;
 
-        ctx.fillText(text.charAt(i), 0, 0);
+        ctx.fillText(char, 0, 0);
         ctx.translate(charWidth, 0);
         width += charWidth;
       }