Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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