From 52e7b1424a3613122e9ca30879298df42733acda Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 13 May 2012 02:02:58 +0200 Subject: [PATCH] [util] Make hb-view print out Unicode art if stdout is a terminal --- configure.ac | 4 +- util/Makefile.am | 4 + util/ansi-print.cc | 411 ++++++++++++++++++++++++++++++++++++++ util/ansi-print.hh | 39 ++++ util/helper-cairo-ansi.cc | 102 ++++++++++ util/helper-cairo-ansi.hh | 39 ++++ util/helper-cairo.cc | 70 ++++++- util/options.hh | 3 + 8 files changed, 668 insertions(+), 4 deletions(-) create mode 100644 util/ansi-print.cc create mode 100644 util/ansi-print.hh create mode 100644 util/helper-cairo-ansi.cc create mode 100644 util/helper-cairo-ansi.hh diff --git a/configure.ac b/configure.ac index 7617af21a..6c5959a61 100644 --- a/configure.ac +++ b/configure.ac @@ -52,8 +52,8 @@ AC_SUBST(HB_LIBTOOL_VERSION_INFO) dnl GTK_DOC_CHECK([1.15],[--flavour no-tmpl]) # Functions and headers -AC_CHECK_FUNCS(mprotect sysconf getpagesize mmap _setmode) -AC_CHECK_HEADERS(unistd.h sys/mman.h io.h) +AC_CHECK_FUNCS(mprotect sysconf getpagesize mmap _setmode isatty) +AC_CHECK_HEADERS(unistd.h sys/mman.h io.h sys/ioctl.h) # Compiler flags AC_CANONICAL_HOST diff --git a/util/Makefile.am b/util/Makefile.am index cb913817e..9b4b34ac6 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -28,10 +28,14 @@ if HAVE_CAIRO_FT hb_view_SOURCES = \ hb-view.cc \ hb-view.hh \ + ansi-print.cc \ + ansi-print.hh \ options.cc \ options.hh \ helper-cairo.cc \ helper-cairo.hh \ + helper-cairo-ansi.cc \ + helper-cairo-ansi.hh \ view-cairo.cc \ view-cairo.hh \ $(NULL) diff --git a/util/ansi-print.cc b/util/ansi-print.cc new file mode 100644 index 000000000..0ad9a7d9a --- /dev/null +++ b/util/ansi-print.cc @@ -0,0 +1,411 @@ +/* + * Copyright © 2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "ansi-print.hh" + +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include /* for isatty() */ +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif + +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a,b) ((a) > (b) ? (a) : (b)) + +#define CELL_W 8 +#define CELL_H (2 * CELL_W) + +struct color_diff_t +{ + int dot (const color_diff_t &o) + { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; } + + int v[4]; +}; + +struct color_t +{ + static color_t from_ansi (unsigned int x) + { + color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))}; + return c; + } + unsigned int to_ansi (void) + { + return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4); + } + + color_diff_t diff (const color_t &o) + { + color_diff_t d; + for (unsigned int i = 0; i < 4; i++) + d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF); + return d; + } + + uint32_t v; +}; + +struct image_t +{ + public: + + image_t (unsigned int width_, + unsigned int height_, + const uint32_t *data_, + unsigned int stride_) : + width (width_), + height (height_), + own_data (false), + data ((color_t *) data_), + stride (stride_) {} + image_t (unsigned int width_, + unsigned int height_) : + width (width_), + height (height_), + own_data (true), + data ((color_t *) malloc (sizeof (data[0]) * width * height)), + stride (width) {} + ~image_t (void) + { if (own_data) free (data); } + + color_t &operator () (unsigned int x, unsigned int y) + { return data[x + y * stride]; } + + color_t operator () (unsigned int x, unsigned int y) const + { return data[x + y * stride]; } + + void + copy_sub_image (const image_t &s, + unsigned int x, unsigned int y, + unsigned int w, unsigned int h) + { + assert (x < width); + assert (y < height); + for (unsigned int row = 0; row < h; row++) { + color_t *p = data + x + MIN (y + row, height - 1) * stride; + color_t *q = s.data + row * s.stride; + if (x + w <= width) + for (unsigned int col = 0; col < w; col++) + *q++ = *p++; + else { + unsigned int limit = width - x; + for (unsigned int col = 0; col < limit; col++) + *q++ = *p++; + p--; + for (unsigned int col = limit; col < w; col++) + *q++ = *p; + } + } + } + + const unsigned int width; + const unsigned int height; + + private: + bool own_data; + color_t * const data; + const unsigned int stride; +}; + +struct biimage_t +{ + public: + + biimage_t (unsigned int width, unsigned int height) : + width (width), + height (height), + data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {} + ~biimage_t (void) + { free (data); } + + void set (const image_t &image) + { + assert (image.width == width); + assert (image.height == height); + int freq[8] = {0}; + for (unsigned int y = 0; y < height; y++) + for (unsigned int x = 0; x < width; x++) { + color_t c = image (x, y); + freq[c.to_ansi ()]++; + } + bg = 0; + for (unsigned int i = 1; i < 8; i++) + if (freq[bg] < freq[i]) + bg = i; + fg = 0; + for (unsigned int i = 1; i < 8; i++) + if (i != bg && freq[fg] < freq[i]) + fg = i; + if (fg == bg || freq[fg] == 0) { + fg = bg; + unicolor = true; + } + else + unicolor = false; + + /* Set the data... */ + + if (unicolor) { + memset (data, 0, sizeof (data[0]) * width * height); + return; + } + + color_t bgc = color_t::from_ansi (bg); + color_t fgc = color_t::from_ansi (fg); + color_diff_t diff = fgc.diff (bgc); + int dd = diff.dot (diff); + for (unsigned int y = 0; y < height; y++) + for (unsigned int x = 0; x < width; x++) { + int d = diff.dot (image (x, y).diff (bgc)); + (*this)(x, y) = d < 0 ? 0 : d > dd ? 255 : lround (d * 255. / dd); + } + } + + uint8_t &operator () (unsigned int x, unsigned int y) + { return data[x + y * width]; } + + uint8_t operator () (unsigned int x, unsigned int y) const + { return data[x + y * width]; } + + const unsigned int width; + const unsigned int height; + unsigned int bg; + unsigned int fg; + bool unicolor; + + private: + uint8_t * const data; +}; + +const char * +block_best (const biimage_t &bi, unsigned int *score, bool *inverse) +{ + unsigned int row_sum[bi.height]; + unsigned int col_sum[bi.width]; + unsigned int row_sum_i[bi.height]; + unsigned int col_sum_i[bi.width]; + + for (unsigned int i = 0; i < bi.height; i++) + row_sum[i] = row_sum_i[i] = 0; + for (unsigned int i = 0; i < bi.width; i++) + col_sum[i] = col_sum_i[i] = 0; + + unsigned int total = 0; + unsigned int total_i = 0; + unsigned int quad[2][2] = {{0}}; + unsigned int quad_i[2][2] = {{0}}; + for (unsigned int y = 0; y < bi.height; y++) + for (unsigned int x = 0; x < bi.width; x++) { + unsigned int c = bi (x, y); + row_sum[y] += c; + col_sum[x] += c; + quad[2 * y / bi.height][2 * x / bi.width] += c; + quad_i[2 * y / bi.height][2 * x / bi.width] += 255 - c; + total += c; + total_i += 255 - c; + } + for (unsigned int i = 0; i < bi.height; i++) + row_sum_i[i] = 255 * bi.width - row_sum[i]; + for (unsigned int i = 0; i < bi.width; i++) + col_sum_i[i] = 255 * bi.height - col_sum[i]; + + /* Make the sums cummulative */ + for (unsigned int i = 1; i < bi.height; i++) { + row_sum[i] += row_sum[i - 1]; + row_sum_i[i] += row_sum_i[i - 1]; + } + for (unsigned int i = 1; i < bi.width; i++) { + col_sum[i] += col_sum[i - 1]; + col_sum_i[i] += col_sum_i[i - 1]; + } + + *score = (unsigned int) -1; + *inverse = false; + const char *best_c = " "; + + /* Maybe empty is better! */ + if (total < *score) { + *score = total; + *inverse = false; + best_c = " "; + } + /* Maybe full is better! */ + if (total_i < *score) { + *score = total_i; + *inverse = true; + best_c = " "; + } + + /* Find best lower line */ + if (1) { + unsigned int best_s = (unsigned int) -1; + bool best_inv = false; + int best_i = 0; + for (unsigned int i = 0; i < bi.height - 1; i++) + { + unsigned int s; + s = row_sum[i] + total_i - row_sum_i[i]; + if (s < best_s) { + best_s = s; + best_i = i; + best_inv = false; + } + s = row_sum_i[i] + total - row_sum[i]; + if (s < best_s) { + best_s = s; + best_i = i; + best_inv = true; + } + } + if (best_s < *score) { + static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"}; + unsigned int which = lround (((best_i + 1) * 8) / bi.height); + if (1 <= which && which <= 7) { + *score = best_s; + *inverse = best_inv; + best_c = lower[7 - which]; + } + } + } + + /* Find best left line */ + /* Disable this as it looks ugly because of line height issues. */ + if (1) { + unsigned int best_s = (unsigned int) -1; + bool best_inv = false; + int best_i = 0; + for (unsigned int i = 0; i < bi.width - 1; i++) + { + unsigned int s; + s = col_sum[i] + total_i - col_sum_i[i]; + if (s < best_s) { + best_s = s; + best_i = i; + best_inv = true; + } + s = col_sum_i[i] + total - col_sum[i]; + if (s < best_s) { + best_s = s; + best_i = i; + best_inv = false; + } + } + if (best_s < *score) { + static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"}; + unsigned int which = lround (((best_i + 1) * 8) / bi.width); + if (1 <= which && which <= 7) { + *score = best_s; + *inverse = best_inv; + best_c = left[which - 1]; + } + } + } + + /* Quadrant? */ + unsigned int q = 0; + unsigned int qs = 0; + for (unsigned int i = 0; i < 2; i++) + for (unsigned int j = 0; j < 2; j++) + if (quad[i][j] > quad_i[i][j]) { + q += 1 << (2 * i + j); + qs += quad_i[i][j]; + } else + qs += quad[i][j]; + if (qs < *score) { + const char *c = NULL; + bool inv = false; + switch (q) { + case 1: c = "▟"; inv = true; break; + case 2: c = "▙"; inv = true; break; + case 4: c = "▖"; inv = false; break; + case 8: c = "▗"; inv = false; break; + case 9: c = "▚"; inv = false; break; + case 6: c = "▞"; inv = false; break; + case 7: c = "▜"; inv = true; break; + case 11: c = "▜"; inv = true; break; + case 13: c = "▙"; inv = true; break; + case 14: c = "▟"; inv = true; break; + } + if (c) { + *score = qs; + *inverse = inv; + best_c = c; + } + } + + return best_c; +} + +void +ansi_print_image_rgb24 (const uint32_t *data, + unsigned int width, + unsigned int height, + unsigned int stride) +{ + /* Get screen size */ + struct winsize w; + if (ioctl(1, TIOCGWINSZ, &w)) /* Ought to be stdout, right? */ + { + w.ws_row = 25; + w.ws_col = 80; + } + + image_t image (width, height, data, stride); + + unsigned int rows = (height + CELL_H - 1) / CELL_H; + unsigned int cols = (width + CELL_W - 1) / CELL_W; + image_t cell (CELL_W, CELL_H); + biimage_t bi (CELL_W, CELL_H); + for (unsigned int row = 0; row < rows; row++) { + for (unsigned int col = 0; col < cols; col++) { + image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H); + bi.set (cell); + if (bi.unicolor) + printf ("\e[%dm ", 40 + bi.bg); + else { + /* Figure out the closest character to the biimage */ + unsigned int score = (unsigned int) -1; + bool inverse; + const char *c = block_best (bi, &score, &inverse); + printf ("\e[%d;%dm%s", (inverse ? 30 : 40) + bi.bg, (inverse ? 40 : 30) + bi.fg, c); + } + } + printf ("\e[0m\n"); /* Reset */ + } +} diff --git a/util/ansi-print.hh b/util/ansi-print.hh new file mode 100644 index 000000000..dad4d4cfd --- /dev/null +++ b/util/ansi-print.hh @@ -0,0 +1,39 @@ +/* + * Copyright © 2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifndef ANSI_PRINT_HH +#define ANSI_PRINT_HH + +#include /* for int types */ + +void +ansi_print_image_rgb24 (const uint32_t *data, + unsigned int width, + unsigned int height, + unsigned int stride); + + +#endif diff --git a/util/helper-cairo-ansi.cc b/util/helper-cairo-ansi.cc new file mode 100644 index 000000000..f7c06609d --- /dev/null +++ b/util/helper-cairo-ansi.cc @@ -0,0 +1,102 @@ +/* + * Copyright © 2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include "helper-cairo-ansi.hh" +#include "options.hh" + +#include "ansi-print.hh" + + +cairo_status_t +helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface, + cairo_write_func_t write_func, + void *closure) +{ + unsigned int width = cairo_image_surface_get_width (surface); + unsigned int height = cairo_image_surface_get_height (surface); + if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) { + cairo_surface_t *new_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); + cairo_t *cr = cairo_create (new_surface); + if (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_A8) { + cairo_set_source_rgb (cr, 0., 0., 0.); + cairo_paint (cr); + cairo_set_source_rgb (cr, 1., 1., 1.); + cairo_mask_surface (cr, surface, 0, 0); + } else { + cairo_set_source_rgb (cr, 1., 1., 1.); + cairo_paint (cr); + cairo_set_source_surface (cr, surface, 0, 0); + cairo_paint (cr); + } + cairo_destroy (cr); + surface = new_surface; + } else + cairo_surface_reference (surface); + + unsigned int stride = cairo_image_surface_get_stride (surface); + const uint32_t *data = (uint32_t *) cairo_image_surface_get_data (surface); + + /* We don't have rows to spare on the terminal window... + * Find the tight image top/bottom and only print in between. */ + + /* Use corner color as background color. */ + uint32_t bg_color = * (uint32_t *) data; + + /* Drop first row while empty */ + while (height) + { + unsigned int i; + for (i = 0; i < width; i++) + if (data[i] != bg_color) + break; + if (i < width) + break; + data += stride / 4; + height--; + } + + /* Drop last row while empty */ + unsigned int orig_height = height; + while (height) + { + const uint32_t *row = data + (height - 1) * stride / 4; + unsigned int i; + for (i = 0; i < width; i++) + if (row[i] != bg_color) + break; + if (i < width) + break; + height--; + } + if (height < orig_height) + height++; /* Add one last blank row for padding. */ + + if (width && height) + ansi_print_image_rgb24 (data, width, height, stride / 4); + + cairo_surface_destroy (surface); + return CAIRO_STATUS_SUCCESS; +} diff --git a/util/helper-cairo-ansi.hh b/util/helper-cairo-ansi.hh new file mode 100644 index 000000000..eeaaa5069 --- /dev/null +++ b/util/helper-cairo-ansi.hh @@ -0,0 +1,39 @@ +/* + * Copyright © 2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include + +#ifndef HELPER_CAIRO_ANSI_HH +#define HELPER_CAIRO_ANSI_HH + + +cairo_status_t +helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface, + cairo_write_func_t write_func, + void *closure); + + +#endif diff --git a/util/helper-cairo.cc b/util/helper-cairo.cc index 9374d9eb0..de45fb315 100644 --- a/util/helper-cairo.cc +++ b/util/helper-cairo.cc @@ -29,6 +29,7 @@ #include #include +#include "helper-cairo-ansi.hh" #ifdef CAIRO_HAS_SVG_SURFACE # include #endif @@ -111,6 +112,63 @@ struct finalize_closure_t { }; static cairo_user_data_key_t finalize_closure_key; + +static void +finalize_ansi (finalize_closure_t *closure) +{ + cairo_status_t status; + status = helper_cairo_surface_write_to_ansi_stream (closure->surface, + closure->write_func, + closure->closure); + if (status != CAIRO_STATUS_SUCCESS) + fail (FALSE, "Failed to write output: %s", + cairo_status_to_string (status)); +} + +static cairo_surface_t * +_cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func, + void *closure, + double width, + double height, + cairo_content_t content) +{ + cairo_surface_t *surface; + int w = ceil (width); + int h = ceil (height); + + switch (content) { + case CAIRO_CONTENT_ALPHA: + surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h); + break; + default: + case CAIRO_CONTENT_COLOR: + surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h); + break; + case CAIRO_CONTENT_COLOR_ALPHA: + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); + break; + } + cairo_status_t status = cairo_surface_status (surface); + if (status != CAIRO_STATUS_SUCCESS) + fail (FALSE, "Failed to create cairo surface: %s", + cairo_status_to_string (status)); + + finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1); + ansi_closure->callback = finalize_ansi; + ansi_closure->surface = surface; + ansi_closure->write_func = write_func; + ansi_closure->closure = closure; + + if (cairo_surface_set_user_data (surface, + &finalize_closure_key, + (void *) ansi_closure, + (cairo_destroy_func_t) g_free)) + g_free ((void *) closure); + + return surface; +} + + #ifdef CAIRO_HAS_PNG_FUNCTIONS static void @@ -204,10 +262,18 @@ helper_cairo_create_context (double w, double h, cairo_content_t content) = NULL; const char *extension = out_opts->output_format; - if (!extension) - extension = "png"; + if (!extension) { +#if HAVE_ISATTY + if (isatty (fileno (out_opts->get_file_handle ()))) + extension = "ansi"; + else +#endif + extension = "png"; + } if (0) ; + else if (0 == strcasecmp (extension, "ansi")) + constructor2 = _cairo_ansi_surface_create_for_stream; #ifdef CAIRO_HAS_PNG_FUNCTIONS else if (0 == strcasecmp (extension, "png")) constructor2 = _cairo_png_surface_create_for_stream; diff --git a/util/options.hh b/util/options.hh index bf9123b45..6b2b226a7 100644 --- a/util/options.hh +++ b/util/options.hh @@ -40,6 +40,9 @@ #include #include #include +#ifdef HAVE_UNISTD_H +#include /* for isatty() */ +#endif #ifdef HAVE_IO_H #include /* for _setmode() under Windows */ #endif