Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

294 wiersze
7.7 KiB

  1. /*
  2. * TOIlet The Other Implementation’s letters
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program 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. #include "config.h"
  14. #include "common.h"
  15. #if !defined(__KERNEL__)
  16. # if defined(HAVE_INTTYPES_H)
  17. # include <inttypes.h>
  18. # endif
  19. # if defined(HAVE_GETOPT_H)
  20. # include <getopt.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <string.h>
  24. # include <stdlib.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "caca.h"
  28. /* String to canvas transformations */
  29. static cucul_canvas_t *cuculize_big(uint32_t const *, unsigned int);
  30. static cucul_canvas_t *cuculize_tiny(uint32_t const *, unsigned int);
  31. /* Canvas special effects */
  32. static void filter_autocrop(cucul_canvas_t *);
  33. static void filter_gay(cucul_canvas_t *);
  34. int main(int argc, char *argv[])
  35. {
  36. cucul_canvas_t *cv;
  37. cucul_buffer_t *buffer;
  38. uint32_t *string = NULL;
  39. unsigned int length;
  40. int i;
  41. char const *export = "utf8";
  42. unsigned flag_gay = 0;
  43. #if defined(HAVE_GETOPT_H)
  44. for(;;)
  45. {
  46. # ifdef HAVE_GETOPT_LONG
  47. # define MOREINFO "Try `%s --help' for more information.\n"
  48. int option_index = 0;
  49. static struct option long_options[] =
  50. {
  51. /* Long option, needs arg, flag, short option */
  52. { "gay", 0, NULL, 'g' },
  53. { "irc", 0, NULL, 'i' },
  54. { "help", 0, NULL, 'h' },
  55. { "version", 0, NULL, 'v' },
  56. { NULL, 0, NULL, 0 }
  57. };
  58. int c = getopt_long(argc, argv, "gihv", long_options, &option_index);
  59. # else
  60. # define MOREINFO "Try `%s -h' for more information.\n"
  61. int c = getopt(argc, argv, "gihv");
  62. # endif
  63. if(c == -1)
  64. break;
  65. switch(c)
  66. {
  67. case 'h': /* --help */
  68. printf("Usage: %s [ -gihv ] [ message ]\n", argv[0]);
  69. # ifdef HAVE_GETOPT_LONG
  70. printf(" -g, --gay add a rainbow effect to the text\n");
  71. printf(" -i, --irc output IRC colour codes\n");
  72. printf(" -h, --help display this help and exit\n");
  73. printf(" -v, --version output version information and exit\n");
  74. # else
  75. printf(" -g add a rainbow effect to the text\n");
  76. printf(" -i output IRC colour codes\n");
  77. printf(" -h display this help and exit\n");
  78. printf(" -v output version information and exit\n");
  79. # endif
  80. return 0;
  81. case 'g': /* --gay */
  82. flag_gay = 1;
  83. break;
  84. case 'i': /* --irc */
  85. export = "irc";
  86. break;
  87. case 'v': /* --version */
  88. printf("TOIlet Copyright 2006 Sam Hocevar %s\n", VERSION);
  89. printf("Internet: <sam@zoy.org> Version: 0, date: 21 Sep 2006\n");
  90. printf("\n");
  91. return 0;
  92. case '?':
  93. printf(MOREINFO, argv[0]);
  94. return 1;
  95. default:
  96. printf("%s: invalid option -- %i\n", argv[0], c);
  97. printf(MOREINFO, argv[0]);
  98. return 1;
  99. }
  100. }
  101. #else
  102. # define MOREINFO "Usage: %s message...\n"
  103. int optind = 1;
  104. #endif
  105. if(optind >= argc)
  106. {
  107. printf("%s: too few arguments\n", argv[0]);
  108. printf(MOREINFO, argv[0]);
  109. return 1;
  110. }
  111. /* Load rest of commandline into a UTF-32 string */
  112. for(i = optind, length = 0; i < argc; i++)
  113. {
  114. unsigned int k, guessed_len, real_len;
  115. guessed_len = strlen(argv[i]);
  116. if(i > optind)
  117. string[length++] = (uint32_t)' ';
  118. string = realloc(string, (length + guessed_len + 1) * 4);
  119. for(k = 0, real_len = 0; k < guessed_len; real_len++)
  120. {
  121. unsigned int char_len;
  122. string[length + real_len] =
  123. cucul_utf8_to_utf32(argv[i] + k, &char_len);
  124. k += char_len;
  125. }
  126. length += real_len;
  127. }
  128. /* Do gay stuff with our string (léopard) */
  129. cv = cuculize_big(string, length);
  130. filter_autocrop(cv);
  131. if(flag_gay)
  132. filter_gay(cv);
  133. /* Output char */
  134. buffer = cucul_export_canvas(cv, export);
  135. fwrite(cucul_get_buffer_data(buffer),
  136. cucul_get_buffer_size(buffer), 1, stdout);
  137. cucul_free_buffer(buffer);
  138. cucul_free_canvas(cv);
  139. return 0;
  140. }
  141. static cucul_canvas_t *cuculize_big(uint32_t const *string,
  142. unsigned int length)
  143. {
  144. cucul_canvas_t *cv;
  145. cucul_font_t *f;
  146. char const * const * fonts;
  147. unsigned char *buf;
  148. unsigned int w, h, x, y, miny, maxy;
  149. cv = cucul_create_canvas(length, 1);
  150. for(x = 0; x < length; x++)
  151. cucul_putchar(cv, x, 0, string[x]);
  152. fonts = cucul_get_font_list();
  153. f = cucul_load_font(fonts[0], 0);
  154. /* Create our bitmap buffer (32-bit ARGB) */
  155. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  156. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  157. buf = malloc(4 * w * h);
  158. /* Render the canvas onto our image buffer */
  159. cucul_render_canvas(cv, f, buf, w, h, 4 * w);
  160. /* Free our canvas, and allocate a bigger one */
  161. cucul_free_font(f);
  162. cucul_free_canvas(cv);
  163. cv = cucul_create_canvas(w, h);
  164. /* Render the image buffer on the canvas */
  165. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT);
  166. cucul_clear_canvas(cv);
  167. miny = h; maxy = 0;
  168. for(y = 0; y < h; y++)
  169. for(x = 0; x < w; x++)
  170. {
  171. unsigned char c = buf[4 * (x + y * w) + 2];
  172. if(c >= 0xa0)
  173. cucul_putstr(cv, x, y, "█");
  174. else if(c >= 0x80)
  175. cucul_putstr(cv, x, y, "▓");
  176. else if(c >= 0x40)
  177. cucul_putstr(cv, x, y, "▒");
  178. else if(c >= 0x20)
  179. cucul_putstr(cv, x, y, "░");
  180. }
  181. free(buf);
  182. return cv;
  183. }
  184. static cucul_canvas_t *cuculize_tiny(uint32_t const *string,
  185. unsigned int length)
  186. {
  187. unsigned int x;
  188. cucul_canvas_t *cv = cucul_create_canvas(length, 1);
  189. for(x = 0; x < length; x++)
  190. cucul_putchar(cv, x, 0, string[x]);
  191. return cv;
  192. }
  193. static void filter_autocrop(cucul_canvas_t *cv)
  194. {
  195. unsigned int x, y, w, h;
  196. unsigned int xmin, xmax, ymin, ymax;
  197. xmin = w = cucul_get_canvas_width(cv);
  198. xmax = 0;
  199. ymin = h = cucul_get_canvas_height(cv);
  200. ymax = 0;
  201. for(y = 0; y < h; y++)
  202. for(x = 0; x < w; x++)
  203. {
  204. unsigned long int ch = cucul_getchar(cv, x, y);
  205. if(ch != (unsigned char)' ')
  206. {
  207. if(x < xmin)
  208. xmin = x;
  209. if(x > xmax)
  210. xmax = x;
  211. if(y < ymin)
  212. ymin = y;
  213. if(y > ymax)
  214. ymax = y;
  215. }
  216. }
  217. cucul_set_canvas_boundaries(cv, xmin, ymin,
  218. xmax - xmin + 1, ymax - ymin + 1);
  219. }
  220. static void filter_gay(cucul_canvas_t *cv)
  221. {
  222. static unsigned char const rainbow[] =
  223. {
  224. CUCUL_COLOR_LIGHTMAGENTA,
  225. CUCUL_COLOR_LIGHTRED,
  226. CUCUL_COLOR_YELLOW,
  227. CUCUL_COLOR_LIGHTGREEN,
  228. CUCUL_COLOR_LIGHTCYAN,
  229. CUCUL_COLOR_LIGHTBLUE,
  230. };
  231. unsigned int x, y, w, h;
  232. w = cucul_get_canvas_width(cv);
  233. h = cucul_get_canvas_height(cv);
  234. for(y = 0; y < h; y++)
  235. for(x = 0; x < w; x++)
  236. {
  237. unsigned long int ch = cucul_getchar(cv, x, y);
  238. if(ch != (unsigned char)' ')
  239. {
  240. cucul_set_color(cv, rainbow[(x / 2 + y) % 6],
  241. CUCUL_COLOR_TRANSPARENT);
  242. cucul_putchar(cv, x, y, ch);
  243. }
  244. }
  245. }