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 #ifndef DOM_SVG_SVGGEOMETRYELEMENT_H_
8 #define DOM_SVG_SVGGEOMETRYELEMENT_H_
10 #include "mozilla/dom/SVGGraphicsElement.h"
11 #include "mozilla/gfx/2D.h"
12 #include "SVGAnimatedNumber.h"
27 SVGMark(float aX
, float aY
, float aAngle
, Type aType
)
28 : x(aX
), y(aY
), angle(aAngle
), type(aType
) {}
33 class DOMSVGAnimatedNumber
;
36 using SVGGeometryElementBase
= mozilla::dom::SVGGraphicsElement
;
38 class SVGGeometryElement
: public SVGGeometryElementBase
{
40 using CapStyle
= mozilla::gfx::CapStyle
;
41 using DrawTarget
= mozilla::gfx::DrawTarget
;
42 using FillRule
= mozilla::gfx::FillRule
;
43 using Float
= mozilla::gfx::Float
;
44 using Matrix
= mozilla::gfx::Matrix
;
45 using Path
= mozilla::gfx::Path
;
46 using Point
= mozilla::gfx::Point
;
47 using PathBuilder
= mozilla::gfx::PathBuilder
;
48 using Rect
= mozilla::gfx::Rect
;
49 using StrokeOptions
= mozilla::gfx::StrokeOptions
;
52 explicit SVGGeometryElement(
53 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
55 virtual nsresult
AfterSetAttr(int32_t aNamespaceID
, nsAtom
* aName
,
56 const nsAttrValue
* aValue
,
57 const nsAttrValue
* aOldValue
,
58 nsIPrincipal
* aSubjectPrincipal
,
59 bool aNotify
) override
;
60 bool IsNodeOfType(uint32_t aFlags
) const override
;
63 * Causes this element to discard any Path object that GetOrBuildPath may
66 void ClearAnyCachedPath() final
{ mCachedPath
= nullptr; }
68 virtual bool AttributeDefinesGeometry(const nsAtom
* aName
);
71 * Returns true if this element's geometry depends on the width or height of
72 * its coordinate context (typically the viewport established by its nearest
73 * <svg> ancestor). In other words, returns true if one of the attributes for
74 * which AttributeDefinesGeometry returns true has a percentage value.
76 * This could be moved up to a more general class so it can be used for
77 * non-leaf elements, but that would require care and for now there's no need.
79 bool GeometryDependsOnCoordCtx();
81 virtual bool IsMarkable();
82 virtual void GetMarkPoints(nsTArray
<SVGMark
>* aMarks
);
85 * A method that can be faster than using a Moz2D Path and calling GetBounds/
86 * GetStrokedBounds on it. It also helps us avoid rounding error for simple
87 * shapes and simple transforms where the Moz2D Path backends can fail to
88 * produce the clean integer bounds that content authors expect in some cases.
90 * If |aToNonScalingStrokeSpace| is non-null then |aBounds|, which is computed
91 * in bounds space, has the property that it's the smallest (axis-aligned)
92 * rectangular bound containing the image of this shape as stroked in
93 * non-scaling-stroke space. (When all transforms involved are rectilinear
94 * the bounds of the image of |aBounds| in non-scaling-stroke space will be
95 * tight, but if there are non-rectilinear transforms involved then that may
96 * be impossible and this method will return false).
98 * If |aToNonScalingStrokeSpace| is non-null then |*aToNonScalingStrokeSpace|
99 * must be non-singular.
101 virtual bool GetGeometryBounds(
102 Rect
* aBounds
, const StrokeOptions
& aStrokeOptions
,
103 const Matrix
& aToBoundsSpace
,
104 const Matrix
* aToNonScalingStrokeSpace
= nullptr) {
109 * For use with GetAsSimplePath.
114 : mX(0.0), mY(0.0), mWidthOrX2(0.0), mHeightOrY2(0.0), mType(NONE
) {}
115 bool IsPath() const { return mType
!= NONE
; }
116 void SetRect(Float x
, Float y
, Float width
, Float height
) {
120 mHeightOrY2
= height
;
123 Rect
AsRect() const {
124 MOZ_ASSERT(mType
== RECT
);
125 return Rect(mX
, mY
, mWidthOrX2
, mHeightOrY2
);
127 bool IsRect() const { return mType
== RECT
; }
128 void SetLine(Float x1
, Float y1
, Float x2
, Float y2
) {
135 Point
Point1() const {
136 MOZ_ASSERT(mType
== LINE
);
137 return Point(mX
, mY
);
139 Point
Point2() const {
140 MOZ_ASSERT(mType
== LINE
);
141 return Point(mWidthOrX2
, mHeightOrY2
);
143 bool IsLine() const { return mType
== LINE
; }
144 void Reset() { mType
= NONE
; }
147 enum Type
{ NONE
, RECT
, LINE
};
148 Float mX
, mY
, mWidthOrX2
, mHeightOrY2
;
153 * For some platforms there is significant overhead to creating and painting
154 * a Moz2D Path object. For Rects and lines it is better to get the path data
155 * using this method and then use the optimized DrawTarget methods for
156 * filling/stroking rects and lines.
158 virtual void GetAsSimplePath(SimplePath
* aSimplePath
) {
159 aSimplePath
->Reset();
163 * Returns a Path that can be used to paint, hit-test or calculate bounds for
164 * this element. May return nullptr if there is no [valid] path. The path
165 * that is created may be cached and returned on subsequent calls.
167 virtual already_AddRefed
<Path
> GetOrBuildPath(const DrawTarget
* aDrawTarget
,
171 * The same as GetOrBuildPath, but bypasses the cache (neither returns any
172 * previously cached Path, nor caches the Path that in does return).
173 * this element. May return nullptr if there is no [valid] path.
175 virtual already_AddRefed
<Path
> BuildPath(PathBuilder
* aBuilder
) = 0;
178 * Get the distances from the origin of the path segments.
179 * For non-path elements that's just 0 and the total length of the shape.
181 virtual bool GetDistancesFromOriginToEndsOfVisibleSegments(
182 FallibleTArray
<double>* aOutput
) {
184 double distances
[] = {0.0, GetTotalLength()};
185 return aOutput
->AppendElements(Span
<double>(distances
), fallible
);
189 * Returns a Path that can be used to measure the length of this elements
190 * path, or to find the position at a given distance along it.
192 * This is currently equivalent to calling GetOrBuildPath, but it may not be
193 * in the future. The reason for this function to be separate from
194 * GetOrBuildPath is because SVGPathData::BuildPath inserts small lines into
195 * the path if zero length subpaths are encountered, in order to implement
196 * the SVG specifications requirements that zero length subpaths should
197 * render circles/squares if stroke-linecap is round/square, respectively.
198 * In principle these inserted lines could interfere with path measurement,
199 * so we keep callers that are looking to do measurement separate in case we
200 * run into problems with the inserted lines negatively affecting measuring
203 virtual already_AddRefed
<Path
> GetOrBuildPathForMeasuring();
206 * Return |true| if some geometry properties (|x|, |y|, etc) are changed
207 * because of CSS change.
209 bool IsGeometryChangedViaCSS(ComputedStyle
const& aNewStyle
,
210 ComputedStyle
const& aOldStyle
) const;
213 * Returns the current computed value of the CSS property 'fill-rule' for
216 FillRule
GetFillRule();
218 enum PathLengthScaleForType
{ eForTextPath
, eForStroking
};
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
226 float GetPathLengthScale(PathLengthScaleForType aFor
);
229 already_AddRefed
<DOMSVGAnimatedNumber
> PathLength();
230 bool IsPointInFill(const DOMPointInit
& aPoint
);
231 bool IsPointInStroke(const DOMPointInit
& aPoint
);
232 float GetTotalLength();
233 already_AddRefed
<DOMSVGPoint
> GetPointAtLength(float distance
,
238 virtual NumberAttributesInfo
GetNumberInfo() override
;
240 SVGAnimatedNumber mPathLength
;
241 static NumberInfo sNumberInfo
;
242 mutable RefPtr
<Path
> mCachedPath
;
245 already_AddRefed
<Path
> GetOrBuildPathForHitTest();
249 } // namespace mozilla
251 #endif // DOM_SVG_SVGGEOMETRYELEMENT_H_