Changeset 69755 in webkit


Ignore:
Timestamp:
Oct 14, 2010 3:47:24 AM (13 years ago)
Author:
andreas.kling@nokia.com
Message:

2010-10-14 Andreas Kling <kling@webkit.org>

Reviewed by Darin Adler.

Canvas: "currentColor" should inherit the canvas element's color
https://bugs.webkit.org/show_bug.cgi?id=40273

Add support for "currentColor" in CanvasRenderingContext2D APIs.
If the canvas is in-document, "currentColor" is replaced by canvas.style.color,
otherwise we use fully opaque black.

For gradient addColorStop(), "currentColor" always means fully opaque black.

Spec link:
http://www.whatwg.org/specs/web-apps/current-work/#2dcontext

Test: fast/canvas/canvas-currentColor.html

  • html/canvas/CanvasGradient.cpp: (WebCore::CanvasGradient::addColorStop):
  • html/canvas/CanvasRenderingContext2D.cpp: (WebCore::CanvasRenderingContext2D::setStrokeStyle): (WebCore::CanvasRenderingContext2D::setFillStyle): (WebCore::CanvasRenderingContext2D::setShadowColor): (WebCore::CanvasRenderingContext2D::setShadow):
  • html/canvas/CanvasStyle.cpp: (WebCore::parseColor): (WebCore::currentColor): (WebCore::parseColorOrCurrentColor): (WebCore::CanvasStyle::CanvasStyle): (WebCore::CanvasStyle::createFromString): (WebCore::CanvasStyle::createFromStringWithOverrideAlpha): (WebCore::CanvasStyle::isEquivalentColor): (WebCore::CanvasStyle::applyStrokeColor): (WebCore::CanvasStyle::applyFillColor):
  • html/canvas/CanvasStyle.h: (WebCore::CanvasStyle::isCurrentColor): (WebCore::CanvasStyle::hasOverrideAlpha): (WebCore::CanvasStyle::overrideAlpha):

2010-10-14 Andreas Kling <kling@webkit.org>

Reviewed by Darin Adler.

Canvas: "currentColor" should inherit the canvas element's color
https://bugs.webkit.org/show_bug.cgi?id=40273

Added test to verify support for "currentColor" value in CanvasRenderingContext2D.

Also unskipped 3 tests which now pass:

  • canvas/philip/tests/2d.fillStyle.parse.current.basic.html
  • canvas/philip/tests/2d.fillStyle.parse.current.changed.html
  • canvas/philip/tests/2d.fillStyle.parse.current.removed.html

Spec link:
http://www.whatwg.org/specs/web-apps/current-work/#2dcontext

  • fast/canvas/canvas-currentColor-expected.txt: Added.
  • fast/canvas/canvas-currentColor.html: Added.
  • fast/canvas/script-tests/canvas-currentColor.js: Added. (attachCanvasToDocument): (tryLinearGradientColor): (tryRadialGradientColor):
  • platform/gtk/Skipped:
  • platform/mac/Skipped:
  • platform/qt/Skipped:
