File: svg_element.cpp

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (1176 lines) | stat: -rw-r--r-- 43,191 bytes parent folder | download | duplicates (2)
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
#include <algorithm>
#include <cassert>
#include <cmath>
#include <format>
#include <limits>
#include <stdexcept>

#include "svg_element.h"
#include <util/unreachable.h>

SVG::SVGElement::SVGElement(SVGElementType etype) : elem_type(etype) {}

static double px_to_pt(double px) {
  // a `pt` is 0.75 `px`. See e.g.
  // https://oreillymedia.github.io/Using_SVG/guide/units.html
  return px * 3 / 4;
}

bool SVG::SVGElement::is_closed_shape_element() const {
  switch (elem_type) {
  case SVG::SVGElementType::Circle:
  case SVG::SVGElementType::Ellipse:
  case SVG::SVGElementType::Polygon:
  case SVG::SVGElementType::Rect:
    return true;
  default:
    return false;
  }
}

bool SVG::SVGElement::is_shape_element() const {
  switch (elem_type) {
  case SVG::SVGElementType::Circle:
  case SVG::SVGElementType::Ellipse:
  case SVG::SVGElementType::Line:
  case SVG::SVGElementType::Path:
  case SVG::SVGElementType::Polygon:
  case SVG::SVGElementType::Polyline:
  case SVG::SVGElementType::Rect:
    return true;
  default:
    return false;
  }
}

static std::string xml_encode(const std::string &text) {
  std::string out;
  for (const char &ch : text) {
    switch (ch) {
    case '>':
      out += "&gt;";
      break;
    case '<':
      out += "&lt;";
      break;
    case '-':
      out += "&#45;";
      break;
    case '&':
      out += "&amp;";
      break;
    default:
      out += ch;
    }
  }
  return out;
}

static std::string rgb_to_hex(const std::string &color, double opacity = 1.0) {
  const auto comma1 = color.find_first_of(",");
  const auto r = std::stoi(color.substr(4, comma1 - 1));
  const auto comma2 = color.find_first_of(",", comma1 + 1);
  const auto g = std::stoi(color.substr(comma1 + 1, comma2 - 1));
  const auto close_par = color.find_first_of(")", comma2 + 1);
  const auto b = std::stoi(color.substr(comma2 + 1, close_par - 1));
  const auto opacity_int = std::lround(opacity * 255.0);
  const auto opacity_hex_str =
      opacity_int < 255 ? std::format("{:02x}", opacity_int) : "";
  return std::format("#{:02x}{:02x}{:02x}{}", r, g, b, opacity_hex_str);
}

// convert a valid color specification to the flavor that Graphviz uses in the
// SVG
static std::string to_graphviz_color(const std::string &color) {
  if (color == "rgb(0,0,0)") {
    return "black";
  } else if (color == "rgb(255,255,255)") {
    return "white";
  } else if (color.starts_with("rgb")) {
    return rgb_to_hex(color);
  } else {
    return color;
  }
}

// convert a valid color specification to the RGB or RGBA type that Graphviz
// uses in the DOT source
std::string SVG::to_dot_color(const std::string &color, double opacity) {
  if (color == "none") {
    return "#00000000";
  }
  if (opacity < 1.0) {
    if (!color.starts_with("rgb")) {
      throw std::runtime_error{std::format(
          "Cannot convert stroke={}, stroke_opacity={} to Graphviz color",
          color, opacity)};
    }
  }
  return rgb_to_hex(color, opacity);
}

void SVG::SVGElement::add_bbox() {
  const auto bbox = SVGElement::bbox();
  add_rect(bbox, "green");
}

void SVG::SVGElement::add_rect(SVGRect rect, const std::string color) {
  SVG::SVGElement element{SVG::SVGElementType::Rect};
  element.attributes.x = rect.x;
  element.attributes.y = rect.y;
  element.attributes.width = rect.width;
  element.attributes.height = rect.height;
  element.attributes.stroke_width = 0.1;
  element.attributes.stroke = color;
  element.attributes.fill = "none";
  children.push_back(element);
}

void SVG::SVGElement::add_outline_bbox() {
  const auto bbox = SVGElement::outline_bbox();
  add_rect(bbox, "blue");
}

void SVG::SVGElement::add_outline_overlap_bbox(SVG::SVGElement other,
                                               const double tolerance) {
  const auto bbox = outline_bbox();
  const auto other_bbox = other.outline_bbox();
  const auto overlap_bbox = bbox.intersection(other_bbox);
  if (overlap_bbox.width <= 0 || overlap_bbox.height <= 0) {
    return;
  }
  const auto within_tolerance =
      overlap_bbox.width <= tolerance && overlap_bbox.height <= tolerance;
  const auto color = within_tolerance ? "yellow" : "red";
  add_rect(overlap_bbox, color);
}

