13bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod/*
28f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod * Copyright © 2011,2012  Google, Inc.
33bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
43bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
53bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
63bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Permission is hereby granted, without written agreement and without
73bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
83bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * software and its documentation for any purpose, provided that the
93bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
103bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * all copies of this software.
113bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
123bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
133bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
143bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
153bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DAMAGE.
173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
193bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
213bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
223bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Google Author(s): Behdad Esfahbod
253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod */
263bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
273bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod#include "options.hh"
283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
295ddd9cc499f2470eca239ae357a5c8a3626c0809Behdad Esfahbod#ifdef HAVE_FREETYPE
30b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod#include <hb-ft.h>
318650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif
328650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_OT
33903648437c180c7b039801cdb0672e0f8e14afd4Behdad Esfahbod#include <hb-ot-font.h>
34b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod#endif
353bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
368650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbodstruct supported_font_funcs_t {
378650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	char name[4];
388650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	void (*func) (hb_font_t *);
398650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod} supported_font_funcs[] =
408650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod{
418650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_FREETYPE
428650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {"ft",	hb_ft_font_set_funcs},
438650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif
448650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_OT
458650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {"ot",	hb_ot_font_set_funcs},
468650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif
478650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod};
488650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod
493bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
508b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid
518b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodfail (hb_bool_t suggest_help, const char *format, ...)
528b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
538b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  const char *msg;
548b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  va_list vap;
568b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  va_start (vap, format);
578b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  msg = g_strdup_vprintf (format, vap);
588b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  const char *prgname = g_get_prgname ();
598b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  g_printerr ("%s: %s\n", prgname, msg);
608b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  if (suggest_help)
618b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    g_printerr ("Try `%s --help' for more information.\n", prgname);
628b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
638b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  exit (1);
648b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
658b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
668b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
670594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbodhb_bool_t debug = false;
68b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
69b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gchar *
70b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshapers_to_string (void)
71b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
72b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GString *shapers = g_string_new (NULL);
73b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  const char **shaper_list = hb_shape_list_shapers ();
74b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
75b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  for (; *shaper_list; shaper_list++) {
76b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_string_append (shapers, *shaper_list);
77b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_string_append_c (shapers, ',');
78b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
79b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
80b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
810594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return g_string_free (shapers, false);
82b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
83b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
84b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic G_GNUC_NORETURN gboolean
85b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshow_version (const char *name G_GNUC_UNUSED,
86b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      const char *arg G_GNUC_UNUSED,
87b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      gpointer    data G_GNUC_UNUSED,
88b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
89b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
90b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
91b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
92b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  char *shapers = shapers_to_string ();
93b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_printf ("Available shapers: %s\n", shapers);
94b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_free (shapers);
95b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (strcmp (HB_VERSION_STRING, hb_version_string ()))
96b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
97b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
98b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  exit(0);
99b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
100b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
101b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
102b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
103b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_main_options (void)
104b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
105b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionEntry entries[] =
106b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
107b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"version",		0, G_OPTION_FLAG_NO_ARG,
108b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &show_version,	"Show version numbers",			NULL},
109b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"debug",		0, 0, G_OPTION_ARG_NONE,	&debug,				"Free all resources before exit",	NULL},
110b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {NULL}
111b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  };
112b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_context_add_main_entries (context, entries, NULL);
113b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
114b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
115b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean
116b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpre_parse (GOptionContext *context G_GNUC_UNUSED,
117b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   GOptionGroup *group G_GNUC_UNUSED,
118b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   gpointer data,
119b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   GError **error)
120b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
121b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group_t *option_group = (option_group_t *) data;
122b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group->pre_parse (error);
123b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return *error == NULL;
124b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
125b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
126b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean
127b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpost_parse (GOptionContext *context G_GNUC_UNUSED,
128b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    GOptionGroup *group G_GNUC_UNUSED,
129b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    gpointer data,
130b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    GError **error)
131b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
132b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group_t *option_group = static_cast<option_group_t *>(data);
133b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group->post_parse (error);
134b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return *error == NULL;
135b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
136b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
137b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
138b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_group (GOptionEntry   *entries,
139b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *name,
140b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *description,
141b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *help_description,
142b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    option_group_t *option_group)
143b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
144b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionGroup *group = g_option_group_new (name, description, help_description,
145b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod					    static_cast<gpointer>(option_group), NULL);
146b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_group_add_entries (group, entries);
147b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_group_set_parse_hooks (group, pre_parse, post_parse);
148b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_context_add_group (context, group);
149b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
150b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
151b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
152b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::parse (int *argc, char ***argv)
153b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
1548b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  setlocale (LC_ALL, "");
1558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
156b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GError *parse_error = NULL;
157b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (!g_option_context_parse (context, argc, argv, &parse_error))
158b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
159a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    if (parse_error != NULL) {
1600594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "%s", parse_error->message);
161a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod      //g_error_free (parse_error);
162a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    } else
1630594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "Option parse error");
164b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
165b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
1663bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1673bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1683bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
1693bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_margin (const char *name G_GNUC_UNUSED,
1703bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	      const char *arg,
171bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	      gpointer    data,
1723bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
1733bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
174bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  view_options_t *view_opts = (view_options_t *) data;
1753bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  view_options_t::margin_t &m = view_opts->margin;
17697796453aab56873809a15b5e316cba8acea7449Behdad Esfahbod  switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
1773bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 1: m.r = m.t;
1783bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 2: m.b = m.t;
1793bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 3: m.l = m.r;
1800594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    case 4: return true;
1813bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    default:
1823bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1833bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod		   "%s argument should be one to four space-separated numbers",
1843bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod		   name);
1850594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      return false;
1863bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
1873bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
1883bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1893bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1903bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
1913bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_shapers (const char *name G_GNUC_UNUSED,
1923bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	       const char *arg,
193bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	       gpointer    data,
1943bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	       GError    **error G_GNUC_UNUSED)
1953bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
196bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  shape_options_t *shape_opts = (shape_options_t *) data;
197ade7459ea7c75b4f33f7cfa43dd5bdfa0c18d6d5Behdad Esfahbod  g_strfreev (shape_opts->shapers);
1983bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->shapers = g_strsplit (arg, ",", 0);
1990594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
2003bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
2013bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
202fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodstatic G_GNUC_NORETURN gboolean
203fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodlist_shapers (const char *name G_GNUC_UNUSED,
204fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      const char *arg G_GNUC_UNUSED,
205fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      gpointer    data G_GNUC_UNUSED,
206fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
207fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod{
208fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod  for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
209fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod    g_printf ("%s\n", *shaper);
210fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
211fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod  exit(0);
212fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod}
213fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
214fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
2153bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
2163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_features (const char *name G_GNUC_UNUSED,
2173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	        const char *arg,
218bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	        gpointer    data,
2193bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	        GError    **error G_GNUC_UNUSED)
2203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
221bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  shape_options_t *shape_opts = (shape_options_t *) data;
2223bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  char *s = (char *) arg;
2233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  char *p;
2243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->num_features = 0;
2268f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod  g_free (shape_opts->features);
2273bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->features = NULL;
2283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2293bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  if (!*s)
2300594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    return true;
2313bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2323bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  /* count the features first, so we can allocate memory */
2333bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  p = s;
2343bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  do {
2353bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    shape_opts->num_features++;
2363bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    p = strchr (p, ',');
2373bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    if (p)
2383bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      p++;
2393bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  } while (p);
2403bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2413bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
2423bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2433bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  /* now do the actual parsing */
2443bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  p = s;
2453bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->num_features = 0;
246e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod  while (p && *p) {
247e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    char *end = strchr (p, ',');
248e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
2493bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      shape_opts->num_features++;
250e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    p = end ? end + 1 : NULL;
2513bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
2523bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2530594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
2543bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
2553bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2563bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2573bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodvoid
258b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodview_options_t::add_options (option_parser_t *parser)
2593bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
2603bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  GOptionEntry entries[] =
2613bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  {
262bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"annotate",	0, 0, G_OPTION_ARG_NONE,	&this->annotate,		"Annotate output rendering",				NULL},
263d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod    {"background",	0, 0, G_OPTION_ARG_STRING,	&this->back,			"Set background color (default: " DEFAULT_BACK ")",	"rrggbb/rrggbbaa"},
264d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod    {"foreground",	0, 0, G_OPTION_ARG_STRING,	&this->fore,			"Set foreground color (default: " DEFAULT_FORE ")",	"rrggbb/rrggbbaa"},
265bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"line-space",	0, 0, G_OPTION_ARG_DOUBLE,	&this->line_space,		"Set space between lines (default: 0)",			"units"},
2669a34a50daac3563464e0031f377e85f0c704f2d6Behdad Esfahbod    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_margin,	"Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
2679a34a50daac3563464e0031f377e85f0c704f2d6Behdad Esfahbod    {"font-size",	0, 0, G_OPTION_ARG_DOUBLE,	&this->font_size,		"Font size (default: " G_STRINGIFY(DEFAULT_FONT_SIZE) ")","size"},
268109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
269109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
270b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
271b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "view",
272b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "View options:",
27330874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod		     "Options controlling output rendering",
274b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
275109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
2763bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
277109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
278b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshape_options_t::add_options (option_parser_t *parser)
279109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
280109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
281109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
282fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod    {"list-shapers",	0, G_OPTION_FLAG_NO_ARG,
283fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &list_shapers,	"List available shapers and quit",	NULL},
2848f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod    {"shaper",		0, G_OPTION_FLAG_HIDDEN,
2858f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Hidden duplicate of --shapers",	NULL},
2868650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"shapers",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Set comma-separated list of shapers to try","list"},
287bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
288bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
289bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
290407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"bot",		0, 0, G_OPTION_ARG_NONE,	&this->bot,			"Treat text as beginning-of-paragraph",	NULL},
291407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"eot",		0, 0, G_OPTION_ARG_NONE,	&this->eot,			"Treat text as end-of-paragraph",	NULL},
292407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE,	&this->preserve_default_ignorables,	"Preserve Default-Ignorable characters",	NULL},
29330874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF8 byte indices, not char indices",	NULL},
29439b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,	&this->normalize_glyphs,	"Rearrange glyph clusters in nominal order",	NULL},
29550067e280f381918b8e90b26df9e7bf20f98f0bdBehdad Esfahbod    {"num-iterations",	0, 0, G_OPTION_ARG_INT,		&this->num_iterations,		"Run shaper N times (default: 1)",	"N"},
296109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
297109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
298b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
299b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "shape",
300b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Shape options:",
301b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Options controlling the shaping process",
302b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
3038750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod
30430874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod  const gchar *features_help = "Comma-separated list of font features\n"
3058750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3068750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Features can be enabled or disabled, either globally or limited to\n"
3070de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    specific character ranges.  The format for specifying feature settings\n"
3080de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    follows.  All valid CSS font-feature-settings values other than 'normal'\n"
3090de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    and 'inherited' are also accepted, though, not documented below.\n"
31095cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "\n"
31195cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    The range indices refer to the positions between Unicode characters,\n"
31295cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    unless the --utf8-clusters is provided, in which case range indices\n"
31395cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    refer to UTF-8 byte indices. The position before the first character\n"
31495cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    is always 0.\n"
315d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod    "\n"
316d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod    "    The format is Python-esque.  Here is how it all works:\n"
3178750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3188750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      Syntax:       Value:    Start:    End:\n"
3198750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3208750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Setting value:\n"
3218750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern\"        1         0         ∞         # Turn feature on\n"
3228750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"+kern\"       1         0         ∞         # Turn feature on\n"
3238750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"-kern\"       0         0         ∞         # Turn feature off\n"
3248750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern=0\"      0         0         ∞         # Turn feature off\n"
3258750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern=1\"      1         0         ∞         # Turn feature on\n"
3268750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"aalt=2\"      2         0         ∞         # Choose 2nd alternate\n"
3278750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3288750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Setting index:\n"
3298750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[]\"      1         0         ∞         # Turn feature on\n"
3308750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[:]\"     1         0         ∞         # Turn feature on\n"
3318750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[5:]\"    1         5         ∞         # Turn feature on, partial\n"
3328750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[:5]\"    1         0         5         # Turn feature on, partial\n"
3338750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[3:5]\"   1         3         5         # Turn feature on, range\n"
3348750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[3]\"     1         3         3+1       # Turn feature on, single char\n"
3358750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3368750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Mixing it all:\n"
3378750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3383bc22eb7b843c77706bb13fc332009097f247813Behdad Esfahbod    "      \"aalt[3:5]=2\" 2         3         5         # Turn 2nd alternate on for range";
3398750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod
3408750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  GOptionEntry entries2[] =
3418750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  {
3428750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    {"features",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_features,	features_help,	"list"},
3438750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    {NULL}
3448750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  };
3458750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  parser->add_group (entries2,
3468750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     "features",
3478750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     "Features options:",
34830874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod		     "Options controlling font features used",
3498750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     this);
350109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
3513bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
352109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
353b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::add_options (option_parser_t *parser)
354109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
3558650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  char *text = NULL;
3568650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod
3578650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
3588650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0);
3598650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    GString *s = g_string_new (NULL);
3608650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n    Supported font function implementations are: %s",
3618650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod		     supported_font_funcs[0].name,
3628650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod		     supported_font_funcs[0].name);
3638650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
3648650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {
3658650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      g_string_append_c (s, '/');
3668650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      g_string_append (s, supported_font_funcs[i].name);
3678650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    }
3688650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    text = g_string_free (s, FALSE);
3698650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    parser->free_later (text);
3708650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
3718650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod
372109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
373109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
3748650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"font-file",	0, 0, G_OPTION_ARG_STRING,	&this->font_file,		"Set font file-name",			"filename"},
3758650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"face-index",	0, 0, G_OPTION_ARG_INT,		&this->face_index,		"Set face index (default: 0)",		"index"},
3768650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"font-funcs",	0, 0, G_OPTION_ARG_STRING,	&this->font_funcs,		text,					"format"},
377109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
378109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
379b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
380b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "font",
381b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Font options:",
382b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Options controlling the font",
383b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
384109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
3853bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
386109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
387b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::add_options (option_parser_t *parser)
388109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
389109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
390109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
391b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
39278d41d8d69142ae95928b51215c0c0c5e3f5b3f0Behdad Esfahbod    {"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.\n",		"filename"},
393321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod    {"text-before",	0, 0, G_OPTION_ARG_STRING,	&this->text_before,		"Set text context before each line",	"string"},
394321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod    {"text-after",	0, 0, G_OPTION_ARG_STRING,	&this->text_after,		"Set text context after each line",	"string"},
3953bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    {NULL}
3963bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  };
397b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
398b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "text",
399b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Text options:",
400b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Options controlling the input text",
401b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
402b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
403b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
404b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
405b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoutput_options_t::add_options (option_parser_t *parser)
406b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
4079815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  const char *text;
4089815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod
4099815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  if (NULL == supported_formats)
4109815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod    text = "Set output format";
4119815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  else
412ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod  {
413ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod    char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
4142306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod    text = g_strdup_printf ("Set output format\n\n    Supported output formats are: %s", items);
415ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod    g_free (items);
4162306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod    parser->free_later ((char *) text);
417ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod  }
4189815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod
419b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionEntry entries[] =
420b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
421b5afd8f78e6b372f1bbed469329c1554adb20eeaBehdad Esfahbod    {"output-file",	0, 0, G_OPTION_ARG_STRING,	&this->output_file,		"Set output file-name (default: stdout)","filename"},
4229815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod    {"output-format",	0, 0, G_OPTION_ARG_STRING,	&this->output_format,		text,					"format"},
423b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {NULL}
424b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  };
425b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
426b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "output",
427b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Output options:",
428b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Options controlling the output",
429b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
430b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
4313bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
4323bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
4333bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
434b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodhb_font_t *
435b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::get_font (void) const
436b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
437b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (font)
438b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    return font;
439b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
440b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_blob_t *blob = NULL;
441b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
442b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  /* Create the blob */
4433bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  {
4444451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    char *font_data;
4454451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    unsigned int len = 0;
446b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    hb_destroy_func_t destroy;
447b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    void *user_data;
448b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    hb_memory_mode_t mm;
449b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
4504451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    /* This is a hell of a lot of code for just reading a file! */
451b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    if (!font_file)
4520594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "No font file set");
453b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
4544451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    if (0 == strcmp (font_file, "-")) {
4554451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      /* read it */
4564451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      GString *gs = g_string_new (NULL);
4574451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      char buf[BUFSIZ];
458e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__)
459bc76449f51f20491a4540b4f240f5a3e0dcb20feBehdad Esfahbod      setmode (fileno (stdin), _O_BINARY);
4604451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod#endif
4614451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      while (!feof (stdin)) {
4624451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	size_t ret = fread (buf, 1, sizeof (buf), stdin);
4634451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (ferror (stdin))
4640594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	  fail (false, "Failed reading font from standard input: %s",
4654451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod		strerror (errno));
4664451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	g_string_append_len (gs, buf, ret);
4674451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
4684451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      len = gs->len;
4690594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      font_data = g_string_free (gs, false);
4704451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      user_data = font_data;
4714451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      destroy = (hb_destroy_func_t) g_free;
4724451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      mm = HB_MEMORY_MODE_WRITABLE;
4734451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    } else {
474f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod      GError *error = NULL;
4750594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
4764451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      if (mf) {
4774451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	font_data = g_mapped_file_get_contents (mf);
4784451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	len = g_mapped_file_get_length (mf);
4794451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (len) {
480c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod	  destroy = (hb_destroy_func_t) g_mapped_file_unref;
4814451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  user_data = (void *) mf;
4824451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
4834451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	} else
484c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod	  g_mapped_file_unref (mf);
485f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod      } else {
4860594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	fail (false, "%s", error->message);
487f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod	//g_error_free (error);
4884451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
4894451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      if (!len) {
4904451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	/* GMappedFile is buggy, it doesn't fail if file isn't regular.
4914451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	 * Try reading.
4924451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
4934451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod        GError *error = NULL;
4944451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	gsize l;
4954451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (g_file_get_contents (font_file, &font_data, &l, &error)) {
4964451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  len = l;
4974451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  destroy = (hb_destroy_func_t) g_free;
4984451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  user_data = (void *) font_data;
4994451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  mm = HB_MEMORY_MODE_WRITABLE;
5004451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	} else {
5010594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	  fail (false, "%s", error->message);
5024451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  //g_error_free (error);
5034451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	}
5044451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
5054451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    }
506b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
507b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
508b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
509b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
510b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  /* Create the face */
511b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_face_t *face = hb_face_create (blob, face_index);
512b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_blob_destroy (blob);
513b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
514b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
515b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  font = hb_font_create (face);
516b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
517b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  unsigned int upem = hb_face_get_upem (face);
5187bf6ecd3bfb1ccf5d9ac6fe274efa74b46885feaBehdad Esfahbod  hb_font_set_scale (font, upem, upem);
519b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_face_destroy (face);
520b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
5218650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  void (*set_font_funcs) (hb_font_t *) = NULL;
5228650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  if (!font_funcs)
5238650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
5248650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    set_font_funcs = supported_font_funcs[0].func;
5258650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
5268650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  else
5278650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
5288650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
5298650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name))
5308650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      {
5318650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	set_font_funcs = supported_font_funcs[i].func;
5328650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	break;
5338650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      }
5348650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    if (!set_font_funcs)
5358650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {
5368650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      GString *s = g_string_new (NULL);
5378650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
5388650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      {
5398650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod        if (i)
5408650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	  g_string_append_c (s, '/');
5418650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	g_string_append (s, supported_font_funcs[i].name);
5428650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      }
5438650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      char *p = g_string_free (s, FALSE);
5448650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
5458650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    font_funcs,
5468650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    p,
5478650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    supported_font_funcs[0].name);
5488650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      //free (p);
5498650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    }
5508650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
5518650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  set_font_funcs (font);
552b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
553b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return font;
554b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
555b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
556b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
557b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodconst char *
558b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::get_line (unsigned int *len)
559b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
56055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (text) {
56155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (text_len == (unsigned int) -1)
56255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      text_len = strlen (text);
56355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
56455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (!text_len) {
56555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      *len = 0;
56655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      return NULL;
56755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
56855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
56955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    const char *ret = text;
57055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    const char *p = (const char *) memchr (text, '\n', text_len);
57155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    unsigned int ret_len;
57255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (!p) {
57355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      ret_len = text_len;
57455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      text += ret_len;
57555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      text_len = 0;
57655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    } else {
57755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      ret_len = p - ret;
57855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      text += ret_len + 1;
57955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      text_len -= ret_len + 1;
58055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
58155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
58255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    *len = ret_len;
58355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    return ret;
58455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  }
58555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
58655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (!fp) {
587b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    if (!text_file)
5880594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "At least one of text or text-file must be set");
589b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
59055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (0 != strcmp (text_file, "-"))
59155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      fp = fopen (text_file, "r");
59255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    else
59355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      fp = stdin;
5943bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
59555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (!fp)
5960594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (false, "Failed opening text file `%s': %s",
59755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod	    text_file, strerror (errno));
598b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
59955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    gs = g_string_new (NULL);
600b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
601b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
60255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  g_string_set_size (gs, 0);
60355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  char buf[BUFSIZ];
60455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  while (fgets (buf, sizeof (buf), fp)) {
60555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    unsigned int bytes = strlen (buf);
60627c36af411c7c4d75dd25d79fc76dd92c6bb9643Behdad Esfahbod    if (bytes && buf[bytes - 1] == '\n') {
60755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      bytes--;
60855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      g_string_append_len (gs, buf, bytes);
60955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      break;
61055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
61155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      g_string_append_len (gs, buf, bytes);
6123bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
61355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (ferror (fp))
6140594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    fail (false, "Failed reading text: %s",
61555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod	  strerror (errno));
61655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  *len = gs->len;
61755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  return !*len && feof (fp) ? NULL : gs->str;
6183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
619a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
620a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
621a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad EsfahbodFILE *
622a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbodoutput_options_t::get_file_handle (void)
623a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod{
624a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (fp)
625a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    return fp;
626a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
627a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (output_file)
628a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    fp = fopen (output_file, "wb");
629a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  else {
630e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__)
631ceeae30f47b9d628484ae398bc38f3f60ee8eab4Behdad Esfahbod    setmode (fileno (stdout), _O_BINARY);
632a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod#endif
633a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    fp = stdout;
634a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  }
635a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (!fp)
6360594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    fail (false, "Cannot open output file `%s': %s",
637a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod	  g_filename_display_name (output_file), strerror (errno));
638a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
639a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  return fp;
640a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod}
6418b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
642c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodstatic gboolean
643c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodparse_verbose (const char *name G_GNUC_UNUSED,
644c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       const char *arg G_GNUC_UNUSED,
645c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       gpointer    data G_GNUC_UNUSED,
646c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       GError    **error G_GNUC_UNUSED)
647c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod{
648c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod  format_options_t *format_opts = (format_options_t *) data;
6490594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
6500594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
651c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod}
6528b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
6538b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid
6548b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodformat_options_t::add_options (option_parser_t *parser)
6558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
6568b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  GOptionEntry entries[] =
6578b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  {
6588b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    {"no-glyph-names",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_glyph_names,	"Use glyph indices instead of names",	NULL},
6598b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    {"no-positions",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_positions,		"Do not show glyph positions",		NULL},
6608b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    {"no-clusters",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_clusters,		"Do not show cluster mapping",		NULL},
661cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    {"show-text",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_text,		"Show input text",			NULL},
662cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    {"show-unicode",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_unicode,		"Show input Unicode codepoints",	NULL},
663cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    {"show-line-num",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_line_num,		"Show line numbers",			NULL},
664c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod    {"verbose",		0, G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose,	"Show everything",			NULL},
6658b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    {NULL}
6668b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  };
6678b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  parser->add_group (entries,
6688b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     "format",
6698b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     "Format options:",
6708b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     "Options controlling the formatting of buffer contents",
6718b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     this);
6728b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
6738b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
6748b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid
675cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_unicode (hb_buffer_t *buffer,
676cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				     GString     *gs)
677cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod{
678cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  unsigned int num_glyphs = hb_buffer_get_length (buffer);
679cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
680cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod
681cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  g_string_append_c (gs, '<');
682cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  for (unsigned int i = 0; i < num_glyphs; i++)
683cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  {
684cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    if (i)
685cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod      g_string_append_c (gs, ',');
686cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    g_string_append_printf (gs, "U+%04X", info->codepoint);
687cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    info++;
688cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  }
689cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  g_string_append_c (gs, '>');
690cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod}
691cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod
692cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodvoid
693cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_glyphs (hb_buffer_t *buffer,
694cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				    hb_font_t   *font,
695f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod				    hb_buffer_serialize_format_t output_format,
696f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod				    hb_buffer_serialize_flags_t flags,
697cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				    GString     *gs)
6988b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
699c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod  g_string_append_c (gs, '[');
700f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  unsigned int num_glyphs = hb_buffer_get_length (buffer);
701f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  unsigned int start = 0;
702f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod
703f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  while (start < num_glyphs) {
704f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    char buf[1024];
705f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    unsigned int consumed;
706f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
707f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					 buf, sizeof (buf), &consumed,
708f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					 font, output_format, flags);
709f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!consumed)
710f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      break;
711f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    g_string_append (gs, buf);
7128b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  }
713c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod  g_string_append_c (gs, ']');
7148b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
715cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid
716cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodformat_options_t::serialize_line_no (unsigned int  line_no,
717cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod				     GString      *gs)
718cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{
719cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_line_num)
720cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_printf (gs, "%d: ", line_no);
721cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod}
722cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid
7235db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
7245db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    unsigned int  line_no,
7255db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    const char   *text,
7265db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    unsigned int  text_len,
7275db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    hb_font_t    *font,
7285db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    GString      *gs)
729cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{
730cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_text) {
731cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    serialize_line_no (line_no, gs);
732d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod    g_string_append_c (gs, '(');
733cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_len (gs, text, text_len);
734d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod    g_string_append_c (gs, ')');
735cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_c (gs, '\n');
736cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  }
737cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod
738cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_unicode) {
739cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    serialize_line_no (line_no, gs);
740ae62166519291057316a9d15cea3f1570fcb5eafBehdad Esfahbod    serialize_unicode (buffer, gs);
741cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_c (gs, '\n');
742cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  }
7435db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod}
7445db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid
7455db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_message (unsigned int  line_no,
7465db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod				     const char   *msg,
7475db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod				     GString      *gs)
7485db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{
7495db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  serialize_line_no (line_no, gs);
7505db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  g_string_append_printf (gs, "%s", msg);
7515db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  g_string_append_c (gs, '\n');
7525db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod}
7535db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid
7545db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
7555db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      unsigned int  line_no,
7565db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      const char   *text,
7575db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      unsigned int  text_len,
7585db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      hb_font_t    *font,
759f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					      hb_buffer_serialize_format_t output_format,
760f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					      hb_buffer_serialize_flags_t format_flags,
7615db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      GString      *gs)
7625db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{
763cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  serialize_line_no (line_no, gs);
764f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  serialize_glyphs (buffer, font, output_format, format_flags, gs);
765cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  g_string_append_c (gs, '\n');
766cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod}
767