Chromium Code Reviews| 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..b50c659131612b902e53fd4e4b13dfa008e815a5 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 isSpaceCharacters(UChar c) |
|
Justin Novosad
2014/05/21 15:14:46
Remove 's' at the end of name. There is just one 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 (isSpaceCharacters(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. |