Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the PangoLayout to get baseline #1038

Merged
merged 1 commit into from Nov 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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