1/*
2 * Copyright (c) 2009, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "RenderSVGModelObject.h"
33
34#include "RenderLayerModelObject.h"
35#include "RenderSVGResource.h"
36#include "SVGNames.h"
37#include "SVGResourcesCache.h"
38#include "ShadowRoot.h"
39#include <wtf/IsoMallocInlines.h>
40
41namespace WebCore {
42
43WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGModelObject);
44
45RenderSVGModelObject::RenderSVGModelObject(SVGElement& element, RenderStyle&& style)
46 : RenderElement(element, WTFMove(style), 0)
47{
48}
49
50LayoutRect RenderSVGModelObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
51{
52 return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
53}
54
55Optional<FloatRect> RenderSVGModelObject::computeFloatVisibleRectInContainer(const FloatRect& rect, const RenderLayerModelObject* container, VisibleRectContext context) const
56{
57 return SVGRenderSupport::computeFloatVisibleRectInContainer(*this, rect, container, context);
58}
59
60void RenderSVGModelObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
61{
62 SVGRenderSupport::mapLocalToContainer(*this, repaintContainer, transformState, wasFixed);
63}
64
65const RenderObject* RenderSVGModelObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
66{
67 return SVGRenderSupport::pushMappingToContainer(*this, ancestorToStopAt, geometryMap);
68}
69
70// Copied from RenderBox, this method likely requires further refactoring to work easily for both SVG and CSS Box Model content.
71// FIXME: This may also need to move into SVGRenderSupport as the RenderBox version depends
72// on borderBoundingBox() which SVG RenderBox subclases (like SVGRenderBlock) do not implement.
73LayoutRect RenderSVGModelObject::outlineBoundsForRepaint(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap*) const
74{
75 LayoutRect box = enclosingLayoutRect(repaintRectInLocalCoordinates());
76 adjustRectForOutlineAndShadow(box);
77
78 FloatQuad containerRelativeQuad = localToContainerQuad(FloatRect(box), repaintContainer);
79 return LayoutRect(snapRectToDevicePixels(LayoutRect(containerRelativeQuad.boundingBox()), document().deviceScaleFactor()));
80}
81
82void RenderSVGModelObject::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
83{
84 IntRect rect = enclosingIntRect(strokeBoundingBox());
85 rect.moveBy(roundedIntPoint(accumulatedOffset));
86 rects.append(rect);
87}
88
89void RenderSVGModelObject::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
90{
91 quads.append(localToAbsoluteQuad(strokeBoundingBox(), UseTransforms, wasFixed));
92}
93
94void RenderSVGModelObject::willBeDestroyed()
95{
96 SVGResourcesCache::clientDestroyed(*this);
97 RenderElement::willBeDestroyed();
98}
99
100void RenderSVGModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
101{
102 if (diff == StyleDifference::Layout) {
103 setNeedsBoundariesUpdate();
104 if (style().hasTransform())
105 setNeedsTransformUpdate();
106 }
107 RenderElement::styleDidChange(diff, oldStyle);
108 SVGResourcesCache::clientStyleChanged(*this, diff, style());
109}
110
111bool RenderSVGModelObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
112{
113 ASSERT_NOT_REACHED();
114 return false;
115}
116
117static void getElementCTM(SVGElement* element, AffineTransform& transform)
118{
119 ASSERT(element);
120
121 SVGElement* stopAtElement = SVGLocatable::nearestViewportElement(element);
122 ASSERT(stopAtElement);
123
124 AffineTransform localTransform;
125 Node* current = element;
126
127 while (current && current->isSVGElement()) {
128 SVGElement& currentElement = downcast<SVGElement>(*current);
129 localTransform = currentElement.renderer()->localToParentTransform();
130 transform = localTransform.multiply(transform);
131 // For getCTM() computation, stop at the nearest viewport element
132 if (&currentElement == stopAtElement)
133 break;
134
135 current = current->parentOrShadowHostNode();
136 }
137}
138
139// FloatRect::intersects does not consider horizontal or vertical lines (because of isEmpty()).
140// So special-case handling of such lines.
141static bool intersectsAllowingEmpty(const FloatRect& r, const FloatRect& other)
142{
143 if (r.isEmpty() && other.isEmpty())
144 return false;
145 if (r.isEmpty() && !other.isEmpty()) {
146 return (other.contains(r.x(), r.y()) && !other.contains(r.maxX(), r.maxY()))
147 || (!other.contains(r.x(), r.y()) && other.contains(r.maxX(), r.maxY()));
148 }
149 if (other.isEmpty() && !r.isEmpty())
150 return intersectsAllowingEmpty(other, r);
151 return r.intersects(other);
152}
153
154// One of the element types that can cause graphics to be drawn onto the target canvas. Specifically: circle, ellipse,
155// image, line, path, polygon, polyline, rect, text and use.
156static bool isGraphicsElement(const RenderElement& renderer)
157{
158 return renderer.isSVGShape() || renderer.isSVGText() || renderer.isSVGImage() || renderer.element()->hasTagName(SVGNames::useTag);
159}
160
161// The SVG addFocusRingRects() method adds rects in local coordinates so the default absoluteFocusRingQuads
162// returns incorrect values for SVG objects. Overriding this method provides access to the absolute bounds.
163void RenderSVGModelObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
164{
165 quads.append(localToAbsoluteQuad(FloatQuad(repaintRectInLocalCoordinates())));
166}
167
168bool RenderSVGModelObject::checkIntersection(RenderElement* renderer, const FloatRect& rect)
169{
170 if (!renderer || renderer->style().pointerEvents() == PointerEvents::None)
171 return false;
172 if (!isGraphicsElement(*renderer))
173 return false;
174 AffineTransform ctm;
175 SVGElement* svgElement = downcast<SVGElement>(renderer->element());
176 getElementCTM(svgElement, ctm);
177 ASSERT(svgElement->renderer());
178 return intersectsAllowingEmpty(rect, ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
179}
180
181bool RenderSVGModelObject::checkEnclosure(RenderElement* renderer, const FloatRect& rect)
182{
183 if (!renderer || renderer->style().pointerEvents() == PointerEvents::None)
184 return false;
185 if (!isGraphicsElement(*renderer))
186 return false;
187 AffineTransform ctm;
188 SVGElement* svgElement = downcast<SVGElement>(renderer->element());
189 getElementCTM(svgElement, ctm);
190 ASSERT(svgElement->renderer());
191 return rect.contains(ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
192}
193
194} // namespace WebCore
195