[HarfBuzz] harfbuzz-ng: Branch 'master' - 8 commits

Behdad Esfahbod behdad at kemper.freedesktop.org
Sat May 12 22:56:00 PDT 2012


 TODO                                                                         |    2 
 configure.ac                                                                 |    4 
 src/hb-ot-shape-complex-indic.cc                                             |    7 
 src/hb-ot-shape-complex-private.hh                                           |    2 
 test/shaping/hb_test_tools.py                                                |    9 
 test/shaping/texts/in-tree/shaper-indic/indic/script-malayalam/misc/misc.txt |   38 
 util/Makefile.am                                                             |    4 
 util/ansi-print.cc                                                           |  411 ++++++++++
 util/ansi-print.hh                                                           |   39 
 util/hb-view.hh                                                              |    4 
 util/helper-cairo-ansi.cc                                                    |  102 ++
 util/helper-cairo-ansi.hh                                                    |   39 
 util/helper-cairo.cc                                                         |   70 +
 util/options.cc                                                              |   12 
 util/options.hh                                                              |    9 
 15 files changed, 725 insertions(+), 27 deletions(-)

New commits:
commit 52e7b1424a3613122e9ca30879298df42733acda
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun May 13 02:02:58 2012 +0200

    [util] Make hb-view print out Unicode art if stdout is a terminal

diff --git a/configure.ac b/configure.ac
index 7617af2..6c5959a 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 cb91381..9b4b34a 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 0000000..0ad9a7d
--- /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 <assert.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <fcntl.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h> /* for isatty() */
+#endif
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#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 0000000..dad4d4c
--- /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 <hb.h> /* 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 0000000..f7c0660
--- /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 0000000..eeaaa50
--- /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 <cairo.h>
+
+#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 9374d9e..de45fb3 100644
--- a/util/helper-cairo.cc
+++ b/util/helper-cairo.cc
@@ -29,6 +29,7 @@
 #include <cairo-ft.h>
 #include <hb-ft.h>
 
+#include "helper-cairo-ansi.hh"
 #ifdef CAIRO_HAS_SVG_SURFACE
 #  include <cairo-svg.h>
 #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 bf9123b..6b2b226 100644
--- a/util/options.hh
+++ b/util/options.hh
@@ -40,6 +40,9 @@
 #include <locale.h>
 #include <errno.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h> /* for isatty() */
+#endif
 #ifdef HAVE_IO_H
 #include <io.h> /* for _setmode() under Windows */
 #endif
commit 8b2753ce2bea8a21ea757186d86dc4a55d8c8b0c
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun May 13 00:54:07 2012 +0200

    Minor

diff --git a/TODO b/TODO
index 56eac54..b6b7c89 100644
--- a/TODO
+++ b/TODO
@@ -22,6 +22,8 @@ General fixes:
 
 - GSUB ligation should call merge_clusters().
 
+- Convert NBSP into space glyph.
+
 
 API issues to fix before 1.0:
 ============================
commit 30874b4819a99cc84fa39e794266685e1b8735d2
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sat May 12 15:54:27 2012 +0200

    [util] Make tools default to stdin if no text is provided
    
    One less argument to type in typical testing workflow!

diff --git a/util/hb-view.hh b/util/hb-view.hh
index 66d955b..61af2a8 100644
--- a/util/hb-view.hh
+++ b/util/hb-view.hh
@@ -50,8 +50,10 @@ struct hb_view_t
     if (argc && !input.text && !input.text_file) input.text = argv[0], argc--, argv++;
     if (argc)
       fail (TRUE, "Too many arguments on the command line");
-    if (!font_opts.font_file || (!input.text && !input.text_file))
+    if (!font_opts.font_file)
       options.usage ();
+    if (!input.text && !input.text_file)
+      input.text_file = "-";
 
     output.init (&font_opts);
 
diff --git a/util/options.cc b/util/options.cc
index e24a026..363a150 100644
--- a/util/options.cc
+++ b/util/options.cc
@@ -376,7 +376,7 @@ view_options_t::add_options (option_parser_t *parser)
   parser->add_group (entries,
 		     "view",
 		     "View options:",
-		     "Options controlling the output rendering",
+		     "Options controlling output rendering",
 		     this);
 }
 
