yWorks / svg2pdf.js

A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
MIT License
649 stars 98 forks source link

Implement nested <tspan> support #161

Closed Mrfence97 closed 3 years ago

Mrfence97 commented 3 years ago

Addresses issue #123.

Implementation is how you'd expect: using a recursive function to process each <tspan> in turn, the base case being when a child is simply a plain #text element. The context of the parent element is passed down to each child.

Note browsers seem to treat text chunks in a linear manner. That is to say that setting the x/y attribute of a nested <tspan> will impact any subsequent <tspan>s, regardless of whether they're a child, sibling or at a higher level. This effectively means that the collection of TextChunks can be stored as a 1D array, rather than a tree. See the third and fourth tests in the new spec SVG.

Things are complicated by the correct trimming of text when xml:space = "default". In particular, spaces need to be consolidated across elements in each TextChunk and between TextChunks. Browsers also treat xml:space differently:

This PR uses the Chrome convention.

Mrfence97 commented 3 years ago

Tests were expected to fail, I haven't pushed updated reference PDFs in case you wanted to review yourself. All are due to a better text trimming/space consolidation implementation: