]> git.parisson.com Git - pdf.js.git/commitdiff
Add a fake canvas for scaling fonts to improve perfs
authorVivien Nicolas <21@vingtetun.org>
Tue, 28 Jun 2011 16:24:16 +0000 (18:24 +0200)
committerVivien Nicolas <21@vingtetun.org>
Tue, 28 Jun 2011 16:24:16 +0000 (18:24 +0200)
fonts.js
pdf.js

index d69746993ff18849c61987da7f67daeef6a1286a..4ac5935b5c26755e5f984766c4aedeebc88cafab 100644 (file)
--- a/fonts.js
+++ b/fonts.js
@@ -26,12 +26,15 @@ var fontName  = "";
  */
 var kDisableFonts = false;
 
+
 /**
  * Hold a map of decoded fonts and of the standard fourteen Type1 fonts and
  * their acronyms.
  * TODO Add the standard fourteen Type1 fonts list by default
  *      http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65
  */
+
+var kScalePrecision = 100;
 var Fonts = {
   _active: null,
 
@@ -39,8 +42,9 @@ var Fonts = {
     return this._active;
   },
 
-  set active(name) {
+  setActive: function fonts_setActive(name, size) {
     this._active = this[name];
+    this.ctx.font = (size * kScalePrecision) + 'px "' + name + '"';
   },
 
   charsToUnicode: function fonts_chars2Unicode(chars) {
@@ -77,6 +81,17 @@ var Fonts = {
 
     // Enter the translated string into the cache
     return active.cache[chars] = str;
+  },
+
+  get ctx() {
+    delete this.ctx;
+    var ctx = document.createElement("canvas").getContext("2d");
+    ctx.scale(1 / kScalePrecision, 1);
+    return this.ctx = ctx;
+  },
+
+  measureText: function fonts_measureText(text) {
+    return this.ctx.measureText(text).width / kScalePrecision;
   }
 };
 
@@ -1292,7 +1307,7 @@ CFF.prototype = {
       var glyph = glyphs[i];
       var unicode = GlyphsUnicode[glyph.glyph];
       if (!unicode) {
-        if (glyph != ".notdef")
+        if (glyph.glyph != ".notdef")
           warn(glyph + " does not have an entry in the glyphs unicode dictionary");
       } else {
         charstrings.push({
diff --git a/pdf.js b/pdf.js
index 2d25cf707943b95dab742348068f9f442e06e426..f09ccd866f1796a48a58b4913bf8d8f5f359b3ef 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -2797,19 +2797,20 @@ var CanvasGraphics = (function() {
             if (fontDescriptor && fontDescriptor.num) {
                 var fontDescriptor = this.xref.fetchIfRef(fontDescriptor);
                 fontName = fontDescriptor.get("FontName").name.replace("+", "_");
-                Fonts.active = fontName;
+                Fonts.setActive(fontName, size);
             }
 
             if (!fontName) {
                 // TODO: fontDescriptor is not available, fallback to default font
                 this.current.fontSize = size;
                 this.ctx.font = this.current.fontSize + 'px sans-serif';
+                Fonts.setActive("sans-serif", this.current.fontSize);
                 return;
             }
 
             this.current.fontName = fontName;
             this.current.fontSize = size;
-            this.ctx.font = this.current.fontSize +'px "' + fontName + '", Symbol';
+            this.ctx.font = this.current.fontSize + 'px "' + this.current.fontName + '"';
         },
         setTextRenderingMode: function(mode) {
             TODO("text rendering mode");
@@ -2851,11 +2852,7 @@ var CanvasGraphics = (function() {
                 text = Fonts.charsToUnicode(text);
                 this.ctx.translate(this.current.x, -1 * this.current.y);
                 this.ctx.fillText(text, 0, 0);
-
-                this.ctx.scale(0.05, 1);
-                this.ctx.font = (this.current.fontSize * 20) + 'px "' + this.current.fontName + '", Symbol';
-                this.current.x += this.ctx.measureText(text).width / 20;
-                this.ctx.scale(1, 1);
+                this.current.x += Fonts.measureText(text);
             }
 
             this.ctx.restore();