Browse Source

Added style support to parseFont()

v1.x
Tj Holowaychuk 14 years ago
parent
commit
58b55691fd
  1. 9
      lib/canvas.js
  2. 15
      test/canvas.test.js

9
lib/canvas.js

@ -76,11 +76,12 @@ function normalizedColor(prop) {
exports.parseFont = function(str){
if (cache[str]) return cache[str];
var obj = {}
, captures = /^ *(\d+)(px|pt) *((?:"([^"]+)"|[\w-]+)( *, *(?:"([^"]+)"|[\w-]+))*)/.exec(str);
, captures = /^ *(?:(normal|italic|oblique) *)?(\d+)(px|pt) *((?:"([^"]+)"|[\w-]+)( *, *(?:"([^"]+)"|[\w-]+))*)/.exec(str);
if (!captures) return;
obj.size = parseInt(captures[1], 10);
obj.unit = captures[2];
obj.family = captures[3];
obj.style = captures[1];
obj.size = parseInt(captures[2], 10);
obj.unit = captures[3];
obj.family = captures[4];
return cache[str] = obj;
};

15
test/canvas.test.js

@ -79,12 +79,23 @@ module.exports = {
, { size: 50, unit: 'px', family: '"Helvetica Nueue", sans-serif' }
, '50px "Helvetica Nueue", "foo bar baz" , sans-serif'
, { size: 50, unit: 'px', family: '"Helvetica Nueue", "foo bar baz" , sans-serif' }
, 'italic 20px Arial'
, { size: 20, unit: 'px', style: 'italic', family: 'Arial' }
, 'oblique 20px Arial'
, { size: 20, unit: 'px', style: 'oblique', family: 'Arial' }
, 'normal 20px Arial'
, { size: 20, unit: 'px', style: 'normal', family: 'Arial' }
];
for (var i = 0, len = tests.length; i < len; ++i) {
var str = tests[i++]
, obj = tests[i];
assert.eql(obj, Canvas.parseFont(str));
, obj = tests[i]
, got = Canvas.parseFont(str);
if (!obj.style) obj.style = undefined;
assert.eql(obj, got, ''
+ '\n from: ' + JSON.stringify(str)
+ '\n got: ' + JSON.stringify(got)
+ '\n expected: ' + JSON.stringify(obj));
}
},

Loading…
Cancel
Save