Skip to content

Commit

Permalink
Merge pull request #414 from mlouielu/add-colemak-dh-key-mappings
Browse files Browse the repository at this point in the history
feat: add Colemak-DH key mapping
  • Loading branch information
kanru authored Feb 25, 2024
2 parents 1711d4e + 64ff57c commit acd5bff
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ preference settings.
- HanYu PinYin
- Taiwan Huayu Luomapinyin
- MPS2 Pinyin
- Colemak-DH ANSI
- Colemak-DH Ortholinear


### 3. External and unmerged projects
Expand Down
2 changes: 2 additions & 0 deletions doc/libchewing.texi
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ The string @var{str} might be one of the following layouts:
@item @code{KB_THL_PINYIN}
@item @code{KB_MPS2_PINYIN}
@item @code{KB_CARPALX}
@item @code{KB_COLEMAK_DH_ANSI}
@item @code{KB_COLEMAK_DH_ORTH}
@end itemize

See also the @code{chewing_kbtype_*} enumeration functions.
Expand Down
2 changes: 2 additions & 0 deletions include/chewing_rs.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ typedef enum KB {
KB_THL_PINYIN,
KB_MPS2_PINYIN,
KB_CARPALX,
KB_COLEMAK_DH_ANSI,
KB_COLEMAK_DH_ORTH,
KB_TYPE_NUM,
} KB;

Expand Down
2 changes: 2 additions & 0 deletions include/internal/bopomofo-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ enum {
KB_THL_PINYIN,
KB_MPS2_PINYIN,
KB_CARPALX,
KB_COLEMAK_DH_ANSI,
KB_COLEMAK_DH_ORTH,
KB_TYPE_NUM
};

Expand Down
8 changes: 8 additions & 0 deletions src/capi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ pub extern "C" fn chewing_set_KBType(ctx: *mut ChewingContext, kbtype: c_int) ->
KB::ThlPinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::thl())),
KB::Mps2Pinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::mps2())),
KB::Carpalx => (AnyKeyboardLayout::qwerty(), Box::new(Standard::new())),
KB::ColemakDhAnsi => (
AnyKeyboardLayout::colemak_dh_ansi(),
Box::new(Standard::new()),
),
KB::ColemakDhOrth => (
AnyKeyboardLayout::colemak_dh_orth(),
Box::new(Standard::new()),
),
};
ctx.kb_compat = kb_compat;
ctx.keyboard = keyboard;
Expand Down
4 changes: 3 additions & 1 deletion src/chewingio.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ const char *const kb_type_str[] = {
"KB_HANYU_PINYIN",
"KB_THL_PINYIN",
"KB_MPS2_PINYIN",
"KB_CARPALX"
"KB_CARPALX",
"KB_COLEMAK_DH_ANSI",
"KB_COLEMAK_DH_ORTH",
};

#ifdef WITH_RUST
Expand Down
2 changes: 2 additions & 0 deletions src/common/key2pho.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ static const char *const key_str[KB_TYPE_NUM] = {
"1qaz2wsxedcrfv5tgbyhnujm8ik,9ol.0p;/-7634", /* Luoma Pinyin */
"1qaz2wsxedcrfv5tgbyhnujm8ik,9ol.0p;/-7634", /* secondary Bopomofo Pinyin */
"1qdz2gsxmtclnv5wrjyikfap8ue,9bo.0;h/-7634", /* Carpalx */
"1qax2wrcfsdptv5bgzjmklnh8ue,9yi.0;o/-7634", /* Colemak-DH ANSI */
"1qaz2wrxfscptd5bgvjmklnh8ue,9yi.0;o/-7634", /* Colemak-DH Orth */
};

