Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGPathData.h
blob2e00112ff247894ce97896f19e85785ec2e05016
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_SVGPATHDATA_H_
8 #define DOM_SVG_SVGPATHDATA_H_
10 #include "nsCOMPtr.h"
11 #include "nsDebug.h"
12 #include "nsIContent.h"
13 #include "nsINode.h"
14 #include "nsIWeakReferenceUtils.h"
15 #include "mozilla/dom/SVGElement.h"
16 #include "mozilla/gfx/2D.h"
17 #include "mozilla/gfx/Types.h"
18 #include "mozilla/MemoryReporting.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/ServoStyleConsts.h"
21 #include "nsTArray.h"
23 #include <string.h>
25 namespace mozilla {
27 struct SVGMark;
28 enum class StyleStrokeLinecap : uint8_t;
30 class SVGPathData {
31 friend class SVGAnimatedPathSegList;
32 friend class SVGPathDataAndInfo;
33 friend class SVGPathSegListSMILType;
35 using DrawTarget = gfx::DrawTarget;
36 using Path = gfx::Path;
37 using PathBuilder = gfx::PathBuilder;
38 using FillRule = gfx::FillRule;
39 using Float = gfx::Float;
40 using CapStyle = gfx::CapStyle;
42 public:
43 SVGPathData() = default;
44 ~SVGPathData() = default;
46 explicit SVGPathData(const nsACString& aString) {
47 SetValueFromString(aString);
50 SVGPathData& operator=(const SVGPathData&) = default;
51 SVGPathData(const SVGPathData&) = default;
52 SVGPathData& operator=(SVGPathData&&) = default;
53 SVGPathData(SVGPathData&&) = default;
55 // Used by SMILCompositor to check if the cached base val is out of date
56 bool operator==(const SVGPathData& rhs) const { return mData == rhs.mData; }
58 // Only methods that don't make/permit modification to this list are public.
59 // Only our friend classes can access methods that may change us.
61 /// This may return an incomplete string on OOM, but that's acceptable.
62 void GetValueAsString(nsACString& aValue) const;
64 Span<const StylePathCommand> AsSpan() const { return mData._0.AsSpan(); }
65 bool IsEmpty() const { return AsSpan().IsEmpty(); }
67 const StyleSVGPathData& RawData() const { return mData; }
69 void GetMarkerPositioningData(float aZoom, nsTArray<SVGMark>* aMarks) const;
71 static void GetMarkerPositioningData(Span<const StylePathCommand> aPath,
72 float aZoom, nsTArray<SVGMark>* aMarks);
73 /**
74 * Returns true, except on OOM, in which case returns false.
76 bool GetDistancesFromOriginToEndsOfVisibleSegments(
77 FallibleTArray<double>* aOutput) const;
79 /**
80 * This is identical to the above one but accepts StylePathCommand.
82 static bool GetDistancesFromOriginToEndsOfVisibleSegments(
83 Span<const StylePathCommand> aPath, FallibleTArray<double>* aOutput);
85 /**
86 * This returns a path without the extra little line segments that
87 * ApproximateZeroLengthSubpathSquareCaps can insert if we have square-caps.
88 * See the comment for that function for more info on that.
90 already_AddRefed<Path> BuildPathForMeasuring(float aZoom) const;
92 already_AddRefed<Path> BuildPath(PathBuilder* aBuilder,
93 StyleStrokeLinecap aStrokeLineCap,
94 Float aStrokeWidth, float aZoom) const;
96 static already_AddRefed<Path> BuildPathForMeasuring(
97 Span<const StylePathCommand> aPath, float aZoom);
99 /**
100 * This function tries to build the path from an array of GenericShapeCommand,
101 * which is generated by cbindgen from Rust (see ServoStyleConsts.h).
102 * Basically, this is a variant of the above BuildPath() functions.
103 * Note: |StylePathCommand| doesn't accept percentage values, so its |aBasis|
104 * is empty by default.
106 static already_AddRefed<Path> BuildPath(Span<const StylePathCommand> aPath,
107 PathBuilder* aBuilder,
108 StyleStrokeLinecap aStrokeLineCap,
109 Float aStrokeWidth,
110 const CSSSize& aBasis = {},
111 const gfx::Point& aOffset = {},
112 float aZoomFactor = 1.0);
113 static already_AddRefed<Path> BuildPath(
114 Span<const StyleShapeCommand> aShape, PathBuilder* aBuilder,
115 StyleStrokeLinecap aStrokeLineCap, Float aStrokeWidth,
116 const CSSSize& aBasis, const gfx::Point& aOffset = gfx::Point(),
117 float aZoomFactor = 1.0);
119 // memory reporting methods
120 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
121 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
123 protected:
124 nsresult SetValueFromString(const nsACString& aValue);
126 void Clear() { mData = {}; }
128 StyleSVGPathData& RawData() { return mData; }
130 mozilla::StyleSVGPathData mData;
134 * This SVGPathData subclass is for SVGPathSegListSMILType which needs to
135 * have write access to the lists it works with.
137 * Instances of this class do not have DOM wrappers that need to be kept in
138 * sync, so we can safely expose any protected base class methods required by
139 * the SMIL code.
141 class SVGPathDataAndInfo final : public SVGPathData {
142 public:
143 explicit SVGPathDataAndInfo(dom::SVGElement* aElement = nullptr)
144 : mElement(do_GetWeakReference(static_cast<nsINode*>(aElement))) {}
146 void SetElement(dom::SVGElement* aElement) {
147 mElement = do_GetWeakReference(static_cast<nsINode*>(aElement));
150 dom::SVGElement* Element() const {
151 nsCOMPtr<nsIContent> e = do_QueryReferent(mElement);
152 return static_cast<dom::SVGElement*>(e.get());
155 // If you use this, you need to call SetElement manually.
156 void CopyFrom(const SVGPathData& aOther) { mData = aOther.mData; }
157 void CopyFrom(const SVGPathDataAndInfo& aOther) {
158 CopyFrom(static_cast<const SVGPathData&>(aOther));
159 mElement = aOther.mElement;
163 * Returns true if this object is an "identity" value, from the perspective
164 * of SMIL. In other words, returns true until the initial value set up in
165 * SVGPathSegListSMILType::Init() has been changed with a SetElement() call.
167 bool IsIdentity() const {
168 if (!mElement) {
169 MOZ_ASSERT(IsEmpty(), "target element propagation failure");
170 return true;
172 return false;
175 private:
176 // We must keep a weak reference to our element because we may belong to a
177 // cached baseVal SMILValue. See the comments starting at:
178 // https://bugzilla.mozilla.org/show_bug.cgi?id=515116#c15
179 // See also https://bugzilla.mozilla.org/show_bug.cgi?id=653497
180 nsWeakPtr mElement;
183 } // namespace mozilla
185 #endif // DOM_SVG_SVGPATHDATA_H_