Per-script numpad

The numeric layout and the optional right hand side numpad are modified
to show the digits belonging to the script used in the current layout.

The numpads are still defined as it was before. The digits are changed
in `modify_numpad` if needed.
This commit is contained in:
Jules Aguillon 2023-09-03 23:38:55 +02:00
parent b079e5cf43
commit 687d88f4f7
5 changed files with 129 additions and 13 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard name="বাংলা (জাতীয়)">
<keyboard name="বাংলা (জাতীয়)" script="bengali">
<row>
<key key0="ঙ" key2="ং" key3="esc" key4="১"/>
<key key0="য" key2="য়" key3="¶" key4="২"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<keyboard name="বাংলা (প্রভাত)">
<keyboard name="বাংলা (প্রভাত)" script="bengali">
<row>
<key key0="দ" key1="ধ" key2="১" key3="!" key5="esc" />
<key key0="ূ" key1="ঊ" key2="২" key3="\@" />

View File

@ -242,7 +242,7 @@ final class Config
}
});
if (show_numpad)
kw = kw.addNumPad(modify_numpad(KeyboardData.num_pad));
kw = kw.addNumPad(modify_numpad(KeyboardData.num_pad, kw.script));
if (number_row)
kw = kw.addNumberRow();
if (extra_keys.size() > 0)
@ -253,9 +253,10 @@ final class Config
/**
* Handle the numpad layout.
*/
public KeyboardData modify_numpad(KeyboardData kw)
public KeyboardData modify_numpad(KeyboardData kw, String script)
{
final KeyValue action_key = action_key();
final KeyModifier.Map_char map_digit = KeyModifier.modify_numpad_script(script);
return kw.mapKeys(new KeyboardData.MapKeyValues() {
public KeyValue apply(KeyValue key, boolean localized)
{
@ -277,11 +278,15 @@ final class Config
}
break;
case Char:
char a = key.getChar(), b = a;
char prev_c = key.getChar();
char c = prev_c;
if (inverse_numpad)
b = inverse_numpad_char(a);
if (a != b)
return key.withChar(b);
c = inverse_numpad_char(c);
String modified = map_digit.apply(c);
if (modified != null) // Was modified by script
return key.withSymbol(modified);
if (prev_c != c) // Was inverted
return key.withChar(c);
break;
}
return key;

View File

@ -83,6 +83,20 @@ class KeyModifier
return k;
}
public static Map_char modify_numpad_script(String script)
{
if (script == null)
return map_char_none;
switch (script)
{
case "arabic": return map_char_numpad_arabic;
case "bengali": return map_char_numpad_bengali;
case "devanagari": return map_char_numpad_devanagari;
case "persian": return map_char_numpad_persian;
default: return map_char_none;
}
}
private static KeyValue apply_map_char(KeyValue k, Map_char map)
{
switch (k.getKind())
@ -427,13 +441,18 @@ class KeyModifier
return ks;
}
private static abstract class Map_char
public static abstract class Map_char
{
/** Modify a char or return [null] if the modifier do not apply. Return a
[String] that can contains combining diacritics. */
public abstract String apply(char c);
}
private static final Map_char map_char_none =
new Map_char() {
public String apply(char _c) { return null; }
};
private static char map_char_shift(char c)
{
switch (c)
@ -971,4 +990,88 @@ class KeyModifier
}
}
};
private static final Map_char map_char_numpad_arabic =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "٠";
case '1': return "١";
case '2': return "٢";
case '3': return "٣";
case '4': return "٤";
case '5': return "٥";
case '6': return "٦";
case '7': return "٧";
case '8': return "٨";
case '9': return "٩";
default: return null;
}
}
};
private static final Map_char map_char_numpad_bengali =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "";
case '1': return "";
case '2': return "";
case '3': return "";
case '4': return "";
case '5': return "";
case '6': return "";
case '7': return "";
case '8': return "";
case '9': return "";
default: return null;
}
}
};
private static final Map_char map_char_numpad_devanagari =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "";
case '1': return "";
case '2': return "";
case '3': return "";
case '4': return "";
case '5': return "";
case '6': return "";
case '7': return "";
case '8': return "";
case '9': return "";
default: return null;
}
}
};
private static final Map_char map_char_numpad_persian =
new Map_char() {
public String apply(char c)
{
switch (c)
{
case '0': return "۰";
case '1': return "۱";
case '2': return "۲";
case '3': return "۳";
case '4': return "۴";
case '5': return "۵";
case '6': return "۶";
case '7': return "۷";
case '8': return "۸";
case '9': return "۹";
default: return null;
}
}
};
}

View File

@ -40,8 +40,8 @@ public class Keyboard2 extends InputMethodService
private Config _config;
/** Layout currently visible. */
KeyboardData current_layout()
/** Layout currently visible before it has been modified. */
KeyboardData current_layout_unmodified()
{
if (_currentSpecialLayout != null)
return _currentSpecialLayout;
@ -52,7 +52,13 @@ public class Keyboard2 extends InputMethodService
layout = _config.layouts.get(_currentTextLayout);
if (layout == null)
layout = _localeTextLayout;
return _config.modify_layout(layout);
return layout;
}
/** Layout currently visible. */
KeyboardData current_layout()
{
return _config.modify_layout(current_layout_unmodified());
}
void setTextLayout(int l)
@ -84,7 +90,9 @@ public class Keyboard2 extends InputMethodService
/** Load a layout that contains a numpad (eg. the pin entry). */
KeyboardData loadNumpad(int layout_id)
{
return _config.modify_numpad(KeyboardData.load(getResources(), layout_id));
String current_script = current_layout_unmodified().script;
return _config.modify_numpad(KeyboardData.load(getResources(), layout_id),
current_script);
}
@Override