@@ -391,7 +391,7 @@ shape_options_t::add_options (option_parser_t *parser)
     {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
     {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
     {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
-    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF-8 byte indices, not char indices",	NULL},
+    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF8 byte indices, not char indices",	NULL},
     {NULL}
   };
   parser->add_group (entries,
@@ -400,9 +400,7 @@ shape_options_t::add_options (option_parser_t *parser)
 		     "Options controlling the shaping process",
 		     this);
 
-  const gchar *features_help = "\n"
-    "\n"
-    "    Comma-separated list of font features to apply to text\n"
+  const gchar *features_help = "Comma-separated list of font features\n"
     "\n"
     "    Features can be enabled or disabled, either globally or limited to\n"
     "    specific character ranges.\n"
@@ -444,7 +442,7 @@ shape_options_t::add_options (option_parser_t *parser)
   parser->add_group (entries2,
 		     "features",
 		     "Features options:",
-		     "Options controlling the OpenType font features applied",
+		     "Options controlling font features used",
 		     this);
 }
 
@@ -470,7 +468,7 @@ text_options_t::add_options (option_parser_t *parser)
   GOptionEntry entries[] =
   {
     {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
-    {"text-file",	0, 0, G_OPTION_ARG_STRING,	&this->text_file,		"Set input text file-name",		"filename"},
+    {"text-file",	0, 0, G_OPTION_ARG_STRING,	&this->text_file,		"Set input text file-name\n\n    If no text is provided, standard input is used for input.",		"filename"},
     {NULL}
   };
   parser->add_group (entries,
diff --git a/util/options.hh b/util/options.hh
index ec51ed3..bf9123b 100644
--- a/util/options.hh
+++ b/util/options.hh
@@ -239,7 +239,7 @@ struct text_options_t : option_group_t
     if (text && text_file)
       g_set_error (error,
 		   G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
-		   "Only one of text and text-file must be set");
+		   "Only one of text and text-file can be set");
 
   };
 
commit 2097951110e33fe091ed9515ae77e2683c46c889
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sat May 12 15:41:48 2012 +0200

    [util] Change default font size of hb-view to 256
    
    Most common usecase of hb-view is to test rendering of short words for
    testing / inspection.  Not having to type "--font-size 150" each time
    isn't such a bad idea...

diff --git a/util/options.hh b/util/options.hh
index 15d9402..ec51ed3 100644
--- a/util/options.hh
+++ b/util/options.hh
@@ -102,10 +102,10 @@ struct option_parser_t
 };
 
 
-#define DEFAULT_MARGIN 18
+#define DEFAULT_MARGIN 64
 #define DEFAULT_FORE "#000000"
 #define DEFAULT_BACK "#FFFFFF"
