Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

152 linhas
3.3 KiB

  1. /*
  2. * TOIlet The Other Implementation’s letters
  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 file contains text to canvas rendering functions.
  15. */
  16. #include "config.h"
  17. #if defined(HAVE_INTTYPES_H)
  18. # include <inttypes.h>
  19. #endif
  20. #include <stdlib.h>
  21. #include <cucul.h>
  22. #include "toilet.h"
  23. #include "render.h"
  24. static int feed_tiny(context_t *, uint32_t);
  25. static int end_tiny(context_t *);
  26. int init_tiny(context_t *cx)
  27. {
  28. cx->ew = 16;
  29. cx->eh = 2;
  30. cx->x = cx->y = 0;
  31. cx->w = cx->h = 0;
  32. cx->cv = cucul_create_canvas(cx->ew, cx->eh);
  33. cx->feed = feed_tiny;
  34. cx->end = end_tiny;
  35. return 0;
  36. }
  37. static int feed_tiny(context_t *cx, uint32_t ch)
  38. {
  39. if(cx->x >= cx->w)
  40. cx->w = cx->x + 1;
  41. if(cx->y >= cx->h)
  42. cx->h = cx->y + 1;
  43. switch(ch)
  44. {
  45. case (uint32_t)'\r':
  46. return 0;
  47. case (uint32_t)'\n':
  48. cx->x = 0;
  49. cx->y++;
  50. break;
  51. case (uint32_t)'\t':
  52. cx->x = (cx->x & ~7) + 8;
  53. break;
  54. default:
  55. cucul_putchar(cx->cv, cx->x, cx->y, ch);
  56. cx->x++;
  57. break;
  58. }
  59. if(cx->x >= cx->term_width)
  60. {
  61. cx->x = 0;
  62. cx->y++;
  63. }
  64. if(cx->x >= cx->ew)
  65. cx->ew = cx->ew + cx->ew / 2;
  66. if(cx->y >= cx->eh)
  67. cx->eh = cx->eh + cx->eh / 2;
  68. cucul_set_canvas_size(cx->cv, cx->ew, cx->eh);
  69. return 0;
  70. }
  71. static int end_tiny(context_t *cx)
  72. {
  73. cucul_set_canvas_size(cx->cv, cx->w, cx->h);
  74. return 0;
  75. }
  76. #if 0
  77. cucul_canvas_t *render_big(uint32_t const *string, unsigned int length)
  78. {
  79. cucul_canvas_t *cv;
  80. cucul_font_t *f;
  81. char const * const * fonts;
  82. unsigned char *buf;
  83. unsigned int w, h, x, y, miny, maxy;
  84. cv = cucul_create_canvas(length, 1);
  85. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
  86. for(x = 0; x < length; x++)
  87. cucul_putchar(cv, x, 0, string[x]);
  88. fonts = cucul_get_font_list();
  89. f = cucul_load_font(fonts[0], 0);
  90. /* Create our bitmap buffer (32-bit ARGB) */
  91. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  92. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  93. buf = malloc(4 * w * h);
  94. /* Render the canvas onto our image buffer */
  95. cucul_render_canvas(cv, f, buf, w, h, 4 * w);
  96. /* Free our canvas, and allocate a bigger one */
  97. cucul_free_font(f);
  98. cucul_free_canvas(cv);
  99. cv = cucul_create_canvas(w, h);
  100. /* Render the image buffer on the canvas */
  101. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT);
  102. cucul_clear_canvas(cv);
  103. miny = h; maxy = 0;
  104. for(y = 0; y < h; y++)
  105. for(x = 0; x < w; x++)
  106. {
  107. unsigned char c = buf[4 * (x + y * w) + 2];
  108. if(c >= 0xa0)
  109. cucul_putstr(cv, x, y, "█");
  110. else if(c >= 0x80)
  111. cucul_putstr(cv, x, y, "▓");
  112. else if(c >= 0x40)
  113. cucul_putstr(cv, x, y, "▒");
  114. else if(c >= 0x20)
  115. cucul_putstr(cv, x, y, "░");
  116. }
  117. free(buf);
  118. return cv;
  119. }
  120. #endif