SVG::SVGRect SVG::SVGElement::bbox(bool throw_if_bbox_not_defined) {
  if (!m_bbox.has_value()) {
    // negative width and height bbox that will be immediately replaced by the
    // first bbox found
    m_bbox = {.x = std::numeric_limits<double>::max() / 2,
              .y = std::numeric_limits<double>::max() / 2,
              .width = std::numeric_limits<double>::lowest(),
              .height = std::numeric_limits<double>::lowest()};
    switch (elem_type) {
    case SVG::SVGElementType::Group:
      // SVG group bounding box is determined solely by its children
      break;
    case SVG::SVGElementType::Ellipse: {
      m_bbox = {
          .x = attributes.cx - attributes.rx,
          .y = attributes.cy - attributes.ry,
          .width = attributes.rx * 2,
          .height = attributes.ry * 2,
      };
      break;
    }
    case SVG::SVGElementType::Polygon:
    case SVG::SVGElementType::Polyline: {
      for (const auto &point : attributes.points) {
        m_bbox->extend(point);
      }
      break;
    }
    case SVG::SVGElementType::Path: {
      if (path_points.empty()) {
        throw std::runtime_error{"No points for 'path' element"};
      }
      for (const auto &point : path_points) {
        m_bbox->extend(point);
      }
      break;
    }
    case SVG::SVGElementType::Rect: {
      m_bbox = {
          .x = attributes.x,
          .y = attributes.y,
          .width = attributes.width,
          .height = attributes.height,
      };
      break;
    }
    case SVG::SVGElementType::Text: {
      m_bbox = text_bbox();
      break;
    }
    case SVG::SVGElementType::Title:
      // title has no size
      if (throw_if_bbox_not_defined) {
        throw std::runtime_error{"A 'title' element has no bounding box"};
      }
      break;
    default:
      throw std::runtime_error{
          std::format("Unhandled svg element type {}", tag(elem_type))};
    }

    const auto throw_if_child_bbox_is_not_defined = false;
    for (auto &child : children) {
      const auto child_bbox = child.bbox(throw_if_child_bbox_is_not_defined);
      m_bbox->extend(child_bbox);
    }
  }

  return *m_bbox;
}

