From 253e200957c8982cf2e0dc231a81a2d7d070dd35 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 13:53:29 -0700 Subject: [PATCH 1/3] Added textBaseline= --- lib/canvas.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/canvas.js b/lib/canvas.js index 99cf0b7..065ff6f 100644 --- a/lib/canvas.js +++ b/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. * From 0d5b80a284514b082bb49c15d2b9a6cf108d8ecc Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 14:05:12 -0700 Subject: [PATCH 2/3] Added SetTextBaseline() --- src/CanvasRenderingContext2d.cc | 30 +++++++++++++++++++++++++++++- src/CanvasRenderingContext2d.h | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 41816a1..14cd319 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/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 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 +Context2d::SetTextBaseline(const Arguments &args) { + HandleScope scope; + + if (!args[0]->IsInt32()) return Undefined(); + Context2d *context = ObjectWrap::Unwrap(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. */ diff --git a/src/CanvasRenderingContext2d.h b/src/CanvasRenderingContext2d.h index 8e16de3..f32054f 100644 --- a/src/CanvasRenderingContext2d.h +++ b/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 SetShadowRGBA(const Arguments &args); static Handle SetFillPattern(const Arguments &args); static Handle SetStrokePattern(const Arguments &args); + static Handle SetTextBaseline(const Arguments &args); static Handle SetTextAlignment(const Arguments &args); static Handle SetTextPath(const Arguments &args); static Handle MeasureText(const Arguments &args); From 55f4830c9c0e68835d05d0aef0f31ef5f2958799 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 15:49:54 -0700 Subject: [PATCH 3/3] Implemented textBaseline= --- src/CanvasRenderingContext2d.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 14cd319..d44d7b1 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -949,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);