summaryrefslogtreecommitdiffstats
path: root/dom/svg/SVGPathData.cpp
blob: 2b2eaca46b6e3d8e1ad48af9605649b6c7d4ef0e (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
/* -*- 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 "SVGPathData.h"

#include "gfx2DGlue.h"
#include "gfxPlatform.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/gfx/Point.h"
#include "mozilla/RefPtr.h"
#include "nsError.h"
#include "nsString.h"
#include "SVGPathDataParser.h"
#include <stdarg.h>
#include "nsStyleConsts.h"
#include "SVGContentUtils.h"
#include "SVGGeometryElement.h"
#include "SVGPathSegUtils.h"
#include <algorithm>

using namespace mozilla::dom::SVGPathSeg_Binding;
using namespace mozilla::gfx;

namespace mozilla {

static inline bool IsMoveto(uint16_t aSegType) {
  return aSegType == PATHSEG_MOVETO_ABS || aSegType == PATHSEG_MOVETO_REL;
}

static inline bool IsValidType(uint16_t aSegType) {
  return SVGPathSegUtils::IsValidType(aSegType);
}

static inline bool IsClosePath(uint16_t aSegType) {
  return aSegType == PATHSEG_CLOSEPATH;
}

nsresult SVGPathData::CopyFrom(const SVGPathData& rhs) {
  if (!mData.Assign(rhs.mData, fallible)) {
    return NS_ERROR_OUT_OF_MEMORY;
  }
  return NS_OK;
}

void SVGPathData::GetValueAsString(nsAString& aValue) const {
  // we need this function in DidChangePathSegList
  aValue.Truncate();
  if (!Length()) {
    return;
  }
  uint32_t i = 0;
  for (;;) {
    nsAutoString segAsString;
    SVGPathSegUtils::GetValueAsString(&mData[i], segAsString);
    // We ignore OOM, since it's not useful for us to return an error.
    aValue.Append(segAsString);
    i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
    if (i >= mData.Length()) {
      MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
      return;
    }
    aValue.Append(' ');
  }
}

nsresult SVGPathData::SetValueFromString(const nsAString& aValue) {
  // We don't use a temp variable since the spec says to parse everything up to
  // the first error. We still return any error though so that callers know if
  // there's a problem.

  SVGPathDataParser pathParser(aValue, this);
  return pathParser.Parse() ? NS_OK : NS_ERROR_DOM_SYNTAX_ERR;
}

nsresult SVGPathData::AppendSeg(uint32_t aType, ...) {
  uint32_t oldLength = mData.Length();
  uint32_t newLength = oldLength + 1 + SVGPathSegUtils::ArgCountForType(aType);
  if (!mData.SetLength(newLength, fallible)) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  mData[oldLength] = SVGPathSegUtils::EncodeType(aType);
  va_list args;
  va_start(args, aType);
  for (uint32_t i = oldLength + 1; i < newLength; ++i) {
    // NOTE! 'float' is promoted to 'double' when passed through '...'!
    mData[i] = float(va_arg(args, double));
  }
  va_end(args);
  return NS_OK;
}

float SVGPathData::GetPathLength() const {
  SVGPathTraversalState state;

  uint32_t i = 0;
  while (i < mData.Length()) {
    SVGPathSegUtils::TraversePathSegment(&mData[i], state);
    i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");

  return state.length;
}

#ifdef DEBUG
uint32_t SVGPathData::CountItems() const {
  uint32_t i = 0, count = 0;

  while (i < mData.Length()) {
    i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
    count++;
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");

  return count;
}
#endif

bool SVGPathData::GetDistancesFromOriginToEndsOfVisibleSegments(
    FallibleTArray<double>* aOutput) const {
  SVGPathTraversalState state;

  aOutput->Clear();

  uint32_t i = 0;
  while (i < mData.Length()) {
    uint32_t segType = SVGPathSegUtils::DecodeType(mData[i]);
    SVGPathSegUtils::TraversePathSegment(&mData[i], state);

    // With degenerately large point coordinates, TraversePathSegment can fail
    // and end up producing NaNs.
    if (!std::isfinite(state.length)) {
      return false;
    }

    // We skip all moveto commands except an initial moveto. See the text 'A
    // "move to" command does not count as an additional point when dividing up
    // the duration...':
    //
    // http://www.w3.org/TR/SVG11/animate.html#AnimateMotionElement
    //
    // This is important in the non-default case of calcMode="linear". In
    // this case an equal amount of time is spent on each path segment,
    // except on moveto segments which are jumped over immediately.

    if (i == 0 || !IsMoveto(segType)) {
      if (!aOutput->AppendElement(state.length, fallible)) {
        return false;
      }
    }
    i += 1 + SVGPathSegUtils::ArgCountForType(segType);
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt?");

  return true;
}

/* static */
bool SVGPathData::GetDistancesFromOriginToEndsOfVisibleSegments(
    Span<const StylePathCommand> aPath, FallibleTArray<double>* aOutput) {
  SVGPathTraversalState state;

  aOutput->Clear();

  bool firstMoveToIsChecked = false;
  for (const auto& cmd : aPath) {
    SVGPathSegUtils::TraversePathSegment(cmd, state);
    if (!std::isfinite(state.length)) {
      return false;
    }

    // We skip all moveto commands except for the initial moveto.
    if (!cmd.IsMove() || !firstMoveToIsChecked) {
      if (!aOutput->AppendElement(state.length, fallible)) {
        return false;
      }
    }

    if (cmd.IsMove() && !firstMoveToIsChecked) {
      firstMoveToIsChecked = true;
    }
  }

  return true;
}

uint32_t SVGPathData::GetPathSegAtLength(float aDistance) const {
  // TODO [SVGWG issue] get specified what happen if 'aDistance' < 0, or
  // 'aDistance' > the length of the path, or the seg list is empty.
  // Return -1? Throwing would better help authors avoid tricky bugs (DOM
  // could do that if we return -1).

  uint32_t i = 0, segIndex = 0;
  SVGPathTraversalState state;

  while (i < mData.Length()) {
    SVGPathSegUtils::TraversePathSegment(&mData[i], state);
    if (state.length >= aDistance) {
      return segIndex;
    }
    i += 1 + SVGPathSegUtils::ArgCountForType(mData[i]);
    segIndex++;
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");

  return std::max(1U, segIndex) -
         1;  // -1 because while loop takes us 1 too far
}

/* static */
uint32_t SVGPathData::GetPathSegAtLength(Span<const StylePathCommand> aPath,
                                         float aDistance) {
  uint32_t segIndex = 0;
  SVGPathTraversalState state;

  for (const auto& cmd : aPath) {
    SVGPathSegUtils::TraversePathSegment(cmd, state);
    if (state.length >= aDistance) {
      return segIndex;
    }
    segIndex++;
  }

  return std::max(1U, segIndex) - 1;
}

/**
 * The SVG spec says we have to paint stroke caps for zero length subpaths:
 *
 *   http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
 *
 * Cairo only does this for |stroke-linecap: round| and not for
 * |stroke-linecap: square| (since that's what Adobe Acrobat has always done).
 * Most likely the other backends that DrawTarget uses have the same behavior.
 *
 * To help us conform to the SVG spec we have this helper function to draw an
 * approximation of square caps for zero length subpaths. It does this by
 * inserting a subpath containing a single user space axis aligned straight
 * line that is as small as it can be while minimizing the risk of it being
 * thrown away by the DrawTarget's backend for being too small to affect
 * rendering. The idea is that we'll then get stroke caps drawn for this axis
 * aligned line, creating an axis aligned rectangle that approximates the
 * square that would ideally be drawn.
 *
 * Since we don't have any information about transforms from user space to
 * device space, we choose the length of the small line that we insert by
 * making it a small percentage of the stroke width of the path. This should
 * hopefully allow us to make the line as long as possible (to avoid rounding
 * issues in the backend resulting in the backend seeing it as having zero
 * length) while still avoiding the small rectangle being noticeably different
 * from a square.
 *
 * Note that this function inserts a subpath into the current gfx path that
 * will be present during both fill and stroke operations.
 */
static void ApproximateZeroLengthSubpathSquareCaps(PathBuilder* aPB,
                                                   const Point& aPoint,
                                                   Float aStrokeWidth) {
  // Note that caps are proportional to stroke width, so if stroke width is
  // zero it's actually fine for |tinyLength| below to end up being zero.
  // However, it would be a waste to inserting a LineTo in that case, so better
  // not to.
  MOZ_ASSERT(aStrokeWidth > 0.0f,
             "Make the caller check for this, or check it here");

  // The fraction of the stroke width that we choose for the length of the
  // line is rather arbitrary, other than being chosen to meet the requirements
  // described in the comment above.

  Float tinyLength = aStrokeWidth / SVG_ZERO_LENGTH_PATH_FIX_FACTOR;

  aPB->LineTo(aPoint + Point(tinyLength, 0));
  aPB->MoveTo(aPoint);
}

#define MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT  \
  do {                                                           \
    if (!subpathHasLength && hasLineCaps && aStrokeWidth > 0 &&  \
        subpathContainsNonMoveTo && IsValidType(prevSegType) &&  \
        (!IsMoveto(prevSegType) || IsClosePath(segType))) {      \
      ApproximateZeroLengthSubpathSquareCaps(aBuilder, segStart, \
                                             aStrokeWidth);      \
    }                                                            \
  } while (0)

already_AddRefed<Path> SVGPathData::BuildPath(PathBuilder* aBuilder,
                                              StyleStrokeLinecap aStrokeLineCap,
                                              Float aStrokeWidth) const {
  if (mData.IsEmpty() || !IsMoveto(SVGPathSegUtils::DecodeType(mData[0]))) {
    return nullptr;  // paths without an initial moveto are invalid
  }

  bool hasLineCaps = aStrokeLineCap != StyleStrokeLinecap::Butt;
  bool subpathHasLength = false;  // visual length
  bool subpathContainsNonMoveTo = false;

  uint32_t segType = PATHSEG_UNKNOWN;
  uint32_t prevSegType = PATHSEG_UNKNOWN;
  Point pathStart(0.0, 0.0);  // start point of [sub]path
  Point segStart(0.0, 0.0);
  Point segEnd;
  Point cp1, cp2;    // previous bezier's control points
  Point tcp1, tcp2;  // temporaries

  // Regarding cp1 and cp2: If the previous segment was a cubic bezier curve,
  // then cp2 is its second control point. If the previous segment was a
  // quadratic curve, then cp1 is its (only) control point.

  uint32_t i = 0;
  while (i < mData.Length()) {
    segType = SVGPathSegUtils::DecodeType(mData[i++]);
    uint32_t argCount = SVGPathSegUtils::ArgCountForType(segType);

    switch (segType) {
      case PATHSEG_CLOSEPATH:
        // set this early to allow drawing of square caps for "M{x},{y} Z":
        subpathContainsNonMoveTo = true;
        MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
        segEnd = pathStart;
        aBuilder->Close();
        break;

      case PATHSEG_MOVETO_ABS:
        MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
        pathStart = segEnd = Point(mData[i], mData[i + 1]);
        aBuilder->MoveTo(segEnd);
        subpathHasLength = false;
        break;

      case PATHSEG_MOVETO_REL:
        MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;
        pathStart = segEnd = segStart + Point(mData[i], mData[i + 1]);
        aBuilder->MoveTo(segEnd);
        subpathHasLength = false;
        break;

      case PATHSEG_LINETO_ABS:
        segEnd = Point(mData[i], mData[i + 1]);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_LINETO_REL:
        segEnd = segStart + Point(mData[i], mData[i + 1]);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_CURVETO_CUBIC_ABS:
        cp1 = Point(mData[i], mData[i + 1]);
        cp2 = Point(mData[i + 2], mData[i + 3]);
        segEnd = Point(mData[i + 4], mData[i + 5]);
        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(cp1, cp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_CUBIC_REL:
        cp1 = segStart + Point(mData[i], mData[i + 1]);
        cp2 = segStart + Point(mData[i + 2], mData[i + 3]);
        segEnd = segStart + Point(mData[i + 4], mData[i + 5]);
        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(cp1, cp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_QUADRATIC_ABS:
        cp1 = Point(mData[i], mData[i + 1]);
        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;
        segEnd = Point(mData[i + 2], mData[i + 3]);  // set before setting tcp2!
        tcp2 = cp1 + (segEnd - cp1) / 3;
        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(tcp1, tcp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_QUADRATIC_REL:
        cp1 = segStart + Point(mData[i], mData[i + 1]);
        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;
        segEnd = segStart +
                 Point(mData[i + 2], mData[i + 3]);  // set before setting tcp2!
        tcp2 = cp1 + (segEnd - cp1) / 3;
        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(tcp1, tcp2, segEnd);
        }
        break;

      case PATHSEG_ARC_ABS:
      case PATHSEG_ARC_REL: {
        Point radii(mData[i], mData[i + 1]);
        segEnd = Point(mData[i + 5], mData[i + 6]);
        if (segType == PATHSEG_ARC_REL) {
          segEnd += segStart;
        }
        if (segEnd != segStart) {
          subpathHasLength = true;
          if (radii.x == 0.0f || radii.y == 0.0f) {
            aBuilder->LineTo(segEnd);
          } else {
            SVGArcConverter converter(segStart, segEnd, radii, mData[i + 2],
                                      mData[i + 3] != 0, mData[i + 4] != 0);
            while (converter.GetNextSegment(&cp1, &cp2, &segEnd)) {
              aBuilder->BezierTo(cp1, cp2, segEnd);
            }
          }
        }
        break;
      }

      case PATHSEG_LINETO_HORIZONTAL_ABS:
        segEnd = Point(mData[i], segStart.y);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_LINETO_HORIZONTAL_REL:
        segEnd = segStart + Point(mData[i], 0.0f);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_LINETO_VERTICAL_ABS:
        segEnd = Point(segStart.x, mData[i]);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_LINETO_VERTICAL_REL:
        segEnd = segStart + Point(0.0f, mData[i]);
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(segEnd);
        }
        break;

      case PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
        cp1 = SVGPathSegUtils::IsCubicType(prevSegType) ? segStart * 2 - cp2
                                                        : segStart;
        cp2 = Point(mData[i], mData[i + 1]);
        segEnd = Point(mData[i + 2], mData[i + 3]);
        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(cp1, cp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
        cp1 = SVGPathSegUtils::IsCubicType(prevSegType) ? segStart * 2 - cp2
                                                        : segStart;
        cp2 = segStart + Point(mData[i], mData[i + 1]);
        segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(cp1, cp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
        cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType) ? segStart * 2 - cp1
                                                            : segStart;
        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;
        segEnd = Point(mData[i], mData[i + 1]);  // set before setting tcp2!
        tcp2 = cp1 + (segEnd - cp1) / 3;
        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(tcp1, tcp2, segEnd);
        }
        break;

      case PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
        cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType) ? segStart * 2 - cp1
                                                            : segStart;
        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;
        segEnd = segStart +
                 Point(mData[i], mData[i + 1]);  // changed before setting tcp2!
        tcp2 = cp1 + (segEnd - cp1) / 3;
        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(tcp1, tcp2, segEnd);
        }
        break;

      default:
        MOZ_ASSERT_UNREACHABLE("Bad path segment type");
        return nullptr;  // according to spec we'd use everything up to the bad
                         // seg anyway
    }

    subpathContainsNonMoveTo = !IsMoveto(segType);
    i += argCount;
    prevSegType = segType;
    segStart = segEnd;
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");
  MOZ_ASSERT(prevSegType == segType,
             "prevSegType should be left at the final segType");

  MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT;

  return aBuilder->Finish();
}

#undef MAYBE_APPROXIMATE_ZERO_LENGTH_SUBPATH_SQUARE_CAPS_TO_DT

already_AddRefed<Path> SVGPathData::BuildPathForMeasuring() const {
  // Since the path that we return will not be used for painting it doesn't
  // matter what we pass to CreatePathBuilder as aFillRule. Hawever, we do want
  // to pass something other than NS_STYLE_STROKE_LINECAP_SQUARE as
  // aStrokeLineCap to avoid the insertion of extra little lines (by
  // ApproximateZeroLengthSubpathSquareCaps), in which case the value that we
  // pass as aStrokeWidth doesn't matter (since it's only used to determine the
  // length of those extra little lines).

  RefPtr<DrawTarget> drawTarget =
      gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
  RefPtr<PathBuilder> builder =
      drawTarget->CreatePathBuilder(FillRule::FILL_WINDING);
  return BuildPath(builder, StyleStrokeLinecap::Butt, 0);
}

/* static */
already_AddRefed<Path> SVGPathData::BuildPathForMeasuring(
    Span<const StylePathCommand> aPath) {
  RefPtr<DrawTarget> drawTarget =
      gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
  RefPtr<PathBuilder> builder =
      drawTarget->CreatePathBuilder(FillRule::FILL_WINDING);
  return BuildPath(aPath, builder, StyleStrokeLinecap::Butt, 0);
}

static inline StyleCSSFloat GetRotate(const StyleCSSFloat& aAngle) {
  return aAngle;
}

static inline StyleCSSFloat GetRotate(const StyleAngle& aAngle) {
  return aAngle.ToDegrees();
}

static inline StyleCSSFloat Resolve(const StyleCSSFloat& aValue,
                                    CSSCoord aBasis) {
  return aValue;
}

static inline StyleCSSFloat Resolve(const LengthPercentage& aValue,
                                    CSSCoord aBasis) {
  return aValue.ResolveToCSSPixels(aBasis);
}

template <typename Angle, typename LP>
static already_AddRefed<Path> BuildPathInternal(
    Span<const StyleGenericShapeCommand<Angle, LP>> aPath,
    PathBuilder* aBuilder, StyleStrokeLinecap aStrokeLineCap,
    Float aStrokeWidth, const CSSSize& aPercentageBasis, const Point& aOffset,
    float aZoomFactor) {
  using Command = StyleGenericShapeCommand<Angle, LP>;

  if (aPath.IsEmpty() || !aPath[0].IsMove()) {
    return nullptr;  // paths without an initial moveto are invalid
  }

  bool hasLineCaps = aStrokeLineCap != StyleStrokeLinecap::Butt;
  bool subpathHasLength = false;  // visual length
  bool subpathContainsNonMoveTo = false;

  const Command* seg = nullptr;
  const Command* prevSeg = nullptr;
  Point pathStart(0.0, 0.0);  // start point of [sub]path
  Point segStart(0.0, 0.0);
  Point segEnd;
  Point cp1, cp2;    // previous bezier's control points
  Point tcp1, tcp2;  // temporaries

  auto maybeApproximateZeroLengthSubpathSquareCaps =
      [&](const Command* aPrevSeg, const Command* aSeg) {
        if (!subpathHasLength && hasLineCaps && aStrokeWidth > 0 &&
            subpathContainsNonMoveTo && aPrevSeg && aSeg &&
            (!aPrevSeg->IsMove() || aSeg->IsClose())) {
          ApproximateZeroLengthSubpathSquareCaps(aBuilder, segStart,
                                                 aStrokeWidth);
        }
      };

  auto scale = [aOffset, aZoomFactor](const Point& p) {
    return Point(p.x * aZoomFactor, p.y * aZoomFactor) + aOffset;
  };

  // Regarding cp1 and cp2: If the previous segment was a cubic bezier curve,
  // then cp2 is its second control point. If the previous segment was a
  // quadratic curve, then cp1 is its (only) control point.

  for (const auto& cmd : aPath) {
    seg = &cmd;
    switch (cmd.tag) {
      case Command::Tag::Close:
        // set this early to allow drawing of square caps for "M{x},{y} Z":
        subpathContainsNonMoveTo = true;
        maybeApproximateZeroLengthSubpathSquareCaps(prevSeg, seg);
        segEnd = pathStart;
        aBuilder->Close();
        break;
      case Command::Tag::Move: {
        maybeApproximateZeroLengthSubpathSquareCaps(prevSeg, seg);
        const Point& p = cmd.move.point.ToGfxPoint(aPercentageBasis);
        pathStart = segEnd = cmd.move.by_to == StyleByTo::To ? p : segStart + p;
        aBuilder->MoveTo(scale(segEnd));
        subpathHasLength = false;
        break;
      }
      case Command::Tag::Line: {
        const Point& p = cmd.line.point.ToGfxPoint(aPercentageBasis);
        segEnd = cmd.line.by_to == StyleByTo::To ? p : segStart + p;
        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(scale(segEnd));
        }
        break;
      }
      case Command::Tag::CubicCurve:
        cp1 = cmd.cubic_curve.control1.ToGfxPoint(aPercentageBasis);
        cp2 = cmd.cubic_curve.control2.ToGfxPoint(aPercentageBasis);
        segEnd = cmd.cubic_curve.point.ToGfxPoint(aPercentageBasis);

        if (cmd.cubic_curve.by_to == StyleByTo::By) {
          cp1 += segStart;
          cp2 += segStart;
          segEnd += segStart;
        }

        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
        }
        break;

      case Command::Tag::QuadCurve:
        cp1 = cmd.quad_curve.control1.ToGfxPoint(aPercentageBasis);
        segEnd = cmd.quad_curve.point.ToGfxPoint(aPercentageBasis);

        if (cmd.quad_curve.by_to == StyleByTo::By) {
          cp1 += segStart;
          segEnd += segStart;  // set before setting tcp2!
        }

        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;
        tcp2 = cp1 + (segEnd - cp1) / 3;

        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(scale(tcp1), scale(tcp2), scale(segEnd));
        }
        break;

      case Command::Tag::Arc: {
        const auto& arc = cmd.arc;
        const Point& radii = arc.radii.ToGfxPoint(aPercentageBasis);
        segEnd = arc.point.ToGfxPoint(aPercentageBasis);
        if (arc.by_to == StyleByTo::By) {
          segEnd += segStart;
        }
        if (segEnd != segStart) {
          subpathHasLength = true;
          if (radii.x == 0.0f || radii.y == 0.0f) {
            aBuilder->LineTo(scale(segEnd));
          } else {
            const bool arc_is_large = arc.arc_size == StyleArcSize::Large;
            const bool arc_is_cw = arc.arc_sweep == StyleArcSweep::Cw;
            SVGArcConverter converter(segStart, segEnd, radii,
                                      GetRotate(arc.rotate), arc_is_large,
                                      arc_is_cw);
            while (converter.GetNextSegment(&cp1, &cp2, &segEnd)) {
              aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
            }
          }
        }
        break;
      }
      case Command::Tag::HLine: {
        const float x = Resolve(cmd.h_line.x, aPercentageBasis.width);
        if (cmd.h_line.by_to == StyleByTo::To) {
          segEnd = Point(x, segStart.y);
        } else {
          segEnd = segStart + Point(x, 0.0f);
        }

        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(scale(segEnd));
        }
        break;
      }
      case Command::Tag::VLine: {
        const float y = Resolve(cmd.v_line.y, aPercentageBasis.height);
        if (cmd.v_line.by_to == StyleByTo::To) {
          segEnd = Point(segStart.x, y);
        } else {
          segEnd = segStart + Point(0.0f, y);
        }

        if (segEnd != segStart) {
          subpathHasLength = true;
          aBuilder->LineTo(scale(segEnd));
        }
        break;
      }
      case Command::Tag::SmoothCubic:
        cp1 = prevSeg && prevSeg->IsCubicType() ? segStart * 2 - cp2 : segStart;
        cp2 = cmd.smooth_cubic.control2.ToGfxPoint(aPercentageBasis);
        segEnd = cmd.smooth_cubic.point.ToGfxPoint(aPercentageBasis);

        if (cmd.smooth_cubic.by_to == StyleByTo::By) {
          cp2 += segStart;
          segEnd += segStart;
        }

        if (segEnd != segStart || segEnd != cp1 || segEnd != cp2) {
          subpathHasLength = true;
          aBuilder->BezierTo(scale(cp1), scale(cp2), scale(segEnd));
        }
        break;

      case Command::Tag::SmoothQuad: {
        cp1 = prevSeg && prevSeg->IsQuadraticType() ? segStart * 2 - cp1
                                                    : segStart;
        // Convert quadratic curve to cubic curve:
        tcp1 = segStart + (cp1 - segStart) * 2 / 3;

        const Point& p = cmd.smooth_quad.point.ToGfxPoint(aPercentageBasis);
        // set before setting tcp2!
        segEnd = cmd.smooth_quad.by_to == StyleByTo::To ? p : segStart + p;
        tcp2 = cp1 + (segEnd - cp1) / 3;

        if (segEnd != segStart || segEnd != cp1) {
          subpathHasLength = true;
          aBuilder->BezierTo(scale(tcp1), scale(tcp2), scale(segEnd));
        }
        break;
      }
    }

    subpathContainsNonMoveTo = !cmd.IsMove();
    prevSeg = seg;
    segStart = segEnd;
  }

  MOZ_ASSERT(prevSeg == seg, "prevSegType should be left at the final segType");

  maybeApproximateZeroLengthSubpathSquareCaps(prevSeg, seg);

  return aBuilder->Finish();
}

