helper-cairo-ansi.hh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright © 2012 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HELPER_CAIRO_ANSI_HH
  27. #define HELPER_CAIRO_ANSI_HH
  28. #include "hb.hh"
  29. #include <cairo.h>
  30. #include "ansi-print.hh"
  31. #ifdef HAVE_CHAFA
  32. # include <chafa.h>
  33. /* Similar to ansi-print.cc */
  34. # define CELL_W 8
  35. # define CELL_H (2 * CELL_W)
  36. static void
  37. chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level)
  38. {
  39. ChafaTermInfo *term_info;
  40. ChafaSymbolMap *symbol_map;
  41. ChafaCanvasConfig *config;
  42. ChafaCanvas *canvas;
  43. GString *gs;
  44. unsigned int cols = (width + CELL_W - 1) / CELL_W;
  45. unsigned int rows = (height + CELL_H - 1) / CELL_H;
  46. gchar **environ;
  47. ChafaCanvasMode mode;
  48. ChafaPixelMode pixel_mode;
  49. /* Adapt to terminal; use sixels if available, and fall back to symbols
  50. * with as many colors as are supported */
  51. environ = g_get_environ ();
  52. term_info = chafa_term_db_detect (chafa_term_db_get_default (),
  53. environ);
  54. pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS;
  55. if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_BEGIN_SIXELS))
  56. {
  57. pixel_mode = CHAFA_PIXEL_MODE_SIXELS;
  58. mode = CHAFA_CANVAS_MODE_TRUECOLOR;
  59. }
  60. // else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT))
  61. // mode = CHAFA_CANVAS_MODE_TRUECOLOR;
  62. else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_256))
  63. mode = CHAFA_CANVAS_MODE_INDEXED_240;
  64. else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16))
  65. mode = CHAFA_CANVAS_MODE_INDEXED_16;
  66. else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_INVERT_COLORS))
  67. mode = CHAFA_CANVAS_MODE_FGBG_BGFG;
  68. else
  69. mode = CHAFA_CANVAS_MODE_FGBG;
  70. /* Create the configuration */
  71. symbol_map = chafa_symbol_map_new ();
  72. chafa_symbol_map_add_by_tags (symbol_map,
  73. (ChafaSymbolTags) (CHAFA_SYMBOL_TAG_BLOCK
  74. | CHAFA_SYMBOL_TAG_SPACE
  75. | (level >= 2 ? CHAFA_SYMBOL_TAG_WEDGE : 0)
  76. | (level >= 3 ? CHAFA_SYMBOL_TAG_ALL : 0)
  77. ));
  78. config = chafa_canvas_config_new ();
  79. chafa_canvas_config_set_canvas_mode (config, mode);
  80. chafa_canvas_config_set_pixel_mode (config, pixel_mode);
  81. chafa_canvas_config_set_cell_geometry (config, CELL_W, CELL_H);
  82. chafa_canvas_config_set_geometry (config, cols, rows);
  83. chafa_canvas_config_set_symbol_map (config, symbol_map);
  84. chafa_canvas_config_set_color_extractor (config, CHAFA_COLOR_EXTRACTOR_MEDIAN);
  85. chafa_canvas_config_set_work_factor (config, 1.0f);
  86. /* Create canvas, draw to it and render output string */
  87. canvas = chafa_canvas_new (config);
  88. chafa_canvas_draw_all_pixels (canvas,
  89. /* Cairo byte order is host native */
  90. G_BYTE_ORDER == G_LITTLE_ENDIAN
  91. ? CHAFA_PIXEL_BGRA8_PREMULTIPLIED
  92. : CHAFA_PIXEL_ARGB8_PREMULTIPLIED,
  93. (const guint8 *) data,
  94. width,
  95. height,
  96. stride);
  97. gs = chafa_canvas_print (canvas, term_info);
  98. /* Print the string */
  99. fwrite (gs->str, sizeof (char), gs->len, stdout);
  100. if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS)
  101. fputc ('\n', stdout);
  102. /* Free resources */
  103. g_string_free (gs, TRUE);
  104. chafa_canvas_unref (canvas);
  105. chafa_canvas_config_unref (config);
  106. chafa_symbol_map_unref (symbol_map);
  107. chafa_term_info_unref (term_info);
  108. g_strfreev (environ);
  109. }
  110. #endif /* HAVE_CHAFA */
  111. static inline cairo_status_t
  112. helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface,
  113. cairo_write_func_t write_func,
  114. void *closure)
  115. {
  116. unsigned int width = cairo_image_surface_get_width (surface);
  117. unsigned int height = cairo_image_surface_get_height (surface);
  118. if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
  119. cairo_surface_t *new_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
  120. cairo_t *cr = cairo_create (new_surface);
  121. if (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_A8) {
  122. cairo_set_source_rgb (cr, 0., 0., 0.);
  123. cairo_paint (cr);
  124. cairo_set_source_rgb (cr, 1., 1., 1.);
  125. cairo_mask_surface (cr, surface, 0, 0);
  126. } else {
  127. cairo_set_source_rgb (cr, 1., 1., 1.);
  128. cairo_paint (cr);
  129. cairo_set_source_surface (cr, surface, 0, 0);
  130. cairo_paint (cr);
  131. }
  132. cairo_destroy (cr);
  133. surface = new_surface;
  134. } else
  135. cairo_surface_reference (surface);
  136. unsigned int stride = cairo_image_surface_get_stride (surface);
  137. const uint32_t *data = (uint32_t *) (void *) cairo_image_surface_get_data (surface);
  138. /* We don't have rows to spare on the terminal window...
  139. * Find the tight image top/bottom and only print in between. */
  140. /* Use corner color as background color. */
  141. uint32_t bg_color = data ? * (uint32_t *) data : 0;
  142. /* Drop first row while empty */
  143. auto orig_data = data;
  144. while (height)
  145. {
  146. unsigned int i;
  147. for (i = 0; i < width; i++)
  148. if (data[i] != bg_color)
  149. break;
  150. if (i < width)
  151. break;
  152. data += stride / 4;
  153. height--;
  154. }
  155. if (orig_data < data)
  156. {
  157. data -= stride / 4;
  158. height++; /* Add one first blank row for padding. */
  159. }
  160. /* Drop last row while empty */
  161. auto orig_height = height;
  162. while (height)
  163. {
  164. const uint32_t *row = data + (height - 1) * stride / 4;
  165. unsigned int i;
  166. for (i = 0; i < width; i++)
  167. if (row[i] != bg_color)
  168. break;
  169. if (i < width)
  170. break;
  171. height--;
  172. }
  173. if (height < orig_height)
  174. height++; /* Add one last blank row for padding. */
  175. if (width && height)
  176. {
  177. #ifdef HAVE_CHAFA
  178. const char *env = getenv ("HB_CHAFA");
  179. int chafa_level = 1;
  180. if (env)
  181. chafa_level = atoi (env);
  182. if (chafa_level)
  183. chafa_print_image_rgb24 (data, width, height, stride, chafa_level);
  184. else
  185. #endif
  186. ansi_print_image_rgb24 (data, width, height, stride / 4);
  187. }
  188. cairo_surface_destroy (surface);
  189. return CAIRO_STATUS_SUCCESS;
  190. }
  191. #endif