diff --git a/CODE_CONVENTION.md b/CODE_CONVENTION.md new file mode 100644 index 0000000..b383c73 --- /dev/null +++ b/CODE_CONVENTION.md @@ -0,0 +1,60 @@ +# Coding Conventions + +For better readability of codes, there are coding conventions to follow. +Other dependent codes (library such as libhangul) are not applied by this convention. + +## Indent + +* Use 4 spaces for indentation. +* Tabs aren't allowed whether it generates compile errors. + +## Identifiers + +* Use `snake_case` for functions, variables names. +* Use `CONSTANT_CASE` for constant variables, constant macros, enums. +* Use `PascalCase` for defining struct(class) type names. +* Other casing rules can be used in their circumstances, but prefer rules above. + * GObject, GLib may use PascalCase for struct(class) type names. + * GoogleTest prefers not to use underscore(`_`) in function/test namings. + +## Spacing + +### Horizontal spacing + +* Split control-flow statements, conditions, bracket with spaces. +* Do not split function name and its parameter definition with spaces. + +### Vertical spacing (line breaks) + +* Use single line break to split paragraph in function code blocks. +* Use double line break between function definitions for better distinguish. +* Declaring multiple functions and variables can skip line break by its context. +* Try to write all keywords on defining/declaring function and its signature. + * Try to write `extern`/`static`, `inline`, return type and function name in same line. + * When the signature is too long to represent in single line, try to break in parameters. +* For unary operators such as `&`, `++`, `*`(pointer dereference), do not separate. +* For binary operators such as `+`, `-`, `==`, separate with spaces. + +### Bracket `{`, `}` + +* For function, structure, union, class and enums: + opening and closing brackets must be placed on separate line. +* For initializing struct, array: + opening and closing brackets must be placed on separate line. + When the initialization is short enough to place in single line, it's allowed. +* For control-flow statements (ex. `if`, `for`, `while`...): + opening bracket must be placed on same line. + on following `else`, `else if` statements, closing bracket must be placed on same line. + (ex. `} else {`) +* When the control-flow handles single line expression, bracket can be skipped. + But, one of `if`, `else if` and `else` has multiple line expression, + use bracket even the others handle single line expression. + +## Comments + +* Both C style (`/* ... */`) and C++ style (`// ...`) comments are allowed. +* For consistency, try to use each styles for: + * Use C style for multiple line comments. Such as documentation, long descriptions. + * Use C++ style for single line comments. Such as supplimentary, short descriptions. +* Use space after/before comment specifiers. + diff --git a/hanjpautomata.c b/hanjpautomata.c index aa7c453..53c6986 100644 --- a/hanjpautomata.c +++ b/hanjpautomata.c @@ -3,21 +3,25 @@ #include #include -/* Extern function signature which isn't exported in libhangul header */ +// Extern function signature which aren't exported in libhangul header extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); -/* Kana Table index enums */ -enum { - KANA_VOWEL_A, + +/* + * Kana Table + */ +// table indexing enums +enum +{ + // Vowel indices + KANA_VOWEL_A = 0, KANA_VOWEL_I, KANA_VOWEL_U, KANA_VOWEL_E, - KANA_VOWEL_O -}; - -enum { - KANA_CONSONANT__, + KANA_VOWEL_O, + // Consonant indices + KANA_CONSONANT__ = 0, KANA_CONSONANT_K, KANA_CONSONANT_S, KANA_CONSONANT_T, @@ -29,27 +33,40 @@ enum { KANA_CONSONANT_W }; -// Fifty notes -// For example か(ka) = KANA_TABLE[KANA_CONSONANT_K][KANA_VOWEL_A] +// Fifty notes, ex. か(ka) = KANA_TABLE[KANA_CONSONANT_K][KANA_VOWEL_A] static const gunichar KANA_TABLE[][5] = { - // A, I, U, E, O - {0x3042, 0x3044, 0x3046, 0x3048, 0x304A}, // A - {0x304B, 0x304D, 0x304F, 0x3051, 0x3053}, // KA - {0x3055, 0x3057, 0x3059, 0x305B, 0x305D}, // SA - {0x305F, 0x3061, 0x3064, 0x3066, 0x3068}, // TA - {0x306A, 0x306B, 0x306C, 0x306D, 0x306E}, // NA - {0x306F, 0x3072, 0x3075, 0x3078, 0x307B}, // HA - {0x307E, 0x307F, 0x3080, 0x3081, 0x3082}, // MO - {0x3084, 0x0000, 0x3086, 0x0000, 0x3088}, // YA - {0x3089, 0x308A, 0x308B, 0x308C, 0x308D}, // RA - {0x308F, 0x3090, 0x0000, 0x3091, 0x3092} // WA + // _A, _I, _U, _E, _O + { 0x3042, 0x3044, 0x3046, 0x3048, 0x304A }, // _ + { 0x304B, 0x304D, 0x304F, 0x3051, 0x3053 }, // K_ + { 0x3055, 0x3057, 0x3059, 0x305B, 0x305D }, // S_ + { 0x305F, 0x3061, 0x3064, 0x3066, 0x3068 }, // T_ + { 0x306A, 0x306B, 0x306C, 0x306D, 0x306E }, // N_ + { 0x306F, 0x3072, 0x3075, 0x3078, 0x307B }, // H_ + { 0x307E, 0x307F, 0x3080, 0x3081, 0x3082 }, // M_ + { 0x3084, 0x0000, 0x3086, 0x0000, 0x3088 }, // Y_ + { 0x3089, 0x308A, 0x308B, 0x308C, 0x308D }, // R_ + { 0x308F, 0x3090, 0x0000, 0x3091, 0x3092 } // W_ }; + +// Other Kana characters (which cannot be indexed through KANA_TABLE static const gunichar KANA_SMALL_TU = 0x3063; static const gunichar KANA_NN = 0x3093; +// Jamo to Kana conversion results +#define KANA_CONV_SUCCESS (0) +#define KANA_CONV_FAIL (-1) + +// Jamo to Kana conversion functions +static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit); +static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx); +static gint jongseong_to_kana(gunichar jong, gunichar *kana); + + // Combine multiple JungSeong into single int code -typedef union { - struct { +typedef union +{ + struct + { gunichar jung; gunichar jung2; }; @@ -59,15 +76,20 @@ typedef union { #define N_COMBINE_TABLE_ELEMENTS 30 -/* Automata Interface Definition */ +/* + * Automata Interface Definition + */ G_DEFINE_INTERFACE(HanjpAutomata, hanjp_am, G_TYPE_OBJECT) -static void -hanjp_am_default_init(HanjpAutomataInterface *iface) { - /* add properties and signals to the interface here */ + +static void hanjp_am_default_init(HanjpAutomataInterface *iface) +{ + // Nothing to do } -gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { + +gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -77,7 +99,9 @@ gint hanjp_am_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { return iface->to_kana(am, dest, buffer); } -gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar ch) { + +gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar ch) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -87,7 +111,9 @@ gint hanjp_am_push(HanjpAutomata *am, GArray *result, GArray *hangul, gunichar c return iface->push(am, result, hangul, ch); } -gboolean hanjp_am_backspace(HanjpAutomata *am) { + +gboolean hanjp_am_backspace(HanjpAutomata *am) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -97,7 +123,9 @@ gboolean hanjp_am_backspace(HanjpAutomata *am) { return iface->backspace(am); } -void hanjp_am_flush(HanjpAutomata *am) { + +void hanjp_am_flush(HanjpAutomata *am) +{ HanjpAutomataInterface *iface; g_return_if_fail(HANJP_IS_AM(am)); @@ -107,177 +135,176 @@ void hanjp_am_flush(HanjpAutomata *am) { iface->flush(am); } -/**/ -/* AutomataBase Implementation */ -typedef struct { +/* + * AutomataBase Implementation + */ +typedef struct +{ HanjpBuffer buffer; GHashTable *combine_table; JungBox combine_table_keys[N_COMBINE_TABLE_ELEMENTS]; guint32 combine_table_vals[N_COMBINE_TABLE_ELEMENTS]; } HanjpAutomataBasePrivate; + G_DEFINE_TYPE_WITH_PRIVATE(HanjpAutomataBase, hanjp_am_base, G_TYPE_OBJECT) -static gint -choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) { - gint ret = 0; // In normal indexing, it returns 0 - - *diacrit = 0; - // Select row index and set adjuster - switch (cho) { - case 0: // VOID - *diacrit = -1; - case HANGUL_CHOSEONG_IEUNG: // ㅇ - *conso_idx = KANA_CONSONANT__; - break; - case HANGUL_CHOSEONG_KIYEOK: // ㄱ - *diacrit = 1; - case HANGUL_CHOSEONG_KHIEUKH: // ㅋ - case HANGUL_CHOSEONG_SSANGKIYEOK: // ㄲ - *conso_idx = KANA_CONSONANT_K; - break; - case HANGUL_CHOSEONG_CIEUC: // ㅈ - *diacrit = 1; - case HANGUL_CHOSEONG_SIOS: // ㅅ - case HANGUL_CHOSEONG_SSANGSIOS: // ㅆ - *conso_idx = KANA_CONSONANT_S; - break; - case HANGUL_CHOSEONG_TIKEUT: // ㄷ - *diacrit = 1; - case HANGUL_CHOSEONG_SSANGTIKEUT: // ㄸ - case HANGUL_CHOSEONG_CHIEUCH: // ㅊ - case HANGUL_CHOSEONG_THIEUTH: // ㅌ - *conso_idx = KANA_CONSONANT_T; - break; - case HANGUL_CHOSEONG_NIEUN: // ㄴ - *conso_idx = KANA_CONSONANT_N; - break; - case HANGUL_CHOSEONG_PHIEUPH: // ㅍ - case HANGUL_CHOSEONG_SSANGPIEUP: // ㅃ - *diacrit = 1; - case HANGUL_CHOSEONG_PIEUP: // ㅂ - *diacrit += 1; - case HANGUL_CHOSEONG_HIEUH: // ㅎ - *conso_idx = KANA_CONSONANT_H; - break; - case HANGUL_CHOSEONG_MIEUM: // ㅁ - *conso_idx = KANA_CONSONANT_M; - break; - case HANGUL_CHOSEONG_RIEUL: // ㄹ - *conso_idx = KANA_CONSONANT_R; - break; - case HANGUL_CHOSEONG_SSANGNIEUN: - default: - ret = -1; - } - return ret; + +static gint choseong_to_kana_index(gunichar cho, gint *conso_idx, gint *diacrit) +{ + *diacrit = 0; + switch (cho) { + case 0: // VOID + *diacrit = -1; + case HANGUL_CHOSEONG_IEUNG: // ㅇ + *conso_idx = KANA_CONSONANT__; + break; + case HANGUL_CHOSEONG_KIYEOK: // ㄱ + *diacrit = 1; + case HANGUL_CHOSEONG_KHIEUKH: // ㅋ + case HANGUL_CHOSEONG_SSANGKIYEOK: // ㄲ + *conso_idx = KANA_CONSONANT_K; + break; + case HANGUL_CHOSEONG_CIEUC: // ㅈ + *diacrit = 1; + case HANGUL_CHOSEONG_SIOS: // ㅅ + case HANGUL_CHOSEONG_SSANGSIOS: // ㅆ + *conso_idx = KANA_CONSONANT_S; + break; + case HANGUL_CHOSEONG_TIKEUT: // ㄷ + *diacrit = 1; + case HANGUL_CHOSEONG_SSANGTIKEUT: // ㄸ + case HANGUL_CHOSEONG_CHIEUCH: // ㅊ + case HANGUL_CHOSEONG_THIEUTH: // ㅌ + *conso_idx = KANA_CONSONANT_T; + break; + case HANGUL_CHOSEONG_NIEUN: // ㄴ + *conso_idx = KANA_CONSONANT_N; + break; + case HANGUL_CHOSEONG_PHIEUPH: // ㅍ + case HANGUL_CHOSEONG_SSANGPIEUP: // ㅃ + *diacrit = 1; + case HANGUL_CHOSEONG_PIEUP: // ㅂ + *diacrit += 1; + case HANGUL_CHOSEONG_HIEUH: // ㅎ + *conso_idx = KANA_CONSONANT_H; + break; + case HANGUL_CHOSEONG_MIEUM: // ㅁ + *conso_idx = KANA_CONSONANT_M; + break; + case HANGUL_CHOSEONG_RIEUL: // ㄹ + *conso_idx = KANA_CONSONANT_R; + break; + case HANGUL_CHOSEONG_SSANGNIEUN: + default: + return KANA_CONV_FAIL; + } + + return KANA_CONV_SUCCESS; } -static void -divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) + +static void divide_jungseong(HanjpBuffer *buffer, gint *conso_idx) { - switch (buffer->jung) { - case HANGUL_JUNGSEONG_WA: - if(*conso_idx == KANA_CONSONANT__) { - *conso_idx = KANA_CONSONANT_W; - } - else { - buffer->jung = HANGUL_JUNGSEONG_O; - buffer->jung2 = HANGUL_JUNGSEONG_A; - } - break; - case HANGUL_JUNGSEONG_YA: - case HANGUL_JUNGSEONG_YU: - case HANGUL_JUNGSEONG_YO: - case HANGUL_JUNGSEONG_YEO: - if(*conso_idx == KANA_CONSONANT__) { - *conso_idx = KANA_CONSONANT_Y; - } - else{ - // Insert Jungseong_I before existing jungseong - buffer->jung2 = buffer->jung; - buffer->jung = HANGUL_JUNGSEONG_I; - } - break; - case HANGUL_JUNGSEONG_YE: - case HANGUL_JUNGSEONG_YAE: - buffer->jung = HANGUL_JUNGSEONG_I; - buffer->jung2 = HANGUL_JUNGSEONG_E; - break; - } + switch (buffer->jung) { + case HANGUL_JUNGSEONG_WA: + if (*conso_idx == KANA_CONSONANT__) { + *conso_idx = KANA_CONSONANT_W; + } else { + buffer->jung = HANGUL_JUNGSEONG_O; + buffer->jung2 = HANGUL_JUNGSEONG_A; + } + break; + case HANGUL_JUNGSEONG_YA: + case HANGUL_JUNGSEONG_YU: + case HANGUL_JUNGSEONG_YO: + case HANGUL_JUNGSEONG_YEO: + if (*conso_idx == KANA_CONSONANT__) { + *conso_idx = KANA_CONSONANT_Y; + } else { + // Insert Jungseong_I before existing jungseong + buffer->jung2 = buffer->jung; + buffer->jung = HANGUL_JUNGSEONG_I; + } + break; + case HANGUL_JUNGSEONG_YE: + case HANGUL_JUNGSEONG_YAE: + buffer->jung = HANGUL_JUNGSEONG_I; + buffer->jung2 = HANGUL_JUNGSEONG_E; + break; + } } - -static gint -jungseong_to_kana_index(gunichar jung, gint *vowel_idx) { - gint ret = 0; // In normal indexing, it returns 0 - - switch(jung) { - case HANGUL_JUNGSEONG_WA: - case HANGUL_JUNGSEONG_YA: - case HANGUL_JUNGSEONG_A: - *vowel_idx = KANA_VOWEL_A; - break; - case HANGUL_JUNGSEONG_I: - *vowel_idx = KANA_VOWEL_I; - break; - case HANGUL_JUNGSEONG_YU: - case HANGUL_JUNGSEONG_EU: - case HANGUL_JUNGSEONG_U: - case 0: - *vowel_idx = KANA_VOWEL_U; - break; - case HANGUL_JUNGSEONG_AE: - case HANGUL_JUNGSEONG_E: - *vowel_idx = KANA_VOWEL_E; - break; - case HANGUL_JUNGSEONG_YO: - case HANGUL_JUNGSEONG_YEO: - case HANGUL_JUNGSEONG_EO: - case HANGUL_JUNGSEONG_O: - *vowel_idx = KANA_VOWEL_O; - break; - default: - ret = -1; - } - return ret; + +static gint jungseong_to_kana_index(gunichar jung, gint *vowel_idx) +{ + switch (jung) { + case HANGUL_JUNGSEONG_WA: + case HANGUL_JUNGSEONG_YA: + case HANGUL_JUNGSEONG_A: + *vowel_idx = KANA_VOWEL_A; + break; + case HANGUL_JUNGSEONG_I: + *vowel_idx = KANA_VOWEL_I; + break; + case HANGUL_JUNGSEONG_YU: + case HANGUL_JUNGSEONG_EU: + case HANGUL_JUNGSEONG_U: + case 0: + *vowel_idx = KANA_VOWEL_U; + break; + case HANGUL_JUNGSEONG_AE: + case HANGUL_JUNGSEONG_E: + *vowel_idx = KANA_VOWEL_E; + break; + case HANGUL_JUNGSEONG_YO: + case HANGUL_JUNGSEONG_YEO: + case HANGUL_JUNGSEONG_EO: + case HANGUL_JUNGSEONG_O: + *vowel_idx = KANA_VOWEL_O; + break; + default: + return KANA_CONV_FAIL; + } + + return KANA_CONV_SUCCESS; } -static gint -jongseong_to_kana(gunichar jong, gunichar *kana) { - gint ret = 0; // In normal conversion, it returns 0 - - switch (jong) { - case HANGUL_JONGSEONG_KIYEOK: - case HANGUL_JONGSEONG_SSANGKIYEOK: - case HANGUL_JONGSEONG_SIOS: - case HANGUL_JONGSEONG_SSANGSIOS: - case HANGUL_JONGSEONG_KHIEUKH: - *kana = KANA_SMALL_TU; - break; - case HANGUL_JONGSEONG_NIEUN: - case HANGUL_JONGSEONG_MIEUM: - case HANGUL_JONGSEONG_PIEUP: - case HANGUL_JONGSEONG_PHIEUPH: - case HANGUL_JONGSEONG_IEUNG: - *kana = KANA_NN; - break; - case 0: - *kana = 0; - break; - default: - ret = -1; - } - return ret; + +static gint jongseong_to_kana(gunichar jong, gunichar *kana) +{ + switch (jong) { + case HANGUL_JONGSEONG_KIYEOK: + case HANGUL_JONGSEONG_SSANGKIYEOK: + case HANGUL_JONGSEONG_SIOS: + case HANGUL_JONGSEONG_SSANGSIOS: + case HANGUL_JONGSEONG_KHIEUKH: + *kana = KANA_SMALL_TU; + break; + case HANGUL_JONGSEONG_NIEUN: + case HANGUL_JONGSEONG_MIEUM: + case HANGUL_JONGSEONG_PIEUP: + case HANGUL_JONGSEONG_PHIEUPH: + case HANGUL_JONGSEONG_IEUNG: + *kana = KANA_NN; + break; + case 0: + *kana = 0; + break; + default: + return KANA_CONV_FAIL; + } + + return KANA_CONV_SUCCESS; } -static gint -hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) + +static gint hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) { gint i, r = 0; gint conso_idx, vowel_idx, diacrit; - gint conv_err; + gint conv_err; JungBox jungkey; gunichar jamo, kana; gunichar *comb_jungseong; @@ -286,15 +313,15 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); // Check buffer - if (!hanjp_buffer_is_valid(buffer)) { - r = hanjp_buffer_copy_jamoes(buffer, dest); + if (!hanjp_buffer_is_valid(buffer)) { + r = hanjp_buffer_copy_jamoes(buffer, dest); return -r; } hanjp_buffer_clear_filler(buffer); - // check whether batchim is available and move choseong to jongseong if conditions are met - if(buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) { + // When Batchim is available, then move choseong to jongseong + if (buffer->cho != 0 && buffer->jung == 0 && dest->len != 0) { kana = g_array_index(dest, gunichar, dest->len - 1); // Last kana character if(kana != KANA_NN && kana != KANA_SMALL_TU) { buffer->jong = hangul_choseong_to_jongseong(buffer->cho); @@ -302,45 +329,45 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) } } - //eat Choseong and Jungseong - while(buffer->cho || buffer->jung || buffer->jung2) { - // Convert choseong into kana indexing - jamo = hanjp_buffer_pop_choseong(buffer); - conv_err = choseong_to_kana_index(jamo, &conso_idx, &diacrit); - if (conv_err && jamo == HANGUL_CHOSEONG_SSANGNIEUN) { - // it directly converts into KANA_NN - g_array_append_val(dest, KANA_NN); - r++; - continue; - } else if (conv_err) { - r += hanjp_buffer_copy_jamoes(buffer, dest); - return -r; - } + // eat Choseong and Jungseong + while (buffer->cho || buffer->jung || buffer->jung2) { + // Convert choseong into kana indexing + jamo = hanjp_buffer_pop_choseong(buffer); + conv_err = choseong_to_kana_index(jamo, &conso_idx, &diacrit); + if (conv_err && jamo == HANGUL_CHOSEONG_SSANGNIEUN) { + // it directly converts into KANA_NN + g_array_append_val(dest, KANA_NN); + r++; + continue; + } else if (conv_err) { + r += hanjp_buffer_copy_jamoes(buffer, dest); + return -r; + } // try reducing double Jungseong to single character - hanjp_buffer_align_jungseong(buffer); + hanjp_buffer_align_jungseong(buffer); if (buffer->jung2 != 0) { jungkey.jung = buffer->jung; jungkey.jung2 = buffer->jung2; comb_jungseong = (gunichar *)g_hash_table_lookup(priv->combine_table, &jungkey.value); - if(comb_jungseong != NULL) { + if (comb_jungseong != NULL) { buffer->jung = *comb_jungseong; - buffer->jung2 = 0; + buffer->jung2 = 0; } } // Divide Jungseong into eatable - divide_jungseong(buffer, &conso_idx); - + divide_jungseong(buffer, &conso_idx); + //select column index jamo = hanjp_buffer_pop_jungseong(buffer); // victim - conv_err = jungseong_to_kana_index(jamo, &vowel_idx); - if (conv_err) { - r += hanjp_buffer_copy_jamoes(buffer, dest); - return -r; - } - - // indexing done, convert into kana + conv_err = jungseong_to_kana_index(jamo, &vowel_idx); + if (conv_err) { + r += hanjp_buffer_copy_jamoes(buffer, dest); + return -r; + } + + // indexing done, convert into kana kana = KANA_TABLE[conso_idx][vowel_idx] + diacrit; g_array_append_val(dest, kana); r++; @@ -348,15 +375,14 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) // eat jongseong jamo = hanjp_buffer_pop_jongseong(buffer); - conv_err = jongseong_to_kana(jamo, &kana); - if (conv_err) { - // promote jongseong into choseong and retry convert + conv_err = jongseong_to_kana(jamo, &kana); + if (conv_err) { + // promote jongseong into choseong and retry convert jamo = hangul_jongseong_to_choseong(jamo); - hanjp_buffer_push(buffer, jamo); + hanjp_buffer_push(buffer, jamo); r += hanjp_am_base_to_kana(am, dest, buffer); - } - else if (jamo != 0) { - // When jamo were not empty, it may converted + } else if (jamo != 0) { + // When jamo were not empty, it may converted g_array_append_val(dest, kana); r++; } @@ -364,8 +390,8 @@ hanjp_am_base_to_kana(HanjpAutomata *am, GArray *dest, HanjpBuffer *buffer) return r; } -static void -hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) + +static void hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) { JungBox jungkey; gunichar c; @@ -375,9 +401,9 @@ hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); - hanjp_buffer_align_jungseong(&priv->buffer); + hanjp_buffer_align_jungseong(&priv->buffer); - if(priv->buffer.jung != 0 && priv->buffer.jung2 != 0) { + if (priv->buffer.jung != 0 && priv->buffer.jung2 != 0) { jungkey.jung = priv->buffer.jung; jungkey.jung2 = priv->buffer.jung2; val = (gunichar*)g_hash_table_lookup(priv->combine_table, &jungkey.value); @@ -388,23 +414,22 @@ hanjp_am_base_peek(HanjpAutomata *am, GArray *hangul) } c = hangul_jamo_to_syllable(priv->buffer.cho, priv->buffer.jung, priv->buffer.jong); - if(c == 0) { - for(i = 0; i < 4; i++) { + if (c == 0) { + for (i = 0; i < 4; i++) { c = priv->buffer.stack[i]; - if(c != 0) { + if (c != 0) { c = hangul_jamo_to_cjamo(c); g_array_append_val(hangul, c); break; } } - } - else { + } else { g_array_append_val(hangul, c); } } -static gboolean -hanjp_am_base_backspace(HanjpAutomata *am) + +static gboolean hanjp_am_base_backspace(HanjpAutomata *am) { int i; HanjpAutomataBasePrivate *priv; @@ -413,26 +438,26 @@ hanjp_am_base_backspace(HanjpAutomata *am) return (hanjp_buffer_pop(&priv->buffer) != 0); } -static void -hanjp_am_base_flush(HanjpAutomata *am) + +static void hanjp_am_base_flush(HanjpAutomata *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); hanjp_buffer_flush(&priv->buffer); } -static void -hanjp_am_base_init(HanjpAutomataBase *am) + +static void hanjp_am_base_init(HanjpAutomataBase *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(am); - hanjp_buffer_flush(&priv->buffer); + hanjp_buffer_flush(&priv->buffer); priv->combine_table = g_hash_table_new(g_int64_hash, g_int64_equal); } -static void -hanjp_am_base_finalize(GObject *gobject) + +static void hanjp_am_base_finalize(GObject *gobject) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(gobject)); @@ -442,8 +467,8 @@ hanjp_am_base_finalize(GObject *gobject) G_OBJECT_CLASS(hanjp_am_base_parent_class)->finalize(gobject); } -static void -hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) + +static void hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -455,16 +480,16 @@ hanjp_am_base_class_init(HanjpAutomataBaseClass *klass) klass->flush = hanjp_am_base_flush; } -/**/ -/* AutomataBuiltin Implementation */ +/* + * AutomataBuiltin Implementation + */ static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface); G_DEFINE_TYPE_WITH_CODE(HanjpAutomataBuiltin, hanjp_am_builtin, HANJP_TYPE_AM_BASE, - G_IMPLEMENT_INTERFACE(HANJP_TYPE_AM, - hanjp_am_builtin_interface_init)) + G_IMPLEMENT_INTERFACE(HANJP_TYPE_AM, hanjp_am_builtin_interface_init)) -static void -hanjp_am_builtin_init(HanjpAutomataBuiltin *am) + +static void hanjp_am_builtin_init(HanjpAutomataBuiltin *am) { HanjpAutomataBasePrivate *priv; priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); @@ -475,8 +500,8 @@ hanjp_am_builtin_init(HanjpAutomataBuiltin *am) g_hash_table_insert(priv->combine_table, &priv->combine_table_keys[0].value, &priv->combine_table_vals[0]); } -static gint -hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunichar ch) + +static gint hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunichar ch) { gint r = 0; HanjpAutomataBasePrivate *priv; @@ -485,7 +510,7 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich priv = hanjp_am_base_get_instance_private(HANJP_AM_BASE(am)); buffer = &priv->buffer; - if(ch == 0) { + if (ch == 0) { return 0; } @@ -494,16 +519,15 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich // post-step // convert poped string to kana - if(ch != 0 && buffer->jong == 0) { + if (ch != 0 && buffer->jong == 0) { hanjp_am_base_peek(am, hangul); r = hanjp_am_base_to_kana(am, preedit, buffer); - if(!hangul_is_jamo(ch)) { + if (!hangul_is_jamo(ch)) { g_array_append_val(hangul, ch); g_array_append_val(preedit, ch); r++; r = -r; - } - else { + } else { hanjp_buffer_push(buffer, ch); } } @@ -512,26 +536,26 @@ hanjp_am_builtin_push(HanjpAutomata *am, GArray *preedit, GArray *hangul, gunich return r; } -static void -hanjp_am_builtin_class_init(HanjpAutomataBuiltinClass *klass) + +static void hanjp_am_builtin_class_init(HanjpAutomataBuiltinClass *klass) { HanjpAutomataBaseClass *base_class = HANJP_AM_BASE_CLASS(klass); base_class->push = hanjp_am_builtin_push; } + HanjpAutomataBuiltin *hanjp_am_builtin_new() { return g_object_new(HANJP_TYPE_AM_BUILTIN, NULL); } -static void -hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface) + +static void hanjp_am_builtin_interface_init(HanjpAutomataInterface *iface) { iface->to_kana = hanjp_am_base_to_kana; iface->push = hanjp_am_builtin_push; iface->backspace = hanjp_am_base_backspace; iface->flush = hanjp_am_base_flush; } - /**/ diff --git a/hanjpbuffer.c b/hanjpbuffer.c index 4f94e79..52e1d41 100644 --- a/hanjpbuffer.c +++ b/hanjpbuffer.c @@ -2,41 +2,43 @@ #include #include -/* Extern function signature which isn't exported in libhangul header */ +// Extern function signature which aren't exported in libhangul header extern ucschar hangul_choseong_to_jongseong(ucschar c); extern ucschar hangul_jongseong_to_choseong(ucschar c); static const int BUFF_SIZE = - (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); + (sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0])); -/* HanjpBuffer Implementation */ -gunichar -hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) { + +/* + * HanjpBuffer Implementation + */ +gunichar hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) +{ if ((hangul_is_choseong(ch)) && (buffer->cho == 0)) { buffer->cho = ch; - return 0; - } - else if (hangul_is_jungseong(ch)) { - if(buffer->jung == 0) { + return 0; + } else if (hangul_is_jungseong(ch)) { + if (buffer->jung == 0) { buffer->jung = ch; - return 0; + return 0; } - else if(buffer->jung2 == 0) { + else if (buffer->jung2 == 0) { buffer->jung2 = ch; - return 0; + return 0; } - } - else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) { + } else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) { buffer->jong = ch; - return 0; + return 0; } return ch; } -gint -hanjp_buffer_peek(HanjpBuffer *buffer) { - int i; + +gint hanjp_buffer_peek(HanjpBuffer *buffer) +{ + int i; for (i = 0; i < BUFF_SIZE; i++) { if (buffer->stack[i]) { @@ -44,17 +46,18 @@ hanjp_buffer_peek(HanjpBuffer *buffer) { } } - return 0; + return 0; } -gunichar -hanjp_buffer_pop(HanjpBuffer *buffer) { - gint ret = 0; + +gunichar hanjp_buffer_pop(HanjpBuffer *buffer) +{ + gint ret = 0; int i; for (i = BUFF_SIZE-1; i > -1; i--) { if (buffer->stack[i]) { - ret = buffer->stack[i]; + ret = buffer->stack[i]; buffer->stack[i] = 0; break; } @@ -63,83 +66,91 @@ hanjp_buffer_pop(HanjpBuffer *buffer) { return ret; } -void -hanjp_buffer_flush(HanjpBuffer *buffer) { + +void hanjp_buffer_flush(HanjpBuffer *buffer) +{ buffer->cho = 0; buffer->jung = 0; buffer->jung2 = 0; buffer->jong = 0; } -bool -hanjp_buffer_is_valid(HanjpBuffer *buffer) { - int i; - for (i = 0; i < BUFF_SIZE; i++) { - if (buffer->stack[i] && !hangul_is_jamo(buffer->stack[i])) { - return false; - } - } +bool hanjp_buffer_is_valid(HanjpBuffer *buffer) +{ + int i; + + for (i = 0; i < BUFF_SIZE; i++) { + if (buffer->stack[i] && !hangul_is_jamo(buffer->stack[i])) { + return false; + } + } - return true; + return true; } -gint -hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) { - int i; - int copy_cnt = 0; - - for (i = 0; i < BUFF_SIZE; i++) { - if (buffer->stack[i]) { - g_array_append_val(arr, buffer->stack[i]); - copy_cnt += 1; - } - } - return copy_cnt; + +gint hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) +{ + int i; + int copy_cnt = 0; + + for (i = 0; i < BUFF_SIZE; i++) { + if (buffer->stack[i]) { + g_array_append_val(arr, buffer->stack[i]); + copy_cnt += 1; + } + } + return copy_cnt; } -void -hanjp_buffer_clear_filler(HanjpBuffer *buffer) { - int i; - for (i = 0; i < BUFF_SIZE; i++) { - if ((buffer->stack[i] == HANGUL_CHOSEONG_FILLER) - || (buffer->stack[i] == HANGUL_JUNGSEONG_FILLER)) { - buffer->stack[i] = 0; - } - } +void hanjp_buffer_clear_filler(HanjpBuffer *buffer) +{ + int i; + + for (i = 0; i < BUFF_SIZE; i++) { + if ((buffer->stack[i] == HANGUL_CHOSEONG_FILLER) + || (buffer->stack[i] == HANGUL_JUNGSEONG_FILLER)) { + buffer->stack[i] = 0; + } + } } -void -hanjp_buffer_align_jungseong(HanjpBuffer *buffer) { - if (buffer->jung == 0) { - buffer->jung = buffer-> jung2; - buffer->jung2 = 0; - } + +void hanjp_buffer_align_jungseong(HanjpBuffer *buffer) +{ + if (buffer->jung == 0) { + buffer->jung = buffer-> jung2; + buffer->jung2 = 0; + } } -gunichar -hanjp_buffer_pop_choseong(HanjpBuffer *buffer) { - gunichar ch = buffer->cho; - buffer->cho = 0; - return ch; +gunichar hanjp_buffer_pop_choseong(HanjpBuffer *buffer) +{ + gunichar ch = buffer->cho; + buffer->cho = 0; + + return ch; } -gunichar -hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) { - gunichar ch = buffer->jung; - buffer->jung = 0; - return ch; +gunichar hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) +{ + gunichar ch = buffer->jung; + buffer->jung = 0; + + return ch; } -gunichar -hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) { - gunichar ch = buffer->jong; - buffer->jong = 0; - return ch; +gunichar hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) +{ + gunichar ch = buffer->jong; + buffer->jong = 0; + + return ch; } /**/ diff --git a/hanjpbuffer.h b/hanjpbuffer.h index 2f3a3c0..97fc7b6 100644 --- a/hanjpbuffer.h +++ b/hanjpbuffer.h @@ -5,8 +5,10 @@ #include #include -typedef union { - struct { +typedef union +{ + struct + { gunichar cho; gunichar jung; gunichar jung2; diff --git a/hanjpinputcontext.c b/hanjpinputcontext.c index a7bb9ff..7aa788a 100644 --- a/hanjpinputcontext.c +++ b/hanjpinputcontext.c @@ -2,17 +2,22 @@ #include "hanjpautomata.h" #include "hanjpkeyboard.h" -static gboolean is_hiragana(gunichar ch) { +static gboolean is_hiragana(gunichar ch) +{ return (ch >= 0x3041 && ch <= 0x3096) || (ch >= 0x309D && ch <= 0x309E); } -static gboolean is_katakana(gunichar ch) { + +static gboolean is_katakana(gunichar ch) +{ return (ch >= 0x30A1 && ch <= 0x30F6) || (ch >= 0x30FD && ch <= 0x30FE); } + #define KANA_GAP 0x60 -typedef struct { +typedef struct +{ HanjpAutomata *ams[2]; HanjpAutomata *cur_am; HanjpKeyboard *keyboard; @@ -25,8 +30,8 @@ typedef struct { G_DEFINE_TYPE_WITH_PRIVATE(HanjpInputContext, hanjp_ic, G_TYPE_OBJECT) -static void -hanjp_ic_dispose(GObject *self) + +static void hanjp_ic_dispose(GObject *self) { HanjpInputContextPrivate *priv; priv = hanjp_ic_get_instance_private(HANJP_INPUTCONTEXT(self)); @@ -36,24 +41,24 @@ hanjp_ic_dispose(GObject *self) g_clear_object(&priv->cur_am); g_clear_object(&priv->keyboard); g_array_unref(priv->preedit); - priv->preedit = NULL; + priv->preedit = NULL; g_array_unref(priv->committed); - priv->committed = NULL; + priv->committed = NULL; g_array_unref(priv->hangul); - priv->hangul = NULL; + priv->hangul = NULL; - //chain up + // chain up G_OBJECT_CLASS(hanjp_ic_parent_class)->dispose(self); } -static void -hanjp_ic_finalize(GObject *self) + +static void hanjp_ic_finalize(GObject *self) { G_OBJECT_CLASS(hanjp_ic_parent_class)->finalize(self); } -static void -hanjp_ic_class_init(HanjpInputContextClass *klass) + +static void hanjp_ic_class_init(HanjpInputContextClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -61,8 +66,8 @@ hanjp_ic_class_init(HanjpInputContextClass *klass) object_class->finalize = hanjp_ic_finalize; } -static void -hanjp_ic_init(HanjpInputContext *self) + +static void hanjp_ic_init(HanjpInputContext *self) { gint i; HanjpInputContextPrivate *priv; @@ -79,11 +84,13 @@ hanjp_ic_init(HanjpInputContext *self) priv->output_mode = HANJP_OUTPUT_HIRAGANA; } + HanjpInputContext* hanjp_ic_new() { return g_object_new(HANJP_TYPE_INPUTCONTEXT, NULL); } + void hanjp_ic_reset(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -99,6 +106,7 @@ void hanjp_ic_reset(HanjpInputContext *self) priv->output_mode = HANJP_OUTPUT_HIRAGANA; } + void hanjp_ic_flush(HanjpInputContext *self) { gint i; @@ -108,13 +116,14 @@ void hanjp_ic_flush(HanjpInputContext *self) priv = hanjp_ic_get_instance_private(self); hanjp_am_flush(priv->cur_am); - for(i = 0; i < priv->preedit->len; i++) { + for (i = 0; i < priv->preedit->len; i++) { g_array_append_val(priv->committed, g_array_index(priv->preedit, gunichar, i)); } g_array_set_size(priv->preedit, 0); priv->kana_len = 0; } + gint hanjp_ic_process(HanjpInputContext *self, gint ascii) { int i; @@ -125,27 +134,26 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) g_return_if_fail(HANJP_IS_INPUTCONTEXT(self)); priv = hanjp_ic_get_instance_private(self); - //map jaso from ascii + // map jaso from ascii ch = hanjp_kb_get_mapping(priv->keyboard, 0, ascii); - if(ch == 0) { + if (ch == 0) { ch = (gunichar)ascii; } - //shrink preedit before push + // shrink preedit before push g_array_set_size(priv->preedit, priv->kana_len); g_array_set_size(priv->committed, 0); g_array_set_size(priv->hangul, 0); - //push jaso into automata + // push jaso into automata res = hanjp_am_push(priv->cur_am, priv->preedit, priv->hangul, ch); - if(res < 0) { + if (res < 0) { priv->kana_len += -res; hanjp_ic_flush(self); - } - else if(res > 0) { + } else if(res > 0) { // if mode is katakana, change result to katakana - if(priv->output_mode == HANJP_OUTPUT_KATAKANA) { - for(i = priv->kana_len; i < priv->preedit->len; i++) { - if(!is_hiragana(g_array_index(priv->preedit, gunichar, i))){ + if (priv->output_mode == HANJP_OUTPUT_KATAKANA) { + for (i = priv->kana_len; i < priv->preedit->len; i++) { + if (!is_hiragana(g_array_index(priv->preedit, gunichar, i))) { continue; } g_array_index(priv->preedit, gunichar, i) += KANA_GAP; @@ -157,6 +165,7 @@ gint hanjp_ic_process(HanjpInputContext *self, gint ascii) return res; } + void hanjp_ic_toggle_preedit(HanjpInputContext *self) { int i; @@ -174,6 +183,7 @@ void hanjp_ic_toggle_preedit(HanjpInputContext *self) } } + void hanjp_ic_to_haragana_preedit(HanjpInputContext *self) { int i; @@ -189,7 +199,9 @@ void hanjp_ic_to_haragana_preedit(HanjpInputContext *self) } } -void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) { + +void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) +{ int i; HanjpInputContextPrivate *priv; @@ -203,6 +215,7 @@ void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) { } } + void hanjp_ic_replace(HanjpInputContext *self, int start, int end, const gunichar* str_insert) { int i; @@ -215,14 +228,14 @@ void hanjp_ic_replace(HanjpInputContext *self, int start, int end, const gunicha g_array_remove_range(priv->preedit, start, end - start); - while (*str_insert) - { + while (*str_insert) { g_array_insert_val(priv->preedit, start, *str_insert); start++; str_insert++; } } + gboolean hanjp_ic_backspace(HanjpInputContext *self) { g_return_if_fail(HANJP_IS_INPUTCONTEXT(self)); @@ -230,21 +243,18 @@ gboolean hanjp_ic_backspace(HanjpInputContext *self) HanjpInputContextPrivate *priv; priv = hanjp_ic_get_instance_private(self); - if(!hanjp_am_backspace(priv->cur_am)){ - - if((priv->preedit->len)==0) - { + if (!hanjp_am_backspace(priv->cur_am)) { + if ((priv->preedit->len)==0) { return FALSE; } g_array_remove_index(priv->preedit,(priv->preedit->len)-1); - } return TRUE; - } + GArray* hanjp_ic_ref_preedit_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -255,6 +265,7 @@ GArray* hanjp_ic_ref_preedit_string(HanjpInputContext *self) return g_array_ref(priv->preedit); } + GArray* hanjp_ic_ref_commit_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -265,6 +276,7 @@ GArray* hanjp_ic_ref_commit_string(HanjpInputContext *self) return g_array_ref(priv->committed); } + GArray* hanjp_ic_ref_hangul_string(HanjpInputContext *self) { HanjpInputContextPrivate *priv; @@ -275,6 +287,7 @@ GArray* hanjp_ic_ref_hangul_string(HanjpInputContext *self) return g_array_ref(priv->hangul); } + void hanjp_ic_set_am(HanjpInputContext *self, gint i) { HanjpInputContextPrivate *priv; @@ -286,6 +299,7 @@ void hanjp_ic_set_am(HanjpInputContext *self, gint i) priv->cur_am = g_object_ref(priv->ams[i]); } + void hanjp_ic_set_output_mode(HanjpInputContext *self, gint i) { HanjpInputContextPrivate *priv; @@ -295,6 +309,7 @@ void hanjp_ic_set_output_mode(HanjpInputContext *self, gint i) priv->output_mode = i; } + gint hanjp_ic_get_output_mode(HanjpInputContext *self) { HanjpInputContextPrivate *priv; diff --git a/hanjpinputcontext.h b/hanjpinputcontext.h index 6d99713..0af44ad 100644 --- a/hanjpinputcontext.h +++ b/hanjpinputcontext.h @@ -6,7 +6,8 @@ G_BEGIN_DECLS -enum { +enum +{ HANJP_OUTPUT_HIRAGANA, HANJP_OUTPUT_KATAKANA, HANJP_OUTPUT_HALFKATAKANA @@ -53,4 +54,4 @@ gint hanjp_ic_get_output_mode(HanjpInputContext *self); G_END_DECLS -#endif // __HANJP_INPUTCONTEXT_H__ \ No newline at end of file +#endif // __HANJP_INPUTCONTEXT_H__ diff --git a/hanjpkeyboard.c b/hanjpkeyboard.c index c4b2878..431f09a 100644 --- a/hanjpkeyboard.c +++ b/hanjpkeyboard.c @@ -1,16 +1,22 @@ #include "hanjpkeyboard.h" #include "hangul.h" -extern ucschar -hangul_keyboard_get_mapping(const HangulKeyboard* keyboard, int tableid, unsigned key); +// Extern function signature which isn't exported in libhangul header +extern ucschar hangul_keyboard_get_mapping(const HangulKeyboard* keyboard, int tableid, unsigned key); + +/* + * Keyboard Interface Definition + */ G_DEFINE_INTERFACE(HanjpKeyboard, hanjp_kb, G_TYPE_OBJECT) -static void -hanjp_kb_default_init(HanjpKeyboardInterface *iface) { - // Nothing to do + +static void hanjp_kb_default_init(HanjpKeyboardInterface *iface) +{ + // Nothing to do } + gunichar hanjp_kb_get_mapping(HanjpKeyboard* self, gint tableid, gint ascii) { HanjpKeyboardInterface *iface; @@ -22,33 +28,37 @@ gunichar hanjp_kb_get_mapping(HanjpKeyboard* self, gint tableid, gint ascii) return iface->get_mapping(self, tableid, ascii); } -typedef struct { + +typedef struct +{ HangulKeyboard *keyboard; } HanjpKeyboardBuiltinPrivate; + static void hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface); G_DEFINE_TYPE_WITH_CODE(HanjpKeyboardBuiltin, hanjp_kb_builtin, G_TYPE_OBJECT, G_ADD_PRIVATE(HanjpKeyboardBuiltin) G_IMPLEMENT_INTERFACE(HANJP_TYPE_KB, hanjp_kb_builtin_interface_init)) + HanjpKeyboardBuiltin *hanjp_kb_builtin_new() { return g_object_new(HANJP_TYPE_KEYBOARDDEFAULT, NULL); } -static gunichar -hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) + +static gunichar hanjp_kb_builtin_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(HANJP_KB_BUILTIN(self)); - g_return_if_fail(priv->keyboard != NULL); + g_return_if_fail(priv->keyboard != NULL); - return hangul_keyboard_get_mapping(priv->keyboard, tableid, ascii); + return hangul_keyboard_get_mapping(priv->keyboard, tableid, ascii); } -static void -hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) + +static void hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(self); @@ -56,14 +66,14 @@ hanjp_kb_builtin_init(HanjpKeyboardBuiltin *self) priv->keyboard = hangul_keyboard_list_get_keyboard("2"); } -static void -hanjp_kb_builtin_dispose(GObject *gobject) + +static void hanjp_kb_builtin_dispose(GObject *gobject) { G_OBJECT_CLASS(hanjp_kb_builtin_parent_class)->dispose(gobject); } -static void -hanjp_kb_builtin_finalize(GObject *gobject) + +static void hanjp_kb_builtin_finalize(GObject *gobject) { HanjpKeyboardBuiltinPrivate *priv; priv = hanjp_kb_builtin_get_instance_private(HANJP_KB_BUILTIN(gobject)); @@ -71,8 +81,8 @@ hanjp_kb_builtin_finalize(GObject *gobject) G_OBJECT_CLASS(hanjp_kb_builtin_parent_class)->finalize(gobject); } -static void -hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) + +static void hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); @@ -80,8 +90,8 @@ hanjp_kb_builtin_class_init(HanjpKeyboardBuiltinClass *klass) object_class->finalize = hanjp_kb_builtin_finalize; } -static void -hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface) + +static void hanjp_kb_builtin_interface_init(HanjpKeyboardInterface *iface) { iface->get_mapping = hanjp_kb_builtin_get_mapping; } diff --git a/hanjpkeyboard.h b/hanjpkeyboard.h index f94f699..f820028 100644 --- a/hanjpkeyboard.h +++ b/hanjpkeyboard.h @@ -8,7 +8,8 @@ G_BEGIN_DECLS #define HANJP_TYPE_KB hanjp_kb_get_type() G_DECLARE_INTERFACE(HanjpKeyboard, hanjp_kb, HANJP, KB, GObject) -struct _HanjpKeyboardInterface { +struct _HanjpKeyboardInterface +{ GTypeInterface parent_iface; gunichar (*get_mapping) (HanjpKeyboard *self, gint tableid, gint ascii); @@ -19,7 +20,8 @@ gunichar hanjp_kb_get_mapping(HanjpKeyboard *self, gint tableid, gint ascii); #define HANJP_TYPE_KEYBOARDDEFAULT hanjp_kb_builtin_get_type() G_DECLARE_DERIVABLE_TYPE(HanjpKeyboardBuiltin, hanjp_kb_builtin, HANJP, KB_BUILTIN, GObject) -struct _HanjpKeyboardBuiltinClass { +struct _HanjpKeyboardBuiltinClass +{ GObjectClass parent_class; }; @@ -27,4 +29,4 @@ HanjpKeyboardBuiltin *hanjp_kb_builtin_new(); G_END_DECLS -#endif // __HANJP_KEYBOARD_H__ \ No newline at end of file +#endif // __HANJP_KEYBOARD_H__ diff --git a/hanjpunicode.h b/hanjpunicode.h index b1b93b4..b9ef7f8 100644 --- a/hanjpunicode.h +++ b/hanjpunicode.h @@ -4,8 +4,9 @@ // The belows are Unicode presets for Hangul syllables // syllable leading consonants Jamo (초성) -enum { - // modern Hangul leading consonants +enum +{ + // modern Hangul leading consonants HANGUL_CHOSEONG_KIYEOK = 0x1100, HANGUL_CHOSEONG_SSANGKIYEOK, HANGUL_CHOSEONG_NIEUN, @@ -105,7 +106,8 @@ enum { }; // syllable medial vowels Jamo (중성) -enum { +enum +{ // modern Hangul medial vowels HANGUL_JUNGSEONG_A = 0x1161, HANGUL_JUNGSEONG_AE, @@ -181,8 +183,9 @@ enum { HANGUL_JUNGSEONG_O_YAE }; -// syllable trailing consonants jamo (초성) -enum { +// syllable trailing consonants jamo (종성) +enum +{ // modern Hangul trailing consonants HANGUL_JONGSEONG_KIYEOK = 0x11A8, // ㄱ HANGUL_JONGSEONG_SSANGKIYEOK, // ㄲ diff --git a/test/test_gobj_construct_dispose.cpp b/test/test_gobj_construct_dispose.cpp index 86a73b3..803711f 100644 --- a/test/test_gobj_construct_dispose.cpp +++ b/test/test_gobj_construct_dispose.cpp @@ -4,80 +4,83 @@ #include "hanjpinputcontext.h" -TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) { - // Given HanjpAutomataDefault object - HanjpAutomataBuiltin *amBuiltin = NULL; - const char* strExpectObjType = "HanjpAutomataBuiltin"; - const char* strActualObjType = NULL; - - // When constructed - amBuiltin = hanjp_am_builtin_new(); - - // Then, object pointer must not be null - ASSERT_NE(amBuiltin, nullptr) - << "HanjpAutomataBuiltin construct failed"; - - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(amBuiltin); - ASSERT_STREQ(strActualObjType, strExpectObjType); - - // When destructed - g_object_unref(amBuiltin); - - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(amBuiltin)) - << "HanjpAutomataBuiltin destruct failed"; +TEST(GObjectConstructDisposeTest, AutomataDefaultConstructDispose) +{ + // Given HanjpAutomataDefault object + HanjpAutomataBuiltin *amBuiltin = NULL; + const char* strExpectObjType = "HanjpAutomataBuiltin"; + const char* strActualObjType = NULL; + + // When constructed + amBuiltin = hanjp_am_builtin_new(); + + // Then, object pointer must not be null + ASSERT_NE(amBuiltin, nullptr) + << "HanjpAutomataBuiltin construct failed"; + + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(amBuiltin); + ASSERT_STREQ(strActualObjType, strExpectObjType); + + // When destructed + g_object_unref(amBuiltin); + + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(amBuiltin)) + << "HanjpAutomataBuiltin destruct failed"; } -TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) { - // Given HanjpKeyboardDefault object - HanjpKeyboardBuiltin *kbBuiltin = NULL; - const char* strExpectObjType = "HanjpKeyboardBuiltin"; - const char* strActualObjType = NULL; +TEST(GObjectConstructDisposeTest, KeyboardDefaultConstructDispose) +{ + // Given HanjpKeyboardDefault object + HanjpKeyboardBuiltin *kbBuiltin = NULL; + const char* strExpectObjType = "HanjpKeyboardBuiltin"; + const char* strActualObjType = NULL; - // When constructed - kbBuiltin = hanjp_kb_builtin_new(); + // When constructed + kbBuiltin = hanjp_kb_builtin_new(); - // Then, object pointer must not be null - ASSERT_NE(kbBuiltin, nullptr) - << "HanjpKeyboardBuiltin construct failed"; + // Then, object pointer must not be null + ASSERT_NE(kbBuiltin, nullptr) + << "HanjpKeyboardBuiltin construct failed"; - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(kbBuiltin); - ASSERT_STREQ(strActualObjType, strExpectObjType); + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(kbBuiltin); + ASSERT_STREQ(strActualObjType, strExpectObjType); - // When destructed - g_object_unref(kbBuiltin); + // When destructed + g_object_unref(kbBuiltin); - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(kbBuiltin)) - << "HanjpKeyboardBuiltin destruct failed"; + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(kbBuiltin)) + << "HanjpKeyboardBuiltin destruct failed"; } -TEST(GObjectConstructDisposeTest, InputContextConstructDispose) { - // Given HanjpInputContext object - HanjpInputContext *ic = NULL; - const char* strExpectObjType = "HanjpInputContext"; - const char* strActualObjType = NULL; +TEST(GObjectConstructDisposeTest, InputContextConstructDispose) +{ + // Given HanjpInputContext object + HanjpInputContext *ic = NULL; + const char* strExpectObjType = "HanjpInputContext"; + const char* strActualObjType = NULL; - // When constructed - ic = hanjp_ic_new(); + // When constructed + ic = hanjp_ic_new(); - // Then, object pointer must not be null - ASSERT_NE(ic, nullptr) - << "HanjpInputContext construct failed"; + // Then, object pointer must not be null + ASSERT_NE(ic, nullptr) + << "HanjpInputContext construct failed"; - // Then, object type name must be same as the class name - strActualObjType = G_OBJECT_TYPE_NAME(ic); - ASSERT_STREQ(strActualObjType, strExpectObjType); + // Then, object type name must be same as the class name + strActualObjType = G_OBJECT_TYPE_NAME(ic); + ASSERT_STREQ(strActualObjType, strExpectObjType); - // When destructed - g_object_unref(ic); + // When destructed + g_object_unref(ic); - // Then, type check must be false - ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(ic)) - << "HanjpInputContext destruct failed"; + // Then, type check must be false + ASSERT_FALSE(G_TYPE_CHECK_INSTANCE(ic)) + << "HanjpInputContext destruct failed"; } diff --git a/test/test_input_context_convert.cpp b/test/test_input_context_convert.cpp index 9fb9e2b..ae3c057 100644 --- a/test/test_input_context_convert.cpp +++ b/test/test_input_context_convert.cpp @@ -1,44 +1,47 @@ #include #include "hanjpinputcontext.h" -class InputContextTest : public ::testing::Test { +class InputContextTest : public ::testing::Test +{ protected: - void SetUp() override { - // Create context object for each tests + void SetUp() override { + // Create context object for each tests context = hanjp_ic_new(); } void TearDown() override { - // Destruct context object when each tests were done + // Destruct context object when each tests were done g_object_unref(context); } HanjpInputContext *context; }; -TEST_F(InputContextTest, IC_Hajimemashite) { +TEST_F(InputContextTest, IC_Hajimemashite) +{ const char *strGivenKeyInput = "gkwlapaktlxp."; - const char *strExpectJapanese = "はじめまして."; - const char *strActualJapanese = NULL; + const char *strExpectJapanese = "はじめまして."; + const char *strActualJapanese = NULL; - // Given Hanjp Input context + // Given Hanjp Input context GArray *committed = hanjp_ic_ref_commit_string(context); - // When given key input were processed + // When given key input were processed while(*strGivenKeyInput) { hanjp_ic_process(context, (gint)(*strGivenKeyInput)); strGivenKeyInput++; } strActualJapanese = g_ucs4_to_utf8((const gunichar*)committed->data, committed->len, NULL, NULL, NULL); - // Then, converted japanese must be same as expected + // Then, converted japanese must be same as expected EXPECT_STREQ(strActualJapanese, strExpectJapanese); - // Destruct temporal variable in test + // Destruct temporal variable in test g_array_unref(committed); } -TEST_F(InputContextTest, IC_KYOUWAKOKU) { +TEST_F(InputContextTest, IC_KYOUWAKOKU) +{ const char *strGivenKeyInput = "zydndhkzhzn."; const char *strExpectJapenese = "きょうわこく."; const char *strActualJapanese = NULL;