PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/game-ui-solution/CEGUI-0.7.5/cegui/src/minibidi.cpp

http://game-ui-solution.googlecode.com/
C++ | 1987 lines | 1677 code | 86 blank | 224 comment | 266 complexity | 9638bac966002e582237737cc2f0cb7e MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, ISC, Unlicense, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, MIT, LGPL-3.0
  1. /************************************************************************
  2. * $Id$
  3. *
  4. * ------------
  5. * Description:
  6. * ------------
  7. * This is an implemention of Unicode's Bidirectional Algorithm
  8. * (known as UAX #9).
  9. *
  10. * http://www.unicode.org/reports/tr9/
  11. *
  12. * Author: Ahmad Khalifa
  13. *
  14. * -----------------
  15. * Revision Details: (Updated by Revision Control System)
  16. * -----------------
  17. * $Date$
  18. * $Author$
  19. * $Revision$
  20. * $Source$
  21. *
  22. * (www.arabeyes.org - under MIT license)
  23. *
  24. ************************************************************************/
  25. #include <malloc.h> /* malloc() and free() definition */
  26. #ifdef _LINUX
  27. #include <wchar.h>
  28. #endif
  29. /*
  30. * Datatype Extension Macros
  31. */
  32. #define BLOCKTYPE unsigned int*
  33. #define CHARTYPE unsigned int
  34. #define GETCHAR(from,x) from[x]
  35. #define GetType(x) getType(x)
  36. /*=====TESTING mode===========*/
  37. //#define GetType(x) getCAPRtl(x)
  38. #define lenof(x) sizeof(x) / sizeof(x[0])
  39. #define MAX_STACK 60
  40. /* character types */
  41. enum
  42. {
  43. /* Strong Char Types */
  44. L, /* Left-to-Right char */
  45. LRE, /* Left-to-Right Embedding */
  46. LRO, /* Left-to-Right Override */
  47. R, /* Right-to-Left char */
  48. AL, /* Right-to-Left Arabic char */
  49. RLE, /* Right-to-Left Embedding */
  50. RLO, /* Right-to-Left Override */
  51. /* Weak Char Types */
  52. PDF, /* Pop Directional Format */
  53. EN, /* European Number */
  54. ES, /* European Number Separator */
  55. ET, /* European Number Terminator */
  56. AN, /* Arabic Number */
  57. CS, /* Common Number Separator */
  58. NSM, /* Non Spacing Mark */
  59. BN, /* Boundary Neutral */
  60. /* Neutral Char Types */
  61. B, /* Paragraph Separator */
  62. S, /* Segment Separator */
  63. WS, /* Whitespace */
  64. ON, /* Other Neutrals */
  65. };
  66. /* Shaping Types */
  67. enum
  68. {
  69. SL, /* Left-Joining, doesnt exist in U+0600 - U+06FF */
  70. SR, /* Right-Joining, ie has Isolated, Final */
  71. SD, /* Dual-Joining, ie has Isolated, Final, Initial, Medial */
  72. SU, /* Non-Joining */
  73. SC /* Join-Causing, like U+0640 (TATWEEL) */
  74. };
  75. #define odd(x) (x%2)
  76. /* Returns the first odd/even value greater than x */
  77. #define leastGreaterOdd(x) odd(x) ? (x+2) : (x+1)
  78. #define leastGreaterEven(x) odd(x) ? (x+1) : (x+2)
  79. /* Shaping Helpers */
  80. #define STYPE(xh) ((xh >= SHAPE_FIRST) && (xh <= SHAPE_LAST)) ? \
  81. shapetypes[xh-SHAPE_FIRST].type : SU
  82. #define SISOLATED(xh) shapetypes[xh-SHAPE_FIRST].form_b
  83. #define SFINAL(xh) xh+1
  84. #define SINITIAL(xh) xh+2
  85. #define SMEDIAL(xh) xh+3
  86. /* function declarations */
  87. int findIndexOfRun(unsigned char* level , int start, int count, int tlevel);
  88. unsigned char getType(CHARTYPE ch);
  89. unsigned char getCAPRtl(CHARTYPE ch);
  90. void doMirror(CHARTYPE* ch);
  91. typedef struct{
  92. unsigned char type;
  93. unsigned short form_b;
  94. } shape_node;
  95. /* Kept near the actual table, for verification. */
  96. #define SHAPE_FIRST 0x621
  97. #define SHAPE_LAST 0x64A
  98. /* very bad Memory alignment for 32-bit machines
  99. * could split it to 2 arrays or promote type to 2 bytes type
  100. */
  101. shape_node shapetypes[] = {
  102. /* index, Typ, Iso */
  103. /* 621 */ {SU, 0xFE80},
  104. /* 622 */ {SR, 0xFE81},
  105. /* 623 */ {SR, 0xFE83},
  106. /* 624 */ {SR, 0xFE85},
  107. /* 625 */ {SR, 0xFE87},
  108. /* 626 */ {SD, 0xFE89},
  109. /* 627 */ {SR, 0xFE8D},
  110. /* 628 */ {SD, 0xFE8F},
  111. /* 629 */ {SR, 0xFE93},
  112. /* 62A */ {SD, 0xFE95},
  113. /* 62B */ {SD, 0xFE99},
  114. /* 62C */ {SD, 0xFE9D},
  115. /* 62D */ {SD, 0xFEA1},
  116. /* 62E */ {SD, 0xFEA5},
  117. /* 62F */ {SR, 0xFEA9},
  118. /* 630 */ {SR, 0xFEAB},
  119. /* 631 */ {SR, 0xFEAD},
  120. /* 632 */ {SR, 0xFEAF},
  121. /* 633 */ {SD, 0xFEB1},
  122. /* 634 */ {SD, 0xFEB5},
  123. /* 635 */ {SD, 0xFEB9},
  124. /* 636 */ {SD, 0xFEBD},
  125. /* 637 */ {SD, 0xFEC1},
  126. /* 638 */ {SD, 0xFEC5},
  127. /* 639 */ {SD, 0xFEC9},
  128. /* 63A */ {SD, 0xFECD},
  129. /* 63B */ {SU, 0x0},
  130. /* 63C */ {SU, 0x0},
  131. /* 63D */ {SU, 0x0},
  132. /* 63E */ {SU, 0x0},
  133. /* 63F */ {SU, 0x0},
  134. /* 640 */ {SC, 0x0},
  135. /* 641 */ {SD, 0xFED1},
  136. /* 642 */ {SD, 0xFED5},
  137. /* 643 */ {SD, 0xFED9},
  138. /* 644 */ {SD, 0xFEDD},
  139. /* 645 */ {SD, 0xFEE1},
  140. /* 646 */ {SD, 0xFEE5},
  141. /* 647 */ {SD, 0xFEE9},
  142. /* 648 */ {SR, 0xFEED},
  143. /* 649 */ {SR, 0xFEEF}, /* SD */
  144. /* 64A */ {SD, 0xFEF1}
  145. };
  146. /*
  147. * Flips the text buffer, according to max level, and
  148. * all higher levels
  149. *
  150. * Input:
  151. * from: text buffer, on which to apply flipping
  152. * level: resolved levels buffer
  153. * max: the maximum level found in this line (should be unsigned char)
  154. * count: line size in wchar_t
  155. */
  156. void flipThisRun(BLOCKTYPE from, unsigned char* level, int max, int count, int * v2l)
  157. {
  158. int i, j, rcount, tlevel, tempInt;
  159. CHARTYPE temp;
  160. j = i = 0;
  161. while(i<count && j<count)
  162. {
  163. /* find the start of the run of level=max */
  164. tlevel = max;
  165. i = j = findIndexOfRun(level, i, count, max);
  166. /* find the end of the run */
  167. while((tlevel <= level[i]) && (i < count))
  168. {
  169. i++;
  170. }
  171. rcount = i-j;
  172. for(; rcount>((i-j)/2); rcount--)
  173. {
  174. temp = GETCHAR(from, j+rcount-1);
  175. GETCHAR(from, j+rcount-1) = GETCHAR(from, i-rcount);
  176. GETCHAR(from, i-rcount) = temp;
  177. if(v2l)
  178. {
  179. tempInt = v2l[j+rcount-1];
  180. v2l[j+rcount-1] = v2l[i-rcount];
  181. v2l[i-rcount] = tempInt;
  182. }
  183. }
  184. }
  185. }
  186. /*
  187. * Finds the index of a run with level equals tlevel
  188. */
  189. int findIndexOfRun(unsigned char* level , int start, int count, int tlevel)
  190. {
  191. int i;
  192. for(i=start; i<count; i++)
  193. {
  194. if(tlevel <= level[i])
  195. {
  196. return i;
  197. }
  198. }
  199. return count;
  200. }
  201. unsigned char GetParagraphLevel(BLOCKTYPE line, int count)
  202. {
  203. int i;
  204. for( i=0; i<count ; i++)
  205. {
  206. if(GetType(GETCHAR(line, i)) == R || GetType(/*line[i]*/ GETCHAR(line, i)) == AL)
  207. return 1;
  208. else if(GetType(GETCHAR(line, i)) == L)
  209. return 0;
  210. }
  211. return 0; /* Compiler Nag-Stopper */
  212. }
  213. /*
  214. * Returns character type of ch, by calling RLE table lookup
  215. * function
  216. */
  217. unsigned char getCAPRtl(CHARTYPE ch)
  218. {
  219. /* CAPRtl Charset */
  220. int TypesFromChar[] =
  221. {
  222. //0 1 2 3 4 5 6 7 8 9 a b c d e f
  223. ON, ON, ON, ON, L, R, ON, ON, ON, ON, ON, ON, ON, B, RLO,RLE, /*00-0f*/
  224. LRO,LRE,PDF,WS, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, ON, /*10-1f*/
  225. WS, ON, ON, ON, ET, ON, ON, ON, ON, ON, ON, ET, CS, ON, ES, ES, /*20-2f*/
  226. EN, EN, EN, EN, EN, EN, AN, AN, AN, AN, LRE, RLE, RLO, PDF, LRO, ON, /*30-3f*/
  227. // EN, EN, EN, EN, EN, EN, AN, AN, AN, AN, CS, ON, ON, ON, ON, ON, /*30-3f*/
  228. R, AL, AL, AL, AL, AL, AL, R, R, R, R, R, R, R, R, R, /*40-4f*/
  229. R, R, R, R, R, R, R, R, R, R, R, ON, B, ON, ON, ON, /*50-5f*/
  230. NSM, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, /*60-6f*/
  231. L, L, L, L, L, L, L, L, L, L, L, ON, S, ON, ON, ON, /*70-7f*/
  232. };
  233. if(ch <0x7f)
  234. return TypesFromChar[ch];
  235. else
  236. return R;
  237. }
  238. unsigned char getType(CHARTYPE ch)
  239. {
  240. static const struct {
  241. CHARTYPE first, last, type;
  242. } lookup[] = {
  243. {0x0000, 0x0008, BN},
  244. {0x0009, 0x0009, S},
  245. {0x000a, 0x000a, B},
  246. {0x000b, 0x000b, S},
  247. {0x000c, 0x000c, WS},
  248. {0x000d, 0x000d, B},
  249. {0x000e, 0x001b, BN},
  250. {0x001c, 0x001e, B},
  251. {0x001f, 0x001f, S},
  252. {0x0020, 0x0020, WS},
  253. {0x0023, 0x0025, ET},
  254. {0x002b, 0x002b, ES},
  255. {0x002c, 0x002c, CS},
  256. {0x002d, 0x002d, ES},
  257. {0x002e, 0x002f, CS},
  258. {0x0030, 0x0039, EN},
  259. {0x003a, 0x003a, CS},
  260. {0x0041, 0x005a, L},
  261. {0x0061, 0x007a, L},
  262. {0x007f, 0x0084, BN},
  263. {0x0085, 0x0085, B},
  264. {0x0086, 0x009f, BN},
  265. {0x00a0, 0x00a0, CS},
  266. {0x00a2, 0x00a5, ET},
  267. {0x00aa, 0x00aa, L},
  268. {0x00ad, 0x00ad, BN},
  269. {0x00b0, 0x00b1, ET},
  270. {0x00b2, 0x00b3, EN},
  271. {0x00b5, 0x00b5, L},
  272. {0x00b9, 0x00b9, EN},
  273. {0x00ba, 0x00ba, L},
  274. {0x00c0, 0x00d6, L},
  275. {0x00d8, 0x00f6, L},
  276. {0x00f8, 0x0236, L},
  277. {0x0250, 0x02b8, L},
  278. {0x02bb, 0x02c1, L},
  279. {0x02d0, 0x02d1, L},
  280. {0x02e0, 0x02e4, L},
  281. {0x02ee, 0x02ee, L},
  282. {0x0300, 0x0357, NSM},
  283. {0x035d, 0x036f, NSM},
  284. {0x037a, 0x037a, L},
  285. {0x0386, 0x0386, L},
  286. {0x0388, 0x038a, L},
  287. {0x038c, 0x038c, L},
  288. {0x038e, 0x03a1, L},
  289. {0x03a3, 0x03ce, L},
  290. {0x03d0, 0x03f5, L},
  291. {0x03f7, 0x03fb, L},
  292. {0x0400, 0x0482, L},
  293. {0x0483, 0x0486, NSM},
  294. {0x0488, 0x0489, NSM},
  295. {0x048a, 0x04ce, L},
  296. {0x04d0, 0x04f5, L},
  297. {0x04f8, 0x04f9, L},
  298. {0x0500, 0x050f, L},
  299. {0x0531, 0x0556, L},
  300. {0x0559, 0x055f, L},
  301. {0x0561, 0x0587, L},
  302. {0x0589, 0x0589, L},
  303. {0x0591, 0x05a1, NSM},
  304. {0x05a3, 0x05b9, NSM},
  305. {0x05bb, 0x05bd, NSM},
  306. {0x05be, 0x05be, R},
  307. {0x05bf, 0x05bf, NSM},
  308. {0x05c0, 0x05c0, R},
  309. {0x05c1, 0x05c2, NSM},
  310. {0x05c3, 0x05c3, R},
  311. {0x05c4, 0x05c4, NSM},
  312. {0x05d0, 0x05ea, R},
  313. {0x05f0, 0x05f4, R},
  314. {0x0600, 0x0603, AL},
  315. {0x060c, 0x060c, CS},
  316. {0x060d, 0x060d, AL},
  317. {0x0610, 0x0615, NSM},
  318. {0x061b, 0x061b, AL},
  319. {0x061f, 0x061f, AL},
  320. {0x0621, 0x063a, AL},
  321. {0x0640, 0x064a, AL},
  322. {0x064b, 0x0658, NSM},
  323. {0x0660, 0x0669, AN},
  324. {0x066a, 0x066a, ET},
  325. {0x066b, 0x066c, AN},
  326. {0x066d, 0x066f, AL},
  327. {0x0670, 0x0670, NSM},
  328. {0x0671, 0x06d5, AL},
  329. {0x06d6, 0x06dc, NSM},
  330. {0x06dd, 0x06dd, AL},
  331. {0x06de, 0x06e4, NSM},
  332. {0x06e5, 0x06e6, AL},
  333. {0x06e7, 0x06e8, NSM},
  334. {0x06ea, 0x06ed, NSM},
  335. {0x06ee, 0x06ef, AL},
  336. {0x06f0, 0x06f9, EN},
  337. {0x06fa, 0x070d, AL},
  338. {0x070f, 0x070f, BN},
  339. {0x0710, 0x0710, AL},
  340. {0x0711, 0x0711, NSM},
  341. {0x0712, 0x072f, AL},
  342. {0x0730, 0x074a, NSM},
  343. {0x074d, 0x074f, AL},
  344. {0x0780, 0x07a5, AL},
  345. {0x07a6, 0x07b0, NSM},
  346. {0x07b1, 0x07b1, AL},
  347. {0x0901, 0x0902, NSM},
  348. {0x0903, 0x0939, L},
  349. {0x093c, 0x093c, NSM},
  350. {0x093d, 0x0940, L},
  351. {0x0941, 0x0948, NSM},
  352. {0x0949, 0x094c, L},
  353. {0x094d, 0x094d, NSM},
  354. {0x0950, 0x0950, L},
  355. {0x0951, 0x0954, NSM},
  356. {0x0958, 0x0961, L},
  357. {0x0962, 0x0963, NSM},
  358. {0x0964, 0x0970, L},
  359. {0x0981, 0x0981, NSM},
  360. {0x0982, 0x0983, L},
  361. {0x0985, 0x098c, L},
  362. {0x098f, 0x0990, L},
  363. {0x0993, 0x09a8, L},
  364. {0x09aa, 0x09b0, L},
  365. {0x09b2, 0x09b2, L},
  366. {0x09b6, 0x09b9, L},
  367. {0x09bc, 0x09bc, NSM},
  368. {0x09bd, 0x09c0, L},
  369. {0x09c1, 0x09c4, NSM},
  370. {0x09c7, 0x09c8, L},
  371. {0x09cb, 0x09cc, L},
  372. {0x09cd, 0x09cd, NSM},
  373. {0x09d7, 0x09d7, L},
  374. {0x09dc, 0x09dd, L},
  375. {0x09df, 0x09e1, L},
  376. {0x09e2, 0x09e3, NSM},
  377. {0x09e6, 0x09f1, L},
  378. {0x09f2, 0x09f3, ET},
  379. {0x09f4, 0x09fa, L},
  380. {0x0a01, 0x0a02, NSM},
  381. {0x0a03, 0x0a03, L},
  382. {0x0a05, 0x0a0a, L},
  383. {0x0a0f, 0x0a10, L},
  384. {0x0a13, 0x0a28, L},
  385. {0x0a2a, 0x0a30, L},
  386. {0x0a32, 0x0a33, L},
  387. {0x0a35, 0x0a36, L},
  388. {0x0a38, 0x0a39, L},
  389. {0x0a3c, 0x0a3c, NSM},
  390. {0x0a3e, 0x0a40, L},
  391. {0x0a41, 0x0a42, NSM},
  392. {0x0a47, 0x0a48, NSM},
  393. {0x0a4b, 0x0a4d, NSM},
  394. {0x0a59, 0x0a5c, L},
  395. {0x0a5e, 0x0a5e, L},
  396. {0x0a66, 0x0a6f, L},
  397. {0x0a70, 0x0a71, NSM},
  398. {0x0a72, 0x0a74, L},
  399. {0x0a81, 0x0a82, NSM},
  400. {0x0a83, 0x0a83, L},
  401. {0x0a85, 0x0a8d, L},
  402. {0x0a8f, 0x0a91, L},
  403. {0x0a93, 0x0aa8, L},
  404. {0x0aaa, 0x0ab0, L},
  405. {0x0ab2, 0x0ab3, L},
  406. {0x0ab5, 0x0ab9, L},
  407. {0x0abc, 0x0abc, NSM},
  408. {0x0abd, 0x0ac0, L},
  409. {0x0ac1, 0x0ac5, NSM},
  410. {0x0ac7, 0x0ac8, NSM},
  411. {0x0ac9, 0x0ac9, L},
  412. {0x0acb, 0x0acc, L},
  413. {0x0acd, 0x0acd, NSM},
  414. {0x0ad0, 0x0ad0, L},
  415. {0x0ae0, 0x0ae1, L},
  416. {0x0ae2, 0x0ae3, NSM},
  417. {0x0ae6, 0x0aef, L},
  418. {0x0af1, 0x0af1, ET},
  419. {0x0b01, 0x0b01, NSM},
  420. {0x0b02, 0x0b03, L},
  421. {0x0b05, 0x0b0c, L},
  422. {0x0b0f, 0x0b10, L},
  423. {0x0b13, 0x0b28, L},
  424. {0x0b2a, 0x0b30, L},
  425. {0x0b32, 0x0b33, L},
  426. {0x0b35, 0x0b39, L},
  427. {0x0b3c, 0x0b3c, NSM},
  428. {0x0b3d, 0x0b3e, L},
  429. {0x0b3f, 0x0b3f, NSM},
  430. {0x0b40, 0x0b40, L},
  431. {0x0b41, 0x0b43, NSM},
  432. {0x0b47, 0x0b48, L},
  433. {0x0b4b, 0x0b4c, L},
  434. {0x0b4d, 0x0b4d, NSM},
  435. {0x0b56, 0x0b56, NSM},
  436. {0x0b57, 0x0b57, L},
  437. {0x0b5c, 0x0b5d, L},
  438. {0x0b5f, 0x0b61, L},
  439. {0x0b66, 0x0b71, L},
  440. {0x0b82, 0x0b82, NSM},
  441. {0x0b83, 0x0b83, L},
  442. {0x0b85, 0x0b8a, L},
  443. {0x0b8e, 0x0b90, L},
  444. {0x0b92, 0x0b95, L},
  445. {0x0b99, 0x0b9a, L},
  446. {0x0b9c, 0x0b9c, L},
  447. {0x0b9e, 0x0b9f, L},
  448. {0x0ba3, 0x0ba4, L},
  449. {0x0ba8, 0x0baa, L},
  450. {0x0bae, 0x0bb5, L},
  451. {0x0bb7, 0x0bb9, L},
  452. {0x0bbe, 0x0bbf, L},
  453. {0x0bc0, 0x0bc0, NSM},
  454. {0x0bc1, 0x0bc2, L},
  455. {0x0bc6, 0x0bc8, L},
  456. {0x0bca, 0x0bcc, L},
  457. {0x0bcd, 0x0bcd, NSM},
  458. {0x0bd7, 0x0bd7, L},
  459. {0x0be7, 0x0bf2, L},
  460. {0x0bf9, 0x0bf9, ET},
  461. {0x0c01, 0x0c03, L},
  462. {0x0c05, 0x0c0c, L},
  463. {0x0c0e, 0x0c10, L},
  464. {0x0c12, 0x0c28, L},
  465. {0x0c2a, 0x0c33, L},
  466. {0x0c35, 0x0c39, L},
  467. {0x0c3e, 0x0c40, NSM},
  468. {0x0c41, 0x0c44, L},
  469. {0x0c46, 0x0c48, NSM},
  470. {0x0c4a, 0x0c4d, NSM},
  471. {0x0c55, 0x0c56, NSM},
  472. {0x0c60, 0x0c61, L},
  473. {0x0c66, 0x0c6f, L},
  474. {0x0c82, 0x0c83, L},
  475. {0x0c85, 0x0c8c, L},
  476. {0x0c8e, 0x0c90, L},
  477. {0x0c92, 0x0ca8, L},
  478. {0x0caa, 0x0cb3, L},
  479. {0x0cb5, 0x0cb9, L},
  480. {0x0cbc, 0x0cbc, NSM},
  481. {0x0cbd, 0x0cc4, L},
  482. {0x0cc6, 0x0cc8, L},
  483. {0x0cca, 0x0ccb, L},
  484. {0x0ccc, 0x0ccd, NSM},
  485. {0x0cd5, 0x0cd6, L},
  486. {0x0cde, 0x0cde, L},
  487. {0x0ce0, 0x0ce1, L},
  488. {0x0ce6, 0x0cef, L},
  489. {0x0d02, 0x0d03, L},
  490. {0x0d05, 0x0d0c, L},
  491. {0x0d0e, 0x0d10, L},
  492. {0x0d12, 0x0d28, L},
  493. {0x0d2a, 0x0d39, L},
  494. {0x0d3e, 0x0d40, L},
  495. {0x0d41, 0x0d43, NSM},
  496. {0x0d46, 0x0d48, L},
  497. {0x0d4a, 0x0d4c, L},
  498. {0x0d4d, 0x0d4d, NSM},
  499. {0x0d57, 0x0d57, L},
  500. {0x0d60, 0x0d61, L},
  501. {0x0d66, 0x0d6f, L},
  502. {0x0d82, 0x0d83, L},
  503. {0x0d85, 0x0d96, L},
  504. {0x0d9a, 0x0db1, L},
  505. {0x0db3, 0x0dbb, L},
  506. {0x0dbd, 0x0dbd, L},
  507. {0x0dc0, 0x0dc6, L},
  508. {0x0dca, 0x0dca, NSM},
  509. {0x0dcf, 0x0dd1, L},
  510. {0x0dd2, 0x0dd4, NSM},
  511. {0x0dd6, 0x0dd6, NSM},
  512. {0x0dd8, 0x0ddf, L},
  513. {0x0df2, 0x0df4, L},
  514. {0x0e01, 0x0e30, L},
  515. {0x0e31, 0x0e31, NSM},
  516. {0x0e32, 0x0e33, L},
  517. {0x0e34, 0x0e3a, NSM},
  518. {0x0e3f, 0x0e3f, ET},
  519. {0x0e40, 0x0e46, L},
  520. {0x0e47, 0x0e4e, NSM},
  521. {0x0e4f, 0x0e5b, L},
  522. {0x0e81, 0x0e82, L},
  523. {0x0e84, 0x0e84, L},
  524. {0x0e87, 0x0e88, L},
  525. {0x0e8a, 0x0e8a, L},
  526. {0x0e8d, 0x0e8d, L},
  527. {0x0e94, 0x0e97, L},
  528. {0x0e99, 0x0e9f, L},
  529. {0x0ea1, 0x0ea3, L},
  530. {0x0ea5, 0x0ea5, L},
  531. {0x0ea7, 0x0ea7, L},
  532. {0x0eaa, 0x0eab, L},
  533. {0x0ead, 0x0eb0, L},
  534. {0x0eb1, 0x0eb1, NSM},
  535. {0x0eb2, 0x0eb3, L},
  536. {0x0eb4, 0x0eb9, NSM},
  537. {0x0ebb, 0x0ebc, NSM},
  538. {0x0ebd, 0x0ebd, L},
  539. {0x0ec0, 0x0ec4, L},
  540. {0x0ec6, 0x0ec6, L},
  541. {0x0ec8, 0x0ecd, NSM},
  542. {0x0ed0, 0x0ed9, L},
  543. {0x0edc, 0x0edd, L},
  544. {0x0f00, 0x0f17, L},
  545. {0x0f18, 0x0f19, NSM},
  546. {0x0f1a, 0x0f34, L},
  547. {0x0f35, 0x0f35, NSM},
  548. {0x0f36, 0x0f36, L},
  549. {0x0f37, 0x0f37, NSM},
  550. {0x0f38, 0x0f38, L},
  551. {0x0f39, 0x0f39, NSM},
  552. {0x0f3e, 0x0f47, L},
  553. {0x0f49, 0x0f6a, L},
  554. {0x0f71, 0x0f7e, NSM},
  555. {0x0f7f, 0x0f7f, L},
  556. {0x0f80, 0x0f84, NSM},
  557. {0x0f85, 0x0f85, L},
  558. {0x0f86, 0x0f87, NSM},
  559. {0x0f88, 0x0f8b, L},
  560. {0x0f90, 0x0f97, NSM},
  561. {0x0f99, 0x0fbc, NSM},
  562. {0x0fbe, 0x0fc5, L},
  563. {0x0fc6, 0x0fc6, NSM},
  564. {0x0fc7, 0x0fcc, L},
  565. {0x0fcf, 0x0fcf, L},
  566. {0x1000, 0x1021, L},
  567. {0x1023, 0x1027, L},
  568. {0x1029, 0x102a, L},
  569. {0x102c, 0x102c, L},
  570. {0x102d, 0x1030, NSM},
  571. {0x1031, 0x1031, L},
  572. {0x1032, 0x1032, NSM},
  573. {0x1036, 0x1037, NSM},
  574. {0x1038, 0x1038, L},
  575. {0x1039, 0x1039, NSM},
  576. {0x1040, 0x1057, L},
  577. {0x1058, 0x1059, NSM},
  578. {0x10a0, 0x10c5, L},
  579. {0x10d0, 0x10f8, L},
  580. {0x10fb, 0x10fb, L},
  581. {0x1100, 0x1159, L},
  582. {0x115f, 0x11a2, L},
  583. {0x11a8, 0x11f9, L},
  584. {0x1200, 0x1206, L},
  585. {0x1208, 0x1246, L},
  586. {0x1248, 0x1248, L},
  587. {0x124a, 0x124d, L},
  588. {0x1250, 0x1256, L},
  589. {0x1258, 0x1258, L},
  590. {0x125a, 0x125d, L},
  591. {0x1260, 0x1286, L},
  592. {0x1288, 0x1288, L},
  593. {0x128a, 0x128d, L},
  594. {0x1290, 0x12ae, L},
  595. {0x12b0, 0x12b0, L},
  596. {0x12b2, 0x12b5, L},
  597. {0x12b8, 0x12be, L},
  598. {0x12c0, 0x12c0, L},
  599. {0x12c2, 0x12c5, L},
  600. {0x12c8, 0x12ce, L},
  601. {0x12d0, 0x12d6, L},
  602. {0x12d8, 0x12ee, L},
  603. {0x12f0, 0x130e, L},
  604. {0x1310, 0x1310, L},
  605. {0x1312, 0x1315, L},
  606. {0x1318, 0x131e, L},
  607. {0x1320, 0x1346, L},
  608. {0x1348, 0x135a, L},
  609. {0x1361, 0x137c, L},
  610. {0x13a0, 0x13f4, L},
  611. {0x1401, 0x1676, L},
  612. {0x1680, 0x1680, WS},
  613. {0x1681, 0x169a, L},
  614. {0x16a0, 0x16f0, L},
  615. {0x1700, 0x170c, L},
  616. {0x170e, 0x1711, L},
  617. {0x1712, 0x1714, NSM},
  618. {0x1720, 0x1731, L},
  619. {0x1732, 0x1734, NSM},
  620. {0x1735, 0x1736, L},
  621. {0x1740, 0x1751, L},
  622. {0x1752, 0x1753, NSM},
  623. {0x1760, 0x176c, L},
  624. {0x176e, 0x1770, L},
  625. {0x1772, 0x1773, NSM},
  626. {0x1780, 0x17b6, L},
  627. {0x17b7, 0x17bd, NSM},
  628. {0x17be, 0x17c5, L},
  629. {0x17c6, 0x17c6, NSM},
  630. {0x17c7, 0x17c8, L},
  631. {0x17c9, 0x17d3, NSM},
  632. {0x17d4, 0x17da, L},
  633. {0x17db, 0x17db, ET},
  634. {0x17dc, 0x17dc, L},
  635. {0x17dd, 0x17dd, NSM},
  636. {0x17e0, 0x17e9, L},
  637. {0x180b, 0x180d, NSM},
  638. {0x180e, 0x180e, WS},
  639. {0x1810, 0x1819, L},
  640. {0x1820, 0x1877, L},
  641. {0x1880, 0x18a8, L},
  642. {0x18a9, 0x18a9, NSM},
  643. {0x1900, 0x191c, L},
  644. {0x1920, 0x1922, NSM},
  645. {0x1923, 0x1926, L},
  646. {0x1927, 0x192b, NSM},
  647. {0x1930, 0x1931, L},
  648. {0x1932, 0x1932, NSM},
  649. {0x1933, 0x1938, L},
  650. {0x1939, 0x193b, NSM},
  651. {0x1946, 0x196d, L},
  652. {0x1970, 0x1974, L},
  653. {0x1d00, 0x1d6b, L},
  654. {0x1e00, 0x1e9b, L},
  655. {0x1ea0, 0x1ef9, L},
  656. {0x1f00, 0x1f15, L},
  657. {0x1f18, 0x1f1d, L},
  658. {0x1f20, 0x1f45, L},
  659. {0x1f48, 0x1f4d, L},
  660. {0x1f50, 0x1f57, L},
  661. {0x1f59, 0x1f59, L},
  662. {0x1f5b, 0x1f5b, L},
  663. {0x1f5d, 0x1f5d, L},
  664. {0x1f5f, 0x1f7d, L},
  665. {0x1f80, 0x1fb4, L},
  666. {0x1fb6, 0x1fbc, L},
  667. {0x1fbe, 0x1fbe, L},
  668. {0x1fc2, 0x1fc4, L},
  669. {0x1fc6, 0x1fcc, L},
  670. {0x1fd0, 0x1fd3, L},
  671. {0x1fd6, 0x1fdb, L},
  672. {0x1fe0, 0x1fec, L},
  673. {0x1ff2, 0x1ff4, L},
  674. {0x1ff6, 0x1ffc, L},
  675. {0x2000, 0x200a, WS},
  676. {0x200b, 0x200d, BN},
  677. {0x200e, 0x200e, L},
  678. {0x200f, 0x200f, R},
  679. {0x2028, 0x2028, WS},
  680. {0x2029, 0x2029, B},
  681. {0x202a, 0x202a, LRE},
  682. {0x202b, 0x202b, RLE},
  683. {0x202c, 0x202c, PDF},
  684. {0x202d, 0x202d, LRO},
  685. {0x202e, 0x202e, RLO},
  686. {0x202f, 0x202f, WS},
  687. {0x2030, 0x2034, ET},
  688. {0x2044, 0x2044, CS},
  689. {0x205f, 0x205f, WS},
  690. {0x2060, 0x2063, BN},
  691. {0x206a, 0x206f, BN},
  692. {0x2070, 0x2070, EN},
  693. {0x2071, 0x2071, L},
  694. {0x2074, 0x2079, EN},
  695. {0x207a, 0x207b, ET},
  696. {0x207f, 0x207f, L},
  697. {0x2080, 0x2089, EN},
  698. {0x208a, 0x208b, ET},
  699. {0x20a0, 0x20b1, ET},
  700. {0x20d0, 0x20ea, NSM},
  701. {0x2102, 0x2102, L},
  702. {0x2107, 0x2107, L},
  703. {0x210a, 0x2113, L},
  704. {0x2115, 0x2115, L},
  705. {0x2119, 0x211d, L},
  706. {0x2124, 0x2124, L},
  707. {0x2126, 0x2126, L},
  708. {0x2128, 0x2128, L},
  709. {0x212a, 0x212d, L},
  710. {0x212e, 0x212e, ET},
  711. {0x212f, 0x2131, L},
  712. {0x2133, 0x2139, L},
  713. {0x213d, 0x213f, L},
  714. {0x2145, 0x2149, L},
  715. {0x2160, 0x2183, L},
  716. {0x2212, 0x2213, ET},
  717. {0x2336, 0x237a, L},
  718. {0x2395, 0x2395, L},
  719. {0x2488, 0x249b, EN},
  720. {0x249c, 0x24e9, L},
  721. {0x2800, 0x28ff, L},
  722. {0x3000, 0x3000, WS},
  723. {0x3005, 0x3007, L},
  724. {0x3021, 0x3029, L},
  725. {0x302a, 0x302f, NSM},
  726. {0x3031, 0x3035, L},
  727. {0x3038, 0x303c, L},
  728. {0x3041, 0x3096, L},
  729. {0x3099, 0x309a, NSM},
  730. {0x309d, 0x309f, L},
  731. {0x30a1, 0x30fa, L},
  732. {0x30fc, 0x30ff, L},
  733. {0x3105, 0x312c, L},
  734. {0x3131, 0x318e, L},
  735. {0x3190, 0x31b7, L},
  736. {0x31f0, 0x321c, L},
  737. {0x3220, 0x3243, L},
  738. {0x3260, 0x327b, L},
  739. {0x327f, 0x32b0, L},
  740. {0x32c0, 0x32cb, L},
  741. {0x32d0, 0x32fe, L},
  742. {0x3300, 0x3376, L},
  743. {0x337b, 0x33dd, L},
  744. {0x33e0, 0x33fe, L},
  745. {0x3400, 0x4db5, L},
  746. {0x4e00, 0x9fa5, L},
  747. {0xa000, 0xa48c, L},
  748. {0xac00, 0xd7a3, L},
  749. {0xd800, 0xfa2d, L},
  750. {0xfa30, 0xfa6a, L},
  751. {0xfb00, 0xfb06, L},
  752. {0xfb13, 0xfb17, L},
  753. {0xfb1d, 0xfb1d, R},
  754. {0xfb1e, 0xfb1e, NSM},
  755. {0xfb1f, 0xfb28, R},
  756. {0xfb29, 0xfb29, ET},
  757. {0xfb2a, 0xfb36, R},
  758. {0xfb38, 0xfb3c, R},
  759. {0xfb3e, 0xfb3e, R},
  760. {0xfb40, 0xfb41, R},
  761. {0xfb43, 0xfb44, R},
  762. {0xfb46, 0xfb4f, R},
  763. {0xfb50, 0xfbb1, AL},
  764. {0xfbd3, 0xfd3d, AL},
  765. {0xfd50, 0xfd8f, AL},
  766. {0xfd92, 0xfdc7, AL},
  767. {0xfdf0, 0xfdfc, AL},
  768. {0xfe00, 0xfe0f, NSM},
  769. {0xfe20, 0xfe23, NSM},
  770. {0xfe50, 0xfe50, CS},
  771. {0xfe52, 0xfe52, CS},
  772. {0xfe55, 0xfe55, CS},
  773. {0xfe5f, 0xfe5f, ET},
  774. {0xfe62, 0xfe63, ET},
  775. {0xfe69, 0xfe6a, ET},
  776. {0xfe70, 0xfe74, AL},
  777. {0xfe76, 0xfefc, AL},
  778. {0xfeff, 0xfeff, BN},
  779. {0xff03, 0xff05, ET},
  780. {0xff0b, 0xff0b, ET},
  781. {0xff0c, 0xff0c, CS},
  782. {0xff0d, 0xff0d, ET},
  783. {0xff0e, 0xff0e, CS},
  784. {0xff0f, 0xff0f, ES},
  785. {0xff10, 0xff19, EN},
  786. {0xff1a, 0xff1a, CS},
  787. {0xff21, 0xff3a, L},
  788. {0xff41, 0xff5a, L},
  789. {0xff66, 0xffbe, L},
  790. {0xffc2, 0xffc7, L},
  791. {0xffca, 0xffcf, L},
  792. {0xffd2, 0xffd7, L},
  793. {0xffda, 0xffdc, L},
  794. {0xffe0, 0xffe1, ET},
  795. {0xffe5, 0xffe6, ET},
  796. };
  797. int i, j, k;
  798. i = -1;
  799. j = lenof(lookup);
  800. while (j - i > 1) {
  801. k = (i + j) / 2;
  802. if (ch < lookup[k].first)
  803. j = k;
  804. else if (ch > lookup[k].last)
  805. i = k;
  806. else
  807. return (unsigned char)lookup[k].type;
  808. }
  809. /*
  810. * If we reach here, the character was not in any of the
  811. * intervals listed in the lookup table. This means we return
  812. * ON (`Other Neutrals'). This is the appropriate code for any
  813. * character genuinely not listed in the Unicode table, and
  814. * also the table above has deliberately left out any
  815. * characters _explicitly_ listed as ON (to save space!).
  816. */
  817. return ON;
  818. }
  819. /* The Main shaping function, and the only one to be used
  820. * by the outside world.
  821. *
  822. * line: buffer to apply shaping to. this must be passed by doBidi() first
  823. * to: output buffer for the shaped data
  824. * from: start bidi at this index
  825. * count: number of characters in line
  826. */
  827. int doShape(BLOCKTYPE line, CHARTYPE* to, int from, int count)
  828. {
  829. int i, j, ligFlag;
  830. unsigned char prevTemp, nextTemp;
  831. CHARTYPE tempChar;
  832. ligFlag = 0;
  833. prevTemp = SU;
  834. nextTemp = SU;
  835. for(i=from; i<count; i++)
  836. {
  837. /* Get Previous and next Characters type */
  838. j=i;
  839. while(--j >= 0)
  840. {
  841. if(GetType(GETCHAR(line, j)) != NSM)
  842. {
  843. prevTemp = STYPE(GETCHAR(line, j));
  844. break;
  845. }
  846. }
  847. j=i;
  848. while(++j < count)
  849. {
  850. if(GetType(GETCHAR(line, j)) != NSM)
  851. {
  852. nextTemp = STYPE(GETCHAR(line, j));
  853. break;
  854. }else if(j == count-1)
  855. {
  856. nextTemp = SU;
  857. break;
  858. }
  859. }
  860. switch(STYPE(GETCHAR(line, i)))
  861. {
  862. case SC:
  863. case SU:
  864. to[i] = GETCHAR(line, i);
  865. break;
  866. case SR:
  867. if(prevTemp == SD || prevTemp == SC)
  868. to[i] = SFINAL(SISOLATED(GETCHAR(line, i)));
  869. else
  870. to[i] = SISOLATED(GETCHAR(line, i));
  871. break;
  872. case SD:
  873. /* Make Ligatures */
  874. if(GETCHAR(line, i) == 0x644)
  875. {
  876. j=i;
  877. while(j++<count)
  878. {
  879. if(GetType(GETCHAR(line, j)) != NSM)
  880. {
  881. tempChar = GETCHAR(line, j);
  882. break;
  883. }
  884. }
  885. switch(tempChar)
  886. {
  887. case 0x622:
  888. ligFlag = 1;
  889. if(prevTemp == SD || prevTemp == SC)
  890. to[i] = 0xFEF6;
  891. else
  892. to[i] = 0xFEF5;
  893. break;
  894. case 0x623:
  895. ligFlag = 1;
  896. if(prevTemp == SD || prevTemp == SC)
  897. to[i] = 0xFEF8;
  898. else
  899. to[i] = 0xFEF7;
  900. break;
  901. case 0x625:
  902. ligFlag = 1;
  903. if(prevTemp == SD || prevTemp == SC)
  904. to[i] = 0xFEFA;
  905. else
  906. to[i] = 0xFEF9;
  907. break;
  908. case 0x627:
  909. ligFlag = 1;
  910. if(prevTemp == SD || prevTemp == SC)
  911. to[i] = 0xFEFC;
  912. else
  913. to[i] = 0xFEFB;
  914. break;
  915. }
  916. if(ligFlag)
  917. {
  918. to[j] = 0x20;
  919. i = j;
  920. ligFlag = 0;
  921. break;
  922. }
  923. }
  924. if((prevTemp == SD) || (prevTemp == SC))
  925. {
  926. if(nextTemp == SR || nextTemp == SD || nextTemp == SC)
  927. to[i] = SMEDIAL(SISOLATED(GETCHAR(line, i)));
  928. else
  929. to[i] = SFINAL(SISOLATED(GETCHAR(line, i)));
  930. break;
  931. }else
  932. {
  933. if(nextTemp == SR || nextTemp == SD || nextTemp == SC)
  934. to[i] = SINITIAL(SISOLATED(GETCHAR(line, i)));
  935. else
  936. to[i] = SISOLATED(GETCHAR(line, i));
  937. break;
  938. }
  939. }
  940. nextTemp = SU;
  941. }
  942. return 1;
  943. }
  944. /* Rule (X1), (X2), (X3), (X4), (X5), (X6), (X7), (X8), (X9)
  945. * returns the length of the string without explicit marks
  946. */
  947. int doTypes(BLOCKTYPE line, unsigned char paragraphLevel, unsigned char* types,
  948. unsigned char* levels, int count, int fX, int * v2l)
  949. {
  950. unsigned char tempType;
  951. unsigned char currentEmbedding = paragraphLevel;
  952. unsigned char currentOverride = ON;
  953. int i, j;
  954. unsigned char levelStack[MAX_STACK+2];
  955. unsigned char overrideStack[MAX_STACK+2];
  956. int stackTop = 0;
  957. if(fX)
  958. {
  959. for( i=0, j=0; i<count; i++)
  960. {
  961. GETCHAR(line, j) = GETCHAR(line, i);
  962. if(v2l)
  963. {
  964. v2l[j] = v2l[i];
  965. }
  966. tempType = GetType(GETCHAR(line, i));
  967. switch(tempType)
  968. {
  969. case RLE:
  970. if(stackTop < MAX_STACK)
  971. {
  972. levelStack[stackTop] = currentEmbedding;
  973. overrideStack[stackTop] = currentOverride;
  974. stackTop++;
  975. currentEmbedding = leastGreaterOdd(currentEmbedding);
  976. currentOverride = ON;
  977. }
  978. break;
  979. case LRE:
  980. if(stackTop < MAX_STACK)
  981. {
  982. levelStack[stackTop] = currentEmbedding;
  983. overrideStack[stackTop] = currentOverride;
  984. stackTop++;
  985. currentEmbedding = leastGreaterEven(currentEmbedding);
  986. currentOverride = ON;
  987. }
  988. break;
  989. case RLO:
  990. if(stackTop < MAX_STACK)
  991. {
  992. levelStack[stackTop] = currentEmbedding;
  993. overrideStack[stackTop] = currentOverride;
  994. stackTop++;
  995. currentEmbedding = leastGreaterOdd(currentEmbedding);
  996. currentOverride = R;
  997. }
  998. break;
  999. case LRO:
  1000. if(stackTop <= MAX_STACK)
  1001. {
  1002. levelStack[stackTop] = currentEmbedding;
  1003. overrideStack[stackTop] = currentOverride;
  1004. stackTop++;
  1005. currentEmbedding = leastGreaterEven(currentEmbedding);
  1006. currentOverride = L;
  1007. }
  1008. break;
  1009. case PDF:
  1010. if(stackTop > 0)
  1011. {
  1012. currentEmbedding = levelStack[stackTop-1];
  1013. currentOverride = overrideStack[stackTop-1];
  1014. stackTop--;
  1015. }
  1016. break;
  1017. /* Whitespace is treated as neutral for now */
  1018. case WS:
  1019. case B:
  1020. case S:
  1021. levels[j] = currentEmbedding;
  1022. tempType = ON;
  1023. if(currentOverride != ON)
  1024. tempType = currentOverride;
  1025. types[j] = tempType;
  1026. j++;
  1027. break;
  1028. default:
  1029. levels[j] = currentEmbedding;
  1030. if(currentOverride != ON)
  1031. tempType = currentOverride;
  1032. types[j] = tempType;
  1033. j++;
  1034. break;
  1035. }
  1036. }
  1037. }else
  1038. {
  1039. j = count;
  1040. for( i=0; i<count; i++)
  1041. {
  1042. tempType = GetType(GETCHAR(line, i));
  1043. switch(tempType)
  1044. {
  1045. case WS:
  1046. case B:
  1047. case S:
  1048. levels[i] = currentEmbedding;
  1049. tempType = ON;
  1050. if(currentOverride != ON)
  1051. tempType = currentOverride;
  1052. break;
  1053. default:
  1054. levels[i] = currentEmbedding;
  1055. if(currentOverride != ON)
  1056. tempType = currentOverride;
  1057. break;
  1058. }
  1059. types[i] = tempType;
  1060. }
  1061. }
  1062. return j;
  1063. }
  1064. /* Rule (W3) */
  1065. void doALtoR(unsigned char* types, int count)
  1066. {
  1067. int i=0;
  1068. for(; i<count; i++)
  1069. {
  1070. if(types[i] == AL)
  1071. types[i] = R;
  1072. }
  1073. }
  1074. /*
  1075. * The Main Bidi Function, and the only function that should
  1076. * be used by the outside world.
  1077. *
  1078. * line: a buffer of size count containing text to apply
  1079. * the Bidirectional algorithm to.
  1080. */
  1081. int doBidi(BLOCKTYPE line, int count, int applyShape, int reorderCombining, int * v2l, int * l2v)
  1082. {
  1083. unsigned char* types;
  1084. unsigned char* levels;
  1085. unsigned char paragraphLevel;
  1086. unsigned char tempType, tempTypeSec;
  1087. int i, j, imax, fX, fAL, fET, fNSM, tempInt;
  1088. CHARTYPE* shapeTo;
  1089. if (v2l)
  1090. {
  1091. for(i=0; i<count; i++)
  1092. {
  1093. v2l[i] = i;
  1094. l2v[i] = i;
  1095. }
  1096. }
  1097. fX = fAL = fET = fNSM = 0;
  1098. for(i=0; i<count; i++)
  1099. {
  1100. switch(GetType(line[i]))
  1101. {
  1102. case AL:
  1103. case R:
  1104. fAL = 1;
  1105. break;
  1106. case LRE:
  1107. case LRO:
  1108. case RLE:
  1109. case RLO:
  1110. case PDF:
  1111. case BN:
  1112. fX = 1;
  1113. break;
  1114. case ET:
  1115. fET = 1;
  1116. break;
  1117. case NSM:
  1118. fNSM = 1;
  1119. break;
  1120. }
  1121. }
  1122. if(!fAL && !fX)
  1123. return 0;
  1124. /* Initialize types, levels */
  1125. types = (unsigned char*)malloc(sizeof(unsigned char) * count);
  1126. levels = (unsigned char*)malloc(sizeof(unsigned char) * count);
  1127. if(applyShape)
  1128. shapeTo = (CHARTYPE*)malloc(sizeof(CHARTYPE) * count);
  1129. /* Rule (P1) NOT IMPLEMENTED
  1130. * P1. Split the text into separate paragraphs. A paragraph separator is
  1131. * kept with the previous paragraph. Within each paragraph, apply all the
  1132. * other rules of this algorithm.
  1133. */
  1134. /* Rule (P2), (P3)
  1135. * P2. In each paragraph, find the first character of type L, AL, or R.
  1136. * P3. If a character is found in P2 and it is of type AL or R, then set
  1137. * the paragraph embedding level to one; otherwise, set it to zero.
  1138. */
  1139. paragraphLevel = GetParagraphLevel(line, count);
  1140. /* Rule (X1), (X2), (X3), (X4), (X5), (X6), (X7), (X8), (X9)
  1141. * X1. Begin by setting the current embedding level to the paragraph
  1142. * embedding level. Set the directional override status to neutral.
  1143. * X2. With each RLE, compute the least greater odd embedding level.
  1144. * X3. With each LRE, compute the least greater even embedding level.
  1145. * X4. With each RLO, compute the least greater odd embedding level.
  1146. * X5. With each LRO, compute the least greater even embedding level.
  1147. * X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  1148. * a. Set the level of the current character to the current
  1149. * embedding level.
  1150. * b. Whenever the directional override status is not neutral,
  1151. * reset the current character type to the directional
  1152. * override status.
  1153. * X7. With each PDF, determine the matching embedding or override code.
  1154. * If there was a valid matching code, restore (pop) the last
  1155. * remembered (pushed) embedding level and directional override.
  1156. * X8. All explicit directional embeddings and overrides are completely
  1157. * terminated at the end of each paragraph. Paragraph separators are not
  1158. * included in the embedding. (Useless here) NOT IMPLEMENTED
  1159. * X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  1160. * Here, they're converted to BN.
  1161. */
  1162. count = doTypes(line, paragraphLevel, types, levels, count, fX, v2l);
  1163. GETCHAR(line, count) = 0;
  1164. /* Rule (W1)
  1165. * W1. Examine each non-spacing mark (NSM) in the level run, and change
  1166. * the type of the NSM to the type of the previous character. If the NSM
  1167. * is at the start of the level run, it will get the type of sor.
  1168. */
  1169. if(fNSM)
  1170. {
  1171. if(types[0] == NSM)
  1172. types[0] = paragraphLevel;
  1173. for(i=1; i<count; i++)
  1174. {
  1175. if(types[i] == NSM)
  1176. types[i] = types[i-1];
  1177. /* Is this a safe assumption?
  1178. * I assumed the previous, IS a character.
  1179. */
  1180. }
  1181. }
  1182. /* Rule (W2)
  1183. * W2. Search backwards from each instance of a European number until the
  1184. * first strong type (R, L, AL, or sor) is found. If an AL is found,
  1185. * change the type of the European number to Arabic number.
  1186. */
  1187. for(i=0; i<count; i++)
  1188. {
  1189. if(types[i] == EN)
  1190. {
  1191. tempType = levels[i];
  1192. j=i;
  1193. while(--j >= 0 && levels[j] == tempType)
  1194. {
  1195. if(types[j] == AL)
  1196. {
  1197. types[i] = AN;
  1198. break;
  1199. }
  1200. else if(types[j] == R || types[j] == L)
  1201. {
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. }
  1207. /* Rule (W3)
  1208. * W3. Change all ALs to R.
  1209. *
  1210. * Optimization: on Rule Xn, we might set a flag on AL type
  1211. * to prevent this loop in L R lines only...
  1212. */
  1213. doALtoR(types, count);
  1214. /* Rule (W4)
  1215. * W4. A single European separator between two European numbers changes
  1216. * to a European number. A single common separator between two numbers
  1217. * of the same type changes to that type.
  1218. */
  1219. for( i=0; i<(count-1); i++)
  1220. {
  1221. if(types[i] == ES)
  1222. {
  1223. if(types[i-1] == EN && types[i+1] == EN)
  1224. types[i] = EN;
  1225. }else if(types[i] == CS)
  1226. {
  1227. if(types[i-1] == EN && types[i+1] == EN)
  1228. types[i] = EN;
  1229. else if(types[i-1] == AN && types[i+1] == AN)
  1230. types[i] = AN;
  1231. }
  1232. }
  1233. /* Rule (W5)
  1234. * W5. A sequence of European terminators adjacent to European numbers
  1235. * changes to all European numbers.
  1236. *
  1237. * Optimization: lots here... else ifs need rearrangement
  1238. */
  1239. if(fET)
  1240. {
  1241. for(i=0; i<count; i++)
  1242. {
  1243. if(types[i] == ET)
  1244. {
  1245. if(types[i-1] == EN)
  1246. {
  1247. types[i] = EN;
  1248. continue;
  1249. }else if(types[i+1] == EN)
  1250. {
  1251. types[i] = EN;
  1252. continue;
  1253. }else if(types[i+1] == ET)
  1254. {
  1255. j=i;
  1256. while(j <count && types[j] == ET)
  1257. {
  1258. j++;
  1259. }
  1260. if(types[j] == EN)
  1261. types[i] = EN;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. /* Rule (W6)
  1267. * W6. Otherwise, separators and terminators change to Other Neutral:
  1268. */
  1269. for(i=0; i<count; i++)
  1270. {
  1271. switch(types[i])
  1272. {
  1273. case ES:
  1274. case ET:
  1275. case CS:
  1276. types[i] = ON;
  1277. break;
  1278. }
  1279. }
  1280. /* Rule (W7)
  1281. * W7. Search backwards from each instance of a European number until
  1282. * the first strong type (R, L, or sor) is found. If an L is found,
  1283. * then change the type of the European number to L.
  1284. */
  1285. for(i=0; i<count; i++)
  1286. {
  1287. if(types[i] == EN)
  1288. {
  1289. tempType = levels[i];
  1290. j=i;
  1291. while(--j >= 0 && levels[j] == tempType)
  1292. {
  1293. if(types[j] == L)
  1294. {
  1295. types[i] = L;
  1296. break;
  1297. }
  1298. else if(types[j] == R || types[j] == AL)
  1299. {
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. }
  1305. /* Rule (N1)
  1306. * N1. A sequence of neutrals takes the direction of the surrounding
  1307. * strong text if the text on both sides has the same direction. European
  1308. * and Arabic numbers are treated as though they were R.
  1309. */
  1310. tempType = paragraphLevel;
  1311. for(i=0; i<count; i++)
  1312. {
  1313. if(types[i] == ON)
  1314. {
  1315. if(types[i-1] == R || types[i-1] == EN || types[i-1] == AN)
  1316. tempType = R;
  1317. else
  1318. tempType = L;
  1319. j=i;
  1320. while(j < count)
  1321. {
  1322. tempTypeSec = types[j];
  1323. if(tempTypeSec == ON)
  1324. j++;
  1325. else
  1326. break;
  1327. }
  1328. if(j == count)
  1329. tempTypeSec = odd(paragraphLevel) ? R : L;
  1330. if(((tempTypeSec == L || tempTypeSec == LRE) && (tempType == L)) ||
  1331. (((tempTypeSec == R) || (tempTypeSec == EN) || (tempTypeSec == AN)) && (tempType == R)))
  1332. {
  1333. while(i<j)
  1334. {
  1335. types[i++] = tempType;
  1336. }
  1337. }else
  1338. i = j;
  1339. }
  1340. }
  1341. /* Rule (N2)
  1342. * N2. Any remaining neutrals take the embedding direction.
  1343. */
  1344. for(i=0; i<count; i++)
  1345. {
  1346. if(types[i] == ON)
  1347. {
  1348. if((levels[i] % 2) == 0)
  1349. types[i] = L;
  1350. else
  1351. types[i] = R;
  1352. }
  1353. }
  1354. /* Rule (I1)
  1355. * I1. For all characters with an even (left-to-right) embedding
  1356. * direction, those of type R go up one level and those of type AN or
  1357. * EN go up two levels.
  1358. */
  1359. for(i=0; i<count; i++)
  1360. {
  1361. if((levels[i] % 2) == 0)
  1362. {
  1363. if(types[i] == R)
  1364. levels[i] += 1;
  1365. else if((types[i] == AN) || (types[i] == EN))
  1366. levels[i] += 2;
  1367. }else
  1368. {
  1369. if((types[i] == L) ||
  1370. (types[i] == EN) ||
  1371. (types[i] == AN))
  1372. levels[i] += 1;
  1373. }
  1374. }
  1375. /* Rule (I2)
  1376. * I2. For all characters with an odd (right-to-left) embedding direction,
  1377. * those of type L, EN or AN go up one level.
  1378. */
  1379. for(i=0; i<count; i++)
  1380. {
  1381. if((levels[i] % 2) == 1)
  1382. {
  1383. if(types[i] == L || types[i] == EN || types[i] == AN)
  1384. levels[i] += 1;
  1385. }
  1386. }
  1387. /* Rule (L1)
  1388. * L1. On each line, reset the embedding level of the following characters
  1389. * to the paragraph embedding level:
  1390. * (1)segment separators, (2)paragraph separators,
  1391. * (3)any sequence of whitespace characters preceding
  1392. * a segment separator or paragraph separator,
  1393. * (4)and any sequence of white space characters
  1394. * at the end of the line.
  1395. * The types of characters used here are the original types, not those
  1396. * modified by the previous phase.
  1397. */
  1398. j=count-1;
  1399. while(j>0 && (GetType(GETCHAR(line, j)) == WS))
  1400. {
  1401. j--;
  1402. }
  1403. if(j < (count-1))
  1404. {
  1405. for(j++; j<count; j++)
  1406. levels[j] = paragraphLevel;
  1407. }
  1408. for(i=0; i<count; i++)
  1409. {
  1410. tempType = GetType(GETCHAR(line, i));
  1411. if(tempType == WS)
  1412. {
  1413. j=i;
  1414. while((++j < count) && ((tempType == WS) || (tempType == RLE)) )
  1415. {
  1416. tempType = GetType(line[j]);
  1417. }
  1418. if(GetType(GETCHAR(line, j)) == B || GetType(GETCHAR(line, j)) == S)
  1419. {
  1420. for(j--; j>=i ; j--)
  1421. {
  1422. levels[j] = paragraphLevel;
  1423. }
  1424. }
  1425. }else if(tempType == B || tempType == S)
  1426. levels[i] = paragraphLevel;
  1427. }
  1428. /* Rule (L4)
  1429. * L4. A character that possesses the mirrored property as specified by
  1430. * Section 4.7, Mirrored, must be depicted by a mirrored glyph if the
  1431. * resolved directionality of that character is R.
  1432. */
  1433. /* Note: this is implemented before L2 for efficiency */
  1434. for(i=0; i<count; i++)
  1435. {
  1436. if((levels[i] % 2) == 1)
  1437. doMirror(&GETCHAR(line, i));
  1438. }
  1439. /* Rule (L3)
  1440. * L3. Combining marks applied to a right-to-left base character will at
  1441. * this point precede their base character. If the rendering engine
  1442. * expects them to follow the base characters in the final display
  1443. * process, then the ordering of the marks and the base character must
  1444. * be reversed.
  1445. * Combining marks are reordered to the right of each character on an
  1446. * odd level.
  1447. */
  1448. if(fNSM && reorderCombining)
  1449. {
  1450. CHARTYPE temp;
  1451. int it;
  1452. for(i=0; i<count; i++)
  1453. {
  1454. if(GetType(GETCHAR(line, i)) == NSM && odd(levels[i]))
  1455. {
  1456. j=i;
  1457. while((++j < count) && (GetType(GETCHAR(line, j)) == NSM));
  1458. j--; i--;
  1459. for(it=j; j>i; i++, j--)
  1460. {
  1461. temp = GETCHAR(line, i);
  1462. GETCHAR(line, i) = GETCHAR(line, j);
  1463. GETCHAR(line, j) = temp;
  1464. if(v2l)
  1465. {
  1466. tempInt = v2l[i];
  1467. v2l[i] = v2l[j];
  1468. v2l[j] = tempInt;
  1469. }
  1470. }
  1471. i=it+1;
  1472. }
  1473. }
  1474. }
  1475. /* Shaping
  1476. * Shaping is Applied to each run of levels separately....
  1477. */
  1478. if(applyShape)
  1479. {
  1480. for(i=0; i<count; i++)
  1481. {
  1482. shapeTo[i] = GETCHAR(line, i);
  1483. }
  1484. j=i=0;
  1485. while(j < count)
  1486. {
  1487. if(GetType(GETCHAR(line, j)) == AL)
  1488. {
  1489. if(j<count && j >= i )
  1490. {
  1491. tempType = levels[j];
  1492. i=j;
  1493. while((i++ < count) && (levels[i] == tempType));
  1494. doShape(line, shapeTo, j, i);
  1495. j=i;
  1496. tempType = levels[j];
  1497. }
  1498. }
  1499. j++;
  1500. }
  1501. for(i=0; i<count; i++)
  1502. {
  1503. GETCHAR(line, i) = shapeTo[i];
  1504. }
  1505. free(shapeTo);
  1506. }
  1507. /* Rule (L2)
  1508. * L2. From the highest level found in the text to the lowest odd level on
  1509. * each line, including intermediate levels not actually present in the
  1510. * text, reverse any contiguous sequence of characters that are at that
  1511. * level or higher
  1512. */
  1513. /* we flip the character string and leave the level array */
  1514. imax = 0;
  1515. i=0;
  1516. tempType = levels[0];
  1517. while(i < count)
  1518. {
  1519. if(levels[i] > tempType)
  1520. {
  1521. tempType = levels[i];
  1522. imax=i;
  1523. }
  1524. i++;
  1525. }
  1526. /* maximum level in tempType, its index in imax. */
  1527. while(tempType > 0) /* loop from highest level to the least odd, */
  1528. { /* which i assume is 1 */
  1529. flipThisRun(line, levels, tempType, count, v2l);
  1530. tempType--;
  1531. }
  1532. free(types);
  1533. free(levels);
  1534. if (l2v && v2l)
  1535. {
  1536. for(i=0; i<count; i++)
  1537. {
  1538. l2v[v2l[i]] = i;
  1539. }
  1540. }
  1541. return count;
  1542. }
  1543. /*
  1544. * takes a pointer to a character that is checked for
  1545. * having a mirror glyph, and replaced on the spot
  1546. */
  1547. void doMirror(CHARTYPE* ch)
  1548. {
  1549. static const struct{
  1550. CHARTYPE first, mirror;
  1551. } lookup[] = {
  1552. {0x0028, 0x0029},
  1553. {0x0029, 0x0028},
  1554. {0x003C, 0x003E},
  1555. {0x003E, 0x003C},
  1556. {0x005B, 0x005D},
  1557. {0x005D, 0x005B},
  1558. {0x007B, 0x007D},
  1559. {0x007D, 0x007B},
  1560. {0x00AB, 0x00BB},
  1561. {0x00BB, 0x00AB},
  1562. {0x2039, 0x203A},
  1563. {0x203A, 0x2039},
  1564. {0x2045, 0x2046},
  1565. {0x2046, 0x2045},
  1566. {0x207D, 0x207E},
  1567. {0x207E, 0x207D},
  1568. {0x208D, 0x208E},
  1569. {0x208E, 0x208D},
  1570. {0x2208, 0x220B},
  1571. {0x2209, 0x220C},
  1572. {0x220A, 0x220D},
  1573. {0x220B, 0x2208},
  1574. {0x220C, 0x2209},
  1575. {0x220D, 0x220A},
  1576. {0x2215, 0x29F5},
  1577. {0x223C, 0x223D},
  1578. {0x223D, 0x223C},
  1579. {0x2243, 0x22CD},
  1580. {0x2252, 0x2253},
  1581. {0x2253, 0x2252},
  1582. {0x2254, 0x2255},
  1583. {0x2255, 0x2254},
  1584. {0x2264, 0x2265},
  1585. {0x2265, 0x2264},
  1586. {0x2266, 0x2267},
  1587. {0x2267, 0x2266},
  1588. {0x2268, 0x2269},
  1589. {0x2269, 0x2268},
  1590. {0x226A, 0x226B},
  1591. {0x226B, 0x226A},
  1592. {0x226E, 0x226F},
  1593. {0x226F, 0x226E},
  1594. {0x2270, 0x2271},
  1595. {0x2271, 0x2270},
  1596. {0x2272, 0x2273},
  1597. {0x2273, 0x2272},
  1598. {0x2274, 0x2275},
  1599. {0x2275, 0x2274},
  1600. {0x2276, 0x2277},
  1601. {0x2277, 0x2276},
  1602. {0x2278, 0x2279},
  1603. {0x2279, 0x2278},
  1604. {0x227A, 0x227B},
  1605. {0x227B, 0x227A},
  1606. {0x227C, 0x227D},
  1607. {0x227D, 0x227C},
  1608. {0x227E, 0x227F},
  1609. {0x227F, 0x227E},
  1610. {0x2280, 0x2281},
  1611. {0x2281, 0x2280},
  1612. {0x2282, 0x2283},
  1613. {0x2283, 0x2282},
  1614. {0x2284, 0x2285},
  1615. {0x2285, 0x2284},
  1616. {0x2286, 0x2287},
  1617. {0x2287, 0x2286},
  1618. {0x2288, 0x2289},
  1619. {0x2289, 0x2288},
  1620. {0x228A, 0x228B},
  1621. {0x228B, 0x228A},
  1622. {0x228F, 0x2290},
  1623. {0x2290, 0x228F},
  1624. {0x2291, 0x2292},
  1625. {0x2292, 0x2291},
  1626. {0x2298, 0x29B8},
  1627. {0x22A2, 0x22A3},
  1628. {0x22A3, 0x22A2},
  1629. {0x22A6, 0x2ADE},
  1630. {0x22A8, 0x2AE4},
  1631. {0x22A9, 0x2AE3},
  1632. {0x22AB, 0x2AE5},
  1633. {0x22B0, 0x22B1},
  1634. {0x22B1, 0x22B0},
  1635. {0x22B2, 0x22B3},
  1636. {0x22B3, 0x22B2},
  1637. {0x22B4, 0x22B5},
  1638. {0x22B5, 0x22B4},
  1639. {0x22B6, 0x22B7},
  1640. {0x22B7, 0x22B6},
  1641. {0x22C9, 0x22CA},
  1642. {0x22CA, 0x22C9},
  1643. {0x22CB, 0x22CC},
  1644. {0x22CC, 0x22CB},
  1645. {0x22CD, 0x2243},
  1646. {0x22D0, 0x22D1},
  1647. {0x22D1, 0x22D0},
  1648. {0x22D6, 0x22D7},
  1649. {0x22D7, 0x22D6},
  1650. {0x22D8, 0x22D9},
  1651. {0x22D9, 0x22D8},
  1652. {0x22DA, 0x22DB},
  1653. {0x22DB, 0x22DA},
  1654. {0x22DC, 0x22DD},
  1655. {0x22DD, 0x22DC},
  1656. {0x22DE, 0x22DF},
  1657. {0x22DF, 0x22DE},
  1658. {0x22E0, 0x22E1},
  1659. {0x22E1, 0x22E0},
  1660. {0x22E2, 0x22E3},
  1661. {0x22E3, 0x22E2},
  1662. {0x22E4, 0x22E5},
  1663. {0x22E5, 0x22E4},
  1664. {0x22E6, 0x22E7},
  1665. {0x22E7, 0x22E6},
  1666. {0x22E8, 0x22E9},
  1667. {0x22E9, 0x22E8},
  1668. {0x22EA, 0x22EB},
  1669. {0x22EB, 0x22EA},
  1670. {0x22EC, 0x22ED},
  1671. {0x22ED, 0x22EC},
  1672. {0x22F0, 0x22F1},
  1673. {0x22F1, 0x22F0},
  1674. {0x22F2, 0x22FA},
  1675. {0x22F3, 0x22FB},
  1676. {0x22F4, 0x22FC},
  1677. {0x22F6, 0x22FD},
  1678. {0x22F7, 0x22FE},
  1679. {0x22FA, 0x22F2},
  1680. {0x22FB, 0x22F3},
  1681. {0x22FC, 0x22F4},
  1682. {0x22FD, 0x22F6},
  1683. {0x22FE, 0x22F7},
  1684. {0x2308, 0x2309},
  1685. {0x2309, 0x2308},
  1686. {0x230A, 0x230B},
  1687. {0x230B, 0x230A},
  1688. {0x2329, 0x232A},
  1689. {0x232A, 0x2329},
  1690. {0x2768, 0x2769},
  1691. {0x2769, 0x2768},
  1692. {0x276A, 0x276B},
  1693. {0x276B, 0x276A},
  1694. {0x276C, 0x276D},
  1695. {0x276D, 0x276C},
  1696. {0x276E, 0x276F},
  1697. {0x276F, 0x276E},
  1698. {0x2770, 0x2771},
  1699. {0x2771, 0x2770},
  1700. {0x2772, 0x2773},
  1701. {0x2773, 0x2772},
  1702. {0x2774, 0x2775},
  1703. {0x2775, 0x2774},
  1704. {0x27C3, 0x27C4},
  1705. {0x27C4, 0x27C3},
  1706. {0x27C5, 0x27C6},
  1707. {0x27C6, 0x27C5},
  1708. {0x27D5, 0x27D6},
  1709. {0x27D6, 0x27D5},
  1710. {0x27DD, 0x27DE},
  1711. {0x27DE, 0x27DD},
  1712. {0x27E2, 0x27E3},
  1713. {0x27E3, 0x27E2},
  1714. {0x27E4, 0x27E5},
  1715. {0x27E5, 0x27E4},
  1716. {0x27E6, 0x27E7},
  1717. {0x27E7, 0x27E6},
  1718. {0x27E8, 0x27E9},
  1719. {0x27E9, 0x27E8},
  1720. {0x27EA, 0x27EB},
  1721. {0x27EB, 0x27EA},
  1722. {0x2983, 0x2984},
  1723. {0x2984, 0x2983},
  1724. {0x2985, 0x2986},
  1725. {0x2986, 0x2985},
  1726. {0x2987, 0x2988},
  1727. {0x2988, 0x2987},
  1728. {0x2989, 0x298A},
  1729. {0x298A, 0x2989},
  1730. {0x298B, 0x298C},
  1731. {0x298C, 0x298B},
  1732. {0x298D, 0x2990},
  1733. {0x298E, 0x298F},
  1734. {0x298F, 0x298E},
  1735. {0x2990, 0x298D},
  1736. {0x2991, 0x2992},
  1737. {0x2992, 0x2991},
  1738. {0x2993, 0x2994},
  1739. {0x2994, 0x2993},
  1740. {0x2995, 0x2996},
  1741. {0x2996, 0x2995},
  1742. {0x2997, 0x2998},
  1743. {0x2998, 0x2997},
  1744. {0x29B8, 0x2298},
  1745. {0x29C0, 0x29C1},
  1746. {0x29C1, 0x29C0},
  1747. {0x29C4, 0x29C5},
  1748. {0x29C5, 0x29C4},
  1749. {0x29CF, 0x29D0},
  1750. {0x29D0, 0x29CF},
  1751. {0x29D1, 0x29D2},
  1752. {0x29D2, 0x29D1},
  1753. {0x29D4, 0x29D5},
  1754. {0x29D5, 0x29D4},
  1755. {0x29D8, 0x29D9},
  1756. {0x29D9, 0x29D8},
  1757. {0x29DA, 0x29DB},
  1758. {0x29DB, 0x29DA},
  1759. {0x29F5, 0x2215},
  1760. {0x29F8, 0x29F9},
  1761. {0x29F9, 0x29F8},
  1762. {0x29FC, 0x29FD},
  1763. {0x29FD, 0x29FC},
  1764. {0x2A2B, 0x2A2C},
  1765. {0x2A2C, 0x2A2B},
  1766. {0x2A2D, 0x2A2E},
  1767. {0x2A2E, 0x2A2D},
  1768. {0x2A34, 0x2A35},
  1769. {0x2A35, 0x2A34},
  1770. {0x2A3C, 0x2A3D},
  1771. {0x2A3D, 0x2A3C},
  1772. {0x2A64, 0x2A65},
  1773. {0x2A65, 0x2A64},
  1774. {0x2A79, 0x2A7A},
  1775. {0x2A7A, 0x2A79},
  1776. {0x2A7D, 0x2A7E},
  1777. {0x2A7E, 0x2A7D},
  1778. {0x2A7F, 0x2A80},
  1779. {0x2A80, 0x2A7F},
  1780. {0x2A81, 0x2A82},
  1781. {0x2A82, 0x2A81},
  1782. {0x2A83, 0x2A84},
  1783. {0x2A84, 0x2A83},
  1784. {0x2A8B, 0x2A8C},
  1785. {0x2A8C, 0x2A8B},
  1786. {0x2A91, 0x2A92},
  1787. {0x2A92, 0x2A91},
  1788. {0x2A93, 0x2A94},
  1789. {0x2A94, 0x2A93},
  1790. {0x2A95, 0x2A96},
  1791. {0x2A96, 0x2A95},
  1792. {0x2A97, 0x2A98},
  1793. {0x2A98, 0x2A97},
  1794. {0x2A99, 0x2A9A},
  1795. {0x2A9A, 0x2A99},
  1796. {0x2A9B, 0x2A9C},
  1797. {0x2A9C, 0x2A9B},
  1798. {0x2AA1, 0x2AA2},
  1799. {0x2AA2, 0x2AA1},
  1800. {0x2AA6, 0x2AA7},
  1801. {0x2AA7, 0x2AA6},
  1802. {0x2AA8, 0x2AA9},
  1803. {0x2AA9, 0x2AA8},
  1804. {0x2AAA, 0x2AAB},
  1805. {0x2AAB, 0x2AAA},
  1806. {0x2AAC, 0x2AAD},
  1807. {0x2AAD, 0x2AAC},
  1808. {0x2AAF, 0x2AB0},
  1809. {0x2AB0, 0x2AAF},
  1810. {0x2AB3, 0x2AB4},
  1811. {0x2AB4, 0x2AB3},
  1812. {0x2ABB, 0x2ABC},
  1813. {0x2ABC, 0x2ABB},
  1814. {0x2ABD, 0x2ABE},
  1815. {0x2ABE, 0x2ABD},
  1816. {0x2ABF, 0x2AC0},
  1817. {0x2AC0, 0x2ABF},
  1818. {0x2AC1, 0x2AC2},
  1819. {0x2AC2, 0x2AC1},
  1820. {0x2AC3, 0x2AC4},
  1821. {0x2AC4, 0x2AC3},
  1822. {0x2AC5, 0x2AC6},
  1823. {0x2AC6, 0x2AC5},
  1824. {0x2ACD, 0x2ACE},
  1825. {0x2ACE, 0x2ACD},
  1826. {0x2ACF, 0x2AD0},
  1827. {0x2AD0, 0x2ACF},
  1828. {0x2AD1, 0x2AD2},
  1829. {0x2AD2, 0x2AD1},
  1830. {0x2AD3, 0x2AD4},
  1831. {0x2AD4, 0x2AD3},
  1832. {0x2AD5, 0x2AD6},
  1833. {0x2AD6, 0x2AD5},
  1834. {0x2ADE, 0x22A6},
  1835. {0x2AE3, 0x22A9},
  1836. {0x2AE4, 0x22A8},
  1837. {0x2AE5, 0x22AB},
  1838. {0x2AEC, 0x2AED},
  1839. {0x2AED, 0x2AEC},
  1840. {0x2AF7, 0x2AF8},
  1841. {0x2AF8, 0x2AF7},
  1842. {0x2AF9, 0x2AFA},
  1843. {0x2AFA, 0x2AF9},
  1844. {0x2E02, 0x2E03},
  1845. {0x2E03, 0x2E02},
  1846. {0x2E04, 0x2E05},
  1847. {0x2E05, 0x2E04},
  1848. {0x2E09, 0x2E0A},
  1849. {0x2E0A, 0x2E09},
  1850. {0x2E0C, 0x2E0D},
  1851. {0x2E0D, 0x2E0C},
  1852. {0x2E1C, 0x2E1D},
  1853. {0x2E1D, 0x2E1C},
  1854. {0x3008, 0x3009},
  1855. {0x3009, 0x3008},
  1856. {0x300A, 0x300B},
  1857. {0x300B, 0x300A},
  1858. {0x300C, 0x300D},
  1859. {0x300D, 0x300C},
  1860. {0x300E, 0x300F},
  1861. {0x300F, 0x300E},
  1862. {0x3010, 0x3011},
  1863. {0x3011, 0x3010},
  1864. {0x3014, 0x3015},
  1865. {0x3015, 0x3014},
  1866. {0x3016, 0x3017},
  1867. {0x3017, 0x3016},
  1868. {0x3018, 0x3019},
  1869. {0x3019, 0x3018},
  1870. {0x301A, 0x301B},
  1871. {0x301B, 0x301A},
  1872. {0xFF08, 0xFF09},
  1873. {0xFF09, 0xFF08},
  1874. {0xFF1C, 0xFF1E},
  1875. {0xFF1E, 0xFF1C},
  1876. {0xFF3B, 0xFF3D},
  1877. {0xFF3D, 0xFF3B},
  1878. {0xFF5B, 0xFF5D},
  1879. {0xFF5D, 0xFF5B},
  1880. {0xFF5F, 0xFF60},
  1881. {0xFF60, 0xFF5F},
  1882. {0xFF62, 0xFF63},
  1883. {0xFF63, 0xFF62},
  1884. };
  1885. int i, j, k;
  1886. i = -1;
  1887. j = lenof(lookup);
  1888. while (j - i > 1) {
  1889. k = (i + j) / 2;
  1890. if (*ch < lookup[k].first)
  1891. j = k;
  1892. else if (*ch > lookup[k].first)
  1893. i = k;
  1894. else if(*ch == lookup[k].first)
  1895. {
  1896. //return (unsigned char)lookup[k].type;
  1897. *ch = lookup[k].mirror;
  1898. return;
  1899. }
  1900. }
  1901. }