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

Side by Side Diff: Source/core/html/canvas/CanvasRenderingContext2D.cpp

Issue 468483003: Implement canvas2d direction attribute (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Supporting the case of save/restore Created 6 years, 4 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 static const char defaultFontFamily[] = "sans-serif"; 76 static const char defaultFontFamily[] = "sans-serif";
77 static const char defaultFont[] = "10px sans-serif"; 77 static const char defaultFont[] = "10px sans-serif";
78 static const double TryRestoreContextInterval = 0.5; 78 static const double TryRestoreContextInterval = 0.5;
79 static const unsigned MaxTryRestoreContextAttempts = 4; 79 static const unsigned MaxTryRestoreContextAttempts = 4;
80 80
81 static bool contextLostRestoredEventsEnabled() 81 static bool contextLostRestoredEventsEnabled()
82 { 82 {
83 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); 83 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled();
84 } 84 }
85 85
86 static inline TextDirection inheritedDirection(HTMLCanvasElement& canvasElement)
87 {
88 canvasElement.document().updateRenderTreeIfNeeded();
89 RenderStyle* computedStyle = canvasElement.computedStyle();
90 return computedStyle ? computedStyle->direction() : LTR;
91 }
92
86 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode) 93 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode)
87 : CanvasRenderingContext(canvas) 94 : CanvasRenderingContext(canvas)
88 , m_usesCSSCompatibilityParseMode(usesCSSCompatibilityParseMode) 95 , m_usesCSSCompatibilityParseMode(usesCSSCompatibilityParseMode)
89 , m_hasAlpha(!attrs || attrs->alpha()) 96 , m_hasAlpha(!attrs || attrs->alpha())
90 , m_isContextLost(false) 97 , m_isContextLost(false)
91 , m_contextRestorable(true) 98 , m_contextRestorable(true)
92 , m_storageMode(!attrs ? PersistentStorage : attrs->parsedStorage()) 99 , m_storageMode(!attrs ? PersistentStorage : attrs->parsedStorage())
93 , m_tryRestoreContextAttemptCount(0) 100 , m_tryRestoreContextAttemptCount(0)
94 , m_dispatchContextLostEventTimer(this, &CanvasRenderingContext2D::dispatchC ontextLostEvent) 101 , m_dispatchContextLostEventTimer(this, &CanvasRenderingContext2D::dispatchC ontextLostEvent)
95 , m_dispatchContextRestoredEventTimer(this, &CanvasRenderingContext2D::dispa tchContextRestoredEvent) 102 , m_dispatchContextRestoredEventTimer(this, &CanvasRenderingContext2D::dispa tchContextRestoredEvent)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 m_stateStack.resize(1); 239 m_stateStack.resize(1);
233 m_stateStack.first() = adoptPtrWillBeNoop(new State()); 240 m_stateStack.first() = adoptPtrWillBeNoop(new State());
234 m_path.clear(); 241 m_path.clear();
235 validateStateStack(); 242 validateStateStack();
236 } 243 }
237 244
238 // Important: Several of these properties are also stored in GraphicsContext's 245 // Important: Several of these properties are also stored in GraphicsContext's
239 // StrokeData. The default values that StrokeData uses may not the same values 246 // StrokeData. The default values that StrokeData uses may not the same values
240 // that the canvas 2d spec specifies. Make sure to sync the initial state of the 247 // that the canvas 2d spec specifies. Make sure to sync the initial state of the
241 // GraphicsContext in HTMLCanvasElement::createImageBuffer()! 248 // GraphicsContext in HTMLCanvasElement::createImageBuffer()!
242 CanvasRenderingContext2D::State::State() 249 CanvasRenderingContext2D::State::State(Direction direction)
Stephen White 2014/08/21 14:04:38 This seems out of keeping with the other State par
243 : m_unrealizedSaveCount(0) 250 : m_unrealizedSaveCount(0)
244 , m_strokeStyle(CanvasStyle::createFromRGBA(Color::black)) 251 , m_strokeStyle(CanvasStyle::createFromRGBA(Color::black))
245 , m_fillStyle(CanvasStyle::createFromRGBA(Color::black)) 252 , m_fillStyle(CanvasStyle::createFromRGBA(Color::black))
246 , m_lineWidth(1) 253 , m_lineWidth(1)
247 , m_lineCap(ButtCap) 254 , m_lineCap(ButtCap)
248 , m_lineJoin(MiterJoin) 255 , m_lineJoin(MiterJoin)
249 , m_miterLimit(10) 256 , m_miterLimit(10)
250 , m_shadowBlur(0) 257 , m_shadowBlur(0)
251 , m_shadowColor(Color::transparent) 258 , m_shadowColor(Color::transparent)
252 , m_globalAlpha(1) 259 , m_globalAlpha(1)
253 , m_globalComposite(CompositeSourceOver) 260 , m_globalComposite(CompositeSourceOver)
254 , m_globalBlend(blink::WebBlendModeNormal) 261 , m_globalBlend(blink::WebBlendModeNormal)
255 , m_invertibleCTM(true) 262 , m_invertibleCTM(true)
256 , m_lineDashOffset(0) 263 , m_lineDashOffset(0)
257 , m_imageSmoothingEnabled(true) 264 , m_imageSmoothingEnabled(true)
258 , m_textAlign(StartTextAlign) 265 , m_textAlign(StartTextAlign)
259 , m_textBaseline(AlphabeticTextBaseline) 266 , m_textBaseline(AlphabeticTextBaseline)
267 , m_direction(direction)
260 , m_unparsedFont(defaultFont) 268 , m_unparsedFont(defaultFont)
261 , m_realizedFont(false) 269 , m_realizedFont(false)
262 , m_hasClip(false) 270 , m_hasClip(false)
263 { 271 {
264 } 272 }
265 273
266 CanvasRenderingContext2D::State::State(const State& other) 274 CanvasRenderingContext2D::State::State(const State& other)
267 : CSSFontSelectorClient() 275 : CSSFontSelectorClient()
268 , m_unrealizedSaveCount(other.m_unrealizedSaveCount) 276 , m_unrealizedSaveCount(other.m_unrealizedSaveCount)
269 , m_unparsedStrokeColor(other.m_unparsedStrokeColor) 277 , m_unparsedStrokeColor(other.m_unparsedStrokeColor)
270 , m_unparsedFillColor(other.m_unparsedFillColor) 278 , m_unparsedFillColor(other.m_unparsedFillColor)
271 , m_strokeStyle(other.m_strokeStyle) 279 , m_strokeStyle(other.m_strokeStyle)
272 , m_fillStyle(other.m_fillStyle) 280 , m_fillStyle(other.m_fillStyle)
273 , m_lineWidth(other.m_lineWidth) 281 , m_lineWidth(other.m_lineWidth)
274 , m_lineCap(other.m_lineCap) 282 , m_lineCap(other.m_lineCap)
275 , m_lineJoin(other.m_lineJoin) 283 , m_lineJoin(other.m_lineJoin)
276 , m_miterLimit(other.m_miterLimit) 284 , m_miterLimit(other.m_miterLimit)
277 , m_shadowOffset(other.m_shadowOffset) 285 , m_shadowOffset(other.m_shadowOffset)
278 , m_shadowBlur(other.m_shadowBlur) 286 , m_shadowBlur(other.m_shadowBlur)
279 , m_shadowColor(other.m_shadowColor) 287 , m_shadowColor(other.m_shadowColor)
280 , m_globalAlpha(other.m_globalAlpha) 288 , m_globalAlpha(other.m_globalAlpha)
281 , m_globalComposite(other.m_globalComposite) 289 , m_globalComposite(other.m_globalComposite)
282 , m_globalBlend(other.m_globalBlend) 290 , m_globalBlend(other.m_globalBlend)
283 , m_transform(other.m_transform) 291 , m_transform(other.m_transform)
284 , m_invertibleCTM(other.m_invertibleCTM) 292 , m_invertibleCTM(other.m_invertibleCTM)
285 , m_lineDashOffset(other.m_lineDashOffset) 293 , m_lineDashOffset(other.m_lineDashOffset)
286 , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled) 294 , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled)
287 , m_textAlign(other.m_textAlign) 295 , m_textAlign(other.m_textAlign)
288 , m_textBaseline(other.m_textBaseline) 296 , m_textBaseline(other.m_textBaseline)
297 , m_direction(other.m_direction)
289 , m_unparsedFont(other.m_unparsedFont) 298 , m_unparsedFont(other.m_unparsedFont)
290 , m_font(other.m_font) 299 , m_font(other.m_font)
291 , m_realizedFont(other.m_realizedFont) 300 , m_realizedFont(other.m_realizedFont)
292 , m_hasClip(other.m_hasClip) 301 , m_hasClip(other.m_hasClip)
293 { 302 {
294 if (m_realizedFont) 303 if (m_realizedFont)
295 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this); 304 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this);
296 } 305 }
297 306
298 CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(cons t State& other) 307 CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(cons t State& other)
(...skipping 19 matching lines...) Expand all
318 m_shadowBlur = other.m_shadowBlur; 327 m_shadowBlur = other.m_shadowBlur;
319 m_shadowColor = other.m_shadowColor; 328 m_shadowColor = other.m_shadowColor;
320 m_globalAlpha = other.m_globalAlpha; 329 m_globalAlpha = other.m_globalAlpha;
321 m_globalComposite = other.m_globalComposite; 330 m_globalComposite = other.m_globalComposite;
322 m_globalBlend = other.m_globalBlend; 331 m_globalBlend = other.m_globalBlend;
323 m_transform = other.m_transform; 332 m_transform = other.m_transform;
324 m_invertibleCTM = other.m_invertibleCTM; 333 m_invertibleCTM = other.m_invertibleCTM;
325 m_imageSmoothingEnabled = other.m_imageSmoothingEnabled; 334 m_imageSmoothingEnabled = other.m_imageSmoothingEnabled;
326 m_textAlign = other.m_textAlign; 335 m_textAlign = other.m_textAlign;
327 m_textBaseline = other.m_textBaseline; 336 m_textBaseline = other.m_textBaseline;
337 m_direction = other.m_direction;
328 m_unparsedFont = other.m_unparsedFont; 338 m_unparsedFont = other.m_unparsedFont;
329 m_font = other.m_font; 339 m_font = other.m_font;
330 m_realizedFont = other.m_realizedFont; 340 m_realizedFont = other.m_realizedFont;
331 m_hasClip = other.m_hasClip; 341 m_hasClip = other.m_hasClip;
332 342
333 if (m_realizedFont) 343 if (m_realizedFont)
334 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this); 344 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this);
335 345
336 return *this; 346 return *this;
337 } 347 }
(...skipping 1671 matching lines...) Expand 10 before | Expand all | Expand 10 after
2009 { 2019 {
2010 TextBaseline baseline; 2020 TextBaseline baseline;
2011 if (!parseTextBaseline(s, baseline)) 2021 if (!parseTextBaseline(s, baseline))
2012 return; 2022 return;
2013 if (state().m_textBaseline == baseline) 2023 if (state().m_textBaseline == baseline)
2014 return; 2024 return;
2015 realizeSaves(); 2025 realizeSaves();
2016 modifiableState().m_textBaseline = baseline; 2026 modifiableState().m_textBaseline = baseline;
2017 } 2027 }
2018 2028
2029 String CanvasRenderingContext2D::direction() const
2030 {
2031 TextDirection direction = LTR;
2032 switch (state().m_direction) {
2033 case DirectionInherit:
2034 direction = inheritedDirection(*canvas());
2035 break;
2036 case DirectionRTL:
2037 direction = RTL;
2038 break;
2039 case DirectionLTR:
2040 direction = LTR;
2041 break;
2042 default:
2043 ASSERT_NOT_REACHED();
2044 break;
2045 }
2046 return direction == RTL ? "rtl" : "ltr";
2047 }
2048
2049 void CanvasRenderingContext2D::setDirection(const String& directionString)
2050 {
2051 Direction direction;
2052 if (directionString == "inherit")
2053 direction = DirectionInherit;
2054 else if (directionString == "rtl")
2055 direction = DirectionRTL;
2056 else if (directionString == "ltr")
2057 direction = DirectionLTR;
2058 else
2059 return;
2060
2061 realizeSaves();
2062 modifiableState().m_direction = direction;
2063 }
2064
2019 void CanvasRenderingContext2D::fillText(const String& text, float x, float y) 2065 void CanvasRenderingContext2D::fillText(const String& text, float x, float y)
2020 { 2066 {
2021 drawTextInternal(text, x, y, true); 2067 drawTextInternal(text, x, y, true);
2022 } 2068 }
2023 2069
2024 void CanvasRenderingContext2D::fillText(const String& text, float x, float y, fl oat maxWidth) 2070 void CanvasRenderingContext2D::fillText(const String& text, float x, float y, fl oat maxWidth)
2025 { 2071 {
2026 drawTextInternal(text, x, y, true, maxWidth, true); 2072 drawTextInternal(text, x, y, true, maxWidth, true);
2027 } 2073 }
2028 2074
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2135 return; 2181 return;
2136 2182
2137 FontCachePurgePreventer fontCachePurgePreventer; 2183 FontCachePurgePreventer fontCachePurgePreventer;
2138 2184
2139 const Font& font = accessFont(); 2185 const Font& font = accessFont();
2140 const FontMetrics& fontMetrics = font.fontMetrics(); 2186 const FontMetrics& fontMetrics = font.fontMetrics();
2141 String normalizedText = normalizeSpaces(text); 2187 String normalizedText = normalizeSpaces(text);
2142 2188
2143 // FIXME: Need to turn off font smoothing. 2189 // FIXME: Need to turn off font smoothing.
2144 2190
2191 if (state().m_direction == DirectionInherit)
2192 canvas()->document().updateRenderTreeIfNeeded();
2145 RenderStyle* computedStyle = canvas()->computedStyle(); 2193 RenderStyle* computedStyle = canvas()->computedStyle();
2146 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2194 TextDirection direction = LTR;
2195 switch (state().m_direction) {
2196 case DirectionInherit:
2197 direction = computedStyle ? computedStyle->direction() : LTR;
2198 break;
2199 case DirectionRTL:
2200 direction = RTL;
2201 break;
2202 case DirectionLTR:
2203 direction = LTR;
2204 break;
2205 default:
2206 ASSERT_NOT_REACHED();
2207 break;
2208 }
2147 bool isRTL = direction == RTL; 2209 bool isRTL = direction == RTL;
2148 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2210 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2149 2211
2150 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true); 2212 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true);
2151 // Draw the item text at the correct point. 2213 // Draw the item text at the correct point.
2152 FloatPoint location(x, y + getFontBaseline(fontMetrics)); 2214 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2153 2215
2154 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2216 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2155 2217
2156 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2218 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
2444 2506
2445 unsigned CanvasRenderingContext2D::hitRegionsCount() const 2507 unsigned CanvasRenderingContext2D::hitRegionsCount() const
2446 { 2508 {
2447 if (m_hitRegionManager) 2509 if (m_hitRegionManager)
2448 return m_hitRegionManager->getHitRegionsCount(); 2510 return m_hitRegionManager->getHitRegionsCount();
2449 2511
2450 return 0; 2512 return 0;
2451 } 2513 }
2452 2514
2453 } // namespace blink 2515 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/html/canvas/CanvasRenderingContext2D.h ('k') | Source/core/html/canvas/CanvasRenderingContext2D.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698