SVG::SVGRect SVG::SVGElement::outline_bbox(bool throw_if_bbox_not_defined) {
  if (m_outline_bbox.has_value()) {
    return *m_outline_bbox;
  }
  // negative width and height bbox that will be immediately replaced by the
  // first bbox found
  m_bbox = {.x = std::numeric_limits<double>::max() / 2,
            .y = std::numeric_limits<double>::max() / 2,
            .width = std::numeric_limits<double>::lowest(),
            .height = std::numeric_limits<double>::lowest()};
  switch (elem_type) {
  case SVG::SVGElementType::Group:
    // SVG group bounding box is determined solely by its children
    break;
  case SVG::SVGElementType::Ellipse: {
    const auto stroke_width = (attributes.rx == 0 && attributes.ry == 0)
                                  ? 0
                                  : attributes.stroke_width;
    m_bbox = {
        .x = attributes.cx - attributes.rx - stroke_width / 2,
        .y = attributes.cy - attributes.ry - stroke_width / 2,
        .width = attributes.rx * 2 + stroke_width,
        .height = attributes.ry * 2 + stroke_width,
    };
    break;
  }
  case SVG::SVGElementType::Polygon: {
    // it takes at least 3 points to make a polygon (triangle) and Graphviz
    // always generates the last point to be the same as the first so there
    // will always be at least 4 points
    const auto &points = attributes.points;
    if (points.size() < 4) {
      throw std::runtime_error{"Too few points"};
    }
    if (points.front().x != points.back().x ||
        points.front().y != points.back().y) {
      throw std::runtime_error{"First and last point are not the same"};
    }
    if (has_all_points_equal()) {
      // the polygon has no size and will not be visible, so just arbitrarily
      // select one of its (equal) points as its outline bounding box
      const auto first_point = points.at(0);
      m_bbox->extend(first_point);
      break;
    }
    const auto clockwise = has_clockwise_points();
    // the first and last points are always the same so we skip the last
    for (auto it = points.cbegin(); it != points.cend() - 1; ++it) {
      const SVG::SVGPoint &prev_point = [&]() {
        if (it == points.begin()) {
          // the last point is the same as the first so we must use the next
          // to last one as the next point to get the start point of the
          // current path segment
          return *(points.cend() - 2);
        } else {
          return *std::prev(it);
        }
      }();
      const auto &point = *it;
      // there is always a next point since we iterate only to the next to
      // last point
      const auto &next_point = *std::next(it);
      const SVG::SVGPoints miter_shape =
          clockwise ?
                    // Graphviz draws some polygons clockwise and some
                    // counter-clockwise.
              SVGElement::miter_shape(prev_point, point, next_point)
                    :
                    // the SVG spec assumes clockwise so we swap the points
              SVGElement::miter_shape(next_point, point, prev_point);
      for (const auto &p : miter_shape) {
        m_bbox->extend(p);
      }
    }
    break;
  }
  case SVG::SVGElementType::Path: {
    if (path_points.empty()) {
      throw std::runtime_error{"No points for 'path' element"};
    }
    const auto first_point = path_points.front();
    auto is_vertical = std::all_of(
        path_points.cbegin(), path_points.cend(),
        [&](const SVGPoint &point) { return point.x == first_point.x; });
    auto is_horizontal = std::all_of(
        path_points.cbegin(), path_points.cend(),
        [&](const SVGPoint &point) { return point.y == first_point.y; });
    if (!is_vertical && !is_horizontal) {
      const std::size_t num_points_in_cylinder_node_shape_path1 = 19;
      const std::size_t num_points_in_cylinder_node_shape_path2 = 7;
      if (path_points.size() == num_points_in_cylinder_node_shape_path1 ||
          path_points.size() == num_points_in_cylinder_node_shape_path2) {
        // cylinder node shape which is flat at the extreme points so we can
        // just extend the crossing points with penwidth / 2 and exclude the
        // intermediate control points. Graphviz uses cubic splines so there are
        // always two intermediate control points between the curve segment
        // endpoints.
        const auto num_intermediate_control_points = 2;
        for (std::size_t i = 0; i < path_points.size();
             i += num_intermediate_control_points + 1) {
          const auto &point = path_points[i];
          SVG::SVGRect point_bbox = {
              .x = point.x - attributes.stroke_width / 2,
              .y = point.y - attributes.stroke_width / 2,
              .width = attributes.stroke_width,
              .height = attributes.stroke_width,
          };
          m_bbox->extend(point_bbox);
        }
        break;
      }
      const std::size_t num_points_in_curve_arrow_shape_path = 4;
      if (path_points.size() == num_points_in_curve_arrow_shape_path) {
        const auto horizontal_start = path_points[0].x;
        const auto horizontal_control1 = path_points[1].x;
        const auto horizontal_control2 = path_points[2].x;
        const auto horizontal_end = path_points[3].x;

        const auto vertical_start = path_points[0].y;
        const auto vertical_control1 = path_points[1].y;
        const auto vertical_control2 = path_points[2].y;
        const auto vertical_end = path_points[3].y;

        const auto has_horizontally_symmetric_control_points =
            horizontal_start - horizontal_control1 ==
            horizontal_control2 - horizontal_end;

        const auto has_vertically_symmetric_control_points =
            vertical_start - vertical_control1 ==
            vertical_control2 - vertical_end;

        const auto is_horizontally_mirrored =
            horizontal_start == horizontal_end &&
            horizontal_control1 == horizontal_control2 &&
            has_vertically_symmetric_control_points;

        const auto is_vertically_mirrored =
            vertical_start == vertical_end &&
            vertical_control1 == vertical_control2 &&
            has_horizontally_symmetric_control_points;

        if (is_horizontally_mirrored || is_vertically_mirrored) {
          // A bezier curve which is either vertically or horizontally mirrored
          // is flat at the center and has a negligible extension "backwards" at
          // the start and end points. The Graphviz arrow shape `curve`, which
          // is a cubic Bezier approximation of a semicircle, is a special case
          // of this. A vertical edge with a `curve` arrow head is vertically
          // mirrored and a horizontal edge with a `curve` arrow head is
          // horizontally mirrored.
          {
            const auto horizontal_endpoint_extension =
                is_vertically_mirrored ? attributes.stroke_width / 2 : 0;
            const auto vertical_endpoint_extension =
                is_horizontally_mirrored ? attributes.stroke_width / 2 : 0;
            const auto num_intermediate_control_points = 2;
            for (std::size_t i = 0; i < path_points.size();
                 i += num_intermediate_control_points + 1) {
              const auto &point = path_points[i];
              const SVG::SVGRect point_bbox = {
                  .x = point.x - horizontal_endpoint_extension,
                  .y = point.y - vertical_endpoint_extension,
                  .width = horizontal_endpoint_extension * 2,
                  .height = vertical_endpoint_extension * 2,
              };
              m_bbox->extend(point_bbox);
            }
            const SVGPoint midpoint = cubic_bezier(0.5);
            const SVG::SVGRect point_bbox = {
                .x = midpoint.x - attributes.stroke_width / 2,
                .y = midpoint.y - attributes.stroke_width / 2,
                .width = attributes.stroke_width,
                .height = attributes.stroke_width,
            };
            m_bbox->extend(point_bbox);
          }
          break;
        }
        const auto xmin = std::min(horizontal_start, horizontal_end);
        const auto xmax = std::max(horizontal_start, horizontal_end);
        const auto ymin = std::min(vertical_start, vertical_end);
        const auto ymax = std::max(vertical_start, vertical_end);
        if (horizontal_control1 >= xmin && horizontal_control1 <= xmax &&
            horizontal_control2 >= xmin && horizontal_control2 <= xmax &&
            vertical_control1 >= ymin && vertical_control1 <= ymax &&
            vertical_control2 >= ymin && vertical_control2 <= ymax) {
          // the control points are within a bounding box defined by the start
          // and end points. This means that the Bezier curve is completely
          // contained within this box, except for the extension caused by the
          // stroke width. This extension can be calculated using the angle at
          // the start and end points which is given by the closest control
          // point. The Graphviz arrow shapes `rcurve`, `lcurve`, `ricurve` and
          // `licurve` fulfills this condition.
          const auto start_dx = horizontal_start - horizontal_control1;
          const auto start_dy = vertical_start - vertical_control1;
          const auto start_hypot = std::hypot(start_dx, start_dy);
          const auto start_cos = start_dx / start_hypot;
          const auto start_sin = start_dy / start_hypot;
          const SVG::SVGPoint start_extension_left = {
              horizontal_start + attributes.stroke_width / 2 * start_sin,
              vertical_start - attributes.stroke_width / 2 * start_cos};
          const SVG::SVGPoint start_extension_right = {
              horizontal_start - attributes.stroke_width / 2 * start_sin,
              vertical_start + attributes.stroke_width / 2 * start_cos};
          m_bbox->extend(start_extension_left);
          m_bbox->extend(start_extension_right);
          const auto end_dx = horizontal_end - horizontal_control2;
          const auto end_dy = vertical_end - vertical_control2;
          const auto end_hypot = std::hypot(end_dx, end_dy);
          const auto end_cos = end_dx / end_hypot;
          const auto end_sin = end_dy / end_hypot;
          const SVG::SVGPoint end_extension_left = {
              horizontal_end + attributes.stroke_width / 2 * end_sin,
              vertical_end + -attributes.stroke_width / 2 * end_cos};
          const SVG::SVGPoint end_extension_right = {
              horizontal_end + -attributes.stroke_width / 2 * end_sin,
              vertical_end + attributes.stroke_width / 2 * end_cos};
          m_bbox->extend(end_extension_left);
          m_bbox->extend(end_extension_right);
          break;
        }
      }
      throw std::runtime_error(
          "paths other than straight vertical, straight horizontal or the "
          "cylinder special case are currently not supported");
    }
    // we now know we have a straight horizontal or vertical line (or the
    // degenerate case of a point)
    if (is_vertical) {
      const SVG::SVGRect first_point_bbox = {
          first_point.x - attributes.stroke_width / 2, first_point.y,
          attributes.stroke_width, 0};
      m_bbox->extend(first_point_bbox);
      for (const auto &point : path_points) {
        m_bbox->extend(point);
      }
    }
    if (is_horizontal) {
      for (const auto &point : path_points) {
        m_bbox->extend(point);
      }
      const SVG::SVGRect first_point_bbox = {
          first_point.x, first_point.y - attributes.stroke_width / 2, 0,
          attributes.stroke_width};
      m_bbox->extend(first_point_bbox);
    }
    break;
  }
  case SVG::SVGElementType::Polyline: {
    const auto &points = attributes.points;
    if (points.size() < 2) {
      throw std::runtime_error{"Too few points for 'polyline' element"};
    }

    // handle first point which may not be part of a corner
    {
      const auto first_point = points.front();
      const auto next_point = points[1];
      const auto dx = first_point.x - next_point.x;
      const auto dy = first_point.y - next_point.y;
      const auto hypot = std::hypot(dx, dy);
      const auto cosAlpha = dx / hypot;
      const auto sinAlpha = dy / hypot;
      // a polyline extends with half the stroke width in both perpendicular
      // directions, but not along the line at its endpoints
      const auto x_extension = std::abs(attributes.stroke_width / 2 * sinAlpha);
      const auto y_extension = std::abs(attributes.stroke_width / 2 * cosAlpha);
      const SVG::SVGRect first_point_bbox = {points.front().x - x_extension,
                                             points.front().y - y_extension,
                                             x_extension * 2, y_extension * 2};
      m_bbox->extend(first_point_bbox);
    }

    // handle last point which may not be part of a corner
    {
      const auto last_point = points.back();
      const auto prev_point = *(points.cend() - 2);
      const auto dx = last_point.x - prev_point.x;
      const auto dy = last_point.y - prev_point.y;
      const auto hypot = std::hypot(dx, dy);
      const auto cosAlpha = dx / hypot;
      const auto sinAlpha = dy / hypot;
      // a polyline extends with half the stroke width in both perpendicular
      // directions, but not along the line at its endpoints
      const auto x_extension = std::abs(attributes.stroke_width / 2 * sinAlpha);
      const auto y_extension = std::abs(attributes.stroke_width / 2 * cosAlpha);
      const SVG::SVGRect last_point_bbox = {points.back().x - x_extension,
                                            points.back().y - y_extension,
                                            x_extension * 2, y_extension * 2};
      m_bbox->extend(last_point_bbox);
    }

    if (points.size() >= 3) {
      // at least one corner
      if (has_all_points_equal()) {
        // the polyline has no size and will not be visible, so just arbitrarily
        // select one of its (equal) points as its outline bounding box
        const auto first_point = points.at(0);
        m_bbox->extend(first_point);
        break;
      }

      const auto clockwise = has_clockwise_points();
      for (auto it = points.cbegin() + 1; it < points.cend() - 1; ++it) {
        // there is always a previous point since we iterate from the second
        // point
        const auto &prev_point = *std::prev(it);
        const auto &point = *it;
        // there is always a next point since we iterate only to the next to
        // last point
        const auto &next_point = *std::next(it);
        SVG::SVGPoints miter_shape =
            // Graphviz draws some polylines clockwise and some
            // counter-clockwise.
            clockwise ? SVGElement::miter_shape(prev_point, point, next_point) :
                      // `miter_point` assumes clockwise so we swap the points
                SVGElement::miter_shape(next_point, point, prev_point);
        for (const auto &p : miter_shape) {
          m_bbox->extend(p);
        }
      }
    }
    break;
  }
  case SVG::SVGElementType::Rect:
    m_bbox = {
        .x = attributes.x - attributes.stroke_width / 2,
        .y = attributes.y - attributes.stroke_width / 2,
        .width = attributes.width + attributes.stroke_width,
        .height = attributes.height + attributes.stroke_width,
    };
    break;
  case SVG::SVGElementType::Text:
    m_bbox = text_bbox();
    break;
  case SVG::SVGElementType::Title:
    // title has no size
    if (throw_if_bbox_not_defined) {
      throw std::runtime_error{"A 'title' element has no bounding box"};
    }
    break;
  default:
    throw std::runtime_error{
        std::format("Unhandled svg element type {}", tag(elem_type))};
  }

  const auto throw_if_child_bbox_is_not_defined = false;
  for (auto &child : children) {
    const auto child_bbox =
        child.outline_bbox(throw_if_child_bbox_is_not_defined);
    m_bbox->extend(child_bbox);
  }

  return *m_bbox;
}

