Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

19 лет назад
19 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * libcucul Unicode canvas library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file export.c
  12. * \version \$Id: export.c 361 2006-03-09 13:24:06Z jylam $
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  15. * \brief Export function
  16. *
  17. * This file contains export functions for ANSI
  18. */
  19. #include "config.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <stdio.h>
  23. # include <string.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /** \brief Generate ANSI representation of current image.
  28. *
  29. * This function generates and returns an ANSI representation of
  30. * the current image.
  31. * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
  32. * able to cut/paste the result to a function like printf
  33. * \return buffer containing generated ANSI codes as a big string
  34. */
  35. char * cucul_get_ansi(cucul_t *qq, int trailing, int *size)
  36. {
  37. static int const palette[] =
  38. {
  39. 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
  40. 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
  41. };
  42. char *cur;
  43. unsigned int x, y;
  44. /* 20 bytes assumed for max length per pixel.
  45. * Add height*9 to that (zeroes color at the end and jump to next line) */
  46. if(qq->ansi_buffer)
  47. free(qq->ansi_buffer);
  48. qq->ansi_buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
  49. if(qq->ansi_buffer == NULL)
  50. return NULL;
  51. cur = qq->ansi_buffer;
  52. // *cur++ = '';
  53. for(y = 0; y < qq->height; y++)
  54. {
  55. uint8_t *lineattr = qq->attr + y * qq->width;
  56. uint32_t *linechar = qq->chars + y * qq->width;
  57. uint8_t prevfg = -1;
  58. uint8_t prevbg = -1;
  59. for(x = 0; x < qq->width; x++)
  60. {
  61. uint8_t fg = palette[lineattr[x] & 0x0f];
  62. uint8_t bg = (palette[lineattr[x] >> 4])+10;
  63. uint32_t c = linechar[x];
  64. if(!trailing)
  65. cur += sprintf(cur, "\033[");
  66. else
  67. cur += sprintf(cur, "\\033[");
  68. if(fg > 7)
  69. cur += sprintf(cur, "1;%d;%dm",fg,bg);
  70. else
  71. cur += sprintf(cur, "0;%d;%dm",fg,bg);
  72. *cur++ = c & 0x7f;
  73. if((c == '%') && trailing)
  74. *cur++ = c & 0x7f;
  75. prevfg = fg;
  76. prevbg = bg;
  77. }
  78. if(!trailing)
  79. cur += sprintf(cur, "\033[0m\r\n");
  80. else
  81. cur += sprintf(cur, "\\033[0m\\n\n");
  82. }
  83. /* Crop to really used size */
  84. *size = (strlen(qq->ansi_buffer) + 1)* sizeof(char);
  85. qq->ansi_buffer = realloc(qq->ansi_buffer, *size);
  86. return qq->ansi_buffer;
  87. }