Location:
trunk
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r69754 r69755  
     12010-10-14  Andreas Kling  <kling@webkit.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Canvas: "currentColor" should inherit the canvas element's color
     6        https://bugs.webkit.org/show_bug.cgi?id=40273
     7
     8        Added test to verify support for "currentColor" value in CanvasRenderingContext2D.
     9
     10        Also unskipped 3 tests which now pass:
     11        - canvas/philip/tests/2d.fillStyle.parse.current.basic.html
     12        - canvas/philip/tests/2d.fillStyle.parse.current.changed.html
     13        - canvas/philip/tests/2d.fillStyle.parse.current.removed.html
     14
     15        Spec link:
     16        http://www.whatwg.org/specs/web-apps/current-work/#2dcontext
     17
     18        * fast/canvas/canvas-currentColor-expected.txt: Added.
     19        * fast/canvas/canvas-currentColor.html: Added.
     20        * fast/canvas/script-tests/canvas-currentColor.js: Added.
     21        (attachCanvasToDocument):
     22        (tryLinearGradientColor):
     23        (tryRadialGradientColor):
     24        * platform/gtk/Skipped:
     25        * platform/mac/Skipped:
     26        * platform/qt/Skipped:
     27
    1282010-10-14  Csaba Osztrogonác  <ossy@webkit.org>
    229
  • trunk/LayoutTests/platform/gtk/Skipped

    r69751 r69755  
    55655565canvas/philip/tests/2d.drawImage.incomplete.html
    55665566canvas/philip/tests/2d.drawImage.null.html
    5567 canvas/philip/tests/2d.fillStyle.parse.current.basic.html
    5568 canvas/philip/tests/2d.fillStyle.parse.current.changed.html
    5569 canvas/philip/tests/2d.fillStyle.parse.current.removed.html
    55705567canvas/philip/tests/2d.fillStyle.parse.system.html
    55715568canvas/philip/tests/2d.gradient.radial.cone.front.html
  • trunk/LayoutTests/platform/mac/Skipped

    r69727 r69755  
    170170canvas/philip/tests/2d.drawImage.incomplete.html
    171171canvas/philip/tests/2d.drawImage.null.html
    172 canvas/philip/tests/2d.fillStyle.parse.current.basic.html
    173 canvas/philip/tests/2d.fillStyle.parse.current.changed.html
    174 canvas/philip/tests/2d.fillStyle.parse.current.removed.html
    175172canvas/philip/tests/2d.fillStyle.parse.system.html
    176173canvas/philip/tests/2d.gradient.radial.cone.front.html
  • trunk/LayoutTests/platform/qt/Skipped

    r69752 r69755  
    51795179canvas/philip/tests/2d.drawImage.incomplete.html
    51805180canvas/philip/tests/2d.drawImage.null.html
    5181 canvas/philip/tests/2d.fillStyle.parse.current.basic.html
    5182 canvas/philip/tests/2d.fillStyle.parse.current.changed.html
    5183 canvas/philip/tests/2d.fillStyle.parse.current.removed.html
    51845181canvas/philip/tests/2d.fillStyle.parse.system.html
    51855182canvas/philip/tests/2d.gradient.radial.cone.behind.html
  • trunk/WebCore/ChangeLog

    r69752 r69755  
     12010-10-14  Andreas Kling  <kling@webkit.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Canvas: "currentColor" should inherit the canvas element's color
     6        https://bugs.webkit.org/show_bug.cgi?id=40273
     7
     8        Add support for "currentColor" in CanvasRenderingContext2D APIs.
     9        If the canvas is in-document, "currentColor" is replaced by canvas.style.color,
     10        otherwise we use fully opaque black.
     11
     12        For gradient addColorStop(), "currentColor" always means fully opaque black.
     13
     14        Spec link:
     15        http://www.whatwg.org/specs/web-apps/current-work/#2dcontext
     16
     17        Test: fast/canvas/canvas-currentColor.html
     18
     19        * html/canvas/CanvasGradient.cpp:
     20        (WebCore::CanvasGradient::addColorStop):
     21        * html/canvas/CanvasRenderingContext2D.cpp:
     22        (WebCore::CanvasRenderingContext2D::setStrokeStyle):
     23        (WebCore::CanvasRenderingContext2D::setFillStyle):
     24        (WebCore::CanvasRenderingContext2D::setShadowColor):
     25        (WebCore::CanvasRenderingContext2D::setShadow):
     26        * html/canvas/CanvasStyle.cpp:
     27        (WebCore::parseColor):
     28        (WebCore::currentColor):
     29        (WebCore::parseColorOrCurrentColor):
     30        (WebCore::CanvasStyle::CanvasStyle):
     31        (WebCore::CanvasStyle::createFromString):
     32        (WebCore::CanvasStyle::createFromStringWithOverrideAlpha):
     33        (WebCore::CanvasStyle::isEquivalentColor):
     34        (WebCore::CanvasStyle::applyStrokeColor):
     35        (WebCore::CanvasStyle::applyFillColor):
     36        * html/canvas/CanvasStyle.h:
     37        (WebCore::CanvasStyle::isCurrentColor):
     38        (WebCore::CanvasStyle::hasOverrideAlpha):
     39        (WebCore::CanvasStyle::overrideAlpha):
     40
    1412010-10-08  Yury Semikhatsky  <yurys@chromium.org>
    242
  • trunk/WebCore/html/canvas/CanvasGradient.cpp

    r49734 r69755  
    2828#include "CanvasGradient.h"
    2929
     30#include "CanvasPattern.h"
     31#include "CanvasStyle.h"
    3032#include "CSSParser.h"
    3133#include "ExceptionCode.h"
     
    5355
    5456    RGBA32 rgba = 0;
    55     if (!CSSParser::parseColor(rgba, color)) {
     57    if (!parseColorOrCurrentColor(rgba, color, 0 /*canvas*/)) {
    5658        if (!m_dashbardCompatibilityMode)
    5759            ec = SYNTAX_ERR;
  • trunk/WebCore/html/canvas/CanvasRenderingContext2D.cpp

    r69727 r69755  
    250250        return;
    251251
    252     checkOrigin(style->canvasPattern());
     252    if (style->isCurrentColor()) {
     253        if (style->hasOverrideAlpha())
     254            style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
     255        else
     256            style = CanvasStyle::createFromRGBA(currentColor(canvas()));
     257    } else
     258        checkOrigin(style->canvasPattern());
    253259
    254260    state().m_strokeStyle = style;
     
    273279        return;
    274280
    275     checkOrigin(style->canvasPattern());
     281    if (style->isCurrentColor()) {
     282        if (style->hasOverrideAlpha())
     283            style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
     284        else
     285            style = CanvasStyle::createFromRGBA(currentColor(canvas()));
     286    } else
     287        checkOrigin(style->canvasPattern());
    276288
    277289    state().m_fillStyle = style;
     
    395407void CanvasRenderingContext2D::setShadowColor(const String& color)
    396408{
    397     if (!CSSParser::parseColor(state().m_shadowColor, color))
     409    if (!parseColorOrCurrentColor(state().m_shadowColor, color, canvas()))
    398410        return;
    399411
     
    983995void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color)
    984996{
    985     if (!CSSParser::parseColor(state().m_shadowColor, color))
     997    if (!parseColorOrCurrentColor(state().m_shadowColor, color, canvas()))
    986998        return;
    987999
     
    10081020    RGBA32 rgba;
    10091021
    1010     if (!CSSParser::parseColor(rgba, color))
     1022    if (!parseColorOrCurrentColor(rgba, color, canvas()))
    10111023        return;
    10121024
  • trunk/WebCore/html/canvas/CanvasStyle.cpp

    r67574 r69755  
    3131
    3232#include "CSSParser.h"
     33#include "CSSPropertyNames.h"
    3334#include "CanvasGradient.h"
    3435#include "CanvasPattern.h"
    3536#include "GraphicsContext.h"
     37#include "HTMLCanvasElement.h"
    3638#include <wtf/Assertions.h>
    3739#include <wtf/PassRefPtr.h>
     
    5052namespace WebCore {
    5153
     54enum ColorParseResult { ParsedRGBA, ParsedCurrentColor, ParseFailed };
     55
     56static ColorParseResult parseColor(RGBA32& parsedColor, const String& colorString)
     57{
     58    if (equalIgnoringCase(colorString, "currentcolor"))
     59        return ParsedCurrentColor;
     60    if (CSSParser::parseColor(parsedColor, colorString))
     61        return ParsedRGBA;
     62    return ParseFailed;
     63}
     64
     65RGBA32 currentColor(HTMLCanvasElement* canvas)
     66{
     67    if (!canvas || !canvas->inDocument())
     68        return Color::black;
     69    RGBA32 rgba = Color::black;
     70    CSSParser::parseColor(rgba, canvas->style()->getPropertyValue(CSSPropertyColor));
     71    return rgba;
     72}
     73
     74bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement* canvas)
     75{
     76    ColorParseResult parseResult = parseColor(parsedColor, colorString);
     77    switch (parseResult) {
     78    case ParsedRGBA:
     79        return true;
     80    case ParsedCurrentColor:
     81        parsedColor = currentColor(canvas);
     82        return true;
     83    case ParseFailed:
     84        return false;
     85    }
     86}
     87
     88CanvasStyle::CanvasStyle(Type type, float overrideAlpha)
     89    : m_type(type)
     90    , m_overrideAlpha(overrideAlpha)
     91{
     92}
     93
    5294CanvasStyle::CanvasStyle(RGBA32 rgba)
    5395    : m_type(RGBA)
     
    90132{
    91133    RGBA32 rgba;
    92     if (!CSSParser::parseColor(rgba, color))
    93         return 0;
    94     return adoptRef(new CanvasStyle(rgba));
     134    ColorParseResult parseResult = parseColor(rgba, color);
     135    switch (parseResult) {
     136    case ParsedRGBA:
     137        return adoptRef(new CanvasStyle(rgba));
     138    case ParsedCurrentColor:
     139        return adoptRef(new CanvasStyle(CurrentColor));
     140    case ParseFailed:
     141        return 0;
     142    }
    95143}
    96144
     
    98146{
    99147    RGBA32 rgba;
    100     if (!CSSParser::parseColor(rgba, color))
    101         return 0;
    102     return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
     148    ColorParseResult parseResult = parseColor(rgba, color);
     149    switch (parseResult) {
     150    case ParsedRGBA:
     151        return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
     152    case ParsedCurrentColor:
     153        return adoptRef(new CanvasStyle(CurrentColorWithOverrideAlpha, alpha));
     154    case ParseFailed:
     155        return 0;
     156    }
    103157}
    104158
     
    122176
    123177    switch (m_type) {
    124     case CanvasStyle::RGBA:
     178    case RGBA:
    125179        return m_rgba == other.m_rgba;
    126     case CanvasStyle::CMYKA:
     180    case CMYKA:
    127181        return m_cmyka.c == other.m_cmyka.c
    128182            && m_cmyka.m == other.m_cmyka.m
     
    130184            && m_cmyka.k == other.m_cmyka.k
    131185            && m_cmyka.a == other.m_cmyka.a;
    132     case CanvasStyle::Gradient:
    133     case CanvasStyle::ImagePattern:
     186    case Gradient:
     187    case ImagePattern:
     188    case CurrentColor:
     189    case CurrentColorWithOverrideAlpha:
    134190        return false;
    135191    }
     
    189245        context->setStrokePattern(canvasPattern()->pattern());
    190246        break;
     247    case CurrentColor:
     248    case CurrentColorWithOverrideAlpha:
     249        ASSERT_NOT_REACHED();
     250        break;
    191251    }
    192252}
     
    222282        context->setFillPattern(canvasPattern()->pattern());
    223283        break;
    224     }
    225 }
    226 
    227 }
     284    case CurrentColor:
     285    case CurrentColorWithOverrideAlpha:
     286        ASSERT_NOT_REACHED();
     287        break;
     288    }
     289}
     290
     291}
  • trunk/WebCore/html/canvas/CanvasStyle.h

    r67574 r69755  
    3030#include "Color.h"
    3131#include "PlatformString.h"
     32#include <wtf/Assertions.h>
    3233
    3334namespace WebCore {
     
    3637    class CanvasPattern;
    3738    class GraphicsContext;
     39    class HTMLCanvasElement;
    3840
    3941    class CanvasStyle : public RefCounted<CanvasStyle> {
     
    4850        static PassRefPtr<CanvasStyle> createFromPattern(PassRefPtr<CanvasPattern>);
    4951
    50         String color() const { return Color(m_rgba).serialized(); }
     52        bool isCurrentColor() const { return m_type == CurrentColor || m_type == CurrentColorWithOverrideAlpha; }
     53        bool hasOverrideAlpha() const { return m_type == CurrentColorWithOverrideAlpha; }
     54        float overrideAlpha() const { ASSERT(m_type == CurrentColorWithOverrideAlpha); return m_overrideAlpha; }
     55
     56        String color() const { ASSERT(m_type == RGBA || m_type == CMYKA); return Color(m_rgba).serialized(); }
    5157        CanvasGradient* canvasGradient() const { return m_gradient.get(); }
    5258        CanvasPattern* canvasPattern() const { return m_pattern.get(); }
     
    6066
    6167    private:
     68        enum Type { RGBA, CMYKA, Gradient, ImagePattern, CurrentColor, CurrentColorWithOverrideAlpha };
     69
     70        CanvasStyle(Type, float overrideAlpha = 0);
    6271        CanvasStyle(RGBA32 rgba);
    6372        CanvasStyle(float grayLevel, float alpha);
     
    6776        CanvasStyle(PassRefPtr<CanvasPattern>);
    6877
    69         enum Type { RGBA, CMYKA, Gradient, ImagePattern };
    70 
    7178        Type m_type;
    7279
    73         RGBA32 m_rgba;
     80        union {
     81            RGBA32 m_rgba;
     82            float m_overrideAlpha;
     83        };
    7484
    7585        RefPtr<CanvasGradient> m_gradient;
     
    8797    };
    8898
     99    RGBA32 currentColor(HTMLCanvasElement*);
     100    bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement*);
     101
    89102} // namespace WebCore
    90103
Note: See TracChangeset for help on using the changeset viewer.