Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding Colemak #674

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ preference settings.
- HanYu PinYin
- Taiwan Huayu Luomapinyin
- MPS2 Pinyin
- Colemak
- Colemak-DH ANSI
- Colemak-DH Ortholinear

Expand Down
2 changes: 2 additions & 0 deletions capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ pub unsafe extern "C" fn chewing_config_set_str(
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::Colemak => (AnyKeyboardLayout::colemak(), Box::new(Standard::new())),
KB::ColemakDhAnsi => (
AnyKeyboardLayout::colemak_dh_ansi(),
Box::new(Standard::new()),
Expand Down Expand Up @@ -611,6 +612,7 @@ pub unsafe extern "C" fn chewing_set_KBType(ctx: *mut ChewingContext, kbtype: c_
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::Colemak => (AnyKeyboardLayout::colemak(), Box::new(Standard::new())),
KB::ColemakDhAnsi => (
AnyKeyboardLayout::colemak_dh_ansi(),
Box::new(Standard::new()),
Expand Down
1 change: 1 addition & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub mod layout {
/// * KB_THL_PINYIN
/// * KB_MPS2_PINYIN
/// * KB_CARPALX
/// * KB_COLEMAK
/// * KB_COLEMAK_DH_ANSI
/// * KB_COLEMAK_DH_ORTH
/// * KB_WORKMAN
Expand Down
1 change: 1 addition & 0 deletions capi/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum KB {
ThlPinyin,
Mps2Pinyin,
Carpalx,
Colemak,
ColemakDhAnsi,
ColemakDhOrth,
Workman,
Expand Down
1 change: 1 addition & 0 deletions doc/libchewing.texi
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ 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}
@item @code{KB_COLEMAK_DH_ANSI}
@item @code{KB_COLEMAK_DH_ORTH}
@end itemize
Expand Down
1 change: 1 addition & 0 deletions include/chewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ typedef enum KB {
KB_THL_PINYIN,
KB_MPS2_PINYIN,
KB_CARPALX,
KB_COLEMAK,
KB_COLEMAK_DH_ANSI,
KB_COLEMAK_DH_ORTH,
KB_WORKMAN,
Expand Down
48 changes: 48 additions & 0 deletions src/editor/keyboard/colemak.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 keyboard.
#[derive(Debug)]
pub struct Colemak;

#[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, G, J, L, U, Y, SColon, LBracket, RBracket,
A, R, S, T, D, H, N, E, I, O, Quote,
Z, X, C, V, B, K, M, 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', 'g', 'j', 'l', 'u', 'y', ';', '[', ']',
'a', 'r', 's', 't', 'd', 'h', 'n', 'e', 'i', 'o', '\'',
'z', 'x', 'c', 'v', 'b', 'k', 'm', ',', '.', '/', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

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

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

mod colemak;
mod colemak_dh_ansi;
mod colemak_dh_orth;
mod dvorak;
Expand All @@ -15,6 +16,7 @@ mod workman;

use core::fmt;

pub use colemak::Colemak;
pub use colemak_dh_ansi::ColemakDhAnsi;
pub use colemak_dh_orth::ColemakDhOrth;
pub use dvorak::Dvorak;
Expand Down Expand Up @@ -139,6 +141,7 @@ pub enum AnyKeyboardLayout {
Dvorak(Dvorak),
DvorakOnQwerty(DvorakOnQwerty),
Qgmlwy(Qgmlwy),
Colemak(Colemak),
ColemakDhAnsi(ColemakDhAnsi),
ColemakDhOrth(ColemakDhOrth),
Workman(Workman),
Expand All @@ -157,6 +160,9 @@ impl AnyKeyboardLayout {
pub fn qgmlwy() -> AnyKeyboardLayout {
AnyKeyboardLayout::Qgmlwy(Qgmlwy)
}
pub fn colemak() -> AnyKeyboardLayout {
AnyKeyboardLayout::Colemak(Colemak)
}
pub fn colemak_dh_ansi() -> AnyKeyboardLayout {
AnyKeyboardLayout::ColemakDhAnsi(ColemakDhAnsi)
}
Expand All @@ -175,6 +181,7 @@ impl KeyboardLayout for AnyKeyboardLayout {
AnyKeyboardLayout::Dvorak(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::DvorakOnQwerty(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Qgmlwy(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Colemak(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),
AnyKeyboardLayout::Workman(kb) => kb.map_with_mod(keycode, modifiers),
Expand Down
5 changes: 5 additions & 0 deletions src/editor/zhuyin_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub enum KeyboardLayoutCompat {
/// TODO: docs
Carpalx,
/// TODO: docs
Colemak,
/// TODO: docs
ColemakDhAnsi,
/// TODO: docs
ColemakDhOrth,
Expand Down Expand Up @@ -109,6 +111,7 @@ impl FromStr for KeyboardLayoutCompat {
"KB_THL_PINYIN" => Self::ThlPinyin,
"KB_MPS2_PINYIN" => Self::Mps2Pinyin,
"KB_CARPALX" => Self::Carpalx,
"KB_COLEMAK" => Self::Colemak,
"KB_COLEMAK_DH_ANSI" => Self::ColemakDhAnsi,
"KB_COLEMAK_DH_ORTH" => Self::ColemakDhOrth,
"KB_WORKMAN" => Self::Workman,
Expand All @@ -134,6 +137,7 @@ 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::Colemak => f.write_str("KB_COLEMAK"),
KeyboardLayoutCompat::ColemakDhAnsi => f.write_str("KB_COLEMAK_DH_ANSI"),
KeyboardLayoutCompat::ColemakDhOrth => f.write_str("KB_COLEMAK_DH_ORTH"),
KeyboardLayoutCompat::Workman => f.write_str("KB_WORKMAN"),
Expand Down Expand Up @@ -162,6 +166,7 @@ impl TryFrom<u8> for KeyboardLayoutCompat {
13 => Self::ColemakDhAnsi,
14 => Self::ColemakDhOrth,
15 => Self::Workman,
16 => Self::Colemak,
_ => return Err(()),
})
}
Expand Down
17 changes: 17 additions & 0 deletions tests/test-bopomofo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,22 @@ void test_KB_DVORAK_HSU()
chewing_delete(ctx);
}

void test_KB_COLEMAK()
{
ChewingContext *ctx;

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

chewing_set_KBType(ctx, KB_COLEMAK);
type_keystroke_by_string(ctx, "vl; 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_COLEMAK_DH_ANSI()
{
ChewingContext *ctx;
Expand Down Expand Up @@ -2307,6 +2323,7 @@ void test_KB()
test_KB_DACHEN_CP26();
test_KB_DVORAK();
test_KB_DVORAK_HSU();
test_KB_COLEMAK();
test_KB_COLEMAK_DH_ANSI();
test_KB_COLEMAK_DH_ORTH();

Expand Down
3 changes: 2 additions & 1 deletion tests/test-keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ static const char *const KEYBOARD_STRING[] = {
"KB_CARPALX",
"KB_COLEMAK_DH_ANSI",
"KB_COLEMAK_DH_ORTH",
"KB_WORKMAN"
"KB_WORKMAN",
"KB_COLEMAK"
};

static const int KEYBOARD_DEFAULT_TYPE = 0;
Expand Down
Loading