excalidraw-render 0.1.0

Render Excalidraw drawings to SVG, PNG and PDF in pure Rust, without a browser
Documentation
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
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
//! The SVG document writer: `SvgPlan::write` spells the `<svg>` root, the
//! metadata comments and `<metadata>` carrier, `<defs>` with the frame and
//! image clips, the shared image `<symbol>`s, the dark-mode image filters and
//! the embedded font style, then every `SvgElement` in paint order with its
//! frame-clip group, link anchor, Rough paths spelled through
//! `push_rough_number`, freedraw outlines through the ECMAScript reduction,
//! laid-out text as per-run `<tspan>`s with source text beside the glyphs,
//! images as `<use>` references inside their crop viewports, and
//! compatibility placeholders. Every helper here takes any `SvgSink`, so one
//! pass counts and the next writes.

use crate::color::CanonicalColor;
use crate::normalization::NormalizedElementSource;

use super::super::geometry::{
    Path, PathOperation, Point, FRAME_OUTLINE_STROKE_WIDTH, FRAME_RADIUS,
};
use super::super::metadata::{operation, ArtifactMetadata, Stage};
use super::super::number_format::{ecmascript_number, push_rough_number, push_svg_number};
use super::super::resolve::{
    decoration_fill_color, decoration_stroke_style, ResolvedClip, StrokeStyle,
};
use super::super::rough::{Drawing, FillKind};
use super::super::svg_fonts::Bundle;
use super::super::text::{ResolvedLabel, ResolvedTextAlign, ResolvedTextRun, FRAME_NAME_FONT_SIZE};
use super::super::{freedraw, FormatEncodeError};
use super::sink::SvgSink;
use super::{
    SvgElement, SvgElementKind, SvgFrameName, SvgImageSymbol, SvgPlan,
    IMAGE_INVERT_FILTER_DEFINITION, IMAGE_INVERT_FILTER_ID, THEME_FILTER,
};

/// Every frame clip the plan references, in first-use order, each defined once
/// in `<defs>` and referenced by every child it clips.
fn frame_clip_definitions(
    elements: &[SvgElement],
) -> Result<Vec<&ResolvedClip>, FormatEncodeError> {
    let mut definitions = Vec::<&ResolvedClip>::new();
    for clip in elements.iter().flat_map(|element| &element.frame_clips) {
        if !definitions
            .iter()
            .any(|known| known.frame_source_index == clip.frame_source_index)
        {
            definitions
                .try_reserve(1)
                .map_err(|_| FormatEncodeError::MemoryUnavailable {
                    subject_kind: "format_plan",
                    subject: "svg_frame_clip_definitions",
                })?;
            definitions.push(clip);
        }
    }
    Ok(definitions)
}

fn push_frame_clip_definition<W: SvgSink + ?Sized>(
    svg: &mut W,
    clip: &ResolvedClip,
) -> Result<(), FormatEncodeError> {
    svg.push_str("<clipPath id=\"");
    push_frame_clip_id(svg, clip)?;
    svg.push_str("\"><rect transform=\"translate(");
    push_svg_number(
        svg,
        clip.transform.translation_x,
        "SVG frame clip translation x",
    )?;
    svg.push(' ');
    push_svg_number(
        svg,
        clip.transform.translation_y,
        "SVG frame clip translation y",
    )?;
    svg.push_str(") rotate(");
    push_svg_number(
        svg,
        180.0 * clip.transform.angle_radians / std::f64::consts::PI,
        "SVG frame clip rotation",
    )?;
    svg.push(' ');
    push_svg_number(svg, clip.transform.center_x, "SVG frame clip center x")?;
    svg.push(' ');
    push_svg_number(svg, clip.transform.center_y, "SVG frame clip center y")?;
    svg.push_str(")\" width=\"");
    push_svg_number(svg, clip.width, "SVG frame clip width")?;
    svg.push_str("\" height=\"");
    push_svg_number(svg, clip.height, "SVG frame clip height")?;
    svg.push('"');
    push_svg_corner_radius(svg, clip.radius)?;
    svg.push_str("/></clipPath>");
    Ok(())
}

/// `rx`/`ry` for a rounded clip rectangle: a frame's in a scene export, or an
/// image's corner clip. The rectangular frame-export clip has none, as in the
/// reference.
fn push_svg_corner_radius<W: SvgSink + ?Sized>(
    svg: &mut W,
    radius: f64,
) -> Result<(), FormatEncodeError> {
    if radius > 0.0 {
        svg.push_str(" rx=\"");
        push_svg_number(svg, radius, "SVG clip corner radius")?;
        svg.push_str("\" ry=\"");
        push_svg_number(svg, radius, "SVG clip corner radius")?;
        svg.push('"');
    }
    Ok(())
}

fn push_frame_clip_prefix<W: SvgSink + ?Sized>(
    svg: &mut W,
    clips: &[ResolvedClip],
) -> Result<(), FormatEncodeError> {
    for clip in clips {
        svg.push_str("<g clip-path=\"url(#");
        push_frame_clip_id(svg, clip)?;
        svg.push_str(")\">");
    }
    Ok(())
}

/// The frame name as the reference's synthesized text element: an unrotated
/// group at the label box's top-left corner holding one left-aligned 14 px
/// line on Helvetica's metadata baseline, in the theme's frame-name color.
fn push_frame_name<W: SvgSink + ?Sized>(
    svg: &mut W,
    source: &NormalizedElementSource,
    name: &SvgFrameName,
) -> Result<(), FormatEncodeError> {
    let SvgFrameName {
        label,
        transform,
        color,
    } = name;
    svg.push_str("<g transform=\"translate(");
    push_svg_number(svg, transform.translation_x, "SVG frame name translation x")?;
    svg.push(' ');
    push_svg_number(svg, transform.translation_y, "SVG frame name translation y")?;
    svg.push_str(") rotate(");
    push_svg_number(svg, transform.rotation_degrees, "SVG frame name rotation")?;
    svg.push(' ');
    push_svg_number(svg, transform.center_x, "SVG frame name center x")?;
    svg.push(' ');
    push_svg_number(svg, transform.center_y, "SVG frame name center y")?;
    svg.push_str(")\"><text data-frame-name=\"");
    push_escaped_attribute(svg, source.id());
    svg.push_str("\" data-source-text=\"");
    push_escaped_attribute(svg, &label.text);
    svg.push_str("\" x=\"");
    if label.glyphs.is_empty() {
        svg.push('0');
    } else {
        for (index, glyph) in label.glyphs.iter().enumerate() {
            if index != 0 {
                svg.push(' ');
            }
            push_svg_number(svg, glyph.x, "SVG frame name glyph x")?;
        }
    }
    svg.push_str("\" y=\"");
    if label.glyphs.is_empty() {
        push_svg_number(svg, label.baseline, "SVG frame name baseline")?;
    } else {
        for (index, glyph) in label.glyphs.iter().enumerate() {
            if index != 0 {
                svg.push(' ');
            }
            push_svg_number(svg, glyph.y, "SVG frame name glyph y")?;
        }
    }
    svg.push_str("\" font-family=\"");
    push_escaped_attribute(svg, Bundle::font_family_list("Liberation Sans"));
    svg.push_str("\" font-size=\"");
    push_svg_number(svg, FRAME_NAME_FONT_SIZE, "SVG frame name font size")?;
    svg.push_str("\" fill=\"");
    color.write_hex(svg).map_err(|_| {
        FormatEncodeError::Encoding("failed to write SVG frame name color".to_owned())
    })?;
    svg.push_str("\" text-anchor=\"start\">");
    push_escaped_text(svg, &label.display_text);
    svg.push_str("</text></g>");
    Ok(())
}

