Skip to content

Commit a46f0f2

Browse files
committed
feat(svg): Pre-encoding Base64 strings
1 parent 2ed2e42 commit a46f0f2

2 files changed

Lines changed: 39 additions & 27 deletions

File tree

src/cache.gleam

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gleam/bit_array
12
import gleam/dict.{type Dict}
23
import gleam/int
34
import gleam/list
@@ -8,7 +9,7 @@ import simplifile
89
import wisp
910

1011
pub type CachedImage {
11-
CachedImage(data: BitArray, info: image.ImageInformation)
12+
CachedImage(base64: String, info: image.ImageInformation)
1213
}
1314

1415
pub type ThemeCache =
@@ -49,7 +50,10 @@ pub fn load_themes() {
4950
dict.insert(
5051
accumulated_digits,
5152
digit,
52-
CachedImage(data: image_data, info: info),
53+
CachedImage(
54+
base64: bit_array.base64_encode(image_data, False),
55+
info: info,
56+
),
5357
)
5458
Error(_) -> {
5559
wisp.log_error(

src/svg.gleam

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cache
2-
import gleam/bit_array
32
import gleam/int
43
import gleam/list
54
import gleam/option.{Some}
@@ -10,16 +9,20 @@ type XmlImages {
109
XmlImages(xml: String, width: Int, height: Int)
1110
}
1211

13-
fn image(data, image: image.ImageInformation, width) {
14-
"<image
15-
height=\"" <> int.to_string(image.height) <> "\"
16-
width=\"" <> int.to_string(image.width) <> "\"
17-
x=\"" <> int.to_string(width) <> "\"
18-
y=\"0\"
19-
xlink:href=\"data:image/" <> image.extension <> ";base64," <> bit_array.base64_encode(
20-
data,
21-
False,
22-
) <> "\"/>"
12+
fn image(base64, image: image.ImageInformation, width) {
13+
string_builder.new()
14+
|> string_builder.append("<image height=\"")
15+
|> string_builder.append(int.to_string(image.height))
16+
|> string_builder.append("\" width=\"")
17+
|> string_builder.append(int.to_string(image.width))
18+
|> string_builder.append("\" x=\"")
19+
|> string_builder.append(int.to_string(width))
20+
|> string_builder.append("\" y=\"0\" xlink:href=\"data:image/")
21+
|> string_builder.append(image.extension)
22+
|> string_builder.append(";base64,")
23+
|> string_builder.append(base64)
24+
|> string_builder.append("\"/>")
25+
|> string_builder.to_string()
2326
}
2427

2528
fn images(image_cache, theme, digits, width, height, svgs) {
@@ -34,7 +37,10 @@ fn images(image_cache, theme, digits, width, height, svgs) {
3437
rest,
3538
width + cached.info.width,
3639
int.max(height, cached.info.height),
37-
string_builder.append(svgs, image(cached.data, cached.info, width)),
40+
string_builder.append(
41+
svgs,
42+
image(cached.base64, cached.info, width),
43+
),
3844
)
3945
_ -> images(image_cache, theme, rest, width, height, svgs)
4046
}
@@ -60,17 +66,19 @@ pub fn xml(image_cache, theme, number, padding) {
6066
string_builder.new(),
6167
)
6268

63-
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
64-
<svg
65-
height=\"" <> int.to_string(xml.height) <> "\"
66-
style=\"image-rendering: pixelated;\"
67-
version=\"1.1\"
68-
width=\"" <> int.to_string(xml.width) <> "\"
69-
xmlns=\"http://www.w3.org/2000/svg\"
70-
xmlns:xlink=\"http://www.w3.org/1999/xlink\"
71-
>
72-
<title>Mayu</title>
73-
74-
<g>" <> xml.xml <> "</g>
75-
</svg>"
69+
string_builder.new()
70+
|> string_builder.append(
71+
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><svg height=\"",
72+
)
73+
|> string_builder.append(int.to_string(xml.height))
74+
|> string_builder.append(
75+
"\" style=\"image-rendering: pixelated;\" version=\"1.1\" width=\"",
76+
)
77+
|> string_builder.append(int.to_string(xml.width))
78+
|> string_builder.append(
79+
"\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>Mayu</title><g>",
80+
)
81+
|> string_builder.append(xml.xml)
82+
|> string_builder.append("</g></svg>")
83+
|> string_builder.to_string()
7684
}

0 commit comments

Comments
 (0)