no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / layout / svg / SVGUseFrame.cpp
blobc655f7b24f833fb98da13feaf39a9b963302287c
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 //----------------------------------------------------------------------
19 // Implementation
21 nsIFrame* NS_NewSVGUseFrame(mozilla::PresShell* aPresShell,
22 mozilla::ComputedStyle* aStyle) {
23 return new (aPresShell)
24 mozilla::SVGUseFrame(aStyle, aPresShell->GetPresContext());
27 namespace mozilla {
29 NS_IMPL_FRAMEARENA_HELPERS(SVGUseFrame)
31 //----------------------------------------------------------------------
32 // nsIFrame methods:
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!");
39 mHasValidDimensions =
40 static_cast<SVGUseElement*>(aContent)->HasValidDimensions();
42 SVGGFrame::Init(aContent, aParent, aPrevInFlow);
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);
55 return SVGGFrame::AttributeChanged(aNamespaceID, aAttribute, aModType);
58 void SVGUseFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
59 SVGGFrame::DidSetComputedStyle(aOldComputedStyle);
61 if (!aOldComputedStyle) {
62 return;
64 const auto* newSVGReset = StyleSVGReset();
65 const auto* oldSVGReset = aOldComputedStyle->StyleSVGReset();
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);
76 void SVGUseFrame::DimensionAttributeChanged(bool aHadValidDimensions,
77 bool aAttributeIsUsed) {
78 bool invalidate = aAttributeIsUsed;
79 if (mHasValidDimensions != aHadValidDimensions) {
80 mHasValidDimensions = !mHasValidDimensions;
81 invalidate = true;
84 if (invalidate) {
85 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
86 nsChangeHint_InvalidateRenderingObservers);
87 SVGUtils::ScheduleReflowSVG(this);
91 void SVGUseFrame::HrefChanged() {
92 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
93 nsChangeHint_InvalidateRenderingObservers);
94 SVGUtils::ScheduleReflowSVG(this);
97 //----------------------------------------------------------------------
98 // ISVGDisplayableFrame methods
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* content = SVGUseElement::FromNode(GetContent());
105 float x = SVGContentUtils::CoordToFloat(content, StyleSVGReset()->mX,
106 SVGContentUtils::X);
107 float y = SVGContentUtils::CoordToFloat(content, StyleSVGReset()->mY,
108 SVGContentUtils::Y);
109 mRect.MoveTo(nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x, y, 0, 0),
110 AppUnitsPerCSSPixel())
111 .TopLeft());
113 // If we have a filter, we need to invalidate ourselves because filter
114 // output can change even if none of our descendants need repainting.
115 if (StyleEffects()->HasFilters()) {
116 InvalidateFrame();
119 SVGGFrame::ReflowSVG();
122 void SVGUseFrame::NotifySVGChanged(uint32_t aFlags) {
123 if (aFlags & COORD_CONTEXT_CHANGED && !(aFlags & TRANSFORM_CHANGED)) {
124 // Coordinate context changes affect mCanvasTM if we have a
125 // percentage 'x' or 'y'
126 if (StyleSVGReset()->mX.HasPercent() || StyleSVGReset()->mY.HasPercent()) {
127 aFlags |= TRANSFORM_CHANGED;
128 // Ancestor changes can't affect how we render from the perspective of
129 // any rendering observers that we may have, so we don't need to
130 // invalidate them. We also don't need to invalidate ourself, since our
131 // changed ancestor will have invalidated its entire area, which includes
132 // our area.
133 // For perf reasons we call this before calling NotifySVGChanged() below.
134 SVGUtils::ScheduleReflowSVG(this);
138 // We don't remove the TRANSFORM_CHANGED flag here if we have a viewBox or
139 // non-percentage width/height, since if they're set then they are cloned to
140 // an anonymous child <svg>, and its SVGInnerSVGFrame will do that.
142 SVGGFrame::NotifySVGChanged(aFlags);
145 } // namespace mozilla