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

Side by Side 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 code and adding Layout test case 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. 9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
10 * 10 *
(...skipping 2069 matching lines...) Expand 10 before | Expand all | Expand 10 after
2080 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y) 2080 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y)
2081 { 2081 {
2082 drawTextInternal(text, x, y, false); 2082 drawTextInternal(text, x, y, false);
2083 } 2083 }
2084 2084
2085 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth) 2085 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2086 { 2086 {
2087 drawTextInternal(text, x, y, false, maxWidth, true); 2087 drawTextInternal(text, x, y, false, maxWidth, true);
2088 } 2088 }
2089 2089
2090 static inline bool isSpaceCharacter(UChar c)
2091 {
2092 // According to specification all space characters should be replaced with 0 x0020 space character.
2093 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-el ement.html#text-preparation-algorithm
2094 // The space characters according to specification are : U+0020, U+0009, U+0 00A, U+000C, and U+000D.
2095 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-micros yntaxes.html#space-character
2096 // This function returns true for 0x000B also, so that this is backward comp atible.
2097 // Otherwise, the test LayoutTests/canvas/philip/tests/2d.text.draw.space.co llapse.space.html will fail
2098 return c == 0x0009 || c == 0x000A || c == 0x000B || c == 0x000C || c == 0x00 0D;
2099 }
2100
2101 static String normalizeSpaces(const String& text)
2102 {
2103 unsigned textLength = text.length();
2104 Vector<UChar> charVector(textLength);
2105
2106 for (unsigned i = 0; i < textLength; i++) {
2107 if (isSpaceCharacter(text[i]))
2108 charVector[i] = ' ';
2109 else
2110 charVector[i] = text[i];
2111 }
2112
2113 return String(charVector);
2114 }
2115
2116 PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text) 2090 PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
2117 { 2091 {
2118 RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create(); 2092 RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create();
2119 2093
2120 // The style resolution required for rendering text is not available in fram e-less documents. 2094 // The style resolution required for rendering text is not available in fram e-less documents.
2121 if (!canvas()->document().frame()) 2095 if (!canvas()->document().frame())
2122 return metrics.release(); 2096 return metrics.release();
2123 2097
2124 FontCachePurgePreventer fontCachePurgePreventer; 2098 FontCachePurgePreventer fontCachePurgePreventer;
2125 canvas()->document().updateRenderTreeIfNeeded(); 2099 canvas()->document().updateRenderTreeIfNeeded();
2126 const Font& font = accessFont(); 2100 const Font& font = accessFont();
2127 String normalizedText = normalizeSpaces(text); 2101 const TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion | TextRun: :ForbidLeadingExpansion, LTR, false, true, true);
2128 const TextRun textRun(normalizedText);
2129 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true); 2102 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2130 2103
2131 // x direction 2104 // x direction
2132 metrics->setWidth(font.width(textRun)); 2105 metrics->setWidth(font.width(textRun));
2133 metrics->setActualBoundingBoxLeft(-textBounds.x()); 2106 metrics->setActualBoundingBoxLeft(-textBounds.x());
2134 metrics->setActualBoundingBoxRight(textBounds.maxX()); 2107 metrics->setActualBoundingBoxRight(textBounds.maxX());
2135 2108
2136 // y direction 2109 // y direction
2137 const FontMetrics& fontMetrics = font.fontMetrics(); 2110 const FontMetrics& fontMetrics = font.fontMetrics();
2138 const float ascent = fontMetrics.floatAscent(); 2111 const float ascent = fontMetrics.floatAscent();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2182 return; 2155 return;
2183 2156
2184 gradient = c->fillGradient(); 2157 gradient = c->fillGradient();
2185 if (fill && gradient && gradient->isZeroSize()) 2158 if (fill && gradient && gradient->isZeroSize())
2186 return; 2159 return;
2187 2160
2188 FontCachePurgePreventer fontCachePurgePreventer; 2161 FontCachePurgePreventer fontCachePurgePreventer;
2189 2162
2190 const Font& font = accessFont(); 2163 const Font& font = accessFont();
2191 const FontMetrics& fontMetrics = font.fontMetrics(); 2164 const FontMetrics& fontMetrics = font.fontMetrics();
2192 String normalizedText = normalizeSpaces(text);
2193 2165
2194 // FIXME: Need to turn off font smoothing. 2166 // FIXME: Need to turn off font smoothing.
2195 2167
2196 RenderStyle* computedStyle; 2168 RenderStyle* computedStyle;
2197 TextDirection direction = toTextDirection(state().m_direction, &computedStyl e); 2169 TextDirection direction = toTextDirection(state().m_direction, &computedStyl e);
2198 bool isRTL = direction == RTL; 2170 bool isRTL = direction == RTL;
2199 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2171 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2200 2172
2201 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true); 2173 TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion, direction, over ride, true, true);
2202 // Draw the item text at the correct point. 2174 // Draw the item text at the correct point.
2203 FloatPoint location(x, y + getFontBaseline(fontMetrics)); 2175 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2204 2176 float fontWidth = font.width(textRun);
2205 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2206 2177
2207 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2178 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2208 float width = useMaxWidth ? maxWidth : fontWidth; 2179 float width = useMaxWidth ? maxWidth : fontWidth;
2209 2180
2210 TextAlign align = state().m_textAlign; 2181 TextAlign align = state().m_textAlign;
2211 if (align == StartTextAlign) 2182 if (align == StartTextAlign)
2212 align = isRTL ? RightTextAlign : LeftTextAlign; 2183 align = isRTL ? RightTextAlign : LeftTextAlign;
2213 else if (align == EndTextAlign) 2184 else if (align == EndTextAlign)
2214 align = isRTL ? LeftTextAlign : RightTextAlign; 2185 align = isRTL ? LeftTextAlign : RightTextAlign;
2215 2186
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
2502 2473
2503 unsigned CanvasRenderingContext2D::hitRegionsCount() const 2474 unsigned CanvasRenderingContext2D::hitRegionsCount() const
2504 { 2475 {
2505 if (m_hitRegionManager) 2476 if (m_hitRegionManager)
2506 return m_hitRegionManager->getHitRegionsCount(); 2477 return m_hitRegionManager->getHitRegionsCount();
2507 2478
2508 return 0; 2479 return 0;
2509 } 2480 }
2510 2481
2511 } // namespace blink 2482 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698