]> git.parisson.com Git - pdf.js.git/commitdiff
Expose FontMeasure only if running on the main thread as the worker doesnt have a...
authorJulian Viereck <julian.viereck@gmail.com>
Tue, 6 Sep 2011 01:10:57 +0000 (18:10 -0700)
committerJulian Viereck <julian.viereck@gmail.com>
Thu, 15 Sep 2011 15:20:03 +0000 (08:20 -0700)
fonts.js

index ed2496727d97c73bfa969204700b5f753ab14fcf..69bb50590ca91f3e296b7ae3dd2edbb8d54b846b 100755 (executable)
--- a/fonts.js
+++ b/fonts.js
@@ -117,6 +117,47 @@ var serifFonts = {
   'Wide Latin': true, 'Windsor': true, 'XITS': true
 };
 
+// Create the FontMeasure object only on the main thread.
+if (!isWorker) {
+  var FontMeasure = (function FontMeasure() {
+    var kScalePrecision = 30;
+    var ctx = document.createElement('canvas').getContext('2d');
+    ctx.scale(1 / kScalePrecision, 1);
+
+    var current;
+    var measureCache;
+
+    return {
+      setActive: function fonts_setActive(font, size) {
+        if (current == font) {
+          var sizes = current.sizes;
+          if (!(measureCache = sizes[size]))
+            measureCache = sizes[size] = Object.create(null);
+        } else {
+          measureCache = null;
+        }
+
+        var name = font.loadedName;
+        var bold = font.bold ? 'bold' : 'normal';
+        var italic = font.italic ? 'italic' : 'normal';
+        size *= kScalePrecision;
+        var rule = italic + ' ' + bold + ' ' + size + 'px "' + name + '"';
+        ctx.font = rule;
+        current = font;
+      },
+      measureText: function fonts_measureText(text) {
+        var width;
+        if (measureCache && (width = measureCache[text]))
+          return width;
+        width = ctx.measureText(text).width / kScalePrecision;
+        if (measureCache)
+          measureCache[text] = width;
+        return width;
+      }
+    };
+  })();  
+}
+
 var FontLoader = {
   listeningForFontLoad: false,