25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

253 satır
6.4 KiB

  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 <caca.h>
  25. enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
  26. static void list_fonts(void);
  27. static void add_char(unsigned long int);
  28. caca_font_t *f;
  29. caca_canvas_t *out, *onechar;
  30. uint32_t 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 = caca_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 = caca_get_font_width(f);
  69. h = caca_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 = caca_get_font_blocks(f);
  91. onechar = caca_create_canvas(0, 0);
  92. caca_set_color_ansi(onechar, CACA_WHITE, CACA_BLACK);
  93. image = malloc(4 * iw * ih);
  94. out = caca_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 = caca_utf32_to_utf8(buf, ch);
  123. buf[len] = '\0';
  124. printf("0x%.04lX %s\n", ch, buf);
  125. add_char(ch);
  126. }
  127. }
  128. caca_free_canvas(out);
  129. caca_free_canvas(onechar);
  130. free(image);
  131. caca_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 = caca_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 = caca_utf32_is_fullwidth(ch);
  149. caca_set_canvas_size(onechar, full ? 2 : 1, 1);
  150. caca_put_char(onechar, 0, 0, ch);
  151. caca_render_canvas(onechar, f, image, iw, ih, 4 * iw);
  152. myw = full ? 2 * w : w;
  153. mygw = full ? fgw : gw;
  154. caca_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. caca_put_str(out, x, y, "█");
  164. else if(c >= 0x80)
  165. caca_put_str(out, x, y, "▓");
  166. else if(c >= 0x40)
  167. caca_put_str(out, x, y, "▒");
  168. else if(c >= 0x20)
  169. caca_put_str(out, x, y, "░");
  170. else
  171. caca_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. caca_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. caca_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. caca_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
  205. caca_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
  206. }
  207. caca_draw_line(out, mygw, 0, mygw, gh - 1, '@');
  208. caca_put_char(out, mygw + 1, gh - 1, '@');
  209. buf = caca_export_memory(out, "utf8", &len);
  210. fwrite(buf, len, 1, stdout);
  211. free(buf);
  212. }