SVG::SVGElement &SVG::SVGElement::find_child(const SVG::SVGElementType etype,
                                             std::size_t index) {
  std::size_t i = 0;
  for (auto &child : children) {
    if (child.elem_type == etype) {
      if (i == index) {
        return child;
      }
      ++i;
    }
  }
  throw std::runtime_error(
      std::format("SVG element only has {} \"{}\" children, index {} not found",
                  i, tag(etype), index));
}

SVG::SVGRect SVG::SVGElement::text_bbox() const {
  assert(elem_type == SVG::SVGElementType::Text && "Not a 'text' element");

  if (attributes.font_family != "Courier,monospace") {
    throw std::runtime_error(
        std::format("Cannot calculate bounding box for font \"{}\"",
                    attributes.font_family));
  }

  // Empirically determined font metrics for the Courier font
  const auto courier_width_per_pt = 0.6;
  const auto courier_height_per_pt = 1.2;
  const auto descent_per_pt = 0.257;
  const auto font_width = attributes.font_size * courier_width_per_pt;
  const auto font_height = attributes.font_size * courier_height_per_pt;
  const auto descent = attributes.font_size * descent_per_pt;

  const SVG::SVGRect bbox = {
      .x = attributes.x - font_width * text.size() / 2,
      .y = attributes.y - font_height + descent,
      .width = font_width * text.size(),
      .height = font_height,
  };
  return bbox;
}

