JuliaGraphics / Luxor.jl

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

Threads doesn't work for settext with font warning #242

Closed oheil closed 2 years ago

oheil commented 2 years ago

@guo-yong-zhi wrote:

I found that it doesn't work for settext. If I delete the settext line, the code runs fine.

using Luxor

function make_drawing()
    Drawing(200, 200, :image)
    setcolor(1, 0, 0)
    poly([Point(1,10), Point(100,110), Point(50,50)], close=true, action=:fill) 
    settext("abcd", Point(50,50), halign="center", valign="center")
    m = image_as_matrix()    
    finish()
    return m
end
Threads.@threads for i = 1:1000
    make_drawing()
end

results in

(process:22772): Pango-WARNING **: 11:31:06.236: couldn't load font "serif Not-Rotated 12", falling back to "Sans Not-Rotated 12", expect ugly output.
(process:22772): Pango-WARNING **: 11:31:06.236: couldn't load font "Sans Not-Rotated 12", falling back to "Sans Not-Rotated 12", expect ugly output.

No threads and all is fine, no warnings.

]st

[ae8d54c2] Luxor v3.5.0
[36c8627f] Pango_jll v1.42.4+10 ⚲
cormullion commented 2 years ago

I can't reproduce this on MacOS. I tried hard to break it - switching between different fonts all the time, running with 16 threads ... - but it behaved impeccably all the time:

function make_drawing(i)
    Drawing(200, 200, "/tmp/t-$(i).png")
    origin()
    background("black")
    fontlist = ["Zapfino", "Bodoni 72 Oldstyle", "Comic Sans MS", "JuliaMono Black"]
    setfont(fontlist[mod1(Threads.threadid() + rand(1:4), 4)], 35)
    sethue("gold")
    settext("$(i) - $(Threads.threadid())", O, halign="center", valign="center")
    finish()
end
Threads.@threads for i = 1:100
    make_drawing(i)
end
Screenshot 2022-07-30 at 16 35 27

If you can reliably reproduce it on Windows (without your pinned Pango_jll perhaps?), we'll have to see if someone can reproduce it on Unix. The range of possible causes is probably too wide at the moment.

oheil commented 2 years ago
versioninfo()

Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
JULIA_NUM_THREADS = auto

Perhaps it is Windows specific. So far I found those related: https://bugs.freedesktop.org/show_bug.cgi?id=73012 https://github.com/lovell/sharp/issues/1277

But it's not yet clear what the current status really is now. I think for sure it's not a Luxor issue, but a Cario and/or Pango issue. But perhaps we can find some kind of workaround to make it thread safe for us.

oheil commented 2 years ago

Related: https://gitlab.freedesktop.org/cairo/cairo/-/issues/190

oheil commented 2 years ago

Seems to be quite an old issue: https://gitlab.gnome.org/GNOME/pango/-/issues/?search=thread&sort=created_date&state=opened&first_page_size=20

oheil commented 2 years ago

I think I was wrong with my confirmation. The warning is only printed once, so I thought, in the second run without threads, no threads, no warning means no problem, but it was no warning because it already went out.

But actually I can't reproduce it anymore except for the missing font in general when using "Aachen"-Font.

@guo-yong-zhi , could you check again, if your problem is perhaps just a missing font? New REPL with this:

using Luxor

function make_drawing()
    Drawing(200, 200, :image)
    setcolor(1, 0, 0)
    poly([Point(1,10), Point(100,110), Point(50,50)], close=true, action=:fill) 
    settext("abcd", Point(50,50), halign="center", valign="center")
    m = image_as_matrix()    
    finish()
    return
end

make_drawing()

should give the same warning about missing font.

cormullion commented 2 years ago

I never see any warnings for missing fonts on MacOS - the Aachen font doesn't exist on my system either so I get Helvetica by default (it should have been "AachenStd-Bold" for the toy API).

It's not surprising if it's a font issue.

Some similar issues:

https://github.com/Automattic/node-canvas/issues/1608

https://github.com/Automattic/node-canvas/issues/1643

guo-yong-zhi commented 2 years ago

In a New REPL, julia -t 4

using Luxor

function make_drawing()
    Drawing(200, 200, :image)
    setcolor(1, 0, 0)
    poly([Point(1,10), Point(100,110), Point(50,50)], close=true, action=:fill) 
    settext("abcd", Point(50,50), halign="center", valign="center")
    m = image_as_matrix()    
    finish()
    return
end

make_drawing()

it gives no warning. Then, I run

for i = 1:1000
    make_drawing()
end

still no warning, then I run

Threads.@threads for i = 1:1000
    make_drawing()
end

results in (process:27156): Pango-WARNING **: 09:06:29.574: All font fallbacks failed!!!! ... If I start julia with julia -t 1, the WARNING is gone.


I pinned Pango_jll to v1.42.4 because of https://github.com/guo-yong-zhi/WordCloud-Gallery/issues/1. I try to free Pango_jll and up Pango_jll, that is [36c8627f] ↑ Pango_jll v1.42.4+10 ⇒ v1.50.3+0, then all the warnings are gone. Everything goes well. Weird.

guo-yong-zhi commented 2 years ago

If you confirm that there is no warning with Pango_jll v1.50.3 on windows, I think this issue can be closed. @oheil

oheil commented 2 years ago

I have reproduced your exact finding. There seems to be a threading problem in Pango_jll v1.42.4 which is resolved in v1.50.3 Thanx for your report, this is all good to know!