OSDN Git Service

implement chekc each character's state for text insert.
authorornse01 <ornse01@users.sourceforge.jp>
Sun, 1 Dec 2013 05:06:06 +0000 (05:06 +0000)
committerornse01 <ornse01@users.sourceforge.jp>
Sun, 1 Dec 2013 05:06:06 +0000 (05:06 +0000)
git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchanf/trunk@592 20a0b8eb-f62a-4a12-8fe1-b598822500fb

src/control/test_texteditor_characterstate.c [new file with mode: 0644]
src/control/texteditor_characterstate.c [new file with mode: 0644]
src/control/texteditor_characterstate.h [new file with mode: 0644]

diff --git a/src/control/test_texteditor_characterstate.c b/src/control/test_texteditor_characterstate.c
new file mode 100644 (file)
index 0000000..4c6c2f4
--- /dev/null
@@ -0,0 +1,240 @@
+/*
+ * test_texteditor_characterstate.c
+ *
+ * Copyright (c) 2013 project bchan
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ *    distribution.
+ *
+ */
+
+#include "test_control.h"
+
+#include "texteditor_characterstate.h"
+
+#include       <basic.h>
+#include       <bstdio.h>
+#include       <bstdlib.h>
+#include       <bstring.h>
+#include       <tstring.h>
+
+#include    <unittest_driver.h>
+
+typedef struct {
+       tadfragment_cursor_segment input;
+       struct {
+               TC *lang;
+               W lang_len;
+               Bool is_hankaku;
+       } expected;
+} test_texteditor_characterstate_t;
+
+LOCAL UNITTEST_RESULT test_texteditor_characterstate_common(test_texteditor_characterstate_t *testdata, W len)
+{
+       texteditor_characterstate_t state;
+       W i, err;
+       Bool is_hankaku, ok;
+       TC *lang, lang_len;
+       UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
+
+       texteditor_characterstate_initialize(&state);
+
+       for (i = 0; i < len; i++) {
+               err = texteditor_charactorstate_input(&state, &(testdata[i].input));
+               if (err < 0) {
+                       printf("[%d] texteditor_charactorstate_input error\n", i);
+                       result = UNITTEST_RESULT_FAIL;
+                       break;
+               }
+               is_hankaku = texteditor_characterstate_ishankaku(&state);
+               if (is_hankaku != False) {
+                       if (testdata[i].expected.is_hankaku == False) {
+                               printf("[%d] ishankaku error\n", i);
+                               result = UNITTEST_RESULT_FAIL;
+                       }
+               } else {
+                       if (testdata[i].expected.is_hankaku != False) {
+                               printf("[%d] ishankaku error\n", i);
+                               result = UNITTEST_RESULT_FAIL;
+                       }
+               }
+
+               ok = texteditor_characterstate_islang(&state, testdata[i].expected.lang, testdata[i].expected.lang_len);
+               if (ok == False) {
+                       printf("[%d] islang error\n", i);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+
+               lang_len = texteditor_characterstate_getlang(&state, NULL, -1);
+               if (lang_len != testdata[i].expected.lang_len) {
+                       printf("[%d] getlang length 1 error. expected = %d, result = %d\n", i, testdata[i].expected.lang_len, lang_len);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+
+               lang = malloc(sizeof(TC)*testdata[i].expected.lang_len);
+               lang_len = texteditor_characterstate_getlang(&state, lang, testdata[i].expected.lang_len);
+               if (lang_len != testdata[i].expected.lang_len) {
+                       printf("[%d] getlang length 2 error. expected = %d, result = %d\n", i, testdata[i].expected.lang_len, lang_len);
+                       result = UNITTEST_RESULT_FAIL;
+               } else {
+                       if (tc_strncmp(lang, testdata[i].expected.lang, lang_len) != 0) {
+                               printf("[%d] getlang code error\n", i);
+                               result = UNITTEST_RESULT_FAIL;
+                       }
+               }
+               free(lang);
+       }
+
+       texteditor_characterstate_finalize(&state);
+
+       return result;
+}
+
+LOCAL UNITTEST_RESULT test_texteditor_characterstate_1()
+{
+       test_texteditor_characterstate_t testdata[] = {
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x21, 0xFE}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x22, 0xFE}, 2},
+                       {(TC[]){0xFE22}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0xFE, 0xFE, 0x21, 0xFE}, 4},
+                       {(TC[]){0xFEFE, 0xFE21}, 2, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0xFE, 0xFE, 0x21, 0x00}, 4},
+                       {(TC[]){0xFEFE, 0x0021}, 2, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0xFE, 0xFE, 0xFE, 0xFE, 0x21, 0xFE}, 6},
+                       {(TC[]){0xFEFE, 0xFEFE, 0xFE21}, 3, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0xFE, 0xFE, 0xFE, 0xFE, 0x21, 0x00}, 6},
+                       {(TC[]){0xFEFE, 0xFEFE, 0x0021}, 3, False}
+               }
+       };
+       return test_texteditor_characterstate_common(testdata, 6);
+}
+
+LOCAL UNITTEST_RESULT test_texteditor_characterstate_2()
+{
+       test_texteditor_characterstate_t testdata[] = {
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x22, 0xFE}, 2},
+                       {(TC[]){0xFE22}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE22}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x21, 0xFE}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+       };
+       return test_texteditor_characterstate_common(testdata, 5);
+}
+
+LOCAL UNITTEST_RESULT test_texteditor_characterstate_3()
+{
+       test_texteditor_characterstate_t testdata[] = {
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00}, 10},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x01}, 10},
+                       {(TC[]){0xFE21}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00}, 10},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x01}, 10},
+                       {(TC[]){0xFE21}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01, 0x01}, 10},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+       };
+       return test_texteditor_characterstate_common(testdata, 5);
+}
+
+
+LOCAL UNITTEST_RESULT test_texteditor_characterstate_4()
+{
+       test_texteditor_characterstate_t testdata[] = {
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x01}, 10},
+                       {(TC[]){0xFE21}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE21}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x22, 0xFE}, 2},
+                       {(TC[]){0xFE22}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE22}, 1, True}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB[]){0xA2, 0xFF, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00}, 10},
+                       {(TC[]){0xFE22}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE22}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB[]){0x21, 0xFE}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+               {
+                       {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB[]){0x21, 0x21}, 2},
+                       {(TC[]){0xFE21}, 1, False}
+               },
+       };
+       return test_texteditor_characterstate_common(testdata, 8);
+}
+
+EXPORT VOID test_texteditor_characterstate_main(unittest_driver_t *driver)
+{
+       UNITTEST_DRIVER_REGIST(driver, test_texteditor_characterstate_1);
+       UNITTEST_DRIVER_REGIST(driver, test_texteditor_characterstate_2);
+       UNITTEST_DRIVER_REGIST(driver, test_texteditor_characterstate_3);
+       UNITTEST_DRIVER_REGIST(driver, test_texteditor_characterstate_4);
+}
diff --git a/src/control/texteditor_characterstate.c b/src/control/texteditor_characterstate.c
new file mode 100644 (file)
index 0000000..7cbea56
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * texteditor_characterstate.h
+ *
+ * Copyright (c) 2013 project bchan
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ *    distribution.
+ *
+ */
+
+#include       "texteditor_characterstate.h"
+
+#include       <bstdio.h>
+
+#ifdef BCHAN_CONFIG_DEBUG
+# define DP(arg) printf arg
+# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
+#else
+# define DP(arg) /**/
+# define DP_ER(msg, err) /**/
+#endif
+
+EXPORT W texteditor_charactorstate_input(texteditor_characterstate_t *state, tadfragment_cursor_segment *segment)
+{
+       TC ch;
+       W iter;
+       UB segid, subid;
+
+       if (segment->type == TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE) {
+               if (segment->len != 10) {
+                       return 0;
+               }
+               segid = *(TC*)segment->p & 0xFF;
+               if (segid != TS_TFONT) {
+                       return 0;
+               }
+               subid = *(TC*)(segment->p + 4) >> 8;
+               if (subid != 3) {
+                       return 0;
+               }
+               state->w_ratio = *(RATIO*)(segment->p + 8);
+       } else if (segment->type == TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE) {
+               ch = *(TC*)(segment->p + segment->len - 2);
+               iter = segment->len - 2;
+               if ((ch & 0xFE00) == 0xFE00) {
+                       iter++;
+               }
+               state->lang.iter = iter - 1;
+               state->lang.code = 0x00FF & ch;
+       }
+       return 0;
+}
+
+EXPORT Bool texteditor_characterstate_ishankaku(texteditor_characterstate_t *state)
+{
+       W ratio_a, ratio_b;
+
+       ratio_a = state->w_ratio >> 8;
+       ratio_b = state->w_ratio & 0xFF;
+       if ((ratio_a * 2 > ratio_b)||(ratio_b == 0)) {
+               return False;
+       } else {
+               return True;
+       }
+}
+
+EXPORT W texteditor_characterstate_getlang(texteditor_characterstate_t *state, TC *str, W len)
+{
+       W i, req_len;
+
+       req_len = 2 + (state->lang.iter+1)/2*2;
+
+       if (str == NULL || len < 0) {
+               return req_len / sizeof(TC);
+       }
+
+       for (i = 0; i < (state->lang.iter + 1) / 2; i++) {
+               str[i] = 0xFEFE;
+       }
+       if (state->lang.iter % 2 == 1) {
+               str[i] = state->lang.code;
+       } else {
+               str[i] = 0xFE00 | state->lang.code;
+       }
+
+       return req_len / sizeof(TC);
+}
+
+EXPORT Bool texteditor_characterstate_islang(texteditor_characterstate_t *state, TC *str, W len)
+{
+       TC ch;
+       W iter;
+       UH code;
+
+       ch = str[len - 1];
+       iter = (len - 1) * 2;
+       if ((ch & 0xFE00) == 0xFE00) {
+               iter++;
+       }
+       iter--;
+       code = 0x00FF & ch;
+
+       if ((state->lang.iter == iter)&&(state->lang.code == code)) {
+               return True;
+       }
+       return False;
+}
+
+EXPORT VOID texteditor_characterstate_initialize(texteditor_characterstate_t *state)
+{
+       state->w_ratio = 0x0101;
+       state->lang.iter = 0;
+       state->lang.code = 0x21; // system script
+}
+
+EXPORT VOID texteditor_characterstate_finalize(texteditor_characterstate_t *state)
+{
+}
diff --git a/src/control/texteditor_characterstate.h b/src/control/texteditor_characterstate.h
new file mode 100644 (file)
index 0000000..e242a88
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * texteditor_characterstate.h
+ *
+ * Copyright (c) 2013 project bchan
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ *    distribution.
+ *
+ */
+
+#include    <basic.h>
+
+#include       <tad/tadfragment.h>
+
+#ifndef __TEXTEDITOR_CHARACTERSTATE_H__
+#define __TEXTEDITOR_CHARACTERSTATE_H__
+
+/* Functionality name: texteditor */
+/* Detail name: characterstate */
+struct texteditor_characterstate_t_ {
+       RATIO w_ratio;
+       struct {
+               UW iter;
+               UB code;
+       } lang;
+};
+typedef struct texteditor_characterstate_t_ texteditor_characterstate_t;
+
+IMPORT VOID texteditor_characterstate_initialize(texteditor_characterstate_t *state);
+IMPORT VOID texteditor_characterstate_finalize(texteditor_characterstate_t *state);
+IMPORT W texteditor_charactorstate_input(texteditor_characterstate_t *state, tadfragment_cursor_segment *segment);
+IMPORT Bool texteditor_characterstate_ishankaku(texteditor_characterstate_t *state);
+IMPORT W texteditor_characterstate_getlang(texteditor_characterstate_t *state, TC *str, W len);
+IMPORT Bool texteditor_characterstate_islang(texteditor_characterstate_t *state, TC *str, W len);
+
+#endif