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

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 test expectation file Created 6 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 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 2071 matching lines...) Expand 10 before | Expand all | Expand 10 after
2082 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y) 2082 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y)
2083 { 2083 {
2084 drawTextInternal(text, x, y, false); 2084 drawTextInternal(text, x, y, false);
2085 } 2085 }
2086 2086
2087 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth) 2087 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2088 { 2088 {
2089 drawTextInternal(text, x, y, false, maxWidth, true); 2089 drawTextInternal(text, x, y, false, maxWidth, true);
2090 } 2090 }
2091 2091
2092 static inline bool isSpaceCharacter(UChar c)
2093 {
2094 // According to specification all space characters should be replaced with 0 x0020 space character.
2095 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-el ement.html#text-preparation-algorithm
2096 // The space characters according to specification are : U+0020, U+0009, U+0 00A, U+000C, and U+000D.
2097 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-micros yntaxes.html#space-character
2098 // This function returns true for 0x000B also, so that this is backward comp atible.
2099 // Otherwise, the test LayoutTests/canvas/philip/tests/2d.text.draw.space.co llapse.space.html will fail
2100 return c == 0x0009 || c == 0x000A || c == 0x000B || c == 0x000C || c == 0x00 0D;
2101 }
2102
2103 static String normalizeSpaces(const String& text)
2104 {
2105 unsigned textLength = text.length();
2106 Vector<UChar> charVector(textLength);
2107
2108 for (unsigned i = 0; i < textLength; i++) {
2109 if (isSpaceCharacter(text[i]))
2110 charVector[i] = ' ';
2111 else
2112 charVector[i] = text[i];
2113 }
2114
2115 return String(charVector);
2116 }
2117
2118 PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text) 2092 PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
2119 { 2093 {
2120 RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create(); 2094 RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create();
2121 2095
2122 // The style resolution required for rendering text is not available in fram e-less documents. 2096 // The style resolution required for rendering text is not available in fram e-less documents.
2123 if (!canvas()->document().frame()) 2097 if (!canvas()->document().frame())
2124 return metrics.release(); 2098 return metrics.release();
2125 2099
2126 FontCachePurgePreventer fontCachePurgePreventer; 2100 FontCachePurgePreventer fontCachePurgePreventer;
2127 canvas()->document().updateRenderTreeIfNeeded(); 2101 canvas()->document().updateRenderTreeIfNeeded();
2128 const Font& font = accessFont(); 2102 const Font& font = accessFont();
2129 String normalizedText = normalizeSpaces(text); 2103 const TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion | TextRun: :ForbidLeadingExpansion, LTR, false, true, true);
2130 const TextRun textRun(normalizedText);
2131 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true); 2104 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2132 2105
2133 // x direction 2106 // x direction
2134 metrics->setWidth(font.width(textRun)); 2107 metrics->setWidth(font.width(textRun));
2135 metrics->setActualBoundingBoxLeft(-textBounds.x()); 2108 metrics->setActualBoundingBoxLeft(-textBounds.x());
2136 metrics->setActualBoundingBoxRight(textBounds.maxX()); 2109 metrics->setActualBoundingBoxRight(textBounds.maxX());
2137 2110
2138 // y direction 2111 // y direction
2139 const FontMetrics& fontMetrics = font.fontMetrics(); 2112 const FontMetrics& fontMetrics = font.fontMetrics();
2140 const float ascent = fontMetrics.floatAscent(); 2113 const float ascent = fontMetrics.floatAscent();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 return; 2157 return;
2185 2158
2186 gradient = c->fillGradient(); 2159 gradient = c->fillGradient();
2187 if (fill && gradient && gradient->isZeroSize()) 2160 if (fill && gradient && gradient->isZeroSize())
2188 return; 2161 return;
2189 2162
2190 FontCachePurgePreventer fontCachePurgePreventer; 2163 FontCachePurgePreventer fontCachePurgePreventer;
2191 2164
2192 const Font& font = accessFont(); 2165 const Font& font = accessFont();
2193 const FontMetrics& fontMetrics = font.fontMetrics(); 2166 const FontMetrics& fontMetrics = font.fontMetrics();
2194 String normalizedText = normalizeSpaces(text);
2195 2167
2196 // FIXME: Need to turn off font smoothing. 2168 // FIXME: Need to turn off font smoothing.
2197 2169
2198 RenderStyle* computedStyle; 2170 RenderStyle* computedStyle;
2199 TextDirection direction = toTextDirection(state().m_direction, &computedStyl e); 2171 TextDirection direction = toTextDirection(state().m_direction, &computedStyl e);
2200 bool isRTL = direction == RTL; 2172 bool isRTL = direction == RTL;
2201 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2173 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2202 2174
2203 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true); 2175 TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion, direction, over ride, true, true);
2204 // Draw the item text at the correct point. 2176 // Draw the item text at the correct point.
2205 FloatPoint location(x, y + getFontBaseline(fontMetrics)); 2177 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2206 2178 float fontWidth = font.width(textRun);
2207 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2208 2179
2209 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2180 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2210 float width = useMaxWidth ? maxWidth : fontWidth; 2181 float width = useMaxWidth ? maxWidth : fontWidth;
2211 2182
2212 TextAlign align = state().m_textAlign; 2183 TextAlign align = state().m_textAlign;
2213 if (align == StartTextAlign) 2184 if (align == StartTextAlign)
2214 align = isRTL ? RightTextAlign : LeftTextAlign; 2185 align = isRTL ? RightTextAlign : LeftTextAlign;
2215 else if (align == EndTextAlign) 2186 else if (align == EndTextAlign)
2216 align = isRTL ? LeftTextAlign : RightTextAlign; 2187 align = isRTL ? LeftTextAlign : RightTextAlign;
2217 2188
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
2494 2465
2495 unsigned CanvasRenderingContext2D::hitRegionsCount() const 2466 unsigned CanvasRenderingContext2D::hitRegionsCount() const
2496 { 2467 {
2497 if (m_hitRegionManager) 2468 if (m_hitRegionManager)
2498 return m_hitRegionManager->getHitRegionsCount(); 2469 return m_hitRegionManager->getHitRegionsCount();
2499 2470
2500 return 0; 2471 return 0;
2501 } 2472 }
2502 2473
2503 } // namespace blink 2474 } // namespace blink
OLDNEW
« 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