Skip to content

Render actual text rather than paths in SVG (possibly with embedded fonts) #4702

Open

Description

@ongun-kanat

Description

While rendering text and fonts as paths in SVG images guarantee a consistent output, it makes them unselectable and unsearchable. The rendered path is also limited by the capability of the rendering engine and the font libraries Typst uses which are currently not as complete as the text rendering infrastructure that come with the OS and browsers.

An option in the typst CLI to opt for SVG <text> element output for the textual data would be very useful, especially when the file is to be viewed in the browser.

An optional improvement over this is embedding the actual fonts within the SVG file. This has a potential to bloat the SVG file, but could be useful for things like figures.

Use Case

I'm using Typst to generate figures containing shapes and tables. I would prefer the text in them to be selectable when viewed in a browser.

SVG files that use <text> elements are also smaller than the SVG files whose text is converted into paths.

Activity

  1. added
    feature request
    on Aug 7, 2024
  2. Enivex commented on Aug 7, 2024

    @Enivex
    Collaborator

    Someone more knowledgeable can correct me if I'm wrong, but I do not believe it's possible to embed fonts in an SVG, at least not in a way that's usable outside of a browser. Therefore such SVGs would be non-portable, which would be a non-starter I think.

    (Just a wild thought though: Would it be possible to put a transparent text layer on top, just for selectability purposes?)

  3. ongun-kanat commented on Aug 8, 2024

    @ongun-kanat
    Author

    Yeah it is true that the embedding is done via CSS styling. There are even tools that analyze the local fonts and embed the TTF / WOFF files inside SVGs:

    Displaying those fonts correctly of course requires a CSS capable renderer. However, <style> tag is a part of the SVG standard (optional for 1.1, mandatory for 2.0): https://svgwg.org/svg2-draft/styling.html#StyleElement . The level of implementation of CSS in viewers varies quite a bit of course. They usually support simple styling.

    I am okay with the invisible text layer as well. As I said it is optional to embed fonts.

    Just outputting the SVG with the text tag and hoping the user's system has the font is also an option but I can see that it can cause some unwanted results when the font is fed via CLI.

    PS: Here is an example file with an embedded font, if anybody wonders what the end result looks like. It seems like Github's own content security policy headers are blocking the inline CSS styling when the full link is followed, its thumbnail is correctly rendered though:

    svg embed

  4. Enivex commented on Aug 8, 2024

    @Enivex
    Collaborator

    SVG 2.0 isn't finalized (and I'm not entirely convinced it ever will be)

  5. LaurenzV commented on Aug 8, 2024

    @LaurenzV
    Collaborator

    The main problem is that exporting the text as strings in <text> elements would only work for the very simplest texts, but Typst supports a lot more (think subscripts, superscripts, specifying OpenType feature to use when shaping the text, and also math mode which (I think) has very complex positioning rules), and I don't think you can reliably represent those in SVG.

    Yes, one option could be to just overlay invisible text on top, but that feels very hacky and I'm not sure how well it would work for more complex scenarios.

  6. added
    svg
    on Aug 15, 2024
  7. Bequen commented on Apr 2, 2025

    @Bequen
    Contributor

    @LaurenzV Hello, I was also interested in this feature, so I made a quick experiment: https://github.com/Bequen/typst/tree/svg-render-text

    My issue was that the final HTML file was huge. In my case the one with glyphs was 2x bigger. And I do not think that I use that many frames. I was unable to do optimization as post processing, because it would be very hard to guess correct font and letters to optimize.

    I think size optimizations for web are very important. Sometimes for a price that it won't look the same everywhere. It is not PDF after all. This optimization could be at turned on with a CLI argument maybe?

    I tried to address the issue with Math by a very temporal solution. All math text uses hard coded font "New Computer Modern Math", so I check font family and render math as glyphs. But otherwise I switched it with <text></text>. I tried using text rendering even for math, and it kinda worked. The layout engine already does all the placing etc.

    My concern is with font-size. I used font size in pt from Font struct, which maps pretty accurately to px in HTML. I am not sure if that is not just coincidence.

    There is a little problem with font-family, because it was not working for many fonts for some reason. I just used style as temporary solution. I used Deduplicator for each fonts and create class for every font at the end inside <style></style>.

    For reference.

    Using glyphs:
    Image

    Using text:
    Image

  8. Bequen commented on Apr 14, 2025

    @Bequen
    Contributor

    you might like read this -- https://vecta.io/blog/using-fonts-in-svg

    Thanks, I did not realize that the <style> with @font-face needs to be directly inside the <svg> tag for font-name attribute to work. Unfortunately I cannot add @font-face during export to the <svg>, because I do not know the value for src attribute. For my tests on the HTML page I had @font-face defined in the outside css file, but I looks like it do not get picked up by embedded SVG.

    Anyway I noticed even Inkscape exports SVG files without fonts embedded and is using `"style: font-family: 'SomeFont'", which looks much better and simpler than using classes. I tried it and it works even for font faces defined outside of the SVG.

  9. terefang commented on Apr 14, 2025

    @terefang

    Thanks, I did not realize that the <style> with @font-face needs to be directly inside the <svg> tag for font-name attribute to work. Unfortunately I cannot add @font-face during export to the <svg>, because I do not know the value for src attribute. For my tests on the HTML page I had @font-face defined in the outside css file, but I looks like it do not get picked up by embedded SVG.

    you can embed fonts via data: urls.

  10. Bequen commented on Apr 18, 2025

    @Bequen
    Contributor

    Thanks, I did not realize that the <style> with @font-face needs to be directly inside the <svg> tag for font-name attribute to work. Unfortunately I cannot add @font-face during export to the <svg>, because I do not know the value for src attribute. For my tests on the HTML page I had @font-face defined in the outside css file, but I looks like it do not get picked up by embedded SVG.

    you can embed fonts via data: urls.

    Good idea! I tried that, but now the SVG is enormous. But it might be the correct solution:

    • it is portable
    • it uses pure, selectable text

    On the other hand, optimizing away these embedded fonts seems overly complicated and would even require some configuring. But for using it on the web, this optimization is a must while being quite inconvinient.

    So I propose to NOT embed the fonts by default and have a CLI option to enable it. Just like in Clang you can turn on and off some optimizations and warnings. What do you think?

    My argument is, that most people are going to use the SVGs for figures on HTML page. Embedding by default would require an extra step for most people. And as I said, not even Inkscape embeds the fonts.

    Is there some other reason for portability of SVG that I am missing? And if so, would it be that common?

  11. terefang commented on Apr 20, 2025

    @terefang

    maybe it may be good t also apply the same subsetting techniques to the font embedded in SVGs as in PDFs.

    that can reduce the size of large fonts significantly based on the number of characters used.

  12. Bequen commented on Apr 25, 2025

    @Bequen
    Contributor

    maybe it may be good t also apply the same subsetting techniques to the font embedded in SVGs as in PDFs.

    that can reduce the size of large fonts significantly based on the number of characters used.

    Unfortunately the method used for PDFs is PDF specific. There are some libraries that can do more general subsetting like

    Klippa is smaller and more focused on subsetting. Unfortunately I cannot even find it as a crate.

  13. added a commit that references this issue on Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestsvg

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions