[hb-shape] Improve shaping-debug output

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
This commit is contained in:
Behdad Esfahbod 2017-07-18 19:14:19 -07:00
parent 65f64d1400
commit e60350551a
7 changed files with 65 additions and 39 deletions

View File

@ -53,14 +53,15 @@ struct shape_closure_consumer_t : option_group_t
this); 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 (); glyphs = hb_set_create ();
font = hb_font_reference (font_opts->get_font ()); font = hb_font_reference (font_opts->get_font ());
failed = false; failed = false;
buffer = hb_buffer_reference (buffer_);
} }
void consume_line (hb_buffer_t *buffer, void consume_line (const char *text,
const char *text,
unsigned int text_len, unsigned int text_len,
const char *text_before, const char *text_before,
const char *text_after) const char *text_after)
@ -95,6 +96,8 @@ struct shape_closure_consumer_t : option_group_t
font = NULL; font = NULL;
hb_set_destroy (glyphs); hb_set_destroy (glyphs);
glyphs = NULL; glyphs = NULL;
hb_buffer_destroy (buffer);
buffer = NULL;
} }
bool failed; bool failed;
@ -105,6 +108,7 @@ struct shape_closure_consumer_t : option_group_t
hb_set_t *glyphs; hb_set_t *glyphs;
hb_font_t *font; hb_font_t *font;
hb_buffer_t *buffer;
}; };
int int

View File

@ -39,7 +39,7 @@ struct output_buffer_t
output_format (HB_BUFFER_SERIALIZE_FORMAT_INVALID), output_format (HB_BUFFER_SERIALIZE_FORMAT_INVALID),
format_flags (HB_BUFFER_SERIALIZE_FLAG_DEFAULT) {} 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 (); options.get_file_handle ();
gs = g_string_new (NULL); gs = g_string_new (NULL);
@ -75,6 +75,9 @@ struct output_buffer_t
if (format.show_extents) if (format.show_extents)
flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS; flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS;
format_flags = (hb_buffer_serialize_flags_t) flags; 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) void new_line (void)
{ {
@ -108,14 +111,40 @@ struct output_buffer_t
output_format, format_flags, gs); output_format, format_flags, gs);
fprintf (options.fp, "%s", gs->str); 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); hb_font_destroy (font);
g_string_free (gs, true); g_string_free (gs, true);
gs = NULL; gs = NULL;
font = 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: protected:
output_options_t options; output_options_t options;
format_options_t format; format_options_t format;

View File

@ -46,23 +46,6 @@ locale_to_utf8 (char *s)
return t; 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> template <typename consumer_t, int default_font_size, int subpixel_bits>
struct main_font_text_t struct main_font_text_t
{ {
@ -87,16 +70,14 @@ struct main_font_text_t
if (!input.text && !input.text_file) if (!input.text && !input.text_file)
input.text_file = g_strdup ("-"); input.text_file = g_strdup ("-");
consumer.init (&font_opts);
hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_t *buffer = hb_buffer_create ();
if (debug) consumer.init (buffer, &font_opts);
hb_buffer_set_message_func (buffer, message_func, NULL, NULL); hb_buffer_destroy (buffer);
unsigned int text_len; unsigned int text_len;
const char *text; const char *text;
while ((text = input.get_line (&text_len))) while ((text = input.get_line (&text_len)))
consumer.consume_line (buffer, text, text_len, input.text_before, input.text_after); consumer.consume_line (text, text_len, input.text_before, input.text_after);
hb_buffer_destroy (buffer);
consumer.finish (&font_opts); consumer.finish (&font_opts);

View File

@ -777,6 +777,7 @@ format_options_t::add_options (option_parser_t *parser)
{"no-clusters", 0, G_OPTION_FLAG_REVERSE, {"no-clusters", 0, G_OPTION_FLAG_REVERSE,
G_OPTION_ARG_NONE, &this->show_clusters, "Do not output cluster indices", NULL}, 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-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} {NULL}
}; };
parser->add_group (entries, 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 num_glyphs = hb_buffer_get_length (buffer);
unsigned int start = 0; unsigned int start = 0;
while (start < num_glyphs) { while (start < num_glyphs)
{
char buf[1024]; char buf[1024];
unsigned int consumed; unsigned int consumed;
start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs, 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, hb_font_t *font,
GString *gs) GString *gs)
{ {
if (show_text) { if (show_text)
{
serialize_line_no (line_no, gs); serialize_line_no (line_no, gs);
g_string_append_c (gs, '('); g_string_append_c (gs, '(');
g_string_append_len (gs, text, text_len); 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'); g_string_append_c (gs, '\n');
} }
if (show_unicode) { if (show_unicode)
{
serialize_line_no (line_no, gs); serialize_line_no (line_no, gs);
serialize_unicode (buffer, gs); serialize_unicode (buffer, gs);
g_string_append_c (gs, '\n'); g_string_append_c (gs, '\n');

View File

@ -437,6 +437,7 @@ struct format_options_t : option_group_t
show_unicode = false; show_unicode = false;
show_line_num = false; show_line_num = false;
show_extents = false; show_extents = false;
show_messages = false;
add_options (parser); add_options (parser);
} }
@ -478,6 +479,7 @@ struct format_options_t : option_group_t
hb_bool_t show_unicode; hb_bool_t show_unicode;
hb_bool_t show_line_num; hb_bool_t show_line_num;
hb_bool_t show_extents; hb_bool_t show_extents;
hb_bool_t show_messages;
}; };
/* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */ /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */

View File

@ -39,14 +39,16 @@ struct shape_consumer_t
output (parser), output (parser),
font (NULL) {} 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 ()); font = hb_font_reference (font_opts->get_font ());
output.init (font_opts);
failed = false; failed = false;
buffer = hb_buffer_reference (buffer_);
output.init (buffer, font_opts);
} }
void consume_line (hb_buffer_t *buffer, void consume_line (const char *text,
const char *text,
unsigned int text_len, unsigned int text_len,
const char *text_before, const char *text_before,
const char *text_after) const char *text_after)
@ -58,7 +60,8 @@ struct shape_consumer_t
shaper.populate_buffer (buffer, text, text_len, text_before, text_after); shaper.populate_buffer (buffer, text, text_len, text_before, text_after);
if (n == 1) if (n == 1)
output.consume_text (buffer, text, text_len, shaper.utf8_clusters); output.consume_text (buffer, text, text_len, shaper.utf8_clusters);
if (!shaper.shape (font, buffer)) { if (!shaper.shape (font, buffer))
{
failed = true; failed = true;
hb_buffer_set_length (buffer, 0); hb_buffer_set_length (buffer, 0);
output.shape_failed (buffer, text, text_len, shaper.utf8_clusters); 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) void finish (const font_options_t *font_opts)
{ {
output.finish (font_opts); output.finish (buffer, font_opts);
hb_font_destroy (font); hb_font_destroy (font);
font = NULL; font = NULL;
hb_buffer_destroy (buffer);
buffer = NULL;
} }
public: public:
@ -83,6 +88,7 @@ struct shape_consumer_t
output_t output; output_t output;
hb_font_t *font; hb_font_t *font;
hb_buffer_t *buffer;
}; };

View File

@ -43,7 +43,7 @@ struct view_cairo_t
cairo_debug_reset_static_data (); 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)); lines = g_array_new (false, false, sizeof (helper_cairo_line_t));
scale_bits = -font_opts->subpixel_bits; 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); helper_cairo_line_from_buffer (&l, buffer, text, text_len, scale_bits, utf8_clusters);
g_array_append_val (lines, l); 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); render (font_opts);