Chromium Code Reviews| Index: Source/core/html/canvas/CanvasRenderingContext2D.cpp |
| diff --git a/Source/core/html/canvas/CanvasRenderingContext2D.cpp b/Source/core/html/canvas/CanvasRenderingContext2D.cpp |
| index 4eea12475e641bce52e6482f6efa3b73d6060b7e..4caa2a2f2d77e98b9ffe011f557af5cb27bf165c 100644 |
| --- a/Source/core/html/canvas/CanvasRenderingContext2D.cpp |
| +++ b/Source/core/html/canvas/CanvasRenderingContext2D.cpp |
| @@ -83,6 +83,12 @@ static bool contextLostRestoredEventsEnabled() |
| return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); |
| } |
| +static inline TextDirection inheritedDirection(HTMLCanvasElement& canvasElement) |
| +{ |
| + RenderStyle* computedStyle = canvasElement.computedStyle(); |
| + return computedStyle ? computedStyle->direction() : LTR; |
| +} |
| + |
| CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, const Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode) |
| : CanvasRenderingContext(canvas) |
| , m_usesCSSCompatibilityParseMode(usesCSSCompatibilityParseMode) |
| @@ -96,6 +102,7 @@ CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co |
| , m_tryRestoreContextEventTimer(this, &CanvasRenderingContext2D::tryRestoreContextEvent) |
| { |
| m_stateStack.append(adoptPtrWillBeNoop(new State())); |
| + m_stateStack.first()->m_direction = inheritedDirection(*canvas); |
| ScriptWrappable::init(this); |
| } |
| @@ -231,6 +238,7 @@ void CanvasRenderingContext2D::reset() |
| unwindStateStack(); |
| m_stateStack.resize(1); |
| m_stateStack.first() = adoptPtrWillBeNoop(new State()); |
| + m_stateStack.first()->m_direction = inheritedDirection(*canvas()); |
| m_path.clear(); |
| validateStateStack(); |
| } |
| @@ -257,6 +265,7 @@ CanvasRenderingContext2D::State::State() |
| , m_imageSmoothingEnabled(true) |
| , m_textAlign(StartTextAlign) |
| , m_textBaseline(AlphabeticTextBaseline) |
| + , m_direction(LTR) |
| , m_unparsedFont(defaultFont) |
| , m_realizedFont(false) |
| , m_hasClip(false) |
| @@ -286,6 +295,7 @@ CanvasRenderingContext2D::State::State(const State& other) |
| , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled) |
| , m_textAlign(other.m_textAlign) |
| , m_textBaseline(other.m_textBaseline) |
| + , m_direction(other.m_direction) |
| , m_unparsedFont(other.m_unparsedFont) |
| , m_font(other.m_font) |
| , m_realizedFont(other.m_realizedFont) |
| @@ -325,6 +335,7 @@ CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(cons |
| m_imageSmoothingEnabled = other.m_imageSmoothingEnabled; |
| m_textAlign = other.m_textAlign; |
| m_textBaseline = other.m_textBaseline; |
| + m_direction = other.m_direction; |
| m_unparsedFont = other.m_unparsedFont; |
| m_font = other.m_font; |
| m_realizedFont = other.m_realizedFont; |
| @@ -2016,6 +2027,24 @@ void CanvasRenderingContext2D::setTextBaseline(const String& s) |
| modifiableState().m_textBaseline = baseline; |
| } |
| +String CanvasRenderingContext2D::direction() const |
| +{ |
| + return (state().m_direction == RTL) ? "rtl" : "ltr"; |
|
esprehn
2014/08/18 19:22:44
no parens
|
| +} |
| + |
| +void CanvasRenderingContext2D::setDirection(const String& s) |
|
esprehn
2014/08/18 19:22:44
Don't use single letter variable bames.
|
| +{ |
| + if (s != "inherit" && s != "rtl" && s != "ltr") |
| + return; |
| + |
| + TextDirection direction = (s == "inherit") ? inheritedDirection(*canvas()) : (s == "rtl") ? RTL : LTR; |
|
esprehn
2014/08/18 19:22:44
Don't nest ternaries.
|
| + if (state().m_direction == direction) |
| + return; |
| + |
| + realizeSaves(); |
| + modifiableState().m_direction = direction; |
| +} |
| + |
| void CanvasRenderingContext2D::fillText(const String& text, float x, float y) |
| { |
| drawTextInternal(text, x, y, true); |
| @@ -2143,15 +2172,14 @@ void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, flo |
| // FIXME: Need to turn off font smoothing. |
| RenderStyle* computedStyle = canvas()->computedStyle(); |
| - TextDirection direction = computedStyle ? computedStyle->direction() : LTR; |
| - bool isRTL = direction == RTL; |
| + bool isRTL = state().m_direction == RTL; |
| bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : false; |
| - TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direction, override, true); |
| + TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, state().m_direction, override, 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(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, state().m_direction, override)); |
| useMaxWidth = (useMaxWidth && maxWidth < fontWidth); |
| float width = useMaxWidth ? maxWidth : fontWidth; |