/* static */
already_AddRefed<Path> SVGPathData::BuildPath(
    Span<const StylePathCommand> aPath, PathBuilder* aBuilder,
    StyleStrokeLinecap aStrokeLineCap, Float aStrokeWidth,
    const CSSSize& aBasis, const gfx::Point& aOffset, float aZoomFactor) {
  return BuildPathInternal(aPath, aBuilder, aStrokeLineCap, aStrokeWidth,
                           aBasis, aOffset, aZoomFactor);
}

/* static */
already_AddRefed<Path> SVGPathData::BuildPath(
    Span<const StyleShapeCommand> aShape, PathBuilder* aBuilder,
    StyleStrokeLinecap aStrokeLineCap, Float aStrokeWidth,
    const CSSSize& aBasis, const gfx::Point& aOffset, float aZoomFactor) {
  return BuildPathInternal(aShape, aBuilder, aStrokeLineCap, aStrokeWidth,
                           aBasis, aOffset, aZoomFactor);
}

static double AngleOfVector(const Point& aVector) {
  // C99 says about atan2 "A domain error may occur if both arguments are
  // zero" and "On a domain error, the function returns an implementation-
  // defined value". In the case of atan2 the implementation-defined value
  // seems to commonly be zero, but it could just as easily be a NaN value.
  // We specifically want zero in this case, hence the check:

  return (aVector != Point(0.0, 0.0)) ? atan2(aVector.y, aVector.x) : 0.0;
}

