WebCore/html/canvas/CanvasStyle.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
       
     3  * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     4  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
       
     5  * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
       
     6  *
       
     7  * Redistribution and use in source and binary forms, with or without
       
     8  * modification, are permitted provided that the following conditions
       
     9  * are met:
       
    10  * 1. Redistributions of source code must retain the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer.
       
    12  * 2. Redistributions in binary form must reproduce the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer in the
       
    14  *    documentation and/or other materials provided with the distribution.
       
    15  *
       
    16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    27  */
       
    28 
       
    29 #include "config.h"
       
    30 #include "CanvasStyle.h"
       
    31 
       
    32 #include "CSSParser.h"
       
    33 #include "CanvasGradient.h"
       
    34 #include "CanvasPattern.h"
       
    35 #include "GraphicsContext.h"
       
    36 #include <wtf/Assertions.h>
       
    37 #include <wtf/PassRefPtr.h>
       
    38 
       
    39 #if PLATFORM(CG)
       
    40 #include <CoreGraphics/CGContext.h>
       
    41 #endif
       
    42 
       
    43 #if PLATFORM(QT)
       
    44 #include <QPainter>
       
    45 #include <QBrush>
       
    46 #include <QPen>
       
    47 #include <QColor>
       
    48 #endif
       
    49 
       
    50 namespace WebCore {
       
    51 
       
    52 CanvasStyle::CanvasStyle(RGBA32 rgba)
       
    53     : m_type(RGBA)
       
    54     , m_rgba(rgba)
       
    55 {
       
    56 }
       
    57 
       
    58 CanvasStyle::CanvasStyle(float grayLevel)
       
    59     : m_type(RGBA)
       
    60     , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f))
       
    61 {
       
    62 }
       
    63 
       
    64 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
       
    65     : m_type(RGBA)
       
    66     , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
       
    67 {
       
    68 }
       
    69 
       
    70 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
       
    71     : m_type(RGBA)
       
    72     , m_rgba(makeRGBA32FromFloats(r, g, b, a))
       
    73 {
       
    74 }
       
    75 
       
    76 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
       
    77     : m_type(CMYKA)
       
    78     , m_rgba(makeRGBAFromCMYKA(c, m, y, k, a))
       
    79     , m_cmyka(c, m, y, k, a)
       
    80 {
       
    81 }
       
    82 
       
    83 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
       
    84     : m_type(Gradient)
       
    85     , m_gradient(gradient)
       
    86 {
       
    87 }
       
    88 
       
    89 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
       
    90     : m_type(ImagePattern)
       
    91     , m_pattern(pattern)
       
    92 {
       
    93 }
       
    94 
       
    95 PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color)
       
    96 {
       
    97     RGBA32 rgba;
       
    98     if (!CSSParser::parseColor(rgba, color))
       
    99         return 0;
       
   100     return adoptRef(new CanvasStyle(rgba));
       
   101 }
       
   102 
       
   103 PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color, float alpha)
       
   104 {
       
   105     RGBA32 rgba;
       
   106     if (!CSSParser::parseColor(rgba, color))
       
   107         return 0;
       
   108     return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
       
   109 }
       
   110 
       
   111 PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasGradient> gradient)
       
   112 {
       
   113     if (!gradient)
       
   114         return 0;
       
   115     return adoptRef(new CanvasStyle(gradient));
       
   116 }
       
   117 PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasPattern> pattern)
       
   118 {
       
   119     if (!pattern)
       
   120         return 0;
       
   121     return adoptRef(new CanvasStyle(pattern));
       
   122 }
       
   123 
       
   124 bool CanvasStyle::isEquivalentColor(const CanvasStyle& other) const
       
   125 {
       
   126     if (m_type != other.m_type)
       
   127         return false;
       
   128 
       
   129     switch (m_type) {
       
   130     case CanvasStyle::RGBA:
       
   131         return m_rgba == other.m_rgba;
       
   132     case CanvasStyle::CMYKA:
       
   133         return m_cmyka.c == other.m_cmyka.c
       
   134             && m_cmyka.m == other.m_cmyka.m
       
   135             && m_cmyka.y == other.m_cmyka.y
       
   136             && m_cmyka.k == other.m_cmyka.k
       
   137             && m_cmyka.a == other.m_cmyka.a;
       
   138     case CanvasStyle::Gradient:
       
   139     case CanvasStyle::ImagePattern:
       
   140         return false;
       
   141     }
       
   142 
       
   143     ASSERT_NOT_REACHED();
       
   144     return false;
       
   145 }
       
   146 
       
   147 void CanvasStyle::applyStrokeColor(GraphicsContext* context)
       
   148 {
       
   149     if (!context)
       
   150         return;
       
   151     switch (m_type) {
       
   152     case RGBA:
       
   153         context->setStrokeColor(m_rgba, DeviceColorSpace);
       
   154         break;
       
   155     case CMYKA: {
       
   156         // FIXME: Do this through platform-independent GraphicsContext API.
       
   157         // We'll need a fancier Color abstraction to support CMYKA correctly
       
   158 #if PLATFORM(CG)
       
   159         CGContextSetCMYKStrokeColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
       
   160 #elif PLATFORM(QT)
       
   161         QPen currentPen = context->platformContext()->pen();
       
   162         QColor clr;
       
   163         clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
       
   164         currentPen.setColor(clr);
       
   165         context->platformContext()->setPen(currentPen);
       
   166 #else
       
   167         context->setStrokeColor(m_rgba, DeviceColorSpace);
       
   168 #endif
       
   169         break;
       
   170     }
       
   171     case Gradient:
       
   172         context->setStrokeGradient(canvasGradient()->gradient());
       
   173         break;
       
   174     case ImagePattern:
       
   175         context->setStrokePattern(canvasPattern()->pattern());
       
   176         break;
       
   177     }
       
   178 }
       
   179 
       
   180 void CanvasStyle::applyFillColor(GraphicsContext* context)
       
   181 {
       
   182     if (!context)
       
   183         return;
       
   184     switch (m_type) {
       
   185     case RGBA:
       
   186         context->setFillColor(m_rgba, DeviceColorSpace);
       
   187         break;
       
   188     case CMYKA: {
       
   189         // FIXME: Do this through platform-independent GraphicsContext API.
       
   190         // We'll need a fancier Color abstraction to support CMYKA correctly
       
   191 #if PLATFORM(CG)
       
   192         CGContextSetCMYKFillColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
       
   193 #elif PLATFORM(QT)
       
   194         QBrush currentBrush = context->platformContext()->brush();
       
   195         QColor clr;
       
   196         clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
       
   197         currentBrush.setColor(clr);
       
   198         context->platformContext()->setBrush(currentBrush);
       
   199 #else
       
   200         context->setFillColor(m_rgba, DeviceColorSpace);
       
   201 #endif
       
   202         break;
       
   203     }
       
   204     case Gradient:
       
   205         context->setFillGradient(canvasGradient()->gradient());
       
   206         break;
       
   207     case ImagePattern:
       
   208         context->setFillPattern(canvasPattern()->pattern());
       
   209         break;
       
   210     }
       
   211 }
       
   212 
       
   213 }