[hb-shape] Improve shaping-debug output
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 19 Jul 2017 02:14:19 +0000 (19:14 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 19 Jul 2017 02:16:09 +0000 (19:16 -0700)
Before, that was printed using --debug (and in both hb-shape and hb-view).
Changed it, now hb-shape has a new command-line argument called --show-messages.
When invoked, it also respects other output formatting options.  The messages
are better formatted and printed to te same place that hb-shape output is
directed to.  Previously they were written to stderr.

Fixes https://github.com/behdad/harfbuzz/issues/506

util/hb-ot-shape-closure.cc
util/hb-shape.cc
util/main-font-text.hh
util/options.cc
util/options.hh
util/shape-consumer.hh
util/view-cairo.hh

index 859f9a6..be71a89 100644 (file)
@@ -53,14 +53,15 @@ struct shape_closure_consumer_t : option_group_t
                       this);
   }
 
-  void init (const font_options_t *font_opts)
+  void init (hb_buffer_t  *buffer_,
+            const font_options_t *font_opts)
   {
     glyphs = hb_set_create ();
     font = hb_font_reference (font_opts->get_font ());
     failed = false;
+    buffer = hb_buffer_reference (buffer_);
   }
-  void consume_line (hb_buffer_t  *buffer,
-                    const char   *text,
+  void consume_line (const char   *text,
                     unsigned int  text_len,
                     const char   *text_before,
                     const char   *text_after)
@@ -95,6 +96,8 @@ struct shape_closure_consumer_t : option_group_t
     font = NULL;
     hb_set_destroy (glyphs);
     glyphs = NULL;
+    hb_buffer_destroy (buffer);
+    buffer = NULL;
   }
 
   bool failed;
@@ -105,6 +108,7 @@ struct shape_closure_consumer_t : option_group_t
 
   hb_set_t *glyphs;
   hb_font_t *font;
+  hb_buffer_t *buffer;
 };
 
 int
index 75c3793..8858431 100644 (file)
@@ -39,7 +39,7 @@ struct output_buffer_t
                    output_format (HB_BUFFER_SERIALIZE_FORMAT_INVALID),
                    format_flags (HB_BUFFER_SERIALIZE_FLAG_DEFAULT) {}
 
-  void init (const font_options_t *font_opts)
+  void init (hb_buffer_t *buffer, const font_options_t *font_opts)
   {
     options.get_file_handle ();
     gs = g_string_new (NULL);
@@ -75,6 +75,9 @@ struct output_buffer_t
     if (format.show_extents)
       flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS;
     format_flags = (hb_buffer_serialize_flags_t) flags;
+
+    if (format.show_messages)
+      hb_buffer_set_message_func (buffer, message_func, this, NULL);
   }
   void new_line (void)
   {
@@ -108,14 +111,40 @@ struct output_buffer_t
                                       output_format, format_flags, gs);
     fprintf (options.fp, "%s", gs->str);
   }
-  void finish (const font_options_t *font_opts)
+  void finish (hb_buffer_t *buffer, const font_options_t *font_opts)
   {
+    hb_buffer_set_message_func (buffer, NULL, NULL, NULL);
     hb_font_destroy (font);
     g_string_free (gs, true);
     gs = NULL;
     font = NULL;
   }
 
+  static hb_bool_t
+  message_func (hb_buffer_t *buffer,
+               hb_font_t *font,
+               const char *message,
+               void *user_data)
+  {
+    output_buffer_t *that = (output_buffer_t *) user_data;
+    that->message (buffer, font, message);
+    return true;
+  }
+
+  void
+  message (hb_buffer_t *buffer,
+          hb_font_t *font,
+          const char *message)
+  {
+    g_string_set_size (gs, 0);
+    format.serialize_line_no (line_no, gs);
+    g_string_append_printf (gs, "HB: %s        buffer: ", message);
+    format.serialize_glyphs (buffer, font, output_format, format_flags, gs);
+    g_string_append_c (gs, '\n');
+    fprintf (options.fp, "%s", gs->str);
+  }
+
+
   protected:
   output_options_t options;
   format_options_t format;
index 55de567..51f1627 100644 (file)
@@ -46,23 +46,6 @@ locale_to_utf8 (char *s)
   return t;
 }
 
