1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
2004, 2005 Rob Buis <buis@kde.org>
2005 Eric Seidel <eric@webkit.org>
2009 Dirk Schulze <krit@webkit.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
aint with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(SVG) && ENABLE(FILTERS)
#include "SVGResourceFilter.h"
#include "FilterEffect.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "PlatformString.h"
#include "SVGFilter.h"
#include "SVGFilterBuilder.h"
#include "SVGFilterElement.h"
#include "SVGRenderTreeAsText.h"
#include "SVGFilterPrimitiveStandardAttributes.h"
static const float kMaxFilterSize = 5000.0f;
using std::min;
namespace WebCore {
SVGResourceFilter::SVGResourceFilter(const SVGFilterElement* ownerElement)
: SVGResource()
, m_ownerElement(ownerElement)
, m_filterBBoxMode(false)
, m_effectBBoxMode(false)
, m_filterRes(false)
, m_scaleX(1.f)
, m_scaleY(1.f)
, m_savedContext(0)
, m_sourceGraphicBuffer(0)
{
m_filterBuilder.set(new SVGFilterBuilder());
}
SVGResourceFilter::~SVGResourceFilter()
{
}
static inline bool shouldProcessFilter(SVGResourceFilter* filter)
{
return (!filter->scaleX() || !filter->scaleY() || !filter->filterBoundingBox().width()
|| !filter->filterBoundingBox().height());
}
void SVGResourceFilter::addFilterEffect(SVGFilterPrimitiveStandardAttributes* effectAttributes, PassRefPtr<FilterEffect> effect)
{
effectAttributes->setStandardAttributes(this, effect.get());
builder()->add(effectAttributes->result(), effect);
}
bool SVGResourceFilter::fitsInMaximumImageSize(const FloatSize& size)
{
bool matchesFilterSize = true;
if (size.width() > kMaxFilterSize) {
m_scaleX *= kMaxFilterSize / size.width();
matchesFilterSize = false;
}
if (size.height() > kMaxFilterSize) {
m_scaleY *= kMaxFilterSize / size.height();
matchesFilterSize = false;
}
return matchesFilterSize;
}
void SVGResourceFilter::prepareFilter(GraphicsContext*& context, const RenderObject* object)
{
FloatRect targetRect = object->objectBoundingBox();
m_ownerElement->buildFilter(targetRect);
if (shouldProcessFilter(this))
return;
// clip sourceImage to filterRegion
FloatRect clippedSourceRect = targetRect;
clippedSourceRect.intersect(m_filterBBox);
// scale filter size to filterRes
FloatRect tempSourceRect = clippedSourceRect;
if (m_filterRes) {
m_scaleX = m_filterResSize.width() / m_filterBBox.width();
m_scaleY = m_filterResSize.height() / m_filterBBox.height();
}
// scale to big sourceImage size to kMaxFilterSize
tempSourceRect.scale(m_scaleX, m_scaleY);
fitsInMaximumImageSize(tempSourceRect.size());
// prepare Filters
m_filter = SVGFilter::create(targetRect, m_filterBBox, m_effectBBoxMode);
m_filter->setFilterResolution(FloatSize(m_scaleX, m_scaleY));
FilterEffect* lastEffect = m_filterBuilder->lastEffect();
if (lastEffect) {
lastEffect->calculateEffectRect(m_filter.get());
// at least one FilterEffect has a too big image size,
// recalculate the effect sizes with new scale factors
if (!fitsInMaximumImageSize(m_filter->maxImageSize())) {
m_filter->setFilterResolution(FloatSize(m_scaleX, m_scaleY));
lastEffect->calculateEffectRect(m_filter.get());
}
}
clippedSourceRect.scale(m_scaleX, m_scaleY);
// Draw the content of the current element and it's childs to a imageBuffer to get the SourceGraphic.
// The size of the SourceGraphic is clipped to the size of the filterRegion.
IntRect bufferRect = enclosingIntRect(clippedSourceRect);
OwnPtr<ImageBuffer> sourceGraphic(ImageBuffer::create(bufferRect.size(), LinearRGB));
if (!sourceGraphic.get())
return;
GraphicsContext* sourceGraphicContext = sourceGraphic->context();
sourceGraphicContext->scale(FloatSize(m_scaleX, m_scaleY));
sourceGraphicContext->translate(-targetRect.x(), -targetRect.y());
sourceGraphicContext->clearRect(FloatRect(FloatPoint(), targetRect.size()));
m_sourceGraphicBuffer.set(sourceGraphic.release());
m_savedContext = context;
context = sourceGraphicContext;
}
void SVGResourceFilter::applyFilter(GraphicsContext*& context, const RenderObject* object)
{
if (shouldProcessFilter(this))
return;
if (!m_savedContext)
return;
context = m_savedContext;
m_savedContext = 0;
#if !PLATFORM(CG)
m_sourceGraphicBuffer->transformColorSpace(DeviceRGB, LinearRGB);
#endif
FilterEffect* lastEffect = m_filterBuilder->lastEffect();
if (lastEffect && !m_filterBBox.isEmpty() && !lastEffect->subRegion().isEmpty()) {
m_filter->setSourceImage(m_sourceGraphicBuffer.release());
lastEffect->apply(m_filter.get());
ImageBuffer* resultImage = lastEffect->resultImage();
if (resultImage) {
#if !PLATFORM(CG)
resultImage->transformColorSpace(LinearRGB, DeviceRGB);
#endif
ColorSpace colorSpace = DeviceColorSpace;
if (object)
colorSpace = object->style()->colorSpace();
context->drawImage(resultImage->image(), colorSpace, lastEffect->subRegion());
}
}
m_sourceGraphicBuffer.clear();
}
TextStream& SVGResourceFilter::externalRepresentation(TextStream& ts) const
{
ts << "[type=FILTER] ";
FloatRect bbox = filterRect();
static FloatRect defaultFilterRect(0, 0, 1, 1);
if (!filterBoundingBoxMode() || bbox != defaultFilterRect) {
ts << " [bounding box=";
if (filterBoundingBoxMode()) {
bbox.scale(100.f);
ts << "at (" << bbox.x() << "%," << bbox.y() << "%) size " << bbox.width() << "%x" << bbox.height() << "%";
} else
ts << filterRect();
ts << "]";
}
if (!filterBoundingBoxMode()) // default is true
ts << " [bounding box mode=" << filterBoundingBoxMode() << "]";
if (effectBoundingBoxMode()) // default is false
ts << " [effect bounding box mode=" << effectBoundingBoxMode() << "]";
return ts;
}
SVGResourceFilter* getFilterById(Document* document, const AtomicString& id)
{
SVGResource* resource = getResourceById(document, id);
if (resource && resource->isFilter())
return static_cast<SVGResourceFilter*>(resource);
return 0;
}
} // namespace WebCore
#endif // ENABLE(SVG)
|