450 行
12 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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains horizontal and vertical flipping routines.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. #endif
  20. #include "cucul.h"
  21. #include "cucul_internals.h"
  22. static uint32_t flipchar(uint32_t ch);
  23. static uint32_t flopchar(uint32_t ch);
  24. static uint32_t rotatechar(uint32_t ch);
  25. /** \brief Invert a canvas' colours.
  26. *
  27. * This function inverts a canvas' colours (black becomes white, red
  28. * becomes cyan, etc.) without changing the characters in it.
  29. *
  30. * This function never fails.
  31. *
  32. * \param cv The canvas to invert.
  33. * \return This function always returns 0.
  34. */
  35. int cucul_invert(cucul_canvas_t *cv)
  36. {
  37. uint32_t *attr = cv->attr;
  38. unsigned int i;
  39. for(i = cv->height * cv->width; i--; )
  40. {
  41. *attr = *attr ^ 0x000f000f;
  42. attr++;
  43. }
  44. return 0;
  45. }
  46. /** \brief Flip a canvas horizontally.
  47. *
  48. * This function flips a canvas horizontally, choosing characters that
  49. * look like the mirrored version wherever possible.
  50. *
  51. * This function never fails.
  52. *
  53. * \param cv The canvas to flip.
  54. * \return This function always returns 0.
  55. */
  56. int cucul_flip(cucul_canvas_t *cv)
  57. {
  58. unsigned int y;
  59. for(y = 0; y < cv->height; y++)
  60. {
  61. uint32_t *cleft = cv->chars + y * cv->width;
  62. uint32_t *cright = cleft + cv->width - 1;
  63. uint32_t *aleft = cv->attr + y * cv->width;
  64. uint32_t *aright = aleft + cv->width - 1;
  65. while(cleft < cright)
  66. {
  67. uint32_t ch;
  68. uint32_t attr;
  69. /* Swap attributes */
  70. attr = *aright;
  71. *aright-- = *aleft;
  72. *aleft++ = attr;
  73. /* Swap characters */
  74. ch = *cright;
  75. *cright-- = flipchar(*cleft);
  76. *cleft++ = flipchar(ch);
  77. }
  78. if(cleft == cright)
  79. *cleft = flipchar(*cleft);
  80. cleft = cv->chars + y * cv->width;
  81. cright = cleft + cv->width - 1;
  82. for( ; cleft < cright; cleft++)
  83. {
  84. if(cleft[0] == CUCUL_MAGIC_FULLWIDTH)
  85. {
  86. cleft[0] = cleft[1];
  87. cleft[1] = CUCUL_MAGIC_FULLWIDTH;
  88. cleft++;
  89. }
  90. }
  91. }
  92. return 0;
  93. }
  94. /** \brief Flip a canvas vertically.
  95. *
  96. * This function flips a canvas vertically, choosing characters that
  97. * look like the mirrored version wherever possible.
  98. *
  99. * This function never fails.
  100. *
  101. * \param cv The canvas to flop.
  102. * \return This function always returns 0.
  103. */
  104. int cucul_flop(cucul_canvas_t *cv)
  105. {
  106. unsigned int x;
  107. for(x = 0; x < cv->width; x++)
  108. {
  109. uint32_t *ctop = cv->chars + x;
  110. uint32_t *cbottom = ctop + cv->width * (cv->height - 1);
  111. uint32_t *atop = cv->attr + x;
  112. uint32_t *abottom = atop + cv->width * (cv->height - 1);
  113. while(ctop < cbottom)
  114. {
  115. uint32_t ch;
  116. uint32_t attr;
  117. /* Swap attributes */
  118. attr = *abottom; *abottom = *atop; *atop = attr;
  119. /* Swap characters */
  120. ch = *cbottom; *cbottom = flopchar(*ctop); *ctop = flopchar(ch);
  121. ctop += cv->width; cbottom -= cv->width;
  122. atop += cv->width; abottom -= cv->width;
  123. }
  124. if(ctop == cbottom)
  125. *ctop = flopchar(*ctop);
  126. }
  127. return 0;
  128. }
  129. /** \brief Rotate a canvas.
  130. *
  131. * This function applies a 180 degrees transformation to a canvas,
  132. * choosing characters that look like the mirrored version wherever
  133. * possible.
  134. *
  135. * This function never fails.
  136. *
  137. * \param cv The canvas to rotate.
  138. * \return This function always returns 0.
  139. */
  140. int cucul_rotate(cucul_canvas_t *cv)
  141. {
  142. uint32_t *cbegin = cv->chars;
  143. uint32_t *cend = cbegin + cv->width * cv->height - 1;
  144. uint32_t *abegin = cv->attr;
  145. uint32_t *aend = abegin + cv->width * cv->height - 1;
  146. unsigned int y;
  147. while(cbegin < cend)
  148. {
  149. uint32_t ch;
  150. uint32_t attr;
  151. /* Swap attributes */
  152. attr = *aend; *aend = *abegin; *abegin = attr;
  153. /* Swap characters */
  154. ch = *cend; *cend = rotatechar(*cbegin); *cbegin = rotatechar(ch);
  155. cbegin++; cend--; abegin++; aend--;
  156. }
  157. if(cbegin == cend)
  158. *cbegin = rotatechar(*cbegin);
  159. for(y = 0; y < cv->height; y++)
  160. {
  161. cbegin = cv->chars + y * cv->width;
  162. cend = cbegin + cv->width - 1;
  163. for( ; cbegin < cend; cbegin++)
  164. {
  165. if(cbegin[0] == CUCUL_MAGIC_FULLWIDTH)
  166. {
  167. cbegin[0] = cbegin[1];
  168. cbegin[1] = CUCUL_MAGIC_FULLWIDTH;
  169. cbegin++;
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. /* FIXME: as the lookup tables grow bigger, use a log(n) lookup instead
  176. * of linear lookup. */
  177. static uint32_t flipchar(uint32_t ch)
  178. {
  179. int i;
  180. static uint32_t const noflip[] =
  181. {
  182. /* ASCII */
  183. ' ', '"', '#', '\'', '-', '.', '*', '+', ':', '=', '0', '8',
  184. 'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y', '^',
  185. '_', 'i', 'o', 'v', 'w', 'x', '|',
  186. /* CP437 */
  187. 0x2591, 0x2592, 0x2593, 0x2588, 0x2584, 0x2580, /* ░ ▒ ▓ █ ▄ ▀ */
  188. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  189. 0x252c, 0x2534, 0x2533, 0x253b, 0x2566, 0x2569, /* ┬ ┴ ┳ ┻ ╦ ╩ */
  190. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  191. 0
  192. };
  193. static uint32_t const pairs[] =
  194. {
  195. /* ASCII */
  196. '(', ')',
  197. '/', '\\',
  198. '<', '>',
  199. '[', ']',
  200. 'b', 'd',
  201. 'p', 'q',
  202. '{', '}',
  203. /* ASCII-Unicode */
  204. ';', 0x204f, /* ; ⁏ */
  205. '`', 0x00b4, /* ` ´ */
  206. ',', 0x02ce, /* , ˎ */
  207. 'C', 0x03fd, /* C Ͻ */
  208. 'E', 0x018e, /* E Ǝ */
  209. 'N', 0x0418, /* N И */
  210. 'R', 0x042f, /* R Я */
  211. 'S', 0x01a7, /* S Ƨ */
  212. 'c', 0x0254, /* c ɔ */
  213. 'e', 0x0258, /* e ɘ */
  214. /* CP437 */
  215. 0x258c, 0x2590, /* ▌ ▐ */
  216. 0x2596, 0x2597, /* ▖ ▗ */
  217. 0x2598, 0x259d, /* ▘ ▝ */
  218. 0x2599, 0x259f, /* ▙ ▟ */
  219. 0x259a, 0x259e, /* ▚ ▞ */
  220. 0x259b, 0x259c, /* ▛ ▜ */
  221. 0x25ba, 0x25c4, /* ► ◄ */
  222. 0x2192, 0x2190, /* → ← */
  223. 0x2310, 0xac, /* ⌐ ¬ */
  224. /* Box drawing */
  225. 0x250c, 0x2510, /* ┌ ┐ */
  226. 0x2514, 0x2518, /* └ ┘ */
  227. 0x251c, 0x2524, /* ├ ┤ */
  228. 0x250f, 0x2513, /* ┏ ┓ */
  229. 0x2517, 0x251b, /* ┗ ┛ */
  230. 0x2523, 0x252b, /* ┣ ┫ */
  231. 0x2554, 0x2557, /* ╔ ╗ */
  232. 0x255a, 0x255d, /* ╚ ╝ */
  233. 0x2560, 0x2563, /* ╠ ╣ */
  234. 0
  235. };
  236. for(i = 0; noflip[i]; i++)
  237. if(ch == noflip[i])
  238. return ch;
  239. for(i = 0; pairs[i]; i++)
  240. if(ch == pairs[i])
  241. return pairs[i ^ 1];
  242. return ch;
  243. }
  244. static uint32_t flopchar(uint32_t ch)
  245. {
  246. int i;
  247. static uint32_t const noflop[] =
  248. {
  249. /* ASCII */
  250. ' ', '(', ')', '*', '+', '-', '0', '3', '8', ':', '<', '=',
  251. '>', 'B', 'C', 'D', 'E', 'H', 'I', 'K', 'O', 'X', '[', ']',
  252. 'c', 'o', '{', '|', '}',
  253. /* CP437 */
  254. 0x2591, 0x2592, 0x2593, 0x2588, 0x258c, 0x2590, /* ░ ▒ ▓ █ ▌ ▐ */
  255. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  256. 0x251c, 0x2524, 0x2523, 0x252b, 0x2560, 0x2563, /* ├ ┤ ┣ ┫ ╠ ╣ */
  257. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  258. 0
  259. };
  260. static uint32_t const pairs[] =
  261. {
  262. /* ASCII */
  263. '/', '\\',
  264. 'M', 'W',
  265. ',', '`',
  266. 'b', 'p',
  267. 'd', 'q',
  268. 'p', 'q',
  269. 'f', 't',
  270. '.', '\'',
  271. /* ASCII-Unicode */
  272. '_', 0x203e, /* _ ‾ */
  273. '!', 0x00a1, /* ! ¡ */
  274. 'L', 0x0413, /* L Г */
  275. 'N', 0x0418, /* N И */
  276. 'P', 0x042c, /* P Ь */
  277. 'R', 0x0281, /* R ʁ */
  278. 'S', 0x01a7, /* S Ƨ */
  279. 'U', 0x0548, /* U Ո */
  280. 'V', 0x039b, /* V Λ */
  281. 'h', 0x03bc, /* h μ */
  282. 'i', 0x1d09, /* i ᴉ */
  283. 'v', 0x028c, /* v ʌ */
  284. 'w', 0x028d, /* w ʍ */
  285. 'y', 0x03bb, /* y λ */
  286. /* Not perfect, but better than nothing */
  287. 'm', 0x026f, /* m ɯ */
  288. 'n', 'u',
  289. /* CP437 */
  290. 0x2584, 0x2580, /* ▄ ▀ */
  291. 0x2596, 0x2598, /* ▖ ▘ */
  292. 0x2597, 0x259d, /* ▗ ▝ */
  293. 0x2599, 0x259b, /* ▙ ▛ */
  294. 0x259f, 0x259c, /* ▟ ▜ */
  295. 0x259a, 0x259e, /* ▚ ▞ */
  296. /* Box drawing */
  297. 0x250c, 0x2514, /* ┌ └ */
  298. 0x2510, 0x2518, /* ┐ ┘ */
  299. 0x252c, 0x2534, /* ┬ ┴ */
  300. 0x250f, 0x2517, /* ┏ ┗ */
  301. 0x2513, 0x251b, /* ┓ ┛ */
  302. 0x2533, 0x253b, /* ┳ ┻ */
  303. 0x2554, 0x255a, /* ╔ ╚ */
  304. 0x2557, 0x255d, /* ╗ ╝ */
  305. 0x2566, 0x2569, /* ╦ ╩ */
  306. 0
  307. };
  308. for(i = 0; noflop[i]; i++)
  309. if(ch == noflop[i])
  310. return ch;
  311. for(i = 0; pairs[i]; i++)
  312. if(ch == pairs[i])
  313. return pairs[i ^ 1];
  314. return ch;
  315. }
  316. static uint32_t rotatechar(uint32_t ch)
  317. {
  318. int i;
  319. static uint32_t const norotate[] =
  320. {
  321. /* ASCII */
  322. ' ', '*', '+', '-', '/', '0', '8', ':', '=', 'H', 'I', 'N',
  323. 'O', 'S', 'X', 'Z', '\\', 'l', 'o', 's', 'u', 'x', 'z', '|',
  324. /* Unicode */
  325. 0x2591, 0x2592, 0x2593, 0x2588, 0x259a, 0x259e, /* ░ ▒ ▓ █ ▚ ▞ */
  326. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  327. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  328. 0
  329. };
  330. static uint32_t const pairs[] =
  331. {
  332. /* ASCII */
  333. '(', ')',
  334. '<', '>',
  335. '[', ']',
  336. '{', '}',
  337. '.', '\'',
  338. '6', '9',
  339. 'M', 'W',
  340. 'b', 'q',
  341. 'd', 'p',
  342. 'n', 'u',
  343. /* ASCII-Unicode */
  344. '_', 0x203e, /* _ ‾ */
  345. ',', 0x00b4, /* , ´ */
  346. '`', 0x02ce, /* ` ˎ */
  347. '&', 0x214b, /* & ⅋ */
  348. '!', 0x00a1, /* ! ¡ */
  349. '?', 0x00bf, /* ? ¿ */
  350. 'C', 0x03fd, /* C Ͻ */
  351. 'E', 0x018e, /* E Ǝ */
  352. 'F', 0x2132, /* F Ⅎ */
  353. 'U', 0x0548, /* U Ո */
  354. 'V', 0x039b, /* V Λ */
  355. 'a', 0x0250, /* a ɐ */
  356. 'c', 0x0254, /* c ɔ */
  357. 'e', 0x0259, /* e ə */
  358. 'f', 0x025f, /* f ɟ */
  359. 'g', 0x1d77, /* g ᵷ */
  360. 'h', 0x0265, /* h ɥ */
  361. 'i', 0x1d09, /* i ᴉ */
  362. 'k', 0x029e, /* k ʞ */
  363. 'm', 0x026f, /* m ɯ */
  364. 'r', 0x0279, /* r ɹ */
  365. 't', 0x0287, /* t ʇ */
  366. 'v', 0x028c, /* v ʌ */
  367. 'w', 0x028d, /* w ʍ */
  368. 'y', 0x028e, /* y ʎ */
  369. /* CP437 */
  370. 0x258c, 0x2590, /* ▌ ▐ */
  371. 0x2584, 0x2580, /* ▄ ▀ */
  372. 0x2596, 0x259d, /* ▖ ▝ */
  373. 0x2597, 0x2598, /* ▗ ▘ */
  374. 0x2599, 0x259c, /* ▙ ▜ */
  375. 0x259f, 0x259b, /* ▟ ▛ */
  376. /* Box drawing */
  377. 0x250c, 0x2518, /* ┌ ┘ */
  378. 0x2510, 0x2514, /* ┐ └ */
  379. 0x251c, 0x2524, /* ├ ┤ */
  380. 0x252c, 0x2534, /* ┬ ┴ */
  381. 0x250f, 0x251b, /* ┏ ┛ */
  382. 0x2513, 0x2517, /* ┓ ┗ */
  383. 0x2523, 0x252b, /* ┣ ┫ */
  384. 0x2533, 0x253b, /* ┳ ┻ */
  385. 0x2554, 0x255d, /* ╔ ╝ */
  386. 0x2557, 0x255a, /* ╗ ╚ */
  387. 0x2560, 0x2563, /* ╠ ╣ */
  388. 0x2566, 0x2569, /* ╦ ╩ */
  389. 0
  390. };
  391. for(i = 0; norotate[i]; i++)
  392. if(ch == norotate[i])
  393. return ch;
  394. for(i = 0; pairs[i]; i++)
  395. if(ch == pairs[i])
  396. return pairs[i ^ 1];
  397. return ch;
  398. }