void SVG::SVGElement::append_attribute(std::string &output,
                                       const std::string &attribute) const {
  if (attribute.empty()) {
    return;
  }
  if (!output.empty()) {
    output += " ";
  }
  output += attribute;
}

static double interpolate(double y0, double y1, double x) {
  double dy = y1 - y0;

  return y0 + x * dy;
}

// The cubic Bezier implementation is taken from
// https://stackoverflow.com/a/37642695/3122101
SVG::SVGPoint SVG::SVGElement::cubic_bezier(double t) {
  assert(t >= 0);
  assert(t <= 1);
  assert(path_points.size() == 4);

  const auto x0 = path_points[0].x;
  const auto y0 = path_points[0].y;
  const auto x1 = path_points[1].x;
  const auto y1 = path_points[1].y;
  const auto x2 = path_points[2].x;
  const auto y2 = path_points[2].y;
  const auto x3 = path_points[3].x;
  const auto y3 = path_points[3].y;

  const auto xa = interpolate(x0, x1, t);
  const auto ya = interpolate(y0, y1, t);
  const auto xb = interpolate(x1, x2, t);
  const auto yb = interpolate(y1, y2, t);
  const auto xc = interpolate(x2, x3, t);
  const auto yc = interpolate(y2, y3, t);

  const auto xm = interpolate(xa, xb, t);
  const auto ym = interpolate(ya, yb, t);
  const auto xn = interpolate(xb, xc, t);
  const auto yn = interpolate(yb, yc, t);

  const auto x = interpolate(xm, xn, t);
  const auto y = interpolate(ym, yn, t);

  return {x, y};
}

bool SVG::SVGElement::has_all_points_equal() const {
  assert((elem_type == SVG::SVGElementType::Polygon ||
          elem_type == SVG::SVGElementType::Polyline) &&
         "not a polygon or polyline");
  const auto points = attributes.points;
  assert(!points.empty() > 0 && "no points");
  return std::equal(points.begin() + 1, points.end(), points.begin());
}

bool SVG::SVGElement::has_clockwise_points() const {
  assert((elem_type == SVG::SVGElementType::Polygon ||
          elem_type == SVG::SVGElementType::Polyline) &&
         "not a polygon or polyline");
  assert(attributes.points.size() >= 3 && "too few points");
  // Sum over the edges, (x2 − x1)(y2 + y1). If the result is positive, the
  // curve is clockwise, if it's negative the curve is counter-clockwise.
  // Implementation is based on https://stackoverflow.com/a/1165943/3122101
  const auto &points = attributes.points;
  double sum = 0;
  for (auto it = points.cbegin(); it < points.cend() - 1; ++it) {
    const auto &[x1, y1i] = *it;
    const auto &[x2, y2i] = *std::next(it);
    // SVG uses inverted y axis, so negate y values
    const auto y1 = -y1i;
    const auto y2 = -y2i;
    sum += (x2 - x1) * (y2 + y1);
  }
  return sum > 0;
}

std::string SVG::SVGElement::fill_attribute_to_string() const {
  if (attributes.fill.empty()) {
    return "";
  }

  return std::format(R"(fill="{}")", to_graphviz_color(attributes.fill));
}

std::string SVG::SVGElement::id_attribute_to_string() const {
  if (attributes.id.empty()) {
    return "";
  }

  return std::format(R"(id="{}")", attributes.id);
}

std::string SVG::SVGElement::fill_opacity_attribute_to_string() const {
  if (attributes.fill_opacity == 1) {
    // Graphviz doesn't set `fill-opacity` to 1 since that's the default
    return "";
  }

  if (attributes.fill_opacity == 0) {
    // Graphviz doesn't set `fill-opacity` to 0 since in that case it sets
    // `fill` to "none" instead
    return "";
  }

  return std::format(R"(fill-opacity="{}")", attributes.fill_opacity);
}

std::string SVG::SVGElement::points_attribute_to_string() const {
  std::string points_attribute_str = R"|(points=")|";
  const char *separator = "";
  for (const auto &point : attributes.points) {
    points_attribute_str += separator + std::format("{},{}", point.x, point.y);
    separator = " ";
  }
  points_attribute_str += '"';

  return points_attribute_str;
}

