2 * Copyright © 2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HELPER_CAIRO_ANSI_HH
28 #define HELPER_CAIRO_ANSI_HH
34 #include "ansi-print.hh"
39 /* Similar to ansi-print.cc */
41 # define CELL_H (2 * CELL_W)
44 chafa_print_image_rgb24 (const void *data, int width, int height, int stride)
46 ChafaTermInfo *term_info;
47 ChafaSymbolMap *symbol_map;
48 ChafaCanvasConfig *config;
51 unsigned int cols = (width + CELL_W - 1) / CELL_W;
52 unsigned int rows = (height + CELL_H - 1) / CELL_H;
55 ChafaPixelMode pixel_mode;
57 /* Adapt to terminal; use sixels if available, and fall back to symbols
58 * with as many colors as are supported */
60 environ = g_get_environ ();
61 term_info = chafa_term_db_detect (chafa_term_db_get_default (),
64 pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS;
66 if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_BEGIN_SIXELS))
68 pixel_mode = CHAFA_PIXEL_MODE_SIXELS;
69 mode = CHAFA_CANVAS_MODE_TRUECOLOR;
71 // else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT))
72 // mode = CHAFA_CANVAS_MODE_TRUECOLOR;
73 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_256))
74 mode = CHAFA_CANVAS_MODE_INDEXED_240;
75 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16))
76 mode = CHAFA_CANVAS_MODE_INDEXED_16;
77 else if (chafa_term_info_have_seq (term_info, CHAFA_TERM_SEQ_INVERT_COLORS))
78 mode = CHAFA_CANVAS_MODE_FGBG_BGFG;
80 mode = CHAFA_CANVAS_MODE_FGBG;
82 /* Create the configuration */
84 symbol_map = chafa_symbol_map_new ();
85 chafa_symbol_map_add_by_tags (symbol_map,
86 (ChafaSymbolTags) (CHAFA_SYMBOL_TAG_BLOCK
87 | CHAFA_SYMBOL_TAG_SPACE));
89 config = chafa_canvas_config_new ();
90 chafa_canvas_config_set_canvas_mode (config, mode);
91 chafa_canvas_config_set_pixel_mode (config, pixel_mode);
92 chafa_canvas_config_set_cell_geometry (config, 10, 20);
93 chafa_canvas_config_set_geometry (config, cols, rows);
94 chafa_canvas_config_set_symbol_map (config, symbol_map);
95 chafa_canvas_config_set_color_extractor (config, CHAFA_COLOR_EXTRACTOR_MEDIAN);
96 chafa_canvas_config_set_work_factor (config, 1.0f);
98 /* Create canvas, draw to it and render output string */
100 canvas = chafa_canvas_new (config);
101 chafa_canvas_draw_all_pixels (canvas,
102 /* Cairo byte order is host native */
103 G_BYTE_ORDER == G_LITTLE_ENDIAN
104 ? CHAFA_PIXEL_BGRA8_PREMULTIPLIED
105 : CHAFA_PIXEL_ARGB8_PREMULTIPLIED,
106 (const guint8 *) data,
110 gs = chafa_canvas_print (canvas, term_info);
112 /* Print the string */
114 fwrite (gs->str, sizeof (char), gs->len, stdout);
116 if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS)
117 fputc ('\n', stdout);
121 g_string_free (gs, TRUE);
122 chafa_canvas_unref (canvas);
123 chafa_canvas_config_unref (config);
124 chafa_symbol_map_unref (symbol_map);
125 chafa_term_info_unref (term_info);
126 g_strfreev (environ);
129 #endif /* HAVE_CHAFA */
131 static inline cairo_status_t
132 helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface,
133 cairo_write_func_t write_func,
136 unsigned int width = cairo_image_surface_get_width (surface);
137 unsigned int height = cairo_image_surface_get_height (surface);
138 if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
139 cairo_surface_t *new_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
140 cairo_t *cr = cairo_create (new_surface);
141 if (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_A8) {
142 cairo_set_source_rgb (cr, 0., 0., 0.);
144 cairo_set_source_rgb (cr, 1., 1., 1.);
145 cairo_mask_surface (cr, surface, 0, 0);
147 cairo_set_source_rgb (cr, 1., 1., 1.);
149 cairo_set_source_surface (cr, surface, 0, 0);
153 surface = new_surface;
155 cairo_surface_reference (surface);
157 unsigned int stride = cairo_image_surface_get_stride (surface);
158 const uint32_t *data = (uint32_t *) (void *) cairo_image_surface_get_data (surface);
160 /* We don't have rows to spare on the terminal window...
161 * Find the tight image top/bottom and only print in between. */
163 /* Use corner color as background color. */
164 uint32_t bg_color = data ? * (uint32_t *) data : 0;
166 /* Drop first row while empty */
170 for (i = 0; i < width; i++)
171 if (data[i] != bg_color)
179 /* Drop last row while empty */
180 unsigned int orig_height = height;
183 const uint32_t *row = data + (height - 1) * stride / 4;
185 for (i = 0; i < width; i++)
186 if (row[i] != bg_color)
192 if (height < orig_height)
193 height++; /* Add one last blank row for padding. */
199 chafa_print_image_rgb24 (data, width, height, stride);
202 ansi_print_image_rgb24 (data, width, height, stride / 4);
205 cairo_surface_destroy (surface);
206 return CAIRO_STATUS_SUCCESS;