fn push_svg_link_hit_rect<W: SvgSink + ?Sized>(
    svg: &mut W,
    link: Option<&str>,
    width: f64,
    height: f64,
) -> Result<(), FormatEncodeError> {
    if link.is_none() {
        return Ok(());
    }
    svg.push_str("<rect x=\"0\" y=\"0\" width=\"");
    push_svg_number(svg, width, "SVG link hit width")?;
    svg.push_str("\" height=\"");
    push_svg_number(svg, height, "SVG link hit height")?;
    svg.push_str("\" fill=\"transparent\"/>");
    Ok(())
}

/// Writes one glyph run's ` x="..." y="..."` with one value per character of
/// the run's `<tspan>` content. SVG assigns the nth value to the nth character
/// of the content, which is in logical order, while the line's glyphs sit in
/// visual order; the run's logical glyph mapping bridges the two, so a
/// right-to-left run lists its positions from its rightmost glyph down to its
/// leftmost. `offset_y` shifts every `y`; `subject` names the run kind in the
/// error for a mapping outside the line's glyphs.
fn push_svg_run_positions<W: SvgSink + ?Sized>(
    svg: &mut W,
    line: &ResolvedLabel,
    run: &ResolvedTextRun,
    offset_y: f64,
    subject: &str,
) -> Result<(), FormatEncodeError> {
    let glyph = |index: &usize| {
        line.glyphs.get(*index).ok_or_else(|| {
            FormatEncodeError::Encoding(format!("{subject} has invalid glyph bounds"))
        })
    };
    svg.push_str(" x=\"");
    for (position, index) in run.logical_glyphs.iter().enumerate() {
        if position != 0 {
            svg.push(' ');
        }
        push_svg_number(svg, glyph(index)?.x, "SVG run glyph x")?;
    }
    svg.push_str("\" y=\"");
    for (position, index) in run.logical_glyphs.iter().enumerate() {
        if position != 0 {
            svg.push(' ');
        }
        push_svg_number(svg, glyph(index)?.y + offset_y, "SVG run glyph y")?;
    }
    svg.push('"');
    Ok(())
}

/// The clip's identity is its frame's, so every child of one frame references
/// the one definition. The source index is renderer-owned: an element id is
/// untrusted input and need not be a usable XML ID.
fn push_frame_clip_id<W: SvgSink + ?Sized>(
    svg: &mut W,
    clip: &ResolvedClip,
) -> Result<(), FormatEncodeError> {
    write!(svg, "frame-clip-{}", clip.frame_source_index)
        .map_err(|_| FormatEncodeError::Encoding("failed to write SVG frame clip ID".to_owned()))
}

/// An image's rounded-corner clip, keyed like a frame clip by the image's
/// source index rather than its untrusted element id. The reference defines
/// the clip as a `<clipPath>` holding a `<rect>` of the element's size with
/// `getCornerRadius(min(width, height))` corners and references it from the
/// image's translated and rotated group, so the clip sits in element space,
/// outside the crop viewport and the flip. The plan defines it in `<defs>`
/// beside the frame clips, in paint order.
fn push_image_clip_id<W: SvgSink + ?Sized>(
    svg: &mut W,
    source: &NormalizedElementSource,
) -> Result<(), FormatEncodeError> {
    write!(svg, "image-clip-{}", source.source_index())
        .map_err(|_| FormatEncodeError::Encoding("failed to write SVG image clip ID".to_owned()))
}

fn push_image_clip_definition<W: SvgSink + ?Sized>(
    svg: &mut W,
    source: &NormalizedElementSource,
    width: f64,
    height: f64,
    radius: f64,
) -> Result<(), FormatEncodeError> {
    svg.push_str("<clipPath id=\"");
    push_image_clip_id(svg, source)?;
    svg.push_str("\"><rect width=\"");
    push_svg_number(svg, width, "SVG image clip width")?;
    svg.push_str("\" height=\"");
    push_svg_number(svg, height, "SVG image clip height")?;
    svg.push('"');
    push_svg_corner_radius(svg, radius)?;
    svg.push_str("/></clipPath>");
    Ok(())
}

/// A shared image's id, keyed like a clip by the source index of the first
/// element in paint order that draws the asset (see [`SvgImageSymbol`]).
fn push_image_symbol_id<W: SvgSink + ?Sized>(
    svg: &mut W,
    source_index: usize,
) -> Result<(), FormatEncodeError> {
    write!(svg, "image-{source_index}")
        .map_err(|_| FormatEncodeError::Encoding("failed to write SVG image symbol ID".to_owned()))
}

/// The reference's shared image definition: a `<symbol>` holding the asset's
/// data URL once as an `<image>` that fills whatever viewport the referencing
/// `<use>` sizes, with the reference's `preserveAspectRatio="none"`. The
/// symbol carries no `overflow` attribute, like the reference's, so its
/// viewport clips as a symbol's does by default (#164): a nested-SVG payload
/// whose content overshoots its own viewBox is clipped to the image viewport
/// under every viewer, as the reference canvas clips it, and the document
/// rasterizes like the official one under the same viewer. The cropped image's
/// mask and `<use>` viewport stay the nested `<svg overflow="hidden">`
/// elements around the `<use>` (spec 8.2).
fn push_image_symbol_definition<W: SvgSink + ?Sized>(
    svg: &mut W,
    symbol: &SvgImageSymbol,
) -> Result<(), FormatEncodeError> {
    svg.push_str("<symbol id=\"");
    push_image_symbol_id(svg, symbol.source_index)?;
    svg.push_str("\"><image href=\"data:");
    svg.push_str(symbol.asset.media_type);
    svg.push_str(";base64,");
    push_base64(svg, &symbol.asset.source_bytes)?;
    svg.push_str("\" preserveAspectRatio=\"none\" width=\"100%\" height=\"100%\"/></symbol>");
    Ok(())
}

