Commit 339739f6 authored by David Quiroz Marin's avatar David Quiroz Marin Committed by Commit Bot

Fix precision errors in canvas textBaseline

For a long time when setting a textBaseline on canvas we have been
rounding the returning values to always* use integers. This extra
round operation seems to be unnecessary now and it is actually
creating other alignment issues. This change fixes does alignment
problems with no noticeable regressions.

*For super tiny fonts (< 5px) we already returned floats.
(crbug.com/338908)

Bug: 890515
Change-Id: I099af9834aaf28bb2770e080bdb849eb729c18fd
Reviewed-on: https://chromium-review.googlesource.com/c/1260210Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Commit-Queue: David Quiroz Marin <davidqu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#598758}
parent 6575a81c
...@@ -13,40 +13,23 @@ constexpr int kHangingAsPercentOfAscent = 80; ...@@ -13,40 +13,23 @@ constexpr int kHangingAsPercentOfAscent = 80;
float TextMetrics::GetFontBaseline(const TextBaseline& text_baseline, float TextMetrics::GetFontBaseline(const TextBaseline& text_baseline,
const SimpleFontData& font_data) { const SimpleFontData& font_data) {
FontMetrics font_metrics = font_data.GetFontMetrics(); FontMetrics font_metrics = font_data.GetFontMetrics();
// If the font is so tiny that the lroundf operations result in two
// different types of text baselines to return the same baseline, use
// floating point metrics (crbug.com/338908).
// If you changed the heuristic here, for consistency please also change it
// in SimpleFontData::platformInit().
bool use_float_ascent_descent = font_data.EmHeightAscent().ToInt() < 4;
switch (text_baseline) { switch (text_baseline) {
case kTopTextBaseline: case kTopTextBaseline:
return use_float_ascent_descent return font_data.EmHeightAscent().ToFloat();
? font_data.EmHeightAscent().ToFloat()
: lround(font_data.EmHeightAscent().ToFloat());
case kHangingTextBaseline: case kHangingTextBaseline:
// According to // According to
// http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling // http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling
// "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of
// the ascender height" // the ascender height"
return use_float_ascent_descent return font_metrics.FloatAscent() * kHangingAsPercentOfAscent / 100.0;
? font_metrics.FloatAscent() * kHangingAsPercentOfAscent / 100
: font_metrics.Ascent() * kHangingAsPercentOfAscent / 100;
case kIdeographicTextBaseline: case kIdeographicTextBaseline:
return use_float_ascent_descent ? -font_metrics.FloatDescent() return -font_metrics.FloatDescent();
: -font_metrics.Descent();
case kBottomTextBaseline: case kBottomTextBaseline:
return use_float_ascent_descent return -font_data.EmHeightDescent().ToFloat();
? -font_data.EmHeightDescent().ToFloat()
: -lround(font_data.EmHeightDescent().ToFloat());
case kMiddleTextBaseline: case kMiddleTextBaseline:
return use_float_ascent_descent return (font_data.EmHeightAscent().ToFloat() -
? (-font_data.EmHeightDescent() + font_data.EmHeightAscent()) font_data.EmHeightDescent().ToFloat()) /
.ToFloat() / 2.0f;
2.0f
: (-lround(font_data.EmHeightDescent().ToFloat()) +
lround(font_data.EmHeightAscent().ToFloat())) /
2;
case kAlphabeticTextBaseline: case kAlphabeticTextBaseline:
default: default:
// Do nothing. // Do nothing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment