CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

Custom fonts problem

Open lbluitt opened this issue 4 years ago • 2 comments

I'm converting SVG with custom fonts to PNG. This line works well in my local OS with the fonts installed import cairosvg png_content = cairosvg.svg2png(bytestring=svg_content)

However, the same code doesn't work on a docker image using Ubuntu as OS. The text font gets replaced by a default font even though the font files are located on the paths usr/local/share/fonts and usr/share/fonts and show up properly on the cache font lists (fc-list command)

lbluitt avatar May 11 '21 23:05 lbluitt

I fixed the error changing a bit the SVG file in order to use the Google fonts. Especially I used the font-weight to match the sub family font I want to use. You may find more information here

1. Rewrite fonts in the CSS.

From <style type="text/css"> .st1{font-family:'Roboto-Bold';} .st2{font-size:64px;} .st3{font-family:'Roboto-Medium';} .st4{font-size:49.2853px;} .st5{fill:#EA5920;} </style> to

<style type="text/css"> .st1{font-family:'Roboto';} .st2{font-size:64px;} .st3{font-size:49.2853px;} .st4{fill:#EA5920;} </style>

2. Add attribute to XML elements.

From <text class="st1 st2 st5">Your text</text> To <text class="st1 st2 st5" font-weight="700">Your text</text>

After doing that I can export a PNG image with the right Google fonts using CairoSVG

SyPRX avatar Oct 29 '21 15:10 SyPRX

As CairoSVG uses Fontconfig on Linux, you should get the same result as fc-match. Using fc-list is good to know if the font is installed, but fc-match sometimes gives strange results depending on your Fontconfig configuration.

liZe avatar Oct 30 '21 09:10 liZe