No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

117 líneas
2.9 KiB

  1. /*
  2. * TOIlet The Other Implementation’s letters
  3. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /*
  13. * This file contains functions for handling FIGlet fonts.
  14. */
  15. #include "config.h"
  16. #if defined(HAVE_INTTYPES_H)
  17. # include <inttypes.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <dirent.h>
  23. #include <caca.h>
  24. #include "toilet.h"
  25. #include "render.h"
  26. #define STD_GLYPHS (127 - 32)
  27. #define EXT_GLYPHS (STD_GLYPHS + 7)
  28. static int feed_figlet(context_t *, uint32_t, uint32_t);
  29. static int flush_figlet(context_t *);
  30. static int end_figlet(context_t *);
  31. void list_dir_fonts(char const *dir)
  32. {
  33. DIR *d;
  34. struct dirent *dr;
  35. int n = 0;
  36. d = opendir(dir);
  37. while(d && (dr = readdir(d)) != NULL)
  38. {
  39. size_t len = strlen(dr->d_name);
  40. if(len<5)
  41. continue;
  42. if(!strcmp(dr->d_name + len - 4, ".flf") || !strcmp(dr->d_name + len - 4, ".tlf"))
  43. {
  44. if(!n++)
  45. printf("- %s:", dir);
  46. printf(" %s", dr->d_name);
  47. }
  48. }
  49. if(n)
  50. printf("\n");
  51. closedir(d);
  52. }
  53. int list_fonts(char const *dir)
  54. {
  55. list_dir_fonts(dir);
  56. if(strcmp(dir, getenv("PWD")))
  57. list_dir_fonts(getenv("PWD"));
  58. return 0;
  59. }
  60. int init_figlet(context_t *cx)
  61. {
  62. char path[2048];
  63. snprintf(path, 2047, "%s/%s", cx->dir, cx->font);
  64. if(caca_canvas_set_figfont(cx->cv, path))
  65. {
  66. snprintf(path, 2047, "./%s", cx->font);
  67. if(caca_canvas_set_figfont(cx->cv, path))
  68. {
  69. fprintf(stderr, "error: could not load font %s\n", cx->font);
  70. return -1;
  71. }
  72. }
  73. caca_set_figfont_smush(cx->cv, cx->hmode);
  74. caca_set_figfont_width(cx->cv, cx->term_width);
  75. cx->feed = feed_figlet;
  76. cx->flush = flush_figlet;
  77. cx->end = end_figlet;
  78. return 0;
  79. }
  80. static int feed_figlet(context_t *cx, uint32_t ch, uint32_t attr)
  81. {
  82. return caca_put_figchar(cx->cv, ch);
  83. }
  84. static int flush_figlet(context_t *cx)
  85. {
  86. /* We copy cx->cv into cx->torender instead of swapping pointers
  87. * because that would lose the figfont information. */
  88. /* FIXME: use caca_copy_canvas() or whatever when it's implemented. */
  89. int ret = caca_flush_figlet(cx->cv);
  90. cx->torender = caca_create_canvas(caca_get_canvas_width(cx->cv),
  91. caca_get_canvas_height(cx->cv));
  92. caca_blit(cx->torender, 0, 0, cx->cv, NULL);
  93. caca_set_canvas_size(cx->cv, 0, 0);
  94. return ret;
  95. }
  96. static int end_figlet(context_t *cx)
  97. {
  98. return caca_canvas_set_figfont(cx->cv, NULL);
  99. }