From a46f0f2fd84e33e7cbc74e1885961cdb9accb31c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 1 Jan 2026 16:57:24 -0800 Subject: feat(svg): Pre-encoding Base64 strings --- src/cache.gleam | 8 ++++++-- src/svg.gleam | 58 ++++++++++++++++++++++++++++++++------------------------- 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) { - " 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(" 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(), ) - " - 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\" - > - Mayu - - " <> xml.xml <> " - " + string_builder.new() + |> string_builder.append( + " 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\">Mayu", + ) + |> string_builder.append(xml.xml) + |> string_builder.append("") + |> string_builder.to_string() } -- cgit v1.2.3