svgtypst

typst svg string as image, deprecation warning


I have a string in typst that I want to render. Since a couple of versions, I get the deprecation warning that "image.decode is deprecated, directly pass bytes to image instead".

I have the following code which either works but throws warning #image.decode or does not compile #image(svg)

// a bootstrap icon
#let svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-7-circle\" viewBox=\"0 0 16 16\">
  <path d=\"M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8m15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0M5.37 5.11V4.001h5.308V5.15L7.42 12H6.025l3.317-6.82v-.07H5.369Z\"/>
</svg>"
#image.decode(svg) // throws deprecation warning but works
#image(svg) // does not compile: file not found

What is the right way to render the svg string directly?

Note I don't want to save the string to file first as I have many icons that I want to render and it feels over the top.


Solution

  • pass bytes ...

    The key is to wrap the string in bytes, which works without deprecation warnings:

    #let svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-7-circle\" viewBox=\"0 0 16 16\">
      <path d=\"M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8m15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0M5.37 5.11V4.001h5.308V5.15L7.42 12H6.025l3.317-6.82v-.07H5.369Z\"/>
    </svg>"
    #image(bytes(svg)) // Compiles without warnings