From 157610652b479b16d3763e78b47b457e52dda912 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 2 Nov 2010 13:24:13 -0700 Subject: [PATCH] Implemented text alignment. Closes #37 --- src/CanvasRenderingContext2d.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 9dca222..49621c5 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -904,7 +904,23 @@ Context2d::SetTextPath(const Arguments &args) { Context2d *context = ObjectWrap::Unwrap(args.This()); cairo_t *ctx = context->getContext(); + + // Text extents cairo_text_extents_t te; + cairo_text_extents(ctx, *str, &te); + + // Alignment + switch (context->state->textAlignment) { + // center + case 0: + x -= te.width / 2 + te.x_bearing; + break; + // right + case 1: + x -= te.width + te.x_bearing; + break; + } + cairo_move_to(ctx, x, y); cairo_text_path(ctx, *str);