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
use std::{fmt, io, iter, mem, str};

use serde::Serialize;
use termcolor::{ColorSpec, WriteColor};

use super::{IndexOrRgb, LineBreak, LineSplitter, StyledSpan, WriteLines, WriteStr};

impl StyledSpan {
    fn for_bg(color: IndexOrRgb, intense: bool, dimmed: bool) -> Self {
        let classes = if dimmed {
            vec!["dimmed".to_owned()]
        } else {
            vec![]
        };
        let mut this = Self {
            classes,
            styles: vec![],
        };
        this.set_fg(color, intense, &["fill", "stroke"]);
        // ^ Ideally, we'd want to add `stroke: context-fill` to the `.output-bg` selector.
        // Unfortunately, it's not supported by all viewers.
        this
    }
}

#[derive(Debug, Serialize)]
pub(crate) struct SvgLine {
    pub background: Option<String>,
    pub foreground: String,
}

impl SvgLine {
    fn new(foreground: String, background_segments: Vec<BackgroundSegment>) -> Self {
        let background = if let Some(segment) = background_segments.last() {
            let estimated_capacity =
                16 * background_segments.len() + segment.start_pos + segment.char_width;
            let mut background = String::with_capacity(estimated_capacity);
            let mut pos = 0;
            for segment in background_segments {
                background.extend(iter::repeat('\u{a0}').take(segment.start_pos - pos));
                pos = segment.start_pos + segment.char_width;
                segment.write_tspan(&mut background);
            }
            Some(background)
        } else {
            None
        };

        Self {
            background,
            foreground,
        }
    }
}

#[derive(Debug)]
struct BackgroundSegment {
    start_pos: usize,
    char_width: usize,
    span: StyledSpan,
}

impl BackgroundSegment {
    fn write_tspan(self, output: &mut String) {
        self.span.write_tag(output, "tspan").unwrap();
        output.extend(iter::repeat('█').take(self.char_width));
        output.push_str("</tspan>");
    }
}

#[derive(Debug)]
pub(crate) struct SvgWriter {
    output: Vec<SvgLine>,
    current_background: Vec<BackgroundSegment>,
    current_line: String,
    current_style: Option<ColorSpec>,
    line_splitter: LineSplitter,
}

impl SvgWriter {
    pub fn new(max_width: Option<usize>) -> Self {
        Self {
            output: vec![],
            current_background: vec![],
            current_line: String::new(),
            current_style: None,
            line_splitter: max_width.map_or_else(LineSplitter::default, LineSplitter::new),
        }
    }

    fn write_color(&mut self, spec: ColorSpec, start_pos: usize) -> io::Result<()> {
        use fmt::Write as _;

        let mut span = StyledSpan::new(&spec, "fill")?;

        let mut back_color_class = String::with_capacity(4);
        back_color_class.push_str("bg");
        let back_color = spec.bg().map(|&color| IndexOrRgb::new(color)).transpose()?;
        match back_color {
            Some(IndexOrRgb::Index(idx)) => {
                let final_idx = if spec.intense() { idx | 8 } else { idx };
                write!(&mut back_color_class, "{final_idx}").unwrap();
                // ^-- `unwrap` is safe; writing to a string never fails.
                span.classes.push(back_color_class);
            }
            Some(IndexOrRgb::Rgb(r, g, b)) => {
                write!(&mut back_color_class, "#{r:02x}{g:02x}{b:02x}").unwrap();
                span.classes.push(back_color_class);
            }
            None => { /* Do nothing. */ }
        }
        if let Some(color) = back_color {
            self.current_background.push(BackgroundSegment {
                start_pos,
                char_width: 0,
                span: StyledSpan::for_bg(color, spec.intense(), spec.dimmed()),
            });
        }

        span.write_tag(self, "tspan")?;
        self.current_style = Some(spec);
        Ok(())
    }

    fn reset_inner(&mut self, line_width: Option<usize>) -> io::Result<()> {
        if let Some(spec) = &self.current_style {
            if spec.bg().is_some() {
                let line_width = line_width.unwrap_or(self.line_splitter.current_width);
                self.terminate_bg_segment(line_width);
            }
            self.current_style = None;
            self.write_str("</tspan>")?;
        }
        Ok(())
    }

    fn terminate_bg_segment(&mut self, current_width: usize) {
        let segment = self.current_background.last_mut().unwrap();
        segment.char_width = current_width - segment.start_pos;
    }

    pub fn into_lines(mut self) -> Vec<SvgLine> {
        if self.line_splitter.current_width > 0 {
            self.output.push(SvgLine::new(
                mem::take(&mut self.current_line),
                mem::take(&mut self.current_background),
            ));
        }
        self.output
    }
}

