Fix text baselines and measureText #985
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fillText
,strokeText
,measureText
), including when rotation and scale transforms are active. (Fixes Text baseline for rotated text is wrong in 2.0 #983)measureText
results. Some had the wrong signs and/or weren't adjusting for the baseline properly.tl;dr: Fixes stuff, no regressions, still not perfect.
Baselines
The good - these are fixed:
The bad - this still doesn't work:
Known issue: all we have is the current transformation matrix, and it's impossible to uniquely decompose that matrix into the scale, rotation and skew matrices. (We need the scale matrix only to adjust the pango metrics.) See https://math.stackexchange.com/questions/1415854/decompose-affine-transformation-including-shear-in-x-and-y and the linked questions. Given that the Canvas spec defines
context.transform(a, b, c, d, e, f)
which thus lets you define a skew, scale and rotation simultaneously, I don't know what to do ... but I think skew is relatively rare, and this PR is an improvement over what it was. Haven't looked what browsers do; they might have tighter integration of their text and graphics libraries that avoids this problem.measureText
Also tricky because there's nothing to compare against. Chrome is the only browser with any support right now, but I think some of their values and test assertions are wrong as I noted in my comment. (Definitions in the spec are here. There's also an older polyfill with visual tests -- the logic for drawing the bounding box in the new
tests['measureText()']
test matches the logic from the polyfill's tests so I feel reasonably comfortable that the metrics are now correct.)(left is node-canvas, right is Chrome with the experimental canvas flag enabled)