Skip to content

Commit

Permalink
Merge pull request #1038 from chearon/baseline-fix
Browse files Browse the repository at this point in the history
use the PangoLayout to get baseline
  • Loading branch information
LinusU committed Nov 20, 2017
2 parents 7f1be7b + f950be3 commit e677667
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/CanvasRenderingContext2d.cc
Expand Up @@ -1945,19 +1945,23 @@ 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_line_get_extents(pango_layout_get_line(layout, 0), NULL, &logical_rect);

double scale = 1.0 / PANGO_SCALE;
double ascent = scale * pango_layout_get_baseline(layout);
double descent = scale * logical_rect.height - ascent;

switch (baseline) {
case TEXT_BASELINE_ALPHABETIC:
return (pango_font_metrics_get_ascent(metrics) / PANGO_SCALE) * yScale;
return ascent;
case TEXT_BASELINE_MIDDLE:
return ((pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)) / (2.0 * PANGO_SCALE)) * yScale;
return (ascent + descent) / 2.0;
case TEXT_BASELINE_BOTTOM:
return ((pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)) / PANGO_SCALE) * yScale;
return ascent + descent;
default:
return 0;
}
Expand All @@ -1970,13 +1974,10 @@ inline double getBaselineAdjustment(PangoFontMetrics* metrics, cairo_matrix_t ma
void
Context2d::setTextPath(const char *str, double x, double y) {
PangoRectangle logical_rect;
cairo_matrix_t matrix;

pango_layout_set_text(_layout, str, -1);
pango_cairo_update_layout(_context, _layout);

cairo_get_matrix(_context, &matrix);

switch (state->textAlignment) {
// center
case 0:
Expand All @@ -1990,9 +1991,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 @@ -2132,7 +2131,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 e677667

Please sign in to comment.