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

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

Issue 567543002: Avoid re-parsing of string in fillText and measureText in Canvas (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Updating test expectation file Created 6 years, 3 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-normalize-string-expected.txt ('k') | Source/platform/fonts/Character.h » ('j') | 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 ad67737c43af1ed7b88fdd360eb41fc4eebd35f8..5230cf68daf101e33efea90053d80324a24e893a 100644
--- a/Source/core/html/canvas/CanvasRenderingContext2D.cpp
+++ b/Source/core/html/canvas/CanvasRenderingContext2D.cpp
@@ -2089,32 +2089,6 @@ 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);
-}
-
PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
{
RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create();
@@ -2126,8 +2100,7 @@ PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const
FontCachePurgePreventer fontCachePurgePreventer;
canvas()->document().updateRenderTreeIfNeeded();
const Font& font = accessFont();
- String normalizedText = normalizeSpaces(text);
- const TextRun textRun(normalizedText);
+ const TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, LTR, false, true, true);
FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font.fontDescription().computedSize(), 0, -1, true);
// x direction
@@ -2191,7 +2164,6 @@ void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, flo
const Font& font = accessFont();
const FontMetrics& fontMetrics = font.fontMetrics();
- String normalizedText = normalizeSpaces(text);
// FIXME: Need to turn off font smoothing.
@@ -2200,11 +2172,10 @@ void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, flo
bool isRTL = direction == RTL;
bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : false;
- TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direction, override, true);
+ TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion, direction, override, true, true);
// Draw the item text at the correct point.
FloatPoint location(x, y + getFontBaseline(fontMetrics));
-
- float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direction, override));
+ float fontWidth = font.width(textRun);
useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
float width = useMaxWidth ? maxWidth : fontWidth;
« no previous file with comments | « LayoutTests/fast/canvas/canvas-normalize-string-expected.txt ('k') | Source/platform/fonts/Character.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698