Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

export_ansi.c 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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_ansi.c
  12. * \version \$Id$
  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. void _cucul_get_ansi(cucul_t *qq, struct cucul_export *ex)
  36. {
  37. static int const palette[] =
  38. {
  39. 0, 4, 2, 6, 1, 5, 3, 7,
  40. 8, 12, 10, 14, 9, 13, 11, 15
  41. };
  42. char *cur;
  43. unsigned int x, y;
  44. /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
  45. * 4 max bytes for a UTF-8 character).
  46. * Add height*9 to that (zeroes color at the end and jump to next line) */
  47. ex->size = (qq->height * 9) + (qq->width * qq->height * 23);
  48. ex->buffer = malloc(ex->size);
  49. cur = ex->buffer;
  50. for(y = 0; y < qq->height; y++)
  51. {
  52. uint32_t *lineattr = qq->attr + y * qq->width;
  53. uint32_t *linechar = qq->chars + y * qq->width;
  54. uint8_t prevfg = -1;
  55. uint8_t prevbg = -1;
  56. for(x = 0; x < qq->width; x++)
  57. {
  58. uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
  59. uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
  60. uint32_t c = linechar[x];
  61. if(fg != prevfg || bg != prevbg)
  62. {
  63. cur += sprintf(cur, "\033[0;");
  64. if(fg < 8)
  65. if(bg < 8)
  66. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  67. else
  68. cur += sprintf(cur, "5;3%d;4%d;10%dm",
  69. fg, bg - 8, bg - 8);
  70. else
  71. if(bg < 8)
  72. cur += sprintf(cur, "1;3%d;4%d;9%dm",
  73. fg - 8, bg, fg - 8);
  74. else
  75. cur += sprintf(cur, "5;1;3%d;4%d;9%d;10%dm",
  76. fg - 8, bg - 8, fg - 8, bg - 8);
  77. }
  78. *cur++ = c & 0x7f;
  79. prevfg = fg;
  80. prevbg = bg;
  81. }
  82. cur += sprintf(cur, "\033[0m\r\n");
  83. }
  84. /* Crop to really used size */
  85. ex->size = (uintptr_t)(cur - ex->buffer);
  86. ex->buffer = realloc(ex->buffer, ex->size);
  87. }