From e35c461d93da9033a30e24048cbfc5157f4b41a5 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 1 Nov 2010 15:51:37 -0700 Subject: [PATCH] Added weight support to parseFont() --- lib/canvas.js | 11 ++++++----- test/canvas.test.js | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/canvas.js b/lib/canvas.js index b172509..cc1a20a 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -76,12 +76,13 @@ function normalizedColor(prop) { exports.parseFont = function(str){ if (cache[str]) return cache[str]; var obj = {} - , captures = /^ *(?:(normal|italic|oblique) *)?(\d+)(px|pt) *((?:"([^"]+)"|[\w-]+)( *, *(?:"([^"]+)"|[\w-]+))*)/.exec(str); + , captures = /^ *(?:(normal|bold|bolder|lighter|[1-9](?:00)) *)?(?:(normal|italic|oblique) *)?(\d+)(px|pt) *((?:"([^"]+)"|[\w-]+)( *, *(?:"([^"]+)"|[\w-]+))*)/.exec(str); if (!captures) return; - obj.style = captures[1]; - obj.size = parseInt(captures[2], 10); - obj.unit = captures[3]; - obj.family = captures[4]; + obj.weight = captures[1] || 'normal'; + obj.style = captures[2] || 'normal'; + obj.size = parseInt(captures[3], 10); + obj.unit = captures[4]; + obj.family = captures[5]; return cache[str] = obj; }; diff --git a/test/canvas.test.js b/test/canvas.test.js index 6b1afe3..a8d2e62 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -6,6 +6,7 @@ var Canvas = require('canvas') , assert = require('assert') , crypto = require('crypto') + , sys = require('sys') , fs = require('fs'); function hash(val) { @@ -85,17 +86,24 @@ module.exports = { , { size: 20, unit: 'px', style: 'oblique', family: 'Arial' } , 'normal 20px Arial' , { size: 20, unit: 'px', style: 'normal', family: 'Arial' } + , '800 20px Arial' + , { size: 20, unit: 'px', weight: '800', family: 'Arial' } + , 'bolder 20px Arial' + , { size: 20, unit: 'px', weight: 'bolder', family: 'Arial' } + , 'lighter 20px Arial' + , { size: 20, unit: 'px', weight: 'lighter', family: 'Arial' } ]; for (var i = 0, len = tests.length; i < len; ++i) { var str = tests[i++] , obj = tests[i] , got = Canvas.parseFont(str); - if (!obj.style) obj.style = undefined; + if (!obj.style) obj.style = 'normal'; + if (!obj.weight) obj.weight = 'normal'; assert.eql(obj, got, '' - + '\n from: ' + JSON.stringify(str) - + '\n got: ' + JSON.stringify(got) - + '\n expected: ' + JSON.stringify(obj)); + + '\n from: ' + sys.inspect(str) + + '\n got:\n' + sys.inspect(got) + + '\n expected:\n' + sys.inspect(obj)); } },