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
27 #ifndef HELPER_CAIRO_ANSI_HH
28 #define HELPER_CAIRO_ANSI_HH
29
30 #include "hb.hh"
31
32 #include <cairo.h>
33
34 #include "ansi-print.hh"
35
36 #ifdef HAVE_CHAFA
37 # include <chafa.h>
38
39 /* Similar to ansi-print.cc */
40 # define CELL_W 8
41 # define CELL_H (2 * CELL_W)
42
43 static void
chafa_print_image_rgb24(const void * data,int width,int height,int stride,int level,cairo_write_func_t write_func,void * closure)44 chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level,
45 cairo_write_func_t write_func,
46 void *closure)
47 {
48 ChafaTermInfo *term_info;
49 ChafaSymbolMap *symbol_map;
50 ChafaCanvasConfig *config;
51 ChafaCanvas *canvas;
52 GString *gs;
53 unsigned int cols = (width + CELL_W - 1) / CELL_W;
54 unsigned int rows = (height + CELL_H - 1) / CELL_H;
55 gchar **environ;
56 ChafaCanvasMode mode;
57 ChafaPixelMode pixel_mode;
58
59 /* Adapt to terminal; use sixels if available, and fall back to symbols
60 * with as many colors as are supported */
61
62 chafa_set_n_threads (1); // https://github.com/hpjansson/chafa/issues/125#issuecomment-1397475217
63
64 environ = g_get_environ ();
65 term_info = chafa_term_db_detect (chafa_term_db_get_default (),
66 environ);
67
68 pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS;
69
70 if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_BEGIN_SIXELS))
71 {
72 pixel_mode = CHAFA_PIXEL_MODE_SIXELS;
73 mode = CHAFA_CANVAS_MODE_TRUECOLOR;
74 }
75 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT))
76 mode = CHAFA_CANVAS_MODE_TRUECOLOR;
77 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_256))
78 mode = CHAFA_CANVAS_MODE_INDEXED_240;
79 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16))
80 mode = CHAFA_CANVAS_MODE_INDEXED_16;
81 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_INVERT_COLORS))
82 mode = CHAFA_CANVAS_MODE_FGBG_BGFG;
83 else
84 mode = CHAFA_CANVAS_MODE_FGBG;
85
86 /* Create the configuration */
87
88 symbol_map = chafa_symbol_map_new ();
89 chafa_symbol_map_add_by_tags (symbol_map,
90 (ChafaSymbolTags) (CHAFA_SYMBOL_TAG_BLOCK
91 | CHAFA_SYMBOL_TAG_SPACE
92 | (level >= 2 ? CHAFA_SYMBOL_TAG_WEDGE : 0)
93 | (level >= 3 ? CHAFA_SYMBOL_TAG_ALL : 0)
94 ));
95
96 config = chafa_canvas_config_new ();
97 chafa_canvas_config_set_canvas_mode (config, mode);
98 chafa_canvas_config_set_pixel_mode (config, pixel_mode);
99 chafa_canvas_config_set_cell_geometry (config, CELL_W, CELL_H);
100 chafa_canvas_config_set_geometry (config, cols, rows);
101 chafa_canvas_config_set_symbol_map (config, symbol_map);
102 chafa_canvas_config_set_color_extractor (config, CHAFA_COLOR_EXTRACTOR_MEDIAN);
103 chafa_canvas_config_set_work_factor (config, 1.0f);
104
105 /* Create canvas, draw to it and render output string */
106
107 canvas = chafa_canvas_new (config);
108 chafa_canvas_draw_all_pixels (canvas,
109 /* Cairo byte order is host native */
110 G_BYTE_ORDER == G_LITTLE_ENDIAN
111 ? CHAFA_PIXEL_BGRA8_PREMULTIPLIED
112 : CHAFA_PIXEL_ARGB8_PREMULTIPLIED,
113 (const guint8 *) data,
114 width,
115 height,
116 stride);
117 gs = chafa_canvas_print (canvas, term_info);
118
119 /* Print the string */
120
121 write_func (closure, (const unsigned char *) gs->str, gs->len);
122
123 if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS)
124 write_func (closure, (const unsigned char *) "\n", 1);
125
126 /* Free resources */
127
128 g_string_free (gs, TRUE);
129 chafa_canvas_unref (canvas);
130 chafa_canvas_config_unref (config);
131 chafa_symbol_map_unref (symbol_map);
132 chafa_term_info_unref (term_info);
133 g_strfreev (environ);
134 }
135
136 #endif /* HAVE_CHAFA */
137
138 static inline cairo_status_t
helper_cairo_surface_write_to_ansi_stream(cairo_surface_t * surface,cairo_write_func_t write_func,void * closure)139 helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface,
140 cairo_write_func_t write_func,
141 void *closure)
142 {
143 unsigned int width = cairo_image_surface_get_width (surface);
144 unsigned int height = cairo_image_surface_get_height (surface);
145 if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
146 cairo_surface_t *new_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
147 cairo_t *cr = cairo_create (new_surface);
148 if (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_A8) {
149 cairo_set_source_rgb (cr, 0., 0., 0.);
150 cairo_paint (cr);
151 cairo_set_source_rgb (cr, 1., 1., 1.);
152 cairo_mask_surface (cr, surface, 0, 0);
153 } else {
154 cairo_set_source_rgb (cr, 1., 1., 1.);
155 cairo_paint (cr);
156 cairo_set_source_surface (cr, surface, 0, 0);
157 cairo_paint (cr);
158 }
159 cairo_destroy (cr);
160 surface = new_surface;
161 } else
162 cairo_surface_reference (surface);
163
164 unsigned int stride = cairo_image_surface_get_stride (surface);
165 const uint32_t *data = (uint32_t *) (void *) cairo_image_surface_get_data (surface);
166
167 /* We don't have rows to spare on the terminal window...
168 * Find the tight image top/bottom and only print in between. */
169
170 /* Use corner color as background color. */
171 uint32_t bg_color = data ? * (uint32_t *) data : 0;
172
173 /* Drop first row while empty */
174 auto orig_data = data;
175 while (height)
176 {
177 unsigned int i;
178 for (i = 0; i < width; i++)
179 if (data[i] != bg_color)
180 break;
181 if (i < width)
182 break;
183 data += stride / 4;
184 height--;
185 }
186 if (orig_data < data)
187 {
188 data -= stride / 4;
189 height++; /* Add one first blank row for padding. */
190 }
191
192 /* Drop last row while empty */
193 auto orig_height = height;
194 while (height)
195 {
196 const uint32_t *row = data + (height - 1) * stride / 4;
197 unsigned int i;
198 for (i = 0; i < width; i++)
199 if (row[i] != bg_color)
200 break;
201 if (i < width)
202 break;
203 height--;
204 }
205 if (height < orig_height)
206 height++; /* Add one last blank row for padding. */
207
208 if (width && height)
209 {
210 #ifdef HAVE_CHAFA
211 const char *env = getenv ("HB_CHAFA");
212 int chafa_level = 1;
213 if (env)
214 chafa_level = atoi (env);
215 if (chafa_level)
216 chafa_print_image_rgb24 (data, width, height, stride, chafa_level,
217 write_func, closure);
218 else
219 #endif
220 ansi_print_image_rgb24 (data, width, height, stride / 4,
221 write_func, closure);
222 }
223
224 cairo_surface_destroy (surface);
225 return CAIRO_STATUS_SUCCESS;
226 }
227
228
229 #endif
230