Browse Source

Added weight support to parseFont()

v1.x
Tj Holowaychuk 14 years ago
parent
commit
e35c461d93
  1. 11
      lib/canvas.js
  2. 16
      test/canvas.test.js

11
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;
};

16
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));
}
},

Loading…
Cancel
Save