impl WriteStr for SvgWriter {
    fn write_str(&mut self, s: &str) -> io::Result<()> {
        self.current_line.write_str(s)
    }
}

impl WriteLines for SvgWriter {
    fn line_splitter_mut(&mut self) -> Option<&mut LineSplitter> {
        Some(&mut self.line_splitter)
    }

    fn write_line_break(&mut self, br: LineBreak, char_width: usize) -> io::Result<()> {
        const HARD_BR: &str =
            r#"<tspan class="hard-br" rotate="45" dx=".1em" dy="-.2em">↓</tspan>"#;
        match br {
            LineBreak::Hard => self.write_str(HARD_BR)?,
        }
        self.write_new_line(char_width)
    }

    fn write_new_line(&mut self, char_width: usize) -> io::Result<()> {
        let current_style = self.current_style.clone();
        self.reset_inner(Some(char_width))?;

        self.output.push(SvgLine::new(
            mem::take(&mut self.current_line),
            mem::take(&mut self.current_background),
        ));

        if let Some(spec) = current_style {
            self.write_color(spec, 0)?;
        }
        Ok(())
    }
}

impl io::Write for SvgWriter {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        self.io_write(buffer, true)
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl WriteColor for SvgWriter {
    fn supports_color(&self) -> bool {
        true
    }

    fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
        debug_assert!(spec.reset());
        self.reset()?;
        if !spec.is_none() {
            let start_pos = self.line_splitter.current_width;
            self.write_color(spec.clone(), start_pos)?;
        }
        Ok(())
    }

    fn reset(&mut self) -> io::Result<()> {
        self.reset_inner(None)
    }
}

#[cfg(test)]
mod tests {
    use io::Write as _;
    use termcolor::Color;

    use super::*;

    #[test]
    fn svg_writer_basic_colors() -> anyhow::Result<()> {
        let mut writer = SvgWriter::new(None);
        write!(writer, "Hello, ")?;
        writer.set_color(
            ColorSpec::new()
                .set_bold(true)
                .set_underline(true)
                .set_fg(Some(Color::Green))
                .set_bg(Some(Color::White)),
        )?;
        write!(writer, "world")?;
        writer.reset()?;
        write!(writer, "!")?;

        let mut lines = writer.into_lines();
        assert_eq!(lines.len(), 1);
        let SvgLine {
            background,
            foreground,
        } = lines.pop().unwrap();
        let background = background.unwrap();
        assert_eq!(
            background,
            "\u{a0}\u{a0}\u{a0}\u{a0}\u{a0}\u{a0}\u{a0}<tspan class=\"fg7\">█████</tspan>"
        );
        assert_eq!(
            foreground,
            "Hello,\u{a0}<tspan class=\"bold underline fg2 bg7\">world</tspan>!"
        );
        Ok(())
    }

