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.
 
 
 
 
 
 

752 lines
21 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains horizontal and vertical flipping routines.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. static uint32_t flipchar(uint32_t ch);
  25. static uint32_t flopchar(uint32_t ch);
  26. static uint32_t rotatechar(uint32_t ch);
  27. static uint32_t leftchar(uint32_t ch);
  28. static uint32_t rightchar(uint32_t ch);
  29. /** \brief Invert a canvas' colours.
  30. *
  31. * Invert a canvas' colours (black becomes white, red becomes cyan, etc.)
  32. * without changing the characters in it.
  33. *
  34. * This function never fails.
  35. *
  36. * \param cv The canvas to invert.
  37. * \return This function always returns 0.
  38. */
  39. int cucul_invert(cucul_canvas_t *cv)
  40. {
  41. uint32_t *attrs = cv->attrs;
  42. unsigned int i;
  43. for(i = cv->height * cv->width; i--; )
  44. {
  45. *attrs = *attrs ^ 0x000f000f;
  46. attrs++;
  47. }
  48. return 0;
  49. }
  50. /** \brief Flip a canvas horizontally.
  51. *
  52. * Flip a canvas horizontally, choosing characters that look like the
  53. * mirrored version wherever possible. Some characters will stay
  54. * unchanged by the process, but the operation is guaranteed to be
  55. * involutive: performing it again gives back the original canvas.
  56. *
  57. * This function never fails.
  58. *
  59. * \param cv The canvas to flip.
  60. * \return This function always returns 0.
  61. */
  62. int cucul_flip(cucul_canvas_t *cv)
  63. {
  64. unsigned int y;
  65. for(y = 0; y < cv->height; y++)
  66. {
  67. uint32_t *cleft = cv->chars + y * cv->width;
  68. uint32_t *cright = cleft + cv->width - 1;
  69. uint32_t *aleft = cv->attrs + y * cv->width;
  70. uint32_t *aright = aleft + cv->width - 1;
  71. while(cleft < cright)
  72. {
  73. uint32_t ch;
  74. uint32_t attr;
  75. /* Swap attributes */
  76. attr = *aright;
  77. *aright-- = *aleft;
  78. *aleft++ = attr;
  79. /* Swap characters */
  80. ch = *cright;
  81. *cright-- = flipchar(*cleft);
  82. *cleft++ = flipchar(ch);
  83. }
  84. if(cleft == cright)
  85. *cleft = flipchar(*cleft);
  86. /* Fix fullwidth characters. Could it be done in one loop? */
  87. cleft = cv->chars + y * cv->width;
  88. cright = cleft + cv->width - 1;
  89. for( ; cleft < cright; cleft++)
  90. {
  91. if(cleft[0] == CUCUL_MAGIC_FULLWIDTH)
  92. {
  93. cleft[0] = cleft[1];
  94. cleft[1] = CUCUL_MAGIC_FULLWIDTH;
  95. cleft++;
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. /** \brief Flip a canvas vertically.
  102. *
  103. * Flip a canvas vertically, choosing characters that look like the
  104. * mirrored version wherever possible. Some characters will stay
  105. * unchanged by the process, but the operation is guaranteed to be
  106. * involutive: performing it again gives back the original canvas.
  107. *
  108. * This function never fails.
  109. *
  110. * \param cv The canvas to flop.
  111. * \return This function always returns 0.
  112. */
  113. int cucul_flop(cucul_canvas_t *cv)
  114. {
  115. unsigned int x;
  116. for(x = 0; x < cv->width; x++)
  117. {
  118. uint32_t *ctop = cv->chars + x;
  119. uint32_t *cbottom = ctop + cv->width * (cv->height - 1);
  120. uint32_t *atop = cv->attrs + x;
  121. uint32_t *abottom = atop + cv->width * (cv->height - 1);
  122. while(ctop < cbottom)
  123. {
  124. uint32_t ch;
  125. uint32_t attr;
  126. /* Swap attributes */
  127. attr = *abottom; *abottom = *atop; *atop = attr;
  128. /* Swap characters */
  129. ch = *cbottom; *cbottom = flopchar(*ctop); *ctop = flopchar(ch);
  130. ctop += cv->width; cbottom -= cv->width;
  131. atop += cv->width; abottom -= cv->width;
  132. }
  133. if(ctop == cbottom)
  134. *ctop = flopchar(*ctop);
  135. }
  136. return 0;
  137. }
  138. /** \brief Rotate a canvas.
  139. *
  140. * Apply a 180-degree transformation to a canvas, choosing characters
  141. * that look like the upside-down version wherever possible. Some
  142. * characters will stay unchanged by the process, but the operation is
  143. * guaranteed to be involutive: performing it again gives back the
  144. * original canvas.
  145. *
  146. * This function never fails.
  147. *
  148. * \param cv The canvas to rotate.
  149. * \return This function always returns 0.
  150. */
  151. int cucul_rotate_180(cucul_canvas_t *cv)
  152. {
  153. uint32_t *cbegin = cv->chars;
  154. uint32_t *cend = cbegin + cv->width * cv->height - 1;
  155. uint32_t *abegin = cv->attrs;
  156. uint32_t *aend = abegin + cv->width * cv->height - 1;
  157. unsigned int y;
  158. while(cbegin < cend)
  159. {
  160. uint32_t ch;
  161. uint32_t attr;
  162. /* Swap attributes */
  163. attr = *aend; *aend = *abegin; *abegin = attr;
  164. /* Swap characters */
  165. ch = *cend; *cend = rotatechar(*cbegin); *cbegin = rotatechar(ch);
  166. cbegin++; cend--; abegin++; aend--;
  167. }
  168. if(cbegin == cend)
  169. *cbegin = rotatechar(*cbegin);
  170. /* Fix fullwidth characters. Could it be done in one loop? */
  171. for(y = 0; y < cv->height; y++)
  172. {
  173. cbegin = cv->chars + y * cv->width;
  174. cend = cbegin + cv->width - 1;
  175. for( ; cbegin < cend; cbegin++)
  176. {
  177. if(cbegin[0] == CUCUL_MAGIC_FULLWIDTH)
  178. {
  179. cbegin[0] = cbegin[1];
  180. cbegin[1] = CUCUL_MAGIC_FULLWIDTH;
  181. cbegin++;
  182. }
  183. }
  184. }
  185. return 0;
  186. }
  187. /** \brief Rotate a canvas, 90 degrees counterclockwise.
  188. *
  189. * Apply a 90-degree transformation to a canvas, choosing characters
  190. * that look like the rotated version wherever possible. Some characters
  191. * will stay unchanged by the process, some others will be replaced by
  192. * close equivalents. Fullwidth characters will be lost. The operation is
  193. * not guaranteed to be reversible at all.
  194. *
  195. * Note that the width and height of the canvas are swapped.
  196. *
  197. * If an error occurs, -1 is returned and \b errno is set accordingly:
  198. * - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
  199. * - \c ENOMEM Not enough memory to allocate the new canvas size. If this
  200. * happens, the previous canvas handle is still valid.
  201. *
  202. * \param cv The canvas to rotate left.
  203. * \return 0 in case of success, -1 if an error occurred.
  204. */
  205. int cucul_rotate_left(cucul_canvas_t *cv)
  206. {
  207. uint32_t *newchars, *newattrs;
  208. unsigned int x, y;
  209. if(cv->refcount)
  210. {
  211. seterrno(EBUSY);
  212. return -1;
  213. }
  214. /* Save the current frame shortcuts */
  215. _cucul_save_frame_info(cv);
  216. newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
  217. if(!newchars)
  218. return -1;
  219. newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
  220. if(!newattrs)
  221. {
  222. free(newchars);
  223. return -1;
  224. }
  225. for(y = 0; y < cv->height; y++)
  226. {
  227. for(x = 0; x < cv->width; x++)
  228. {
  229. uint32_t ch, attr;
  230. ch = cv->chars[cv->width * y + x];
  231. attr = cv->attrs[cv->width * y + x];
  232. /* FIXME: do something about fullwidth characters */
  233. ch = leftchar(ch);
  234. newchars[cv->height * (cv->width - 1 - x) + y] = ch;
  235. newattrs[cv->height * (cv->width - 1 - x) + y] = attr;
  236. }
  237. }
  238. free(cv->chars);
  239. free(cv->attrs);
  240. /* Swap X and Y information */
  241. x = cv->frames[cv->frame].x;
  242. y = cv->frames[cv->frame].y;
  243. cv->frames[cv->frame].x = y;
  244. cv->frames[cv->frame].y = cv->width - 1 - x;
  245. x = cv->frames[cv->frame].handlex;
  246. y = cv->frames[cv->frame].handley;
  247. cv->frames[cv->frame].handlex = y;
  248. cv->frames[cv->frame].handley = cv->width - 1 - x;
  249. cv->frames[cv->frame].width = cv->height;
  250. cv->frames[cv->frame].height = cv->width;
  251. cv->frames[cv->frame].chars = newchars;
  252. cv->frames[cv->frame].attrs = newattrs;
  253. /* Reset the current frame shortcuts */
  254. _cucul_load_frame_info(cv);
  255. return 0;
  256. }
  257. /** \brief Rotate a canvas, 90 degrees clockwise.
  258. *
  259. * Apply a 270-degree transformation to a canvas, choosing characters
  260. * that look like the rotated version wherever possible. Some characters
  261. * will stay unchanged by the process, some others will be replaced by
  262. * close equivalents. Fullwidth characters will be lost. The operation is
  263. * not guaranteed to be reversible at all.
  264. *
  265. * Note that the width and height of the canvas are swapped.
  266. *
  267. * If an error occurs, -1 is returned and \b errno is set accordingly:
  268. * - \c EBUSY The canvas is in use by a display driver and cannot be rotated.
  269. * - \c ENOMEM Not enough memory to allocate the new canvas size. If this
  270. * happens, the previous canvas handle is still valid.
  271. *
  272. * \param cv The canvas to rotate right.
  273. * \return 0 in case of success, -1 if an error occurred.
  274. */
  275. int cucul_rotate_right(cucul_canvas_t *cv)
  276. {
  277. uint32_t *newchars, *newattrs;
  278. unsigned int x, y;
  279. if(cv->refcount)
  280. {
  281. seterrno(EBUSY);
  282. return -1;
  283. }
  284. /* Save the current frame shortcuts */
  285. _cucul_save_frame_info(cv);
  286. newchars = malloc(cv->width * cv->height * sizeof(uint32_t));
  287. if(!newchars)
  288. {
  289. seterrno(ENOMEM);
  290. return -1;
  291. }
  292. newattrs = malloc(cv->width * cv->height * sizeof(uint32_t));
  293. if(!newattrs)
  294. {
  295. free(newchars);
  296. seterrno(ENOMEM);
  297. return -1;
  298. }
  299. for(y = 0; y < cv->height; y++)
  300. {
  301. for(x = 0; x < cv->width; x++)
  302. {
  303. uint32_t ch, attr;
  304. ch = cv->chars[cv->width * y + x];
  305. attr = cv->attrs[cv->width * y + x];
  306. /* FIXME: do something about fullwidth characters */
  307. ch = rightchar(ch);
  308. newchars[cv->height * x + cv->height - 1 - y] = ch;
  309. newattrs[cv->height * x + cv->height - 1 - y] = attr;
  310. }
  311. }
  312. free(cv->chars);
  313. free(cv->attrs);
  314. /* Swap X and Y information */
  315. x = cv->frames[cv->frame].x;
  316. y = cv->frames[cv->frame].y;
  317. cv->frames[cv->frame].x = cv->height - 1 - y;
  318. cv->frames[cv->frame].y = x;
  319. x = cv->frames[cv->frame].handlex;
  320. y = cv->frames[cv->frame].handley;
  321. cv->frames[cv->frame].handlex = cv->height - 1 - y;
  322. cv->frames[cv->frame].handley = x;
  323. cv->frames[cv->frame].width = cv->height;
  324. cv->frames[cv->frame].height = cv->width;
  325. cv->frames[cv->frame].chars = newchars;
  326. cv->frames[cv->frame].attrs = newattrs;
  327. /* Reset the current frame shortcuts */
  328. _cucul_load_frame_info(cv);
  329. return 0;
  330. }
  331. /* FIXME: as the lookup tables grow bigger, use a log(n) lookup instead
  332. * of linear lookup. */
  333. static uint32_t flipchar(uint32_t ch)
  334. {
  335. int i;
  336. static uint32_t const noflip[] =
  337. {
  338. /* ASCII */
  339. ' ', '"', '#', '\'', '-', '.', '*', '+', ':', '=', '0', '8',
  340. 'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y', '^',
  341. '_', 'i', 'o', 'v', 'w', 'x', '|',
  342. /* CP437 and box drawing */
  343. 0x2591, 0x2592, 0x2593, 0x2588, 0x2584, 0x2580, /* ░ ▒ ▓ █ ▄ ▀ */
  344. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  345. 0x252c, 0x2534, 0x2533, 0x253b, 0x2566, 0x2569, /* ┬ ┴ ┳ ┻ ╦ ╩ */
  346. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  347. 0x2575, 0x2577, 0x2579, 0x257b, /* ╵ ╷ ╹ ╻ */
  348. 0
  349. };
  350. static uint32_t const pairs[] =
  351. {
  352. /* ASCII */
  353. '(', ')',
  354. '/', '\\',
  355. '<', '>',
  356. '[', ']',
  357. 'b', 'd',
  358. 'p', 'q',
  359. '{', '}',
  360. /* ASCII-Unicode */
  361. ';', 0x204f, /* ; ⁏ */
  362. '`', 0x00b4, /* ` ´ */
  363. ',', 0x02ce, /* , ˎ */
  364. 'C', 0x03fd, /* C Ͻ */
  365. 'E', 0x018e, /* E Ǝ */
  366. 'L', 0x2143, /* L ⅃ */
  367. 'N', 0x0418, /* N И */
  368. 'R', 0x042f, /* R Я */
  369. 'S', 0x01a7, /* S Ƨ */
  370. 'c', 0x0254, /* c ɔ */
  371. 'e', 0x0258, /* e ɘ */
  372. /* CP437 */
  373. 0x258c, 0x2590, /* ▌ ▐ */
  374. 0x2596, 0x2597, /* ▖ ▗ */
  375. 0x2598, 0x259d, /* ▘ ▝ */
  376. 0x2599, 0x259f, /* ▙ ▟ */
  377. 0x259a, 0x259e, /* ▚ ▞ */
  378. 0x259b, 0x259c, /* ▛ ▜ */
  379. 0x25ba, 0x25c4, /* ► ◄ */
  380. 0x2192, 0x2190, /* → ← */
  381. 0x2310, 0xac, /* ⌐ ¬ */
  382. /* Box drawing */
  383. 0x250c, 0x2510, /* ┌ ┐ */
  384. 0x2514, 0x2518, /* └ ┘ */
  385. 0x251c, 0x2524, /* ├ ┤ */
  386. 0x250f, 0x2513, /* ┏ ┓ */
  387. 0x2517, 0x251b, /* ┗ ┛ */
  388. 0x2523, 0x252b, /* ┣ ┫ */
  389. 0x2552, 0x2555, /* ╒ ╕ */
  390. 0x2558, 0x255b, /* ╘ ╛ */
  391. 0x2553, 0x2556, /* ╓ ╖ */
  392. 0x2559, 0x255c, /* ╙ ╜ */
  393. 0x2554, 0x2557, /* ╔ ╗ */
  394. 0x255a, 0x255d, /* ╚ ╝ */
  395. 0x255e, 0x2561, /* ╞ ╡ */
  396. 0x255f, 0x2562, /* ╟ ╢ */
  397. 0x2560, 0x2563, /* ╠ ╣ */
  398. 0x2574, 0x2576, /* ╴ ╶ */
  399. 0x2578, 0x257a, /* ╸ ╺ */
  400. 0
  401. };
  402. for(i = 0; noflip[i]; i++)
  403. if(ch == noflip[i])
  404. return ch;
  405. for(i = 0; pairs[i]; i++)
  406. if(ch == pairs[i])
  407. return pairs[i ^ 1];
  408. return ch;
  409. }
  410. static uint32_t flopchar(uint32_t ch)
  411. {
  412. int i;
  413. static uint32_t const noflop[] =
  414. {
  415. /* ASCII */
  416. ' ', '(', ')', '*', '+', '-', '0', '3', '8', ':', '<', '=',
  417. '>', 'B', 'C', 'D', 'E', 'H', 'I', 'K', 'O', 'X', '[', ']',
  418. 'c', 'o', '{', '|', '}',
  419. /* CP437 and box drawing */
  420. 0x2591, 0x2592, 0x2593, 0x2588, 0x258c, 0x2590, /* ░ ▒ ▓ █ ▌ ▐ */
  421. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  422. 0x251c, 0x2524, 0x2523, 0x252b, 0x2560, 0x2563, /* ├ ┤ ┣ ┫ ╠ ╣ */
  423. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  424. 0x2574, 0x2576, 0x2578, 0x257a, /* ╴ ╶ ╸ ╺ */
  425. 0
  426. };
  427. static uint32_t const pairs[] =
  428. {
  429. /* ASCII */
  430. '/', '\\',
  431. 'M', 'W',
  432. ',', '`',
  433. 'b', 'p',
  434. 'd', 'q',
  435. 'p', 'q',
  436. 'f', 't',
  437. '.', '\'',
  438. /* ASCII-Unicode */
  439. '_', 0x203e, /* _ ‾ */
  440. '!', 0x00a1, /* ! ¡ */
  441. 'L', 0x0413, /* L Г */
  442. 'N', 0x0418, /* N И */
  443. 'P', 0x042c, /* P Ь */
  444. 'R', 0x0281, /* R ʁ */
  445. 'S', 0x01a7, /* S Ƨ */
  446. 'U', 0x0548, /* U Ո */
  447. 'V', 0x039b, /* V Λ */
  448. 'Y', 0x2144, /* Y ⅄ */
  449. 'h', 0x03bc, /* h μ */
  450. 'i', 0x1d09, /* i ᴉ */
  451. 'v', 0x028c, /* v ʌ */
  452. 'w', 0x028d, /* w ʍ */
  453. 'y', 0x03bb, /* y λ */
  454. /* Not perfect, but better than nothing */
  455. '"', 0x201e, /* " „ */
  456. 'm', 0x026f, /* m ɯ */
  457. 'n', 'u',
  458. /* CP437 */
  459. 0x2584, 0x2580, /* ▄ ▀ */
  460. 0x2596, 0x2598, /* ▖ ▘ */
  461. 0x2597, 0x259d, /* ▗ ▝ */
  462. 0x2599, 0x259b, /* ▙ ▛ */
  463. 0x259f, 0x259c, /* ▟ ▜ */
  464. 0x259a, 0x259e, /* ▚ ▞ */
  465. /* Box drawing */
  466. 0x250c, 0x2514, /* ┌ └ */
  467. 0x2510, 0x2518, /* ┐ ┘ */
  468. 0x252c, 0x2534, /* ┬ ┴ */
  469. 0x250f, 0x2517, /* ┏ ┗ */
  470. 0x2513, 0x251b, /* ┓ ┛ */
  471. 0x2533, 0x253b, /* ┳ ┻ */
  472. 0x2554, 0x255a, /* ╔ ╚ */
  473. 0x2557, 0x255d, /* ╗ ╝ */
  474. 0x2566, 0x2569, /* ╦ ╩ */
  475. 0x2552, 0x2558, /* ╒ ╘ */
  476. 0x2555, 0x255b, /* ╕ ╛ */
  477. 0x2564, 0x2567, /* ╤ ╧ */
  478. 0x2553, 0x2559, /* ╓ ╙ */
  479. 0x2556, 0x255c, /* ╖ ╜ */
  480. 0x2565, 0x2568, /* ╥ ╨ */
  481. 0x2575, 0x2577, /* ╵ ╷ */
  482. 0x2579, 0x257b, /* ╹ ╻ */
  483. 0
  484. };
  485. for(i = 0; noflop[i]; i++)
  486. if(ch == noflop[i])
  487. return ch;
  488. for(i = 0; pairs[i]; i++)
  489. if(ch == pairs[i])
  490. return pairs[i ^ 1];
  491. return ch;
  492. }
  493. static uint32_t rotatechar(uint32_t ch)
  494. {
  495. int i;
  496. static uint32_t const norotate[] =
  497. {
  498. /* ASCII */
  499. ' ', '*', '+', '-', '/', '0', '8', ':', '=', 'H', 'I', 'N',
  500. 'O', 'S', 'X', 'Z', '\\', 'l', 'o', 's', 'x', 'z', '|',
  501. /* Unicode */
  502. 0x2591, 0x2592, 0x2593, 0x2588, 0x259a, 0x259e, /* ░ ▒ ▓ █ ▚ ▞ */
  503. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  504. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  505. 0
  506. };
  507. static uint32_t const pairs[] =
  508. {
  509. /* ASCII */
  510. '(', ')',
  511. '<', '>',
  512. '[', ']',
  513. '{', '}',
  514. '.', '\'',
  515. '6', '9',
  516. 'M', 'W',
  517. 'b', 'q',
  518. 'd', 'p',
  519. 'n', 'u',
  520. /* ASCII-Unicode */
  521. '_', 0x203e, /* _ ‾ */
  522. ',', 0x00b4, /* , ´ */
  523. '`', 0x02ce, /* ` ˎ */
  524. '&', 0x214b, /* & ⅋ */
  525. '!', 0x00a1, /* ! ¡ */
  526. '?', 0x00bf, /* ? ¿ */
  527. 'C', 0x03fd, /* C Ͻ */
  528. 'E', 0x018e, /* E Ǝ */
  529. 'F', 0x2132, /* F Ⅎ */
  530. 'L', 0x2142, /* L ⅂ */
  531. 'U', 0x0548, /* U Ո */
  532. 'V', 0x039b, /* V Λ */
  533. 'Y', 0x2144, /* Y ⅄ */
  534. 'a', 0x0250, /* a ɐ */
  535. 'c', 0x0254, /* c ɔ */
  536. 'e', 0x0259, /* e ə */
  537. 'f', 0x025f, /* f ɟ */
  538. 'g', 0x1d77, /* g ᵷ */
  539. 'h', 0x0265, /* h ɥ */
  540. 'i', 0x1d09, /* i ᴉ */
  541. 'k', 0x029e, /* k ʞ */
  542. 'm', 0x026f, /* m ɯ */
  543. 'r', 0x0279, /* r ɹ */
  544. 't', 0x0287, /* t ʇ */
  545. 'v', 0x028c, /* v ʌ */
  546. 'w', 0x028d, /* w ʍ */
  547. 'y', 0x028e, /* y ʎ */
  548. /* Not perfect, but better than nothing */
  549. '"', 0x201e, /* " „ */
  550. /* Misc Unicode */
  551. 0x00e6, 0x1d02, /* æ ᴂ */
  552. 0x0153, 0x1d14, /* œ ᴔ */
  553. /* CP437 */
  554. 0x258c, 0x2590, /* ▌ ▐ */
  555. 0x2584, 0x2580, /* ▄ ▀ */
  556. 0x2596, 0x259d, /* ▖ ▝ */
  557. 0x2597, 0x2598, /* ▗ ▘ */
  558. 0x2599, 0x259c, /* ▙ ▜ */
  559. 0x259f, 0x259b, /* ▟ ▛ */
  560. /* Box drawing */
  561. 0x250c, 0x2518, /* ┌ ┘ */
  562. 0x2510, 0x2514, /* ┐ └ */
  563. 0x251c, 0x2524, /* ├ ┤ */
  564. 0x252c, 0x2534, /* ┬ ┴ */
  565. 0x250f, 0x251b, /* ┏ ┛ */
  566. 0x2513, 0x2517, /* ┓ ┗ */
  567. 0x2523, 0x252b, /* ┣ ┫ */
  568. 0x2533, 0x253b, /* ┳ ┻ */
  569. 0x2554, 0x255d, /* ╔ ╝ */
  570. 0x2557, 0x255a, /* ╗ ╚ */
  571. 0x2560, 0x2563, /* ╠ ╣ */
  572. 0x2566, 0x2569, /* ╦ ╩ */
  573. 0x2552, 0x255b, /* ╒ ╛ */
  574. 0x2555, 0x2558, /* ╕ ╘ */
  575. 0x255e, 0x2561, /* ╞ ╡ */
  576. 0x2564, 0x2567, /* ╤ ╧ */
  577. 0x2553, 0x255c, /* ╓ ╜ */
  578. 0x2556, 0x2559, /* ╖ ╙ */
  579. 0x255f, 0x2562, /* ╟ ╢ */
  580. 0x2565, 0x2568, /* ╥ ╨ */
  581. 0x2574, 0x2576, /* ╴ ╶ */
  582. 0x2575, 0x2577, /* ╵ ╷ */
  583. 0x2578, 0x257a, /* ╸ ╺ */
  584. 0x2579, 0x257b, /* ╹ ╻ */
  585. 0
  586. };
  587. for(i = 0; norotate[i]; i++)
  588. if(ch == norotate[i])
  589. return ch;
  590. for(i = 0; pairs[i]; i++)
  591. if(ch == pairs[i])
  592. return pairs[i ^ 1];
  593. return ch;
  594. }
  595. static uint32_t const leftright2[] =
  596. {
  597. /* ASCII */
  598. '/', '\\',
  599. '|', '-',
  600. '|', '_', /* This is all right because there was already a '|' before */
  601. /* ASCII-Unicode */
  602. '|', 0x203e, /* | ‾ */
  603. /* Misc Unicode */
  604. 0x2571, 0x2572, /* ╱ ╲ */
  605. /* Box drawing */
  606. 0x2500, 0x2502, /* ─ │ */
  607. 0x2501, 0x2503, /* ━ ┃ */
  608. 0x2550, 0x2551, /* ═ ║ */
  609. 0, 0
  610. };
  611. static uint32_t const leftright4[] =
  612. {
  613. /* ASCII */
  614. '<', 'v', '>', '^',
  615. ',', '.', '\'', '`',
  616. /* Misc Unicode */
  617. 0x256d, 0x2570, 0x256f, 0x256e, /* ╭ ╰ ╯ ╮ */
  618. /* CP437 */
  619. 0x258c, 0x2584, 0x2590, 0x2580, /* ▌ ▄ ▐ ▀ */
  620. 0x2596, 0x2597, 0x259d, 0x2598, /* ▖ ▗ ▝ ▘ */
  621. 0x2599, 0x259f, 0x259c, 0x259b, /* ▙ ▟ ▜ ▛ */
  622. /* Box drawing */
  623. 0x250c, 0x2514, 0x2518, 0x2510, /* ┌ └ ┘ ┐ */
  624. 0x250f, 0x2517, 0x251b, 0x2513, /* ┏ ┗ ┛ ┓ */
  625. 0x251c, 0x2534, 0x2524, 0x252c, /* ├ ┴ ┤ ┬ */
  626. 0x2523, 0x253b, 0x252b, 0x2533, /* ┣ ┻ ┫ ┳ */
  627. 0x2552, 0x2559, 0x255b, 0x2556, /* ╒ ╙ ╛ ╖ */
  628. 0x2553, 0x2558, 0x255c, 0x2555, /* ╓ ╘ ╜ ╕ */
  629. 0x2554, 0x255a, 0x255d, 0x2557, /* ╔ ╚ ╝ ╗ */
  630. 0x255e, 0x2568, 0x2561, 0x2565, /* ╞ ╨ ╡ ╥ */
  631. 0x255f, 0x2567, 0x2562, 0x2564, /* ╟ ╧ ╢ ╤ */
  632. 0x2560, 0x2569, 0x2563, 0x2566, /* ╠ ╩ ╣ ╦ */
  633. 0x2574, 0x2577, 0x2576, 0x2575, /* ╴ ╷ ╶ ╵ */
  634. 0x2578, 0x257b, 0x257a, 0x2579, /* ╸ ╻ ╺ ╹ */
  635. 0, 0, 0, 0
  636. };
  637. static uint32_t leftchar(uint32_t ch)
  638. {
  639. int i;
  640. for(i = 0; leftright2[i]; i++)
  641. if(ch == leftright2[i])
  642. return leftright2[(i & ~1) | ((i + 1) & 1)];
  643. for(i = 0; leftright4[i]; i++)
  644. if(ch == leftright4[i])
  645. return leftright4[(i & ~3) | ((i + 1) & 3)];
  646. return ch;
  647. }
  648. static uint32_t rightchar(uint32_t ch)
  649. {
  650. int i;
  651. for(i = 0; leftright2[i]; i++)
  652. if(ch == leftright2[i])
  653. return leftright2[(i & ~1) | ((i - 1) & 1)];
  654. for(i = 0; leftright4[i]; i++)
  655. if(ch == leftright4[i])
  656. return leftright4[(i & ~3) | ((i - 1) & 3)];
  657. return ch;
  658. }