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