Browse Source

Merge branch 'textBaseline'

v1.x
Tj Holowaychuk 14 years ago
parent
commit
8adbb8488d
  1. 30
      lib/canvas.js
  2. 45
      src/CanvasRenderingContext2d.cc
  3. 2
      src/CanvasRenderingContext2d.h

30
lib/canvas.js

@ -41,6 +41,12 @@ exports.cairoVersion = cairoVersion;
var cache = {};
/**
* Text baselines.
*/
var baselines = ['alphabetic', 'top', 'bottom', 'middle', 'ideographic', 'hanging'];
/**
* Font RegExp helpers.
*/
@ -502,6 +508,30 @@ Context2d.prototype.__defineGetter__('font', function(){
return this.lastFontString || '10px sans-serif';
});
/**
* Set text baseline.
*
* @api public
*/
Context2d.prototype.__defineSetter__('textBaseline', function(val){
var n = baselines.indexOf(val);
if (~n) {
this.lastBaseline = val;
this.setTextBaseline(n);
}
});
/**
* Get the current baseline setting.
*
* @api public
*/
Context2d.prototype.__defineGetter__('textAlign', function(){
return this.lastBaseline || 'alphabetic';
});
/**
* Set text alignment.
*

45
src/CanvasRenderingContext2d.cc

@ -59,6 +59,19 @@ using namespace node;
int width = args[2]->Int32Value(); \
int height = args[3]->Int32Value();
/*
* Text baselines.
*/
enum {
TEXT_BASELINE_ALPHABETIC
, TEXT_BASELINE_TOP
, TEXT_BASELINE_BOTTOM
, TEXT_BASELINE_MIDDLE
, TEXT_BASELINE_IDEOGRAPHIC
, TEXT_BASELINE_HANGING
};
/*
* Initialize Context2d.
*/
@ -88,6 +101,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, "setTextBaseline", SetTextBaseline);
NODE_SET_PROTOTYPE_METHOD(t, "setTextAlignment", SetTextAlignment);
NODE_SET_PROTOTYPE_METHOD(t, "setTextPath", SetTextPath);
NODE_SET_PROTOTYPE_METHOD(t, "measureText", MeasureText);
@ -868,6 +882,21 @@ Context2d::MeasureText(const Arguments &args) {
return scope.Close(obj);
}
/*
* Set text baseline.
*/
Handle<Value>
Context2d::SetTextBaseline(const Arguments &args) {
HandleScope scope;
if (!args[0]->IsInt32()) return Undefined();
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
context->state->textBaseline = args[0]->Int32Value();
return Undefined();
}
/*
* Set text alignment. -1 0 1
*/
@ -883,7 +912,6 @@ Context2d::SetTextAlignment(const Arguments &args) {
return Undefined();
}
/*
* Set text path at x, y.
*/
@ -921,6 +949,21 @@ Context2d::SetTextPath(const Arguments &args) {
break;
}
// Baseline approx
// TODO:
switch (context->state->textBaseline) {
case TEXT_BASELINE_TOP:
case TEXT_BASELINE_HANGING:
y += te.height;
break;
case TEXT_BASELINE_MIDDLE:
y += te.height / 2;
break;
case TEXT_BASELINE_BOTTOM:
y -= te.height / 2;
break;
}
cairo_move_to(ctx, x, y);
cairo_text_path(ctx, *str);

2
src/CanvasRenderingContext2d.h

@ -33,6 +33,7 @@ typedef struct {
cairo_pattern_t *strokePattern;
float globalAlpha;
short textAlignment;
short textBaseline;
} canvas_state_t;
class Context2d: public node::ObjectWrap {
@ -65,6 +66,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> SetTextBaseline(const Arguments &args);
static Handle<Value> SetTextAlignment(const Arguments &args);
static Handle<Value> SetTextPath(const Arguments &args);
static Handle<Value> MeasureText(const Arguments &args);

Loading…
Cancel
Save