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

Added HANGING, IDEOGRAPHIC and TOP textBaseline to adjustments #2347

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Changed
### Added
### Fixed
* TextBaseline `top`, `middle` and `hanging` did not align with javascript canvas api

3.0.0
==================
Expand Down
26 changes: 20 additions & 6 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2479,20 +2479,34 @@ 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;
double ascent = pango_layout_get_baseline(layout);
double descent = logical_rect.height - ascent;

double adjustment;

switch (baseline) {
case TEXT_BASELINE_ALPHABETIC:
return ascent;
adjustment = ascent;
break;
case TEXT_BASELINE_MIDDLE:
return (ascent + descent) / 2.0;
adjustment = logical_rect.height / 2.0 + (logical_rect.height - ascent) / 2.0;
break;
case TEXT_BASELINE_BOTTOM:
return ascent + descent;
adjustment = ascent + descent;
break;
case TEXT_BASELINE_TOP:
adjustment = descent;
break;
case TEXT_BASELINE_HANGING:
adjustment = 1.5 * descent;
break;
case TEXT_BASELINE_IDEOGRAPHIC:
adjustment = (ascent + logical_rect.height) / 2.0;
break;
default:
return 0;
}
return 1.0 / PANGO_SCALE * adjustment;
}

/*
Expand Down