1/*
2 * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "SVGFESpotLightElement.h"
22
23#include "GeometryUtilities.h"
24#include "SVGFilterBuilder.h"
25#include "SVGNames.h"
26#include "SpotLightSource.h"
27#include <wtf/MathExtras.h>
28
29namespace WebCore {
30
31inline SVGFESpotLightElement::SVGFESpotLightElement(const QualifiedName& tagName, Document& document)
32 : SVGFELightElement(tagName, document)
33{
34 ASSERT(hasTagName(SVGNames::feSpotLightTag));
35}
36
37Ref<SVGFESpotLightElement> SVGFESpotLightElement::create(const QualifiedName& tagName, Document& document)
38{
39 return adoptRef(*new SVGFESpotLightElement(tagName, document));
40}
41
42Ref<LightSource> SVGFESpotLightElement::lightSource(SVGFilterBuilder& builder) const
43{
44 FloatPoint3D position;
45 FloatPoint3D pointsAt;
46
47 if (builder.primitiveUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
48 FloatRect referenceBox = builder.targetBoundingBox();
49
50 position.setX(referenceBox.x() + x() * referenceBox.width());
51 position.setY(referenceBox.y() + y() * referenceBox.height());
52 // https://www.w3.org/TR/SVG/filters.html#fePointLightZAttribute and https://www.w3.org/TR/SVG/coords.html#Units_viewport_percentage
53 position.setZ(z() * euclidianDistance(referenceBox.minXMinYCorner(), referenceBox.maxXMaxYCorner()) / sqrtOfTwoFloat);
54
55 pointsAt.setX(referenceBox.x() + pointsAtX() * referenceBox.width());
56 pointsAt.setY(referenceBox.y() + pointsAtY() * referenceBox.height());
57 // https://www.w3.org/TR/SVG/filters.html#fePointLightZAttribute and https://www.w3.org/TR/SVG/coords.html#Units_viewport_percentage
58 pointsAt.setZ(pointsAtZ() * euclidianDistance(referenceBox.minXMinYCorner(), referenceBox.maxXMaxYCorner()) / sqrtOfTwoFloat);
59 } else {
60 position = FloatPoint3D(x(), y(), z());
61 pointsAt = FloatPoint3D(pointsAtX(), pointsAtY(), pointsAtZ());
62 }
63
64 return SpotLightSource::create(position, pointsAt, specularExponent(), limitingConeAngle());
65}
66
67}
68