import cache import gleam/int import gleam/list import gleam/string_builder.{type StringBuilder} import image type XmlImages { XmlImages(xml: StringBuilder, width: Int, height: Int) } fn append_image(svgs, base64, image: image.ImageInformation, x_offset) { string_builder.append( svgs, " int.to_string(image.height) <> "\" width=\"" <> int.to_string(image.width) <> "\" x=\"" <> int.to_string(x_offset) <> "\" y=\"0\" xlink:href=\"data:image/" <> image.extension <> ";base64," <> base64 <> "\"/>", ) } fn images(image_cache, theme, digits, width, height, svgs) { case digits { [] -> XmlImages(svgs, width, height) [digit, ..rest] -> case cache.get_image(image_cache, theme, digit) { Ok(cached_image) -> images( image_cache, theme, rest, width + cached_image.info.width, int.max(height, cached_image.info.height), append_image(svgs, cached_image.base64, cached_image.info, width), ) _ -> images(image_cache, theme, rest, width, height, svgs) } } } pub fn xml(image_cache, theme, number, padding) { let xml = images( image_cache, theme, { let assert Ok(digits) = int.digits(int.absolute_value(number), 10) let digits_padding = padding - list.length(digits) case digits_padding > 0 { True -> list.append(list.repeat(0, digits_padding), digits) False -> digits } }, 0, 0, string_builder.new(), ) 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_builder(xml.xml) |> string_builder.append("") |> string_builder.to_string() }