2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2018 Apple Inc. All rights reserved.
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.
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.
16 * You should have received a copy of the GNU Library General Public License
17 * along 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.
23 #include "SVGStopElement.h"
26 #include "RenderSVGGradientStop.h"
27 #include "RenderSVGResource.h"
28 #include "SVGGradientElement.h"
30 #include <wtf/IsoMallocInlines.h>
34 WTF_MAKE_ISO_ALLOCATED_IMPL(SVGStopElement);
36 inline SVGStopElement::SVGStopElement(const QualifiedName& tagName, Document& document)
37 : SVGElement(tagName, document)
39 ASSERT(hasTagName(SVGNames::stopTag));
43 Ref<SVGStopElement> SVGStopElement::create(const QualifiedName& tagName, Document& document)
45 return adoptRef(*new SVGStopElement(tagName, document));
48 void SVGStopElement::registerAttributes()
50 auto& registry = attributeRegistry();
51 if (!registry.isEmpty())
53 registry.registerAttribute<SVGNames::offsetAttr, &SVGStopElement::m_offset>();
56 void SVGStopElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
58 if (name == SVGNames::offsetAttr) {
59 if (value.endsWith('%'))
60 m_offset.setValue(value.string().left(value.length() - 1).toFloat() / 100.0f);
62 m_offset.setValue(value.toFloat());
66 SVGElement::parseAttribute(name, value);
69 void SVGStopElement::svgAttributeChanged(const QualifiedName& attrName)
71 if (attrName == SVGNames::offsetAttr) {
72 if (auto renderer = this->renderer()) {
73 InstanceInvalidationGuard guard(*this);
74 RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
79 SVGElement::svgAttributeChanged(attrName);
82 RenderPtr<RenderElement> SVGStopElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
84 return createRenderer<RenderSVGGradientStop>(*this, WTFMove(style));
87 bool SVGStopElement::rendererIsNeeded(const RenderStyle&)
92 Color SVGStopElement::stopColorIncludingOpacity() const
94 auto* style = renderer() ? &renderer()->style() : nullptr;
95 // FIXME: This check for null style exists to address Bug WK 90814, a rare crash condition in
96 // which the renderer or style is null. This entire class is scheduled for removal (Bug WK 86941)
97 // and we will tolerate this null check until then.
99 return Color(Color::transparent, true); // Transparent black.
101 const SVGRenderStyle& svgStyle = style->svgStyle();
102 float colorAlpha = svgStyle.stopColor().alpha() / 255.0;
103 return colorWithOverrideAlpha(svgStyle.stopColor().rgb(), colorAlpha * svgStyle.stopOpacity());