File: SVGPathElement.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (361 lines) | stat: -rw-r--r-- 13,060 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/SVGPathElement.h"

#include <algorithm>

#include "SVGArcConverter.h"
#include "SVGGeometryProperty.h"
#include "SVGPathSegUtils.h"
#include "gfx2DGlue.h"
#include "gfxPlatform.h"
#include "mozAutoDocUpdate.h"
#include "mozilla/RefPtr.h"
#include "mozilla/SVGContentUtils.h"
#include "mozilla/dom/SVGPathElementBinding.h"
#include "mozilla/dom/SVGPathSegment.h"
#include "mozilla/gfx/2D.h"
#include "nsGkAtoms.h"
#include "nsIFrame.h"
#include "nsStyleConsts.h"
#include "nsStyleStruct.h"
#include "nsWindowSizes.h"

NS_IMPL_NS_NEW_SVG_ELEMENT(Path)

using namespace mozilla::gfx;

namespace mozilla::dom {

//----------------------------------------------------------------------
// Helper class: AutoChangePathSegListNotifier
// Stack-based helper class to pair calls to WillChangePathSegList and
// DidChangePathSegList.
class MOZ_RAII AutoChangePathSegListNotifier : public mozAutoDocUpdate {
 public:
  explicit AutoChangePathSegListNotifier(SVGPathElement* aSVGPathElement)
      : mozAutoDocUpdate(aSVGPathElement->GetComposedDoc(), true),
        mSVGElement(aSVGPathElement) {
    MOZ_ASSERT(mSVGElement, "Expecting non-null value");
    mEmptyOrOldValue = mSVGElement->WillChangePathSegList(*this);
  }

  ~AutoChangePathSegListNotifier() {
    mSVGElement->DidChangePathSegList(mEmptyOrOldValue, *this);
    if (mSVGElement->GetAnimPathSegList()->IsAnimating()) {
      mSVGElement->AnimationNeedsResample();
    }
  }

