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.

преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * caca2tlf Create a TOIlet font from a libcaca font
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  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 <cucul.h>
  25. enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
  26. static void list_fonts(void);
  27. static void add_char(unsigned long int);
  28. cucul_font_t *f;
  29. cucul_canvas_t *out, *onechar;
  30. unsigned long int const *blocks;
  31. uint8_t * image;
  32. unsigned int w, h, gw, fgw, gh, iw, ih;
  33. int main(int argc, char *argv[])
  34. {
  35. char *fontname, *extraflag;
  36. unsigned int b, i;
  37. if(argc < 2)
  38. {
  39. fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
  40. list_fonts();
  41. return -1;
  42. }
  43. if(!strcmp(argv[1], "--half") && argc >= 3)
  44. {
  45. extraflag = "--half ";
  46. mode = HALFBLOCKS;
  47. fontname = argv[2];
  48. }
  49. else if(!strcmp(argv[1], "--quarter") && argc >= 3)
  50. {
  51. extraflag = "--quarter ";
  52. mode = QUARTERBLOCKS;
  53. fontname = argv[2];
  54. }
  55. else
  56. {
  57. extraflag = "";
  58. mode = GRAY;
  59. fontname = argv[1];
  60. }
  61. f = cucul_load_font(fontname, 0);
  62. if(!f)
  63. {
  64. fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
  65. list_fonts();
  66. return -2;
  67. }
  68. w = cucul_get_font_width(f);
  69. h = cucul_get_font_height(f);
  70. iw = w * 2 + 1;
  71. ih = h + 1;
  72. switch(mode)
  73. {
  74. case GRAY:
  75. gw = w;
  76. fgw = w * 2;
  77. gh = h;
  78. break;
  79. case HALFBLOCKS:
  80. gw = w;
  81. fgw = w * 2;
  82. gh = (h + 1) / 2;
  83. break;
  84. case QUARTERBLOCKS:
  85. gw = (w + 1) / 2;
  86. fgw = (w * 2 + 1) / 2;
  87. gh = (h + 1) / 2;
  88. break;
  89. }
  90. blocks = cucul_get_font_blocks(f);
  91. onechar = cucul_create_canvas(0, 0);
  92. cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
  93. image = malloc(4 * iw * ih);
  94. out = cucul_create_canvas(0, 0);
  95. printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
  96. printf("=============================================="
  97. "==================================\n");
  98. printf(" This font was automatically generated using:\n");
  99. printf(" %% caca2tlf %s\"%s\"\n", extraflag, fontname);
  100. printf("=============================================="
  101. "==================================\n");
  102. for(i = 32; i < 127; i++)
  103. add_char(i);
  104. add_char(196);
  105. add_char(214);
  106. add_char(220);
  107. add_char(228);
  108. add_char(246);
  109. add_char(252);
  110. add_char(223);
  111. for(b = 0, i = 0; blocks[i + 1]; i += 2)
  112. {
  113. int j, n = (int)(blocks[i + 1] - blocks[i]);
  114. for(j = 0; j < n; j++)
  115. {
  116. char buf[7];
  117. unsigned int len;
  118. unsigned long int ch = blocks[i] + j;
  119. if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
  120. || ch == 228 || ch == 246 || ch == 252 || ch == 223)
  121. continue;
  122. len = cucul_utf32_to_utf8(buf, ch);
  123. buf[len] = '\0';
  124. printf("0x%.04lX %s\n", ch, buf);
  125. add_char(ch);
  126. }
  127. }
  128. cucul_free_canvas(out);
  129. cucul_free_canvas(onechar);
  130. free(image);
  131. cucul_free_font(f);
  132. return 0;
  133. }
  134. static void list_fonts(void)
  135. {
  136. char const * const * fonts;
  137. unsigned int i;
  138. fprintf(stderr, "Available fonts:\n");
  139. fonts = cucul_get_font_list();
  140. for(i = 0; fonts[i]; i++)
  141. fprintf(stderr, " \"%s\"\n", fonts[i]);
  142. }
  143. static void add_char(unsigned long int ch)
  144. {
  145. void *buf;
  146. unsigned long int len;
  147. unsigned int x, y, myw, mygw;
  148. int full = cucul_utf32_is_fullwidth(ch);
  149. cucul_set_canvas_size(onechar, full ? 2 : 1, 1);
  150. cucul_put_char(onechar, 0, 0, ch);
  151. cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
  152. myw = full ? 2 * w : w;
  153. mygw = full ? fgw : gw;
  154. cucul_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
  155. switch(mode)
  156. {
  157. case GRAY:
  158. for(y = 0; y < h; y++)
  159. for(x = 0; x < myw; x++)
  160. {
  161. uint8_t c = image[4 * (x + y * iw) + 2];
  162. if(c >= 0xa0)
  163. cucul_put_str(out, x, y, "█");
  164. else if(c >= 0x80)
  165. cucul_put_str(out, x, y, "▓");
  166. else if(c >= 0x40)
  167. cucul_put_str(out, x, y, "▒");
  168. else if(c >= 0x20)
  169. cucul_put_str(out, x, y, "░");
  170. else
  171. cucul_put_char(out, x, y, ' ');
  172. }
  173. break;
  174. case HALFBLOCKS:
  175. for(y = 0; y < gh; y++)
  176. for(x = 0; x < mygw; x++)
  177. {
  178. static char const *str[] = { " ", "▀", "▄", "█" };
  179. uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
  180. uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
  181. cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
  182. }
  183. break;
  184. case QUARTERBLOCKS:
  185. for(y = 0; y < gh; y++)
  186. for(x = 0; x < mygw; x++)
  187. {
  188. static char const *str[] =
  189. {
  190. " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
  191. "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
  192. };
  193. uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
  194. uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
  195. uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
  196. uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
  197. cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
  198. 4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
  199. }
  200. break;
  201. }
  202. if(ch == ' ' || ch == 0xa0)
  203. {
  204. cucul_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
  205. cucul_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
  206. }
  207. cucul_draw_line(out, mygw, 0, mygw, gh - 1, '@');
  208. cucul_put_char(out, mygw + 1, gh - 1, '@');
  209. buf = cucul_export_memory(out, "utf8", &len);
  210. fwrite(buf, len, 1, stdout);
  211. free(buf);
  212. }