std::string SVG::SVGElement::stroke_attribute_to_string() const {
  if (attributes.stroke.empty()) {
    return "";
  }

  return std::format(R"(stroke="{}")",
                     stroke_to_graphviz_color(attributes.stroke));
}

std::string SVG::SVGElement::stroke_opacity_attribute_to_string() const {
  if (attributes.stroke_opacity == 1) {
    // Graphviz doesn't set `stroke-opacity` to 1 since that's the default
    return "";
  }

  if (attributes.stroke_opacity == 0) {
    // Graphviz doesn't set `stroke-opacity` to 0 since in that case it sets
    // `stroke` to "none" instead
    return "";
  }

  return std::format(R"(stroke-opacity="{}")", attributes.stroke_opacity);
}

std::string SVG::SVGElement::stroke_width_attribute_to_string() const {
  if (attributes.stroke_width == 1) {
    // Graphviz doesn't set `stroke-width` to 1 since that's the default
    return "";
  }

  return std::format(R"(stroke-width="{}")", attributes.stroke_width);
}

std::string SVG::SVGElement::to_string(std::size_t indent_size = 2) const {
  std::string output;
  output += R"(<?xml version="1.0" encoding="UTF-8" standalone="no"?>)"
            "\n";
  output += R"(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN")"
            "\n";
  output += R"( "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)"
            "\n";
  output += std::format("<!-- Generated by graphviz version {} "
                        "({})\n -->\n",
                        graphviz_version, graphviz_build_date);
  to_string_impl(output, indent_size, 0);
  return output;
}

void SVG::SVGElement::to_string_impl(std::string &output,
                                     std::size_t indent_size,
                                     std::size_t current_indent) const {
  const auto indent_str = std::string(current_indent, ' ');
  output += indent_str;

  if (elem_type == SVG::SVGElementType::Svg) {
    const auto comment = std::format("Title: {} Pages: 1", graphviz_id);
    output += std::format("<!-- {} -->\n", xml_encode(comment));
  }
  if (elem_type == SVG::SVGElementType::Group &&
      (attributes.class_ == "node" || attributes.class_ == "edge")) {
    const auto comment = graphviz_id;
    output += std::format("<!-- {} -->\n", xml_encode(comment));
  }

  output += "<";
  output += tag(elem_type);

  std::string attributes_str{};
  append_attribute(attributes_str, id_attribute_to_string());
  switch (elem_type) {
  case SVG::SVGElementType::Ellipse:
    append_attribute(attributes_str, fill_attribute_to_string());
    append_attribute(attributes_str, fill_opacity_attribute_to_string());
    append_attribute(attributes_str, stroke_attribute_to_string());
    append_attribute(attributes_str, stroke_width_attribute_to_string());
    append_attribute(attributes_str, stroke_opacity_attribute_to_string());
    attributes_str +=
        std::format(R"( cx="{}" cy="{}" rx="{}" ry="{}")", attributes.cx,
                    attributes.cy, attributes.rx, attributes.ry);
    break;
  case SVG::SVGElementType::Group:
    attributes_str += std::format(R"( class="{}")", attributes.class_);
    if (attributes.transform.has_value()) {
      const auto transform = attributes.transform;
      attributes_str += std::format(
          R"|( transform="scale({} {}) rotate({}) translate({} {})")|",
          transform->a, transform->d, transform->c, transform->e, transform->f);
    }
    break;
  case SVG::SVGElementType::Path: {
    append_attribute(attributes_str, fill_attribute_to_string());
    append_attribute(attributes_str, fill_opacity_attribute_to_string());
    append_attribute(attributes_str, stroke_attribute_to_string());
    append_attribute(attributes_str, stroke_width_attribute_to_string());
    append_attribute(attributes_str, stroke_opacity_attribute_to_string());
    attributes_str += R"|( d=")|";
    auto command = 'M';
    for (const auto &point : path_points) {
      attributes_str += std::format("{}{},{}", command, point.x, point.y);
      switch (command) {
      case 'M':
        command = 'C';
        break;
      case 'C':
        command = ' ';
        break;
      case ' ':
        break;
      default:
        UNREACHABLE();
      }
    }
    attributes_str += '"';
    break;
  }
  case SVG::SVGElementType::Polygon:
    append_attribute(attributes_str, fill_attribute_to_string());
    append_attribute(attributes_str, fill_opacity_attribute_to_string());
    append_attribute(attributes_str, stroke_attribute_to_string());
    append_attribute(attributes_str, stroke_width_attribute_to_string());
    append_attribute(attributes_str, stroke_opacity_attribute_to_string());
    append_attribute(attributes_str, points_attribute_to_string());
    break;
  case SVG::SVGElementType::Polyline:
    append_attribute(attributes_str, fill_attribute_to_string());
    append_attribute(attributes_str, stroke_attribute_to_string());
    append_attribute(attributes_str, stroke_width_attribute_to_string());
    append_attribute(attributes_str, stroke_opacity_attribute_to_string());
    append_attribute(attributes_str, points_attribute_to_string());
    break;
  case SVG::SVGElementType::Rect:
    attributes_str +=
        std::format(R"(x="{}" y="{}" width="{}" height="{}")", attributes.x,
                    attributes.y, attributes.width, attributes.height);
    append_attribute(attributes_str, fill_attribute_to_string());
    append_attribute(attributes_str, stroke_attribute_to_string());
    append_attribute(attributes_str, stroke_width_attribute_to_string());
    append_attribute(attributes_str, stroke_opacity_attribute_to_string());
    break;
  case SVG::SVGElementType::Svg:
    attributes_str += std::format(
        R"(width="{}pt" height="{}pt")"
        "\n"
        R"( viewBox="{:.2f} {:.2f} {:.2f} {:.2f}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink")",
        std::lround(px_to_pt(attributes.width)),
        std::lround(px_to_pt(attributes.height)), attributes.viewBox.x,
        attributes.viewBox.y, attributes.viewBox.width,
        attributes.viewBox.height);
    break;
  case SVG::SVGElementType::Text:
    attributes_str += std::format(
        R"(xml:space="preserve" text-anchor="{}" x="{}" y="{}" font-family="{}" font-size="{:.2f}")",
        attributes.text_anchor, attributes.x, attributes.y,
        attributes.font_family, attributes.font_size);
    break;
  case SVG::SVGElementType::Title:
    // Graphviz doesn't generate attributes on 'title' elements
    break;
  default:
    throw std::runtime_error{std::format(
        "Attributes on '{}' elements are not yet implemented", tag(elem_type))};
  }
  if (!attributes_str.empty()) {
    output += " ";
  }
  output += attributes_str;

  if (children.empty() && text.empty()) {
    output += "/>\n";
  } else {
    output += ">";
    if (!text.empty()) {
      output += xml_encode(text);
    }
    if (!children.empty()) {
      output += "\n";
      for (const auto &child : children) {
        child.to_string_impl(output, indent_size, current_indent + indent_size);
      }
      output += indent_str;
    }
    output += "</";
    output += tag(elem_type);
    output += ">\n";
  }
}