/*
Expand Down
48 changes: 48 additions & 0 deletions src/editor/keyboard/colemak_dh_ansi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::{
generic_map_keycode,
KeyCode::{self, *},
KeyEvent, KeyboardLayout, Modifiers, MATRIX_SIZE,
};

/// A Colemak-DH Ansiolinear keyboard.
#[derive(Debug)]
pub struct ColemakDhAnsi;

#[rustfmt::skip]
static KEYCODE_INDEX: [KeyCode; MATRIX_SIZE] = [
Unknown,
N1, N2, N3, N4, N5, N6, N7, N8, N9, N0, Minus, Equal, BSlash, Grave,
Q, W, F, P, B, J, L, U, Y, SColon, LBracket, RBracket,
A, R, S, T, G, M, N, E, I, O, Quote,
X, C, D, V, Z, K, H, Comma, Dot, Slash, Space,
Esc, Enter, Del, Backspace, Tab, Left, Right, Up, Down, Home, End,
PageUp, PageDown, NumLock,
];

#[rustfmt::skip]
static UNICODE_MAP: [char; MATRIX_SIZE] = [
'�',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', '`',
'q', 'w', 'f', 'p', 'b', 'j', 'l', 'u', 'y', ';', '[', ']',
'a', 'r', 's', 't', 'g', 'm', 'n', 'e', 'i', 'o', '\'',
'x', 'c', 'd', 'v', 'z', 'k', 'h', ',', '.', '/', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

#[rustfmt::skip]
static SHIFT_MAP: [char; MATRIX_SIZE] = [
'�',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', '~',
'Q', 'W', 'F', 'P', 'B', 'J', 'L', 'U', 'Y', ':', '{', '}',
'A', 'R', 'S', 'T', 'G', 'M', 'N', 'E', 'I', 'O', '"',
'X', 'C', 'D', 'V', 'Z', 'K', 'H', '<', '>', '?', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

impl KeyboardLayout for ColemakDhAnsi {
fn map_with_mod(&self, keycode: KeyCode, modifiers: Modifiers) -> KeyEvent {
generic_map_keycode(&KEYCODE_INDEX, &UNICODE_MAP, &SHIFT_MAP, keycode, modifiers)
}
}
48 changes: 48 additions & 0 deletions src/editor/keyboard/colemak_dh_orth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::{
generic_map_keycode,
KeyCode::{self, *},
KeyEvent, KeyboardLayout, Modifiers, MATRIX_SIZE,
};

/// A Colemak-DH Ortholinear keyboard.
#[derive(Debug)]
pub struct ColemakDhOrth;

#[rustfmt::skip]
static KEYCODE_INDEX: [KeyCode; MATRIX_SIZE] = [
Unknown,
N1, N2, N3, N4, N5, N6, N7, N8, N9, N0, Minus, Equal, BSlash, Grave,
Q, W, F, P, B, J, L, U, Y, SColon, LBracket, RBracket,
A, R, S, T, G, M, N, E, I, O, Quote,
Z, X, C, D, V, K, H, Comma, Dot, Slash, Space,
Esc, Enter, Del, Backspace, Tab, Left, Right, Up, Down, Home, End,
PageUp, PageDown, NumLock,
];

#[rustfmt::skip]
static UNICODE_MAP: [char; MATRIX_SIZE] = [
'�',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', '`',
'q', 'w', 'f', 'p', 'b', 'j', 'l', 'u', 'y', ';', '[', ']',
'a', 'r', 's', 't', 'g', 'm', 'n', 'e', 'i', 'o', '\'',
'z', 'x', 'c', 'd', 'v', 'k', 'h', ',', '.', '/', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

#[rustfmt::skip]
static SHIFT_MAP: [char; MATRIX_SIZE] = [
'�',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', '~',
'Q', 'W', 'F', 'P', 'B', 'J', 'L', 'U', 'Y', ':', '{', '}',
'A', 'R', 'S', 'T', 'G', 'M', 'N', 'E', 'I', 'O', '"',
'Z', 'X', 'C', 'D', 'V', 'K', 'H', '<', '>', '?', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

impl KeyboardLayout for ColemakDhOrth {
fn map_with_mod(&self, keycode: KeyCode, modifiers: Modifiers) -> KeyEvent {
generic_map_keycode(&KEYCODE_INDEX, &UNICODE_MAP, &SHIFT_MAP, keycode, modifiers)
}
}
14 changes: 14 additions & 0 deletions src/editor/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
//! to map different English layouts to layout independent key indexes that can be
//! used to drive the phonetic conversion engines.
mod colemak_dh_ansi;
mod colemak_dh_orth;
mod dvorak;
mod qgmlwy;
mod qwerty;

use core::fmt;

pub use colemak_dh_ansi::ColemakDhAnsi;
pub use colemak_dh_orth::ColemakDhOrth;
pub use dvorak::Dvorak;
pub use qgmlwy::Qgmlwy;
pub use qwerty::Qwerty;
Expand Down Expand Up @@ -129,6 +133,8 @@ pub enum AnyKeyboardLayout {
Qwerty(Qwerty),
Dvorak(Dvorak),
Qgmlwy(Qgmlwy),
ColemakDhAnsi(ColemakDhAnsi),
ColemakDhOrth(ColemakDhOrth),
}

impl AnyKeyboardLayout {
Expand All @@ -141,6 +147,12 @@ impl AnyKeyboardLayout {
pub fn qgmlwy() -> AnyKeyboardLayout {
AnyKeyboardLayout::Qgmlwy(Qgmlwy)
}
pub fn colemak_dh_ansi() -> AnyKeyboardLayout {
AnyKeyboardLayout::ColemakDhAnsi(ColemakDhAnsi)
}
pub fn colemak_dh_orth() -> AnyKeyboardLayout {
AnyKeyboardLayout::ColemakDhOrth(ColemakDhOrth)
}
}

impl KeyboardLayout for AnyKeyboardLayout {
Expand All @@ -149,6 +161,8 @@ impl KeyboardLayout for AnyKeyboardLayout {
AnyKeyboardLayout::Qwerty(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Dvorak(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Qgmlwy(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhAnsi(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhOrth(kb) => kb.map_with_mod(keycode, modifiers),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/editor/syllable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub enum KeyboardLayoutCompat {
Mps2Pinyin,
/// TODO: docs
Carpalx,
/// TODO: docs
ColemakDhAnsi,
/// TODO: docs
ColemakDhOrth,
}

impl FromStr for KeyboardLayoutCompat {
Expand All @@ -94,6 +98,8 @@ impl FromStr for KeyboardLayoutCompat {
"KB_THL_PINYIN" => Self::ThlPinyin,
"KB_MPS2_PINYIN" => Self::Mps2Pinyin,
"KB_CARPALX" => Self::Carpalx,
"KB_COLEMAK_DH_ANSI" => Self::ColemakDhAnsi,
"KB_COLEMAK_DH_ORTH" => Self::ColemakDhOrth,
_ => todo!("handle error"),
};
Ok(layout)
Expand All @@ -116,6 +122,8 @@ impl Display for KeyboardLayoutCompat {
KeyboardLayoutCompat::ThlPinyin => f.write_str("KB_THL_PINYIN"),
KeyboardLayoutCompat::Mps2Pinyin => f.write_str("KB_MPS2_PINYIN"),
KeyboardLayoutCompat::Carpalx => f.write_str("KB_CARPALX"),
KeyboardLayoutCompat::ColemakDhAnsi => f.write_str("KB_COLEMAK_DH_ANSI"),
KeyboardLayoutCompat::ColemakDhOrth => f.write_str("KB_COLEMAK_DH_ORTH"),
}
}
}
Expand All @@ -138,6 +146,8 @@ impl TryFrom<u8> for KeyboardLayoutCompat {
10 => Self::ThlPinyin,
11 => Self::Mps2Pinyin,
12 => Self::Carpalx,
13 => Self::ColemakDhAnsi,
14 => Self::ColemakDhOrth,
_ => return Err(()),
})
}
Expand Down
35 changes: 35 additions & 0 deletions test/test-bopomofo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,39 @@ void test_KB_MPS2()
chewing_delete(ctx);
}

void test_KB_COLEMAK_DH_ANSI()
{
ChewingContext *ctx;

ctx = chewing_new();
start_testcase(ctx, fd);

chewing_set_KBType(ctx, KB_COLEMAK_DH_ANSI);
type_keystroke_by_string(ctx, "vl; sn4l; 5; 2e7d;31o4");
ok_preedit_buffer(ctx, "\xE6\x96\xB0\xE9\x85\xB7\xE9\x9F\xB3\xE7\x9C\x9F\xE7\x9A\x84\xE5\xBE\x88\xE6\xA3\x92"
/* 新酷音真的很棒 */ );
chewing_clean_preedit_buf(ctx);

