You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Render actual text rather than paths in SVG (possibly with embedded fonts) #4702
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.
Reacted by rkeulemans, Andrew Voynov, HK-SHAO, Luc Briand, Timothy Cyrus, Ghost4Man, m4dh0rs3, Andy Li, Erik Verheij, Spoonbaker and 9 more
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?)
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:
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:
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.
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>.
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.
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.
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?
Activity
Enivex commentedon Aug 7, 2024
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?)
ongun-kanat commentedon Aug 8, 2024
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:
Enivex commentedon Aug 8, 2024
SVG 2.0 isn't finalized (and I'm not entirely convinced it ever will be)
LaurenzV commentedon Aug 8, 2024
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.
Render actual text in SVG instead of Glyphs (typst#4702)
Render actual text in SVG instead of Glyphs (typst#4702)
Bequen commentedon Apr 2, 2025
@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
Fontstruct, which maps pretty accurately topxin 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 usedDeduplicatorfor each fonts and create class for every font at the end inside<style></style>.For reference.
Using glyphs:

Using text:

terefang commentedon Apr 14, 2025
you might like read this -- https://vecta.io/blog/using-fonts-in-svg
Bequen commentedon Apr 14, 2025
Thanks, I did not realize that the
<style>with@font-faceneeds to be directly inside the<svg>tag forfont-nameattribute to work. Unfortunately I cannot add@font-faceduring export to the<svg>, because I do not know the value forsrcattribute. For my tests on the HTML page I had@font-facedefined 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.
terefang commentedon Apr 14, 2025
you can embed fonts via
data:urls.Bequen commentedon Apr 18, 2025
Good idea! I tried that, but now the SVG is enormous. But it might be the correct solution:
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?
terefang commentedon Apr 20, 2025
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.
Bequen commentedon Apr 25, 2025
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.
Embed font (typst#4702)