25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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