SVG::SVGPoints
SVG::SVGElement::miter_shape(SVG::SVGPoint segment_start,
                             SVG::SVGPoint segment_end,
                             SVG::SVGPoint following_segment_end) const {
  /*
   * Compute the stroke shape miter shape according to
   * https://www.w3.org/TR/SVG2/painting.html#StrokeShape.
   *
   * The spec assumes the points of a shape are given in clockwise
   * (mathematically negative) direction which is how Graphviz draws the points
   * of an arrow head. A standard arrow head looks like this:
   *
   *             1
   *             ^
   *            /◡\
   *           / θ \
   *          /     \
   *         /       \
   *        /         \
   *      0 ----------- 2
   *     (3)     |
   *             |
   *             |
   *             |
   *
   *
   * NOTE: Graphviz draws node shapes in the opposite direction, i.e., in
   * counter-clockwise (mathematically positive) direction which means that such
   * points must be reordered before calling this method.
   *
   * See https://www.w3.org/TR/SVG2/painting.html#TermLineJoinShape for how the
   * terminating line join shape should be calculated. Below is an attempt to
   * copy the diagram from the spec with some details added
   *
   *
   *                   P3
   *                   /\
   *                  .  .
   *              l  .    .
   *                .      .
   *               .        .
   *            P1           . P2
   *             /˙·.ٜ  P ٜ .·˙ \
   *            /      /\      \
   *           /      /◟◞\      \
   *          /      / θ  \      \
   *         /      /      \      \
   *  Aleft /    A /        \ B    \ Bleft
   *       /      /          \      \
   *      /      /            \      \
   *     /      / α    /\      \      \  β-π
   * .../....../◝...../..\.....◜ ◝.....\◝.....
   *   /      /      /    \    ◟ \      \
   *         /      /      \   β  \
   *               /        \
   *            Aright     Bright
   *
   * A is the current segment that ends in P.
   * B is the following segment that starts in P.
   *
   * θ is the angle between the A segment and the B segment in the reverse
   * direction
   *
   * α is the angle of the A segment to the x-axis.
   *
   * β is the angle of the B segment to the x-axis.
   *     NOTE: In the diagram above, the B segment crosses the x-axis in the
   *           downwards direction so its angle to the x-axis is in this case
   *           larger than a semi-circle (π or 180°). In the picture it is
   *           around 5π/3 or 300°. The B segment in the opposite direction has
   *           an angle to the x-axis which is β-π. This is denoted next to the
   *           Bleft line in the picture.
   *
   * π is the number pi ≃ 3.14
   *
   * l is the calculated length between P1 and P3.
   *
   * The distance between P and P1 and between P and P2 is stroke-width / 2.
   *
   * NOTE: This method only implements the 'miter' join, but falls back to
   * 'bevel' when stroke-miterlimit is exceeded.
   */

  if (segment_start == segment_end || segment_end == following_segment_end) {
    // the stroke shape is really a point so we just return this point without
    // extending it with stroke width in any direction, which seems to be the
    // way SVG renderers render this.
    return {segment_end};
  }

  const auto stroke_width = attributes.stroke_width;

  // SVG has inverted y axis so invert all y values before use
  const SVG::SVGPoint P = {segment_end.x, -segment_end.y};
  const SVG::SVGLine A = {segment_start.x, -segment_start.y, segment_end.x,
                          -segment_end.y};
  const SVG::SVGLine B = {segment_end.x, -segment_end.y,
                          following_segment_end.x, -following_segment_end.y};

  const auto dxA = A.x2 - A.x1;
  const auto dyA = A.y2 - A.y1;
  const auto hypotA = std::hypot(dxA, dyA);
  const auto cosAlpha = dxA / hypotA;
  const auto sinAlpha = dyA / hypotA;
  const auto alpha = dyA > 0 ? std::acos(cosAlpha) : -std::acos(cosAlpha);

  const SVG::SVGPoint P1 = {P.x - stroke_width / 2.0 * sinAlpha,
                            P.y + stroke_width / 2.0 * cosAlpha};

  const auto dxB = B.x2 - B.x1;
  const auto dyB = B.y2 - B.y1;
  const auto hypotB = std::hypot(dxB, dyB);
  const auto cosBeta = dxB / hypotB;
  const auto beta = dyB > 0 ? std::acos(cosBeta) : -std::acos(cosBeta);

  const auto sinBeta = dyB / hypotB;
  const auto sinBetaMinusPi = -sinBeta;
  const auto cosBetaMinusPi = -cosBeta;
  const SVG::SVGPoint P2 = {P.x + stroke_width / 2.0 * sinBetaMinusPi,
                            P.y - stroke_width / 2.0 * cosBetaMinusPi};

  // angle between the A segment and the B segment in the reverse direction
  const auto beta_rev = beta - std::numbers::pi;
  const auto theta = beta_rev - alpha;

  SVG::SVGPoints line_join_shape;
  const auto miter_limit = 4.0;
  const auto miter_length = stroke_width / std::sin(std::abs(theta) / 2.0);

  // add the bevel, which is is the triangle formed from the three points P, P1
  // and P2, to the line join shape. SVG has inverted y axis so invert the
  // returned y value
  line_join_shape.emplace_back(P.x, -P.y);
  line_join_shape.emplace_back(P1.x, -P1.y);
  line_join_shape.emplace_back(P2.x, -P2.y);

  if (miter_length > miter_limit * stroke_width) {
    // fall back to bevel only
    return line_join_shape;
  }

  // length between P1 and P3 (and between P2 and P3)
  const auto l = stroke_width / 2.0 / std::tan(theta / 2.0);

  const SVG::SVGPoint P3 = {P1.x + l * cosAlpha, P1.y + l * sinAlpha};

  // SVG has inverted y axis so invert the returned y value
  line_join_shape.emplace_back(P3.x, -P3.y);
  return line_join_shape;
}

