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.
 
 
 
 
 
 

292 regels
7.3 KiB

  1. /*
  2. * caca2tlf Create a TOIlet font from a libcaca font
  3. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program 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 is the main program entry point.
  16. */
  17. #include "config.h"
  18. #if defined(HAVE_INTTYPES_H)
  19. # include <inttypes.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <caca.h>
  25. enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
  26. enum charset { ASCII, UTF8 } charset;
  27. static void list_fonts(void);
  28. static void add_char(unsigned long int);
  29. caca_font_t *f;
  30. caca_canvas_t *out, *onechar;
  31. uint32_t const *blocks;
  32. uint8_t * image;
  33. unsigned int w, h, gw, fgw, gh, iw, ih;
  34. int main(int argc, char *argv[])
  35. {
  36. char *flag1, *flag2;
  37. unsigned int b, i;
  38. if(argc < 2)
  39. {
  40. fprintf(stderr,
  41. "Usage: %s [--half|--quarter] [--ascii|--utf8] <font>\n",
  42. argv[0]);
  43. list_fonts();
  44. return -1;
  45. }
  46. if((!strcmp(argv[1], "--half") || !strcmp(argv[1], "-h")) && argc > 2)
  47. {
  48. flag1 = "--half ";
  49. mode = HALFBLOCKS;
  50. argv++; argc--;
  51. }
  52. else if((!strcmp(argv[1], "--quarter") || !strcmp(argv[1], "-q")) && argc > 2)
  53. {
  54. flag1 = "--quarter ";
  55. mode = QUARTERBLOCKS;
  56. argv++; argc--;
  57. }
  58. else
  59. {
  60. flag1 = "";
  61. mode = GRAY;
  62. }
  63. if((!strcmp(argv[1], "--ascii") || !strcmp(argv[1], "-a")) && argc > 2)
  64. {
  65. flag2 = "--ascii ";
  66. charset = ASCII;
  67. argv++; argc--;
  68. }
  69. else if((!strcmp(argv[1], "--utf8") || !strcmp(argv[1], "-u")) && argc > 2)
  70. {
  71. flag2 = "--utf8 ";
  72. charset = UTF8;
  73. argv++; argc--;
  74. }
  75. else
  76. {
  77. flag2 = "";
  78. charset = ASCII;
  79. }
  80. f = caca_load_font(argv[1], 0);
  81. if(!f)
  82. {
  83. fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
  84. list_fonts();
  85. return -2;
  86. }
  87. w = caca_get_font_width(f);
  88. h = caca_get_font_height(f);
  89. iw = w * 2 + 1;
  90. ih = h + 1;
  91. switch(mode)
  92. {
  93. case GRAY:
  94. gw = w;
  95. fgw = w * 2;
  96. gh = h;
  97. break;
  98. case HALFBLOCKS:
  99. gw = w;
  100. fgw = w * 2;
  101. gh = (h + 1) / 2;
  102. break;
  103. case QUARTERBLOCKS:
  104. gw = (w + 1) / 2;
  105. fgw = (w * 2 + 1) / 2;
  106. gh = (h + 1) / 2;
  107. break;
  108. }
  109. blocks = caca_get_font_blocks(f);
  110. onechar = caca_create_canvas(0, 0);
  111. caca_set_color_ansi(onechar, CACA_WHITE, CACA_BLACK);
  112. image = malloc(4 * iw * ih);
  113. out = caca_create_canvas(0, 0);
  114. printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
  115. printf("=============================================="
  116. "==================================\n");
  117. printf(" This font was automatically generated using:\n");
  118. printf(" %% caca2tlf %s%s\"%s\"\n", flag1, flag2, argv[1]);
  119. printf("=============================================="
  120. "==================================\n");
  121. for(i = 32; i < 127; i++)
  122. add_char(i);
  123. add_char(196);
  124. add_char(214);
  125. add_char(220);
  126. add_char(228);
  127. add_char(246);
  128. add_char(252);
  129. add_char(223);
  130. for(b = 0, i = 0; blocks[i + 1]; i += 2)
  131. {
  132. int j, n = (int)(blocks[i + 1] - blocks[i]);
  133. for(j = 0; j < n; j++)
  134. {
  135. char buf[7];
  136. unsigned int len;
  137. unsigned long int ch = blocks[i] + j;
  138. if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
  139. || ch == 228 || ch == 246 || ch == 252 || ch == 223)
  140. continue;
  141. len = caca_utf32_to_utf8(buf, ch);
  142. buf[len] = '\0';
  143. printf("0x%.04lX %s\n", ch, buf);
  144. add_char(ch);
  145. }
  146. }
  147. caca_free_canvas(out);
  148. caca_free_canvas(onechar);
  149. free(image);
  150. caca_free_font(f);
  151. return 0;
  152. }
  153. static void list_fonts(void)
  154. {
  155. char const * const * fonts;
  156. unsigned int i;
  157. fprintf(stderr, "Available fonts:\n");
  158. fonts = caca_get_font_list();
  159. for(i = 0; fonts[i]; i++)
  160. fprintf(stderr, " \"%s\"\n", fonts[i]);
  161. }
  162. static void add_char(unsigned long int ch)
  163. {
  164. static char const * chars[][16] =
  165. {
  166. { "#", "$", ":", ".", " " },
  167. { " ", "\"", "m", "#" },
  168. { " ", "`", "'", "\"", ",", "[", "/", "P",
  169. ".", "\\", "]", "T", "m", "b", "d", "W" },
  170. { "█", "▓", "▒", "░", " " },
  171. { " ", "▀", "▄", "█" },
  172. { " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
  173. "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█" }
  174. };
  175. char const **str;
  176. void *buf;
  177. size_t len;
  178. unsigned int x, y, myw, mygw;
  179. int off = 0;
  180. int full = caca_utf32_is_fullwidth(ch);
  181. caca_set_canvas_size(onechar, full ? 2 : 1, 1);
  182. caca_put_char(onechar, 0, 0, ch);
  183. caca_render_canvas(onechar, f, image, iw, ih, 4 * iw);
  184. myw = full ? 2 * w : w;
  185. mygw = full ? fgw : gw;
  186. caca_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
  187. caca_clear_canvas(out);
  188. switch(charset)
  189. {
  190. case ASCII:
  191. off = 0;
  192. break;
  193. case UTF8:
  194. off = 3;
  195. break;
  196. }
  197. switch(mode)
  198. {
  199. case GRAY:
  200. str = chars[off];
  201. for(y = 0; y < h; y++)
  202. for(x = 0; x < myw; x++)
  203. {
  204. uint8_t c = image[4 * (x + y * iw) + 2];
  205. if(c >= 0xa0)
  206. caca_put_str(out, x, y, str[0]);
  207. else if(c >= 0x80)
  208. caca_put_str(out, x, y, str[1]);
  209. else if(c >= 0x40)
  210. caca_put_str(out, x, y, str[2]);
  211. else if(c >= 0x20)
  212. caca_put_str(out, x, y, str[3]);
  213. else
  214. caca_put_str(out, x, y, str[4]);
  215. }
  216. break;
  217. case HALFBLOCKS:
  218. str = chars[off + 1];
  219. for(y = 0; y < gh; y++)
  220. for(x = 0; x < mygw; x++)
  221. {
  222. uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
  223. uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
  224. caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
  225. }
  226. break;
  227. case QUARTERBLOCKS:
  228. str = chars[off + 2];
  229. for(y = 0; y < gh; y++)
  230. for(x = 0; x < mygw; x++)
  231. {
  232. uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
  233. uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
  234. uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
  235. uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
  236. caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
  237. 4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
  238. }
  239. break;
  240. }
  241. if(ch == ' ' || ch == 0xa0)
  242. {
  243. caca_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
  244. caca_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
  245. }
  246. caca_draw_line(out, mygw, 0, mygw, gh - 1, '@');
  247. caca_put_char(out, mygw + 1, gh - 1, '@');
  248. buf = caca_export_memory(out, "utf8", &len);
  249. fwrite(buf, len, 1, stdout);
  250. free(buf);
  251. }