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.
 
 
 
 
 
 

237 rivejä
6.0 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 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, 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 + 1;
  70. ih = h + 1;
  71. switch(mode)
  72. {
  73. case GRAY:
  74. gw = w;
  75. gh = h;
  76. break;
  77. case HALFBLOCKS:
  78. gw = w;
  79. gh = (h + 1) / 2;
  80. break;
  81. case QUARTERBLOCKS:
  82. gw = (w + 1) / 2;
  83. gh = (h + 1) / 2;
  84. break;
  85. }
  86. blocks = cucul_get_font_blocks(f);
  87. onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
  88. cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
  89. image = malloc(4 * iw * ih);
  90. out = cucul_create_canvas(gw + 2, gh);
  91. printf("tlf2a$ %u %u %u 0 4 0 0 0\n", gh, gh - 1, gw + 2);
  92. printf("=============================================="
  93. "==================================\n");
  94. printf(" This font was automatically generated using:\n");
  95. printf(" %% caca2tlf %s\"%s\"\n", extraflag, fontname);
  96. printf("=============================================="
  97. "==================================\n");
  98. for(i = 32; i < 127; i++)
  99. add_char(i);
  100. add_char(196);
  101. add_char(214);
  102. add_char(220);
  103. add_char(228);
  104. add_char(246);
  105. add_char(252);
  106. add_char(223);
  107. for(b = 0, i = 0; blocks[i + 1]; i += 2)
  108. {
  109. int j, n = (int)(blocks[i + 1] - blocks[i]);
  110. for(j = 0; j < n; j++)
  111. {
  112. char buf[7];
  113. unsigned int len;
  114. unsigned long int ch = blocks[i] + j;
  115. if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
  116. || ch == 228 || ch == 246 || ch == 252 || ch == 223)
  117. continue;
  118. len = cucul_utf32_to_utf8(buf, ch);
  119. buf[len] = '\0';
  120. printf("0x%.04lX %s\n", ch, buf);
  121. add_char(ch);
  122. }
  123. }
  124. cucul_free_canvas(out);
  125. cucul_free_canvas(onechar);
  126. free(image);
  127. cucul_free_font(f);
  128. return 0;
  129. }
  130. static void list_fonts(void)
  131. {
  132. char const * const * fonts;
  133. unsigned int i;
  134. fprintf(stderr, "Available fonts:\n");
  135. fonts = cucul_get_font_list();
  136. for(i = 0; fonts[i]; i++)
  137. fprintf(stderr, " \"%s\"\n", fonts[i]);
  138. }
  139. static void add_char(unsigned int ch)
  140. {
  141. cucul_buffer_t *buf;
  142. unsigned int x, y;
  143. cucul_putchar(onechar, 0, 0, ch);
  144. cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
  145. switch(mode)
  146. {
  147. case GRAY:
  148. for(y = 0; y < h; y++)
  149. for(x = 0; x < w; x++)
  150. {
  151. uint8_t c = image[4 * (x + y * iw) + 2];
  152. if(c >= 0xa0)
  153. cucul_putstr(out, x, y, "█");
  154. else if(c >= 0x80)
  155. cucul_putstr(out, x, y, "▓");
  156. else if(c >= 0x40)
  157. cucul_putstr(out, x, y, "▒");
  158. else if(c >= 0x20)
  159. cucul_putstr(out, x, y, "░");
  160. else
  161. cucul_putchar(out, x, y, ' ');
  162. }
  163. break;
  164. case HALFBLOCKS:
  165. for(y = 0; y < gh; y++)
  166. for(x = 0; x < gw; x++)
  167. {
  168. static char const *str[] = { " ", "▀", "▄", "█" };
  169. uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
  170. uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
  171. cucul_putstr(out, x, y,
  172. str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2)]);
  173. }
  174. break;
  175. case QUARTERBLOCKS:
  176. for(y = 0; y < gh; y++)
  177. for(x = 0; x < gw; x++)
  178. {
  179. static char const *str[] =
  180. {
  181. " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
  182. "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
  183. };
  184. uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
  185. uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
  186. uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
  187. uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
  188. cucul_putstr(out, x, y,
  189. str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2) +
  190. (p3 < 0x80 ? 0 : 4) + (p4 < 0x80 ? 0 : 8)]);
  191. }
  192. break;
  193. }
  194. cucul_draw_line(out, gw, 0, gw, gh - 1, "@");
  195. cucul_putchar(out, gw + 1, gh - 1, '@');
  196. buf = cucul_export_canvas(out, "utf8");
  197. fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);
  198. cucul_free_buffer(buf);
  199. }