static float AngleOfVector(const Point& cp1, const Point& cp2) {
  return static_cast<float>(AngleOfVector(cp1 - cp2));
}

// This implements F.6.5 and F.6.6 of
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
static std::tuple<float, float, float, float>
/* rx, ry, segStartAngle, segEndAngle */
ComputeSegAnglesAndCorrectRadii(const Point& aSegStart, const Point& aSegEnd,
                                const float aAngle, const bool aLargeArcFlag,
                                const bool aSweepFlag, const float aRx,
                                const float aRy) {
  float rx = fabs(aRx);  // F.6.6.1
  float ry = fabs(aRy);

  // F.6.5.1:
  const float angle = static_cast<float>(aAngle * M_PI / 180.0);
  double x1p = cos(angle) * (aSegStart.x - aSegEnd.x) / 2.0 +
               sin(angle) * (aSegStart.y - aSegEnd.y) / 2.0;
  double y1p = -sin(angle) * (aSegStart.x - aSegEnd.x) / 2.0 +
               cos(angle) * (aSegStart.y - aSegEnd.y) / 2.0;

  // This is the root in F.6.5.2 and the numerator under that root:
  double root;
  double numerator =
      rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;

  if (numerator >= 0.0) {
    root = sqrt(numerator / (rx * rx * y1p * y1p + ry * ry * x1p * x1p));
    if (aLargeArcFlag == aSweepFlag) root = -root;
  } else {
    // F.6.6 step 3 - |numerator < 0.0|. This is equivalent to the result
    // of F.6.6.2 (lamedh) being greater than one. What we have here is
    // ellipse radii that are too small for the ellipse to reach between
    // segStart and segEnd. We scale the radii up uniformly so that the
    // ellipse is just big enough to fit (i.e. to the point where there is
    // exactly one solution).

    double lamedh =
        1.0 - numerator / (rx * rx * ry * ry);  // equiv to eqn F.6.6.2
    double s = sqrt(lamedh);
    rx = static_cast<float>((double)rx * s);  // F.6.6.3
    ry = static_cast<float>((double)ry * s);
    root = 0.0;
  }

  double cxp = root * rx * y1p / ry;  // F.6.5.2
  double cyp = -root * ry * x1p / rx;

  double theta =
      AngleOfVector(Point(static_cast<float>((x1p - cxp) / rx),
                          static_cast<float>((y1p - cyp) / ry)));  // F.6.5.5
  double delta =
      AngleOfVector(Point(static_cast<float>((-x1p - cxp) / rx),
                          static_cast<float>((-y1p - cyp) / ry))) -  // F.6.5.6
      theta;
  if (!aSweepFlag && delta > 0) {
    delta -= 2.0 * M_PI;
  } else if (aSweepFlag && delta < 0) {
    delta += 2.0 * M_PI;
  }

  double tx1, ty1, tx2, ty2;
  tx1 = -cos(angle) * rx * sin(theta) - sin(angle) * ry * cos(theta);
  ty1 = -sin(angle) * rx * sin(theta) + cos(angle) * ry * cos(theta);
  tx2 = -cos(angle) * rx * sin(theta + delta) -
        sin(angle) * ry * cos(theta + delta);
  ty2 = -sin(angle) * rx * sin(theta + delta) +
        cos(angle) * ry * cos(theta + delta);

  if (delta < 0.0f) {
    tx1 = -tx1;
    ty1 = -ty1;
    tx2 = -tx2;
    ty2 = -ty2;
  }

  return {rx, ry, static_cast<float>(atan2(ty1, tx1)),
          static_cast<float>(atan2(ty2, tx2))};
}

