2 * Copyright (c) 2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "RenderSVGModelObject.h"
36 #include "RenderLayerModelObject.h"
37 #include "RenderSVGResource.h"
38 #include "SVGElement.h"
40 #include "SVGResourcesCache.h"
41 #include "ShadowRoot.h"
45 RenderSVGModelObject::RenderSVGModelObject(SVGElement& element, PassRef<RenderStyle> style)
46 : RenderElement(element, std::move(style), 0)
47 , m_hasSVGShadow(false)
51 LayoutRect RenderSVGModelObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
53 return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
56 void RenderSVGModelObject::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
58 SVGRenderSupport::computeFloatRectForRepaint(*this, repaintContainer, repaintRect, fixed);
61 void RenderSVGModelObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
63 SVGRenderSupport::mapLocalToContainer(*this, repaintContainer, transformState, wasFixed);
66 const RenderObject* RenderSVGModelObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
68 return SVGRenderSupport::pushMappingToContainer(*this, ancestorToStopAt, geometryMap);
71 // Copied from RenderBox, this method likely requires further refactoring to work easily for both SVG and CSS Box Model content.
72 // FIXME: This may also need to move into SVGRenderSupport as the RenderBox version depends
73 // on borderBoundingBox() which SVG RenderBox subclases (like SVGRenderBlock) do not implement.
74 LayoutRect RenderSVGModelObject::outlineBoundsForRepaint(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap*) const
76 LayoutRect box = enclosingLayoutRect(repaintRectInLocalCoordinates());
77 adjustRectForOutlineAndShadow(box);
79 FloatQuad containerRelativeQuad = localToContainerQuad(FloatRect(box), repaintContainer);
80 return containerRelativeQuad.enclosingBoundingBox();
83 void RenderSVGModelObject::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
85 IntRect rect = enclosingIntRect(strokeBoundingBox());
86 rect.moveBy(roundedIntPoint(accumulatedOffset));
90 void RenderSVGModelObject::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
92 quads.append(localToAbsoluteQuad(strokeBoundingBox(), 0 /* mode */, wasFixed));
95 void RenderSVGModelObject::willBeDestroyed()
97 SVGResourcesCache::clientDestroyed(*this);
98 RenderElement::willBeDestroyed();
101 void RenderSVGModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
103 if (diff == StyleDifferenceLayout) {
104 setNeedsBoundariesUpdate();
105 if (style().hasTransform())
106 setNeedsTransformUpdate();
108 RenderElement::styleDidChange(diff, oldStyle);
109 SVGResourcesCache::clientStyleChanged(*this, diff, style());
112 bool RenderSVGModelObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
114 ASSERT_NOT_REACHED();
118 static void getElementCTM(SVGElement* element, AffineTransform& transform)
121 element->document().updateLayoutIgnorePendingStylesheets();
123 SVGElement* stopAtElement = SVGLocatable::nearestViewportElement(element);
124 ASSERT(stopAtElement);
126 AffineTransform localTransform;
127 Node* current = element;
129 while (current && current->isSVGElement()) {
130 SVGElement* currentElement = toSVGElement(current);
131 localTransform = currentElement->renderer()->localToParentTransform();
132 transform = localTransform.multiply(transform);
133 // For getCTM() computation, stop at the nearest viewport element
134 if (currentElement == stopAtElement)
137 current = current->parentOrShadowHostNode();
141 // FloatRect::intersects does not consider horizontal or vertical lines (because of isEmpty()).
142 // So special-case handling of such lines.
143 static bool intersectsAllowingEmpty(const FloatRect& r, const FloatRect& other)
145 if (r.isEmpty() && other.isEmpty())
147 if (r.isEmpty() && !other.isEmpty()) {
148 return (other.contains(r.x(), r.y()) && !other.contains(r.maxX(), r.maxY()))
149 || (!other.contains(r.x(), r.y()) && other.contains(r.maxX(), r.maxY()));
151 if (other.isEmpty() && !r.isEmpty())
152 return intersectsAllowingEmpty(other, r);
153 return r.intersects(other);
156 // One of the element types that can cause graphics to be drawn onto the target canvas. Specifically: circle, ellipse,
157 // image, line, path, polygon, polyline, rect, text and use.
158 static bool isGraphicsElement(RenderObject* renderer)
160 return renderer->isSVGShape() || renderer->isSVGText() || renderer->isSVGImage() || renderer->node()->hasTagName(SVGNames::useTag);
163 // The SVG addFocusRingRects() method adds rects in local coordinates so the default absoluteFocusRingQuads
164 // returns incorrect values for SVG objects. Overriding this method provides access to the absolute bounds.
165 void RenderSVGModelObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
167 quads.append(localToAbsoluteQuad(FloatQuad(repaintRectInLocalCoordinates())));
170 bool RenderSVGModelObject::checkIntersection(RenderObject* renderer, const FloatRect& rect)
172 if (!renderer || renderer->style().pointerEvents() == PE_NONE)
174 if (!isGraphicsElement(renderer))
177 SVGElement* svgElement = toSVGElement(renderer->node());
178 getElementCTM(svgElement, ctm);
179 ASSERT(svgElement->renderer());
180 return intersectsAllowingEmpty(rect, ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
183 bool RenderSVGModelObject::checkEnclosure(RenderObject* renderer, const FloatRect& rect)
185 if (!renderer || renderer->style().pointerEvents() == PE_NONE)
187 if (!isGraphicsElement(renderer))
190 SVGElement* svgElement = toSVGElement(renderer->node());
191 getElementCTM(svgElement, ctm);
192 ASSERT(svgElement->renderer());
193 return rect.contains(ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
196 } // namespace WebCore
198 #endif // ENABLE(SVG)