Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nested <tspan> support #161

Merged
merged 4 commits into from Nov 24, 2020

Conversation

Mrfence97
Copy link
Contributor

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:

  • Chrome: xml:space can be freely changed and all child elements inherit
  • Firefox: xml:space once set to preserve cannot be set back to default in any child element

This PR uses the Chrome convention.

- Uses recursive function to process nested `<tspan>`s
- Improved text trimming support when `xml:space = "default"`
- Tests also added
@Mrfence97
Copy link
Contributor Author

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:

  • display-none-and-visibility-inheritance: space character after the 'visible' is no longer rendered
  • title-element: spaces by themselves and spaces around the 'Render Me!` text are no longer rendered
  • text-placement: The original reference PDF doesn't match browser renderings of the SVG as some leading/trailing spaces were included. Easiest to see looking at how the first d in 'middle' in text-anchor="middle", two tspans is perfectly aligned with the blue line, but in browser renderings (and svg2pdf after this PR), the d is offset:
    image

Copy link
Member

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this pull request! Looks very good to me. I made a few very minor cosmetic changes and updated the PDF references.

Thanks again for your awesome contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants