options.cc revision 0f68f4a0b5ee78cbdb2a89a9a1a9125afe72ed2f
1/*
2 * Copyright © 2011  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#include "options.hh"
28
29#ifdef HAVE_FREETYPE
30#include <hb-ft.h>
31#endif
32
33
34void
35fail (hb_bool_t suggest_help, const char *format, ...)
36{
37  const char *msg;
38
39  va_list vap;
40  va_start (vap, format);
41  msg = g_strdup_vprintf (format, vap);
42  const char *prgname = g_get_prgname ();
43  g_printerr ("%s: %s\n", prgname, msg);
44  if (suggest_help)
45    g_printerr ("Try `%s --help' for more information.\n", prgname);
46
47  exit (1);
48}
49
50
51hb_bool_t debug = FALSE;
52
53static gchar *
54shapers_to_string (void)
55{
56  GString *shapers = g_string_new (NULL);
57  const char **shaper_list = hb_shape_list_shapers ();
58
59  for (; *shaper_list; shaper_list++) {
60    g_string_append (shapers, *shaper_list);
61    g_string_append_c (shapers, ',');
62  }
63  g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
64
65  return g_string_free (shapers, FALSE);
66}
67
68static G_GNUC_NORETURN gboolean
69show_version (const char *name G_GNUC_UNUSED,
70	      const char *arg G_GNUC_UNUSED,
71	      gpointer    data G_GNUC_UNUSED,
72	      GError    **error G_GNUC_UNUSED)
73{
74  g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
75
76  char *shapers = shapers_to_string ();
77  g_printf ("Available shapers: %s\n", shapers);
78  g_free (shapers);
79  if (strcmp (HB_VERSION_STRING, hb_version_string ()))
80    g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
81
82  exit(0);
83}
84
85
86void
87option_parser_t::add_main_options (void)
88{
89  GOptionEntry entries[] =
90  {
91    {"version",		0, G_OPTION_FLAG_NO_ARG,
92			      G_OPTION_ARG_CALLBACK,	(gpointer) &show_version,	"Show version numbers",			NULL},
93    {"debug",		0, 0, G_OPTION_ARG_NONE,	&debug,				"Free all resources before exit",	NULL},
94    {NULL}
95  };
96  g_option_context_add_main_entries (context, entries, NULL);
97}
98
99static gboolean
100pre_parse (GOptionContext *context G_GNUC_UNUSED,
101	   GOptionGroup *group G_GNUC_UNUSED,
102	   gpointer data,
103	   GError **error)
104{
105  option_group_t *option_group = (option_group_t *) data;
106  option_group->pre_parse (error);
107  return *error == NULL;
108}
109
110static gboolean
111post_parse (GOptionContext *context G_GNUC_UNUSED,
112	    GOptionGroup *group G_GNUC_UNUSED,
113	    gpointer data,
114	    GError **error)
115{
116  option_group_t *option_group = static_cast<option_group_t *>(data);
117  option_group->post_parse (error);
118  return *error == NULL;
119}
120
121void
122option_parser_t::add_group (GOptionEntry   *entries,
123			    const gchar    *name,
124			    const gchar    *description,
125			    const gchar    *help_description,
126			    option_group_t *option_group)
127{
128  GOptionGroup *group = g_option_group_new (name, description, help_description,
129					    static_cast<gpointer>(option_group), NULL);
130  g_option_group_add_entries (group, entries);
131  g_option_group_set_parse_hooks (group, pre_parse, post_parse);
132  g_option_context_add_group (context, group);
133}
134
135void
136option_parser_t::parse (int *argc, char ***argv)
137{
138  setlocale (LC_ALL, "");
139
140  GError *parse_error = NULL;
141  if (!g_option_context_parse (context, argc, argv, &parse_error))
142  {
143    if (parse_error != NULL) {
144      fail (TRUE, "%s", parse_error->message);
145      //g_error_free (parse_error);
146    } else
147      fail (TRUE, "Option parse error");
148  }
149}
150
151
152static gboolean
153parse_margin (const char *name G_GNUC_UNUSED,
154	      const char *arg,
155	      gpointer    data,
156	      GError    **error G_GNUC_UNUSED)
157{
158  view_options_t *view_opts = (view_options_t *) data;
159  view_options_t::margin_t &m = view_opts->margin;
160  switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
161    case 1: m.r = m.t;
162    case 2: m.b = m.t;
163    case 3: m.l = m.r;
164    case 4: return TRUE;
165    default:
166      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
167		   "%s argument should be one to four space-separated numbers",
168		   name);
169      return FALSE;
170  }
171}
172
173
174static gboolean
175parse_shapers (const char *name G_GNUC_UNUSED,
176	       const char *arg,
177	       gpointer    data,
178	       GError    **error G_GNUC_UNUSED)
179{
180  shape_options_t *shape_opts = (shape_options_t *) data;
181  shape_opts->shapers = g_strsplit (arg, ",", 0);
182  return TRUE;
183}
184
185static G_GNUC_NORETURN gboolean
186list_shapers (const char *name G_GNUC_UNUSED,
187	      const char *arg G_GNUC_UNUSED,
188	      gpointer    data G_GNUC_UNUSED,
189	      GError    **error G_GNUC_UNUSED)
190{
191  for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
192    g_printf ("%s\n", *shaper);
193
194  exit(0);
195}
196
197
198
199static void
200parse_space (char **pp)
201{
202  char c;
203#define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
204  while (c = **pp, ISSPACE (c))
205    (*pp)++;
206#undef ISSPACE
207}
208
209static hb_bool_t
210parse_char (char **pp, char c)
211{
212  parse_space (pp);
213
214  if (**pp != c)
215    return FALSE;
216
217  (*pp)++;
218  return TRUE;
219}
220
221static hb_bool_t
222parse_uint (char **pp, unsigned int *pv)
223{
224  char *p = *pp;
225  unsigned int v;
226
227  v = strtol (p, pp, 0);
228
229  if (p == *pp)
230    return FALSE;
231
232  *pv = v;
233  return TRUE;
234}
235
236
237static hb_bool_t
238parse_feature_value_prefix (char **pp, hb_feature_t *feature)
239{
240  if (parse_char (pp, '-'))
241    feature->value = 0;
242  else {
243    parse_char (pp, '+');
244    feature->value = 1;
245  }
246
247  return TRUE;
248}
249
250static hb_bool_t
251parse_feature_tag (char **pp, hb_feature_t *feature)
252{
253  char *p = *pp, c;
254
255  parse_space (pp);
256
257#define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
258  while (c = **pp, ISALNUM(c))
259    (*pp)++;
260#undef ISALNUM
261
262  if (p == *pp)
263    return FALSE;
264
265  feature->tag = hb_tag_from_string (p, *pp - p);
266  return TRUE;
267}
268
269static hb_bool_t
270parse_feature_indices (char **pp, hb_feature_t *feature)
271{
272  parse_space (pp);
273
274  hb_bool_t has_start;
275
276  feature->start = 0;
277  feature->end = (unsigned int) -1;
278
279  if (!parse_char (pp, '['))
280    return TRUE;
281
282  has_start = parse_uint (pp, &feature->start);
283
284  if (parse_char (pp, ':')) {
285    parse_uint (pp, &feature->end);
286  } else {
287    if (has_start)
288      feature->end = feature->start + 1;
289  }
290
291  return parse_char (pp, ']');
292}
293
294static hb_bool_t
295parse_feature_value_postfix (char **pp, hb_feature_t *feature)
296{
297  return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
298}
299
300
301static hb_bool_t
302parse_one_feature (char **pp, hb_feature_t *feature)
303{
304  return parse_feature_value_prefix (pp, feature) &&
305	 parse_feature_tag (pp, feature) &&
306	 parse_feature_indices (pp, feature) &&
307	 parse_feature_value_postfix (pp, feature) &&
308	 (parse_char (pp, ',') || **pp == '\0');
309}
310
311static void
312skip_one_feature (char **pp)
313{
314  char *e;
315  e = strchr (*pp, ',');
316  if (e)
317    *pp = e + 1;
318  else
319    *pp = *pp + strlen (*pp);
320}
321
322static gboolean
323parse_features (const char *name G_GNUC_UNUSED,
324	        const char *arg,
325	        gpointer    data,
326	        GError    **error G_GNUC_UNUSED)
327{
328  shape_options_t *shape_opts = (shape_options_t *) data;
329  char *s = (char *) arg;
330  char *p;
331
332  shape_opts->num_features = 0;
333  shape_opts->features = NULL;
334
335  if (!*s)
336    return TRUE;
337
338  /* count the features first, so we can allocate memory */
339  p = s;
340  do {
341    shape_opts->num_features++;
342    p = strchr (p, ',');
343    if (p)
344      p++;
345  } while (p);
346
347  shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
348
349  /* now do the actual parsing */
350  p = s;
351  shape_opts->num_features = 0;
352  while (*p) {
353    if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features]))
354      shape_opts->num_features++;
355    else
356      skip_one_feature (&p);
357  }
358
359  return TRUE;
360}
361
362
363void
364view_options_t::add_options (option_parser_t *parser)
365{
366  GOptionEntry entries[] =
367  {
368    {"annotate",	0, 0, G_OPTION_ARG_NONE,	&this->annotate,		"Annotate output rendering",				NULL},
369    {"background",	0, 0, G_OPTION_ARG_STRING,	&this->back,			"Set background color (default: "DEFAULT_BACK")",	"red/#rrggbb/#rrggbbaa"},
370    {"foreground",	0, 0, G_OPTION_ARG_STRING,	&this->fore,			"Set foreground color (default: "DEFAULT_FORE")",	"red/#rrggbb/#rrggbbaa"},
371    {"line-space",	0, 0, G_OPTION_ARG_DOUBLE,	&this->line_space,		"Set space between lines (default: 0)",			"units"},
372    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_margin,	"Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"},
373    {"font-size",	0, 0, G_OPTION_ARG_DOUBLE,	&this->font_size,		"Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"},
374    {NULL}
375  };
376  parser->add_group (entries,
377		     "view",
378		     "View options:",
379		     "Options controlling the output rendering",
380		     this);
381}
382
383void
384shape_options_t::add_options (option_parser_t *parser)
385{
386  GOptionEntry entries[] =
387  {
388    {"list-shapers",	0, G_OPTION_FLAG_NO_ARG,
389			      G_OPTION_ARG_CALLBACK,	(gpointer) &list_shapers,	"List available shapers and quit",	NULL},
390    {"shapers",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Comma-separated list of shapers to try","list"},
391    {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
392    {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
393    {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
394    {NULL}
395  };
396  parser->add_group (entries,
397		     "shape",
398		     "Shape options:",
399		     "Options controlling the shaping process",
400		     this);
401
402  const gchar *features_help = "\n"
403    "\n"
404    "    Comma-separated list of font features to apply to text\n"
405    "\n"
406    "    Features can be enabled or disabled, either globally or limited to\n"
407    "    specific byte ranges. The format is Python-esque.  Here is how it all\n"
408    "    works:\n"
409    "\n"
410    "      Syntax:       Value:    Start:    End:\n"
411    "\n"
412    "    Setting value:\n"
413    "      \"kern\"        1         0         ∞         # Turn feature on\n"
414    "      \"+kern\"       1         0         ∞         # Turn feature on\n"
415    "      \"-kern\"       0         0         ∞         # Turn feature off\n"
416    "      \"kern=0\"      0         0         ∞         # Turn feature off\n"
417    "      \"kern=1\"      1         0         ∞         # Turn feature on\n"
418    "      \"aalt=2\"      2         0         ∞         # Choose 2nd alternate\n"
419    "\n"
420    "    Setting index:\n"
421    "      \"kern[]\"      1         0         ∞         # Turn feature on\n"
422    "      \"kern[:]\"     1         0         ∞         # Turn feature on\n"
423    "      \"kern[5:]\"    1         5         ∞         # Turn feature on, partial\n"
424    "      \"kern[:5]\"    1         0         5         # Turn feature on, partial\n"
425    "      \"kern[3:5]\"   1         3         5         # Turn feature on, range\n"
426    "      \"kern[3]\"     1         3         3+1       # Turn feature on, single char\n"
427    "\n"
428    "    Mixing it all:\n"
429    "\n"
430    "      \"kern[3:5]=0\" 1         3         5         # Turn feature off for range";
431
432  GOptionEntry entries2[] =
433  {
434    {"features",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_features,	features_help,	"list"},
435    {NULL}
436  };
437  parser->add_group (entries2,
438		     "features",
439		     "Features options:",
440		     "Options controlling the OpenType font features applied",
441		     this);
442}
443
444void
445font_options_t::add_options (option_parser_t *parser)
446{
447  GOptionEntry entries[] =
448  {
449    {"font-file",	0, 0, G_OPTION_ARG_STRING,	&this->font_file,		"Font file-name",					"filename"},
450    {"face-index",	0, 0, G_OPTION_ARG_INT,		&this->face_index,		"Face index (default: 0)",                              "index"},
451    {NULL}
452  };
453  parser->add_group (entries,
454		     "font",
455		     "Font options:",
456		     "Options controlling the font",
457		     this);
458}
459
460void
461text_options_t::add_options (option_parser_t *parser)
462{
463  GOptionEntry entries[] =
464  {
465    {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
466    {"text-file",	0, 0, G_OPTION_ARG_STRING,	&this->text_file,		"Set input text file-name",		"filename"},
467    {NULL}
468  };
469  parser->add_group (entries,
470		     "text",
471		     "Text options:",
472		     "Options controlling the input text",
473		     this);
474}
475
476void
477output_options_t::add_options (option_parser_t *parser)
478{
479  GOptionEntry entries[] =
480  {
481    {"output-file",	0, 0, G_OPTION_ARG_STRING,	&this->output_file,		"Set output file-name (default: stdout)","filename"},
482    {"output-format",	0, 0, G_OPTION_ARG_STRING,	&this->output_format,		"Set output format",			"format"},
483    {NULL}
484  };
485  parser->add_group (entries,
486		     "output",
487		     "Output options:",
488		     "Options controlling the output",
489		     this);
490}
491
492
493
494hb_font_t *
495font_options_t::get_font (void) const
496{
497  if (font)
498    return font;
499
500  hb_blob_t *blob = NULL;
501
502  /* Create the blob */
503  {
504    char *font_data;
505    unsigned int len = 0;
506    hb_destroy_func_t destroy;
507    void *user_data;
508    hb_memory_mode_t mm;
509
510    /* This is a hell of a lot of code for just reading a file! */
511    if (!font_file)
512      fail (TRUE, "No font file set");
513
514    if (0 == strcmp (font_file, "-")) {
515      /* read it */
516      GString *gs = g_string_new (NULL);
517      char buf[BUFSIZ];
518#ifdef HAVE__SETMODE
519      _setmode (fileno (stdin), _O_BINARY);
520#endif
521      while (!feof (stdin)) {
522	size_t ret = fread (buf, 1, sizeof (buf), stdin);
523	if (ferror (stdin))
524	  fail (FALSE, "Failed reading font from standard input: %s",
525		strerror (errno));
526	g_string_append_len (gs, buf, ret);
527      }
528      len = gs->len;
529      font_data = g_string_free (gs, FALSE);
530      user_data = font_data;
531      destroy = (hb_destroy_func_t) g_free;
532      mm = HB_MEMORY_MODE_WRITABLE;
533    } else {
534      GMappedFile *mf = g_mapped_file_new (font_file, FALSE, NULL);
535      if (mf) {
536	font_data = g_mapped_file_get_contents (mf);
537	len = g_mapped_file_get_length (mf);
538	if (len) {
539	  destroy = (hb_destroy_func_t) g_mapped_file_unref;
540	  user_data = (void *) mf;
541	  mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
542	} else
543	  g_mapped_file_unref (mf);
544      }
545      if (!len) {
546	/* GMappedFile is buggy, it doesn't fail if file isn't regular.
547	 * Try reading.
548	 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
549        GError *error = NULL;
550	gsize l;
551	if (g_file_get_contents (font_file, &font_data, &l, &error)) {
552	  len = l;
553	  destroy = (hb_destroy_func_t) g_free;
554	  user_data = (void *) font_data;
555	  mm = HB_MEMORY_MODE_WRITABLE;
556	} else {
557	  fail (FALSE, "%s", error->message);
558	  //g_error_free (error);
559	}
560      }
561    }
562
563    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
564  }
565
566  /* Create the face */
567  hb_face_t *face = hb_face_create (blob, face_index);
568  hb_blob_destroy (blob);
569
570
571  font = hb_font_create (face);
572
573  unsigned int upem = hb_face_get_upem (face);
574  hb_font_set_scale (font, upem, upem);
575  hb_face_destroy (face);
576
577#ifdef HAVE_FREETYPE
578  hb_ft_font_set_funcs (font);
579#endif
580
581  return font;
582}
583
584
585const char *
586text_options_t::get_line (unsigned int *len)
587{
588  if (text) {
589    if (text_len == (unsigned int) -1)
590      text_len = strlen (text);
591
592    if (!text_len) {
593      *len = 0;
594      return NULL;
595    }
596
597    const char *ret = text;
598    const char *p = (const char *) memchr (text, '\n', text_len);
599    unsigned int ret_len;
600    if (!p) {
601      ret_len = text_len;
602      text += ret_len;
603      text_len = 0;
604    } else {
605      ret_len = p - ret;
606      text += ret_len + 1;
607      text_len -= ret_len + 1;
608    }
609
610    *len = ret_len;
611    return ret;
612  }
613
614  if (!fp) {
615    if (!text_file)
616      fail (TRUE, "At least one of text or text-file must be set");
617
618    if (0 != strcmp (text_file, "-"))
619      fp = fopen (text_file, "r");
620    else
621      fp = stdin;
622
623    if (!fp)
624      fail (FALSE, "Failed opening text file `%s': %s",
625	    text_file, strerror (errno));
626
627    gs = g_string_new (NULL);
628  }
629
630  g_string_set_size (gs, 0);
631  char buf[BUFSIZ];
632  while (fgets (buf, sizeof (buf), fp)) {
633    unsigned int bytes = strlen (buf);
634    if (bytes && buf[bytes - 1] == '\n') {
635      bytes--;
636      g_string_append_len (gs, buf, bytes);
637      break;
638    }
639      g_string_append_len (gs, buf, bytes);
640  }
641  if (ferror (fp))
642    fail (FALSE, "Failed reading text: %s",
643	  strerror (errno));
644  *len = gs->len;
645  return !*len && feof (fp) ? NULL : gs->str;
646}
647
648
649FILE *
650output_options_t::get_file_handle (void)
651{
652  if (fp)
653    return fp;
654
655  if (output_file)
656    fp = fopen (output_file, "wb");
657  else {
658#ifdef HAVE__SETMODE
659    _setmode (fileno (stdout), _O_BINARY);
660#endif
661    fp = stdout;
662  }
663  if (!fp)
664    fail (FALSE, "Cannot open output file `%s': %s",
665	  g_filename_display_name (output_file), strerror (errno));
666
667  return fp;
668}
669
670
671void
672format_options_t::add_options (option_parser_t *parser)
673{
674  GOptionEntry entries[] =
675  {
676    {"no-glyph-names",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_glyph_names,	"Use glyph indices instead of names",	NULL},
677    {"no-positions",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_positions,		"Do not show glyph positions",		NULL},
678    {"no-clusters",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_clusters,		"Do not show cluster mapping",		NULL},
679    {"show-text",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_text,		"Show input text",			NULL},
680    {"show-unicode",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_unicode,		"Show input Unicode codepoints",	NULL},
681    {"show-line-num",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_line_num,		"Show line numbers",			NULL},
682    {NULL}
683  };
684  parser->add_group (entries,
685		     "format",
686		     "Format options:",
687		     "Options controlling the formatting of buffer contents",
688		     this);
689}
690
691void
692format_options_t::serialize_unicode (hb_buffer_t *buffer,
693				     GString     *gs)
694{
695  unsigned int num_glyphs = hb_buffer_get_length (buffer);
696  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
697
698  g_string_append_c (gs, '<');
699  for (unsigned int i = 0; i < num_glyphs; i++)
700  {
701    if (i)
702      g_string_append_c (gs, ',');
703    g_string_append_printf (gs, "U+%04X", info->codepoint);
704    info++;
705  }
706  g_string_append_c (gs, '>');
707}
708
709void
710format_options_t::serialize_glyphs (hb_buffer_t *buffer,
711				    hb_font_t   *font,
712				    GString     *gs)
713{
714  FT_Face ft_face = show_glyph_names ? hb_ft_font_get_face (font) : NULL;
715
716  unsigned int num_glyphs = hb_buffer_get_length (buffer);
717  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
718  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
719
720  g_string_append_c (gs, '[');
721  for (unsigned int i = 0; i < num_glyphs; i++)
722  {
723    if (i)
724      g_string_append_c (gs, '|');
725
726    char glyph_name[30];
727    if (show_glyph_names) {
728      if (!FT_Get_Glyph_Name (ft_face, info->codepoint, glyph_name, sizeof (glyph_name)))
729	g_string_append_printf (gs, "%s", glyph_name);
730      else
731	g_string_append_printf (gs, "gid%u", info->codepoint);
732    } else
733      g_string_append_printf (gs, "%u", info->codepoint);
734
735    if (show_clusters)
736      g_string_append_printf (gs, "=%u", info->cluster);
737
738    if (show_positions && (pos->x_offset || pos->y_offset)) {
739      g_string_append_c (gs, '@');
740      if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset);
741      if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset);
742    }
743    if (show_positions && (pos->x_advance || pos->y_advance)) {
744      g_string_append_c (gs, '+');
745      if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance);
746      if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance);
747    }
748
749    info++;
750    pos++;
751  }
752  g_string_append_c (gs, ']');
753}
754void
755format_options_t::serialize_line_no (unsigned int  line_no,
756				     GString      *gs)
757{
758  if (show_line_num)
759    g_string_append_printf (gs, "%d: ", line_no);
760}
761void
762format_options_t::serialize_line (hb_buffer_t  *buffer,
763				  unsigned int  line_no,
764				  const char   *text,
765				  unsigned int  text_len,
766				  hb_font_t    *font,
767				  GString      *gs)
768{
769  if (show_text) {
770    serialize_line_no (line_no, gs);
771    g_string_append_c (gs, '(');
772    g_string_append_len (gs, text, text_len);
773    g_string_append_c (gs, ')');
774    g_string_append_c (gs, '\n');
775  }
776
777  if (show_unicode) {
778    serialize_line_no (line_no, gs);
779    hb_buffer_reset (scratch);
780    hb_buffer_add_utf8 (scratch, text, text_len, 0, -1);
781    serialize_unicode (scratch, gs);
782    g_string_append_c (gs, '\n');
783  }
784
785  serialize_line_no (line_no, gs);
786  serialize_glyphs (buffer, font, gs);
787  g_string_append_c (gs, '\n');
788}
789