Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Text layouting utilities. 

3""" 

4 

5from .ft2font import KERNING_DEFAULT, LOAD_NO_HINTING 

6 

7 

8def layout(string, font, *, kern_mode=KERNING_DEFAULT): 

9 """ 

10 Render *string* with *font*. For each character in *string*, yield a 

11 (glyph-index, x-position) pair. When such a pair is yielded, the font's 

12 glyph is set to the corresponding character. 

13 

14 Parameters 

15 ---------- 

16 string : str 

17 The string to be rendered. 

18 font : FT2Font 

19 The font. 

20 kern_mode : int 

21 A FreeType kerning mode. 

22 

23 Yields 

24 ------ 

25 glyph_index : int 

26 x_position : float 

27 """ 

28 x = 0 

29 last_glyph_idx = None 

30 for char in string: 

31 glyph_idx = font.get_char_index(ord(char)) 

32 kern = (font.get_kerning(last_glyph_idx, glyph_idx, kern_mode) 

33 if last_glyph_idx is not None else 0) / 64 

34 x += kern 

35 glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING) 

36 yield glyph_idx, x 

37 x += glyph.linearHoriAdvance / 65536 

38 last_glyph_idx = glyph_idx