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
40namespace WebCore {
41
42RenderSVGModelObject::RenderSVGModelObject(SVGElement& element, PassRef<RenderStyle> style)
43    : RenderElement(element, WTF::move(style), 0)
44    , m_hasSVGShadow(false)
45{
46}
47
48LayoutRect RenderSVGModelObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
49{
50    return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
51}
52
53void RenderSVGModelObject::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
54{
55    SVGRenderSupport::computeFloatRectForRepaint(*this, repaintContainer, repaintRect, fixed);
56}
57
58void RenderSVGModelObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
59{
60    SVGRenderSupport::mapLocalToContainer(*this, repaintContainer, transformState, wasFixed);
61}
62
63const RenderObject* RenderSVGModelObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
64{
65    return SVGRenderSupport::pushMappingToContainer(*this, ancestorToStopAt, geometryMap);
66}
67
68// Copied from RenderBox, this method likely requires further refactoring to work easily for both SVG and CSS Box Model content.
69// FIXME: This may also need to move into SVGRenderSupport as the RenderBox version depends
70// on borderBoundingBox() which SVG RenderBox subclases (like SVGRenderBlock) do not implement.
71LayoutRect RenderSVGModelObject::outlineBoundsForRepaint(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap*) const
72{
73    LayoutRect box = enclosingLayoutRect(repaintRectInLocalCoordinates());
74    adjustRectForOutlineAndShadow(box);
75
76    FloatQuad containerRelativeQuad = localToContainerQuad(FloatRect(box), repaintContainer);
77    return LayoutRect(pixelSnappedForPainting(LayoutRect(containerRelativeQuad.boundingBox()), document().deviceScaleFactor()));
78}
79
80void RenderSVGModelObject::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
81{
82    IntRect rect = enclosingIntRect(strokeBoundingBox());
83    rect.moveBy(roundedIntPoint(accumulatedOffset));
84    rects.append(rect);
85}
86
87void RenderSVGModelObject::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
88{
89    quads.append(localToAbsoluteQuad(strokeBoundingBox(), 0 /* mode */, wasFixed));
90}
91
92void RenderSVGModelObject::willBeDestroyed()
93{
94    SVGResourcesCache::clientDestroyed(*this);
95    RenderElement::willBeDestroyed();
96}
97
98void RenderSVGModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
99{
100    if (diff == StyleDifferenceLayout) {
101        setNeedsBoundariesUpdate();
102        if (style().hasTransform())
103            setNeedsTransformUpdate();
104    }
105    RenderElement::styleDidChange(diff, oldStyle);
106    SVGResourcesCache::clientStyleChanged(*this, diff, style());
107}
108
109bool RenderSVGModelObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
110{
111    ASSERT_NOT_REACHED();
112    return false;
113}
114
115static void getElementCTM(SVGElement* element, AffineTransform& transform)
116{
117    ASSERT(element);
118    element->document().updateLayoutIgnorePendingStylesheets();
119
120    SVGElement* stopAtElement = SVGLocatable::nearestViewportElement(element);
121    ASSERT(stopAtElement);
122
123    AffineTransform localTransform;
124    Node* current = element;
125
126    while (current && current->isSVGElement()) {
127        SVGElement* currentElement = toSVGElement(current);
128        localTransform = currentElement->renderer()->localToParentTransform();
129        transform = localTransform.multiply(transform);
130        // For getCTM() computation, stop at the nearest viewport element
131        if (currentElement == stopAtElement)
132            break;
133
134        current = current->parentOrShadowHostNode();
135    }
136}
137
138// FloatRect::intersects does not consider horizontal or vertical lines (because of isEmpty()).
139// So special-case handling of such lines.
140static bool intersectsAllowingEmpty(const FloatRect& r, const FloatRect& other)
141{
142    if (r.isEmpty() && other.isEmpty())
143        return false;
144    if (r.isEmpty() && !other.isEmpty()) {
145        return (other.contains(r.x(), r.y()) && !other.contains(r.maxX(), r.maxY()))
146               || (!other.contains(r.x(), r.y()) && other.contains(r.maxX(), r.maxY()));
147    }
148    if (other.isEmpty() && !r.isEmpty())
149        return intersectsAllowingEmpty(other, r);
150    return r.intersects(other);
151}
152
153// One of the element types that can cause graphics to be drawn onto the target canvas. Specifically: circle, ellipse,
154// image, line, path, polygon, polyline, rect, text and use.
155static bool isGraphicsElement(const RenderElement& renderer)
156{
157    return renderer.isSVGShape() || renderer.isSVGText() || renderer.isSVGImage() || renderer.element()->hasTagName(SVGNames::useTag);
158}
159
160// The SVG addFocusRingRects() method adds rects in local coordinates so the default absoluteFocusRingQuads
161// returns incorrect values for SVG objects. Overriding this method provides access to the absolute bounds.
162void RenderSVGModelObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
163{
164    quads.append(localToAbsoluteQuad(FloatQuad(repaintRectInLocalCoordinates())));
165}
166
167bool RenderSVGModelObject::checkIntersection(RenderElement* renderer, const FloatRect& rect)
168{
169    if (!renderer || renderer->style().pointerEvents() == PE_NONE)
170        return false;
171    if (!isGraphicsElement(*renderer))
172        return false;
173    AffineTransform ctm;
174    SVGElement* svgElement = toSVGElement(renderer->element());
175    getElementCTM(svgElement, ctm);
176    ASSERT(svgElement->renderer());
177    return intersectsAllowingEmpty(rect, ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
178}
179
180bool RenderSVGModelObject::checkEnclosure(RenderElement* renderer, const FloatRect& rect)
181{
182    if (!renderer || renderer->style().pointerEvents() == PE_NONE)
183        return false;
184    if (!isGraphicsElement(*renderer))
185        return false;
186    AffineTransform ctm;
187    SVGElement* svgElement = toSVGElement(renderer->element());
188    getElementCTM(svgElement, ctm);
189    ASSERT(svgElement->renderer());
190    return rect.contains(ctm.mapRect(svgElement->renderer()->repaintRectInLocalCoordinates()));
191}
192
193} // namespace WebCore
194