]> git.parisson.com Git - pdf.js.git/commitdiff
move unicode translation cache into Font
authorAndreas Gal <andreas.gal@gmail.com>
Fri, 8 Jul 2011 23:36:50 +0000 (16:36 -0700)
committerAndreas Gal <andreas.gal@gmail.com>
Fri, 8 Jul 2011 23:36:50 +0000 (16:36 -0700)
fonts.js
pdf.js

index 1f1db3652e0040843f8a222b71e56c518b2f1716..7bdf7cefce6dccb11761fa746a9fc473c98fe32e 100755 (executable)
--- a/fonts.js
+++ b/fonts.js
@@ -38,12 +38,10 @@ var Fonts = (function Fonts() {
     this.properties = properties;
     this.id = fontCount++;
     this.loading = true;
-    this.charsCache = Object.create(null);
     this.sizes = [];
   }
 
   var current;
-  var charsCache;
   var measureCache;
 
   return {
@@ -64,51 +62,15 @@ var Fonts = (function Fonts() {
       // |current| can be null is fontName is a built-in font
       // (e.g. "sans-serif")
       if (fontObj && (current = fonts[fontObj.id])) {
-        charsCache = current.charsCache;
         var sizes = current.sizes;
         if (!(measureCache = sizes[size]))
           measureCache = sizes[size] = Object.create(null);
       } else {
-        charsCache = null;
         measureCache = null
       }
 
       ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"';
     },
-    charsToUnicode: function fonts_chars2Unicode(chars) {
-      if (!charsCache)
-        return chars;
-
-      // if we translated this string before, just grab it from the cache
-      var str = charsCache[chars];
-      if (str)
-        return str;
-
-      // translate the string using the font's encoding
-      var encoding = current ? current.properties.encoding : null;
-      if (!encoding)
-        return chars;
-
-      str = '';
-      for (var i = 0; i < chars.length; ++i) {
-        var charcode = chars.charCodeAt(i);
-        var unicode = encoding[charcode];
-
-        // Check if the glyph has already been converted
-        if (!IsNum(unicode))
-          unicode = encoding[unicode] = GlyphsUnicode[unicode.name];
-
-        // Handle surrogate pairs
-        if (unicode > 0xFFFF) {
-          str += String.fromCharCode(unicode & 0xFFFF);
-          unicode >>= 16;
-        }
-        str += String.fromCharCode(unicode);
-      }
-
-      // Enter the translated string into the cache
-      return charsCache[chars] = str;
-    },
     measureText: function fonts_measureText(text) {
       var width;
       if (measureCache && (width = measureCache[text]))
@@ -1146,6 +1108,46 @@ var Font = (function() {
       styleSheet.insertRule(rule, styleSheet.cssRules.length);
 
       return rule;
+    },
+
+    charsToUnicode: function fonts_chars2Unicode(chars) {
+      var charsCache = this.charsCache;
+
+      // if we translated this string before, just grab it from the cache
+      if (charsCache) {
+        var str = charsCache[chars];
+        if (str)
+          return str;
+      }
+
+      // translate the string using the font's encoding
+      var encoding = this.encoding;
+      if (!encoding)
+        return chars;
+
+      // lazily create the translation cache
+      if (!charsCache)
+        charsCache = this.charsCache = Object.create(null);
+
+      str = '';
+      for (var i = 0; i < chars.length; ++i) {
+        var charcode = chars.charCodeAt(i);
+        var unicode = encoding[charcode];
+
+        // Check if the glyph has already been converted
+        if (!IsNum(unicode))
+          unicode = encoding[unicode] = GlyphsUnicode[unicode.name];
+
+        // Handle surrogate pairs
+        if (unicode > 0xFFFF) {
+          str += String.fromCharCode(unicode & 0xFFFF);
+          unicode >>= 16;
+        }
+        str += String.fromCharCode(unicode);
+      }
+
+      // Enter the translated string into the cache
+      return charsCache[chars] = str;
     }
   };
 
diff --git a/pdf.js b/pdf.js
index e357732f234ed9ddbd9c4fd0a8bd775001a32c95..8fb56bccfbc2990b82859f7dcdb7f0a97e4aa5fc 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -3933,14 +3933,14 @@ var CanvasGraphics = (function() {
       ctx.scale(1, -1);
 
       if (this.ctx.$showText) {
-        ctx.$showText(current.y, Fonts.charsToUnicode(text));
+        ctx.$showText(current.y, text);
       } else {
-        text = Fonts.charsToUnicode(text);
         ctx.translate(current.x, -1 * current.y);
-
         var font = this.current.font;
-        if (font)
+        if (font) {
           ctx.transform.apply(ctx, font.textMatrix);
+          text = font.charsToUnicode(text);
+        }
         ctx.fillText(text, 0, 0);
         current.x += Fonts.measureText(text);
       }