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

Unified Diff: Source/core/html/canvas/CanvasRenderingContext2D.cpp

Issue 293653002: Canvas fillText and measureText handle ideographic spaces differently. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: add v/g/* test expectations Created 6 years, 7 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
« no previous file with comments | « LayoutTests/fast/canvas/canvas-text-space-characters.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/html/canvas/CanvasRenderingContext2D.cpp
diff --git a/Source/core/html/canvas/CanvasRenderingContext2D.cpp b/Source/core/html/canvas/CanvasRenderingContext2D.cpp
index 8456aba1db2b4de549857a809bf166dec4e1618a..eb9dee1de7b0eab5599ca78987e5a8e36e991242 100644
--- a/Source/core/html/canvas/CanvasRenderingContext2D.cpp
+++ b/Source/core/html/canvas/CanvasRenderingContext2D.cpp
@@ -2024,6 +2024,32 @@ void CanvasRenderingContext2D::strokeText(const String& text, float x, float y,
drawTextInternal(text, x, y, false, maxWidth, true);
}
+static inline bool isSpaceCharacter(UChar c)
+{
+ // According to specification all space characters should be replaced with 0x0020 space character.
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#text-preparation-algorithm
+ // The space characters according to specification are : U+0020, U+0009, U+000A, U+000C, and U+000D.
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#space-character
+ // This function returns true for 0x000B also, so that this is backward compatible.
+ // Otherwise, the test LayoutTests/canvas/philip/tests/2d.text.draw.space.collapse.space.html will fail
+ return c == 0x0009 || c == 0x000A || c == 0x000B || c == 0x000C || c == 0x000D;
+}
+
+static String normalizeSpaces(const String& text)
+{
+ unsigned textLength = text.length();
+ Vector<UChar> charVector(textLength);
+
+ for (unsigned i = 0; i < textLength; i++) {
+ if (isSpaceCharacter(text[i]))
+ charVector[i] = ' ';
+ else
+ charVector[i] = text[i];
+ }
+
+ return String(charVector);
+}
+
PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
{
RefPtr<TextMetrics> metrics = TextMetrics::create();
@@ -2035,7 +2061,8 @@ PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text
FontCachePurgePreventer fontCachePurgePreventer;
canvas()->document().updateRenderTreeIfNeeded();
const Font& font = accessFont();
- const TextRun textRun(text);
+ String normalizedText = normalizeSpaces(text);
+ const TextRun textRun(normalizedText);
FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font.fontDescription().computedSize(), 0, -1, true);
// x direction
@@ -2065,16 +2092,6 @@ PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text
return metrics.release();
}
-static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPtr matchFunction, const String& replacement)
-{
- const size_t replacementLength = replacement.length();
- size_t index = 0;
- while ((index = text.find(matchFunction, index)) != kNotFound) {
- text.replace(index, 1, replacement);
- index += replacementLength;
- }
-}
-
void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth, bool useMaxWidth)
{
// The style resolution required for rendering text is not available in frame-less documents.
@@ -2109,9 +2126,7 @@ void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, flo
const Font& font = accessFont();
const FontMetrics& fontMetrics = font.fontMetrics();
- // According to spec, all the space characters must be replaced with U+0020 SPACE characters.
- String normalizedText = text;
- replaceCharacterInString(normalizedText, isSpaceOrNewline, " ");
+ String normalizedText = normalizeSpaces(text);
// FIXME: Need to turn off font smoothing.
« no previous file with comments | « LayoutTests/fast/canvas/canvas-text-space-characters.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698