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

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: 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 #include "wtf/MathExtras.h" 68 #include "wtf/MathExtras.h"
69 #include "wtf/OwnPtr.h" 69 #include "wtf/OwnPtr.h"
70 #include "wtf/Uint8ClampedArray.h" 70 #include "wtf/Uint8ClampedArray.h"
71 #include "wtf/text/StringBuilder.h" 71 #include "wtf/text/StringBuilder.h"
72 72
73 namespace blink { 73 namespace blink {
74 74
75 static const int defaultFontSize = 10; 75 static const int defaultFontSize = 10;
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 char inherit[] = "inherit";
79 static const char rtl[] = "rtl";
80 static const char ltr[] = "ltr";
78 static const double TryRestoreContextInterval = 0.5; 81 static const double TryRestoreContextInterval = 0.5;
79 static const unsigned MaxTryRestoreContextAttempts = 4; 82 static const unsigned MaxTryRestoreContextAttempts = 4;
80 83
81 static bool contextLostRestoredEventsEnabled() 84 static bool contextLostRestoredEventsEnabled()
82 { 85 {
83 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); 86 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled();
84 } 87 }
85 88
86 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode) 89 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode)
87 : CanvasRenderingContext(canvas) 90 : CanvasRenderingContext(canvas)
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 , m_shadowBlur(0) 253 , m_shadowBlur(0)
251 , m_shadowColor(Color::transparent) 254 , m_shadowColor(Color::transparent)
252 , m_globalAlpha(1) 255 , m_globalAlpha(1)
253 , m_globalComposite(CompositeSourceOver) 256 , m_globalComposite(CompositeSourceOver)
254 , m_globalBlend(blink::WebBlendModeNormal) 257 , m_globalBlend(blink::WebBlendModeNormal)
255 , m_invertibleCTM(true) 258 , m_invertibleCTM(true)
256 , m_lineDashOffset(0) 259 , m_lineDashOffset(0)
257 , m_imageSmoothingEnabled(true) 260 , m_imageSmoothingEnabled(true)
258 , m_textAlign(StartTextAlign) 261 , m_textAlign(StartTextAlign)
259 , m_textBaseline(AlphabeticTextBaseline) 262 , m_textBaseline(AlphabeticTextBaseline)
263 , m_direction(DirectionInherit)
260 , m_unparsedFont(defaultFont) 264 , m_unparsedFont(defaultFont)
261 , m_realizedFont(false) 265 , m_realizedFont(false)
262 , m_hasClip(false) 266 , m_hasClip(false)
263 { 267 {
264 } 268 }
265 269
266 CanvasRenderingContext2D::State::State(const State& other) 270 CanvasRenderingContext2D::State::State(const State& other)
267 : CSSFontSelectorClient() 271 : CSSFontSelectorClient()
268 , m_unrealizedSaveCount(other.m_unrealizedSaveCount) 272 , m_unrealizedSaveCount(other.m_unrealizedSaveCount)
269 , m_unparsedStrokeColor(other.m_unparsedStrokeColor) 273 , m_unparsedStrokeColor(other.m_unparsedStrokeColor)
270 , m_unparsedFillColor(other.m_unparsedFillColor) 274 , m_unparsedFillColor(other.m_unparsedFillColor)
271 , m_strokeStyle(other.m_strokeStyle) 275 , m_strokeStyle(other.m_strokeStyle)
272 , m_fillStyle(other.m_fillStyle) 276 , m_fillStyle(other.m_fillStyle)
273 , m_lineWidth(other.m_lineWidth) 277 , m_lineWidth(other.m_lineWidth)
274 , m_lineCap(other.m_lineCap) 278 , m_lineCap(other.m_lineCap)
275 , m_lineJoin(other.m_lineJoin) 279 , m_lineJoin(other.m_lineJoin)
276 , m_miterLimit(other.m_miterLimit) 280 , m_miterLimit(other.m_miterLimit)
277 , m_shadowOffset(other.m_shadowOffset) 281 , m_shadowOffset(other.m_shadowOffset)
278 , m_shadowBlur(other.m_shadowBlur) 282 , m_shadowBlur(other.m_shadowBlur)
279 , m_shadowColor(other.m_shadowColor) 283 , m_shadowColor(other.m_shadowColor)
280 , m_globalAlpha(other.m_globalAlpha) 284 , m_globalAlpha(other.m_globalAlpha)
281 , m_globalComposite(other.m_globalComposite) 285 , m_globalComposite(other.m_globalComposite)
282 , m_globalBlend(other.m_globalBlend) 286 , m_globalBlend(other.m_globalBlend)
283 , m_transform(other.m_transform) 287 , m_transform(other.m_transform)
284 , m_invertibleCTM(other.m_invertibleCTM) 288 , m_invertibleCTM(other.m_invertibleCTM)
285 , m_lineDashOffset(other.m_lineDashOffset) 289 , m_lineDashOffset(other.m_lineDashOffset)
286 , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled) 290 , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled)
287 , m_textAlign(other.m_textAlign) 291 , m_textAlign(other.m_textAlign)
288 , m_textBaseline(other.m_textBaseline) 292 , m_textBaseline(other.m_textBaseline)
293 , m_direction(other.m_direction)
289 , m_unparsedFont(other.m_unparsedFont) 294 , m_unparsedFont(other.m_unparsedFont)
290 , m_font(other.m_font) 295 , m_font(other.m_font)
291 , m_realizedFont(other.m_realizedFont) 296 , m_realizedFont(other.m_realizedFont)
292 , m_hasClip(other.m_hasClip) 297 , m_hasClip(other.m_hasClip)
293 { 298 {
294 if (m_realizedFont) 299 if (m_realizedFont)
295 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this); 300 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this);
296 } 301 }
297 302
298 CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(cons t State& other) 303 CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(cons t State& other)
(...skipping 19 matching lines...) Expand all
318 m_shadowBlur = other.m_shadowBlur; 323 m_shadowBlur = other.m_shadowBlur;
319 m_shadowColor = other.m_shadowColor; 324 m_shadowColor = other.m_shadowColor;
320 m_globalAlpha = other.m_globalAlpha; 325 m_globalAlpha = other.m_globalAlpha;
321 m_globalComposite = other.m_globalComposite; 326 m_globalComposite = other.m_globalComposite;
322 m_globalBlend = other.m_globalBlend; 327 m_globalBlend = other.m_globalBlend;
323 m_transform = other.m_transform; 328 m_transform = other.m_transform;
324 m_invertibleCTM = other.m_invertibleCTM; 329 m_invertibleCTM = other.m_invertibleCTM;
325 m_imageSmoothingEnabled = other.m_imageSmoothingEnabled; 330 m_imageSmoothingEnabled = other.m_imageSmoothingEnabled;
326 m_textAlign = other.m_textAlign; 331 m_textAlign = other.m_textAlign;
327 m_textBaseline = other.m_textBaseline; 332 m_textBaseline = other.m_textBaseline;
333 m_direction = other.m_direction;
328 m_unparsedFont = other.m_unparsedFont; 334 m_unparsedFont = other.m_unparsedFont;
329 m_font = other.m_font; 335 m_font = other.m_font;
330 m_realizedFont = other.m_realizedFont; 336 m_realizedFont = other.m_realizedFont;
331 m_hasClip = other.m_hasClip; 337 m_hasClip = other.m_hasClip;
332 338
333 if (m_realizedFont) 339 if (m_realizedFont)
334 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this); 340 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalid ationCallbacks(this);
335 341
336 return *this; 342 return *this;
337 } 343 }
(...skipping 1672 matching lines...) Expand 10 before | Expand all | Expand 10 after
2010 { 2016 {
2011 TextBaseline baseline; 2017 TextBaseline baseline;
2012 if (!parseTextBaseline(s, baseline)) 2018 if (!parseTextBaseline(s, baseline))
2013 return; 2019 return;
2014 if (state().m_textBaseline == baseline) 2020 if (state().m_textBaseline == baseline)
2015 return; 2021 return;
2016 realizeSaves(); 2022 realizeSaves();
2017 modifiableState().m_textBaseline = baseline; 2023 modifiableState().m_textBaseline = baseline;
2018 } 2024 }
2019 2025
2026 inline TextDirection CanvasRenderingContext2D::toTextDirection(Direction directi on, RenderStyle** computedStyle) const
2027 {
2028 RenderStyle* style = (computedStyle || direction == DirectionInherit) ? canv as()->computedStyle() : nullptr;
2029 if (computedStyle)
2030 *computedStyle = style;
2031 switch (direction) {
2032 case DirectionInherit:
2033 return style ? style->direction() : LTR;
2034 case DirectionRTL:
2035 return RTL;
2036 case DirectionLTR:
2037 return LTR;
2038 }
2039 ASSERT_NOT_REACHED();
2040 return LTR;
2041 }
2042
2043 String CanvasRenderingContext2D::direction() const
2044 {
2045 if (state().m_direction == DirectionInherit)
2046 canvas()->document().updateRenderTreeIfNeeded();
2047 return toTextDirection(state().m_direction) == RTL ? rtl : ltr;
2048 }
2049
2050 void CanvasRenderingContext2D::setDirection(const String& directionString)
2051 {
2052 Direction direction;
2053 if (directionString == inherit)
2054 direction = DirectionInherit;
2055 else if (directionString == rtl)
2056 direction = DirectionRTL;
2057 else if (directionString == ltr)
2058 direction = DirectionLTR;
2059 else
2060 return;
2061
2062 if (state().m_direction == direction)
2063 return;
2064
2065 realizeSaves();
2066 modifiableState().m_direction = direction;
2067 }
2068
2020 void CanvasRenderingContext2D::fillText(const String& text, float x, float y) 2069 void CanvasRenderingContext2D::fillText(const String& text, float x, float y)
2021 { 2070 {
2022 drawTextInternal(text, x, y, true); 2071 drawTextInternal(text, x, y, true);
2023 } 2072 }
2024 2073
2025 void CanvasRenderingContext2D::fillText(const String& text, float x, float y, fl oat maxWidth) 2074 void CanvasRenderingContext2D::fillText(const String& text, float x, float y, fl oat maxWidth)
2026 { 2075 {
2027 drawTextInternal(text, x, y, true, maxWidth, true); 2076 drawTextInternal(text, x, y, true, maxWidth, true);
2028 } 2077 }
2029 2078
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 return; 2185 return;
2137 2186
2138 FontCachePurgePreventer fontCachePurgePreventer; 2187 FontCachePurgePreventer fontCachePurgePreventer;
2139 2188
2140 const Font& font = accessFont(); 2189 const Font& font = accessFont();
2141 const FontMetrics& fontMetrics = font.fontMetrics(); 2190 const FontMetrics& fontMetrics = font.fontMetrics();
2142 String normalizedText = normalizeSpaces(text); 2191 String normalizedText = normalizeSpaces(text);
2143 2192
2144 // FIXME: Need to turn off font smoothing. 2193 // FIXME: Need to turn off font smoothing.
2145 2194
2146 RenderStyle* computedStyle = canvas()->computedStyle(); 2195 RenderStyle* computedStyle;
2147 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2196 TextDirection direction = toTextDirection(state().m_direction, &computedStyl e);
2148 bool isRTL = direction == RTL; 2197 bool isRTL = direction == RTL;
2149 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2198 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2150 2199
2151 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true); 2200 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true);
2152 // Draw the item text at the correct point. 2201 // Draw the item text at the correct point.
2153 FloatPoint location(x, y + getFontBaseline(fontMetrics)); 2202 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2154 2203
2155 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2204 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2156 2205
2157 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2206 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
2445 2494
2446 unsigned CanvasRenderingContext2D::hitRegionsCount() const 2495 unsigned CanvasRenderingContext2D::hitRegionsCount() const
2447 { 2496 {
2448 if (m_hitRegionManager) 2497 if (m_hitRegionManager)
2449 return m_hitRegionManager->getHitRegionsCount(); 2498 return m_hitRegionManager->getHitRegionsCount();
2450 2499
2451 return 0; 2500 return 0;
2452 } 2501 }
2453 2502
2454 } // namespace blink 2503 } // 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