⭐ Moe-Counter Compatible Website Hit Counter Written in Gleam mayu.due.moe
hit-counter svg moe

feat(svg): Pre-encoding Base64 strings

fuwn.net 1819abb9 71570f95

verified
+39 -27
+6 -2
src/cache.gleam
··· 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 - CachedImage(data: BitArray, info: image.ImageInformation) 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 - CachedImage(data: image_data, info: info), 53 + CachedImage( 54 + base64: bit_array.base64_encode(image_data, False), 55 + info: info, 56 + ), 53 57 ) 54 58 Error(_) -> { 55 59 wisp.log_error(
+33 -25
src/svg.gleam
··· 1 1 import cache 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 - 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() 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 - string_builder.append(svgs, image(cached.data, cached.info, width)), 40 + string_builder.append( 41 + svgs, 42 + image(cached.base64, cached.info, width), 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 - "<?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() 76 84 }