1
1
import enum
2
2
import mmap
3
3
import sys
4
+ from dataclasses import dataclass
5
+ from typing import Tuple
4
6
5
7
from xkbcommon ._ffi import ffi , lib
6
8
@@ -115,6 +117,11 @@ class XKBComposeTableCreationFailure(XKBError):
115
117
pass
116
118
117
119
120
+ class XKBComposeTableIteratorCreationFailure (XKBError ):
121
+ """Unable to create a compose table iterator."""
122
+ pass
123
+
124
+
118
125
class XKBComposeStateCreationFailure (XKBError ):
119
126
"""Unable to create a compose state."""
120
127
pass
@@ -1266,8 +1273,29 @@ def __init__(self, context, pointer, load_method):
1266
1273
self ._table = ffi .gc (
1267
1274
pointer , _keepref (lib , lib .xkb_compose_table_unref ))
1268
1275
1269
- # Methods to access and iterate over the compose table will be
1270
- # added for release 1.6
1276
+ def __iter__ (self ):
1277
+ local_lib = lib
1278
+ iterator = local_lib .xkb_compose_table_iterator_new (self ._table )
1279
+ if not iterator :
1280
+ raise XKBComposeTableIteratorCreationFailure ()
1281
+ # This allocates a single size_t
1282
+ sequence_length_ptr = ffi .new ("size_t *" )
1283
+ try :
1284
+ while True :
1285
+ entry = local_lib .xkb_compose_table_iterator_next (iterator )
1286
+ if entry == ffi .NULL :
1287
+ return
1288
+ sequence = local_lib .xkb_compose_table_entry_sequence (
1289
+ entry , sequence_length_ptr )
1290
+ keysym = local_lib .xkb_compose_table_entry_keysym (entry )
1291
+ utf8 = local_lib .xkb_compose_table_entry_utf8 (entry )
1292
+ yield ComposeTableEntry (
1293
+ sequence = tuple (sequence [0 :sequence_length_ptr [0 ]]),
1294
+ keysym = keysym ,
1295
+ string = ffi .string (utf8 ).decode ('utf8' ))
1296
+ finally :
1297
+ del sequence_length_ptr
1298
+ local_lib .xkb_compose_table_iterator_free (iterator )
1271
1299
1272
1300
def compose_state_new (self , flags = None ):
1273
1301
pointer = lib .xkb_compose_state_new (self ._table , flags if flags else 0 )
@@ -1277,6 +1305,29 @@ def compose_state_new(self, flags=None):
1277
1305
return ComposeState (self , pointer )
1278
1306
1279
1307
1308
+ @dataclass
1309
+ class ComposeTableEntry :
1310
+ """A Compose table entry
1311
+
1312
+ Enables access to the left-hand keysym sequence, right-hand result
1313
+ keysym and right-hand result string of a compose table entry.
1314
+
1315
+ Do not instantiate this object directly. Instead, obtain a compose
1316
+ table iterator by calling iter() on a ComposeTable; the iterator
1317
+ will yield ComposeTableEntry instances
1318
+ """
1319
+ sequence : Tuple [int ]
1320
+ keysym : int
1321
+ string : str
1322
+
1323
+ # Someone reading the libxkbcommon documentation may expect the
1324
+ # right hand result string to be called "utf8". This is just an
1325
+ # alias.
1326
+ @property
1327
+ def utf8 (self ):
1328
+ return self .string
1329
+
1330
+
1280
1331
class ComposeState :
1281
1332
"""A Compose state object.
1282
1333
@@ -1366,6 +1417,19 @@ def get_utf8(self):
1366
1417
lib .xkb_compose_state_get_utf8 (self ._state , buffer , buffer_size )
1367
1418
return ffi .string (buffer ).decode ("utf8" )
1368
1419
1420
+ # This is an alias for get_utf8() — the name is more logical but
1421
+ # may be unexpected to somebody just reading the libxkbcommon
1422
+ # documentation
1423
+ def get_string (self ):
1424
+ """Get the result string for a compose sequence.
1425
+
1426
+ This function is only useful when the status is
1427
+ ComposeStatus.XKB_COMPOSE_COMPOSED.
1428
+
1429
+ Returns string for composed sequence or empty string if not viable.
1430
+ """
1431
+ return self .get_utf8 ()
1432
+
1369
1433
def get_one_sym (self ):
1370
1434
"""Get the result keysym for a composed sequence.
1371
1435
0 commit comments