From 0fbefba9982da197b908708854588b5d38c6a0d8 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 08:53:00 -0700 Subject: [PATCH 1/3] Started strokeText() --- src/CanvasRenderingContext2d.cc | 28 ++++++++++++++++++++++++++++ src/CanvasRenderingContext2d.h | 1 + 2 files changed, 29 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 8de9f14..05a42b8 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -88,6 +88,7 @@ Context2d::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "strokeRect", StrokeRect); NODE_SET_PROTOTYPE_METHOD(t, "clearRect", ClearRect); NODE_SET_PROTOTYPE_METHOD(t, "rect", Rect); + NODE_SET_PROTOTYPE_METHOD(t, "strokeText", StrokeText); NODE_SET_PROTOTYPE_METHOD(t, "fillText", FillText); NODE_SET_PROTOTYPE_METHOD(t, "moveTo", MoveTo); NODE_SET_PROTOTYPE_METHOD(t, "lineTo", LineTo); @@ -843,6 +844,33 @@ Context2d::SetFont(const Arguments &args) { return Undefined(); } +/* + * Stroke text at x, y. + */ + +Handle +Context2d::StrokeText(const Arguments &args) { + HandleScope scope; + + // Ignore when args are not present + if (!args[0]->IsString() + || !args[1]->IsNumber() + || !args[2]->IsNumber()) return Undefined(); + + String::Utf8Value str(args[0]); + + double x = args[1]->NumberValue() + , y = args[2]->NumberValue(); + + Context2d *context = ObjectWrap::Unwrap(args.This()); + cairo_t *ctx = context->getContext(); + cairo_text_extents_t te; + cairo_move_to(ctx, x, y); + cairo_show_text(ctx, *str); + + return Undefined(); +} + /* * Fill text at x, y. */ diff --git a/src/CanvasRenderingContext2d.h b/src/CanvasRenderingContext2d.h index b89b78f..27abeb4 100644 --- a/src/CanvasRenderingContext2d.h +++ b/src/CanvasRenderingContext2d.h @@ -64,6 +64,7 @@ class Context2d: public node::ObjectWrap { static Handle SetShadowRGBA(const Arguments &args); static Handle SetFillPattern(const Arguments &args); static Handle SetStrokePattern(const Arguments &args); + static Handle StrokeText(const Arguments &args); static Handle FillText(const Arguments &args); static Handle BezierCurveTo(const Arguments &args); static Handle QuadraticCurveTo(const Arguments &args); From 51e013abc0a836cf546809d040c9f1c7468d0f72 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 08:58:48 -0700 Subject: [PATCH 2/3] Added Context2d#strokeText() --- lib/canvas.js | 16 ++++++++++++++++ src/CanvasRenderingContext2d.cc | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/canvas.js b/lib/canvas.js index cd40e37..f8d795f 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -338,6 +338,22 @@ Context2d.prototype.setTransform = function(){ this.transform.apply(this, arguments); }; +/** + * Set text path then `stroke()`. + * + * @param {String} str + * @param {Number} x + * @param {Number} y + * @api public + */ + +var strokeText = Context2d.prototype.strokeText; +Context2d.prototype.strokeText = function(str, x, y){ + this.beginPath(); + strokeText.call(this, str, x, y); + return this.stroke(); +}; + /** * Set the fill style with the given css color string. * diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 05a42b8..e07ff84 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -866,7 +866,7 @@ Context2d::StrokeText(const Arguments &args) { cairo_t *ctx = context->getContext(); cairo_text_extents_t te; cairo_move_to(ctx, x, y); - cairo_show_text(ctx, *str); + cairo_text_path(ctx, *str); return Undefined(); } From 66229d222f23bff303a3a4370e874338fbe78b9b Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 09:00:14 -0700 Subject: [PATCH 3/3] Added strokeText() test --- test/canvas.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/canvas.test.js b/test/canvas.test.js index f591e7d..cffbf2e 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -721,6 +721,32 @@ module.exports = { , 'Context2d#fillText() transformations failed'); }, + 'test Context2d#strokeText()': function(assert){ + var canvas = new Canvas(200, 200) + , ctx = canvas.getContext('2d') + , path = __dirname + '/images/strokeText.png'; + + ctx.strokeRect(0,0,200,200); + ctx.lineTo(0,100); + ctx.lineTo(200,100); + ctx.stroke(); + + ctx.beginPath(); + ctx.lineTo(100,0); + ctx.lineTo(100,200); + ctx.stroke(); + + ctx.strokeStyle = 'red'; + ctx.font = 'normal 50px Impact'; + ctx.strokeText("bar", 100, 100); + + assertChecksum( + canvas + , path + , 'e5f6d8a3c57e1454c4c79358a32f1c2c' + , 'Context2d#strokeText()'); + }, + 'test Canvas#toBuffer()': function(assert){ assert.ok(Buffer.isBuffer(new Canvas(200, 200).toBuffer()), 'Canvas#toBuffer() failed'); },