    #[test]
    fn svg_writer_intense_color() -> anyhow::Result<()> {
        let mut writer = SvgWriter::new(None);

        writer.set_color(ColorSpec::new().set_intense(true).set_fg(Some(Color::Blue)))?;
        write!(writer, "blue")?;
        writer.reset()?;

        let mut lines = writer.into_lines();
        assert_eq!(lines.len(), 1);
        let SvgLine {
            background,
            foreground,
        } = lines.pop().unwrap();
        assert!(background.is_none());
        assert_eq!(foreground, r#"<tspan class="fg12">blue</tspan>"#);
        Ok(())
    }

    #[test]
    fn final_empty_line_in_writer() -> anyhow::Result<()> {
        let writer = SvgWriter::new(None);
        let lines = writer.into_lines();
        assert!(lines.is_empty());

        let mut writer = SvgWriter::new(None);

        writer.set_color(ColorSpec::new().set_intense(true).set_fg(Some(Color::Blue)))?;
        write!(writer, "")?;
        writer.reset()?;

        let lines = writer.into_lines();
        assert!(lines.is_empty());
        Ok(())
    }

    #[test]
    fn svg_writer_custom_colors() -> anyhow::Result<()> {
        let mut writer = SvgWriter::new(None);
        writer.set_color(ColorSpec::new().set_fg(Some(Color::Ansi256(5))))?;
        write!(writer, "H")?;
        writer.set_color(ColorSpec::new().set_bg(Some(Color::Ansi256(14))))?;
        write!(writer, "e")?;
        writer.set_color(ColorSpec::new().set_bg(Some(Color::Ansi256(76))))?;
        write!(writer, "l")?;
        writer.set_color(ColorSpec::new().set_fg(Some(Color::Ansi256(200))))?;
        write!(writer, "l")?;
        writer.set_color(ColorSpec::new().set_bg(Some(Color::Ansi256(250))))?;
        write!(writer, "o")?;
        writer.reset()?;

        let mut lines = writer.into_lines();
        assert_eq!(lines.len(), 1);
        let SvgLine {
            background,
            foreground,
        } = lines.pop().unwrap();
        let background = background.unwrap();
        assert_eq!(
            background,
            "\u{a0}<tspan class=\"fg14\">█</tspan>\
             <tspan style=\"fill: #5fd700; stroke: #5fd700;\">█</tspan>\
             \u{a0}<tspan style=\"fill: #bcbcbc; stroke: #bcbcbc;\">█</tspan>"
        );
        assert_eq!(
            foreground,
            "<tspan class=\"fg5\">H</tspan>\
             <tspan class=\"bg14\">e</tspan>\
             <tspan class=\"bg#5fd700\">l</tspan>\
             <tspan style=\"fill: #ff00d7;\">l</tspan>\
             <tspan class=\"bg#bcbcbc\">o</tspan>"
        );

        Ok(())
    }

    #[test]
    fn svg_writer_newlines() -> anyhow::Result<()> {
        let mut writer = SvgWriter::new(None);
        writeln!(writer, "Hello,")?;
        write!(writer, " ")?;
        writer.set_color(
            ColorSpec::new()
                .set_bold(true)
                .set_underline(true)
                .set_fg(Some(Color::Green))
                .set_bg(Some(Color::White)),
        )?;
        writeln!(writer, "wor")?;
        write!(writer, "ld")?;
        writer.reset()?;
        write!(writer, "!")?;

        let lines = writer.into_lines();
        let [first, second, third] = lines.as_slice() else {
            panic!("Unexpected lines: {lines:?}");
        };
        assert!(first.background.is_none());
        assert_eq!(first.foreground, "Hello,");
        assert_eq!(
            second.background.as_ref().unwrap(),
            "\u{a0}<tspan class=\"fg7\">███</tspan>"
        );
        assert_eq!(
            second.foreground,
            "\u{a0}<tspan class=\"bold underline fg2 bg7\">wor</tspan>"
        );
        assert_eq!(
            third.background.as_ref().unwrap(),
            "<tspan class=\"fg7\">██</tspan>"
        );
        assert_eq!(
            third.foreground,
            "<tspan class=\"bold underline fg2 bg7\">ld</tspan>!"
        );

        Ok(())
    }

    #[test]
    fn splitting_lines_in_svg_writer() -> anyhow::Result<()> {
        let mut writer = SvgWriter::new(Some(5));

        write!(writer, "Hello, ")?;
        writer.set_color(
            ColorSpec::new()
                .set_bold(true)
                .set_underline(true)
                .set_fg(Some(Color::Green))
                .set_bg(Some(Color::White)),
        )?;
        write!(writer, "world")?;
        writer.reset()?;
        write!(writer, "! More>\ntext")?;

        let lines = writer.into_lines();
        assert_eq!(lines.len(), 5);
        let [first, second, third, ..] = lines.as_slice() else {
            unreachable!();
        };
        assert!(first.background.is_none());
        assert_eq!(
            first.foreground,
            "Hello<tspan class=\"hard-br\" rotate=\"45\" dx=\".1em\" dy=\"-.2em\">↓</tspan>"
        );
        assert_eq!(
            second.background.as_ref().unwrap(),
            "\u{a0}\u{a0}<tspan class=\"fg7\">███</tspan>"
        );
        assert_eq!(
            second.foreground,
            ",\u{a0}<tspan class=\"bold underline fg2 bg7\">wor\
             <tspan class=\"hard-br\" rotate=\"45\" dx=\".1em\" dy=\"-.2em\">↓</tspan></tspan>"
        );
        assert_eq!(
            third.background.as_ref().unwrap(),
            "<tspan class=\"fg7\">██</tspan>"
        );
        assert_eq!(
            third.foreground,
            "<tspan class=\"bold underline fg2 bg7\">ld</tspan>!\u{a0}M\
             <tspan class=\"hard-br\" rotate=\"45\" dx=\".1em\" dy=\"-.2em\">↓</tspan>"
        );

        assert!(lines[3].background.is_none());
        assert_eq!(lines[3].foreground, "ore&gt;");
        assert!(lines[4].background.is_none());
        assert_eq!(lines[4].foreground, "text");

        Ok(())
    }
}