1 /*
2 Copyright (C) 2006, 2007 Nikolas Zimmermann <zimmermann@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 CGContextSaveGState(context);
39 CGContextTranslateCTM(context, 0, patternImage->size().height());
40 CGContextScaleCTM(context, 1.0f, -1.0f);
41 CGContextDrawImage(context, CGRect(FloatRect(FloatPoint(), patternImage->size())), patternImage->image()->getCGImageRef());
42 CGContextRestoreGState(context);
43 }
44
45 bool SVGPaintServerPattern::setup(GraphicsContext*& context, const RenderObject* object, SVGPaintTargetType type, bool isPaintingText) const
46 {
47 CGContextRef contextRef = context->platformContext();
48
49 // Build pattern tile, passing destination object bounding box
50 FloatRect targetRect;
51 if (isPaintingText) {
52 IntRect textBoundary = const_cast<RenderObject*>(object)->absoluteBoundingBoxRect();
53 targetRect = object->absoluteTransform().inverse().mapRect(textBoundary);
54 } else
55 targetRect = object->relativeBBox(false);
56
57 m_ownerElement->buildPattern(targetRect);
58
59 if (!tile())
60 return false;
61
62 context->save();
63
64 // Respect local pattern transformation
65 context->concatCTM(patternTransform());
66
67 // Apply pattern space transformation
68 context->translate(patternBoundaries().x(), patternBoundaries().y());
69
70 // Crude hack to support overflow="visible".
71 // When the patternBoundaries() size is smaller than the actual tile() size, we run into a problem:
72 // Our tile contains content which is larger than the pattern cell size. We just draw the pattern
73 // "out of" cell boundaries, to draw the overflown content, instead of clipping it away. The uppermost
74 // cell doesn't include the overflown content of the cell right above it though -> that's why we're moving
75 // down the phase by a very small amount, so we're sure the "cell right above"'s overflown content gets drawn.
76 CGContextSetPatternPhase(contextRef, CGSizeMake(0.0f, -0.01f));
77
78 RenderStyle* style = object->style();
79 CGContextSetAlpha(contextRef, style->opacity());
80
81 CGPatternCallbacks callbacks = {0, patternCallback, 0};
82
83 ASSERT(!m_pattern);
84 m_pattern = CGPatternCreate(tile(),
85 CGRect(FloatRect(FloatPoint(), tile()->size())),
86 CGContextGetCTM(contextRef),
87 patternBoundaries().width(),
88 patternBoundaries().height(),
89 kCGPatternTilingConstantSpacing,
90 true, // has color
91 &callbacks);
92
93 if (!m_patternSpace)
94 m_patternSpace = CGColorSpaceCreatePattern(0);
95
96 if ((type & ApplyToFillTargetType) && style->svgStyle()->hasFill()) {
97 CGFloat alpha = style->svgStyle()->fillOpacity();
98 CGContextSetFillColorSpace(contextRef, m_patternSpace);
99 CGContextSetFillPattern(contextRef, m_pattern, &alpha);
100
101 if (isPaintingText)
102 context->setTextDrawingMode(cTextFill);
103 }
104
105 if ((type & ApplyToStrokeTargetType) && style->svgStyle()->hasStroke()) {
106 CGFloat alpha = style->svgStyle()->strokeOpacity();
107 CGContextSetStrokeColorSpace(contextRef, m_patternSpace);
108 CGContextSetStrokePattern(contextRef, m_pattern, &alpha);
109 applyStrokeStyleToContext(context, style, object);
110
111 if (isPaintingText)
112 context->setTextDrawingMode(cTextStroke);
113 }
114
115 return true;
116 }
117
118 void SVGPaintServerPattern::teardown(GraphicsContext*& context, const RenderObject*, SVGPaintTargetType, bool) const
119 {
120 CGPatternRelease(m_pattern);
121 m_pattern = 0;
122
123 context->restore();
124 }
125
126 } // namespace WebCore
127
128 #endif
129
130 // vim:ts=4:noet