std::string
SVG::SVGElement::stroke_to_graphviz_color(const std::string &color) const {
  return to_graphviz_color(color);
}

void SVG::SVGRect::extend(const SVGPoint &point) {
  const auto xmin = std::min(x, point.x);
  const auto ymin = std::min(y, point.y);
  const auto xmax = std::max(x + width, point.x);
  const auto ymax = std::max(y + height, point.y);

  x = xmin;
  y = ymin;
  width = xmax - xmin;
  height = ymax - ymin;
}

SVG::SVGPoint SVG::SVGRect::center() const {
  return {x + width / 2, y + height / 2};
}

SVG::SVGRect SVG::SVGRect::intersection(SVG::SVGRect other) const {
  const SVG::SVGLine intersection_diagonal = {
      std::max(x, other.x), std::max(y, other.y),
      std::min(x + width, other.x + other.width),
      std::min(y + height, other.y + other.height)};

  return SVG::SVGRect{
      .x = intersection_diagonal.x1,
      .y = intersection_diagonal.y1,
      .width = intersection_diagonal.x2 - intersection_diagonal.x1,
      .height = intersection_diagonal.y2 - intersection_diagonal.y1};
}

void SVG::SVGRect::extend(const SVG::SVGRect &other) {
  const auto xmin = std::min(x, other.x);
  const auto ymin = std::min(y, other.y);
  const auto xmax = std::max(x + width, other.x + other.width);
  const auto ymax = std::max(y + height, other.y + other.height);

  x = xmin;
  y = ymin;
  width = xmax - xmin;
  height = ymax - ymin;
}

std::string_view SVG::tag(SVGElementType elem_type) {
  switch (elem_type) {
  case SVG::SVGElementType::Circle:
    return "circle";
  case SVG::SVGElementType::Ellipse:
    return "ellipse";
  case SVG::SVGElementType::Group:
    return "g";
  case SVG::SVGElementType::Line:
    return "line";
  case SVG::SVGElementType::Path:
    return "path";
  case SVG::SVGElementType::Polygon:
    return "polygon";
  case SVG::SVGElementType::Polyline:
    return "polyline";
  case SVG::SVGElementType::Rect:
    return "rect";
  case SVG::SVGElementType::Svg:
    return "svg";
  case SVG::SVGElementType::Text:
    return "text";
  case SVG::SVGElementType::Title:
    return "title";
  }
  UNREACHABLE();
}

bool SVG::SVGPoint::operator==(const SVGPoint &rhs) const {
  return x == rhs.x && y == rhs.y;
}

bool SVG::SVGPoint::operator!=(const SVGPoint &rhs) const {
  return !(*this == rhs);
}

bool SVG::SVGPoint::is_higher_than(const SVGPoint &other) const {
  // SVG uses inverted y axis, so smaller is higher
  return y < other.y;
}

bool SVG::SVGPoint::is_lower_than(const SVGPoint &other) const {
  // SVG uses inverted y axis, so larger is lower
  return y > other.y;
}

bool SVG::SVGPoint::is_more_left_than(const SVGPoint &other) const {
  return x < other.x;
}

bool SVG::SVGPoint::is_more_right_than(const SVGPoint &other) const {
  return x > other.x;
}