options.cc revision c8149ca85ed97112778590bc9f090f3ee0254100
1/*
2 * Copyright © 2011,2012  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  g_strfreev (shape_opts->shapers);
182  shape_opts->shapers = g_strsplit (arg, ",", 0);
183  return true;
184}
185
186static G_GNUC_NORETURN gboolean
187list_shapers (const char *name G_GNUC_UNUSED,
188	      const char *arg G_GNUC_UNUSED,
189	      gpointer    data G_GNUC_UNUSED,
190	      GError    **error G_GNUC_UNUSED)
191{
192  for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
193    g_printf ("%s\n", *shaper);
194
195  exit(0);
196}
197
198
199static gboolean
200parse_features (const char *name G_GNUC_UNUSED,
201	        const char *arg,
202	        gpointer    data,
203	        GError    **error G_GNUC_UNUSED)
204{
205  shape_options_t *shape_opts = (shape_options_t *) data;
206  char *s = (char *) arg;
207  char *p;
208
209  shape_opts->num_features = 0;
210  g_free (shape_opts->features);
211  shape_opts->features = NULL;
212
213  if (!*s)
214    return true;
215
216  /* count the features first, so we can allocate memory */
217  p = s;
218  do {
219    shape_opts->num_features++;
220    p = strchr (p, ',');
221    if (p)
222      p++;
223  } while (p);
224
225  shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
226
227  /* now do the actual parsing */
228  p = s;
229  shape_opts->num_features = 0;
230  while (p && *p) {
231    char *end = strchr (p, ',');
232    if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
233      shape_opts->num_features++;
234    p = end ? end + 1 : NULL;
235  }
236
237  return true;
238}
239
240
241void
242view_options_t::add_options (option_parser_t *parser)
243{
244  GOptionEntry entries[] =
245  {
246    {"annotate",	0, 0, G_OPTION_ARG_NONE,	&this->annotate,		"Annotate output rendering",				NULL},
247    {"background",	0, 0, G_OPTION_ARG_STRING,	&this->back,			"Set background color (default: "DEFAULT_BACK")",	"red/#rrggbb/#rrggbbaa"},
248    {"foreground",	0, 0, G_OPTION_ARG_STRING,	&this->fore,			"Set foreground color (default: "DEFAULT_FORE")",	"red/#rrggbb/#rrggbbaa"},
249    {"line-space",	0, 0, G_OPTION_ARG_DOUBLE,	&this->line_space,		"Set space between lines (default: 0)",			"units"},
250    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_margin,	"Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"},
251    {"font-size",	0, 0, G_OPTION_ARG_DOUBLE,	&this->font_size,		"Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"},
252    {NULL}
253  };
254  parser->add_group (entries,
255		     "view",
256		     "View options:",
257		     "Options controlling output rendering",
258		     this);
259}
260
261void
262shape_options_t::add_options (option_parser_t *parser)
263{
264  GOptionEntry entries[] =
265  {
266    {"list-shapers",	0, G_OPTION_FLAG_NO_ARG,
267			      G_OPTION_ARG_CALLBACK,	(gpointer) &list_shapers,	"List available shapers and quit",	NULL},
268    {"shaper",		0, G_OPTION_FLAG_HIDDEN,
269			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Hidden duplicate of --shapers",	NULL},
270    {"shapers",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Comma-separated list of shapers to try","list"},
271    {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
272    {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
273    {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
274    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF8 byte indices, not char indices",	NULL},
275    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,	&this->normalize_glyphs,	"Rearrange glyph clusters in nominal order",	NULL},
276    {NULL}
277  };
278  parser->add_group (entries,
279		     "shape",
280		     "Shape options:",
281		     "Options controlling the shaping process",
282		     this);
283
284  const gchar *features_help = "Comma-separated list of font features\n"
285    "\n"
286    "    Features can be enabled or disabled, either globally or limited to\n"
287    "    specific character ranges.\n"
288    "\n"
289    "    The range indices refer to the positions between Unicode characters,\n"
290    "    unless the --utf8-clusters is provided, in which case range indices\n"
291    "    refer to UTF-8 byte indices. The position before the first character\n"
292    "    is always 0.\n"
293    "\n"
294    "    The format is Python-esque.  Here is how it all works:\n"
295    "\n"
296    "      Syntax:       Value:    Start:    End:\n"
297    "\n"
298    "    Setting value:\n"
299    "      \"kern\"        1         0         ∞         # Turn feature on\n"
300    "      \"+kern\"       1         0         ∞         # Turn feature on\n"
301    "      \"-kern\"       0         0         ∞         # Turn feature off\n"
302    "      \"kern=0\"      0         0         ∞         # Turn feature off\n"
303    "      \"kern=1\"      1         0         ∞         # Turn feature on\n"
304    "      \"aalt=2\"      2         0         ∞         # Choose 2nd alternate\n"
305    "\n"
306    "    Setting index:\n"
307    "      \"kern[]\"      1         0         ∞         # Turn feature on\n"
308    "      \"kern[:]\"     1         0         ∞         # Turn feature on\n"
309    "      \"kern[5:]\"    1         5         ∞         # Turn feature on, partial\n"
310    "      \"kern[:5]\"    1         0         5         # Turn feature on, partial\n"
311    "      \"kern[3:5]\"   1         3         5         # Turn feature on, range\n"
312    "      \"kern[3]\"     1         3         3+1       # Turn feature on, single char\n"
313    "\n"
314    "    Mixing it all:\n"
315    "\n"
316    "      \"aalt[3:5]=2\" 2         3         5         # Turn 2nd alternate on for range";
317
318  GOptionEntry entries2[] =
319  {
320    {"features",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_features,	features_help,	"list"},
321    {NULL}
322  };
323  parser->add_group (entries2,
324		     "features",
325		     "Features options:",
326		     "Options controlling font features used",
327		     this);
328}
329
330void
331font_options_t::add_options (option_parser_t *parser)
332{
333  GOptionEntry entries[] =
334  {
335    {"font-file",	0, 0, G_OPTION_ARG_STRING,	&this->font_file,		"Font file-name",					"filename"},
336    {"face-index",	0, 0, G_OPTION_ARG_INT,		&this->face_index,		"Face index (default: 0)",                              "index"},
337    {NULL}
338  };
339  parser->add_group (entries,
340		     "font",
341		     "Font options:",
342		     "Options controlling the font",
343		     this);
344}
345
346void
347text_options_t::add_options (option_parser_t *parser)
348{
349  GOptionEntry entries[] =
350  {
351    {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
352    {"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"},
353    {NULL}
354  };
355  parser->add_group (entries,
356		     "text",
357		     "Text options:",
358		     "Options controlling the input text",
359		     this);
360}
361
362void
363output_options_t::add_options (option_parser_t *parser)
364{
365  GOptionEntry entries[] =
366  {
367    {"output-file",	0, 0, G_OPTION_ARG_STRING,	&this->output_file,		"Set output file-name (default: stdout)","filename"},
368    {"output-format",	0, 0, G_OPTION_ARG_STRING,	&this->output_format,		"Set output format",			"format"},
369    {NULL}
370  };
371  parser->add_group (entries,
372		     "output",
373		     "Output options:",
374		     "Options controlling the output",
375		     this);
376}
377
378
379
380hb_font_t *
381font_options_t::get_font (void) const
382{
383  if (font)
384    return font;
385
386  hb_blob_t *blob = NULL;
387
388  /* Create the blob */
389  {
390    char *font_data;
391    unsigned int len = 0;
392    hb_destroy_func_t destroy;
393    void *user_data;
394    hb_memory_mode_t mm;
395
396    /* This is a hell of a lot of code for just reading a file! */
397    if (!font_file)
398      fail (true, "No font file set");
399
400    if (0 == strcmp (font_file, "-")) {
401      /* read it */
402      GString *gs = g_string_new (NULL);
403      char buf[BUFSIZ];
404#ifdef HAVE__SETMODE
405      _setmode (fileno (stdin), _O_BINARY);
406#endif
407      while (!feof (stdin)) {
408	size_t ret = fread (buf, 1, sizeof (buf), stdin);
409	if (ferror (stdin))
410	  fail (false, "Failed reading font from standard input: %s",
411		strerror (errno));
412	g_string_append_len (gs, buf, ret);
413      }
414      len = gs->len;
415      font_data = g_string_free (gs, false);
416      user_data = font_data;
417      destroy = (hb_destroy_func_t) g_free;
418      mm = HB_MEMORY_MODE_WRITABLE;
419    } else {
420      GError *error = NULL;
421      GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
422      if (mf) {
423	font_data = g_mapped_file_get_contents (mf);
424	len = g_mapped_file_get_length (mf);
425	if (len) {
426	  destroy = (hb_destroy_func_t) g_mapped_file_unref;
427	  user_data = (void *) mf;
428	  mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
429	} else
430	  g_mapped_file_unref (mf);
431      } else {
432	fail (false, "%s", error->message);
433	//g_error_free (error);
434      }
435      if (!len) {
436	/* GMappedFile is buggy, it doesn't fail if file isn't regular.
437	 * Try reading.
438	 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
439        GError *error = NULL;
440	gsize l;
441	if (g_file_get_contents (font_file, &font_data, &l, &error)) {
442	  len = l;
443	  destroy = (hb_destroy_func_t) g_free;
444	  user_data = (void *) font_data;
445	  mm = HB_MEMORY_MODE_WRITABLE;
446	} else {
447	  fail (false, "%s", error->message);
448	  //g_error_free (error);
449	}
450      }
451    }
452
453    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
454  }
455
456  /* Create the face */
457  hb_face_t *face = hb_face_create (blob, face_index);
458  hb_blob_destroy (blob);
459
460
461  font = hb_font_create (face);
462
463  unsigned int upem = hb_face_get_upem (face);
464  hb_font_set_scale (font, upem, upem);
465  hb_face_destroy (face);
466
467#ifdef HAVE_FREETYPE
468  hb_ft_font_set_funcs (font);
469#endif
470
471  return font;
472}
473
474
475const char *
476text_options_t::get_line (unsigned int *len)
477{
478  if (text) {
479    if (text_len == (unsigned int) -1)
480      text_len = strlen (text);
481
482    if (!text_len) {
483      *len = 0;
484      return NULL;
485    }
486
487    const char *ret = text;
488    const char *p = (const char *) memchr (text, '\n', text_len);
489    unsigned int ret_len;
490    if (!p) {
491      ret_len = text_len;
492      text += ret_len;
493      text_len = 0;
494    } else {
495      ret_len = p - ret;
496      text += ret_len + 1;
497      text_len -= ret_len + 1;
498    }
499
500    *len = ret_len;
501    return ret;
502  }
503
504  if (!fp) {
505    if (!text_file)
506      fail (true, "At least one of text or text-file must be set");
507
508    if (0 != strcmp (text_file, "-"))
509      fp = fopen (text_file, "r");
510    else
511      fp = stdin;
512
513    if (!fp)
514      fail (false, "Failed opening text file `%s': %s",
515	    text_file, strerror (errno));
516
517    gs = g_string_new (NULL);
518  }
519
520  g_string_set_size (gs, 0);
521  char buf[BUFSIZ];
522  while (fgets (buf, sizeof (buf), fp)) {
523    unsigned int bytes = strlen (buf);
524    if (bytes && buf[bytes - 1] == '\n') {
525      bytes--;
526      g_string_append_len (gs, buf, bytes);
527      break;
528    }
529      g_string_append_len (gs, buf, bytes);
530  }
531  if (ferror (fp))
532    fail (false, "Failed reading text: %s",
533	  strerror (errno));
534  *len = gs->len;
535  return !*len && feof (fp) ? NULL : gs->str;
536}
537
538
539FILE *
540output_options_t::get_file_handle (void)
541{
542  if (fp)
543    return fp;
544
545  if (output_file)
546    fp = fopen (output_file, "wb");
547  else {
548#ifdef HAVE__SETMODE
549    _setmode (fileno (stdout), _O_BINARY);
550#endif
551    fp = stdout;
552  }
553  if (!fp)
554    fail (false, "Cannot open output file `%s': %s",
555	  g_filename_display_name (output_file), strerror (errno));
556
557  return fp;
558}
559
560static gboolean
561parse_verbose (const char *name G_GNUC_UNUSED,
562	       const char *arg G_GNUC_UNUSED,
563	       gpointer    data G_GNUC_UNUSED,
564	       GError    **error G_GNUC_UNUSED)
565{
566  format_options_t *format_opts = (format_options_t *) data;
567  format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
568  return true;
569}
570
571void
572format_options_t::add_options (option_parser_t *parser)
573{
574  GOptionEntry entries[] =
575  {
576    {"no-glyph-names",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_glyph_names,	"Use glyph indices instead of names",	NULL},
577    {"no-positions",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_positions,		"Do not show glyph positions",		NULL},
578    {"no-clusters",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_clusters,		"Do not show cluster mapping",		NULL},
579    {"show-text",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_text,		"Show input text",			NULL},
580    {"show-unicode",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_unicode,		"Show input Unicode codepoints",	NULL},
581    {"show-line-num",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_line_num,		"Show line numbers",			NULL},
582    {"verbose",		0, G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose,	"Show everything",			NULL},
583    {NULL}
584  };
585  parser->add_group (entries,
586		     "format",
587		     "Format options:",
588		     "Options controlling the formatting of buffer contents",
589		     this);
590}
591
592void
593format_options_t::serialize_unicode (hb_buffer_t *buffer,
594				     GString     *gs)
595{
596  unsigned int num_glyphs = hb_buffer_get_length (buffer);
597  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
598
599  g_string_append_c (gs, '<');
600  for (unsigned int i = 0; i < num_glyphs; i++)
601  {
602    if (i)
603      g_string_append_c (gs, ',');
604    g_string_append_printf (gs, "U+%04X", info->codepoint);
605    info++;
606  }
607  g_string_append_c (gs, '>');
608}
609
610void
611format_options_t::serialize_glyphs (hb_buffer_t *buffer,
612				    hb_font_t   *font,
613				    hb_bool_t    utf8_clusters,
614				    GString     *gs)
615{
616  unsigned int num_glyphs = hb_buffer_get_length (buffer);
617  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
618  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
619  hb_direction_t direction = hb_buffer_get_direction (buffer);
620
621  g_string_append_c (gs, '[');
622  for (unsigned int i = 0; i < num_glyphs; i++)
623  {
624    if (i)
625      g_string_append_c (gs, '|');
626
627    char glyph_name[128];
628    if (show_glyph_names) {
629      hb_font_glyph_to_string (font, info->codepoint, glyph_name, sizeof (glyph_name));
630      g_string_append_printf (gs, "%s", glyph_name);
631    } else
632      g_string_append_printf (gs, "%u", info->codepoint);
633
634    if (show_clusters) {
635      g_string_append_printf (gs, "=%u", info->cluster);
636      if (utf8_clusters)
637	g_string_append (gs, "u8");
638    }
639
640    if (show_positions && (pos->x_offset || pos->y_offset)) {
641      g_string_append_printf (gs, "@%d,%d", pos->x_offset, pos->y_offset);
642    }
643    if (show_positions) {
644      g_string_append_c (gs, '+');
645      if (HB_DIRECTION_IS_HORIZONTAL (direction) || pos->x_advance)
646	g_string_append_printf (gs, "%d", pos->x_advance);
647      if (HB_DIRECTION_IS_VERTICAL (direction) || pos->y_advance)
648	g_string_append_printf (gs, ",%d", pos->y_advance);
649    }
650
651    info++;
652    pos++;
653  }
654  g_string_append_c (gs, ']');
655}
656void
657format_options_t::serialize_line_no (unsigned int  line_no,
658				     GString      *gs)
659{
660  if (show_line_num)
661    g_string_append_printf (gs, "%d: ", line_no);
662}
663void
664format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
665					    unsigned int  line_no,
666					    const char   *text,
667					    unsigned int  text_len,
668					    hb_font_t    *font,
669					    hb_bool_t     utf8_clusters,
670					    GString      *gs)
671{
672  if (show_text) {
673    serialize_line_no (line_no, gs);
674    g_string_append_c (gs, '(');
675    g_string_append_len (gs, text, text_len);
676    g_string_append_c (gs, ')');
677    g_string_append_c (gs, '\n');
678  }
679
680  if (show_unicode) {
681    serialize_line_no (line_no, gs);
682    serialize_unicode (buffer, gs);
683    g_string_append_c (gs, '\n');
684  }
685}
686void
687format_options_t::serialize_message (unsigned int  line_no,
688				     const char   *msg,
689				     GString      *gs)
690{
691  serialize_line_no (line_no, gs);
692  g_string_append_printf (gs, "%s", msg);
693  g_string_append_c (gs, '\n');
694}
695void
696format_options_t::serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
697					      unsigned int  line_no,
698					      const char   *text,
699					      unsigned int  text_len,
700					      hb_font_t    *font,
701					      hb_bool_t     utf8_clusters,
702					      GString      *gs)
703{
704  serialize_line_no (line_no, gs);
705  serialize_glyphs (buffer, font, utf8_clusters, gs);
706  g_string_append_c (gs, '\n');
707}
708