1 | # coding=utf-8
|
---|
2 |
|
---|
3 | import ctypes
|
---|
4 | import pango
|
---|
5 | import pangocairo
|
---|
6 |
|
---|
7 | context = pangocairo.cairo_font_map_get_default().create_context()
|
---|
8 |
|
---|
9 | fontdesc = pango.FontDescription('Deja Vu Sans 24')
|
---|
10 | #fontdesc = pango.FontDescription('Tex Gyre Pagella 24')
|
---|
11 |
|
---|
12 | class PangoGlyphInfo(ctypes.Structure):
|
---|
13 | _fields_ = [
|
---|
14 | ("glyph", ctypes.c_uint),
|
---|
15 | ("geometry_width", ctypes.c_int),
|
---|
16 | ("geometry_x_offset", ctypes.c_int),
|
---|
17 | ("geometry_y_offset", ctypes.c_int),
|
---|
18 | ("attr", ctypes.c_uint)
|
---|
19 | ]
|
---|
20 |
|
---|
21 | class PangoGlyphString(ctypes.Structure):
|
---|
22 | _fields_ = [
|
---|
23 | ("num_glyphs", ctypes.c_int),
|
---|
24 | ("glyphs", ctypes.POINTER(PangoGlyphInfo)),
|
---|
25 | ("log_clusters", ctypes.c_void_p)
|
---|
26 | ]
|
---|
27 |
|
---|
28 | layout = pango.Layout(context)
|
---|
29 | layout.set_font_description(fontdesc)
|
---|
30 | layout.set_text('a á ā ā́ a')
|
---|
31 | for (item, glyphs) in layout.get_line(0).runs:
|
---|
32 | glyphstr = ctypes.cast(hash(glyphs), ctypes.POINTER(PangoGlyphString))[0]
|
---|
33 | for i in range(glyphstr.num_glyphs):
|
---|
34 | glyph = glyphstr.glyphs[i]
|
---|
35 | print glyph.glyph, glyph.geometry_x_offset, glyph.geometry_y_offset
|
---|
36 |
|
---|