Skip to content

Commit 17388d2

Browse files
committed
Improve rendering of ascii drawing and block characters in SVG previews
1 parent ebab6a2 commit 17388d2

File tree

4 files changed

+396
-44
lines changed

4 files changed

+396
-44
lines changed

lib/asciinema/recordings/snapshot.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ defmodule Asciinema.Recordings.Snapshot do
132132
Enum.member?(@powerline_triangles_range, cp)
133133
end
134134

135+
def block_or_powerline_triangle?(char) do
136+
cp = char |> String.to_charlist() |> Enum.at(0)
137+
138+
Enum.member?(@block_elements_range, cp) || Enum.member?(@powerline_triangles_range, cp)
139+
end
140+
135141
@csi_init "\x1b["
136142
@sgr_reset "\x1b[0m"
137143

@@ -253,7 +259,7 @@ defmodule Asciinema.Recordings.Snapshot do
253259
|> regroup(mode)
254260
end
255261

256-
def text_coords(snapshot) do
262+
def fg_coords(snapshot) do
257263
snapshot
258264
|> regroup(:segments)
259265
|> Map.get(:lines)

lib/asciinema_web/controllers/recording/preview.svg.heex

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<svg
22
xmlns="http://www.w3.org/2000/svg"
33
xmlns:xlink="http://www.w3.org/1999/xlink"
4-
width={(@cols + 2) * 8.433333333}
5-
height={(@rows + 1) * 14 * 1.333333}
4+
width={w(@cols + 2)}
5+
height={h(@rows + 1)}
66
font-family={
77
{:safe,
88
@font_family ||
99
"Consolas, Menlo, 'Bitstream Vera Sans Mono', monospace, 'Powerline Symbols'"}
1010
}
11-
font-size="14px"
11+
font-size={"#{font_size()}px"}
1212
>
1313
<style>
1414
.br { font-weight: bold }
@@ -25,40 +25,39 @@
2525
ry={assigns[:ry] || 4}
2626
/>
2727

28-
<svg
29-
x={percent(1.0 * 100 / (@cols + 2))}
30-
y={percent(0.5 * 100 / (@rows + 1))}
31-
class="default-text-fill"
32-
>
28+
<svg x={x(1)} y={y(0.5)} class="default-text-fill">
3329
<g style="shape-rendering: optimizeSpeed">
34-
<%= for %{y: y, segments: segments} <- bg_coords(@snapshot) do %>
30+
<%= for %{y: y, segments: segments} <- @coords.bg do %>
3531
<rect
36-
:for={segment <- segments}
37-
x={percent(100 * segment.x / (@cols + 2))}
38-
y={percent(100 * y / (@rows + 1))}
39-
width={percent(100 * segment.width / (@cols + 2))}
40-
height="19.7"
41-
style={bg_style(segment.attrs, @theme)}
32+
:for={%{x: x, width: width, attrs: attrs} <- segments}
33+
x={x(x)}
34+
y={y(y)}
35+
width={w(width)}
36+
height={h(1)}
37+
style={bg_style(attrs, @theme)}
4238
/>
4339
<% end %>
40+
41+
<%= for %{y: y, segments: segments} <- @coords.special_chars do %>
42+
<%= for %{x: x, text: char, attrs: attrs} <- segments do %>
43+
<.special_char char={char} x={x} y={y} attrs={attrs} theme={@theme} />
44+
<% end %>
45+
<% end %>
4446
</g>
4547

4648
<text style={"fill: " <> @theme.fg}>
47-
<tspan
48-
:for={%{y: y, segments: segments} <- text_coords(@snapshot)}
49-
y={percent(100 * y / (@rows + 1))}
50-
>
49+
<tspan :for={%{y: y, segments: segments} <- @coords.text} y={y(y)}>
5150
<tspan
52-
:for={{segment, i} <- Enum.with_index(segments)}
51+
:for={{%{x: x, text: text, attrs: attrs}, i} <- Enum.with_index(segments)}
5352
dy={
5453
if i == 0 do
5554
"1em"
5655
end
5756
}
58-
x={percent(100 * segment.x / (@cols + 2))}
59-
{text_extra_attrs(segment.attrs, @theme)}
57+
x={x(x)}
58+
{text_extra_attrs(attrs, @theme)}
6059
>
61-
<%= html_escape(segment.text) %>
60+
<%= html_escape(text) %>
6261
</tspan>
6362
</tspan>
6463
</text>

0 commit comments

Comments
 (0)