aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-01 16:57:24 -0800
committerFuwn <[email protected]>2026-01-01 16:57:24 -0800
commita46f0f2fd84e33e7cbc74e1885961cdb9accb31c (patch)
tree273309444b8012ee8e3971657bc355cba776f034 /src
parent2ed2e42df88745bcdb158b068fee1338740bd8a0 (diff)
downloadmayu-a46f0f2fd84e33e7cbc74e1885961cdb9accb31c.tar.xz
mayu-a46f0f2fd84e33e7cbc74e1885961cdb9accb31c.zip
feat(svg): Pre-encoding Base64 strings
Diffstat (limited to 'src')
-rw-r--r--src/cache.gleam8
-rw-r--r--src/svg.gleam58
2 files changed, 39 insertions, 27 deletions
diff --git a/src/cache.gleam b/src/cache.gleam
index 13c3b05..ccba2a8 100644
--- a/src/cache.gleam
+++ b/src/cache.gleam
@@ -1,3 +1,4 @@
+import gleam/bit_array
import gleam/dict.{type Dict}
import gleam/int
import gleam/list
@@ -8,7 +9,7 @@ import simplifile
import wisp
pub type CachedImage {
- CachedImage(data: BitArray, info: image.ImageInformation)
+ CachedImage(base64: String, info: image.ImageInformation)
}
pub type ThemeCache =
@@ -49,7 +50,10 @@ pub fn load_themes() {
dict.insert(
accumulated_digits,
digit,
- CachedImage(data: image_data, info: info),
+ CachedImage(
+ base64: bit_array.base64_encode(image_data, False),
+ info: info,
+ ),
)
Error(_) -> {
wisp.log_error(
diff --git a/src/svg.gleam b/src/svg.gleam
index 884d6cf..0178dbd 100644
--- a/src/svg.gleam
+++ b/src/svg.gleam
@@ -1,5 +1,4 @@
import cache
-import gleam/bit_array
import gleam/int
import gleam/list
import gleam/option.{Some}
@@ -10,16 +9,20 @@ type XmlImages {
XmlImages(xml: String, width: Int, height: Int)
}
-fn image(data, image: image.ImageInformation, width) {
- "<image
- height=\"" <> int.to_string(image.height) <> "\"
- width=\"" <> int.to_string(image.width) <> "\"
- x=\"" <> int.to_string(width) <> "\"
- y=\"0\"
- xlink:href=\"data:image/" <> image.extension <> ";base64," <> bit_array.base64_encode(
- data,
- False,
- ) <> "\"/>"
+fn image(base64, image: image.ImageInformation, width) {
+ string_builder.new()
+ |> string_builder.append("<image height=\"")
+ |> string_builder.append(int.to_string(image.height))
+ |> string_builder.append("\" width=\"")
+ |> string_builder.append(int.to_string(image.width))
+ |> string_builder.append("\" x=\"")
+ |> string_builder.append(int.to_string(width))
+ |> string_builder.append("\" y=\"0\" xlink:href=\"data:image/")
+ |> string_builder.append(image.extension)
+ |> string_builder.append(";base64,")
+ |> string_builder.append(base64)
+ |> string_builder.append("\"/>")
+ |> string_builder.to_string()
}
fn images(image_cache, theme, digits, width, height, svgs) {
@@ -34,7 +37,10 @@ fn images(image_cache, theme, digits, width, height, svgs) {
rest,
width + cached.info.width,
int.max(height, cached.info.height),
- string_builder.append(svgs, image(cached.data, cached.info, width)),
+ string_builder.append(
+ svgs,
+ image(cached.base64, cached.info, width),
+ ),
)
_ -> images(image_cache, theme, rest, width, height, svgs)
}
@@ -60,17 +66,19 @@ pub fn xml(image_cache, theme, number, padding) {
string_builder.new(),
)
- "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
- <svg
- height=\"" <> int.to_string(xml.height) <> "\"
- style=\"image-rendering: pixelated;\"
- version=\"1.1\"
- width=\"" <> int.to_string(xml.width) <> "\"
- xmlns=\"http://www.w3.org/2000/svg\"
- xmlns:xlink=\"http://www.w3.org/1999/xlink\"
- >
- <title>Mayu</title>
-
- <g>" <> xml.xml <> "</g>
- </svg>"
+ string_builder.new()
+ |> string_builder.append(
+ "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><svg height=\"",
+ )
+ |> string_builder.append(int.to_string(xml.height))
+ |> string_builder.append(
+ "\" style=\"image-rendering: pixelated;\" version=\"1.1\" width=\"",
+ )
+ |> string_builder.append(int.to_string(xml.width))
+ |> string_builder.append(
+ "\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>Mayu</title><g>",
+ )
+ |> string_builder.append(xml.xml)
+ |> string_builder.append("</g></svg>")
+ |> string_builder.to_string()
}