webengine/osswebengine/WebCore/platform/graphics/svg/cg/SVGPaintServerPatternCg.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2     Copyright (C) 2006 Nikolas Zimmermann <wildfox@kde.org>
       
     3 
       
     4     This file is part of the KDE project
       
     5 
       
     6     This library is free software; you can redistribute it and/or
       
     7     modify it under the terms of the GNU Library General Public
       
     8     License as published by the Free Software Foundation; either
       
     9     version 2 of the License, or (at your option) any later version.
       
    10 
       
    11     This library is distributed in the hope that it will be useful,
       
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14     Library General Public License for more details.
       
    15 
       
    16     You should have received a copy of the GNU Library General Public License
       
    17     aint with this library; see the file COPYING.LIB.  If not, write to
       
    18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19     Boston, MA 02110-1301, USA.
       
    20 */
       
    21 
       
    22 #include "config.h"
       
    23 
       
    24 #if ENABLE(SVG)
       
    25 #include "SVGPaintServerPattern.h"
       
    26 
       
    27 #include "CgSupport.h"
       
    28 #include "GraphicsContext.h"
       
    29 #include "ImageBuffer.h"
       
    30 #include "RenderObject.h"
       
    31 #include "SVGPatternElement.h"
       
    32 
       
    33 namespace WebCore {
       
    34 
       
    35 static void patternCallback(void* info, CGContextRef context)
       
    36 {
       
    37     ImageBuffer* patternImage = reinterpret_cast<ImageBuffer*>(info);
       
    38 
       
    39     IntSize patternContentSize = patternImage->size();
       
    40     CGContextDrawImage(context, CGRectMake(0, 0, patternContentSize.width(), patternContentSize.height()), patternImage->cgImage());
       
    41 }
       
    42 
       
    43 bool SVGPaintServerPattern::setup(GraphicsContext*& context, const RenderObject* object, SVGPaintTargetType type, bool isPaintingText) const
       
    44 {
       
    45     CGContextRef contextRef = context->platformContext();
       
    46 
       
    47     // Build pattern tile, passing destination object bounding box
       
    48     FloatRect targetRect;
       
    49     if (isPaintingText) {
       
    50         IntRect textBoundary = const_cast<RenderObject*>(object)->absoluteBoundingBoxRect();
       
    51         targetRect = object->absoluteTransform().inverse().mapRect(textBoundary);
       
    52     } else
       
    53         targetRect = CGContextGetPathBoundingBox(contextRef);
       
    54 
       
    55     m_ownerElement->buildPattern(targetRect);
       
    56 
       
    57     if (!tile())
       
    58         return false;
       
    59 
       
    60     CGSize cellSize = CGSize(tile()->size());
       
    61     CGFloat alpha = 1; // canvasStyle->opacity(); //which?
       
    62 
       
    63     context->save();
       
    64 
       
    65     // Repesct local pattern transformations
       
    66     CGContextConcatCTM(contextRef, patternTransform());
       
    67 
       
    68     // Pattern space seems to start in the lower-left, so we flip the Y here. 
       
    69     CGSize phase = CGSizeMake(patternBoundaries().x(), -patternBoundaries().y());
       
    70     CGContextSetPatternPhase(contextRef, phase);
       
    71 
       
    72     RenderStyle* style = object->style();
       
    73     CGContextSetAlpha(contextRef, style->opacity()); // or do I set the alpha above?
       
    74 
       
    75     ASSERT(!m_pattern);
       
    76     CGPatternCallbacks callbacks = {0, patternCallback, NULL};
       
    77     m_pattern = CGPatternCreate(tile(),
       
    78                                 CGRectMake(0, 0, cellSize.width, cellSize.height),
       
    79                                 CGContextGetCTM(contextRef),
       
    80                                 patternBoundaries().width(),
       
    81                                 patternBoundaries().height(),
       
    82                                 kCGPatternTilingConstantSpacing, // FIXME: should ask CG guys.
       
    83                                 true, // has color
       
    84                                 &callbacks);
       
    85 
       
    86     if (!m_patternSpace)
       
    87         m_patternSpace = CGColorSpaceCreatePattern(0);
       
    88 
       
    89     if ((type & ApplyToFillTargetType) && style->svgStyle()->hasFill()) {
       
    90         CGContextSetFillColorSpace(contextRef, m_patternSpace);
       
    91         CGContextSetFillPattern(contextRef, m_pattern, &alpha);
       
    92  
       
    93         if (isPaintingText) 
       
    94             context->setTextDrawingMode(cTextFill);
       
    95     }
       
    96 
       
    97     if ((type & ApplyToStrokeTargetType) && style->svgStyle()->hasStroke()) {
       
    98         CGContextSetStrokeColorSpace(contextRef, m_patternSpace);
       
    99         CGContextSetStrokePattern(contextRef, m_pattern, &alpha);
       
   100         applyStrokeStyleToContext(contextRef, style, object);
       
   101 
       
   102         if (isPaintingText) 
       
   103             context->setTextDrawingMode(cTextStroke);
       
   104     }
       
   105 
       
   106     return true;
       
   107 }
       
   108 
       
   109 void SVGPaintServerPattern::teardown(GraphicsContext*& context, const RenderObject*, SVGPaintTargetType, bool) const
       
   110 {
       
   111     CGPatternRelease(m_pattern);
       
   112     m_pattern = 0;
       
   113 
       
   114     context->restore();
       
   115 }
       
   116 
       
   117 } // namespace WebCore
       
   118 
       
   119 #endif
       
   120 
       
   121 // vim:ts=4:noet