JuliaGraphics / Luxor.jl

Simple drawings using vector graphics; Cairo "for tourists!"
http://juliagraphics.github.io/Luxor.jl/
Other
576 stars 72 forks source link

Some fonts only render in specfic sizes, even when loaded from svg and saved as-is #266

Closed yha closed 11 months ago

yha commented 11 months ago

Loading an svg with text elements and placing it with placeimage onto an svg drawing mangles the characters for many fonts. e.g., tmp-3, passed through:

using Luxor

function drawing(f, w, h, out = :svg)
    Drawing(w, h, out)
    f()
    finish()
    preview()
end

input = readsvg("3.svg")
draw = drawing(input.width, input.height, "tmp.svg") do 
    background("white")
    placeimage(input)
end

yields: tmp

I would expect text elements to pass through as-is without any rendering step.

I think it's the same problem when drawing text eplicitly:

drawing(1200, 600, "tmp.svg") do
    background("white")
    for (i, size) in enumerate(16:26)
        setfont("Helvetica", size)
        settext(string(size), Point(0, 50i))
        for (j, family) in enumerate(("Calibri", "Georgia", "Impact", "Garamond"))
            setfont(family, size)
            settext("$family $size", Point(250j - 150, 50i))
        end
    end
end

tmp

cormullion commented 11 months ago

Hi!

All the SVG handling is provided by Rsvg.jl, so if there are any problems, it's either a limitation of librsvg, a bug in Rsvg.jl, or a bug in how Luxor.jl calls Rsvg.jl. At the moment, it's difficult to say where the error might be. I'm not sure how well Rsvg handles fonts that are not embedded, but mentioned by name...

If you can't reproduce the error with Rsvg.jl, it will be something that can be investigated in Luxor.jl.

As for the whole fonts issue, it's very dependent on which platform you're on, which OS version, your font management, fontconfig settings, whether you use the Toy or Pro APIs, etc. For example, on my MacOS machine, your code gives:

tmp

For some reason, Impact doesn't want to appear here, with the Pro API, although:

using Luxor

@draw begin
 fontface("Impact")
 fontsize(200)
 text("Impact")
end

finds and draws Impact. I suspect fontconfig on MacOS, but I'm not too sure.

yha commented 11 months ago

If you can't reproduce the error with Rsvg.jl, it will be something that can be investigated in Luxor.jl.

I tried Rsvg+Cairo, and the result is consistent with Luxor. But I couldn't get either to simply transform the text elements from the svg as necessary without rendering them, which should have avoided the issue. I'm basically trying to use Luxor as a kind of programmatic SVG editor, combining different SVGs in various ways, and maybe drawing some simple shapes on top. I don't need it to do any rendering at all (like conversion of text to paths). Maybe Luxor/Cairo is not the right tool for that?

In any case, I have two different workarounds for now: working on a Linux machine, where I don't see these font issues, at least for Impact and Georgia. Or going the opposite route and converting all text to paths (using Inkscape CLI) before using them in Luxor.

cormullion commented 11 months ago

Maybe Luxor/Cairo is not the right tool for that?

Possibly not ... 🤷‍♂️ Cairo, Rsvg, Cairo.jl, and Rsvg.jl all have their limitations, particularly in font-handling, and Luxor will simply inherit all those limitations. It would be a major undertaking to add reliable platform-independent font handling, or to add better SVG interpreting... I think librsvg has or had a reputation of being quite buggy - but to be honest I think all SVG interpreters might have some compatibility issues, unless written by Adobe...

Purely graphical SVGs seem to work to some extent, but we can't output font information in SVG anyway (although PDF output is better...).

If there's nothing to change in Luxor.jl, feel free to close this issue -and good luck with your work!

stale[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

yha commented 11 months ago

Seems like this is a librsvg issue then, so I'm closing this. Thanks for your help

cormullion commented 11 months ago

Thanks - perhaps Rsvg.jl will get some love soon...