chewing_delete(ctx);
}


void test_KB_COLEMAK_DH_ORTH()
{
ChewingContext *ctx;

ctx = chewing_new();
start_testcase(ctx, fd);

chewing_set_KBType(ctx, KB_COLEMAK_DH_ORTH);
type_keystroke_by_string(ctx, "dl; sn4l; 5; 2e7c;31o4");
ok_preedit_buffer(ctx, "\xE6\x96\xB0\xE9\x85\xB7\xE9\x9F\xB3\xE7\x9C\x9F\xE7\x9A\x84\xE5\xBE\x88\xE6\xA3\x92"
/* 新酷音真的很棒 */ );
chewing_clean_preedit_buf(ctx);

chewing_delete(ctx);
}


void test_KB()
{
Expand All @@ -1843,6 +1876,8 @@ void test_KB()
test_KB_ET26();
test_KB_ET26_choice_append();
test_KB_DACHEN_CP26();
test_KB_COLEMAK_DH_ANSI();
test_KB_COLEMAK_DH_ORTH();

test_KB_HANYU();
test_KB_THL();
Expand Down
2 changes: 2 additions & 0 deletions test/test-keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ static const char *const KEYBOARD_STRING[] = {
"KB_THL_PINYIN",
"KB_MPS2_PINYIN",
"KB_CARPALX",
"KB_COLEMAK_DH_ANSI",
"KB_COLEMAK_DH_ORTH",
};

static const int KEYBOARD_DEFAULT_TYPE = 0;
Expand Down

0 comments on commit acd5bff

Please sign in to comment.