void SVGPathData::GetMarkerPositioningData(nsTArray<SVGMark>* aMarks) const {
  // This code should assume that ANY type of segment can appear at ANY index.
  // It should also assume that segments such as M and Z can appear in weird
  // places, and repeat multiple times consecutively.

  // info on current [sub]path (reset every M command):
  Point pathStart(0.0, 0.0);
  float pathStartAngle = 0.0f;
  uint32_t pathStartIndex = 0;

  // info on previous segment:
  uint16_t prevSegType = PATHSEG_UNKNOWN;
  Point prevSegEnd(0.0, 0.0);
  float prevSegEndAngle = 0.0f;
  Point prevCP;  // if prev seg was a bezier, this was its last control point

  uint32_t i = 0;
  while (i < mData.Length()) {
    // info on current segment:
    uint16_t segType =
        SVGPathSegUtils::DecodeType(mData[i++]);  // advances i to args
    Point& segStart = prevSegEnd;
    Point segEnd;
    float segStartAngle, segEndAngle;

    switch (segType)  // to find segStartAngle, segEnd and segEndAngle
    {
      case PATHSEG_CLOSEPATH:
        segEnd = pathStart;
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;

      case PATHSEG_MOVETO_ABS:
      case PATHSEG_MOVETO_REL:
        if (segType == PATHSEG_MOVETO_ABS) {
          segEnd = Point(mData[i], mData[i + 1]);
        } else {
          segEnd = segStart + Point(mData[i], mData[i + 1]);
        }
        pathStart = segEnd;
        pathStartIndex = aMarks->Length();
        // If authors are going to specify multiple consecutive moveto commands
        // with markers, me might as well make the angle do something useful:
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        i += 2;
        break;

      case PATHSEG_LINETO_ABS:
      case PATHSEG_LINETO_REL:
        if (segType == PATHSEG_LINETO_ABS) {
          segEnd = Point(mData[i], mData[i + 1]);
        } else {
          segEnd = segStart + Point(mData[i], mData[i + 1]);
        }
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        i += 2;
        break;

      case PATHSEG_CURVETO_CUBIC_ABS:
      case PATHSEG_CURVETO_CUBIC_REL: {
        Point cp1, cp2;  // control points
        if (segType == PATHSEG_CURVETO_CUBIC_ABS) {
          cp1 = Point(mData[i], mData[i + 1]);
          cp2 = Point(mData[i + 2], mData[i + 3]);
          segEnd = Point(mData[i + 4], mData[i + 5]);
        } else {
          cp1 = segStart + Point(mData[i], mData[i + 1]);
          cp2 = segStart + Point(mData[i + 2], mData[i + 3]);
          segEnd = segStart + Point(mData[i + 4], mData[i + 5]);
        }
        prevCP = cp2;
        segStartAngle = AngleOfVector(
            cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
        segEndAngle = AngleOfVector(
            segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
        i += 6;
        break;
      }

      case PATHSEG_CURVETO_QUADRATIC_ABS:
      case PATHSEG_CURVETO_QUADRATIC_REL: {
        Point cp1;  // control point
        if (segType == PATHSEG_CURVETO_QUADRATIC_ABS) {
          cp1 = Point(mData[i], mData[i + 1]);
          segEnd = Point(mData[i + 2], mData[i + 3]);
        } else {
          cp1 = segStart + Point(mData[i], mData[i + 1]);
          segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
        }
        prevCP = cp1;
        segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
        segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
        i += 4;
        break;
      }

      case PATHSEG_ARC_ABS:
      case PATHSEG_ARC_REL: {
        float rx = mData[i];
        float ry = mData[i + 1];
        float angle = mData[i + 2];
        bool largeArcFlag = mData[i + 3] != 0.0f;
        bool sweepFlag = mData[i + 4] != 0.0f;
        if (segType == PATHSEG_ARC_ABS) {
          segEnd = Point(mData[i + 5], mData[i + 6]);
        } else {
          segEnd = segStart + Point(mData[i + 5], mData[i + 6]);
        }

        // See section F.6 of SVG 1.1 for details on what we're doing here:
        // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes

        if (segStart == segEnd) {
          // F.6.2 says "If the endpoints (x1, y1) and (x2, y2) are identical,
          // then this is equivalent to omitting the elliptical arc segment
          // entirely." We take that very literally here, not adding a mark, and
          // not even setting any of the 'prev' variables so that it's as if
          // this arc had never existed; note the difference this will make e.g.
          // if the arc is proceeded by a bezier curve and followed by a
          // "smooth" bezier curve of the same degree!
          i += 7;
          continue;
        }

        // Below we have funny interleaving of F.6.6 (Correction of out-of-range
        // radii) and F.6.5 (Conversion from endpoint to center
        // parameterization) which is designed to avoid some unnecessary
        // calculations.

        if (rx == 0.0 || ry == 0.0) {
          // F.6.6 step 1 - straight line or coincidental points
          segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
          i += 7;
          break;
        }

        std::tie(rx, ry, segStartAngle, segEndAngle) =
            ComputeSegAnglesAndCorrectRadii(segStart, segEnd, angle,
                                            largeArcFlag, sweepFlag, rx, ry);
        i += 7;
        break;
      }

      case PATHSEG_LINETO_HORIZONTAL_ABS:
      case PATHSEG_LINETO_HORIZONTAL_REL:
        if (segType == PATHSEG_LINETO_HORIZONTAL_ABS) {
          segEnd = Point(mData[i++], segStart.y);
        } else {
          segEnd = segStart + Point(mData[i++], 0.0f);
        }
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;

      case PATHSEG_LINETO_VERTICAL_ABS:
      case PATHSEG_LINETO_VERTICAL_REL:
        if (segType == PATHSEG_LINETO_VERTICAL_ABS) {
          segEnd = Point(segStart.x, mData[i++]);
        } else {
          segEnd = segStart + Point(0.0f, mData[i++]);
        }
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;

      case PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
      case PATHSEG_CURVETO_CUBIC_SMOOTH_REL: {
        Point cp1 = SVGPathSegUtils::IsCubicType(prevSegType)
                        ? segStart * 2 - prevCP
                        : segStart;
        Point cp2;
        if (segType == PATHSEG_CURVETO_CUBIC_SMOOTH_ABS) {
          cp2 = Point(mData[i], mData[i + 1]);
          segEnd = Point(mData[i + 2], mData[i + 3]);
        } else {
          cp2 = segStart + Point(mData[i], mData[i + 1]);
          segEnd = segStart + Point(mData[i + 2], mData[i + 3]);
        }
        prevCP = cp2;
        segStartAngle = AngleOfVector(
            cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
        segEndAngle = AngleOfVector(
            segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
        i += 4;
        break;
      }

      case PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
      case PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL: {
        Point cp1 = SVGPathSegUtils::IsQuadraticType(prevSegType)
                        ? segStart * 2 - prevCP
                        : segStart;
        if (segType == PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS) {
          segEnd = Point(mData[i], mData[i + 1]);
        } else {
          segEnd = segStart + Point(mData[i], mData[i + 1]);
        }
        prevCP = cp1;
        segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
        segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
        i += 2;
        break;
      }

      default:
        // Leave any existing marks in aMarks so we have a visual indication of
        // when things went wrong.
        MOZ_ASSERT(false, "Unknown segment type - path corruption?");
        return;
    }

    // Set the angle of the mark at the start of this segment:
    if (aMarks->Length()) {
      SVGMark& mark = aMarks->LastElement();
      if (!IsMoveto(segType) && IsMoveto(prevSegType)) {
        // start of new subpath
        pathStartAngle = mark.angle = segStartAngle;
      } else if (IsMoveto(segType) && !IsMoveto(prevSegType)) {
        // end of a subpath
        if (prevSegType != PATHSEG_CLOSEPATH) mark.angle = prevSegEndAngle;
      } else {
        if (!(segType == PATHSEG_CLOSEPATH && prevSegType == PATHSEG_CLOSEPATH))
          mark.angle =
              SVGContentUtils::AngleBisect(prevSegEndAngle, segStartAngle);
      }
    }

    // Add the mark at the end of this segment, and set its position:
    // XXX(Bug 1631371) Check if this should use a fallible operation as it
    // pretended earlier.
    aMarks->AppendElement(SVGMark(static_cast<float>(segEnd.x),
                                  static_cast<float>(segEnd.y), 0.0f,
                                  SVGMark::eMid));

    if (segType == PATHSEG_CLOSEPATH && prevSegType != PATHSEG_CLOSEPATH) {
      aMarks->LastElement().angle = aMarks->ElementAt(pathStartIndex).angle =
          SVGContentUtils::AngleBisect(segEndAngle, pathStartAngle);
    }

    prevSegType = segType;
    prevSegEnd = segEnd;
    prevSegEndAngle = segEndAngle;
  }

  MOZ_ASSERT(i == mData.Length(), "Very, very bad - mData corrupt");

  if (aMarks->Length()) {
    if (prevSegType != PATHSEG_CLOSEPATH) {
      aMarks->LastElement().angle = prevSegEndAngle;
    }
    aMarks->LastElement().type = SVGMark::eEnd;
    aMarks->ElementAt(0).type = SVGMark::eStart;
  }
}

// Basically, this is identical to the above function, but replace |mData| with
// |aPath|. We probably can factor out some identical calculation, but I believe
// the above one will be removed because we will use any kind of array of
// StylePathCommand for SVG d attribute in the future.
/* static */
void SVGPathData::GetMarkerPositioningData(Span<const StylePathCommand> aPath,
                                           nsTArray<SVGMark>* aMarks) {
  if (aPath.IsEmpty()) {
    return;
  }

  // info on current [sub]path (reset every M command):
  Point pathStart(0.0, 0.0);
  float pathStartAngle = 0.0f;
  uint32_t pathStartIndex = 0;

  // info on previous segment:
  const StylePathCommand* prevSeg = nullptr;
  Point prevSegEnd(0.0, 0.0);
  float prevSegEndAngle = 0.0f;
  Point prevCP;  // if prev seg was a bezier, this was its last control point

  for (const StylePathCommand& cmd : aPath) {
    Point& segStart = prevSegEnd;
    Point segEnd;
    float segStartAngle, segEndAngle;

    switch (cmd.tag)  // to find segStartAngle, segEnd and segEndAngle
    {
      case StylePathCommand::Tag::Close:
        segEnd = pathStart;
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;

      case StylePathCommand::Tag::Move: {
        const Point& p = cmd.move.point.ToGfxPoint();
        pathStart = segEnd = cmd.move.by_to == StyleByTo::To ? p : segStart + p;
        pathStartIndex = aMarks->Length();
        // If authors are going to specify multiple consecutive moveto commands
        // with markers, me might as well make the angle do something useful:
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;
      }
      case StylePathCommand::Tag::Line: {
        const Point& p = cmd.line.point.ToGfxPoint();
        segEnd = cmd.line.by_to == StyleByTo::To ? p : segStart + p;
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;
      }
      case StylePathCommand::Tag::CubicCurve: {
        Point cp1 = cmd.cubic_curve.control1.ToGfxPoint();
        Point cp2 = cmd.cubic_curve.control2.ToGfxPoint();
        segEnd = cmd.cubic_curve.point.ToGfxPoint();

        if (cmd.cubic_curve.by_to == StyleByTo::By) {
          cp1 += segStart;
          cp2 += segStart;
          segEnd += segStart;
        }

        prevCP = cp2;
        segStartAngle = AngleOfVector(
            cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
        segEndAngle = AngleOfVector(
            segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
        break;
      }
      case StylePathCommand::Tag::QuadCurve: {
        Point cp1 = cmd.quad_curve.control1.ToGfxPoint();
        segEnd = cmd.quad_curve.point.ToGfxPoint();

        if (cmd.quad_curve.by_to == StyleByTo::By) {
          cp1 += segStart;
          segEnd += segStart;  // set before setting tcp2!
        }

        prevCP = cp1;
        segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
        segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
        break;
      }
      case StylePathCommand::Tag::Arc: {
        const auto& arc = cmd.arc;
        float rx = arc.radii.x;
        float ry = arc.radii.y;
        float angle = arc.rotate;
        bool largeArcFlag = arc.arc_size == StyleArcSize::Large;
        bool sweepFlag = arc.arc_sweep == StyleArcSweep::Cw;
        segEnd = arc.point.ToGfxPoint();
        if (arc.by_to == StyleByTo::By) {
          segEnd += segStart;
        }

        // See section F.6 of SVG 1.1 for details on what we're doing here:
        // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes

        if (segStart == segEnd) {
          // F.6.2 says "If the endpoints (x1, y1) and (x2, y2) are identical,
          // then this is equivalent to omitting the elliptical arc segment
          // entirely." We take that very literally here, not adding a mark, and
          // not even setting any of the 'prev' variables so that it's as if
          // this arc had never existed; note the difference this will make e.g.
          // if the arc is proceeded by a bezier curve and followed by a
          // "smooth" bezier curve of the same degree!
          continue;
        }

        // Below we have funny interleaving of F.6.6 (Correction of out-of-range
        // radii) and F.6.5 (Conversion from endpoint to center
        // parameterization) which is designed to avoid some unnecessary
        // calculations.

        if (rx == 0.0 || ry == 0.0) {
          // F.6.6 step 1 - straight line or coincidental points
          segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
          break;
        }

        std::tie(rx, ry, segStartAngle, segEndAngle) =
            ComputeSegAnglesAndCorrectRadii(segStart, segEnd, angle,
                                            largeArcFlag, sweepFlag, rx, ry);
        break;
      }
      case StylePathCommand::Tag::HLine: {
        if (cmd.h_line.by_to == StyleByTo::To) {
          segEnd = Point(cmd.h_line.x, segStart.y);
        } else {
          segEnd = segStart + Point(cmd.h_line.x, 0.0f);
        }
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;
      }
      case StylePathCommand::Tag::VLine: {
        if (cmd.v_line.by_to == StyleByTo::To) {
          segEnd = Point(segStart.x, cmd.v_line.y);
        } else {
          segEnd = segStart + Point(0.0f, cmd.v_line.y);
        }
        segStartAngle = segEndAngle = AngleOfVector(segEnd, segStart);
        break;
      }
      case StylePathCommand::Tag::SmoothCubic: {
        const Point& cp1 = prevSeg && prevSeg->IsCubicType()
                               ? segStart * 2 - prevCP
                               : segStart;
        Point cp2 = cmd.smooth_cubic.control2.ToGfxPoint();
        segEnd = cmd.smooth_cubic.point.ToGfxPoint();

        if (cmd.smooth_cubic.by_to == StyleByTo::By) {
          cp2 += segStart;
          segEnd += segStart;
        }

        prevCP = cp2;
        segStartAngle = AngleOfVector(
            cp1 == segStart ? (cp1 == cp2 ? segEnd : cp2) : cp1, segStart);
        segEndAngle = AngleOfVector(
            segEnd, cp2 == segEnd ? (cp1 == cp2 ? segStart : cp1) : cp2);
        break;
      }
      case StylePathCommand::Tag::SmoothQuad: {
        const Point& cp1 = prevSeg && prevSeg->IsQuadraticType()
                               ? segStart * 2 - prevCP
                               : segStart;
        segEnd = cmd.smooth_quad.by_to == StyleByTo::To
                     ? cmd.smooth_quad.point.ToGfxPoint()
                     : segStart + cmd.smooth_quad.point.ToGfxPoint();

        prevCP = cp1;
        segStartAngle = AngleOfVector(cp1 == segStart ? segEnd : cp1, segStart);
        segEndAngle = AngleOfVector(segEnd, cp1 == segEnd ? segStart : cp1);
        break;
      }
    }

    // Set the angle of the mark at the start of this segment:
    if (aMarks->Length()) {
      SVGMark& mark = aMarks->LastElement();
      if (!cmd.IsMove() && prevSeg && prevSeg->IsMove()) {
        // start of new subpath
        pathStartAngle = mark.angle = segStartAngle;
      } else if (cmd.IsMove() && !(prevSeg && prevSeg->IsMove())) {
        // end of a subpath
        if (!(prevSeg && prevSeg->IsClose())) {
          mark.angle = prevSegEndAngle;
        }
      } else if (!(cmd.IsClose() && prevSeg && prevSeg->IsClose())) {
        mark.angle =
            SVGContentUtils::AngleBisect(prevSegEndAngle, segStartAngle);
      }
    }

    // Add the mark at the end of this segment, and set its position:
    // XXX(Bug 1631371) Check if this should use a fallible operation as it
    // pretended earlier.
    aMarks->AppendElement(SVGMark(static_cast<float>(segEnd.x),
                                  static_cast<float>(segEnd.y), 0.0f,
                                  SVGMark::eMid));

    if (cmd.IsClose() && !(prevSeg && prevSeg->IsClose())) {
      aMarks->LastElement().angle = aMarks->ElementAt(pathStartIndex).angle =
          SVGContentUtils::AngleBisect(segEndAngle, pathStartAngle);
    }

    prevSeg = &cmd;
    prevSegEnd = segEnd;
    prevSegEndAngle = segEndAngle;
  }

  if (aMarks->Length()) {
    if (!(prevSeg && prevSeg->IsClose())) {
      aMarks->LastElement().angle = prevSegEndAngle;
    }
    aMarks->LastElement().type = SVGMark::eEnd;
    aMarks->ElementAt(0).type = SVGMark::eStart;
  }
}

size_t SVGPathData::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
  return mData.ShallowSizeOfExcludingThis(aMallocSizeOf);
}

size_t SVGPathData::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
  return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}

}  // namespace mozilla