Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/svg/SVGGeometryElement.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_SVGGeometryElement_h
8
#define mozilla_dom_SVGGeometryElement_h
9
10
#include "mozilla/gfx/2D.h"
11
#include "SVGGraphicsElement.h"
12
#include "nsISVGPoint.h"
13
#include "nsSVGNumber2.h"
14
15
struct nsSVGMark {
16
  enum Type {
17
    eStart,
18
    eMid,
19
    eEnd,
20
21
    eTypeCount
22
  };
23
24
  float x, y, angle;
25
  Type type;
26
  nsSVGMark(float aX, float aY, float aAngle, Type aType) :
27
    x(aX), y(aY), angle(aAngle), type(aType) {}
28
};
29
30
namespace mozilla {
31
namespace dom {
32
33
class SVGAnimatedNumber;
34
35
typedef mozilla::dom::SVGGraphicsElement SVGGeometryElementBase;
36
37
class SVGGeometryElement : public SVGGeometryElementBase
38
{
39
protected:
40
  typedef mozilla::gfx::CapStyle CapStyle;
41
  typedef mozilla::gfx::DrawTarget DrawTarget;
42
  typedef mozilla::gfx::FillRule FillRule;
43
  typedef mozilla::gfx::Float Float;
44
  typedef mozilla::gfx::Matrix Matrix;
45
  typedef mozilla::gfx::Path Path;
46
  typedef mozilla::gfx::Point Point;
47
  typedef mozilla::gfx::PathBuilder PathBuilder;
48
  typedef mozilla::gfx::Rect Rect;
49
  typedef mozilla::gfx::StrokeOptions StrokeOptions;
50
51
public:
52
  explicit SVGGeometryElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
53
54
  virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
55
                                const nsAttrValue* aValue,
56
                                const nsAttrValue* aOldValue,
57
                                nsIPrincipal* aSubjectPrincipal,
58
                                bool aNotify) override;
59
  bool IsNodeOfType(uint32_t aFlags) const override;
60
61
  /**
62
   * Causes this element to discard any Path object that GetOrBuildPath may
63
   * have cached.
64
   */
65
0
  void ClearAnyCachedPath() final {
66
0
    mCachedPath = nullptr;
67
0
  }
68
69
  virtual bool AttributeDefinesGeometry(const nsAtom *aName);
70
71
  /**
72
   * Returns true if this element's geometry depends on the width or height of its
73
   * coordinate context (typically the viewport established by its nearest <svg>
74
   * ancestor). In other words, returns true if one of the attributes for which
75
   * AttributeDefinesGeometry returns true has a percentage value.
76
   *
77
   * This could be moved up to a more general class so it can be used for non-leaf
78
   * elements, but that would require care and for now there's no need.
79
   */
80
  bool GeometryDependsOnCoordCtx();
81
82
  virtual bool IsMarkable();
83
  virtual void GetMarkPoints(nsTArray<nsSVGMark> *aMarks);
84
85
  /**
86
   * A method that can be faster than using a Moz2D Path and calling GetBounds/
87
   * GetStrokedBounds on it.  It also helps us avoid rounding error for simple
88
   * shapes and simple transforms where the Moz2D Path backends can fail to
89
   * produce the clean integer bounds that content authors expect in some cases.
90
   *
91
   * If |aToNonScalingStrokeSpace| is non-null then |aBounds|, which is computed
92
   * in bounds space, has the property that it's the smallest (axis-aligned)
93
   * rectangular bound containing the image of this shape as stroked in
94
   * non-scaling-stroke space.  (When all transforms involved are rectilinear
95
   * the bounds of the image of |aBounds| in non-scaling-stroke space will be
96
   * tight, but if there are non-rectilinear transforms involved then that may
97
   * be impossible and this method will return false).
98
   *
99
   * If |aToNonScalingStrokeSpace| is non-null then |*aToNonScalingStrokeSpace|
100
   * must be non-singular.
101
   */
102
  virtual bool GetGeometryBounds(Rect* aBounds, const StrokeOptions& aStrokeOptions,
103
                                 const Matrix& aToBoundsSpace,
104
                                 const Matrix* aToNonScalingStrokeSpace = nullptr) {
105
    return false;
106
  }
107
108
  /**
109
   * For use with GetAsSimplePath.
110
   */
111
  class SimplePath
112
  {
113
  public:
114
    SimplePath()
115
      : mX(0.0)
116
      , mY(0.0)
117
      , mWidthOrX2(0.0)
118
      , mHeightOrY2(0.0)
119
      , mType(NONE)
120
0
    {}
121
0
    bool IsPath() const {
122
0
      return mType != NONE;
123
0
    }
124
    void SetRect(Float x, Float y, Float width, Float height) {
125
      mX = x;
126
      mY = y;
127
      mWidthOrX2 = width;
128
      mHeightOrY2 = height;
129
      mType = RECT;
130
    }
131
0
    Rect AsRect() const {
132
0
      MOZ_ASSERT(mType == RECT);
133
0
      return Rect(mX, mY, mWidthOrX2, mHeightOrY2);
134
0
    }
135
0
    bool IsRect() const {
136
0
      return mType == RECT;
137
0
    }
138
    void SetLine(Float x1, Float y1, Float x2, Float y2) {
139
      mX = x1;
140
      mY = y1;
141
      mWidthOrX2 = x2;
142
      mHeightOrY2 = y2;
143
      mType = LINE;
144
    }
145
0
    Point Point1() const {
146
0
      MOZ_ASSERT(mType == LINE);
147
0
      return Point(mX, mY);
148
0
    }
149
0
    Point Point2() const {
150
0
      MOZ_ASSERT(mType == LINE);
151
0
      return Point(mWidthOrX2, mHeightOrY2);
152
0
    }
153
0
    bool IsLine() const {
154
0
      return mType == LINE;
155
0
    }
156
0
    void Reset() {
157
0
      mType = NONE;
158
0
    }
159
  private:
160
    enum Type {
161
      NONE, RECT, LINE
162
    };
163
    Float mX, mY, mWidthOrX2, mHeightOrY2;
164
    Type mType;
165
  };
166
167
  /**
168
   * For some platforms there is significant overhead to creating and painting
169
   * a Moz2D Path object. For Rects and lines it is better to get the path data
170
   * using this method and then use the optimized DrawTarget methods for
171
   * filling/stroking rects and lines.
172
   */
173
0
  virtual void GetAsSimplePath(SimplePath* aSimplePath) {
174
0
    aSimplePath->Reset();
175
0
  }
176
177
  /**
178
   * Returns a Path that can be used to paint, hit-test or calculate bounds for
179
   * this element. May return nullptr if there is no [valid] path. The path
180
   * that is created may be cached and returned on subsequent calls.
181
   */
182
  virtual already_AddRefed<Path> GetOrBuildPath(const DrawTarget* aDrawTarget,
183
                                                FillRule fillRule);
184
185
  /**
186
   * The same as GetOrBuildPath, but bypasses the cache (neither returns any
187
   * previously cached Path, nor caches the Path that in does return).
188
   * this element. May return nullptr if there is no [valid] path.
189
   */
190
  virtual already_AddRefed<Path> BuildPath(PathBuilder* aBuilder) = 0;
191
192
  /**
193
   * Returns a Path that can be used to measure the length of this elements
194
   * path, or to find the position at a given distance along it.
195
   *
196
   * This is currently equivalent to calling GetOrBuildPath, but it may not be
197
   * in the future. The reason for this function to be separate from
198
   * GetOrBuildPath is because SVGPathData::BuildPath inserts small lines into
199
   * the path if zero length subpaths are encountered, in order to implement
200
   * the SVG specifications requirements that zero length subpaths should
201
   * render circles/squares if stroke-linecap is round/square, respectively.
202
   * In principle these inserted lines could interfere with path measurement,
203
   * so we keep callers that are looking to do measurement separate in case we
204
   * run into problems with the inserted lines negatively affecting measuring
205
   * for content.
206
   */
207
  virtual already_AddRefed<Path> GetOrBuildPathForMeasuring();
208
209
  /**
210
   * Returns the current computed value of the CSS property 'fill-rule' for
211
   * this element.
212
   */
213
  FillRule GetFillRule();
214
215
  enum PathLengthScaleForType {
216
    eForTextPath,
217
    eForStroking
218
  };
219
220
  /**
221
   * Gets the ratio of the actual element's length to the content author's
222
   * estimated length (as provided by the element's 'pathLength' attribute).
223
   * This is used to scale stroke dashing, and to scale offsets along a textPath.
224
   */
225
  float GetPathLengthScale(PathLengthScaleForType aFor);
226
227
  // WebIDL
228
  already_AddRefed<SVGAnimatedNumber> PathLength();
229
  float GetTotalLength();
230
  already_AddRefed<nsISVGPoint>
231
    GetPointAtLength(float distance, ErrorResult& rv);
232
233
protected:
234
  // nsSVGElement method
235
  virtual NumberAttributesInfo GetNumberInfo() override;
236
237
  nsSVGNumber2 mPathLength;
238
  static NumberInfo sNumberInfo;
239
  mutable RefPtr<Path> mCachedPath;
240
};
241
242
} // namespace dom
243
} // namespace mozilla
244
245
#endif // mozilla_dom_SVGGeometryElement_h