Li Wang
Li Wang

Reputation: 29

How to render chinese characters and other languages like arabic with cairo in python?

it seems that cairo can't correctly render text like characters in chinese and arabic. Could anyone tell me how to render these texts in cairo? Can pango with cairo in python render these characters? Thanks!

Upvotes: 1

Views: 447

Answers (2)

Uli Schlachter
Uli Schlachter

Reputation: 9877

According to cairo's FAQ, the answer is "use PangoCairo":

[...] you learn that cairo_show_text is part of cairo's "toy" text API. It's fine for quick demos, and for learning how to use cairo, but it's not intended for use in actual applications.

It's not hard to run into some of the limitations of cairo_show_text.

[...]

Fortunately, the pango library exists and does do sophisticated text layout, shaping, etc. and integrates very nicely with cairo. We heartily recommend that "real" applications wanting to display text with cairo use pango to do it.

Upvotes: 1

ardget
ardget

Reputation: 2651

If you select an appropriate font face, you should be able to draw the texts straightforwardly.

cr.select_font_face('Noto Sans CJK SC')
cr.move_to(100, 100)
cr.set_source_rgb(0, 0, 0)

cr.show_text('好吧')

To list available fonts, type in console:

$ fc-list : family

Upvotes: 1

Related Questions