tangled
alpha
login
or
join now
fuwn.net
/
mayu
1
fork
atom
⭐ Moe-Counter Compatible Website Hit Counter Written in Gleam
mayu.due.moe
hit-counter
svg
moe
1
fork
atom
overview
issues
pulls
pipelines
feat(svg): Pre-encoding Base64 strings
fuwn.net
2 months ago
1819abb9
71570f95
verified
This commit was signed with the committer's
known signature
.
fuwn.net
SSH Key Fingerprint:
SHA256:VPdFPyPbd6JkoMyWUdZ/kkTcIAt3sxjXD2XSAZ7FYC4=
+39
-27
2 changed files
expand all
collapse all
unified
split
src
cache.gleam
svg.gleam
+6
-2
src/cache.gleam
···
1
1
+
import gleam/bit_array
1
2
import gleam/dict.{type Dict}
2
3
import gleam/int
3
4
import gleam/list
···
8
9
import wisp
9
10
10
11
pub type CachedImage {
11
11
-
CachedImage(data: BitArray, info: image.ImageInformation)
12
12
+
CachedImage(base64: String, info: image.ImageInformation)
12
13
}
13
14
14
15
pub type ThemeCache =
···
49
50
dict.insert(
50
51
accumulated_digits,
51
52
digit,
52
52
-
CachedImage(data: image_data, info: info),
53
53
+
CachedImage(
54
54
+
base64: bit_array.base64_encode(image_data, False),
55
55
+
info: info,
56
56
+
),
53
57
)
54
58
Error(_) -> {
55
59
wisp.log_error(
+33
-25
src/svg.gleam
···
1
1
import cache
2
2
-
import gleam/bit_array
3
2
import gleam/int
4
3
import gleam/list
5
4
import gleam/option.{Some}
···
10
9
XmlImages(xml: String, width: Int, height: Int)
11
10
}
12
11
13
13
-
fn image(data, image: image.ImageInformation, width) {
14
14
-
"<image
15
15
-
height=\"" <> int.to_string(image.height) <> "\"
16
16
-
width=\"" <> int.to_string(image.width) <> "\"
17
17
-
x=\"" <> int.to_string(width) <> "\"
18
18
-
y=\"0\"
19
19
-
xlink:href=\"data:image/" <> image.extension <> ";base64," <> bit_array.base64_encode(
20
20
-
data,
21
21
-
False,
22
22
-
) <> "\"/>"
12
12
+
fn image(base64, image: image.ImageInformation, width) {
13
13
+
string_builder.new()
14
14
+
|> string_builder.append("<image height=\"")
15
15
+
|> string_builder.append(int.to_string(image.height))
16
16
+
|> string_builder.append("\" width=\"")
17
17
+
|> string_builder.append(int.to_string(image.width))
18
18
+
|> string_builder.append("\" x=\"")
19
19
+
|> string_builder.append(int.to_string(width))
20
20
+
|> string_builder.append("\" y=\"0\" xlink:href=\"data:image/")
21
21
+
|> string_builder.append(image.extension)
22
22
+
|> string_builder.append(";base64,")
23
23
+
|> string_builder.append(base64)
24
24
+
|> string_builder.append("\"/>")
25
25
+
|> string_builder.to_string()
23
26
}
24
27
25
28
fn images(image_cache, theme, digits, width, height, svgs) {
···
34
37
rest,
35
38
width + cached.info.width,
36
39
int.max(height, cached.info.height),
37
37
-
string_builder.append(svgs, image(cached.data, cached.info, width)),
40
40
+
string_builder.append(
41
41
+
svgs,
42
42
+
image(cached.base64, cached.info, width),
43
43
+
),
38
44
)
39
45
_ -> images(image_cache, theme, rest, width, height, svgs)
40
46
}
···
60
66
string_builder.new(),
61
67
)
62
68
63
63
-
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
64
64
-
<svg
65
65
-
height=\"" <> int.to_string(xml.height) <> "\"
66
66
-
style=\"image-rendering: pixelated;\"
67
67
-
version=\"1.1\"
68
68
-
width=\"" <> int.to_string(xml.width) <> "\"
69
69
-
xmlns=\"http://www.w3.org/2000/svg\"
70
70
-
xmlns:xlink=\"http://www.w3.org/1999/xlink\"
71
71
-
>
72
72
-
<title>Mayu</title>
73
73
-
74
74
-
<g>" <> xml.xml <> "</g>
75
75
-
</svg>"
69
69
+
string_builder.new()
70
70
+
|> string_builder.append(
71
71
+
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><svg height=\"",
72
72
+
)
73
73
+
|> string_builder.append(int.to_string(xml.height))
74
74
+
|> string_builder.append(
75
75
+
"\" style=\"image-rendering: pixelated;\" version=\"1.1\" width=\"",
76
76
+
)
77
77
+
|> string_builder.append(int.to_string(xml.width))
78
78
+
|> string_builder.append(
79
79
+
"\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>Mayu</title><g>",
80
80
+
)
81
81
+
|> string_builder.append(xml.xml)
82
82
+
|> string_builder.append("</g></svg>")
83
83
+
|> string_builder.to_string()
76
84
}