-#define DEFAULT_FONT_SIZE 36
+#define DEFAULT_FONT_SIZE 256
 
 struct view_options_t : option_group_t
 {
commit 737dded2e08fcc19935db51c05201a987184d337
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sat May 12 15:40:11 2012 +0200

    Fix compiler warnings

diff --git a/src/hb-ot-shape-complex-indic.cc b/src/hb-ot-shape-complex-indic.cc
index 79560c4..81cfd15 100644
--- a/src/hb-ot-shape-complex-indic.cc
+++ b/src/hb-ot-shape-complex-indic.cc
@@ -651,7 +651,6 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
     /*       1. If reph should be positioned after post-base consonant forms,
      *          proceed to step 5.
      */
-    reph_step_1:
     if (reph_pos == REPH_AFTER_POSTSCRIPT)
     {
       goto reph_step_5;
@@ -668,7 +667,6 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
      *          fixed in shaping engine, there was no case where reph position
      *          will be found on this step.
      */
-    reph_step_2:
     {
       new_reph_pos = start + 1;
       while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
@@ -686,7 +684,6 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
      *          first consonant not ligated with main, or find the first
      *          consonant that is not a potential pre-base reordering Ra.
      */
-    reph_step_3:
     if (reph_pos == REPH_AFTER_MAIN)
     {
       /* XXX */
@@ -697,7 +694,6 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
      *          consonant is found, the target position should be before the
      *          first matra, syllable modifier sign or vedic sign.
      */
-    reph_step_4:
     /* This is our take on what step 4 is trying to say (and failing, BADLY). */
     if (reph_pos == REPH_AFTER_SUBSCRIPT)
     {
@@ -723,7 +719,6 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
 
     /*       6. Otherwise, reorder reph to the end of the syllable.
      */
-    reph_step_6:
     {
       new_reph_pos = end - 1;
       while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
commit f538fcb538f1decb4100ba89457eb83f2350d64b
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sat May 12 15:34:40 2012 +0200

    [test] Make tool usage easier by not requiring "--stdin"
    
    Just default to it.  Added "--help" instead to get usage.

diff --git a/test/shaping/hb_test_tools.py b/test/shaping/hb_test_tools.py
index 3ff75b8..47fa6eb 100644
--- a/test/shaping/hb_test_tools.py
+++ b/test/shaping/hb_test_tools.py
@@ -351,7 +351,7 @@ class UtilMains:
 	@staticmethod
 	def process_multiple_args (callback, mnemonic):
 
-		if len (sys.argv) == 1:
+		if len (sys.argv) == 1 or "--help" in sys.argv:
 			print "Usage: %s %s..." % (sys.argv[0], mnemonic)
 			sys.exit (1)
 
@@ -368,14 +368,13 @@ class UtilMains:
 					      separator = " ", \
 					      concat_separator = False):
 
-		if len (sys.argv) == 1 or ('--stdin' in sys.argv and len (sys.argv) != 2):
-			print "Usage:\n  %s %s...\nor:\n  %s --stdin" \
+		if "--help" in sys.argv:
+			print "Usage:\n  %s %s...\nor:\n  %s\n\nWhen called with no arguments, input is read from standard input." \
 			      % (sys.argv[0], mnemonic, sys.argv[0])
 			sys.exit (1)
 
 		try:
-			if '--stdin' in sys.argv:
-				sys.argv.remove ('--stdin')
+			if len (sys.argv) == 1:
 				while (1):
 					line = sys.stdin.readline ()
 					if not len (line):
commit a3273e30bb7ffd727ffc18af5716dfef705d3d94
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sat May 12 13:34:18 2012 +0200

    [Indic] Add more Malayalam tests

diff --git a/test/shaping/texts/in-tree/shaper-indic/indic/script-malayalam/misc/misc.txt b/test/shaping/texts/in-tree/shaper-indic/indic/script-malayalam/misc/misc.txt
index f5ef618..1b17ef6 100644
--- a/test/shaping/texts/in-tree/shaper-indic/indic/script-malayalam/misc/misc.txt
+++ b/test/shaping/texts/in-tree/shaper-indic/indic/script-malayalam/misc/misc.txt
@@ -20,3 +20,41 @@
 ര്‍വ്വ
 ള്ള
 ഷ്ട്രീ
+ന്‍ 
+ല്‍ 
+ക്‍ 
+ര്‍ 
+ണ്‍ 
+ള്‍ 
+ഖ്യ 
+ര്യ 
+ന്ത്യ 
+ന്ത്ര്യ 
+ഗ്ദ്ധ്രോ 
+സ്പ്ലേ 
+ള്യം 
+സ്പ്രി 
+യും 
+ല്ലാം 
+ണ്ട 
+ത്ത 
+ന്ത 
+മ്പ 
+യ്യ 
+വ്വ 
+ല്ല 
+ത്തെ 
+ക്കെ 
+ത്തൊ 
+ക്കൊ 
+പ്ലോ 
+ല്യ 
+പ്ര 
+ക്ത്ര 
+ഖ്ര 
+കായ്‌കറി 
+കാര്‍ക്കോടകന്‍ 
+മുഖ്യമന്ത്രി 
+കുറ്റ്യാടി 
+സ്വാതന്ത്ര്യം 
+ഹാര്‍ഡ്‌വെയര്‍ 
commit 7f852b644b8143492a02edfc853114aaa23446bd
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Fri May 11 23:10:31 2012 +0200

    Fix compiler warnings

diff --git a/src/hb-ot-shape-complex-indic.cc b/src/hb-ot-shape-complex-indic.cc
index 93b490e..79560c4 100644
--- a/src/hb-ot-shape-complex-indic.cc
+++ b/src/hb-ot-shape-complex-indic.cc
@@ -620,7 +620,7 @@ final_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
      } reph_pos;
 
      /* XXX Figure out old behavior too */
-     switch (buffer->props.script)
+     switch ((hb_tag_t) buffer->props.script)
      {
        case HB_SCRIPT_MALAYALAM:
        case HB_SCRIPT_ORIYA:
diff --git a/src/hb-ot-shape-complex-private.hh b/src/hb-ot-shape-complex-private.hh
index 9b61f8e..ba962b5 100644
--- a/src/hb-ot-shape-complex-private.hh
+++ b/src/hb-ot-shape-complex-private.hh
@@ -68,7 +68,7 @@ enum hb_ot_complex_shaper_t {
 static inline hb_ot_complex_shaper_t
 hb_ot_shape_complex_categorize (const hb_segment_properties_t *props)
 {
-  switch ((int) props->script)
+  switch ((hb_tag_t) props->script)
   {
     default:
       return hb_ot_complex_shaper_default;



More information about the HarfBuzz mailing list