From 5a1488df9fe7412bdc6348e966a06adbef89642b Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Mon, 5 Sep 2011 18:10:57 -0700 Subject: [PATCH] Expose FontMeasure only if running on the main thread as the worker doesnt have a document to attach the canvas to --- fonts.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/fonts.js b/fonts.js index ed2496727..69bb50590 100755 --- 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,