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.
 
 
 
 
 
 

471 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 and box drawing */
  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. 0x2575, 0x2577, 0x2579, 0x257b, /* ╵ ╷ ╹ ╻ */
  200. 0
  201. };
  202. static uint32_t const pairs[] =
  203. {
  204. /* ASCII */
  205. '(', ')',
  206. '/', '\\',
  207. '<', '>',
  208. '[', ']',
  209. 'b', 'd',
  210. 'p', 'q',
  211. '{', '}',
  212. /* ASCII-Unicode */
  213. ';', 0x204f, /* ; ⁏ */
  214. '`', 0x00b4, /* ` ´ */
  215. ',', 0x02ce, /* , ˎ */
  216. 'C', 0x03fd, /* C Ͻ */
  217. 'E', 0x018e, /* E Ǝ */
  218. 'N', 0x0418, /* N И */
  219. 'R', 0x042f, /* R Я */
  220. 'S', 0x01a7, /* S Ƨ */
  221. 'c', 0x0254, /* c ɔ */
  222. 'e', 0x0258, /* e ɘ */
  223. /* CP437 */
  224. 0x258c, 0x2590, /* ▌ ▐ */
  225. 0x2596, 0x2597, /* ▖ ▗ */
  226. 0x2598, 0x259d, /* ▘ ▝ */
  227. 0x2599, 0x259f, /* ▙ ▟ */
  228. 0x259a, 0x259e, /* ▚ ▞ */
  229. 0x259b, 0x259c, /* ▛ ▜ */
  230. 0x25ba, 0x25c4, /* ► ◄ */
  231. 0x2192, 0x2190, /* → ← */
  232. 0x2310, 0xac, /* ⌐ ¬ */
  233. /* Box drawing */
  234. 0x250c, 0x2510, /* ┌ ┐ */
  235. 0x2514, 0x2518, /* └ ┘ */
  236. 0x251c, 0x2524, /* ├ ┤ */
  237. 0x250f, 0x2513, /* ┏ ┓ */
  238. 0x2517, 0x251b, /* ┗ ┛ */
  239. 0x2523, 0x252b, /* ┣ ┫ */
  240. 0x2554, 0x2557, /* ╔ ╗ */
  241. 0x255a, 0x255d, /* ╚ ╝ */
  242. 0x2560, 0x2563, /* ╠ ╣ */
  243. 0x2574, 0x2576, /* ╴ ╶ */
  244. 0x2578, 0x257a, /* ╸ ╺ */
  245. 0
  246. };
  247. for(i = 0; noflip[i]; i++)
  248. if(ch == noflip[i])
  249. return ch;
  250. for(i = 0; pairs[i]; i++)
  251. if(ch == pairs[i])
  252. return pairs[i ^ 1];
  253. return ch;
  254. }
  255. static uint32_t flopchar(uint32_t ch)
  256. {
  257. int i;
  258. static uint32_t const noflop[] =
  259. {
  260. /* ASCII */
  261. ' ', '(', ')', '*', '+', '-', '0', '3', '8', ':', '<', '=',
  262. '>', 'B', 'C', 'D', 'E', 'H', 'I', 'K', 'O', 'X', '[', ']',
  263. 'c', 'o', '{', '|', '}',
  264. /* CP437 and box drawing */
  265. 0x2591, 0x2592, 0x2593, 0x2588, 0x258c, 0x2590, /* ░ ▒ ▓ █ ▌ ▐ */
  266. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  267. 0x251c, 0x2524, 0x2523, 0x252b, 0x2560, 0x2563, /* ├ ┤ ┣ ┫ ╠ ╣ */
  268. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  269. 0x2574, 0x2576, 0x2578, 0x257a, /* ╴ ╶ ╸ ╺ */
  270. 0
  271. };
  272. static uint32_t const pairs[] =
  273. {
  274. /* ASCII */
  275. '/', '\\',
  276. 'M', 'W',
  277. ',', '`',
  278. 'b', 'p',
  279. 'd', 'q',
  280. 'p', 'q',
  281. 'f', 't',
  282. '.', '\'',
  283. /* ASCII-Unicode */
  284. '_', 0x203e, /* _ ‾ */
  285. '!', 0x00a1, /* ! ¡ */
  286. 'L', 0x0413, /* L Г */
  287. 'N', 0x0418, /* N И */
  288. 'P', 0x042c, /* P Ь */
  289. 'R', 0x0281, /* R ʁ */
  290. 'S', 0x01a7, /* S Ƨ */
  291. 'U', 0x0548, /* U Ո */
  292. 'V', 0x039b, /* V Λ */
  293. 'h', 0x03bc, /* h μ */
  294. 'i', 0x1d09, /* i ᴉ */
  295. 'v', 0x028c, /* v ʌ */
  296. 'w', 0x028d, /* w ʍ */
  297. 'y', 0x03bb, /* y λ */
  298. /* Not perfect, but better than nothing */
  299. '"', 0x201e, /* " „ */
  300. 'm', 0x026f, /* m ɯ */
  301. 'n', 'u',
  302. /* CP437 */
  303. 0x2584, 0x2580, /* ▄ ▀ */
  304. 0x2596, 0x2598, /* ▖ ▘ */
  305. 0x2597, 0x259d, /* ▗ ▝ */
  306. 0x2599, 0x259b, /* ▙ ▛ */
  307. 0x259f, 0x259c, /* ▟ ▜ */
  308. 0x259a, 0x259e, /* ▚ ▞ */
  309. /* Box drawing */
  310. 0x250c, 0x2514, /* ┌ └ */
  311. 0x2510, 0x2518, /* ┐ ┘ */
  312. 0x252c, 0x2534, /* ┬ ┴ */
  313. 0x250f, 0x2517, /* ┏ ┗ */
  314. 0x2513, 0x251b, /* ┓ ┛ */
  315. 0x2533, 0x253b, /* ┳ ┻ */
  316. 0x2554, 0x255a, /* ╔ ╚ */
  317. 0x2557, 0x255d, /* ╗ ╝ */
  318. 0x2566, 0x2569, /* ╦ ╩ */
  319. 0x2575, 0x2577, /* ╵ ╷ */
  320. 0x2579, 0x257b, /* ╹ ╻ */
  321. 0
  322. };
  323. for(i = 0; noflop[i]; i++)
  324. if(ch == noflop[i])
  325. return ch;
  326. for(i = 0; pairs[i]; i++)
  327. if(ch == pairs[i])
  328. return pairs[i ^ 1];
  329. return ch;
  330. }
  331. static uint32_t rotatechar(uint32_t ch)
  332. {
  333. int i;
  334. static uint32_t const norotate[] =
  335. {
  336. /* ASCII */
  337. ' ', '*', '+', '-', '/', '0', '8', ':', '=', 'H', 'I', 'N',
  338. 'O', 'S', 'X', 'Z', '\\', 'l', 'o', 's', 'x', 'z', '|',
  339. /* Unicode */
  340. 0x2591, 0x2592, 0x2593, 0x2588, 0x259a, 0x259e, /* ░ ▒ ▓ █ ▚ ▞ */
  341. 0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
  342. 0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
  343. 0
  344. };
  345. static uint32_t const pairs[] =
  346. {
  347. /* ASCII */
  348. '(', ')',
  349. '<', '>',
  350. '[', ']',
  351. '{', '}',
  352. '.', '\'',
  353. '6', '9',
  354. 'M', 'W',
  355. 'b', 'q',
  356. 'd', 'p',
  357. 'n', 'u',
  358. /* ASCII-Unicode */
  359. '_', 0x203e, /* _ ‾ */
  360. ',', 0x00b4, /* , ´ */
  361. '`', 0x02ce, /* ` ˎ */
  362. '&', 0x214b, /* & ⅋ */
  363. '!', 0x00a1, /* ! ¡ */
  364. '?', 0x00bf, /* ? ¿ */
  365. 'C', 0x03fd, /* C Ͻ */
  366. 'E', 0x018e, /* E Ǝ */
  367. 'F', 0x2132, /* F Ⅎ */
  368. 'U', 0x0548, /* U Ո */
  369. 'V', 0x039b, /* V Λ */
  370. 'a', 0x0250, /* a ɐ */
  371. 'c', 0x0254, /* c ɔ */
  372. 'e', 0x0259, /* e ə */
  373. 'f', 0x025f, /* f ɟ */
  374. 'g', 0x1d77, /* g ᵷ */
  375. 'h', 0x0265, /* h ɥ */
  376. 'i', 0x1d09, /* i ᴉ */
  377. 'k', 0x029e, /* k ʞ */
  378. 'm', 0x026f, /* m ɯ */
  379. 'r', 0x0279, /* r ɹ */
  380. 't', 0x0287, /* t ʇ */
  381. 'v', 0x028c, /* v ʌ */
  382. 'w', 0x028d, /* w ʍ */
  383. 'y', 0x028e, /* y ʎ */
  384. /* Not perfect, but better than nothing */
  385. '"', 0x201e, /* " „ */
  386. /* CP437 */
  387. 0x258c, 0x2590, /* ▌ ▐ */
  388. 0x2584, 0x2580, /* ▄ ▀ */
  389. 0x2596, 0x259d, /* ▖ ▝ */
  390. 0x2597, 0x2598, /* ▗ ▘ */
  391. 0x2599, 0x259c, /* ▙ ▜ */
  392. 0x259f, 0x259b, /* ▟ ▛ */
  393. /* Box drawing */
  394. 0x250c, 0x2518, /* ┌ ┘ */
  395. 0x2510, 0x2514, /* ┐ └ */
  396. 0x251c, 0x2524, /* ├ ┤ */
  397. 0x252c, 0x2534, /* ┬ ┴ */
  398. 0x250f, 0x251b, /* ┏ ┛ */
  399. 0x2513, 0x2517, /* ┓ ┗ */
  400. 0x2523, 0x252b, /* ┣ ┫ */
  401. 0x2533, 0x253b, /* ┳ ┻ */
  402. 0x2554, 0x255d, /* ╔ ╝ */
  403. 0x2557, 0x255a, /* ╗ ╚ */
  404. 0x2560, 0x2563, /* ╠ ╣ */
  405. 0x2566, 0x2569, /* ╦ ╩ */
  406. 0x2574, 0x2576, /* ╴ ╶ */
  407. 0x2575, 0x2577, /* ╵ ╷ */
  408. 0x2578, 0x257a, /* ╸ ╺ */
  409. 0x2579, 0x257b, /* ╹ ╻ */
  410. 0
  411. };
  412. for(i = 0; norotate[i]; i++)
  413. if(ch == norotate[i])
  414. return ch;
  415. for(i = 0; pairs[i]; i++)
  416. if(ch == pairs[i])
  417. return pairs[i ^ 1];
  418. return ch;
  419. }