impl SvgPlan {
    pub(super) fn write<W: SvgSink + ?Sized>(
        &self,
        mut svg: &mut W,
    ) -> Result<(), FormatEncodeError> {
        svg.push_str("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 ");
        push_svg_number(svg, self.logical_width, "SVG width")?;
        svg.push(' ');
        push_svg_number(svg, self.logical_height, "SVG height")?;
        svg.push_str("\" width=\"");
        push_svg_number(svg, self.output_width, "scaled SVG width")?;
        svg.push_str("\" height=\"");
        push_svg_number(svg, self.output_height, "scaled SVG height")?;
        if self.dark_mode {
            svg.push_str("\" filter=\"");
            svg.push_str(THEME_FILTER);
        }
        svg.push_str("\">");
        let scene_wrapper = self
            .metadata
            .as_ref()
            .and_then(ArtifactMetadata::scene_wrapper);
        let manifest = self.metadata.as_ref().and_then(ArtifactMetadata::manifest);
        svg.push_str("<!-- svg-source:excalidraw --><metadata>");
        if let Some(wrapper) = scene_wrapper {
            operation(Stage::SceneEmbedding, || {
                svg.push_str(concat!(
                    "<!-- payload-type:application/vnd.excalidraw+json -->",
                    "<!-- payload-version:2 --><!-- payload-start -->"
                ));
                push_base64(svg, wrapper)?;
                svg.push_str("<!-- payload-end -->");
                Ok(())
            })?;
        }
        if let Some(manifest) = manifest {
            operation(Stage::ManifestEmbedding, || {
                svg.push_str("<excalidraw-render:manifest xmlns:excalidraw-render=\"urn:excalidraw-render:artifact-manifest:1\">");
                push_escaped_text(svg, manifest);
                svg.push_str("</excalidraw-render:manifest>");
                Ok(())
            })?;
        }
        svg.push_str("</metadata><defs>");
        for clip in frame_clip_definitions(&self.elements)? {
            push_frame_clip_definition(&mut svg, clip)?;
        }
        for element in &self.elements {
            if let SvgElementKind::Image {
                source,
                corner_radius: Some(radius),
                ..
            } = &element.kind
            {
                push_image_clip_definition(
                    &mut svg,
                    source,
                    element.width,
                    element.height,
                    *radius,
                )?;
            }
        }
        for symbol in &self.image_symbols {
            push_image_symbol_definition(&mut svg, symbol)?;
        }
        if self.uses_image_invert_filter() {
            svg.push_str(IMAGE_INVERT_FILTER_DEFINITION);
        }
        self.font_bundle.write_style(svg, push_base64)?;
        svg.push_str("</defs>");
        if let Some(background) = &self.background {
            svg.push_str("<rect x=\"0\" y=\"0\" width=\"");
            push_svg_number(&mut svg, self.logical_width, "SVG background width")?;
            svg.push_str("\" height=\"");
            push_svg_number(&mut svg, self.logical_height, "SVG background height")?;
            svg.push_str("\" fill=\"");
            background.write_hex(&mut svg).map_err(|_| {
                FormatEncodeError::Encoding("failed to write SVG background color".to_owned())
            })?;
            if background.alpha() != 1.0 {
                svg.push_str("\" fill-opacity=\"");
                push_svg_number(&mut svg, background.alpha(), "SVG background alpha")?;
            }
            svg.push_str("\"/>");
        }
        for (element_index, element) in self.elements.iter().enumerate() {
            let SvgElement {
                frame_clips,
                transform,
                opacity,
                link,
                width,
                height,
                kind,
            } = element;
            let frame_clip_count = frame_clips.len();
            // The name precedes the frame as its own element, outside the
            // frame's clip and link, where the reference export puts it.
            if let SvgElementKind::Frame {
                source,
                name: Some(name),
                ..
            } = kind
            {
                push_frame_name(&mut svg, source, name)?;
            }
            push_frame_clip_prefix(&mut svg, frame_clips)?;
            if let Some(link) = link {
                svg.push_str("<a href=\"");
                push_escaped_attribute(&mut svg, link);
                svg.push_str("\">");
            }
            match kind {
                SvgElementKind::RoughOutline {
                    source,
                    drawing,
                    stroke_color,
                    fill_color,
                    stroke_width,
                    stroke_style,
                    source_stroke_width,
                    background_color,
                    label_mask,
                    generated_label,
                    generated_label_color,
                    generated_label_offset_y,
                } => {
                    if source.id().is_empty() || source.pointer().is_empty() {
                        return Err(FormatEncodeError::Encoding(
                            "SVG plan outline lost its source identity".to_owned(),
                        ));
                    }
                    if let Some(mask) = label_mask {
                        svg.push_str("<defs><mask id=\"arrow-label-mask-");
                        write!(svg, "{element_index}").map_err(|_| {
                            FormatEncodeError::Encoding(
                                "failed to write SVG arrow label mask ID".to_owned(),
                            )
                        })?;
                        svg.push_str("\" maskUnits=\"userSpaceOnUse\" x=\"0\" y=\"0\" width=\"");
                        push_svg_number(&mut svg, self.logical_width, "SVG label mask width")?;
                        svg.push_str("\" height=\"");
                        push_svg_number(&mut svg, self.logical_height, "SVG label mask height")?;
                        svg.push_str(
                            "\"><rect width=\"100%\" height=\"100%\" fill=\"white\"/><rect x=\"",
                        );
                        push_svg_number(&mut svg, mask.x, "SVG label mask x")?;
                        svg.push_str("\" y=\"");
                        push_svg_number(&mut svg, mask.y, "SVG label mask y")?;
                        svg.push_str("\" width=\"");
                        push_svg_number(&mut svg, mask.width, "SVG label mask rectangle width")?;
                        svg.push_str("\" height=\"");
                        push_svg_number(&mut svg, mask.height, "SVG label mask rectangle height")?;
                        svg.push_str(
                            "\" fill=\"black\"/></mask></defs><g mask=\"url(#arrow-label-mask-",
                        );
                        write!(svg, "{element_index}").map_err(|_| {
                            FormatEncodeError::Encoding(
                                "failed to write SVG arrow label mask reference".to_owned(),
                            )
                        })?;
                        svg.push_str(")\">");
                    }
                    svg.push_str("<g stroke-linecap=\"round\"");
                    svg.push_str(" transform=\"translate(");
                    push_svg_number(&mut svg, transform.translation_x, "SVG translation x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.translation_y, "SVG translation y")?;
                    svg.push_str(") rotate(");
                    push_svg_number(&mut svg, transform.rotation_degrees, "SVG rotation")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_x, "SVG rotation center x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_y, "SVG rotation center y")?;
                    svg.push_str(")\"");
                    svg.push('>');
                    push_svg_link_hit_rect(&mut svg, link.as_deref(), *width, *height)?;
                    push_svg_drawing(
                        &mut svg,
                        drawing,
                        *stroke_color,
                        *fill_color,
                        *stroke_width,
                        *stroke_style,
                        *source_stroke_width,
                        *opacity,
                    )?;
                    for decoration in &drawing.decorations {
                        let decoration_fill =
                            decoration_fill_color(decoration, *stroke_color, *background_color);
                        let (decoration_style, decoration_stroke_width) =
                            decoration_stroke_style(decoration, *source_stroke_width);
                        push_svg_drawing(
                            &mut svg,
                            &decoration.drawing,
                            *stroke_color,
                            Some(decoration_fill),
                            *stroke_width,
                            decoration_style,
                            decoration_stroke_width,
                            *opacity,
                        )?;
                    }
                    if let (Some(label), Some(color)) = (generated_label, generated_label_color) {
                        svg.push_str("<text data-source-text=\"");
                        for line in &label.lines {
                            push_escaped_attribute(&mut svg, &line.text);
                        }
                        svg.push_str("\" xml:space=\"preserve\" fill=\"");
                        color.write_hex(&mut svg).map_err(|_| {
                            FormatEncodeError::Encoding(
                                "failed to write SVG embeddable label color".to_owned(),
                            )
                        })?;
                        svg.push_str("\" font-family=\"");
                        push_escaped_attribute(
                            &mut svg,
                            Bundle::font_family_list("Liberation Sans"),
                        );
                        svg.push_str("\" font-size=\"");
                        push_svg_number(&mut svg, label.font_size, "SVG embeddable font size")?;
                        svg.push_str("\">");
                        for line in &label.lines {
                            if !line.runs.is_empty() {
                                for run in &line.runs {
                                    svg.push_str("<tspan font-family=\"");
                                    push_escaped_attribute(
                                        &mut svg,
                                        Bundle::font_family_list(run.font_family),
                                    );
                                    svg.push_str(if run.rtl {
                                        "\" direction=\"rtl\""
                                    } else {
                                        "\" direction=\"ltr\""
                                    });
                                    push_svg_run_positions(
                                        &mut svg,
                                        line,
                                        run,
                                        *generated_label_offset_y,
                                        "SVG embeddable run",
                                    )?;
                                    svg.push('>');
                                    push_escaped_text(&mut svg, &run.display_text);
                                    svg.push_str("</tspan>");
                                }
                                continue;
                            }
                            svg.push_str("<tspan x=\"");
                            for (index, glyph) in line.glyphs.iter().enumerate() {
                                if index != 0 {
                                    svg.push(' ');
                                }
                                push_svg_number(&mut svg, glyph.x, "SVG embeddable glyph x")?;
                            }
                            svg.push_str("\" y=\"");
                            if line.glyphs.is_empty() {
                                push_svg_number(
                                    &mut svg,
                                    line.baseline + generated_label_offset_y,
                                    "SVG embeddable baseline",
                                )?;
                            } else {
                                for (index, glyph) in line.glyphs.iter().enumerate() {
                                    if index != 0 {
                                        svg.push(' ');
                                    }
                                    push_svg_number(
                                        &mut svg,
                                        glyph.y + generated_label_offset_y,
                                        "SVG embeddable glyph y",
                                    )?;
                                }
                            }
                            svg.push_str("\">");
                            push_escaped_text(&mut svg, &line.display_text);
                            svg.push_str("</tspan>");
                        }
                        svg.push_str("</text>");
                    }
                    svg.push_str("</g>");
                    if label_mask.is_some() {
                        svg.push_str("</g>");
                    }
                }
                SvgElementKind::Freedraw {
                    source,
                    path_data,
                    background_fill,
                    color,
                    background_color,
                    source_stroke_width,
                } => {
                    if source.id().is_empty() || source.pointer().is_empty() {
                        return Err(FormatEncodeError::Encoding(
                            "SVG plan freedraw lost its source identity".to_owned(),
                        ));
                    }
                    svg.push_str("<g transform=\"translate(");
                    push_svg_number(
                        &mut svg,
                        transform.translation_x,
                        "SVG freedraw translation x",
                    )?;
                    svg.push(' ');
                    push_svg_number(
                        &mut svg,
                        transform.translation_y,
                        "SVG freedraw translation y",
                    )?;
                    svg.push_str(") rotate(");
                    push_svg_number(
                        &mut svg,
                        transform.rotation_degrees,
                        "SVG freedraw rotation",
                    )?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_x, "SVG freedraw center x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_y, "SVG freedraw center y")?;
                    svg.push_str(")\">");
                    push_svg_link_hit_rect(&mut svg, link.as_deref(), *width, *height)?;
                    if let (Some(fill), Some(background_color)) =
                        (background_fill, background_color)
                    {
                        svg.push_str("<path d=\"");
                        push_rough_path(&mut svg, &fill.path)?;
                        match fill.kind {
                            FillKind::Solid => {
                                svg.push_str("\" stroke=\"none\" fill=\"");
                                background_color.write_hex(&mut svg).map_err(|_| {
                                    FormatEncodeError::Encoding(
                                        "failed to write SVG freedraw background".to_owned(),
                                    )
                                })?;
                                svg.push_str("\" fill-rule=\"evenodd\"");
                                let fill_opacity = background_color.alpha() * opacity;
                                if fill_opacity != 1.0 {
                                    svg.push_str(" fill-opacity=\"");
                                    push_svg_number(
                                        &mut svg,
                                        fill_opacity,
                                        "SVG freedraw background opacity",
                                    )?;
                                    svg.push('"');
                                }
                            }
                            FillKind::Sketch => {
                                svg.push_str("\" stroke=\"");
                                background_color.write_hex(&mut svg).map_err(|_| {
                                    FormatEncodeError::Encoding(
                                        "failed to write SVG freedraw sketch fill".to_owned(),
                                    )
                                })?;
                                svg.push_str("\" stroke-width=\"");
                                push_svg_number(
                                    &mut svg,
                                    source_stroke_width / 2.0,
                                    "SVG freedraw fill weight",
                                )?;
                                svg.push_str("\" fill=\"none\"");
                                let fill_opacity = background_color.alpha() * opacity;
                                if fill_opacity != 1.0 {
                                    svg.push_str(" stroke-opacity=\"");
                                    push_svg_number(
                                        &mut svg,
                                        fill_opacity,
                                        "SVG freedraw sketch opacity",
                                    )?;
                                    svg.push('"');
                                }
                            }
                        }
                        svg.push_str("/> ");
                    }
                    svg.push_str("<path d=\"");
                    svg.push_str(path_data);
                    svg.push_str("\" fill=\"");
                    color.write_hex(&mut svg).map_err(|_| {
                        FormatEncodeError::Encoding("failed to write SVG freedraw color".to_owned())
                    })?;
                    let effective_opacity = color.alpha() * opacity;
                    if effective_opacity != 1.0 {
                        svg.push_str("\" fill-opacity=\"");
                        push_svg_number(
                            &mut svg,
                            effective_opacity,
                            "SVG freedraw effective opacity",
                        )?;
                    }
                    svg.push_str("\"/></g>");
                }
                SvgElementKind::Frame {
                    source,
                    name: _,
                    outline_stroke_color,
                } => {
                    svg.push_str("<g transform=\"translate(");
                    push_svg_number(&mut svg, transform.translation_x, "SVG frame translation x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.translation_y, "SVG frame translation y")?;
                    svg.push_str(") rotate(");
                    push_svg_number(&mut svg, transform.rotation_degrees, "SVG frame rotation")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_x, "SVG frame center x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_y, "SVG frame center y")?;
                    // No opacity: the reference SVG's frame `<rect>` carries
                    // none, whatever the element's opacity (spec 8.1).
                    svg.push_str(")\">");
                    push_svg_link_hit_rect(&mut svg, link.as_deref(), *width, *height)?;
                    if let Some(stroke_color) = outline_stroke_color {
                        // The reference outline: the frame rectangle with
                        // `FRAME_STYLE.radius`, `strokeColor` and `strokeWidth`.
                        svg.push_str("<rect data-frame-outline=\"");
                        push_escaped_attribute(&mut svg, source.id());
                        svg.push_str("\" width=\"");
                        push_svg_number(&mut svg, *width, "SVG frame outline width")?;
                        svg.push_str("\" height=\"");
                        push_svg_number(&mut svg, *height, "SVG frame outline height")?;
                        svg.push('"');
                        push_svg_corner_radius(&mut svg, FRAME_RADIUS)?;
                        svg.push_str(" fill=\"none\" stroke=\"");
                        stroke_color.write_hex(&mut svg).map_err(|_| {
                            FormatEncodeError::Encoding(
                                "failed to write SVG frame outline stroke".to_owned(),
                            )
                        })?;
                        svg.push_str("\" stroke-width=\"");
                        push_svg_number(
                            &mut svg,
                            FRAME_OUTLINE_STROKE_WIDTH,
                            "SVG frame outline stroke width",
                        )?;
                        svg.push_str("\"/>");
                    }
                    svg.push_str("</g>");
                }
                SvgElementKind::Text {
                    source,
                    text,
                    color,
                } => {
                    if source.id().is_empty() || source.pointer().is_empty() {
                        return Err(FormatEncodeError::Encoding(
                            "SVG text lost its source identity".to_owned(),
                        ));
                    }
                    svg.push_str("<g transform=\"translate(");
                    push_svg_number(&mut svg, transform.translation_x, "SVG text translation x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.translation_y, "SVG text translation y")?;
                    svg.push_str(") rotate(");
                    push_svg_number(&mut svg, transform.rotation_degrees, "SVG text rotation")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_x, "SVG text center x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_y, "SVG text center y")?;
                    svg.push_str(")\"");
                    let effective_opacity = opacity * color.alpha();
                    if effective_opacity != 1.0 {
                        svg.push_str(" opacity=\"");
                        push_svg_number(&mut svg, effective_opacity, "SVG text opacity")?;
                        svg.push('"');
                    }
                    svg.push('>');
                    push_svg_link_hit_rect(&mut svg, link.as_deref(), *width, *height)?;
                    svg.push_str("<text data-source-text=\"");
                    for line in &text.lines {
                        push_escaped_attribute(&mut svg, &line.text);
                    }
                    svg.push_str("\" xml:space=\"preserve\" fill=\"");
                    color.write_hex(&mut svg).map_err(|_| {
                        FormatEncodeError::Encoding("failed to write SVG text color".to_owned())
                    })?;
                    svg.push_str("\" font-family=\"");
                    push_escaped_attribute(&mut svg, Bundle::font_family_list(text.font_family));
                    svg.push_str("\" font-size=\"");
                    push_svg_number(&mut svg, text.font_size, "SVG text font size")?;
                    let (line_x, text_anchor) = match text.text_align {
                        ResolvedTextAlign::Left => (0.0, "start"),
                        ResolvedTextAlign::Center => (*width / 2.0, "middle"),
                        ResolvedTextAlign::Right => (*width, "end"),
                    };
                    svg.push_str("\" text-anchor=\"");
                    svg.push_str(text_anchor);
                    svg.push_str("\">");
                    for line in &text.lines {
                        if line.runs.is_empty() {
                            svg.push_str("<tspan x=\"");
                            push_svg_number(&mut svg, line_x, "SVG text line anchor x")?;
                            svg.push_str("\" y=\"");
                            push_svg_number(&mut svg, line.baseline, "SVG text line baseline")?;
                            svg.push_str("\">");
                            push_escaped_text(&mut svg, &line.display_text);
                            svg.push_str("</tspan>");
                        } else {
                            for run in &line.runs {
                                svg.push_str("<tspan font-family=\"");
                                push_escaped_attribute(
                                    &mut svg,
                                    Bundle::font_family_list(run.font_family),
                                );
                                svg.push_str(if run.rtl {
                                    "\" direction=\"rtl\" text-anchor=\"start\""
                                } else {
                                    "\" direction=\"ltr\" text-anchor=\"start\""
                                });
                                push_svg_run_positions(&mut svg, line, run, 0.0, "SVG text run")?;
                                svg.push('>');
                                push_escaped_text(&mut svg, &run.display_text);
                                svg.push_str("</tspan>");
                            }
                        }
                        // Nothing separates one line's tspans from the next.
                        // Under xml:space="preserve" a separator is a rendered
                        // character that joins the preceding text chunk, and a
                        // trailing space shifts every middle- or end-anchored
                        // line off the position the official export gives it.
                        // The source line break is carried by data-source-text.
                    }
                    svg.push_str("</text></g>");
                }
                SvgElementKind::Image {
                    source,
                    symbol_source_index,
                    image_invert_filter,
                    corner_radius,
                    projection,
                    scale,
                } => {
                    if source.id().is_empty() || source.pointer().is_empty() {
                        return Err(FormatEncodeError::Encoding(
                            "SVG image lost its source identity".to_owned(),
                        ));
                    }
                    svg.push_str("<g transform=\"translate(");
                    push_svg_number(&mut svg, transform.translation_x, "SVG image translation x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.translation_y, "SVG image translation y")?;
                    svg.push_str(") rotate(");
                    push_svg_number(&mut svg, transform.rotation_degrees, "SVG image rotation")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_x, "SVG image center x")?;
                    svg.push(' ');
                    push_svg_number(&mut svg, transform.center_y, "SVG image center y")?;
                    svg.push_str(")\"");
                    if corner_radius.is_some() {
                        svg.push_str(" clip-path=\"url(#");
                        push_image_clip_id(&mut svg, source)?;
                        svg.push_str(")\"");
                    }
                    if *opacity != 1.0 {
                        svg.push_str(" opacity=\"");
                        push_svg_number(&mut svg, *opacity, "SVG image opacity")?;
                        svg.push('"');
                    }
                    svg.push('>');
                    push_svg_link_hit_rect(&mut svg, link.as_deref(), *width, *height)?;
                    // The reference's image geometry (spec 8.6): its `<use>`
                    // is the rounded viewport, flipped about the element's
                    // unrounded center; an uncropped symbol image fills the
                    // viewport, a cropped one is drawn at its unrounded
                    // uncropped size under the crop offset, behind a
                    // viewport-sized `<mask>` on the group outside the flip.
                    // The mask and the `<use>` viewport are inlined as nested
                    // `<svg overflow="hidden">` viewports; the payload is the
                    // shared `<symbol>` in `<defs>`, drawn by a `<use>` sized
                    // to the content rectangle.
                    let (_, _, content_width, content_height) = projection.content_rect();
                    if projection.crop.is_some() {
                        svg.push_str("<svg width=\"");
                        push_svg_number(
                            &mut svg,
                            projection.viewport_width,
                            "SVG image crop mask width",
                        )?;
                        svg.push_str("\" height=\"");
                        push_svg_number(
                            &mut svg,
                            projection.viewport_height,
                            "SVG image crop mask height",
                        )?;
                        svg.push_str("\" overflow=\"hidden\">");
                    }
                    if *scale != [1, 1] {
                        svg.push_str("<g transform=\"translate(");
                        push_svg_number(
                            &mut svg,
                            if scale[0] < 0 { *width } else { 0.0 },
                            "SVG image flip translation x",
                        )?;
                        svg.push(' ');
                        push_svg_number(
                            &mut svg,
                            if scale[1] < 0 { *height } else { 0.0 },
                            "SVG image flip translation y",
                        )?;
                        svg.push_str(") scale(");
                        write!(svg, "{} {}", scale[0], scale[1]).map_err(|_| {
                            FormatEncodeError::Encoding(
                                "failed to write SVG image flip scale".to_owned(),
                            )
                        })?;
                        svg.push_str(")\">");
                    }
                    if projection.crop.is_some() {
                        // The `<use>` viewport, whose origin is also where the
                        // symbol image inside it is drawn.
                        let (viewport_x, viewport_y, viewport_width, viewport_height) =
                            projection.viewport_rect();
                        svg.push_str("<svg x=\"");
                        push_svg_number(&mut svg, viewport_x, "SVG image viewport x")?;
                        svg.push_str("\" y=\"");
                        push_svg_number(&mut svg, viewport_y, "SVG image viewport y")?;
                        svg.push_str("\" width=\"");
                        push_svg_number(&mut svg, viewport_width, "SVG image viewport width")?;
                        svg.push_str("\" height=\"");
                        push_svg_number(&mut svg, viewport_height, "SVG image viewport height")?;
                        svg.push_str("\" overflow=\"hidden\">");
                    }
                    // The dark-mode filter sits on the `<use>`, where the
                    // reference puts `IMAGE_INVERT_FILTER`, so the shared
                    // definition stays a plain asset.
                    svg.push_str("<use href=\"#");
                    push_image_symbol_id(&mut svg, *symbol_source_index)?;
                    svg.push_str("\" width=\"");
                    push_svg_number(&mut svg, content_width, "SVG image width")?;
                    svg.push_str("\" height=\"");
                    push_svg_number(&mut svg, content_height, "SVG image height")?;
                    if *image_invert_filter {
                        svg.push_str("\" filter=\"url(#");
                        svg.push_str(IMAGE_INVERT_FILTER_ID);
                        svg.push(')');
                    }
                    svg.push_str("\"/>");
                    if projection.crop.is_some() {
                        svg.push_str("</svg>");
                    }
                    if *scale != [1, 1] {
                        svg.push_str("</g>");
                    }
                    if projection.crop.is_some() {
                        svg.push_str("</svg>");
                    }
                    svg.push_str("</g>");
                }
                SvgElementKind::Fallback {
                    source,
                    clip_index,
                    label,
                } => {
                    if source.pointer.is_empty() {
                        return Err(FormatEncodeError::Encoding(
                            "SVG fallback lost its source location".to_owned(),
                        ));
                    }
                    svg.push_str("<g transform=\"translate(");
                    push_svg_number(
                        &mut svg,
                        transform.translation_x,
                        "SVG fallback translation x",
                    )?;
                    svg.push(' ');
                    push_svg_number(
                        &mut svg,
                        transform.translation_y,
                        "SVG fallback translation y",
                    )?;
                    svg.push_str(") rotate(");
                    push_svg_number(
                        &mut svg,
                        transform.rotation_degrees,
                        "SVG fallback rotation",
                    )?;
                    svg.push(' ');
                    push_svg_number(
                        &mut svg,
                        transform.center_x,
                        "SVG fallback rotation center x",
                    )?;
                    svg.push(' ');
                    push_svg_number(
                        &mut svg,
                        transform.center_y,
                        "SVG fallback rotation center y",
                    )?;
                    svg.push_str(")\"");
                    if *opacity != 1.0 {
                        svg.push_str(" opacity=\"");
                        push_svg_number(&mut svg, *opacity, "SVG fallback opacity")?;
                        svg.push('"');
                    }
                    svg.push_str("><clipPath id=\"fallback-");
                    write!(svg, "{clip_index}").map_err(|_| {
                        FormatEncodeError::Encoding(
                            "failed to write SVG fallback clip identity".to_owned(),
                        )
                    })?;
                    svg.push_str("\"><rect x=\"0\" y=\"0\" width=\"");
                    push_svg_number(&mut svg, *width, "SVG fallback clip width")?;
                    svg.push_str("\" height=\"");
                    push_svg_number(&mut svg, *height, "SVG fallback clip height")?;
                    svg.push_str("\"/></clipPath><g clip-path=\"url(#fallback-");
                    write!(svg, "{clip_index}").map_err(|_| {
                        FormatEncodeError::Encoding(
                            "failed to write SVG fallback clip reference".to_owned(),
                        )
                    })?;
                    svg.push_str(")\"><rect x=\"0\" y=\"0\" width=\"");
                    push_svg_number(&mut svg, *width, "SVG fallback fill width")?;
                    svg.push_str("\" height=\"");
                    push_svg_number(&mut svg, *height, "SVG fallback fill height")?;
                    svg.push_str("\" fill=\"");
                    self.fallback_colors.fill.write_hex(&mut svg).map_err(|_| {
                        FormatEncodeError::Encoding("failed to write SVG fallback fill".to_owned())
                    })?;
                    svg.push_str("\"/>");
                    if *width >= 2.0 && *height >= 2.0 {
                        svg.push_str("<rect x=\"0\" y=\"0\" width=\"");
                        push_svg_number(&mut svg, *width, "SVG fallback stroke width extent")?;
                        svg.push_str("\" height=\"");
                        push_svg_number(&mut svg, *height, "SVG fallback stroke height extent")?;
                        svg.push_str("\" fill=\"none\" stroke=\"");
                        self.fallback_colors
                            .stroke
                            .write_hex(&mut svg)
                            .map_err(|_| {
                                FormatEncodeError::Encoding(
                                    "failed to write SVG fallback stroke".to_owned(),
                                )
                            })?;
                        svg.push_str(concat!(
                            "\" stroke-width=\"2\" stroke-dasharray=\"6 4\" ",
                            "stroke-dashoffset=\"0\"/><path d=\"M0 0 L"
                        ));
                        push_svg_number(&mut svg, *width, "SVG fallback cross width")?;
                        svg.push(' ');
                        push_svg_number(&mut svg, *height, "SVG fallback cross height")?;
                        svg.push_str(" M");
                        push_svg_number(&mut svg, *width, "SVG fallback reverse cross width")?;
                        svg.push_str(" 0 L0 ");
                        push_svg_number(&mut svg, *height, "SVG fallback reverse cross height")?;
                        svg.push_str("\" fill=\"none\" stroke=\"");
                        self.fallback_colors
                            .stroke
                            .write_hex(&mut svg)
                            .map_err(|_| {
                                FormatEncodeError::Encoding(
                                    "failed to write SVG fallback cross".to_owned(),
                                )
                            })?;
                        svg.push_str("\" stroke-width=\"2\"/>");
                    }
                    if let Some(label) = label {
                        svg.push_str("<text x=\"");
                        push_svg_number(&mut svg, *width / 2.0, "SVG fallback label x")?;
                        svg.push_str("\" y=\"");
                        push_svg_number(&mut svg, *height / 2.0, "SVG fallback label y")?;
                        svg.push_str("\" fill=\"");
                        self.fallback_colors
                            .label
                            .write_hex(&mut svg)
                            .map_err(|_| {
                                FormatEncodeError::Encoding(
                                    "failed to write SVG fallback label color".to_owned(),
                                )
                            })?;
                        svg.push_str("\" font-family=\"");
                        push_escaped_attribute(
                            &mut svg,
                            Bundle::font_family_list("Liberation Sans"),
                        );
                        svg.push_str(concat!(
                            "\" font-size=\"14\" ",
                            "text-anchor=\"middle\" dominant-baseline=\"central\">"
                        ));
                        push_escaped_text(&mut svg, label);
                        svg.push_str("</text>");
                    }
                    svg.push_str("</g></g>");
                }
            }
            if link.is_some() {
                svg.push_str("</a>");
            }
            for _ in 0..frame_clip_count {
                svg.push_str("</g>");
            }
        }
        svg.push_str("</svg>");
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
fn push_svg_drawing<W: SvgSink + ?Sized>(
    output: &mut W,
    drawing: &Drawing,
    stroke_color: CanonicalColor,
    fill_color: Option<CanonicalColor>,
    stroke_width: f64,
    stroke_style: StrokeStyle,
    source_stroke_width: f64,
    opacity: f64,
) -> Result<(), FormatEncodeError> {
    if let (Some(fill), Some(fill_color)) = (&drawing.fill, fill_color) {
        output.push_str("<path d=\"");
        push_rough_path(output, &fill.path)?;
        match fill.kind {
            FillKind::Solid => {
                output.push_str("\" stroke=\"none\" fill=\"");
                fill_color.write_hex(output).map_err(|_| {
                    FormatEncodeError::Encoding("failed to write SVG fill color".to_owned())
                })?;
                output.push('"');
                if drawing.shape.svg_uses_even_odd_fill() {
                    output.push_str(" fill-rule=\"evenodd\"");
                }
                if fill_color.alpha() * opacity != 1.0 {
                    output.push_str(" fill-opacity=\"");
                    push_svg_number(
                        output,
                        fill_color.alpha() * opacity,
                        "SVG effective fill alpha",
                    )?;
                    output.push('"');
                }
            }
            FillKind::Sketch => {
                output.push_str("\" stroke=\"");
                fill_color.write_hex(output).map_err(|_| {
                    FormatEncodeError::Encoding("failed to write SVG sketch color".to_owned())
                })?;
                output.push_str("\" stroke-width=\"");
                push_svg_number(output, source_stroke_width / 2.0, "SVG fill weight")?;
                output.push_str("\" fill=\"none\"");
                if fill_color.alpha() * opacity != 1.0 {
                    output.push_str(" stroke-opacity=\"");
                    push_svg_number(
                        output,
                        fill_color.alpha() * opacity,
                        "SVG effective fill alpha",
                    )?;
                    output.push('"');
                }
            }
        }
        output.push_str("/> ");
    }
    output.push_str("<path d=\"");
    push_rough_path(output, &drawing.outline)?;
    output.push_str("\" stroke=\"");
    stroke_color
        .write_hex(output)
        .map_err(|_| FormatEncodeError::Encoding("failed to write SVG stroke color".to_owned()))?;
    output.push_str("\" stroke-width=\"");
    push_svg_number(output, stroke_width, "SVG stroke width")?;
    output.push_str("\" fill=\"none\"");
    match stroke_style {
        StrokeStyle::Solid => {}
        StrokeStyle::Dashed => {
            output.push_str(" stroke-dasharray=\"8 ");
            push_svg_number(output, 8.0 + source_stroke_width, "SVG dash gap")?;
            output.push('"');
        }
        StrokeStyle::Dotted => {
            output.push_str(" stroke-dasharray=\"1.5 ");
            push_svg_number(output, 6.0 + source_stroke_width, "SVG dot gap")?;
            output.push('"');
        }
    }
    if stroke_color.alpha() * opacity != 1.0 {
        output.push_str(" stroke-opacity=\"");
        push_svg_number(
            output,
            stroke_color.alpha() * opacity,
            "SVG effective stroke alpha",
        )?;
        output.push('"');
    }
    output.push_str("/>");
    Ok(())
}

/// Writes a Rough.js path as SVG path data: the operations space-separated,
/// each coordinate in the two-decimal form Rough.js emits.
pub(crate) fn push_rough_path<W: SvgSink + ?Sized>(
    output: &mut W,
    path: &Path,
) -> Result<(), FormatEncodeError> {
    for (index, operation) in path.operations.iter().enumerate() {
        if index != 0 {
            output.push(' ');
        }
        match operation {
            PathOperation::Move(point) => {
                output.push('M');
                push_rough_number(output, point.x)?;
                output.push(' ');
                push_rough_number(output, point.y)?;
            }
            PathOperation::Line(point) => {
                output.push('L');
                push_rough_number(output, point.x)?;
                output.push(' ');
                push_rough_number(output, point.y)?;
            }
            PathOperation::Cubic {
                control_1,
                control_2,
                end,
            } => {
                output.push('C');
                push_rough_number(output, control_1.x)?;
                output.push(' ');
                push_rough_number(output, control_1.y)?;
                output.push_str(", ");
                push_rough_number(output, control_2.x)?;
                output.push(' ');
                push_rough_number(output, control_2.y)?;
                output.push_str(", ");
                push_rough_number(output, end.x)?;
                output.push(' ');
                push_rough_number(output, end.y)?;
            }
        }
    }
    Ok(())
}

/// The SVG path data for a freedraw outline, in the form Excalidraw's
/// `getSvgPathFromStroke` emits: `M x,y Q` then control/end pairs, `L` for a
/// straight segment and `Z` to close, every coordinate cut to two decimals.
pub(crate) fn freedraw_path_data(path: &freedraw::Path) -> Result<String, FormatEncodeError> {
    let mut output = String::new();
    output
        .try_reserve(path.operations.len().saturating_mul(64))
        .map_err(|_| FormatEncodeError::MemoryUnavailable {
            subject_kind: "format_plan",
            subject: "svg_freedraw_path_data",
        })?;
    for operation in &path.operations {
        match *operation {
            freedraw::PathOperation::Move(point) => {
                output.push_str("M ");
                push_freedraw_point(&mut output, point)?;
                output.push_str(" Q");
            }
            freedraw::PathOperation::Quadratic { control, end } => {
                output.push(' ');
                push_freedraw_point(&mut output, control)?;
                output.push(' ');
                push_freedraw_point(&mut output, end)?;
            }
            freedraw::PathOperation::Line(point) => {
                output.push_str(" L ");
                push_freedraw_point(&mut output, point)?;
            }
            freedraw::PathOperation::Close => output.push_str(" Z"),
        }
    }
    Ok(output)
}

fn push_freedraw_point(output: &mut String, point: Point) -> Result<(), FormatEncodeError> {
    push_freedraw_number(output, point.x)?;
    output.push(',');
    push_freedraw_number(output, point.y)
}

fn push_freedraw_number(output: &mut String, value: f64) -> Result<(), FormatEncodeError> {
    if !value.is_finite() {
        return Err(FormatEncodeError::Encoding(
            "SVG freedraw path contains a non-finite coordinate".to_owned(),
        ));
    }
    let token = ecmascript_number(value)?;
    let Some(decimal) = token.find('.') else {
        output.push_str(&token);
        return Ok(());
    };
    let digits = token[decimal + 1..]
        .bytes()
        .take_while(u8::is_ascii_digit)
        .count()
        .min(2);
    let retained_end = decimal + 1 + digits;
    let consumed_end = token[retained_end..]
        .bytes()
        .take_while(|byte| byte.is_ascii_digit() || matches!(byte, b'e' | b'-'))
        .count()
        + retained_end;
    output.push_str(&token[..retained_end]);
    output.push_str(&token[consumed_end..]);
    Ok(())
}

fn push_escaped_attribute<W: SvgSink + ?Sized>(output: &mut W, value: &str) {
    for character in value.chars() {
        match character {
            '&' => output.push_str("&amp;"),
            '"' => output.push_str("&quot;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '\n' => output.push_str("&#10;"),
            '\r' => output.push_str("&#13;"),
            '\t' => output.push_str("&#9;"),
            character => output.push(character),
        }
    }
}

fn push_escaped_text<W: SvgSink + ?Sized>(output: &mut W, value: &str) {
    for character in value.chars() {
        match character {
            '&' => output.push_str("&amp;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            character => output.push(character),
        }
    }
}

fn push_base64<W: SvgSink + ?Sized>(output: &mut W, bytes: &[u8]) -> Result<(), FormatEncodeError> {
    const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for chunk in bytes.chunks(3) {
        let first = chunk[0];
        let second = chunk.get(1).copied().unwrap_or(0);
        let third = chunk.get(2).copied().unwrap_or(0);
        output.push(char::from(ALPHABET[usize::from(first >> 2)]));
        output.push(char::from(
            ALPHABET[usize::from(((first & 0x03) << 4) | (second >> 4))],
        ));
        if chunk.len() > 1 {
            output.push(char::from(
                ALPHABET[usize::from(((second & 0x0f) << 2) | (third >> 6))],
            ));
        } else {
            output.push('=');
        }
        if chunk.len() > 2 {
            output.push(char::from(ALPHABET[usize::from(third & 0x3f)]));
        } else {
            output.push('=');
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{push_escaped_attribute, push_escaped_text};

    /// `data-source-text` carries a text element's own bytes: the markup
    /// characters and every line-ending or tab byte are escaped as character
    /// references so a `\r` survives attribute-value normalization (#110).
    #[test]
    fn attribute_escaping_keeps_line_endings_as_character_references() {
        let escaped = |value: &str| {
            let mut output = String::new();
            push_escaped_attribute(&mut output, value);
            output
        };
        assert_eq!(escaped("a\nb"), "a&#10;b");
        assert_eq!(escaped("a\r\nb"), "a&#13;&#10;b");
        assert_eq!(escaped("a\rb"), "a&#13;b");
        assert_eq!(escaped("\t<\"&\">"), "&#9;&lt;&quot;&amp;&quot;&gt;");
        assert_eq!(escaped("plain ünïcödé"), "plain ünïcödé");
    }

    /// Text content escapes only the markup characters; a quote is literal
    /// there, and whitespace is preserved as written under
    /// `xml:space="preserve"`.
    #[test]
    fn text_escaping_leaves_quotes_and_whitespace_literal() {
        let mut output = String::new();
        push_escaped_text(&mut output, "a \"b\" <c> & d\t");
        assert_eq!(output, "a \"b\" &lt;c&gt; &amp; d\t");
    }
}