 private:
  SVGPathElement* const mSVGElement;
  nsAttrValue mEmptyOrOldValue;
};

JSObject* SVGPathElement::WrapNode(JSContext* aCx,
                                   JS::Handle<JSObject*> aGivenProto) {
  return SVGPathElement_Binding::Wrap(aCx, this, aGivenProto);
}

//----------------------------------------------------------------------
// Implementation

SVGPathElement::SVGPathElement(
    already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
    : SVGPathElementBase(std::move(aNodeInfo)) {}

//----------------------------------------------------------------------
// memory reporting methods

void SVGPathElement::AddSizeOfExcludingThis(nsWindowSizes& aSizes,
                                            size_t* aNodeSize) const {
  SVGPathElementBase::AddSizeOfExcludingThis(aSizes, aNodeSize);
  *aNodeSize += mD.SizeOfExcludingThis(aSizes.mState.mMallocSizeOf);
}

//----------------------------------------------------------------------
// nsINode methods

NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGPathElement)

already_AddRefed<SVGPathSegment> SVGPathElement::GetPathSegmentAtLength(
    float aDistance) {
  FlushIfNeeded();
  RefPtr<SVGPathSegment> segment;
  if (SVGGeometryProperty::DoForComputedStyle(
          this, [&](const ComputedStyle* s) {
            const auto& d = s->StyleSVGReset()->mD;
            if (d.IsPath()) {
              segment = SVGPathData::GetPathSegmentAtLength(
                  this, d.AsPath()._0.AsSpan(), aDistance);
            }
          })) {
    return segment.forget();
  }
  return SVGPathData::GetPathSegmentAtLength(this, mD.GetAnimValue().AsSpan(),
                                             aDistance);
}

static void CreatePathSegments(SVGPathElement* aPathElement,
                               const StyleSVGPathData& aPathData,
                               nsTArray<RefPtr<SVGPathSegment>>& aValues,
                               bool aNormalize) {
  if (aNormalize) {
    StyleSVGPathData normalizedPathData;
    Servo_SVGPathData_NormalizeAndReduce(&aPathData, &normalizedPathData);
    Point pathStart(0.0, 0.0);
    Point segStart(0.0, 0.0);
    Point segEnd(0.0, 0.0);
    for (const auto& cmd : normalizedPathData._0.AsSpan()) {
      switch (cmd.tag) {
        case StylePathCommand::Tag::Close:
          segEnd = pathStart;
          aValues.AppendElement(new SVGPathSegment(aPathElement, cmd));
          break;
        case StylePathCommand::Tag::Move:
          pathStart = segEnd = cmd.move.point.ToGfxPoint();
          aValues.AppendElement(new SVGPathSegment(aPathElement, cmd));
          break;
        case StylePathCommand::Tag::Line:
          segEnd = cmd.line.point.ToGfxPoint();
          aValues.AppendElement(new SVGPathSegment(aPathElement, cmd));
          break;
        case StylePathCommand::Tag::CubicCurve:
          segEnd = cmd.cubic_curve.point.ToGfxPoint();
          aValues.AppendElement(new SVGPathSegment(aPathElement, cmd));
          break;
        case StylePathCommand::Tag::Arc: {
          const auto& arc = cmd.arc;
          segEnd = arc.point.ToGfxPoint();
          SVGArcConverter converter(segStart, arc.point.ToGfxPoint(),
                                    arc.radii.ToGfxPoint(), arc.rotate,
                                    arc.arc_size == StyleArcSize::Large,
                                    arc.arc_sweep == StyleArcSweep::Cw);
          Point cp1, cp2;
          while (converter.GetNextSegment(&cp1, &cp2, &segEnd)) {
            auto curve = StylePathCommand::CubicCurve(
                StyleByTo::To,
                StyleCoordinatePair<StyleCSSFloat>{segEnd.x, segEnd.y},
                StyleCoordinatePair<StyleCSSFloat>{cp1.x, cp1.y},
                StyleCoordinatePair<StyleCSSFloat>{cp2.x, cp2.y});
            aValues.AppendElement(new SVGPathSegment(aPathElement, curve));
          }
          break;
        }
        default:
          MOZ_ASSERT_UNREACHABLE("Unexpected path command");
          break;
      }
      segStart = segEnd;
    }
    return;
  }
  for (const auto& cmd : aPathData._0.AsSpan()) {
    aValues.AppendElement(new SVGPathSegment(aPathElement, cmd));
  }
}

void SVGPathElement::GetPathData(const SVGPathDataSettings& aOptions,
                                 nsTArray<RefPtr<SVGPathSegment>>& aValues) {
  FlushIfNeeded();
  if (SVGGeometryProperty::DoForComputedStyle(
          this, [&](const ComputedStyle* s) {
            const auto& d = s->StyleSVGReset()->mD;
            if (d.IsPath()) {
              CreatePathSegments(this, d.AsPath(), aValues,
                                 aOptions.mNormalize);
            }
          })) {
    return;
  }
  CreatePathSegments(this, mD.GetAnimValue().RawData(), aValues,
                     aOptions.mNormalize);
}

void SVGPathElement::SetPathData(const Sequence<SVGPathSegmentInit>& aValues) {
  AutoChangePathSegListNotifier notifier(this);
  mD.SetBaseValueFromPathSegments(aValues);
}

//----------------------------------------------------------------------
// SVGElement methods

/* virtual */
bool SVGPathElement::HasValidDimensions() const {
  bool hasPath = false;
  auto callback = [&](const ComputedStyle* s) {
    const nsStyleSVGReset* styleSVGReset = s->StyleSVGReset();
    hasPath =
        styleSVGReset->mD.IsPath() && !styleSVGReset->mD.AsPath()._0.IsEmpty();
  };

  SVGGeometryProperty::DoForComputedStyle(this, callback);
  // If hasPath is false, we may disable the pref of d property, so we fallback
  // to check mD.
  return hasPath || !mD.GetAnimValue().IsEmpty();
}

//----------------------------------------------------------------------
// nsIContent methods

NS_IMETHODIMP_(bool)
SVGPathElement::IsAttributeMapped(const nsAtom* name) const {
  return name == nsGkAtoms::d || SVGPathElementBase::IsAttributeMapped(name);
}

already_AddRefed<Path> SVGPathElement::GetOrBuildPathForMeasuring() {
  RefPtr<Path> path;
  bool success = SVGGeometryProperty::DoForComputedStyle(
      this, [&path](const ComputedStyle* s) {
        const auto& d = s->StyleSVGReset()->mD;
        if (d.IsNone()) {
          return;
        }
        path = SVGPathData::BuildPathForMeasuring(d.AsPath()._0.AsSpan(),
                                                  s->EffectiveZoom().ToFloat());
      });
  return success ? path.forget()
                 : mD.GetAnimValue().BuildPathForMeasuring(1.0f);
}

//----------------------------------------------------------------------
// SVGGeometryElement methods

bool SVGPathElement::AttributeDefinesGeometry(const nsAtom* aName) {
  return aName == nsGkAtoms::d || aName == nsGkAtoms::pathLength;
}

bool SVGPathElement::IsMarkable() { return true; }

void SVGPathElement::GetMarkPoints(nsTArray<SVGMark>* aMarks) {
  auto callback = [aMarks](const ComputedStyle* s) {
    const nsStyleSVGReset* styleSVGReset = s->StyleSVGReset();
    if (styleSVGReset->mD.IsPath()) {
      Span<const StylePathCommand> path =
          styleSVGReset->mD.AsPath()._0.AsSpan();
      SVGPathData::GetMarkerPositioningData(path, s->EffectiveZoom().ToFloat(),
                                            aMarks);
    }
  };

  if (SVGGeometryProperty::DoForComputedStyle(this, callback)) {
    return;
  }

  mD.GetAnimValue().GetMarkerPositioningData(1.0f, aMarks);
}

void SVGPathElement::GetAsSimplePath(SimplePath* aSimplePath) {
  aSimplePath->Reset();
  auto callback = [&](const ComputedStyle* s) {
    const nsStyleSVGReset* styleSVGReset = s->StyleSVGReset();
    if (styleSVGReset->mD.IsPath()) {
      auto pathData = styleSVGReset->mD.AsPath()._0.AsSpan();
      auto maybeRect = SVGPathToAxisAlignedRect(pathData);
      if (maybeRect.isSome()) {
        const Rect& r = *maybeRect;
        float zoom = s->EffectiveZoom().ToFloat();
        aSimplePath->SetRect(r.x * zoom, r.y * zoom, r.width * zoom,
                             r.height * zoom);
      }
    }
  };

  SVGGeometryProperty::DoForComputedStyle(this, callback);
}

already_AddRefed<Path> SVGPathElement::BuildPath(PathBuilder* aBuilder) {
  // The Moz2D PathBuilder that our SVGPathData will be using only cares about
  // the fill rule. However, in order to fulfill the requirements of the SVG
  // spec regarding zero length sub-paths when square line caps are in use,
  // SVGPathData needs to know our stroke-linecap style and, if "square", then
  // also our stroke width. See the comment for
  // ApproximateZeroLengthSubpathSquareCaps for more info.

  auto strokeLineCap = StyleStrokeLinecap::Butt;
  Float strokeWidth = 0;
  RefPtr<Path> path;

  auto callback = [&](const ComputedStyle* s) {
    const nsStyleSVG* styleSVG = s->StyleSVG();
    // Note: the path that we return may be used for hit-testing, and SVG
    // exposes hit-testing of strokes that are not actually painted. For that
    // reason we do not check for eStyleSVGPaintType_None or check the stroke
    // opacity here.
    if (styleSVG->mStrokeLinecap != StyleStrokeLinecap::Butt) {
      strokeLineCap = styleSVG->mStrokeLinecap;
      strokeWidth = SVGContentUtils::GetStrokeWidth(this, s, nullptr);
    }

    const auto& d = s->StyleSVGReset()->mD;
    if (d.IsPath()) {
      path = SVGPathData::BuildPath(d.AsPath()._0.AsSpan(), aBuilder,
                                    strokeLineCap, strokeWidth, {}, {},
                                    s->EffectiveZoom().ToFloat());
    }
  };

  bool success = SVGGeometryProperty::DoForComputedStyle(this, callback);
  if (success) {
    return path.forget();
  }

  // Fallback to use the d attribute if it exists.
  return mD.GetAnimValue().BuildPath(aBuilder, strokeLineCap, strokeWidth,
                                     1.0f);
}

bool SVGPathElement::GetDistancesFromOriginToEndsOfVisibleSegments(
    FallibleTArray<double>* aOutput) {
  bool ret = false;
  auto callback = [&ret, aOutput](const ComputedStyle* s) {
    const auto& d = s->StyleSVGReset()->mD;
    ret = d.IsNone() ||
          SVGPathData::GetDistancesFromOriginToEndsOfVisibleSegments(
              d.AsPath()._0.AsSpan(), aOutput);
  };

  if (SVGGeometryProperty::DoForComputedStyle(this, callback)) {
    return ret;
  }

  return mD.GetAnimValue().GetDistancesFromOriginToEndsOfVisibleSegments(
      aOutput);
}

static bool PathIsClosed(Span<const StylePathCommand> aPath) {
  return !aPath.IsEmpty() && aPath.rbegin()->IsClose();
}

// Offset paths (including references to SVG Paths) are closed loops only if the
// final command in the path list is a closepath command ("z" or "Z"), otherwise
// they are unclosed intervals.
// https://drafts.fxtf.org/motion/#path-distance
bool SVGPathElement::IsClosedLoop() const {
  bool isClosed = false;

  auto callback = [&](const ComputedStyle* s) {
    const nsStyleSVGReset* styleSVGReset = s->StyleSVGReset();
    if (styleSVGReset->mD.IsPath()) {
      isClosed = PathIsClosed(styleSVGReset->mD.AsPath()._0.AsSpan());
    }
  };

  if (SVGGeometryProperty::DoForComputedStyle(this, callback)) {
    return isClosed;
  }

  return PathIsClosed(mD.GetAnimValue().AsSpan());
}

/* static */
bool SVGPathElement::IsDPropertyChangedViaCSS(const ComputedStyle& aNewStyle,
                                              const ComputedStyle& aOldStyle) {
  return aNewStyle.StyleSVGReset()->mD != aOldStyle.StyleSVGReset()->mD;
}

}  // namespace mozilla::dom