您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

109 行
2.3 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 <caca.h>
  21. #include "toilet.h"
  22. #include "render.h"
  23. static int feed_tiny(context_t *, uint32_t, uint32_t);
  24. static int flush_tiny(context_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->feed = feed_tiny;
  31. cx->flush = flush_tiny;
  32. cx->end = end_tiny;
  33. return 0;
  34. }
  35. static int feed_tiny(context_t *cx, uint32_t ch, uint32_t attr)
  36. {
  37. switch(ch)
  38. {
  39. case (uint32_t)'\r':
  40. return 0;
  41. case (uint32_t)'\n':
  42. cx->x = 0;
  43. cx->y++;
  44. return 0;
  45. case (uint32_t)'\t':
  46. cx->x = (cx->x & ~7) + 8;
  47. return 0;
  48. }
  49. /* Check whether we reached the end of the screen */
  50. if(cx->x && cx->x + 1 > cx->term_width)
  51. {
  52. cx->x = 0;
  53. cx->y++;
  54. }
  55. /* Check whether the current canvas is large enough */
  56. if(cx->x + 1 > cx->w)
  57. {
  58. cx->w = cx->x + 1 < cx->term_width ? cx->x + 1 : cx->term_width;
  59. if(cx->w > cx->ew)
  60. cx->ew = cx->ew + cx->ew / 2;
  61. }
  62. if(cx->y + 1 > cx->h)
  63. {
  64. cx->h = cx->y + 1;
  65. if(cx->h > cx->eh)
  66. cx->eh = cx->eh + cx->eh / 2;
  67. }
  68. caca_set_attr(cx->cv, attr);
  69. caca_set_canvas_size(cx->cv, cx->ew, cx->eh);
  70. caca_put_char(cx->cv, cx->x, cx->y, ch);
  71. cx->x++;
  72. return 0;
  73. }
  74. static int flush_tiny(context_t *cx)
  75. {
  76. cx->torender = cx->cv;
  77. caca_set_canvas_size(cx->torender, cx->w, cx->h);
  78. cx->ew = 16;
  79. cx->eh = 2;
  80. cx->x = cx->y = 0;
  81. cx->w = cx->h = 0;
  82. cx->cv = caca_create_canvas(cx->ew, cx->eh);
  83. return 0;
  84. }
  85. static int end_tiny(context_t *cx)
  86. {
  87. return 0;
  88. }