| 1 |
/* |
| 2 |
* Copyright © 2013,2021 Ran Benita <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
#include "xkbcommon/xkbcommon-compose.h" |
| 10 |
#include "src/utils.h" |
| 11 |
#include "src/context.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* The compose table data structure is a ternary search tree. |
| 15 |
* |
| 16 |
* Reference: https://www.drdobbs.com/database/ternary-search-trees/184410528 |
| 17 |
* Visualization: https://www.cs.usfca.edu/~galles/visualization/TST.html |
| 18 |
* |
| 19 |
* Short example. Given these sequences: |
| 20 |
* |
| 21 |
* <B> <C> : "first" dead_a |
| 22 |
* <B> <D> <E> : "second" dead_b |
| 23 |
* <A> <F> : "third" dead_c |
| 24 |
* |
| 25 |
* the tree would look like: |
| 26 |
* |
| 27 |
* -------- [<B>]--------- |
| 28 |
* | | # |
| 29 |
* v V |
| 30 |
* -- [<A>] -- [<C>] -------- |
| 31 |
* # | # | | |
| 32 |
* v # -- [<D>] -- |
| 33 |
* -- [<F>] -- # | # |
| 34 |
* # | # v |
| 35 |
* # -- [<E>] -- |
| 36 |
* # | # |
| 37 |
* # |
| 38 |
* |
| 39 |
* where: |
| 40 |
* - [<X>] is a node for a sequence keysym <X>. |
| 41 |
* - right arrows are `hikid` pointers. |
| 42 |
* - left arrows are `lokid` pointers. |
| 43 |
* - down arrows are `eqkid` pointers. |
| 44 |
* - # is a nil pointer. |
| 45 |
* |
| 46 |
* The nodes are all kept in a contiguous array. Pointers are represented |
| 47 |
* as integer offsets into this array. A nil pointer is represented as 0 |
| 48 |
* (which, helpfully, is the offset of an empty dummy node). |
| 49 |
* |
| 50 |
* Nodes without an eqkid are leaf nodes. Since a sequence cannot be a |
| 51 |
* prefix of another, these are exactly the nodes which terminate the |
| 52 |
* sequences (in a bijective manner). |
| 53 |
* |
| 54 |
* A leaf contains the result data of its sequence. The result keysym is |
| 55 |
* contained in the node struct itself; the result UTF-8 string is a byte |
| 56 |
* offset into an array of the form "\0first\0second\0third" (the initial |
| 57 |
* \0 is so offset 0 points to an empty string). |
| 58 |
*/ |
| 59 |
|
| 60 |
/* 7 nodes for every potential Unicode character and then some should be |
| 61 |
* enough for all purposes. */ |
| 62 |
#define MAX_COMPOSE_NODES_LOG2 23 |
| 63 |
#define MAX_COMPOSE_NODES (1u << MAX_COMPOSE_NODES_LOG2) |
| 64 |
|
| 65 |
struct compose_node { |
| 66 |
xkb_keysym_t keysym; |
| 67 |
|
| 68 |
/* Offset into xkb_compose_table::nodes or 0. */ |
| 69 |
uint32_t lokid; |
| 70 |
/* Offset into xkb_compose_table::nodes or 0. */ |
| 71 |
uint32_t hikid; |
| 72 |
|
| 73 |
union { |
| 74 |
struct { |
| 75 |
uint32_t _pad:31; |
| 76 |
bool is_leaf:1; |
| 77 |
}; |
| 78 |
struct { |
| 79 |
uint32_t _pad:31; |
| 80 |
bool is_leaf:1; |
| 81 |
/* Offset into xkb_compose_table::nodes or 0. */ |
| 82 |
uint32_t eqkid; |
| 83 |
} internal; |
| 84 |
struct { |
| 85 |
/* Offset into xkb_compose_table::utf8. */ |
| 86 |
uint32_t utf8:31; |
| 87 |
bool is_leaf:1; |
| 88 |
xkb_keysym_t keysym; |
| 89 |
} leaf; |
| 90 |
}; |
| 91 |
}; |
| 92 |
|
| 93 |
struct xkb_compose_table { |
| 94 |
int refcnt; |
| 95 |
enum xkb_compose_format format; |
| 96 |
enum xkb_compose_compile_flags flags; |
| 97 |
struct xkb_context *ctx; |
| 98 |
|
| 99 |
char *locale; |
| 100 |
|
| 101 |
darray_char utf8; |
| 102 |
darray(struct compose_node) nodes; |
| 103 |
}; |
| 104 |
|
| 105 |
struct xkb_compose_table_entry { |
| 106 |
xkb_keysym_t *sequence; |
| 107 |
size_t sequence_length; |
| 108 |
xkb_keysym_t keysym; |
| 109 |
const char *utf8; |
| 110 |
}; |