Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
12 KiB

  1. /****************************************************************************
  2. HAUTO.CPP
  3. Owner: cslim
  4. Copyright (c) 1997-1999 Microsoft Corporation
  5. Hangul composition state machine class
  6. History:
  7. 14-JUL-1999 cslim Copied from IME98 source tree
  8. *****************************************************************************/
  9. #if !defined (__HAUTOMATA_H__)
  10. #define __HAUTOMATA_H__
  11. #include "debug.h"
  12. // Number of each component
  13. #define NUM_OF_CHOSUNG 19
  14. #define NUM_OF_JUNGSUNG 21
  15. #define NUM_OF_JONGSUNG 28
  16. #define NUM_OF_DOUBLE_CHOSUNG 5
  17. #define NUM_OF_DOUBLE_JUNGSUNG 7
  18. #define NUM_OF_DOUBLE_JONGSUNG_2BEOL 11
  19. #define NUM_OF_DOUBLE_JONGSUNG_3BEOL 13
  20. #define UNICODE_HANGUL_BASE 0xAC00
  21. #define UNICODE_HANGUL_END 0xD7A3
  22. #define UNICODE_HANGUL_COMP_JAMO_START 0x3131
  23. #define UNICODE_HANGUL_COMP_JAMO_VOWEL_START 0x314F
  24. #define UNICODE_HANGUL_COMP_JAMO_END 0x3163
  25. #define UNICODE_HANGUL_COMP_JAMO_START_FILL 0x3130
  26. #define UNICODE_HANGUL_COMP_JAMO_SIOT 0x3145
  27. ///////////////////////////////////////////
  28. // HANGUL Jaso inernal difinitions
  29. // ChoSung
  30. #define _KIYEOK_ 1 // '��'
  31. #define _SSANGKIYEOK_ 2 // '��'
  32. #define _NIEUN_ 3 // '��'
  33. #define _TIKEUT_ 4 // '��'
  34. #define _SSANGTIKEUT_ 5 // '��'
  35. #define _RIEUL_ 6 // '��'
  36. #define _MIEUM_ 7 // '��'
  37. #define _PIEUP_ 8 // '��'
  38. #define _SSANGPIEUP_ 9 // '��'
  39. #define _SIOS_ 10 // '��'
  40. #define _SSANGSIOS_ 11 // '��'
  41. #define _IEUNG_ 12 // '��'
  42. #define _CIEUC_ 13 // '��'
  43. #define _SSANGCIEUC_ 14 // '��'
  44. #define _CHIEUCH_ 15 // '��'
  45. #define _KHIEUKH_ 16 // '��'
  46. #define _THIEUTH_ 17 // '��'
  47. #define _PHIEUPH_ 18 // '��'
  48. #define _HIEUH_ 19 // '��'
  49. // JungSung
  50. #define _A_ 1 // '��'
  51. #define _AE_ 2 // '��'
  52. #define _YA_ 3 // '��'
  53. #define _YAE_ 4 // '��'
  54. #define _EO_ 5 // '��'
  55. #define _E_ 6 // '��'
  56. #define _YEO_ 7 // '��'
  57. #define _YE_ 8 // '��'
  58. #define _O_ 9 // '��'
  59. #define _WA_ 10 // '��'
  60. #define _WAE_ 11 // '��'
  61. #define _OE_ 12 // '��'
  62. #define _YO_ 13 // '��'
  63. #define _U_ 14 // '��'
  64. #define _WEO_ 15 // '��'
  65. #define _WE_ 16 // '��'
  66. #define _WI_ 17 // '��'
  67. #define _YU_ 18 // '��'
  68. #define _EU_ 19 // '��'
  69. #define _YI_ 20 // '��'
  70. #define _I_ 21 // '��'
  71. ////////////////////////////////////////
  72. // JongSung
  73. #define _JONG_KIYEOK_ 1 // '��'
  74. #define _JONG_SSANGKIYEOK_ 2 // '��'
  75. #define _JONG_KIYEOK_SIOS 3
  76. #define _JONG_NIEUN_ 4 // '��'
  77. #define _JONG_NIEUN_CHIEUCH_ 5 // '��'
  78. #define _JONG_NIEUN_HIEUH_ 6 // '��'
  79. #define _JONG_TIKEUT_ 7 // '��'
  80. #define _JONG_RIEUL_ 8 // '��'
  81. #define _JONG_RIEUL_KIYEOK_ 9 // '��'
  82. #define _JONG_RIEUL_MIUM_ 10 // '��'
  83. #define _JONG_RIEUL_PIEUP_ 11 // '��'
  84. #define _JONG_RIEUL_SIOS_ 12 // '��'
  85. #define _JONG_RIEUL_THIEUTH_ 13 // '��'
  86. #define _JONG_RIEUL_PHIEUPH_ 14 // '��'
  87. #define _JONG_RIEUL_HIEUH_ 15 // '��'
  88. #define _JONG_MIEUM_ 16 // '��'
  89. #define _JONG_PIEUP_ 17 // '��'
  90. #define _JONG_PIEUP_SIOS 18 // '��'
  91. #define _JONG_SIOS_ 19 // '��'
  92. #define _JONG_SSANGSIOS_ 20 // '��'
  93. #define _JONG_IEUNG_ 21 // '��'
  94. #define _JONG_CIEUC_ 22 // '��'
  95. #define _JONG_CHIEUCH_ 23 // '��'
  96. #define _JONG_KHIEUKH_ 24 // '��'
  97. #define _JONG_THIEUTH_ 25 // '��'
  98. #define _JONG_PHIEUPH_ 26 // '��'
  99. #define _JONG_HIEUH_ 27 // '��'
  100. //
  101. const int MaxInterimStackSize = 6; // Maximum stack size is 6.
  102. // At most 6 key input
  103. // to complete one Hangul Char.
  104. // ex) ��(3 beolsik)
  105. enum HAutomataReturnState
  106. {
  107. HAUTO_NONHANGULKEY,
  108. HAUTO_COMPOSITION, // Hagul still in interim state.
  109. HAUTO_COMPLETE, // One hangul char completed and have chars
  110. // will takeover as next input.
  111. HAUTO_IMPOSSIBLE
  112. };
  113. const WORD H_HANGUL = 0x8000;
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CHangulAutomata Abstract Class
  116. //
  117. class CHangulAutomata
  118. {
  119. public:
  120. CHangulAutomata() { InitState(); }
  121. // Attributes
  122. public:
  123. // Operations
  124. public:
  125. void InitState()
  126. {
  127. m_CurState = m_NextState = 0;
  128. m_wInternalCode = 0;
  129. m_Chosung = m_Jungsung = m_Jongsung = 0;
  130. m_wcComposition = m_wcComplete = L'\0';
  131. InterimStack.Init();
  132. }
  133. virtual HAutomataReturnState Machine(UINT KeyCode, int iShift) = 0;
  134. virtual BOOL IsInputKey(UINT KeyCode, int iShift) = 0;
  135. virtual BOOL IsHangulKey(UINT KeyCode, int iShift) = 0;
  136. virtual WORD GetKeyMap(UINT KeyCode, int iShift) = 0;
  137. virtual BOOL SetCompositionChar(WCHAR wcComp) = 0;
  138. static WORD GetEnglishKeyMap(UINT KeyCode, int iShift) { return bETable[KeyCode][iShift]; }
  139. BOOL BackSpace();
  140. BOOL MakeComplete();
  141. WCHAR GetCompositionChar() { return m_wcComposition; }
  142. WCHAR GetCompleteChar() { return m_wcComplete; }
  143. // Implementation
  144. public:
  145. virtual ~CHangulAutomata() {}
  146. protected:
  147. void MakeComposition();
  148. BOOL MakeComplete(WORD wcComplete);
  149. WORD FindChosungComb(WORD wPrevCode);
  150. WORD FindJunsungComb(WORD wPrevCode);
  151. virtual WORD FindJonsungComb(WORD wPrevCode) = 0;
  152. void SeparateDJung(LPWORD pJungSung);
  153. void SeparateDJong(LPWORD pJongSung);
  154. virtual HAutomataReturnState Input(WORD InternalCode) = 0;
  155. //
  156. struct InterimStackEntry
  157. {
  158. WORD m_wInternalCode;
  159. WORD m_CurState;
  160. WORD m_Chosung, m_Jungsung, m_Jongsung;
  161. WCHAR m_wcCode;
  162. };
  163. ///////////////////////////////////////////////////////////////////////////
  164. //
  165. class CInterimStack
  166. {
  167. protected:
  168. InterimStackEntry m_StackBuffer[MaxInterimStackSize];
  169. int m_sp; // Stack pointer
  170. public:
  171. CInterimStack() { m_sp = 0; }
  172. ~CInterimStack() {}
  173. void Init() { m_sp = 0; }
  174. void Push(InterimStackEntry& InterimEntry);
  175. void Push(WORD wInternalCode, WORD CurState,
  176. WORD Chosung, WORD Jungsung, WORD Jongsung, WCHAR wcCode);
  177. InterimStackEntry* CInterimStack::Pop()
  178. {
  179. DbgAssert(m_sp > 0);
  180. return &m_StackBuffer[--m_sp];
  181. }
  182. InterimStackEntry* CInterimStack::GetTop()
  183. {
  184. DbgAssert(m_sp > 0);
  185. return &m_StackBuffer[m_sp-1];
  186. }
  187. BOOL IsEmpty() { return m_sp == 0; }
  188. };
  189. ///////////////////////////////////////////////////////////////////////////
  190. CInterimStack InterimStack;
  191. protected:
  192. WORD m_CurState, m_NextState;
  193. WORD m_wInternalCode, m_Chosung, m_Jungsung, m_Jongsung;
  194. WCHAR m_wcComposition;
  195. WCHAR m_wcComplete;
  196. //
  197. const static BYTE bETable[256][2];
  198. const static BYTE Cho2Jong[NUM_OF_CHOSUNG+1];
  199. const static BYTE Jong2Cho[NUM_OF_JONGSUNG];
  200. };
  201. /////////////////////////////////////////////////////////////////////////////
  202. // CHangulAutomata2 Keyboard layout #1 (2 beolsik)
  203. //
  204. class CHangulAutomata2 : public CHangulAutomata
  205. {
  206. public:
  207. CHangulAutomata2() { }
  208. // Attributes
  209. public:
  210. // Operations
  211. public:
  212. HAutomataReturnState Machine(UINT KeyCode, int iShift);
  213. BOOL IsInputKey(UINT KeyCode, int iShift);
  214. BOOL IsHangulKey(UINT KeyCode, int iShift);
  215. WORD GetKeyMap(UINT KeyCode, int iShift);
  216. BOOL SetCompositionChar(WCHAR wcComp);
  217. // Implementation
  218. public:
  219. ~CHangulAutomata2() { }
  220. protected:
  221. WORD FindJonsungComb(WORD wPrevCode);
  222. HAutomataReturnState Input(WORD InternalCode);
  223. protected:
  224. // This enum should be matched with m_NextState
  225. // DO NOT change without changing _Transistion_state !
  226. enum _Transistion_state { FINAL=8, TAKEOVER=9, FIND=10 };
  227. static const WORD m_NextStateTbl[8][5];
  228. static const WORD H_CONSONANT, H_VOWEL, H_DOUBLE, H_ONLYCHO;
  229. static WORD wHTable[256][2];
  230. static BYTE rgbDJongTbl[NUM_OF_DOUBLE_JONGSUNG_2BEOL+1][3];
  231. };
  232. /////////////////////////////////////////////////////////////////////////////
  233. // CHangulAutomata3 Keyboard layout #2 (3 beolsik)
  234. class CHangulAutomata3 : public CHangulAutomata
  235. {
  236. public:
  237. CHangulAutomata3() {}
  238. // Attributes
  239. public:
  240. // Operations
  241. public:
  242. HAutomataReturnState Machine(UINT KeyCode, int iShift);
  243. BOOL IsInputKey(UINT KeyCode, int iShift);
  244. BOOL IsHangulKey(UINT KeyCode, int iShift);
  245. WORD GetKeyMap(UINT KeyCode, int iShift);
  246. BOOL SetCompositionChar(WCHAR wcComp);
  247. // Implementation
  248. public:
  249. ~CHangulAutomata3() { }
  250. protected:
  251. WORD FindJonsungComb(WORD wPrevCode);
  252. HAutomataReturnState Input(WORD InternalCode);
  253. protected:
  254. enum _Transistion_state { FINAL=11, FIND=12 };
  255. static const WORD m_NextStateTbl[11][6];
  256. static const WORD H_CHOSUNG, H_JUNGSUNG, H_JONGSUNG, H_DOUBLE;
  257. static WORD wHTable[256][2];
  258. static BYTE rgbDJongTbl[NUM_OF_DOUBLE_JONGSUNG_3BEOL+1][3];
  259. };
  260. /////////////////////////////////////////////////////////////////////////////
  261. // CHangulAutomata3 Keyboard layout #3 (3 beolsik final)
  262. class CHangulAutomata3Final : public CHangulAutomata3
  263. {
  264. public:
  265. CHangulAutomata3Final() {}
  266. // Attributes
  267. public:
  268. // Operations
  269. public:
  270. HAutomataReturnState Machine(UINT KeyCode, int iShift) ;
  271. BOOL IsInputKey(UINT KeyCode, int iShift);
  272. BOOL IsHangulKey(UINT KeyCode, int iShift);
  273. WORD GetKeyMap(UINT KeyCode, int iShift);
  274. // Implementation
  275. public:
  276. ~CHangulAutomata3Final() { }
  277. protected:
  278. static WORD wHTable[256][2];
  279. };
  280. ///////////////////////////////////////////////////////////////////////////////
  281. // Inline functions
  282. inline
  283. void CHangulAutomata::CInterimStack::Push(InterimStackEntry& InterimEntry)
  284. {
  285. DbgAssert(m_sp<MaxInterimStackSize);
  286. m_StackBuffer[m_sp++] = InterimEntry;
  287. }
  288. inline
  289. void CHangulAutomata::CInterimStack::Push(WORD wInternalCode, WORD CurState,
  290. WORD Chosung, WORD Jungsung, WORD Jongsung, WCHAR wcCode)
  291. {
  292. DbgAssert(m_sp<=5);
  293. m_StackBuffer[m_sp].m_wInternalCode = wInternalCode;
  294. m_StackBuffer[m_sp].m_CurState = CurState;
  295. m_StackBuffer[m_sp].m_Chosung = Chosung;
  296. m_StackBuffer[m_sp].m_Jungsung = Jungsung;
  297. m_StackBuffer[m_sp].m_Jongsung = Jongsung;
  298. m_StackBuffer[m_sp++].m_wcCode = wcCode;
  299. }
  300. // CHangulAutomata2
  301. inline
  302. HAutomataReturnState CHangulAutomata2::Machine(UINT KeyCode, int iShift)
  303. {
  304. return (Input(wHTable[KeyCode][iShift]));
  305. }
  306. inline
  307. BOOL CHangulAutomata2::IsInputKey(UINT KeyCode, int iShift)
  308. {
  309. if (KeyCode<256)
  310. return (wHTable[KeyCode][iShift]);
  311. else
  312. return fFalse;
  313. }
  314. inline
  315. BOOL CHangulAutomata2::IsHangulKey(UINT KeyCode, int iShift)
  316. {
  317. if (KeyCode<256)
  318. return (wHTable[KeyCode][iShift]) & H_HANGUL;
  319. else
  320. return fFalse;
  321. }
  322. inline
  323. WORD CHangulAutomata2::GetKeyMap(UINT KeyCode, int iShift)
  324. {
  325. if (KeyCode<256)
  326. return (wHTable[KeyCode][iShift]);
  327. else
  328. return fFalse;
  329. }
  330. // CHangulAutomata3
  331. inline
  332. HAutomataReturnState CHangulAutomata3::Machine(UINT KeyCode, int iShift)
  333. {
  334. DbgAssert(KeyCode<256);
  335. return (Input(wHTable[KeyCode][iShift]));
  336. }
  337. inline
  338. BOOL CHangulAutomata3::IsInputKey(UINT KeyCode, int iShift)
  339. {
  340. if (KeyCode<256)
  341. return (wHTable[KeyCode][iShift]);
  342. else
  343. return fFalse;
  344. }
  345. inline
  346. BOOL CHangulAutomata3::IsHangulKey(UINT KeyCode, int iShift)
  347. {
  348. if (KeyCode<256)
  349. return (wHTable[KeyCode][iShift]) & H_HANGUL;
  350. else
  351. return fFalse;
  352. }
  353. inline
  354. WORD CHangulAutomata3::GetKeyMap(UINT KeyCode, int iShift)
  355. {
  356. if (KeyCode<256)
  357. return (wHTable[KeyCode][iShift]);
  358. else
  359. return 0;
  360. }
  361. // CHangulAutomata3Final
  362. inline
  363. HAutomataReturnState CHangulAutomata3Final::Machine(UINT KeyCode, int iShift)
  364. {
  365. DbgAssert(KeyCode<256);
  366. return (Input(wHTable[KeyCode][iShift]));
  367. }
  368. inline
  369. BOOL CHangulAutomata3Final::IsInputKey(UINT KeyCode, int iShift)
  370. {
  371. if (KeyCode<256)
  372. return (wHTable[KeyCode][iShift]);
  373. else
  374. return fFalse;
  375. }
  376. inline
  377. BOOL CHangulAutomata3Final::IsHangulKey(UINT KeyCode, int iShift)
  378. {
  379. if (KeyCode<256)
  380. return (wHTable[KeyCode][iShift]) & H_HANGUL;
  381. else
  382. return fFalse;
  383. }
  384. inline
  385. WORD CHangulAutomata3Final::GetKeyMap(UINT KeyCode, int iShift)
  386. {
  387. if (KeyCode<256)
  388. return (wHTable[KeyCode][iShift]);
  389. else
  390. return 0;
  391. }
  392. #endif // !defined (__HAUTOMATA_H__)