[cairo] solutions for display text with Asian fonts

Uli Schlachter psychon at znc.in
Tue Jun 5 07:41:26 PDT 2012


On 05.06.2012 16:11, Jihe Wei wrote:
> Hi,Uli
> thank you.cairo_show_glyphs() works,can display english words correctly.
> but Asian fonts(Chinese, Japanese and Korean), how to, anyidea?

Welcome to today's lesson on unicode and utf8. Our test subject today are the
two characters(?) 中 and 文. As a first step, let's see how these are encoded in

"中" consists of the three bytes 0xe4, 0xb8, 0xad.
"文" consists of the three bytes 0xe6, 0x96, 0x87.

So, as we can see, the code points for these need three bytes when encoded in
utf8. As a next step, let's take a look at how not to parse this.

>     glyph_array_add_text(&glyphs, cr, "中文", 0.0);
>     while (*p) {
>         charcode = *p;
>         index = FT_Get_Char_Index(face, charcode);

This code grabs a single byte of input and tries to turn that into a glyph.
However, we just saw that our two characters each need three bytes, so obviously
this approach won't work. (If these are interpreted as code points, e.g. 0xe4
would be "Latin Small Letter A with diaeresis" aka "ä", see [0])

I don't know freetype, so I can only guess that fixing this would require you to
decode the utf8 into unicode code points which can then be passed to
FT_Get_Char_Index(). Also, no idea how right-to-left text should be handled, so
I can again only propose to use pango[1].


[0] https://en.wikipedia.org/wiki/C1_Controls_and_Latin-1_Supplement
[1] http://www.pango.org/
"Every once in a while, declare peace. It confuses the hell out of your enemies"
 - 79th Rule of Acquisition

More information about the cairo mailing list