diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 767573167..d3001de9f 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -1927,19 +1927,19 @@ NAN_METHOD(Context2d::StrokeText) { } /* - * Gets the baseline adjustment in device pixels, taking into account the - * transformation matrix. TODO This does not handle skew (which cannot easily - * be extracted from the matrix separately from rotation). + * Gets the baseline adjustment in device pixels */ -inline double getBaselineAdjustment(PangoFontMetrics* metrics, cairo_matrix_t matrix, short baseline) { - double yScale = sqrt(matrix.yx * matrix.yx + matrix.yy * matrix.yy); +inline double getBaselineAdjustment(PangoLayout* layout, short baseline) { + PangoRectangle logical_rect; + pango_layout_get_pixel_extents(layout, NULL, &logical_rect); + switch (baseline) { case TEXT_BASELINE_ALPHABETIC: - return (pango_font_metrics_get_ascent(metrics) / PANGO_SCALE) * yScale; + return PANGO_ASCENT(logical_rect); case TEXT_BASELINE_MIDDLE: - return ((pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)) / (2.0 * PANGO_SCALE)) * yScale; + return (PANGO_ASCENT(logical_rect) + PANGO_DESCENT(logical_rect)) / 2.0; case TEXT_BASELINE_BOTTOM: - return ((pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)) / PANGO_SCALE) * yScale; + return PANGO_ASCENT(logical_rect) + PANGO_DESCENT(logical_rect); default: return 0; } @@ -1973,7 +1973,7 @@ Context2d::setTextPath(const char *str, double x, double y) { } PangoFontMetrics *metrics = PANGO_LAYOUT_GET_METRICS(_layout); - y -= getBaselineAdjustment(metrics, matrix, state->textBaseline); + y -= getBaselineAdjustment(_layout, state->textBaseline); pango_font_metrics_unref(metrics); cairo_move_to(_context, x, y); @@ -2097,7 +2097,7 @@ NAN_METHOD(Context2d::MeasureText) { cairo_matrix_t matrix; cairo_get_matrix(ctx, &matrix); - double y_offset = getBaselineAdjustment(metrics, matrix, context->state->textBaseline); + double y_offset = getBaselineAdjustment(layout, context->state->textBaseline); obj->Set(Nan::New("width").ToLocalChecked(), Nan::New(logical_rect.width));