Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "platform/graphics/GradientGeneratedImage.h"
     28 
     29 #include "platform/geometry/FloatRect.h"
     30 #include "platform/graphics/GraphicsContextStateSaver.h"
     31 
     32 namespace WebCore {
     33 
     34 void GradientGeneratedImage::draw(GraphicsContext* destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
     35 {
     36     GraphicsContextStateSaver stateSaver(*destContext);
     37     destContext->setCompositeOperation(compositeOp, blendMode);
     38     destContext->clip(destRect);
     39     destContext->translate(destRect.x(), destRect.y());
     40     if (destRect.size() != srcRect.size())
     41         destContext->scale(destRect.width() / srcRect.width(), destRect.height() / srcRect.height());
     42     destContext->translate(-srcRect.x(), -srcRect.y());
     43     destContext->setFillGradient(m_gradient);
     44     destContext->fillRect(FloatRect(FloatPoint(), m_size));
     45 }
     46 
     47 void GradientGeneratedImage::drawPattern(GraphicsContext* destContext, const FloatRect& srcRect, const FloatSize& scale,
     48     const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect, blink::WebBlendMode blendMode, const IntSize& repeatSpacing)
     49 {
     50     float stepX = srcRect.width() + repeatSpacing.width();
     51     float stepY = srcRect.height() + repeatSpacing.height();
     52     int firstColumn = static_cast<int>(floorf((((destRect.x() - phase.x()) / scale.width()) - srcRect.x()) / srcRect.width()));
     53     int firstRow = static_cast<int>(floorf((((destRect.y() - phase.y()) / scale.height())  - srcRect.y()) / srcRect.height()));
     54     for (int i = firstColumn; ; ++i) {
     55         float dstX = (srcRect.x() + i * stepX) * scale.width() + phase.x();
     56         // assert that first column encroaches left edge of dstRect.
     57         ASSERT(i > firstColumn || dstX <= destRect.x());
     58         ASSERT(i == firstColumn || dstX > destRect.x());
     59 
     60         if (dstX >= destRect.maxX())
     61             break;
     62         float dstMaxX = dstX + srcRect.width() * scale.width();
     63         if (dstX < destRect.x())
     64             dstX = destRect.x();
     65         if (dstMaxX > destRect.maxX())
     66             dstMaxX = destRect.maxX();
     67         if (dstX >= dstMaxX)
     68             continue;
     69 
     70         FloatRect visibleSrcRect;
     71         FloatRect tileDstRect;
     72         tileDstRect.setX(dstX);
     73         tileDstRect.setWidth(dstMaxX - dstX);
     74         visibleSrcRect.setX((tileDstRect.x() - phase.x()) / scale.width() - i * stepX);
     75         visibleSrcRect.setWidth(tileDstRect.width() / scale.width());
     76 
     77         for (int j = firstRow; ; j++) {
     78             float dstY = (srcRect.y() + j * stepY) * scale.height() + phase.y();
     79             // assert that first row encroaches top edge of dstRect.
     80             ASSERT(j > firstRow || dstY <= destRect.y());
     81             ASSERT(j == firstRow || dstY > destRect.y());
     82 
     83             if (dstY >= destRect.maxY())
     84                 break;
     85             float dstMaxY = dstY + srcRect.height() * scale.height();
     86             if (dstY < destRect.y())
     87                 dstY = destRect.y();
     88             if (dstMaxY > destRect.maxY())
     89                 dstMaxY = destRect.maxY();
     90             if (dstY >= dstMaxY)
     91                 continue;
     92 
     93             tileDstRect.setY(dstY);
     94             tileDstRect.setHeight(dstMaxY - dstY);
     95             visibleSrcRect.setY((tileDstRect.y() - phase.y()) / scale.height() - j * stepY);
     96             visibleSrcRect.setHeight(tileDstRect.height() / scale.height());
     97             draw(destContext, tileDstRect, visibleSrcRect, compositeOp, blendMode);
     98         }
     99     }
    100 }
    101 
    102 }
    103