vega / vl-convert

Utilities for converting Vega-Lite specs from the command line and Python
BSD 3-Clause "New" or "Revised" License
84 stars 9 forks source link

Text in pdf (converted from svg) always appears on top #147

Closed ohmyarch closed 2 weeks ago

ohmyarch commented 5 months ago
<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<text x="30" y="50" dy="0.76em" text-anchor="start" font-family="Arial" font-size="16.129032258064516" opacity="1" fill="#000000">
Test
</text>
<rect x="0" y="0" width="100" height="100" opacity="1" fill="#FFFFFF" stroke="none"/>
</svg>

svg: 图片

pdf: 图片

jonmmease commented 5 months ago

Thanks for raising the issue @ohmyarch, we should list this as a known limitation in the README.

As context, how are you using vl-convert, and in what kind of situation is this limitation causing issues for you?

ohmyarch commented 5 months ago

Draw a vector map to svg, and then use vl-convert-pdf to output it to pdf. Some elements in the map, such as titles and legends, need to be overlaid on the base map. But there may be text labels on the base map below them. Therefore, in similar scenarios, this bug or limitation will cause some display confusion.

ohmyarch commented 5 months ago

By the way, can this problem be avoided by using the original version of svg2pdf? I have a confusion about it. Its documentation tells me that text will be converted into shapes, but the same svg, such as the above example, only retains text elements, and uses svg2pdf to output, there will be nothing on the pdf.

jonmmease commented 5 months ago

Yeah, svg2pdf by itself will convert text to paths and should preserve proper layering of text under shapes.

vl-convert-pdf first runs svg2pdf without converting text to paths, so that svg2pdf renders everything except text. Then it performs its own logic to embed and overlay the text.

The long term solution here is probably to integrate text embedding logic into svg2pdf, but that will take a bit of effort. See https://github.com/typst/svg2pdf/issues/21 for more background.

jonmmease commented 3 months ago

Text embedding has been merged into svg2pdf in https://github.com/typst/svg2pdf/pull/64 (thanks @LaurenzV!), so after the next svg2pdf release we should be able to switch to this and drop our vl-convert-pdf crate which always layers text on top.

LaurenzV commented 3 months ago

Yeah, it should work just like that now! Also feel free to test it, if you want to try it before the release. The only caveat right now is that if a CFF font is selected for some text, it might result in rather large file sizes (see also https://github.com/typst/svg2pdf/issues/66). But it should work pretty well otherwise, all test cases that worked before still work now.

Not sure when the next release will be though, first we need a new resvg release and there are also some other smaller things I need to add before we can make a new release. But hopefully within the next 1-2 months.

jonmmease commented 3 months ago

Thanks for the context @LaurenzV! No rush from our side, I'll keep an eye out for release notifications. And I'll let you know if I find time to test it out before release.

jonmmease commented 2 weeks ago

Fixed in 1.5.0