選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

104 行
3.0 KiB

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