Kozea / cairocffi

CFFI-based cairo bindings for Python.
https://doc.courtbouillon.org/cairocffi
BSD 3-Clause "New" or "Revised" License
208 stars 54 forks source link

Extended unicode support? #182

Closed jenstroeger closed 1 year ago

jenstroeger commented 3 years ago

I’m trying to show_text() a string of UTF8 encoded Arabic or Farsi (both are rtl scripts) but I get only rectangles. I’ve tried with Apple’s Monaco and Google’s Noto Sans, both of which contain the proper glyphs.

The Fonts & Text section in the manual mentions that

At the moment cairocffi only supports cairo’s “toy” font selection API.

Does that mean that there’s no support to draw Arabic or Farsi glyphs?

liZe commented 3 years ago

Hello! Thanks for this bug report.

I can’t reproduce your problem. Here’s a simple script:

import cairocffi

surface = cairocffi.ImageSurface(cairocffi.FORMAT_ARGB32, 100, 100)
context = cairocffi.Context(surface)
context.move_to(10, 50)
context.select_font_face('DejaVu Sans')
context.show_text('صباح الخير')
context.fill()
surface.write_to_png('/tmp/out.png')

It gives me out

If you only get rectangles, it means that the characters are not in the font you use. You can try to display Roman characters with Monaco or Noto, and check that Cairo uses the right font. The font finding algorithm is not really forgiving, it’s possible that Cairo doesn’t find the right font if the name is not exactly the one it’s waiting for.

Edit: looks like the glyphes are not displayed in the right direction, show_text is probably not able to do this correctly. But at least, I don’t have rectangles.

jenstroeger commented 3 years ago

Thanks @liZe! Good to know that these glyphs can be rendered, then maybe something went awry when I specified/loaded the font. Is there a way of getting some debug information from Cairo into Python regarding the fonts it uses?

liZe commented 3 years ago

Is there a way of getting some debug information from Cairo into Python regarding the fonts it uses?

You can launch WeasyPrint with the FC_DEBUG environment variable set to 1 and see how Fontconfig decides which font to use.