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

quick fix of https://github.com/Kozea/CairoSVG/issues/317 #408

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Pandaaaa906
Copy link

a quick fix for #317

svg content:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="200" width="800" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

<rect width="100%" height="100%" fill="white"/>
<line x1="400" y1="0" x2="400" y2="200" style="stroke:red; stroke-width:1" />

<text style="font-family:Times; font-size:20;" x="400" y="40" text-anchor="middle">This is center-anchored regular text</text>

<text style="font-family:Times; font-size:20;" x="400" y="80" text-anchor="start">This is left-anchored <tspan font-style="italic">italic</tspan> text</text>

<text style="font-family:Times; font-size:20;" x="400" y="120" text-anchor="middle">This is center-anchored <tspan font-style="italic">italic</tspan> text</text>

<text style="font-family:Times; font-size:20;" x="400" y="160" text-anchor="end">This is right-anchored <tspan font-style="italic">italic</tspan> text</text>

</svg>

result:
asdfzcxvzx

@Pandaaaa906
Copy link
Author

might not be efficient
i've tried to add lru_cache to get_single_node_text_extends
but it will bring a weird offset

left: uncached
right: cached
屏幕截图 2023-12-28 162357

@liZe
Copy link
Member

liZe commented Dec 28, 2023

Thanks for the PR.

That’s funny, I think we just "solved" an equivalent bug in WeasyPrint, in Kozea/WeasyPrint@1460522. The code is a bit different, so we can’t just copy/paste the diff, but it may be possible to get some ideas.

(I didn’t look deeply at your PR yet, that was just an idea.)

@Pandaaaa906
Copy link
Author

Thx for the reply.

This 'text-anchor' problem seem to be a broad issue in svg rendering,
i found this also happened in Imagemagick(both using py-wand and php ones), like this

the main cause is the width in line 77, it should be the total width of the whole text tag it belongs to, by sum up each text part.

I think i also fix another issue. 😄
In the following svg, it defines a different font-size in tspan.
The original code get font size from surface.font_size which is 8 but not 6, it will cause the second tspan get a wrong x y position

...
<g>
    <text xml:space="preserve" text-anchor="end" text-rendering="geometricPrecision">
        <tspan x="220.05" y="199.25" font-family="Times New Roman" font-size="8" fill="rgb(0, 0, 0)">H</tspan>
        <tspan font-family="Times New Roman" dy="1.78" font-size="6" fill="rgb(0, 0, 0)">3</tspan>
        <tspan font-family="Times New Roman" dy="-1.78" font-size="8" fill="rgb(0, 0, 0)">C</tspan>
    </text>
</g>
...

addtionally i concerned about the alignment on y coordinate, but i don't have much cases to test.

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