xref: /aosp_15_r20/external/harfbuzz_ng/util/ansi-print.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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 ANSI_PRINT_HH
28 #define ANSI_PRINT_HH
29 
30 #include "hb.hh"
31 
32 #include <cairo.h>
33 
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <string.h>
38 #include <math.h>
39 
40 #if defined (_MSC_VER) && (_MSC_VER < 1800)
41 static inline long int
lround(double x)42 lround (double x)
43 {
44   if (x >= 0)
45     return floor (x + 0.5);
46   else
47     return ceil (x - 0.5);
48 }
49 #endif
50 
51 #define CELL_W 8
52 #define CELL_H (2 * CELL_W)
53 
54 struct color_diff_t
55 {
dotcolor_diff_t56   int dot (const color_diff_t &o)
57   { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
58 
59   int v[4];
60 };
61 
62 struct color_t
63 {
from_ansicolor_t64   static color_t from_ansi (unsigned int x)
65   {
66     color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
67     return c;
68   }
to_ansicolor_t69   unsigned int to_ansi ()
70   {
71     return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
72   }
73 
diffcolor_t74   color_diff_t diff (const color_t &o)
75   {
76     color_diff_t d;
77     for (unsigned int i = 0; i < 4; i++)
78       d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
79     return d;
80   }
81 
82   uint32_t v;
83 };
84 
85 struct image_t
86 {
87   public:
88 
image_timage_t89   image_t (unsigned int width_,
90 	   unsigned int height_,
91 	   const uint32_t *data_,
92 	   unsigned int stride_) :
93 		width (width_),
94 		height (height_),
95 		own_data (false),
96 		data ((color_t *) data_),
97 		stride (stride_) {}
image_timage_t98   image_t (unsigned int width_,
99 	   unsigned int height_) :
100 		width (width_),
101 		height (height_),
102 		own_data (true),
103 		data ((color_t *) malloc (sizeof (data[0]) * width * height)),
104 		stride (width) {}
~image_timage_t105   ~image_t ()
106   { if (own_data) free (data); }
107 
operator ()image_t108   color_t &operator () (unsigned int x, unsigned int y)
109   { return data[x + y * stride]; }
110 
operator ()image_t111   color_t operator () (unsigned int x, unsigned int y) const
112   { return data[x + y * stride]; }
113 
114   void
copy_sub_imageimage_t115   copy_sub_image (const image_t &s,
116 		  unsigned int x, unsigned int y,
117 		  unsigned int w, unsigned int h)
118   {
119     assert (x < width);
120     assert (y < height);
121     for (unsigned int row = 0; row < h; row++) {
122       color_t *p = data + x + hb_min (y + row, height - 1) * stride;
123       color_t *q = s.data + row * s.stride;
124       if (x + w <= width)
125 	for (unsigned int col = 0; col < w; col++)
126 	  *q++ = *p++;
127       else {
128 	unsigned int limit = width - x;
129 	for (unsigned int col = 0; col < limit; col++)
130 	  *q++ = *p++;
131 	p--;
132 	for (unsigned int col = limit; col < w; col++)
133 	  *q++ = *p;
134       }
135     }
136   }
137 
138   const unsigned int width;
139   const unsigned int height;
140 
141   private:
142   bool own_data;
143   color_t * const data;
144   const unsigned int stride;
145 };
146 
147 struct biimage_t
148 {
149   public:
150 
biimage_tbiimage_t151   biimage_t (unsigned int width, unsigned int height) :
152 		width (width),
153 		height (height),
154 		bg (0), fg (0), unicolor (true),
155 		data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
~biimage_tbiimage_t156   ~biimage_t ()
157   { free (data); }
158 
setbiimage_t159   void set (const image_t &image)
160   {
161     assert (image.width == width);
162     assert (image.height == height);
163     int freq[8] = {0};
164     for (unsigned int y = 0; y < height; y++)
165       for (unsigned int x = 0; x < width; x++) {
166 	color_t c = image (x, y);
167 	freq[c.to_ansi ()]++;
168       }
169     bg = 0;
170     for (unsigned int i = 1; i < 8; i++)
171       if (freq[bg] < freq[i])
172 	bg = i;
173     fg = 8;
174     for (unsigned int i = 0; i < 8; i++)
175       if (i != bg && (fg == 8 || freq[fg] < freq[i]))
176 	fg = i;
177     if (freq[fg] == 0) {
178       fg = bg;
179       unicolor = true;
180     }
181     else
182       unicolor = false;
183 
184     /* Set the data... */
185 
186     if (unicolor) {
187       memset (data, 0, sizeof (data[0]) * width * height);
188       return;
189     }
190 
191     color_t bgc = color_t::from_ansi (bg);
192     color_t fgc = color_t::from_ansi (fg);
193     color_diff_t diff = fgc.diff (bgc);
194     double dd = sqrt (diff.dot (diff));
195     for (unsigned int y = 0; y < height; y++)
196       for (unsigned int x = 0; x < width; x++) {
197 	double d = sqrt (diff.dot (image (x, y).diff (bgc)));
198 	(*this)(x, y) = d <= 0 ? 0 : d >= dd ? 255 : lround (d / dd * 255.);
199       }
200   }
201 
operator ()biimage_t202   uint8_t &operator () (unsigned int x, unsigned int y)
203   { return data[x + y * width]; }
204 
operator ()biimage_t205   uint8_t operator () (unsigned int x, unsigned int y) const
206   { return data[x + y * width]; }
207 
208   const unsigned int width;
209   const unsigned int height;
210   unsigned int bg;
211   unsigned int fg;
212   bool unicolor;
213 
214   private:
215   uint8_t * const data;
216 };
217 
218 static const char *
block_best(const biimage_t & bi,bool * inverse)219 block_best (const biimage_t &bi, bool *inverse)
220 {
221   assert (bi.width  <= CELL_W);
222   assert (bi.height <= CELL_H);
223 
224   unsigned int score = UINT_MAX;
225   unsigned int row_sum[CELL_H] = {0};
226   unsigned int col_sum[CELL_W] = {0};
227   unsigned int row_sum_i[CELL_H] = {0};
228   unsigned int col_sum_i[CELL_W] = {0};
229   unsigned int quad[2][2] = {{0}};
230   unsigned int quad_i[2][2] = {{0}};
231   unsigned int total = 0;
232   unsigned int total_i = 0;
233   for (unsigned int y = 0; y < bi.height; y++)
234     for (unsigned int x = 0; x < bi.width; x++) {
235       unsigned int c = bi (x, y);
236       unsigned int c_i = 255 - c;
237       row_sum[y] += c;
238       row_sum_i[y] += c_i;
239       col_sum[x] += c;
240       col_sum_i[x] += c_i;
241       quad[2 * y / bi.height][2 * x / bi.width] += c;
242       quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
243       total += c;
244       total_i += c_i;
245     }
246 
247   /* Make the sums cumulative */
248   for (unsigned int i = 1; i < bi.height; i++) {
249     row_sum[i] += row_sum[i - 1];
250     row_sum_i[i] += row_sum_i[i - 1];
251   }
252   for (unsigned int i = 1; i < bi.width;  i++) {
253     col_sum[i] += col_sum[i - 1];
254     col_sum_i[i] += col_sum_i[i - 1];
255   }
256 
257   const char *best_c = " ";
258 
259   /* Maybe empty is better! */
260   if (total < score) {
261     score = total;
262     *inverse = false;
263     best_c = " ";
264   }
265   /* Maybe full is better! */
266   if (total_i < score) {
267     score = total_i;
268     *inverse = true;
269     best_c = " ";
270   }
271 
272   /* Find best lower line */
273   if (1) {
274     unsigned int best_s = UINT_MAX;
275     bool best_inv = false;
276     int best_i = 0;
277     for (unsigned int i = 0; i < bi.height - 1; i++)
278     {
279       unsigned int s;
280       s = row_sum[i] + total_i - row_sum_i[i];
281       if (s < best_s) {
282 	best_s = s;
283 	best_i = i;
284 	best_inv = false;
285       }
286       s = row_sum_i[i] + total - row_sum[i];
287       if (s < best_s) {
288 	best_s = s;
289 	best_i = i;
290 	best_inv = true;
291       }
292     }
293     if (best_s < score) {
294       static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
295       unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
296       if (1 <= which && which <= 7) {
297 	score = best_s;
298 	*inverse = best_inv;
299 	best_c = lower[7 - which];
300       }
301     }
302   }
303 
304   /* Find best left line */
305   if (1) {
306     unsigned int best_s = UINT_MAX;
307     bool best_inv = false;
308     int best_i = 0;
309     for (unsigned int i = 0; i < bi.width - 1; i++)
310     {
311       unsigned int s;
312       s = col_sum[i] + total_i - col_sum_i[i];
313       if (s < best_s) {
314 	best_s = s;
315 	best_i = i;
316 	best_inv = true;
317       }
318       s = col_sum_i[i] + total - col_sum[i];
319       if (s < best_s) {
320 	best_s = s;
321 	best_i = i;
322 	best_inv = false;
323       }
324     }
325     if (best_s < score) {
326       static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
327       unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
328       if (1 <= which && which <= 7) {
329 	score = best_s;
330 	*inverse = best_inv;
331 	best_c = left[which - 1];
332       }
333     }
334   }
335 
336   /* Find best quadrant */
337   if (1) {
338     unsigned int q = 0;
339     unsigned int qs = 0;
340     for (unsigned int i = 0; i < 2; i++)
341       for (unsigned int j = 0; j < 2; j++)
342 	if (quad[i][j] > quad_i[i][j]) {
343 	  q += 1 << (2 * i + j);
344 	  qs += quad_i[i][j];
345 	} else
346 	  qs += quad[i][j];
347     if (qs < score) {
348       const char *c = nullptr;
349       bool inv = false;
350       switch (q) {
351 	case 1:  c = "▟"; inv = true;  break;
352 	case 2:  c = "▙"; inv = true;  break;
353 	case 4:  c = "▖"; inv = false; break;
354 	case 6:  c = "▞"; inv = false; break;
355 	case 7:  c = "▛"; inv = false; break;
356 	case 8:  c = "▗"; inv = false; break;
357 	case 9:  c = "▚"; inv = false; break;
358 	case 11: c = "▜"; inv = false; break;
359 	case 13: c = "▙"; inv = false; break;
360 	case 14: c = "▟"; inv = false; break;
361       }
362       if (c) {
363 	score = qs;
364 	*inverse = inv;
365 	best_c = c;
366       }
367     }
368   }
369 
370   return best_c;
371 }
372 
373 static inline void
ansi_print_image_rgb24(const uint32_t * data,unsigned int width,unsigned int height,unsigned int stride,cairo_write_func_t write_func,void * closure)374 ansi_print_image_rgb24 (const uint32_t *data,
375 			unsigned int width,
376 			unsigned int height,
377 			unsigned int stride,
378 		        cairo_write_func_t	write_func,
379 		        void			*closure)
380 {
381   image_t image (width, height, data, stride);
382 
383   unsigned int rows = (height + CELL_H - 1) / CELL_H;
384   unsigned int cols = (width +  CELL_W - 1) / CELL_W;
385   image_t cell (CELL_W, CELL_H);
386   biimage_t bi (CELL_W, CELL_H);
387   unsigned int last_bg = -1, last_fg = -1;
388   for (unsigned int row = 0; row < rows; row++)
389   {
390     for (unsigned int col = 0; col < cols; col++)
391     {
392       image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
393       bi.set (cell);
394       if (bi.unicolor)
395       {
396 	if (last_bg != bi.bg)
397 	{
398 	  char buf[] = "\033[40m";
399 	  buf[3] += bi.bg;
400 	  write_func (closure, (unsigned char *) buf, 5);
401 	  last_bg = bi.bg;
402 	}
403 	write_func (closure, (unsigned char *) " ", 1);
404       }
405       else
406       {
407 	/* Figure out the closest character to the biimage */
408 	bool inverse = false;
409 	const char *c = block_best (bi, &inverse);
410 	if (inverse)
411 	{
412 	  if (last_bg != bi.fg || last_fg != bi.bg)
413 	  {
414 	    char buf[] = "\033[30;40m";
415 	    buf[3] += bi.bg;
416 	    buf[6] += bi.fg;
417 	    write_func (closure, (unsigned char *) buf, 8);
418 	    last_bg = bi.fg;
419 	    last_fg = bi.bg;
420 	  }
421 	}
422 	else
423 	{
424 	  if (last_bg != bi.bg || last_fg != bi.fg)
425 	  {
426 	    char buf[] = "\033[40;30m";
427 	    buf[3] += bi.bg;
428 	    buf[6] += bi.fg;
429 	    write_func (closure, (unsigned char *) buf, 8);
430 	    last_bg = bi.bg;
431 	    last_fg = bi.fg;
432 	  }
433 	}
434 	write_func (closure, (unsigned char *) c, strlen (c));
435       }
436     }
437     write_func (closure, (unsigned char *) "\033[0m\n", 5); /* Reset */
438     last_bg = last_fg = -1;
439   }
440 }
441 
442 #endif
443