3

I am trying to create an svg with one single text element. The size of the svg should be determined solely by the font size of the text, and it should fit the text perfectly. The closest I have come to what I am looking for is this:

             txt_img = Vips::Image.svgload_buffer %(
              <svg height="#{font_size}*2">
                <g>
                <text
                    font-family="some value"
                    font-size="#{font_size}"
                    font-weight="some value"
                    fill="some value"
                    fill-opacity="some value"
                    stroke="some value"
                    stroke-width="some value"
                    dominant-baseline="hanging"
                    text-anchor="start"
                    x="0"
                    y="#{font_size}"
                  >
                  #{txt}
                  </text>
                </g>
               </svg>
             )

But, as you can see in the attached image, the bottom of the text gets cut off, particularly the letters that hang below the baseline. How can I size the svg to perfectly fit the text? Since this is not being rendered in a browser environment, the solution must not involve css.

Also note that the output is being rendered with Vips, which does not offer an svg saver option, which is why the output is in png format.

[!][text rendered to png][1]][1] [1]: https://i.stack.imgur.com/Bx7gJ.png

adam tropp
  • 674
  • 7
  • 22
  • Could you explain your larger goal? If you just want to render text to a bitmap, have a look at `Vips::Image.text`, it can probably do what you want. – jcupitt Jun 22 '20 at 21:50
  • @jcupitt I am trying to perfectly recreate an svg that was initially rendered in the client. I tried using vips::image.text, the issue is that I cannot see how to get it to respect svg properties like stroke-width, stroke-color, and fill-color, which would essentially amount to making bubble letters. The issue I am having with the svg rendering seems to be that librsvg does not parse the dominant-baseline property – adam tropp Jun 22 '20 at 21:55
  • Additionally, librsvg does not seem to support loading external fonts, not local to the filesystem, which I would need as well ( I am aware that vips::image.text supports loading fonts via a fontfile argument, but again, I cannot see how to make it support my use case for the above reasons) – adam tropp Jun 22 '20 at 22:00
  • Ah I understand. Yes, that sounds tricky :( – jcupitt Jun 23 '20 at 05:32

0 Answers0