From eb8c5b4a3ea2f34b0dc0c5a4843ef8e5b1dddd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 19 Aug 2015 12:57:22 +0200 Subject: [PATCH] Handle em and pt font sizes correctly on Firefox. Closes #734 --- src/view/CanvasView.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/view/CanvasView.js b/src/view/CanvasView.js index 498fd76c..996ad97e 100644 --- a/src/view/CanvasView.js +++ b/src/view/CanvasView.js @@ -84,15 +84,28 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ /** * Converts the provide size in any of the units allowed in the browser to - * pixels, by the use of the context.font property. + * pixels. */ getPixelSize: function(size) { - var ctx = this._context, - prevFont = ctx.font; - ctx.font = size + ' serif'; - size = parseFloat(ctx.font); - ctx.font = prevFont; - return size; + var browser = paper.browser, + pixels; + if (browser && browser.firefox) { + // Firefox doesn't appear to convert context.font sizes to pixels, + // while other browsers do. Workaround: + var parent = this._element.parentNode, + temp = document.createElement('div'); + temp.style.fontSize = size; + parent.appendChild(temp); + pixels = parseFloat(DomElement.getStyles(temp).fontSize); + parent.removeChild(temp); + } else { + var ctx = this._context, + prevFont = ctx.font; + ctx.font = size + ' serif'; + pixels = parseFloat(ctx.font); + ctx.font = prevFont; + } + return pixels; }, getTextWidth: function(font, lines) {