2024-02-11 20:46:36 +01:00
|
|
|
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())
|
|
|
|
{
|
2024-02-12 23:36:32 +01:00
|
|
|
case Char:
|
|
|
|
KeyValue res = apply(state, kv.getChar());
|
|
|
|
// Dim characters not part of any sequence instead of removing them.
|
|
|
|
if (res == null)
|
|
|
|
return kv.withFlags(kv.getFlags() | KeyValue.FLAG_SECONDARY);
|
|
|
|
return res;
|
2024-02-11 20:46:36 +01:00
|
|
|
/* These keys must not be removed. */
|
2024-02-12 23:36:32 +01:00
|
|
|
case Event:
|
|
|
|
case Modifier:
|
|
|
|
return kv;
|
2024-02-11 20:46:36 +01:00
|
|
|
/* These keys cannot be part of sequences. */
|
2024-02-12 23:36:32 +01:00
|
|
|
case String:
|
|
|
|
case Keyevent:
|
|
|
|
case Editing:
|
|
|
|
case Placeholder:
|
|
|
|
return kv.withFlags(kv.getFlags() | KeyValue.FLAG_SECONDARY);
|
2024-02-11 20:46:36 +01:00
|
|
|
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;
|
2024-02-12 23:23:38 +01:00
|
|
|
char[] edges = ComposeKeyData.edges;
|
2024-02-11 20:46:36 +01:00
|
|
|
int length = edges[state];
|
|
|
|
int next = Arrays.binarySearch(states, state + 1, state + length, c);
|
|
|
|
if (next < 0)
|
|
|
|
return null;
|
|
|
|
next = edges[next];
|
|
|
|
// 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:
|
2024-02-12 23:23:38 +01:00
|
|
|
- The first cell is the result of the compose sequence if the state is of
|
|
|
|
size 1, unspecified otherwise.
|
2024-02-11 20:46:36 +01:00
|
|
|
- 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. */
|
|
|
|
}
|