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.

transform.c 13 KiB

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