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
use crate::{theme::ColorTable, Canvas};

fn to_style(theme: impl ColorTable) -> String {
    format!(
        r#"

.black{{
fill: rgb{:?};
}}
.red{{
fill: rgb{:?};
}}
.green{{
fill: rgb{:?};
}}
.yellow{{
fill: rgb{:?};
}}
.blue{{
fill: rgb{:?};
}}
.magenta{{
fill: rgb{:?};
}}
.cyan{{
fill: rgb{:?};
}}
.white{{
fill: rgb{:?};
}}

.bright_black{{
fill: rgb{:?};
}}
.bright_red{{
fill: rgb{:?};
}}
.bright_green{{
fill: rgb{:?};
}}
.bright_yellow{{
fill: rgb{:?};
}}
.bright_blue{{
fill: rgb{:?};
}}
.bright_magenta{{
fill: rgb{:?};
}}
.bright_cyan{{
fill: rgb{:?};
}}
.bright_white{{
fill: rgb{:?};
}}

.bold{{
font-weight: bold;
}}

.blink {{
  animation: blink_keyframes 1s steps(1, end) infinite;
}}

@keyframes blink_keyframes{{
  50% {{
    opacity: 0;
  }}
}}
"#,
        theme.black(),
        theme.red(),
        theme.green(),
        theme.yellow(),
        theme.blue(),
        theme.magenta(),
        theme.cyan(),
        theme.white(),
        theme.bright_black(),
        theme.bright_red(),
        theme.bright_green(),
        theme.bright_yellow(),
        theme.bright_blue(),
        theme.bright_magenta(),
        theme.bright_cyan(),
        theme.bright_white(),
    )
}

pub fn to_svg(s: &str, theme: impl ColorTable, width: Option<usize>) -> String {
    let canvas = Canvas::new(s, width);
    let mut s = String::new();
    let mut cur_x = 0;
    let fn_w = 20;
    let fn_h = 32;
    let baseline_h = 16;
    let mut cur_y = 0;
    let style = to_style(theme);

    for row in canvas.pixels.iter() {
        for c in row.iter() {
            let mut text_class = vec![];

            if c.bg_color.0 != 0 {
                let class = [c.bg_color.name()].join(" ");
                s.push_str(&format!(
                    r#"<rect x="{cur_x}px" y="{cur_y}px" width="{fn_w}px" height="{fn_h}px" class="{class}"/>"#,
                ));
            }

            if c.color.0 != 0 {
                text_class.push(c.color.name());
            };

            if c.bold {
                text_class.push("bold".into());
            }
            if c.blink {
                text_class.push("blink".into());
            }

            // baseline offset
            let text_x = cur_x;
            let text_y = cur_y + baseline_h;
            s.push_str(&format!(
r#"<text x="{text_x}px" y="{text_y}px" width="{fn_w}px" height="{fn_h}px" class="{}"><tspan>{}</tspan></text>"#,
                text_class.join(" "),
                html_escape::encode_text(&c.char.to_string())
            ));
            cur_x += fn_w;
        }
        cur_y += fn_h;
        cur_x = 0;
    }

    let svg_w = fn_w * canvas.w;
    let svg_h = fn_h * canvas.h;
    format!(
        r#"<svg
width="{svg_w}px"
height="{svg_h}px"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>

<style>
tspan {{
    font-variant-ligatures: none;
    dominant-baseline: central;
    font-variant-ligatures: none;
    white-space: pre;
    font-family: Courier, monospace;
    font-size: {fn_h}px;
}}

{style}
</style>
{s}
</svg>
"#
    )
}