diff options
Diffstat (limited to 'WebCore/html/canvas')
-rw-r--r-- | WebCore/html/canvas/CanvasRenderingContext2D.cpp | 59 | ||||
-rw-r--r-- | WebCore/html/canvas/CanvasRenderingContext2D.h | 3 | ||||
-rw-r--r-- | WebCore/html/canvas/CanvasStyle.cpp | 200 | ||||
-rw-r--r-- | WebCore/html/canvas/CanvasStyle.h | 42 |
4 files changed, 136 insertions, 168 deletions
diff --git a/WebCore/html/canvas/CanvasRenderingContext2D.cpp b/WebCore/html/canvas/CanvasRenderingContext2D.cpp index 7cdf5d6..184cc14 100644 --- a/WebCore/html/canvas/CanvasRenderingContext2D.cpp +++ b/WebCore/html/canvas/CanvasRenderingContext2D.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. - * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) + * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) * Copyright (C) 2007 Alp Toker <alp@atoker.com> * Copyright (C) 2008 Eric Seidel <eric@webkit.org> * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> @@ -118,14 +118,14 @@ void CanvasRenderingContext2D::reset() } CanvasRenderingContext2D::State::State() - : m_strokeStyle(CanvasStyle::create("#000000")) - , m_fillStyle(CanvasStyle::create("#000000")) + : m_strokeStyle(CanvasStyle::create(Color::black)) + , m_fillStyle(CanvasStyle::create(Color::black)) , m_lineWidth(1) , m_lineCap(ButtCap) , m_lineJoin(MiterJoin) , m_miterLimit(10) , m_shadowBlur(0) - , m_shadowColor("black") + , m_shadowColor(Color::transparent) , m_globalAlpha(1) , m_globalComposite(CompositeSourceOver) , m_invertibleCTM(true) @@ -315,13 +315,14 @@ void CanvasRenderingContext2D::setShadowBlur(float blur) String CanvasRenderingContext2D::shadowColor() const { - // FIXME: What should this return if you called setShadow with a non-string color? - return state().m_shadowColor; + return Color(state().m_shadowColor).serialized(); } void CanvasRenderingContext2D::setShadowColor(const String& color) { - state().m_shadowColor = color; + if (!CSSParser::parseColor(state().m_shadowColor, color)) + return; + applyShadow(); } @@ -814,15 +815,17 @@ void CanvasRenderingContext2D::setShadow(float width, float height, float blur) { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = ""; + state().m_shadowColor = Color::transparent; applyShadow(); } void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color) { + if (!CSSParser::parseColor(state().m_shadowColor, color)) + return; + state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = color; applyShadow(); } @@ -830,65 +833,64 @@ void CanvasRenderingContext2D::setShadow(float width, float height, float blur, { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = ""; + state().m_shadowColor = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f); GraphicsContext* c = drawingContext(); if (!c) return; - RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f); - c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace); + c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace); } void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color, float alpha) { + RGBA32 rgba; + + if (!CSSParser::parseColor(rgba, color)) + return; + + state().m_shadowColor = colorWithOverrideAlpha(rgba, alpha); state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = color; GraphicsContext* c = drawingContext(); if (!c) return; - RGBA32 rgba = 0; // default is transparent black - if (!state().m_shadowColor.isEmpty()) - CSSParser::parseColor(rgba, state().m_shadowColor); - c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(colorWithOverrideAlpha(rgba, alpha)), DeviceColorSpace); + c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace); } void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float grayLevel, float alpha) { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = ""; + state().m_shadowColor = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha); GraphicsContext* c = drawingContext(); if (!c) return; - RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha); - c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace); + c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace); } void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float r, float g, float b, float a) { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = ""; + state().m_shadowColor = makeRGBA32FromFloats(r, g, b, a); GraphicsContext* c = drawingContext(); if (!c) return; - RGBA32 rgba = makeRGBA32FromFloats(r, g, b, a); // default is transparent black - c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace); + c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace); } void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float c, float m, float y, float k, float a) { state().m_shadowOffset = FloatSize(width, height); state().m_shadowBlur = blur; - state().m_shadowColor = ""; + state().m_shadowColor = makeRGBAFromCMYKA(c, m, y, k, a); GraphicsContext* dc = drawingContext(); if (!dc) @@ -901,7 +903,7 @@ void CanvasRenderingContext2D::setShadow(float width, float height, float blur, CGContextSetShadowWithColor(dc->platformContext(), adjustedShadowSize(width, -height), blur, shadowColor); CGColorRelease(shadowColor); #else - dc->setShadow(IntSize(width, -height), blur, Color(c, m, y, k, a), DeviceColorSpace); + dc->setShadow(IntSize(width, -height), blur, state().m_shadowColor, DeviceColorSpace); #endif } @@ -909,7 +911,7 @@ void CanvasRenderingContext2D::clearShadow() { state().m_shadowOffset = FloatSize(); state().m_shadowBlur = 0; - state().m_shadowColor = ""; + state().m_shadowColor = Color::transparent; applyShadow(); } @@ -919,12 +921,9 @@ void CanvasRenderingContext2D::applyShadow() if (!c) return; - RGBA32 rgba = 0; // default is transparent black - if (!state().m_shadowColor.isEmpty()) - CSSParser::parseColor(rgba, state().m_shadowColor); float width = state().m_shadowOffset.width(); float height = state().m_shadowOffset.height(); - c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace); + c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace); } static IntSize size(HTMLImageElement* image) diff --git a/WebCore/html/canvas/CanvasRenderingContext2D.h b/WebCore/html/canvas/CanvasRenderingContext2D.h index a49ff81..d6b0c8a 100644 --- a/WebCore/html/canvas/CanvasRenderingContext2D.h +++ b/WebCore/html/canvas/CanvasRenderingContext2D.h @@ -28,6 +28,7 @@ #include "AffineTransform.h" #include "CanvasRenderingContext.h" +#include "Color.h" #include "FloatSize.h" #include "Font.h" #include "GraphicsTypes.h" @@ -219,7 +220,7 @@ namespace WebCore { float m_miterLimit; FloatSize m_shadowOffset; float m_shadowBlur; - String m_shadowColor; + RGBA32 m_shadowColor; float m_globalAlpha; CompositeOperator m_globalComposite; AffineTransform m_transform; diff --git a/WebCore/html/canvas/CanvasStyle.cpp b/WebCore/html/canvas/CanvasStyle.cpp index 3515e03..67e8201 100644 --- a/WebCore/html/canvas/CanvasStyle.cpp +++ b/WebCore/html/canvas/CanvasStyle.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. - * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) + * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) * Copyright (C) 2007 Alp Toker <alp@atoker.com> * Copyright (C) 2008 Eric Seidel <eric@webkit.org> * @@ -48,122 +48,108 @@ namespace WebCore { -CanvasStyle::CanvasStyle(const String& color) - : m_type(ColorString) - , m_color(color) +CanvasStyle::CanvasStyle(RGBA32 rgba) + : m_type(RGBA) + , m_rgba(rgba) { } CanvasStyle::CanvasStyle(float grayLevel) - : m_type(GrayLevel) - , m_alpha(1) - , m_grayLevel(grayLevel) -{ -} - -CanvasStyle::CanvasStyle(const String& color, float alpha) - : m_type(ColorStringWithAlpha) - , m_color(color) - , m_alpha(alpha) + : m_type(RGBA) + , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f)) { } CanvasStyle::CanvasStyle(float grayLevel, float alpha) - : m_type(GrayLevel) - , m_alpha(alpha) - , m_grayLevel(grayLevel) + : m_type(RGBA) + , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha)) { } CanvasStyle::CanvasStyle(float r, float g, float b, float a) : m_type(RGBA) - , m_alpha(a) - , m_red(r) - , m_green(g) - , m_blue(b) + , m_rgba(makeRGBA32FromFloats(r, g, b, a)) { } CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a) : m_type(CMYKA) - , m_alpha(a) - , m_cyan(c) - , m_magenta(m) - , m_yellow(y) - , m_black(k) + , m_rgba(makeRGBAFromCMYKA(c, m, y, k, a)) + , m_cmyka(c, m, y, k, a) { } CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient) - : m_type(gradient ? Gradient : ColorString) + : m_type(Gradient) , m_gradient(gradient) { } CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern) - : m_type(pattern ? ImagePattern : ColorString) + : m_type(ImagePattern) , m_pattern(pattern) { } +PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color) +{ + RGBA32 rgba; + if (!CSSParser::parseColor(rgba, color)) + return 0; + return adoptRef(new CanvasStyle(rgba)); +} + +PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color, float alpha) +{ + RGBA32 rgba; + if (!CSSParser::parseColor(rgba, color)) + return 0; + return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha))); +} + +PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasGradient> gradient) +{ + if (!gradient) + return 0; + return adoptRef(new CanvasStyle(gradient)); +} +PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasPattern> pattern) +{ + if (!pattern) + return 0; + return adoptRef(new CanvasStyle(pattern)); +} + void CanvasStyle::applyStrokeColor(GraphicsContext* context) { if (!context) return; switch (m_type) { - case ColorString: { - Color c = Color(m_color); - if (c.isValid()) { - context->setStrokeColor(c.rgb(), DeviceColorSpace); - break; - } - RGBA32 color = 0; // default is transparent black - if (CSSParser::parseColor(color, m_color)) - context->setStrokeColor(color, DeviceColorSpace); - break; - } - case ColorStringWithAlpha: { - Color c = Color(m_color); - if (c.isValid()) { - context->setStrokeColor(colorWithOverrideAlpha(c.rgb(), m_alpha), DeviceColorSpace); - break; - } - RGBA32 color = 0; // default is transparent black - if (CSSParser::parseColor(color, m_color)) - context->setStrokeColor(colorWithOverrideAlpha(color, m_alpha), DeviceColorSpace); - break; - } - case GrayLevel: - // We're only supporting 255 levels of gray here. Since this isn't - // even part of HTML5, I don't expect anyone will care. If they do - // we'll make a fancier Color abstraction. - context->setStrokeColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha), DeviceColorSpace); - break; - case RGBA: - context->setStrokeColor(Color(m_red, m_green, m_blue, m_alpha), DeviceColorSpace); - break; - case CMYKA: { - // FIXME: Do this through platform-independent GraphicsContext API. - // We'll need a fancier Color abstraction to support CYMKA correctly + case RGBA: + context->setStrokeColor(m_rgba, DeviceColorSpace); + break; + case CMYKA: { + // FIXME: Do this through platform-independent GraphicsContext API. + // We'll need a fancier Color abstraction to support CMYKA correctly #if PLATFORM(CG) - CGContextSetCMYKStrokeColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha); + CGContextSetCMYKStrokeColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a); #elif PLATFORM(QT) - QPen currentPen = context->platformContext()->pen(); - QColor clr; - clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha); - currentPen.setColor(clr); - context->platformContext()->setPen(currentPen); + QPen currentPen = context->platformContext()->pen(); + QColor clr; + clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a); + currentPen.setColor(clr); + context->platformContext()->setPen(currentPen); #else - context->setStrokeColor(Color(m_cyan, m_magenta, m_yellow, m_black, m_alpha), DeviceColorSpace); + context->setStrokeColor(m_rgba, DeviceColorSpace); #endif - break; - } - case Gradient: - context->setStrokeGradient(canvasGradient()->gradient()); - break; - case ImagePattern: - context->setStrokePattern(canvasPattern()->pattern()); - break; + break; + } + case Gradient: + context->setStrokeGradient(canvasGradient()->gradient()); + break; + case ImagePattern: + context->setStrokePattern(canvasPattern()->pattern()); + break; } } @@ -172,49 +158,31 @@ void CanvasStyle::applyFillColor(GraphicsContext* context) if (!context) return; switch (m_type) { - case ColorString: { - RGBA32 rgba = 0; // default is transparent black - if (CSSParser::parseColor(rgba, m_color)) - context->setFillColor(rgba, DeviceColorSpace); - break; - } - case ColorStringWithAlpha: { - RGBA32 color = 0; // default is transparent black - if (CSSParser::parseColor(color, m_color)) - context->setFillColor(colorWithOverrideAlpha(color, m_alpha), DeviceColorSpace); - break; - } - case GrayLevel: - // We're only supporting 255 levels of gray here. Since this isn't - // even part of HTML5, I don't expect anyone will care. If they do - // we'll make a fancier Color abstraction. - context->setFillColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha), DeviceColorSpace); - break; - case RGBA: - context->setFillColor(Color(m_red, m_green, m_blue, m_alpha), DeviceColorSpace); - break; - case CMYKA: { - // FIXME: Do this through platform-independent GraphicsContext API. - // We'll need a fancier Color abstraction to support CYMKA correctly + case RGBA: + context->setFillColor(m_rgba, DeviceColorSpace); + break; + case CMYKA: { + // FIXME: Do this through platform-independent GraphicsContext API. + // We'll need a fancier Color abstraction to support CMYKA correctly #if PLATFORM(CG) - CGContextSetCMYKFillColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha); + CGContextSetCMYKFillColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a); #elif PLATFORM(QT) - QBrush currentBrush = context->platformContext()->brush(); - QColor clr; - clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha); - currentBrush.setColor(clr); - context->platformContext()->setBrush(currentBrush); + QBrush currentBrush = context->platformContext()->brush(); + QColor clr; + clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a); + currentBrush.setColor(clr); + context->platformContext()->setBrush(currentBrush); #else - context->setFillColor(Color(m_cyan, m_magenta, m_yellow, m_black, m_alpha), DeviceColorSpace); + context->setFillColor(m_rgba, DeviceColorSpace); #endif - break; - } - case Gradient: - context->setFillGradient(canvasGradient()->gradient()); - break; - case ImagePattern: - context->setFillPattern(canvasPattern()->pattern()); - break; + break; + } + case Gradient: + context->setFillGradient(canvasGradient()->gradient()); + break; + case ImagePattern: + context->setFillPattern(canvasPattern()->pattern()); + break; } } diff --git a/WebCore/html/canvas/CanvasStyle.h b/WebCore/html/canvas/CanvasStyle.h index fe01bd1..18e55cf 100644 --- a/WebCore/html/canvas/CanvasStyle.h +++ b/WebCore/html/canvas/CanvasStyle.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -26,6 +27,7 @@ #ifndef CanvasStyle_h #define CanvasStyle_h +#include "Color.h" #include "PlatformString.h" namespace WebCore { @@ -36,16 +38,17 @@ namespace WebCore { class CanvasStyle : public RefCounted<CanvasStyle> { public: - static PassRefPtr<CanvasStyle> create(const String& color) { return adoptRef(new CanvasStyle(color)); } + static PassRefPtr<CanvasStyle> create(RGBA32 rgba) { return adoptRef(new CanvasStyle(rgba)); } + static PassRefPtr<CanvasStyle> create(const String& color); + static PassRefPtr<CanvasStyle> create(const String& color, float alpha); static PassRefPtr<CanvasStyle> create(float grayLevel) { return adoptRef(new CanvasStyle(grayLevel)); } - static PassRefPtr<CanvasStyle> create(const String& color, float alpha) { return adoptRef(new CanvasStyle(color, alpha)); } static PassRefPtr<CanvasStyle> create(float grayLevel, float alpha) { return adoptRef(new CanvasStyle(grayLevel, alpha)); } static PassRefPtr<CanvasStyle> create(float r, float g, float b, float a) { return adoptRef(new CanvasStyle(r, g, b, a)); } static PassRefPtr<CanvasStyle> create(float c, float m, float y, float k, float a) { return adoptRef(new CanvasStyle(c, m, y, k, a)); } - static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasGradient> gradient) { return adoptRef(new CanvasStyle(gradient)); } - static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasPattern> pattern) { return adoptRef(new CanvasStyle(pattern)); } + static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasGradient> gradient); + static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasPattern> pattern); - String color() const { return m_color; } + String color() const { return Color(m_rgba).serialized(); } CanvasGradient* canvasGradient() const { return m_gradient.get(); } CanvasPattern* canvasPattern() const { return m_pattern.get(); } @@ -53,35 +56,32 @@ namespace WebCore { void applyStrokeColor(GraphicsContext*); private: - CanvasStyle(const String& color); + CanvasStyle(RGBA32 rgba); CanvasStyle(float grayLevel); - CanvasStyle(const String& color, float alpha); CanvasStyle(float grayLevel, float alpha); CanvasStyle(float r, float g, float b, float a); CanvasStyle(float c, float m, float y, float k, float a); CanvasStyle(PassRefPtr<CanvasGradient>); CanvasStyle(PassRefPtr<CanvasPattern>); - enum Type { ColorString, ColorStringWithAlpha, GrayLevel, RGBA, CMYKA, Gradient, ImagePattern }; + enum Type { RGBA, CMYKA, Gradient, ImagePattern }; Type m_type; - String m_color; + RGBA32 m_rgba; + RefPtr<CanvasGradient> m_gradient; RefPtr<CanvasPattern> m_pattern; - float m_alpha; - - float m_grayLevel; - - float m_red; - float m_green; - float m_blue; - - float m_cyan; - float m_magenta; - float m_yellow; - float m_black; + struct CMYKAValues { + CMYKAValues() {} + CMYKAValues(float cyan, float magenta, float yellow, float black, float alpha) : c(cyan), m(magenta), y(yellow), k(black), a(alpha) {} + float c; + float m; + float y; + float k; + float a; + } m_cmyka; }; } // namespace WebCore |