Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/svg/SVGViewportElement.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef mozilla_dom_SVGViewportElement_h
8
#define mozilla_dom_SVGViewportElement_h
9
10
#include "mozilla/dom/FromParser.h"
11
#include "nsAutoPtr.h"
12
#include "nsIContentInlines.h"
13
#include "nsISVGPoint.h"
14
#include "nsSVGEnum.h"
15
#include "nsSVGLength2.h"
16
#include "SVGGraphicsElement.h"
17
#include "SVGImageContext.h"
18
#include "nsSVGViewBox.h"
19
#include "SVGPreserveAspectRatio.h"
20
#include "SVGAnimatedPreserveAspectRatio.h"
21
#include "mozilla/Attributes.h"
22
23
class nsSVGOuterSVGFrame;
24
class nsSVGViewportFrame;
25
26
namespace mozilla {
27
class AutoPreserveAspectRatioOverride;
28
class DOMSVGAnimatedPreserveAspectRatio;
29
30
namespace dom {
31
class SVGAnimatedRect;
32
class SVGTransform;
33
class SVGViewElement;
34
class SVGViewportElement;
35
36
class svgFloatSize {
37
public:
38
  svgFloatSize(float aWidth, float aHeight)
39
    : width(aWidth)
40
    , height(aHeight)
41
0
  {}
42
  bool operator!=(const svgFloatSize& rhs) {
43
    return width != rhs.width || height != rhs.height;
44
  }
45
  float width;
46
  float height;
47
};
48
49
class SVGViewportElement : public SVGGraphicsElement
50
{
51
  friend class ::nsSVGOuterSVGFrame;
52
  friend class ::nsSVGViewportFrame;
53
54
protected:
55
56
  SVGViewportElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
57
  ~SVGViewportElement();
58
59
public:
60
61
  // nsIContent interface
62
  NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
63
64
  // nsSVGElement specializations:
65
  virtual gfxMatrix PrependLocalTransformsTo(
66
    const gfxMatrix &aMatrix,
67
    SVGTransformTypes aWhich = eAllTransforms) const override;
68
69
  virtual bool HasValidDimensions() const override;
70
71
  // SVGViewportElement methods:
72
73
  float GetLength(uint8_t mCtxType);
74
75
  // public helpers:
76
77
  /**
78
   * Returns true if this element has a base/anim value for its "viewBox"
79
   * attribute that defines a viewBox rectangle with finite values, or
80
   * if there is a view element overriding this element's viewBox and it
81
   * has a valid viewBox.
82
   *
83
   * Note that this does not check whether we need to synthesize a viewBox,
84
   * so you must call ShouldSynthesizeViewBox() if you need to chck that too.
85
   *
86
   * Note also that this method does not pay attention to whether the width or
87
   * height values of the viewBox rect are positive!
88
   */
89
  bool HasViewBoxRect() const {
90
    return GetViewBoxInternal().HasRect();
91
  }
92
93
  /**
94
   * Returns true if we should synthesize a viewBox for ourselves (that is, if
95
   * we're the root element in an image document, and we're not currently being
96
   * painted for an <svg:image> element).
97
   *
98
   * Only call this method if HasViewBoxRect() returns false.
99
   */
100
  bool ShouldSynthesizeViewBox() const;
101
102
  bool HasViewBoxOrSyntheticViewBox() const {
103
    return HasViewBoxRect() || ShouldSynthesizeViewBox();
104
  }
105
106
  bool HasChildrenOnlyTransform() const {
107
    return mHasChildrenOnlyTransform;
108
  }
109
110
  void UpdateHasChildrenOnlyTransform();
111
112
  enum ChildrenOnlyTransformChangedFlags {
113
    eDuringReflow = 1
114
  };
115
116
  /**
117
   * This method notifies the style system that the overflow rects of our
118
   * immediate childrens' frames need to be updated. It is called by our own
119
   * frame when changes (e.g. to currentScale) cause our children-only
120
   * transform to change.
121
   *
122
   * The reason we have this method instead of overriding
123
   * GetAttributeChangeHint is because we need to act on non-attribute (e.g.
124
   * currentScale) changes in addition to attribute (e.g. viewBox) changes.
125
   */
126
  void ChildrenOnlyTransformChanged(uint32_t aFlags = 0);
127
128
  gfx::Matrix GetViewBoxTransform() const;
129
130
0
  svgFloatSize GetViewportSize() const {
131
0
    return svgFloatSize(mViewportWidth, mViewportHeight);
132
0
  }
133
134
  void SetViewportSize(const svgFloatSize& aSize) {
135
    mViewportWidth  = aSize.width;
136
    mViewportHeight = aSize.height;
137
  }
138
139
  // WebIDL
140
  already_AddRefed<SVGAnimatedRect> ViewBox();
141
  already_AddRefed<DOMSVGAnimatedPreserveAspectRatio> PreserveAspectRatio();
142
  virtual nsSVGViewBox* GetViewBox() override;
143
144
protected:
145
146
  // implementation helpers:
147
148
  bool IsRoot() const {
149
    NS_ASSERTION((IsInUncomposedDoc() && !GetParent()) ==
150
                 (OwnerDoc()->GetRootElement() == this),
151
                 "Can't determine if we're root");
152
    return IsInUncomposedDoc() && !GetParent();
153
  }
154
155
  /**
156
   * Returns true if either this is an SVG <svg> element that is the child of
157
   * another non-foreignObject SVG element, or this is a SVG <symbol> element
158
   * this is the root of a use-element shadow tree.
159
   */
160
  bool IsInner() const {
161
    const nsIContent *parent = GetFlattenedTreeParent();
162
    return parent && parent->IsSVGElement() &&
163
           !parent->IsSVGElement(nsGkAtoms::foreignObject);
164
  }
165
166
  /**
167
   * Returns the explicit or default preserveAspectRatio, unless we're
168
   * synthesizing a viewBox, in which case it returns the "none" value.
169
   */
170
  virtual SVGPreserveAspectRatio GetPreserveAspectRatioWithOverride() const {
171
    return mPreserveAspectRatio.GetAnimValue();
172
  }
173
174
  /**
175
   * Returns the explicit viewBox rect, if specified, or else a synthesized
176
   * viewBox, if appropriate, or else a viewBox matching the dimensions of the
177
   * SVG viewport.
178
   */
179
  nsSVGViewBoxRect GetViewBoxWithSynthesis(
180
      float aViewportWidth, float aViewportHeight) const;
181
182
  /**
183
   * Retrieve the value of currentScale and currentTranslate.
184
   */
185
  virtual SVGPoint GetCurrentTranslate() const
186
  { return SVGPoint(0.0f, 0.0f); }
187
  virtual float GetCurrentScale() const
188
  { return 1.0f; }
189
190
  enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
191
  nsSVGLength2 mLengthAttributes[4];
192
  static LengthInfo sLengthInfo[4];
193
  virtual LengthAttributesInfo GetLengthInfo() override;
194
195
  virtual SVGAnimatedPreserveAspectRatio *GetPreserveAspectRatio() override;
196
197
  virtual const nsSVGViewBox& GetViewBoxInternal() const { return mViewBox; }
198
  virtual nsSVGAnimatedTransformList* GetTransformInternal() const {
199
    return mTransforms;
200
  }
201
  nsSVGViewBox                   mViewBox;
202
  SVGAnimatedPreserveAspectRatio mPreserveAspectRatio;
203
204
  // The size of the rectangular SVG viewport into which we render. This is
205
  // not (necessarily) the same as the content area. See:
206
  //
207
  //   http://www.w3.org/TR/SVG11/coords.html#ViewportSpace
208
  //
209
  // XXXjwatt Currently only used for outer <svg>, but maybe we could use -1 to
210
  // flag this as an inner <svg> to save the overhead of GetCtx calls?
211
  // XXXjwatt our frame should probably reset these when it's destroyed.
212
  float mViewportWidth, mViewportHeight;
213
214
  bool     mHasChildrenOnlyTransform;
215
};
216
217
} // namespace dom
218
219
} // namespace mozilla
220
221
#endif // SVGViewportElement_h