-static hb_bool_t
-message_func (hb_buffer_t *buffer,
-             hb_font_t *font,
-             const char *message,
-             void *user_data)
-{
-  fprintf (stderr, "HB: %s\n", message);
-  char buf[4096];
-  hb_buffer_serialize_glyphs (buffer, 0, hb_buffer_get_length (buffer),
-                             buf, sizeof (buf), NULL,
-                             font,
-                             HB_BUFFER_SERIALIZE_FORMAT_TEXT,
-                             HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
-  fprintf (stderr, "HB: buffer [%s]\n", buf);
-  return true;
-}
-
 template <typename consumer_t, int default_font_size, int subpixel_bits>
 struct main_font_text_t
 {
@@ -87,16 +70,14 @@ struct main_font_text_t
     if (!input.text && !input.text_file)
       input.text_file = g_strdup ("-");
 
-    consumer.init (&font_opts);
-
     hb_buffer_t *buffer = hb_buffer_create ();
-    if (debug)
-      hb_buffer_set_message_func (buffer, message_func, NULL, NULL);
+    consumer.init (buffer, &font_opts);
+    hb_buffer_destroy (buffer);
+
     unsigned int text_len;
     const char *text;
     while ((text = input.get_line (&text_len)))
-      consumer.consume_line (buffer, text, text_len, input.text_before, input.text_after);
-    hb_buffer_destroy (buffer);
+      consumer.consume_line (text, text_len, input.text_before, input.text_after);
 
     consumer.finish (&font_opts);
 
index 0f2e207..ee74964 100644 (file)
@@ -777,6 +777,7 @@ format_options_t::add_options (option_parser_t *parser)
     {"no-clusters",    0, G_OPTION_FLAG_REVERSE,
                              G_OPTION_ARG_NONE,        &this->show_clusters,           "Do not output cluster indices",                                        NULL},
     {"show-extents",   0, 0, G_OPTION_ARG_NONE,        &this->show_extents,            "Output glyph extents",                                                 NULL},
+    {"show-messages",  0, 0, G_OPTION_ARG_NONE,        &this->show_messages,           "Output shaping debug messages",                                        NULL},
     {NULL}
   };
   parser->add_group (entries,
@@ -818,7 +819,8 @@ format_options_t::serialize_glyphs (hb_buffer_t *buffer,
   unsigned int num_glyphs = hb_buffer_get_length (buffer);
   unsigned int start = 0;
 
-  while (start < num_glyphs) {
+  while (start < num_glyphs)
+  {
     char buf[1024];
     unsigned int consumed;
     start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
@@ -845,7 +847,8 @@ format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
                                            hb_font_t    *font,
                                            GString      *gs)
 {
-  if (show_text) {
+  if (show_text)
+  {
     serialize_line_no (line_no, gs);
     g_string_append_c (gs, '(');
     g_string_append_len (gs, text, text_len);
@@ -853,7 +856,8 @@ format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
     g_string_append_c (gs, '\n');
   }
 
-  if (show_unicode) {
+  if (show_unicode)
+  {
     serialize_line_no (line_no, gs);
     serialize_unicode (buffer, gs);
     g_string_append_c (gs, '\n');
index 9ed4fd0..ff6a61b 100644 (file)
@@ -437,6 +437,7 @@ struct format_options_t : option_group_t
     show_unicode = false;
     show_line_num = false;
     show_extents = false;
+    show_messages = false;
 
     add_options (parser);
   }
@@ -478,6 +479,7 @@ struct format_options_t : option_group_t
   hb_bool_t show_unicode;
   hb_bool_t show_line_num;
   hb_bool_t show_extents;
+  hb_bool_t show_messages;
 };
 
 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
index 422c8cd..7849242 100644 (file)
@@ -39,14 +39,16 @@ struct shape_consumer_t
                    output (parser),
                    font (NULL) {}
 
-  void init (const font_options_t *font_opts)
+  void init (hb_buffer_t  *buffer_,
+            const font_options_t *font_opts)
   {
     font = hb_font_reference (font_opts->get_font ());
-    output.init (font_opts);
     failed = false;
+    buffer = hb_buffer_reference (buffer_);
+
+    output.init (buffer, font_opts);
   }
-  void consume_line (hb_buffer_t  *buffer,
-                    const char   *text,
+  void consume_line (const char   *text,
                     unsigned int  text_len,
                     const char   *text_before,
                     const char   *text_after)
@@ -58,7 +60,8 @@ struct shape_consumer_t
       shaper.populate_buffer (buffer, text, text_len, text_before, text_after);
       if (n == 1)
        output.consume_text (buffer, text, text_len, shaper.utf8_clusters);
-      if (!shaper.shape (font, buffer)) {
+      if (!shaper.shape (font, buffer))
+      {
        failed = true;
        hb_buffer_set_length (buffer, 0);
        output.shape_failed (buffer, text, text_len, shaper.utf8_clusters);
@@ -70,9 +73,11 @@ struct shape_consumer_t
   }
   void finish (const font_options_t *font_opts)
   {
-    output.finish (font_opts);
+    output.finish (buffer, font_opts);
     hb_font_destroy (font);
     font = NULL;
+    hb_buffer_destroy (buffer);
+    buffer = NULL;
   }
 
   public:
@@ -83,6 +88,7 @@ struct shape_consumer_t
   output_t output;
 
   hb_font_t *font;
+  hb_buffer_t *buffer;
 };
 
 
index f55d4bb..ef229ff 100644 (file)
@@ -43,7 +43,7 @@ struct view_cairo_t
       cairo_debug_reset_static_data ();
   }
 
-  void init (const font_options_t *font_opts)
+  void init (hb_buffer_t *buffer, const font_options_t *font_opts)
   {
     lines = g_array_new (false, false, sizeof (helper_cairo_line_t));
     scale_bits = -font_opts->subpixel_bits;
@@ -74,7 +74,7 @@ struct view_cairo_t
     helper_cairo_line_from_buffer (&l, buffer, text, text_len, scale_bits, utf8_clusters);
     g_array_append_val (lines, l);
   }
-  void finish (const font_options_t *font_opts)
+  void finish (hb_buffer_t *buffer, const font_options_t *font_opts)
   {
     render (font_opts);