Browse Source

Added textAlign=

v1.x
Tj Holowaychuk 14 years ago
parent
commit
888cb0198d
  1. 36
      lib/canvas.js
  2. 18
      src/CanvasRenderingContext2d.cc
  3. 2
      src/CanvasRenderingContext2d.h

36
lib/canvas.js

@ -501,3 +501,39 @@ Context2d.prototype.__defineSetter__('font', function(val){
Context2d.prototype.__defineGetter__('font', function(){
return this.lastFontString || '10px sans-serif';
});
/**
* Set text alignment.
*
* @api public
*/
Context2d.prototype.__defineSetter__('textAlign', function(val){
switch (val) {
case 'center':
this.setTextAlignment(0);
this.lastTextAlignment = val;
break;
case 'left':
case 'start':
this.setTextAlignment(-1);
this.lastTextAlignment = val;
break;
case 'right':
case 'end':
this.setTextAlignment(1);
this.lastTextAlignment = val;
break;
}
});
/**
* Get the current font.
*
* @see exports.parseFont()
* @api public
*/
Context2d.prototype.__defineGetter__('textAlign', function(){
return this.lastTextAlignment || 'start';
});

18
src/CanvasRenderingContext2d.cc

@ -88,6 +88,7 @@ Context2d::Initialize(Handle<Object> 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, "setTextAlignment", SetTextAlignment);
NODE_SET_PROTOTYPE_METHOD(t, "setTextPath", SetTextPath);
NODE_SET_PROTOTYPE_METHOD(t, "measureText", MeasureText);
NODE_SET_PROTOTYPE_METHOD(t, "moveTo", MoveTo);
@ -139,6 +140,7 @@ Context2d::Context2d(Canvas *canvas): ObjectWrap() {
shadowBlur = shadowOffsetX = shadowOffsetY = 0;
state = states[stateno = 0] = (canvas_state_t *) malloc(sizeof(canvas_state_t));
state->globalAlpha = 1;
state->textAlignment -1;
state->fillPattern = state->strokePattern = NULL;
RGBA(state->fill,0,0,0,1);
RGBA(state->stroke,0,0,0,1);
@ -866,6 +868,22 @@ Context2d::MeasureText(const Arguments &args) {
return scope.Close(obj);
}
/*
* Set text alignment. -1 0 1
*/
Handle<Value>
Context2d::SetTextAlignment(const Arguments &args) {
HandleScope scope;
if (!args[0]->IsInt32()) return Undefined();
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
context->state->textAlignment = args[0]->Int32Value();
return Undefined();
}
/*
* Set text path at x, y.
*/

2
src/CanvasRenderingContext2d.h

@ -32,6 +32,7 @@ typedef struct {
cairo_pattern_t *fillPattern;
cairo_pattern_t *strokePattern;
float globalAlpha;
short textAlignment;
} canvas_state_t;
class Context2d: public node::ObjectWrap {
@ -64,6 +65,7 @@ class Context2d: public node::ObjectWrap {
static Handle<Value> SetShadowRGBA(const Arguments &args);
static Handle<Value> SetFillPattern(const Arguments &args);
static Handle<Value> SetStrokePattern(const Arguments &args);
static Handle<Value> SetTextAlignment(const Arguments &args);
static Handle<Value> SetTextPath(const Arguments &args);
static Handle<Value> MeasureText(const Arguments &args);
static Handle<Value> BezierCurveTo(const Arguments &args);

Loading…
Cancel
Save