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/. */
7 #include "SVGUseFrame.h"
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"
16 using namespace mozilla::dom
;
18 //----------------------------------------------------------------------
21 nsIFrame
* NS_NewSVGUseFrame(mozilla::PresShell
* aPresShell
,
22 mozilla::ComputedStyle
* aStyle
) {
23 return new (aPresShell
)
24 mozilla::SVGUseFrame(aStyle
, aPresShell
->GetPresContext());
29 NS_IMPL_FRAMEARENA_HELPERS(SVGUseFrame
)
31 //----------------------------------------------------------------------
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!");
40 static_cast<SVGUseElement
*>(aContent
)->HasValidDimensions();
42 SVGGFrame::Init(aContent
, aParent
, aPrevInFlow
);
45 void SVGUseFrame::DidSetComputedStyle(ComputedStyle
* aOldComputedStyle
) {
46 SVGGFrame::DidSetComputedStyle(aOldComputedStyle
);
48 if (!aOldComputedStyle
) {
51 const auto* newSVGReset
= StyleSVGReset();
52 const auto* oldSVGReset
= aOldComputedStyle
->StyleSVGReset();
54 if (newSVGReset
->mX
!= oldSVGReset
->mX
||
55 newSVGReset
->mY
!= oldSVGReset
->mY
) {
56 // make sure our cached transform matrix gets (lazily) updated
58 SVGUtils::ScheduleReflowSVG(this);
59 SVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED
);
63 void SVGUseFrame::DimensionAttributeChanged(bool aHadValidDimensions
,
64 bool aAttributeIsUsed
) {
65 bool invalidate
= aAttributeIsUsed
;
66 if (mHasValidDimensions
!= aHadValidDimensions
) {
67 mHasValidDimensions
= !mHasValidDimensions
;
72 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint
{0},
73 nsChangeHint_InvalidateRenderingObservers
);
74 SVGUtils::ScheduleReflowSVG(this);
78 void SVGUseFrame::HrefChanged() {
79 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint
{0},
80 nsChangeHint_InvalidateRenderingObservers
);
81 SVGUtils::ScheduleReflowSVG(this);
84 //----------------------------------------------------------------------
85 // ISVGDisplayableFrame methods
87 void SVGUseFrame::ReflowSVG() {
88 // We only handle x/y offset here, since any width/height that is in force is
89 // handled by the SVGOuterSVGFrame for the anonymous <svg> that will be
90 // created for that purpose.
91 auto* content
= SVGUseElement::FromNode(GetContent());
92 float x
= SVGContentUtils::CoordToFloat(content
, StyleSVGReset()->mX
,
94 float y
= SVGContentUtils::CoordToFloat(content
, StyleSVGReset()->mY
,
96 mRect
.MoveTo(nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x
, y
, 0, 0),
97 AppUnitsPerCSSPixel())
100 // If we have a filter, we need to invalidate ourselves because filter
101 // output can change even if none of our descendants need repainting.
102 if (StyleEffects()->HasFilters()) {
106 SVGGFrame::ReflowSVG();
109 void SVGUseFrame::NotifySVGChanged(uint32_t aFlags
) {
110 if (aFlags
& COORD_CONTEXT_CHANGED
&& !(aFlags
& TRANSFORM_CHANGED
)) {
111 // Coordinate context changes affect mCanvasTM if we have a
112 // percentage 'x' or 'y'
113 if (StyleSVGReset()->mX
.HasPercent() || StyleSVGReset()->mY
.HasPercent()) {
114 aFlags
|= TRANSFORM_CHANGED
;
115 // Ancestor changes can't affect how we render from the perspective of
116 // any rendering observers that we may have, so we don't need to
117 // invalidate them. We also don't need to invalidate ourself, since our
118 // changed ancestor will have invalidated its entire area, which includes
120 // For perf reasons we call this before calling NotifySVGChanged() below.
121 SVGUtils::ScheduleReflowSVG(this);
125 // We don't remove the TRANSFORM_CHANGED flag here if we have a viewBox or
126 // non-percentage width/height, since if they're set then they are cloned to
127 // an anonymous child <svg>, and its SVGInnerSVGFrame will do that.
129 SVGGFrame::NotifySVGChanged(aFlags
);
132 } // namespace mozilla