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.
 
 
 
 
 
 

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