Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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