1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "SVGUseFrame.h"
8 
9 #include "mozilla/PresShell.h"
10 #include "mozilla/SVGObserverUtils.h"
11 #include "mozilla/SVGUtils.h"
12 #include "mozilla/dom/MutationEvent.h"
13 #include "mozilla/dom/SVGUseElement.h"
14 #include "nsLayoutUtils.h"
15 
16 using namespace mozilla::dom;
17 
18 //----------------------------------------------------------------------
19 // Implementation
20 
NS_NewSVGUseFrame(mozilla::PresShell * aPresShell,mozilla::ComputedStyle * aStyle)21 nsIFrame* NS_NewSVGUseFrame(mozilla::PresShell* aPresShell,
22                             mozilla::ComputedStyle* aStyle) {
23   return new (aPresShell)
24       mozilla::SVGUseFrame(aStyle, aPresShell->GetPresContext());
25 }
26 
27 namespace mozilla {
28 
NS_IMPL_FRAMEARENA_HELPERS(SVGUseFrame)29 NS_IMPL_FRAMEARENA_HELPERS(SVGUseFrame)
30 
31 //----------------------------------------------------------------------
32 // nsIFrame methods:
33 
34 void SVGUseFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
35                        nsIFrame* aPrevInFlow) {
36   NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::use),
37                "Content is not an SVG use!");
38 
39   mHasValidDimensions =
40       static_cast<SVGUseElement*>(aContent)->HasValidDimensions();
41 
42   SVGGFrame::Init(aContent, aParent, aPrevInFlow);
43 }
44 
AttributeChanged(int32_t aNamespaceID,nsAtom * aAttribute,int32_t aModType)45 nsresult SVGUseFrame::AttributeChanged(int32_t aNamespaceID, nsAtom* aAttribute,
46                                        int32_t aModType) {
47   // Currently our SMIL implementation does not modify the DOM attributes. Once
48   // we implement the SVG 2 SMIL behaviour this can be removed
49   // SVGUseElement::AfterSetAttr's implementation will be sufficient.
50   if (aModType == MutationEvent_Binding::SMIL) {
51     auto* content = SVGUseElement::FromNode(GetContent());
52     content->ProcessAttributeChange(aNamespaceID, aAttribute);
53   }
54 
55   return SVGGFrame::AttributeChanged(aNamespaceID, aAttribute, aModType);
56 }
57 
DidSetComputedStyle(ComputedStyle * aOldComputedStyle)58 void SVGUseFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
59   SVGGFrame::DidSetComputedStyle(aOldComputedStyle);
60 
61   if (!aOldComputedStyle) {
62     return;
63   }
64   const auto* newSVGReset = StyleSVGReset();
65   const auto* oldSVGReset = aOldComputedStyle->StyleSVGReset();
66 
67   if (newSVGReset->mX != oldSVGReset->mX ||
68       newSVGReset->mY != oldSVGReset->mY) {
69     // make sure our cached transform matrix gets (lazily) updated
70     mCanvasTM = nullptr;
71     SVGUtils::ScheduleReflowSVG(this);
72     SVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED);
73   }
74 }
75 
DimensionAttributeChanged(bool aHadValidDimensions,bool aAttributeIsUsed)76 void SVGUseFrame::DimensionAttributeChanged(bool aHadValidDimensions,
77                                             bool aAttributeIsUsed) {
78   bool invalidate = aAttributeIsUsed;
79   if (mHasValidDimensions != aHadValidDimensions) {
80     mHasValidDimensions = !mHasValidDimensions;
81     invalidate = true;
82   }
83 
84   if (invalidate) {
85     nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
86                                     nsChangeHint_InvalidateRenderingObservers);
87     SVGUtils::ScheduleReflowSVG(this);
88   }
89 }
90 
HrefChanged()91 void SVGUseFrame::HrefChanged() {
92   nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
93                                   nsChangeHint_InvalidateRenderingObservers);
94   SVGUtils::ScheduleReflowSVG(this);
95 }
96 
97 //----------------------------------------------------------------------
98 // ISVGDisplayableFrame methods
99 
ReflowSVG()100 void SVGUseFrame::ReflowSVG() {
101   // We only handle x/y offset here, since any width/height that is in force is
102   // handled by the SVGOuterSVGFrame for the anonymous <svg> that will be
103   // created for that purpose.
104   auto [x, y] = ResolvePosition();
105   mRect.MoveTo(nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x, y, 0, 0),
106                                                     AppUnitsPerCSSPixel())
107                    .TopLeft());
108 
109   // If we have a filter, we need to invalidate ourselves because filter
110   // output can change even if none of our descendants need repainting.
111   if (StyleEffects()->HasFilters()) {
112     InvalidateFrame();
113   }
114 
115   SVGGFrame::ReflowSVG();
116 }
117 
NotifySVGChanged(uint32_t aFlags)118 void SVGUseFrame::NotifySVGChanged(uint32_t aFlags) {
119   if (aFlags & COORD_CONTEXT_CHANGED && !(aFlags & TRANSFORM_CHANGED)) {
120     // Coordinate context changes affect mCanvasTM if we have a
121     // percentage 'x' or 'y'
122     if (StyleSVGReset()->mX.HasPercent() || StyleSVGReset()->mY.HasPercent()) {
123       aFlags |= TRANSFORM_CHANGED;
124       // Ancestor changes can't affect how we render from the perspective of
125       // any rendering observers that we may have, so we don't need to
126       // invalidate them. We also don't need to invalidate ourself, since our
127       // changed ancestor will have invalidated its entire area, which includes
128       // our area.
129       // For perf reasons we call this before calling NotifySVGChanged() below.
130       SVGUtils::ScheduleReflowSVG(this);
131     }
132   }
133 
134   // We don't remove the TRANSFORM_CHANGED flag here if we have a viewBox or
135   // non-percentage width/height, since if they're set then they are cloned to
136   // an anonymous child <svg>, and its SVGInnerSVGFrame will do that.
137 
138   SVGGFrame::NotifySVGChanged(aFlags);
139 }
140 
GetBBoxContribution(const Matrix & aToBBoxUserspace,uint32_t aFlags)141 SVGBBox SVGUseFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
142                                          uint32_t aFlags) {
143   SVGBBox bbox =
144       SVGDisplayContainerFrame::GetBBoxContribution(aToBBoxUserspace, aFlags);
145 
146   if (aFlags & SVGUtils::eForGetClientRects) {
147     auto [x, y] = ResolvePosition();
148     bbox.MoveBy(x, y);
149   }
150   return bbox;
151 }
152 
ResolvePosition() const153 std::pair<float, float> SVGUseFrame::ResolvePosition() const {
154   auto* content = SVGUseElement::FromNode(GetContent());
155   return std::make_pair(SVGContentUtils::CoordToFloat(
156                             content, StyleSVGReset()->mX, SVGContentUtils::X),
157                         SVGContentUtils::CoordToFloat(
158                             content, StyleSVGReset()->mY, SVGContentUtils::Y));
159 }
160 
161 }  // namespace mozilla
162