1/* 2 * Copyright (C) 2006, 2008, 2013 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef CanvasStyle_h 28#define CanvasStyle_h 29 30#include "Color.h" 31#include <wtf/Assertions.h> 32#include <wtf/RefCounted.h> 33#include <wtf/text/WTFString.h> 34 35namespace WebCore { 36 37 class CanvasGradient; 38 class CanvasPattern; 39 class Document; 40 class GraphicsContext; 41 class HTMLCanvasElement; 42 43 class CanvasStyle { 44 public: 45 CanvasStyle(); 46 explicit CanvasStyle(RGBA32); 47 CanvasStyle(float grayLevel, float alpha); 48 CanvasStyle(float r, float g, float b, float alpha); 49 CanvasStyle(float c, float m, float y, float k, float alpha); 50 explicit CanvasStyle(PassRefPtr<CanvasGradient>); 51 explicit CanvasStyle(PassRefPtr<CanvasPattern>); 52 ~CanvasStyle(); 53 54 static CanvasStyle createFromString(const String& color, Document* = 0); 55 static CanvasStyle createFromStringWithOverrideAlpha(const String& color, float alpha); 56 57 bool isValid() const { return m_type != Invalid; } 58 bool isCurrentColor() const { return m_type == CurrentColor || m_type == CurrentColorWithOverrideAlpha; } 59 bool hasOverrideAlpha() const { return m_type == CurrentColorWithOverrideAlpha; } 60 float overrideAlpha() const { ASSERT(m_type == CurrentColorWithOverrideAlpha); return m_overrideAlpha; } 61 62 String color() const; 63 CanvasGradient* canvasGradient() const; 64 CanvasPattern* canvasPattern() const; 65 66 void applyFillColor(GraphicsContext*) const; 67 void applyStrokeColor(GraphicsContext*) const; 68 69 bool isEquivalentColor(const CanvasStyle&) const; 70 bool isEquivalentRGBA(float r, float g, float b, float a) const; 71 bool isEquivalentCMYKA(float c, float m, float y, float k, float a) const; 72 73 CanvasStyle(const CanvasStyle&); 74 CanvasStyle& operator=(const CanvasStyle&); 75 CanvasStyle(CanvasStyle&&); 76 CanvasStyle& operator=(CanvasStyle&&); 77 78 private: 79 enum Type { RGBA, CMYKA, Gradient, ImagePattern, CurrentColor, CurrentColorWithOverrideAlpha, Invalid }; 80 struct CMYKAValues { 81 WTF_MAKE_FAST_ALLOCATED; 82 WTF_MAKE_NONCOPYABLE(CMYKAValues); 83 public: 84 CMYKAValues() : rgba(0), c(0), m(0), y(0), k(0), a(0) { } 85 CMYKAValues(RGBA32 rgba, float cyan, float magenta, float yellow, float black, float alpha) : rgba(rgba), c(cyan), m(magenta), y(yellow), k(black), a(alpha) { } 86 RGBA32 rgba; 87 float c; 88 float m; 89 float y; 90 float k; 91 float a; 92 }; 93 94 enum ConstructCurrentColorTag { ConstructCurrentColor }; 95 CanvasStyle(ConstructCurrentColorTag) : m_type(CurrentColor) { } 96 CanvasStyle(Type type, float overrideAlpha) 97 : m_overrideAlpha(overrideAlpha) 98 , m_type(type) 99 { 100 } 101 102 union { 103 RGBA32 m_rgba; 104 float m_overrideAlpha; 105 CanvasGradient* m_gradient; 106 CanvasPattern* m_pattern; 107 CMYKAValues* m_cmyka; 108 }; 109 Type m_type; 110 }; 111 112 RGBA32 currentColor(HTMLCanvasElement*); 113 bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement*); 114 115 inline CanvasStyle::CanvasStyle() 116 : m_type(Invalid) 117 { 118 } 119 120 inline CanvasGradient* CanvasStyle::canvasGradient() const 121 { 122 if (m_type == Gradient) 123 return m_gradient; 124 return 0; 125 } 126 127 inline CanvasPattern* CanvasStyle::canvasPattern() const 128 { 129 if (m_type == ImagePattern) 130 return m_pattern; 131 return 0; 132 } 133 134 inline String CanvasStyle::color() const 135 { 136 ASSERT(m_type == RGBA || m_type == CMYKA); 137 if (m_type == RGBA) 138 return Color(m_rgba).serialized(); 139 return Color(m_cmyka->rgba).serialized(); 140 } 141 142 inline CanvasStyle::CanvasStyle(CanvasStyle&& other) 143 { 144 memcpy(this, &other, sizeof(CanvasStyle)); 145 other.m_type = Invalid; 146 } 147 148 inline CanvasStyle& CanvasStyle::operator=(CanvasStyle&& other) 149 { 150 if (this != &other) { 151 memcpy(this, &other, sizeof(CanvasStyle)); 152 other.m_type = Invalid; 153 } 154 return *this; 155 } 156 157} // namespace WebCore 158 159#endif 160