Skip to content

Commit

Permalink
use the PangoLayout to get baseline
Browse files Browse the repository at this point in the history
Instead of getting it via the PangoContext, which seems to be buggy

Fixes Automattic#1037
  • Loading branch information
chearon committed Nov 16, 2017
1 parent bc1a1f1 commit 2c1602c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/CanvasRenderingContext2d.cc
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1972,9 +1972,7 @@ Context2d::setTextPath(const char *str, double x, double y) {
break;
}

PangoFontMetrics *metrics = PANGO_LAYOUT_GET_METRICS(_layout);
y -= getBaselineAdjustment(metrics, matrix, state->textBaseline);
pango_font_metrics_unref(metrics);
y -= getBaselineAdjustment(_layout, state->textBaseline);

cairo_move_to(_context, x, y);
if (state->textDrawingMode == TEXT_DRAW_PATHS) {
Expand Down Expand Up @@ -2097,7 +2095,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<String>("width").ToLocalChecked(),
Nan::New<Number>(logical_rect.width));
Expand Down

0 comments on commit 2c1602c

Please sign in to comment.