-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The COMPOSE_PENDING modifier indicate whether a compose sequence is in progress. The new key of kind Compose_pending sets the current state of the sequence. The compose sequences are compiled into a state machine by a python script into a compact encoding. The state of the pending compose is determined by the index of a state.
- Loading branch information
Showing
9 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import textwrap, sys | ||
|
||
def parse_sequences_file(fname): | ||
with open(fname, "r") as inp: | ||
return [ (s[:-2], s[-2]) for s in inp if len(s) > 1 ] | ||
|
||
# Turn a list of sequences into a trie. | ||
def add_sequences_to_trie(seqs, trie): | ||
for seq, result in seqs: | ||
t_ = trie | ||
i = 0 | ||
while i < len(seq) - 1: | ||
c = seq[i] | ||
if c not in t_: | ||
t_[c] = {} | ||
t_ = t_[c] | ||
i += 1 | ||
c = seq[i] | ||
t_[c] = result | ||
|
||
# Compile the trie into a state machine. | ||
def make_automata(tree_root): | ||
states = [] | ||
def add_tree(t): | ||
# Index and size of the new node | ||
i = len(states) | ||
s = len(t.keys()) | ||
# Add node header | ||
states.append((0, s + 1)) | ||
i += 1 | ||
# Reserve space for the current node in both arrays | ||
for c in range(s): | ||
states.append((None, None)) | ||
# Add nested nodes and fill the current node | ||
for c in sorted(t.keys()): | ||
node_i = len(states) | ||
add_node(t[c]) | ||
states[i] = (c, node_i) | ||
i += 1 | ||
def add_leaf(c): | ||
states.append((c, 1)) | ||
def add_node(n): | ||
if type(n) == str: | ||
add_leaf(n) | ||
else: | ||
add_tree(n) | ||
add_tree(tree_root) | ||
return states | ||
|
||
# Print the state machine compiled by make_automata into java code that can be | ||
# used by [ComposeKeyData.java]. | ||
def gen_java(machine): | ||
def gen_array(array, indent): | ||
return textwrap.fill(", ".join(map(str, array)), subsequent_indent=indent) | ||
print("""package juloo.keyboard2; | ||
/** This file is generated, see [srcs/compose/compile.py]. */ | ||
public final class ComposeKeyData | ||
{ | ||
public static final char[] states = { | ||
%s | ||
}; | ||
public static final short[] edges = { | ||
%s | ||
}; | ||
}""" % ( | ||
gen_array(map(lambda s: repr(s[0]), machine), ' '), | ||
gen_array(map(lambda s: s[1], machine), ' '), | ||
)) | ||
|
||
total_sequences = 0 | ||
trie = {} | ||
for fname in sys.argv[1:]: | ||
sequences = parse_sequences_file(fname) | ||
add_sequences_to_trie(sequences, trie) | ||
total_sequences += len(sequences) | ||
gen_java(make_automata(trie)) | ||
print("Compiled %d sequences" % total_sequences, file=sys.stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
=e€ | ||
`eè | ||
`aà | ||
`uù |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package juloo.keyboard2; | ||
|
||
import java.util.Arrays; | ||
|
||
public final class ComposeKey | ||
{ | ||
/** Apply the pending compose sequence to [kv]. Returns [null] if [kv] is not | ||
part of the pending sequence. */ | ||
public static KeyValue apply(int state, KeyValue kv) | ||
{ | ||
switch (kv.getKind()) | ||
{ | ||
case Char: return apply(state, kv.getChar()); | ||
/* These keys must not be removed. */ | ||
case Event: return kv; | ||
case Modifier: return kv; | ||
/* These keys cannot be part of sequences. */ | ||
case String: return null; | ||
case Keyevent: return null; | ||
case Editing: return null; | ||
case Placeholder: return null; | ||
case Compose_pending: return null; | ||
} | ||
return null; | ||
} | ||
|
||
/** Apply the pending compose sequence to char [c]. */ | ||
static KeyValue apply(int state, char c) | ||
{ | ||
char[] states = ComposeKeyData.states; | ||
short[] edges = ComposeKeyData.edges; | ||
int length = edges[state]; | ||
if (length == 1) | ||
return KeyValue.makeCharKey(states[state]); | ||
int next = Arrays.binarySearch(states, state + 1, length - 1, c); | ||
if (next < 0) | ||
return null; | ||
// The next state is the end of a sequence, show the result. | ||
if (edges[next] == 1) | ||
return KeyValue.makeCharKey(states[next]); | ||
return KeyValue.makeComposePending(String.valueOf(c), next, 0); | ||
} | ||
|
||
/** The [states] array represents the different states and their transition. | ||
A state occupies one or several cells of the array: | ||
- The first cell is the result of the conpose sequence if the state is of | ||
size 1, [0] otherwise. | ||
- The remaining cells are the transitions, sorted alphabetically. | ||
The [edges] array represents the transition state corresponding to each | ||
accepted inputs. | ||
Id [states[i]] is the first cell of a state, [edges[i]] is the number of | ||
cells occupied by the state [i]. | ||
If [states[i]] is a transition, [edges[i]] is the index of the state to | ||
jump into. */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package juloo.keyboard2; | ||
|
||
/** This file is generated, see [srcs/compose/compile.py]. */ | ||
|
||
public final class ComposeKeyData | ||
{ | ||
public static final char[] states = { | ||
0, '=', '`', 0, 'e', '€', 0, 'a', 'e', 'u', 'à', 'è', 'ù' | ||
}; | ||
|
||
public static final short[] edges = { | ||
3, 3, 6, 2, 5, 1, 4, 10, 11, 12, 1, 1, 1 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters