2017-08-12 16:38:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
|
|
|
|
* Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
|
|
|
|
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
|
|
|
|
* Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
|
|
|
|
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
|
2023-01-25 00:42:21 +00:00
|
|
|
* Copyright (C) 2018-2019 Apple Inc. All rights reserved.
|
2017-08-12 16:38:52 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "SVGLinearGradientElement.h"
|
|
|
|
|
|
|
|
#include "Document.h"
|
|
|
|
#include "FloatPoint.h"
|
|
|
|
#include "LinearGradientAttributes.h"
|
|
|
|
#include "RenderSVGResourceLinearGradient.h"
|
|
|
|
#include "SVGLengthValue.h"
|
|
|
|
#include "SVGNames.h"
|
|
|
|
#include "SVGUnitTypes.h"
|
2023-01-25 00:42:21 +00:00
|
|
|
#include <wtf/IsoMallocInlines.h>
|
2017-08-12 16:38:52 +00:00
|
|
|
#include <wtf/NeverDestroyed.h>
|
|
|
|
|
|
|
|
namespace WebCore {
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGLinearGradientElement);
|
2017-08-12 16:38:52 +00:00
|
|
|
|
|
|
|
inline SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document& document)
|
|
|
|
: SVGGradientElement(tagName, document)
|
|
|
|
{
|
|
|
|
// Spec: If the x2 attribute is not specified, the effect is as if a value of "100%" were specified.
|
|
|
|
ASSERT(hasTagName(SVGNames::linearGradientTag));
|
2023-01-25 00:42:21 +00:00
|
|
|
|
|
|
|
static std::once_flag onceFlag;
|
|
|
|
std::call_once(onceFlag, [] {
|
|
|
|
PropertyRegistry::registerProperty<SVGNames::x1Attr, &SVGLinearGradientElement::m_x1>();
|
|
|
|
PropertyRegistry::registerProperty<SVGNames::y1Attr, &SVGLinearGradientElement::m_y1>();
|
|
|
|
PropertyRegistry::registerProperty<SVGNames::x2Attr, &SVGLinearGradientElement::m_x2>();
|
|
|
|
PropertyRegistry::registerProperty<SVGNames::y2Attr, &SVGLinearGradientElement::m_y2>();
|
|
|
|
});
|
2017-08-12 16:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<SVGLinearGradientElement> SVGLinearGradientElement::create(const QualifiedName& tagName, Document& document)
|
|
|
|
{
|
|
|
|
return adoptRef(*new SVGLinearGradientElement(tagName, document));
|
|
|
|
}
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
void SVGLinearGradientElement::parseAttribute(const QualifiedName& name, const AtomString& value)
|
2017-08-12 16:38:52 +00:00
|
|
|
{
|
|
|
|
SVGParsingError parseError = NoError;
|
|
|
|
|
|
|
|
if (name == SVGNames::x1Attr)
|
2023-01-25 00:42:21 +00:00
|
|
|
m_x1->setBaseValInternal(SVGLengthValue::construct(SVGLengthMode::Width, value, parseError));
|
2017-08-12 16:38:52 +00:00
|
|
|
else if (name == SVGNames::y1Attr)
|
2023-01-25 00:42:21 +00:00
|
|
|
m_y1->setBaseValInternal(SVGLengthValue::construct(SVGLengthMode::Height, value, parseError));
|
2017-08-12 16:38:52 +00:00
|
|
|
else if (name == SVGNames::x2Attr)
|
2023-01-25 00:42:21 +00:00
|
|
|
m_x2->setBaseValInternal(SVGLengthValue::construct(SVGLengthMode::Width, value, parseError));
|
2017-08-12 16:38:52 +00:00
|
|
|
else if (name == SVGNames::y2Attr)
|
2023-01-25 00:42:21 +00:00
|
|
|
m_y2->setBaseValInternal(SVGLengthValue::construct(SVGLengthMode::Height, value, parseError));
|
2017-08-12 16:38:52 +00:00
|
|
|
|
|
|
|
reportAttributeParsingError(parseError, name, value);
|
|
|
|
|
|
|
|
SVGGradientElement::parseAttribute(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGLinearGradientElement::svgAttributeChanged(const QualifiedName& attrName)
|
|
|
|
{
|
2023-01-25 00:42:21 +00:00
|
|
|
if (PropertyRegistry::isKnownAttribute(attrName)) {
|
|
|
|
InstanceInvalidationGuard guard(*this);
|
|
|
|
updateRelativeLengthsInformation();
|
|
|
|
if (RenderObject* object = renderer())
|
|
|
|
object->setNeedsLayout();
|
2017-08-12 16:38:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
SVGGradientElement::svgAttributeChanged(attrName);
|
2017-08-12 16:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderPtr<RenderElement> SVGLinearGradientElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
|
|
|
|
{
|
|
|
|
return createRenderer<RenderSVGResourceLinearGradient>(*this, WTFMove(style));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setGradientAttributes(SVGGradientElement& element, LinearGradientAttributes& attributes, bool isLinear = true)
|
|
|
|
{
|
|
|
|
if (!attributes.hasSpreadMethod() && element.hasAttribute(SVGNames::spreadMethodAttr))
|
|
|
|
attributes.setSpreadMethod(element.spreadMethod());
|
|
|
|
|
|
|
|
if (!attributes.hasGradientUnits() && element.hasAttribute(SVGNames::gradientUnitsAttr))
|
|
|
|
attributes.setGradientUnits(element.gradientUnits());
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
if (!attributes.hasGradientTransform() && element.hasAttribute(SVGNames::gradientTransformAttr))
|
|
|
|
attributes.setGradientTransform(element.gradientTransform().concatenate());
|
2017-08-12 16:38:52 +00:00
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
if (!attributes.hasStops())
|
|
|
|
attributes.setStops(element.buildStops());
|
2017-08-12 16:38:52 +00:00
|
|
|
|
|
|
|
if (isLinear) {
|
|
|
|
SVGLinearGradientElement& linear = downcast<SVGLinearGradientElement>(element);
|
|
|
|
|
|
|
|
if (!attributes.hasX1() && element.hasAttribute(SVGNames::x1Attr))
|
|
|
|
attributes.setX1(linear.x1());
|
|
|
|
|
|
|
|
if (!attributes.hasY1() && element.hasAttribute(SVGNames::y1Attr))
|
|
|
|
attributes.setY1(linear.y1());
|
|
|
|
|
|
|
|
if (!attributes.hasX2() && element.hasAttribute(SVGNames::x2Attr))
|
|
|
|
attributes.setX2(linear.x2());
|
|
|
|
|
|
|
|
if (!attributes.hasY2() && element.hasAttribute(SVGNames::y2Attr))
|
|
|
|
attributes.setY2(linear.y2());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SVGLinearGradientElement::collectGradientAttributes(LinearGradientAttributes& attributes)
|
|
|
|
{
|
|
|
|
if (!renderer())
|
|
|
|
return false;
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
HashSet<Ref<SVGGradientElement>> processedGradients;
|
|
|
|
Ref<SVGGradientElement> current { *this };
|
2017-08-12 16:38:52 +00:00
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
setGradientAttributes(current.get(), attributes);
|
|
|
|
processedGradients.add(current.copyRef());
|
2017-08-12 16:38:52 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Respect xlink:href, take attributes from referenced element
|
2023-01-25 00:42:21 +00:00
|
|
|
auto target = SVGURIReference::targetElementFromIRIString(current->href(), treeScope());
|
|
|
|
if (is<SVGGradientElement>(target.element)) {
|
|
|
|
current = downcast<SVGGradientElement>(*target.element);
|
2017-08-12 16:38:52 +00:00
|
|
|
|
|
|
|
// Cycle detection
|
|
|
|
if (processedGradients.contains(current))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!current->renderer())
|
|
|
|
return false;
|
|
|
|
|
2023-01-25 00:42:21 +00:00
|
|
|
setGradientAttributes(current.get(), attributes, current->hasTagName(SVGNames::linearGradientTag));
|
|
|
|
processedGradients.add(current.copyRef());
|
2017-08-12 16:38:52 +00:00
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SVGLinearGradientElement::selfHasRelativeLengths() const
|
|
|
|
{
|
|
|
|
return x1().isRelative()
|
|
|
|
|| y1().isRelative()
|
|
|
|
|| x2().isRelative()
|
|
|
|
|| y2().isRelative();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|