CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

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

Open Pandaaaa906 opened this issue 2 years ago • 3 comments

a quick fix for https://github.com/Kozea/CairoSVG/issues/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 avatar Dec 28 '23 08:12 Pandaaaa906

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

Pandaaaa906 avatar Dec 28 '23 10:12 Pandaaaa906

Thanks for the PR.

That’s funny, I think we just "solved" an equivalent bug in WeasyPrint, in https://github.com/Kozea/WeasyPrint/commit/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.)

liZe avatar Dec 28 '23 13:12 liZe

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.

Pandaaaa906 avatar Dec 29 '23 02:12 Pandaaaa906