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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
2008 Eric Seidel <eric@webkit.org>
2008 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
along 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)
#include "SVGRadialGradientElement.h"
#include "FloatConversion.h"
#include "FloatPoint.h"
#include "MappedAttribute.h"
#include "RadialGradientAttributes.h"
#include "RenderObject.h"
#include "SVGLength.h"
#include "SVGNames.h"
#include "SVGPaintServerRadialGradient.h"
#include "SVGStopElement.h"
#include "SVGTransform.h"
#include "SVGTransformList.h"
#include "SVGUnitTypes.h"
namespace WebCore {
SVGRadialGradientElement::SVGRadialGradientElement(const QualifiedName& tagName, Document* doc)
: SVGGradientElement(tagName, doc)
, m_cx(LengthModeWidth, "50%")
, m_cy(LengthModeHeight, "50%")
, m_r(LengthModeOther, "50%")
, m_fx(LengthModeWidth)
, m_fy(LengthModeHeight)
{
// Spec: If the cx/cy/r attribute is not specified, the effect is as if a value of "50%" were specified.
}
SVGRadialGradientElement::~SVGRadialGradientElement()
{
}
void SVGRadialGradientElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == SVGNames::cxAttr)
setCxBaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::cyAttr)
setCyBaseValue(SVGLength(LengthModeHeight, attr->value()));
else if (attr->name() == SVGNames::rAttr) {
setRBaseValue(SVGLength(LengthModeOther, attr->value()));
if (rBaseValue().value(this) < 0.0)
document()->accessSVGExtensions()->reportError("A negative value for radial gradient radius <r> is not allowed");
} else if (attr->name() == SVGNames::fxAttr)
setFxBaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::fyAttr)
setFyBaseValue(SVGLength(LengthModeHeight, attr->value()));
else
SVGGradientElement::parseMappedAttribute(attr);
}
void SVGRadialGradientElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGGradientElement::svgAttributeChanged(attrName);
if (!m_resource)
return;
if (attrName == SVGNames::cxAttr || attrName == SVGNames::cyAttr ||
attrName == SVGNames::fxAttr || attrName == SVGNames::fyAttr ||
attrName == SVGNames::rAttr)
m_resource->invalidate();
}
void SVGRadialGradientElement::synchronizeProperty(const QualifiedName& attrName)
{
SVGGradientElement::synchronizeProperty(attrName);
if (attrName == anyQName()) {
synchronizeCx();
synchronizeCy();
synchronizeFx();
synchronizeFy();
synchronizeR();
return;
}
if (attrName == SVGNames::cxAttr)
synchronizeCx();
else if (attrName == SVGNames::cyAttr)
synchronizeCy();
else if (attrName == SVGNames::fxAttr)
synchronizeFx();
else if (attrName == SVGNames::fyAttr)
synchronizeFy();
else if (attrName == SVGNames::rAttr)
synchronizeR();
}
void SVGRadialGradientElement::buildGradient() const
{
RadialGradientAttributes attributes = collectGradientProperties();
RefPtr<SVGPaintServerRadialGradient> radialGradient = WTF::static_pointer_cast<SVGPaintServerRadialGradient>(m_resource);
FloatPoint focalPoint;
FloatPoint centerPoint;
float radius;
if (attributes.boundingBoxMode()) {
focalPoint = FloatPoint(attributes.fx().valueAsPercentage(), attributes.fy().valueAsPercentage());
centerPoint = FloatPoint(attributes.cx().valueAsPercentage(), attributes.cy().valueAsPercentage());
radius = attributes.r().valueAsPercentage();
} else {
focalPoint = FloatPoint(attributes.fx().value(this), attributes.fy().value(this));
centerPoint = FloatPoint(attributes.cx().value(this), attributes.cy().value(this));
radius = attributes.r().value(this);
}
FloatPoint adjustedFocalPoint = focalPoint;
float dfx = focalPoint.x() - centerPoint.x();
float dfy = focalPoint.y() - centerPoint.y();
float rMax = 0.99f * radius;
// Spec: If (fx, fy) lies outside the circle defined by (cx, cy) and
// r, set (fx, fy) to the point of intersection of the line through
// (fx, fy) and the circle.
// We scale the radius by 0.99 to match the behavior of FireFox.
if (sqrt(dfx * dfx + dfy * dfy) > rMax) {
float angle = atan2f(dfy, dfx);
dfx = cosf(angle) * rMax;
dfy = sinf(angle) * rMax;
adjustedFocalPoint = FloatPoint(dfx + centerPoint.x(), dfy + centerPoint.y());
}
RefPtr<Gradient> gradient = Gradient::create(
adjustedFocalPoint,
0.f, // SVG does not support a "focus radius"
centerPoint,
radius);
gradient->setSpreadMethod(attributes.spreadMethod());
Vector<SVGGradientStop> stops = attributes.stops();
float previousOffset = 0.0f;
for (unsigned i = 0; i < stops.size(); ++i) {
float offset = std::min(std::max(previousOffset, stops[i].first), 1.0f);
previousOffset = offset;
gradient->addColorStop(offset, stops[i].second);
}
radialGradient->setGradient(gradient);
if (attributes.stops().isEmpty())
return;
radialGradient->setBoundingBoxMode(attributes.boundingBoxMode());
radialGradient->setGradientTransform(attributes.gradientTransform());
radialGradient->setGradientCenter(centerPoint);
radialGradient->setGradientFocal(focalPoint);
radialGradient->setGradientRadius(radius);
radialGradient->setGradientStops(attributes.stops());
}
RadialGradientAttributes SVGRadialGradientElement::collectGradientProperties() const
{
RadialGradientAttributes attributes;
HashSet<const SVGGradientElement*> processedGradients;
bool isRadial = true;
const SVGGradientElement* current = this;
while (current) {
if (!attributes.hasSpreadMethod() && current->hasAttribute(SVGNames::spreadMethodAttr))
attributes.setSpreadMethod((GradientSpreadMethod) current->spreadMethod());
if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::gradientUnitsAttr))
attributes.setBoundingBoxMode(current->gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
if (!attributes.hasGradientTransform() && current->hasAttribute(SVGNames::gradientTransformAttr))
attributes.setGradientTransform(current->gradientTransform()->consolidate().matrix());
if (!attributes.hasStops()) {
const Vector<SVGGradientStop>& stops(current->buildStops());
if (!stops.isEmpty())
attributes.setStops(stops);
}
if (isRadial) {
const SVGRadialGradientElement* radial = static_cast<const SVGRadialGradientElement*>(current);
if (!attributes.hasCx() && current->hasAttribute(SVGNames::cxAttr))
attributes.setCx(radial->cx());
if (!attributes.hasCy() && current->hasAttribute(SVGNames::cyAttr))
attributes.setCy(radial->cy());
if (!attributes.hasR() && current->hasAttribute(SVGNames::rAttr))
attributes.setR(radial->r());
if (!attributes.hasFx() && current->hasAttribute(SVGNames::fxAttr))
attributes.setFx(radial->fx());
if (!attributes.hasFy() && current->hasAttribute(SVGNames::fyAttr))
attributes.setFy(radial->fy());
}
processedGradients.add(current);
// Respect xlink:href, take attributes from referenced element
Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
if (refNode && (refNode->hasTagName(SVGNames::radialGradientTag) || refNode->hasTagName(SVGNames::linearGradientTag))) {
current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
// Cycle detection
if (processedGradients.contains(current))
return RadialGradientAttributes();
isRadial = current->gradientType() == RadialGradientPaintServer;
} else
current = 0;
}
// Handle default values for fx/fy
if (!attributes.hasFx())
attributes.setFx(attributes.cx());
if (!attributes.hasFy())
attributes.setFy(attributes.cy());
return attributes;
}
}
#endif // ENABLE(SVG)
|