pdf-raku / PDF-Font-Loader-raku

Font loader for the PDF tool-chain
Artistic License 2.0
1 stars 3 forks source link

Freetype thread safety issues #15

Closed dwarring closed 2 years ago

dwarring commented 2 years ago

I'm sometimes seeing issues when running pdf2image.raku from PDF::To::Cairo. For example:

david@box:~/git/PDF-To-Cairo-raku$ raku -I . bin/pdf2image.raku /tmp/out1.pdf 
saving page 1 -> PNG /tmp/out1-001.png...
saving page 9 -> PNG /tmp/out1-009.png...
saving page 17 -> PNG /tmp/out1-017.png...
saving page 25 -> PNG /tmp/out1-025.png...
saving page 33 -> PNG /tmp/out1-033.png...
saving page 41 -> PNG /tmp/out1-041.png...
saving page 49 -> PNG /tmp/out1-049.png...
loading font: Times-Bold -> /usr/share/fonts/opentype/urw-base35/NimbusRoman-Bold.otf
error processing glyph index: 44: FreeType Error: invalid argument
  in block  at /home/david/git/rakudo/install/share/perl6/site/sources/BB3ACC8ADEDFA6495C127C660154B220EF90E5A2 (PDF::Font::Loader::Enc) line 100
A worker in a parallel iteration (hyper or race) initiated here:
  in method save-as at /home/david/git/PDF-To-Cairo-raku/lib/PDF/To/Cairo.rakumod (PDF::To::Cairo) line 574
  in sub MAIN at bin/pdf2image.raku line 33
  in block <unit> at bin/pdf2image.raku line 18

Died at:
    bad Cairo status 1 CAIRO_STATUS_NO_MEMORY after ShowText(Raku by example 101) operation
      in sub  at /home/david/git/PDF-To-Cairo-raku/lib/PDF/To/Cairo.rakumod (PDF::To::Cairo) line 541
      in block  at /home/david/git/rakudo/install/share/perl6/site/sources/ADBE257FC9A2D570E7135DBB29F8E0E9C69FB6F9 (PDF::Content::Ops) line 947
      in method op at /home/david/git/rakudo/install/share/perl6/site/sources/ADBE257FC9A2D570E7135DBB29F8E0E9C69FB6F9 (PDF::Content::Ops) line 945
      in block  at /home/david/git/rakudo/install/share/perl6/site/sources/ADBE257FC9A2D570E7135DBB29F8E0E9C69FB6F9 (PDF::Content::Ops) line 1024
      in method ops at /home/david/git/rakudo/install/share/perl6/site/sources/ADBE257FC9A2D570E7135DBB29F8E0E9C69FB6F9 (PDF::Content::Ops) line 1023
      in method render at /home/david/git/rakudo/install/share/perl6/site/sources/94B207285FE37A6B2E4D3E0BD474EC06051A8163 (PDF::Content::Graphics) line 81
      in method render at /home/david/git/PDF-To-Cairo-raku/lib/PDF/To/Cairo.rakumod (PDF::To::Cairo) line 60
      in method save-as-image at /home/david/git/PDF-To-Cairo-raku/lib/PDF/To/Cairo.rakumod (PDF::To::Cairo) line 559
      in block  at /home/david/git/PDF-To-Cairo-raku/lib/PDF/To/Cairo.rakumod (PDF::To::Cairo) line 587

Freetype is not thread-safe for concurrent access to face objects, which seems to be going on here.

Investigation needed,

dwarring commented 2 years ago

The same Freetype font is being substituted for different fonts. Distinct Raku Font::Freetype::Face objects are being created so there's something else going on.

Maybe the native freetype library is caching on our behalf?

dwarring commented 2 years ago

Seems that the Raku Font::FreeType library can better support threads by directly using the cache as mentioned https://lists.gnu.org/archive/html/freetype-devel/2015-08/msg00046.html Interface described here https://freetype.org/freetype2/docs/reference/ft2-cache_subsystem.html

Over to you, Font::FreeType.

dwarring commented 2 years ago

Fixed (or greatly improved in release 0.6.3. Locks added with Font::FreeType 0.4.0. Using this + other tweaks for thread-safe glyph access.