44
55from __future__import annotations
66
7- import dataclasses
7+ from collections . abc import Iterator
88
99from .import _api
10- from .ft2font import FT2Font ,GlyphIndexType , Kerning ,LoadFlags
10+ from .ft2font import FT2Font ,CharacterCodeType , LayoutItem ,LoadFlags
1111
1212
13- @dataclasses .dataclass (frozen = True )
14- class LayoutItem :
15- ft_object :FT2Font
16- char :str
17- glyph_index :GlyphIndexType
18- x :float
19- prev_kern :float
20-
21-
22- def warn_on_missing_glyph (codepoint ,fontnames ):
13+ def warn_on_missing_glyph (codepoint :CharacterCodeType ,fontnames :str ):
2314_api .warn_external (
2415f"Glyph{ codepoint } "
2516f"({ chr (codepoint ).encode ('ascii' ,'namereplace' ).decode ('ascii' )} ) "
2617f"missing from font(s){ fontnames } ." )
2718
2819
29- def layout (string ,font ,* ,features = None ,kern_mode = Kerning .DEFAULT ,language = None ):
20+ def layout (string :str ,font :FT2Font ,* ,
21+ features :tuple [str ]| None = ...,
22+ language :str | list [tuple [str ,int ,int ]]| None = ...
23+ )-> Iterator [LayoutItem ]:
3024"""
3125 Render *string* with *font*.
3226
@@ -41,8 +35,6 @@ def layout(string, font, *, features=None, kern_mode=Kerning.DEFAULT, language=N
4135 The font.
4236 features : tuple of str, optional
4337 The font features to apply to the text.
44- kern_mode : Kerning
45- A FreeType kerning mode.
4638 language : str, optional
4739 The language of the text in a format accepted by libraqm, namely `a BCP47
4840 language code <https://www.w3.org/International/articles/language-tags/>`_.
@@ -51,20 +43,8 @@ def layout(string, font, *, features=None, kern_mode=Kerning.DEFAULT, language=N
5143 ------
5244 LayoutItem
5345 """
54- x = 0
55- prev_glyph_index = None
56- char_to_font = font ._get_fontmap (string )# TODO: Pass in features and language.
57- base_font = font
58- for char in string :
59- # This has done the fallback logic
60- font = char_to_font .get (char ,base_font )
61- glyph_index = font .get_char_index (ord (char ))
62- kern = (
63- base_font .get_kerning (prev_glyph_index ,glyph_index ,kern_mode )/ 64
64- if prev_glyph_index is not None else 0.
65- )
66- x += kern
67- glyph = font .load_glyph (glyph_index ,flags = LoadFlags .NO_HINTING )
68- yield LayoutItem (font ,char ,glyph_index ,x ,kern )
69- x += glyph .linearHoriAdvance / 65536
70- prev_glyph_index = glyph_index
46+ for raqm_item in font ._layout (string ,LoadFlags .NO_HINTING ,
47+ features = features ,language = language ):
48+ raqm_item .ft_object .load_glyph (raqm_item .glyph_index ,
49+ flags = LoadFlags .NO_HINTING )
50+ yield raqm_item