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.
 
 
 
 
 
 

167 líneas
3.4 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 text to canvas rendering functions.
  14. */
  15. #include "config.h"
  16. #if defined(HAVE_INTTYPES_H)
  17. # include <inttypes.h>
  18. #endif
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <caca.h>
  23. #include "toilet.h"
  24. #include "render.h"
  25. #include "filter.h"
  26. static int render_flush(context_t *);
  27. int render_init(context_t *cx)
  28. {
  29. cx->x = cx->y = 0;
  30. cx->w = cx->h = 0;
  31. cx->lines = 0;
  32. cx->cv = caca_create_canvas(0, 0);
  33. if(!strcasecmp(cx->font, "term"))
  34. return init_tiny(cx);
  35. return init_figlet(cx);
  36. }
  37. int render_stdin(context_t *cx)
  38. {
  39. caca_canvas_t *cv;
  40. char *line;
  41. int i, len;
  42. /* FIXME: we can't read longer lines */
  43. len = 1024;
  44. line = malloc(len);
  45. cv = caca_create_canvas(0, 0);
  46. /* Read from stdin */
  47. while(!feof(stdin))
  48. {
  49. if(!fgets(line, len, stdin))
  50. break;
  51. caca_set_canvas_size(cv, 0, 0);
  52. caca_import_canvas_from_memory(cv, line, strlen(line), "utf8");
  53. for(i = 0; i < caca_get_canvas_width(cv); i++)
  54. {
  55. uint32_t ch = caca_get_char(cv, i, 0);
  56. uint32_t at = caca_get_attr(cv, i, 0);
  57. cx->feed(cx, ch, at);
  58. if(caca_utf32_is_fullwidth(ch)) i++;
  59. }
  60. render_flush(cx);
  61. }
  62. free(line);
  63. return 0;
  64. }
  65. int render_list(context_t *cx, int argc, char *argv[])
  66. {
  67. caca_canvas_t *cv;
  68. int i, j, len;
  69. char *parser = NULL;
  70. cv = caca_create_canvas(0, 0);
  71. for(j = 0; j < argc; )
  72. {
  73. char *cr;
  74. if(!parser)
  75. {
  76. if(j)
  77. cx->feed(cx, ' ', 0);
  78. parser = argv[j];
  79. }
  80. cr = strchr(parser, '\n');
  81. if(cr)
  82. len = (cr - parser) + 1;
  83. else
  84. len = strlen(parser);
  85. caca_set_canvas_size(cv, 0, 0);
  86. caca_import_canvas_from_memory(cv, parser, len, "utf8");
  87. for(i = 0; i < caca_get_canvas_width(cv); i++)
  88. {
  89. uint32_t ch = caca_get_char(cv, i, 0);
  90. uint32_t at = caca_get_attr(cv, i, 0);
  91. cx->feed(cx, ch, at);
  92. if(caca_utf32_is_fullwidth(ch)) i++;
  93. }
  94. if(cr)
  95. {
  96. parser += len;
  97. render_flush(cx);
  98. }
  99. else
  100. {
  101. parser = NULL;
  102. j++;
  103. }
  104. }
  105. render_flush(cx);
  106. caca_free_canvas(cv);
  107. return 0;
  108. }
  109. int render_end(context_t *cx)
  110. {
  111. cx->end(cx);
  112. caca_free_canvas(cx->cv);
  113. return 0;
  114. }
  115. /* XXX: Following functions are local */
  116. static int render_flush(context_t *cx)
  117. {
  118. size_t len;
  119. void *buffer;
  120. /* Flush current line */
  121. cx->flush(cx);
  122. /* Apply optional effects to our string */
  123. filter_do(cx);
  124. /* Output line */
  125. buffer = caca_export_canvas_to_memory(cx->torender, cx->export, &len);
  126. if(!buffer)
  127. return -1;
  128. fwrite(buffer, len, 1, stdout);
  129. free(buffer);
  130. caca_free_canvas(cx->torender);
  131. return 0;
  132. }