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