typst/svg2pdf

Existing font ignored

rgreinho opened this issue · 10 comments

I have an SVG flyer which uses the "Dharma Gothic E" font. This font is installed correctly on my system (see screenshot):

image

However when I attempt a conversion with svg2pdf it falls back to another font.

Reproducible example

I just created a simple SVG using Inkscape. It renders correctly in the browser:

image

but not the PDF:

image

Here is the code of the SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   width="74mm"
   height="52mm"
   viewBox="0 0 74 52"
   version="1.1"
   id="svg1"
   inkscape:version="1.3 (0e150ed, 2023-07-21)"
   sodipodi:docname="DharmaGothicE.svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <sodipodi:namedview
     id="namedview1"
     pagecolor="#ffffff"
     bordercolor="#000000"
     borderopacity="0.25"
     inkscape:showpageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:deskcolor="#d1d1d1"
     inkscape:document-units="mm"
     inkscape:zoom="2.0384855"
     inkscape:cx="374.54277"
     inkscape:cy="131.22487"
     inkscape:window-width="1392"
     inkscape:window-height="906"
     inkscape:window-x="2162"
     inkscape:window-y="389"
     inkscape:window-maximized="0"
     inkscape:current-layer="layer1" />
  <defs
     id="defs1">
    <rect
       x="71.271088"
       y="58.31271"
       width="165.21935"
       height="150.64117"
       id="rect1" />
  </defs>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <text
       xml:space="preserve"
       transform="scale(0.26458333)"
       id="text1"
       style="font-size:48px;white-space:pre;shape-inside:url(#rect1);fill:#000000"><tspan
         x="71.271484"
         y="252.74498"
         id="tspan6"><tspan
           dx="0 36.984375 30.375 28.828125 20.484375 46.6875"
           id="tspan5">Dharma</tspan></tspan></text>
    <text
       xml:space="preserve"
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.7px;font-family:Sans;-inkscape-font-specification:'Sans, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;fill:#000000;stroke-width:0.264583"
       x="59.11412"
       y="9.7057304"
       id="text2"><tspan
         sodipodi:role="line"
         id="tspan2"
         x="59.11412"
         y="9.7057304"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.7px;font-family:'Dharma Gothic E';-inkscape-font-specification:'Dharma Gothic E, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;text-align:center;text-anchor:middle">Dharma </tspan><tspan
         sodipodi:role="line"
         x="59.11412"
         y="25.741634"
         id="tspan3"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.7px;font-family:'Dharma Gothic E';-inkscape-font-specification:'Dharma Gothic E, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;text-align:center;text-anchor:middle">Gothic </tspan><tspan
         sodipodi:role="line"
         x="59.11412"
         y="41.777538"
         id="tspan4"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:12.7px;font-family:'Dharma Gothic E';-inkscape-font-specification:'Dharma Gothic E, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;text-align:center;text-anchor:middle">E</tspan></text>
  </g>
</svg>

I also tried doing it as code, basically copy/pasting the code from the CLI, and then the text does not appear in the converted PDF.

But if I iterate through the fonts, I do see all my Dharma variants, like for instance:

FaceInfo {
    id: ID(
        InnerId(
            42v1,
        ),
    ),
    source: File(
        "../assets/fonts/DharmaGothicExtended copy/DharmaGothicE-Regular.otf",
    ),
    index: 0,
    families: [
        (
            "Dharma Gothic E",
            English_UnitedStates,
        ),
    ],
    post_script_name: "DharmaGothicE-Regular",
    style: Normal,
    weight: Weight(
        400,
    ),
    stretch: Normal,
    monospaced: false,
}

I did try with another font (Montserrat which is very common), and everything worked fine.

Is there something else I need to do to make it work? Do some fonts require some specific parameters or a special treatment?

I mean the font you are seeing is in fact the same as in the reference picture (Dharma Gothic E), the only problem seems to be that for some reason it chooses the italic version of the font instead of the regular one. I can reproduce this on my system if I add the italic font to my system fonts as well, but if I only add the regular one I get the same result as in the reference picture. Not sure what is happening here, will try to investigate.

True, in this case it is the same font in italic, but in the flyer it is a completely different one:

It renders like this:

image

While it should be like that:

image

I have the same issue on Ubuntu 22.04.3 LTS. The font is installed correctly:
Dharma
But the generated pdf renders the text that is supposed to be Dharma Gothic E with a different font. Also, the pdf's properties only show the following fonts being used:

  • Montserrat-Medium
  • Montserrat-SemiBold
  • Montserrat-ExtraLight
  • Montserrat-Bold

But I guess I should see one more font listed (the one used to render "Canberra", the numbers, etc.).

In case that helps to remove potential setup problems, the python tool cairosvg, converts the svg file to PDF with the right fonts.

We probably should add some kind of command that shows all of the fonts that svg2pdf can find.

We probably should add some kind of command that shows all of the fonts that svg2pdf can find.

I took a quick stab at it. PR #45 takes care of it.

@LaurenzV Now that #45 got merged, how can we proceed to work on this issue? What type of info or help would you need from me?

You could probably try to render your SVG using resvg (which is the underlying library we use for conversion, including for the text). If doesn't work correctly there either, it's probably an upstream issue. If it works there but not with svg2pdf, then I'll try to investigate what we're doing wrong.

@LaurenzV I did find out that the problem was happening with ReSVG too, so I reported the issue upstream as well: RazrFalcon/resvg#702

This has already been fixed upstream IIRC. And I also created a new issue for font fallback (#65), so I'm closing this.