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.
 
 
 
 
 
 

153 line
3.7 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 <stdlib.h>
  22. #include <cucul.h>
  23. static void list_fonts(void);
  24. static void add_char(unsigned int);
  25. cucul_font_t *f;
  26. cucul_canvas_t *out, *onechar;
  27. unsigned long int const *blocks;
  28. uint8_t * image;
  29. unsigned int width, height;
  30. int main(int argc, char *argv[])
  31. {
  32. unsigned int b, i;
  33. if(argc < 2)
  34. {
  35. fprintf(stderr, "Wrong argument count. Usage: caca2tlf <font>\n");
  36. list_fonts();
  37. return -1;
  38. }
  39. f = cucul_load_font(argv[1], 0);
  40. if(!f)
  41. {
  42. fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
  43. list_fonts();
  44. return -2;
  45. }
  46. width = cucul_get_font_width(f);
  47. height = cucul_get_font_height(f);
  48. blocks = cucul_get_font_blocks(f);
  49. onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
  50. cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
  51. out = cucul_create_canvas(width + 2, height);
  52. image = malloc(4 * width * height);
  53. printf("tlf2a$ %u %u %u 0 3 0 0 0\n", height, height - 1, width + 2);
  54. printf("================================================================================\n");
  55. printf(" This font was automatically generated by caca2tlf\n");
  56. printf("================================================================================\n");
  57. for(i = 32; i < 127; i++)
  58. add_char(i);
  59. add_char(196);
  60. add_char(214);
  61. add_char(220);
  62. add_char(228);
  63. add_char(246);
  64. add_char(252);
  65. add_char(223);
  66. for(b = 0, i = 0; blocks[i + 1]; i += 2)
  67. {
  68. int j, n = (int)(blocks[i + 1] - blocks[i]);
  69. for(j = 0; j < n; j++)
  70. {
  71. char buf[7];
  72. unsigned int len;
  73. unsigned long int ch = blocks[i] + j;
  74. if(ch < 0x80)
  75. continue;
  76. len = cucul_utf32_to_utf8(buf, ch);
  77. buf[len] = '\0';
  78. printf("0x%.04lX %s\n", ch, buf);
  79. add_char(ch);
  80. }
  81. }
  82. cucul_free_canvas(out);
  83. cucul_free_canvas(onechar);
  84. free(image);
  85. cucul_free_font(f);
  86. return 0;
  87. }
  88. static void list_fonts(void)
  89. {
  90. char const * const * fonts;
  91. unsigned int i;
  92. fprintf(stderr, "Available fonts:\n");
  93. fonts = cucul_get_font_list();
  94. for(i = 0; fonts[i]; i++)
  95. fprintf(stderr, " \"%s\"\n", fonts[i]);
  96. }
  97. static void add_char(unsigned int ch)
  98. {
  99. cucul_buffer_t *buf;
  100. unsigned int x, y;
  101. cucul_putchar(onechar, 0, 0, ch);
  102. cucul_render_canvas(onechar, f, image, width, height, 4 * width);
  103. for(y = 0; y < height; y++)
  104. for(x = 0; x < width; x++)
  105. {
  106. uint8_t c = image[4 * (x + y * width) + 2];
  107. if(c >= 0xa0)
  108. cucul_putstr(out, x, y, "█");
  109. else if(c >= 0x80)
  110. cucul_putstr(out, x, y, "▓");
  111. else if(c >= 0x40)
  112. cucul_putstr(out, x, y, "▒");
  113. else if(c >= 0x20)
  114. cucul_putstr(out, x, y, "░");
  115. else
  116. cucul_putchar(out, x, y, ' ');
  117. }
  118. cucul_draw_line(out, width, 0, width, height - 1, "@");
  119. cucul_putchar(out, width + 1, height - 1, '@');
  120. buf = cucul_export_canvas(out, "utf8");
  121. fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);
  122. cucul_free_buffer(buf);
  123. }