1/*
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-2019 Apple Inc. All rights reserved.
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 * 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.
20 */
21
22#include "config.h"
23#include "SVGStopElement.h"
24
25#include "Document.h"
26#include "RenderSVGGradientStop.h"
27#include "RenderSVGResource.h"
28#include "SVGGradientElement.h"
29#include "SVGNames.h"
30#include <wtf/IsoMallocInlines.h>
31
32namespace WebCore {
33
34WTF_MAKE_ISO_ALLOCATED_IMPL(SVGStopElement);
35
36inline SVGStopElement::SVGStopElement(const QualifiedName& tagName, Document& document)
37 : SVGElement(tagName, document)
38{
39 ASSERT(hasTagName(SVGNames::stopTag));
40
41 static std::once_flag onceFlag;
42 std::call_once(onceFlag, [] {
43 PropertyRegistry::registerProperty<SVGNames::offsetAttr, &SVGStopElement::m_offset>();
44 });
45}
46
47Ref<SVGStopElement> SVGStopElement::create(const QualifiedName& tagName, Document& document)
48{
49 return adoptRef(*new SVGStopElement(tagName, document));
50}
51
52void SVGStopElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
53{
54 if (name == SVGNames::offsetAttr) {
55 if (value.endsWith('%'))
56 m_offset->setBaseValInternal(value.string().left(value.length() - 1).toFloat() / 100.0f);
57 else
58 m_offset->setBaseValInternal(value.toFloat());
59 return;
60 }
61
62 SVGElement::parseAttribute(name, value);
63}
64
65void SVGStopElement::svgAttributeChanged(const QualifiedName& attrName)
66{
67 if (attrName == SVGNames::offsetAttr) {
68 if (auto renderer = this->renderer()) {
69 InstanceInvalidationGuard guard(*this);
70 RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
71 }
72 return;
73 }
74
75 SVGElement::svgAttributeChanged(attrName);
76}
77
78RenderPtr<RenderElement> SVGStopElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
79{
80 return createRenderer<RenderSVGGradientStop>(*this, WTFMove(style));
81}
82
83bool SVGStopElement::rendererIsNeeded(const RenderStyle&)
84{
85 return true;
86}
87
88Color SVGStopElement::stopColorIncludingOpacity() const
89{
90 auto* style = renderer() ? &renderer()->style() : nullptr;
91 // FIXME: This check for null style exists to address Bug WK 90814, a rare crash condition in which the renderer or style is null.
92 if (!style)
93 return Color(Color::transparent, true);
94
95 const SVGRenderStyle& svgStyle = style->svgStyle();
96 float colorAlpha = svgStyle.stopColor().alpha() / 255.0;
97 // FIXME: This should use colorWithAlphaMultipliedBy() but that has different rounding of the alpha component.
98 return colorWithOverrideAlpha(svgStyle.stopColor().rgb(), colorAlpha * svgStyle.stopOpacity());
99}
100
101}
102