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

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