From 3b28bdf82cba79caa5c74c32ddb166a8c8602c14 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 1 Nov 2010 16:32:25 -0700 Subject: [PATCH] Added Context2d#font= tests --- lib/canvas.js | 31 ++++++++++++++++++++++++++++++- test/canvas.test.js | 9 +++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/canvas.js b/lib/canvas.js index ed79752..7dbb7d4 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -93,7 +93,15 @@ function normalizedColor(prop) { } } -exports.parseFont = function(str){ +/** + * Parse font `str`. + * + * @param {String} str + * @return {Object} + * @api public + */ + +var parseFont = exports.parseFont = function(str){ if (cache[str]) return cache[str]; var obj = {} , captures = font.exec(str); @@ -391,6 +399,7 @@ Context2d.prototype.__defineSetter__('strokeStyle', function(val){ */ Context2d.prototype.__defineGetter__('strokeStyle', normalizedColor('lastStrokeStyle')); + /** * Set the shadow color with the given css color string. * @@ -419,3 +428,23 @@ Context2d.prototype.__defineSetter__('shadowColor', function(val){ */ Context2d.prototype.__defineGetter__('shadowColor', normalizedColor('lastShadowColor')); + +Context2d.prototype.__defineSetter__('font', function(val){ + if ('string' == typeof val) { + var font; + if (font = parseFont(val)) { + this.lastFontString = val; + } + } +}); + +/** + * Get the current font. + * + * @see exports.parseFont() + * @api public + */ + +Context2d.prototype.__defineGetter__('font', function(){ + return this.lastFontString || '10px sans-serif'; +}); \ No newline at end of file diff --git a/test/canvas.test.js b/test/canvas.test.js index 739c6cc..83565b0 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -352,6 +352,15 @@ module.exports = { , 'Context2d#rect() failed'); }, + 'test Context2d#font=': function(assert){ + var canvas = new Canvas(200, 200) + , ctx = canvas.getContext('2d'); + + assert.equal('10px sans-serif', ctx.font); + ctx.font = '15px Arial, sans-serif'; + assert.equal('15px Arial, sans-serif', ctx.font); + }, + 'test Context2d#fillStyle=': function(assert){ var canvas = new Canvas(200, 200) , ctx = canvas.getContext('2d')