Browse Source

Merge branch 'strokeText' into text

v1.x
Tj Holowaychuk 15 years ago
parent
commit
a1de0b0524
  1. 16
      lib/canvas.js
  2. 28
      src/CanvasRenderingContext2d.cc
  3. 1
      src/CanvasRenderingContext2d.h
  4. 26
      test/canvas.test.js

16
lib/canvas.js

@ -338,6 +338,22 @@ Context2d.prototype.setTransform = function(){
this.transform.apply(this, arguments); 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. * Set the fill style with the given css color string.
* *

28
src/CanvasRenderingContext2d.cc

@ -88,6 +88,7 @@ Context2d::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "strokeRect", StrokeRect); NODE_SET_PROTOTYPE_METHOD(t, "strokeRect", StrokeRect);
NODE_SET_PROTOTYPE_METHOD(t, "clearRect", ClearRect); NODE_SET_PROTOTYPE_METHOD(t, "clearRect", ClearRect);
NODE_SET_PROTOTYPE_METHOD(t, "rect", Rect); 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, "fillText", FillText);
NODE_SET_PROTOTYPE_METHOD(t, "moveTo", MoveTo); NODE_SET_PROTOTYPE_METHOD(t, "moveTo", MoveTo);
NODE_SET_PROTOTYPE_METHOD(t, "lineTo", LineTo); NODE_SET_PROTOTYPE_METHOD(t, "lineTo", LineTo);
@ -843,6 +844,33 @@ Context2d::SetFont(const Arguments &args) {
return Undefined(); return Undefined();
} }
/*
* Stroke text at x, y.
*/
Handle<Value>
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<Context2d>(args.This());
cairo_t *ctx = context->getContext();
cairo_text_extents_t te;
cairo_move_to(ctx, x, y);
cairo_text_path(ctx, *str);
return Undefined();
}
/* /*
* Fill text at x, y. * Fill text at x, y.
*/ */

1
src/CanvasRenderingContext2d.h

@ -64,6 +64,7 @@ class Context2d: public node::ObjectWrap {
static Handle<Value> SetShadowRGBA(const Arguments &args); static Handle<Value> SetShadowRGBA(const Arguments &args);
static Handle<Value> SetFillPattern(const Arguments &args); static Handle<Value> SetFillPattern(const Arguments &args);
static Handle<Value> SetStrokePattern(const Arguments &args); static Handle<Value> SetStrokePattern(const Arguments &args);
static Handle<Value> StrokeText(const Arguments &args);
static Handle<Value> FillText(const Arguments &args); static Handle<Value> FillText(const Arguments &args);
static Handle<Value> BezierCurveTo(const Arguments &args); static Handle<Value> BezierCurveTo(const Arguments &args);
static Handle<Value> QuadraticCurveTo(const Arguments &args); static Handle<Value> QuadraticCurveTo(const Arguments &args);

26
test/canvas.test.js

@ -721,6 +721,32 @@ module.exports = {
, 'Context2d#fillText() transformations failed'); , '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){ 'test Canvas#toBuffer()': function(assert){
assert.ok(Buffer.isBuffer(new Canvas(200, 200).toBuffer()), 'Canvas#toBuffer() failed'); assert.ok(Buffer.isBuffer(new Canvas(200, 200).toBuffer()), 'Canvas#toBuffer() failed');
}, },

Loading…
Cancel
Save