Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(744)

Unified Diff: third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp

Issue 2392033002: Correcting text baseline for tiny fonts (Closed)
Patch Set: Fixing flaky layout tests. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp
diff --git a/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp b/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp
index 34159d0147c430dd3b0d49e5ed90a19d0ff5fb7e..9cbb533e0c2fbfa038fa54c4d0f79454ff8fc56a 100644
--- a/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp
+++ b/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp
@@ -829,6 +829,7 @@ void CanvasRenderingContext2D::drawTextInternal(
DisableDeferralReasonSubPixelTextAntiAliasingSupport);
const Font& font = accessFont();
+ font.getFontDescription().setSubpixelAscentDescent(true);
if (!font.primaryFont())
return;
@@ -910,22 +911,34 @@ const Font& CanvasRenderingContext2D::accessFont() {
return state().font();
}
-int CanvasRenderingContext2D::getFontBaseline(
+float CanvasRenderingContext2D::getFontBaseline(
const FontMetrics& fontMetrics) const {
+ // 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 useFloatAscentDescent =
+ fontMetrics.ascent() < 3 || fontMetrics.height() < 2;
switch (state().getTextBaseline()) {
case TopTextBaseline:
- return fontMetrics.ascent();
+ return useFloatAscentDescent ? fontMetrics.floatAscent()
+ : fontMetrics.ascent();
case HangingTextBaseline:
// According to
// http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling
// "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of
// the ascender height"
- return (fontMetrics.ascent() * 4) / 5;
+ return useFloatAscentDescent ? (fontMetrics.floatAscent() * 4.0) / 5.0
+ : (fontMetrics.ascent() * 4) / 5;
case BottomTextBaseline:
case IdeographicTextBaseline:
- return -fontMetrics.descent();
+ return useFloatAscentDescent ? -fontMetrics.floatDescent()
+ : -fontMetrics.descent();
case MiddleTextBaseline:
- return -fontMetrics.descent() + fontMetrics.height() / 2;
+ return useFloatAscentDescent
+ ? -fontMetrics.floatDescent() + fontMetrics.floatHeight() / 2.0
+ : -fontMetrics.descent() + fontMetrics.height() / 2;
case AlphabeticTextBaseline:
default:
// Do nothing.

Powered by Google App Engine
This is Rietveld 408576698