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
3316dac7eccf04bb357e95a8e4c18c8418dcfb4030Behdad Esfahbod#include <hb-ot.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);
58c36c4a9924609df648e62e47fa017b19b844fd98Behdad Esfahbod  va_end (vap);
598b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  const char *prgname = g_get_prgname ();
608b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  g_printerr ("%s: %s\n", prgname, msg);
618b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  if (suggest_help)
628b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    g_printerr ("Try `%s --help' for more information.\n", prgname);
638b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
648b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  exit (1);
658b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
668b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
678b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
680594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbodhb_bool_t debug = false;
69b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
70b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gchar *
71b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshapers_to_string (void)
72b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
73b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GString *shapers = g_string_new (NULL);
74b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  const char **shaper_list = hb_shape_list_shapers ();
75b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
76b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  for (; *shaper_list; shaper_list++) {
77b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_string_append (shapers, *shaper_list);
78b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_string_append_c (shapers, ',');
79b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
80b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
81b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
820594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return g_string_free (shapers, false);
83b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
84b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
85b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic G_GNUC_NORETURN gboolean
86b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshow_version (const char *name G_GNUC_UNUSED,
87b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      const char *arg G_GNUC_UNUSED,
88b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      gpointer    data G_GNUC_UNUSED,
89b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
90b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
91b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
92b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
93b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  char *shapers = shapers_to_string ();
94b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_printf ("Available shapers: %s\n", shapers);
95b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_free (shapers);
96b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (strcmp (HB_VERSION_STRING, hb_version_string ()))
97b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
98b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
99b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  exit(0);
100b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
101b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
102b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
103b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
104b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_main_options (void)
105b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
106b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionEntry entries[] =
107b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
108b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"version",		0, G_OPTION_FLAG_NO_ARG,
109b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &show_version,	"Show version numbers",			NULL},
110b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"debug",		0, 0, G_OPTION_ARG_NONE,	&debug,				"Free all resources before exit",	NULL},
111b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {NULL}
112b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  };
113b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_context_add_main_entries (context, entries, NULL);
114b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
115b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
116b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean
117b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpre_parse (GOptionContext *context G_GNUC_UNUSED,
118b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   GOptionGroup *group G_GNUC_UNUSED,
119b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   gpointer data,
120b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	   GError **error)
121b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
122b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group_t *option_group = (option_group_t *) data;
123b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group->pre_parse (error);
124b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return *error == NULL;
125b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
126b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
127b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean
128b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpost_parse (GOptionContext *context G_GNUC_UNUSED,
129b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    GOptionGroup *group G_GNUC_UNUSED,
130b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    gpointer data,
131b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod	    GError **error)
132b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
133b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group_t *option_group = static_cast<option_group_t *>(data);
134b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  option_group->post_parse (error);
135b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return *error == NULL;
136b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
137b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
138b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
139b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_group (GOptionEntry   *entries,
140b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *name,
141b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *description,
142b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    const gchar    *help_description,
143b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod			    option_group_t *option_group)
144b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
145b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionGroup *group = g_option_group_new (name, description, help_description,
146b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod					    static_cast<gpointer>(option_group), NULL);
147b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_group_add_entries (group, entries);
148b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_group_set_parse_hooks (group, pre_parse, post_parse);
149b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  g_option_context_add_group (context, group);
150b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
151b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
152b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
153b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::parse (int *argc, char ***argv)
154b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
1558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  setlocale (LC_ALL, "");
1568b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
157b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GError *parse_error = NULL;
158b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (!g_option_context_parse (context, argc, argv, &parse_error))
159b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
160a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    if (parse_error != NULL) {
1610594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "%s", parse_error->message);
162a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod      //g_error_free (parse_error);
163a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    } else
1640594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "Option parse error");
165b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
166b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
1673bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1683bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1693bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
1703bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_margin (const char *name G_GNUC_UNUSED,
1713bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	      const char *arg,
172bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	      gpointer    data,
1733bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
1743bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
175bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  view_options_t *view_opts = (view_options_t *) data;
1763bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  view_options_t::margin_t &m = view_opts->margin;
177a664810e38b479e05ea32cac263cb5730629f9d8Behdad Esfahbod  switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf", &m.t, &m.r, &m.b, &m.l)) {
1783bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 1: m.r = m.t;
1793bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 2: m.b = m.t;
1803bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    case 3: m.l = m.r;
1810594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    case 4: return true;
1823bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    default:
1833bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1843bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod		   "%s argument should be one to four space-separated numbers",
1853bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod		   name);
1860594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      return false;
1873bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
1883bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
1893bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1903bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1913bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
1923bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_shapers (const char *name G_GNUC_UNUSED,
1933bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	       const char *arg,
194bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	       gpointer    data,
1953bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	       GError    **error G_GNUC_UNUSED)
1963bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
197bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  shape_options_t *shape_opts = (shape_options_t *) data;
198ade7459ea7c75b4f33f7cfa43dd5bdfa0c18d6d5Behdad Esfahbod  g_strfreev (shape_opts->shapers);
1993bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->shapers = g_strsplit (arg, ",", 0);
2000594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
2013bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
2023bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
203fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodstatic G_GNUC_NORETURN gboolean
204fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodlist_shapers (const char *name G_GNUC_UNUSED,
205fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      const char *arg G_GNUC_UNUSED,
206fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      gpointer    data G_GNUC_UNUSED,
207fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod	      GError    **error G_GNUC_UNUSED)
208fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod{
209fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod  for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
210fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod    g_printf ("%s\n", *shaper);
211fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
212fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod  exit(0);
213fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod}
214fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
215fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod
2163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean
2173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_features (const char *name G_GNUC_UNUSED,
2183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	        const char *arg,
219bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod	        gpointer    data,
2203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod	        GError    **error G_GNUC_UNUSED)
2213bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
222bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod  shape_options_t *shape_opts = (shape_options_t *) data;
2233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  char *s = (char *) arg;
2243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  char *p;
2253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2263bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->num_features = 0;
2278f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod  g_free (shape_opts->features);
2283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->features = NULL;
2293bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2303bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  if (!*s)
2310594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    return true;
2323bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2333bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  /* count the features first, so we can allocate memory */
2343bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  p = s;
2353bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  do {
2363bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    shape_opts->num_features++;
2373bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    p = strchr (p, ',');
2383bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    if (p)
2393bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      p++;
2403bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  } while (p);
2413bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2423bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
2433bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2443bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  /* now do the actual parsing */
2453bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  p = s;
2463bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  shape_opts->num_features = 0;
247e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod  while (p && *p) {
248e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    char *end = strchr (p, ',');
249e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
2503bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod      shape_opts->num_features++;
251e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod    p = end ? end + 1 : NULL;
2523bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
2533bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2540594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
2553bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
2563bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
257111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbodstatic gboolean
258111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbodparse_variations (const char *name G_GNUC_UNUSED,
259111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod	        const char *arg,
260111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod	        gpointer    data,
261111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod	        GError    **error G_GNUC_UNUSED)
262111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod{
263111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  font_options_t *font_opts = (font_options_t *) data;
264111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  char *s = (char *) arg;
265111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  char *p;
266111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
267111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  font_opts->num_variations = 0;
268111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  g_free (font_opts->variations);
269111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  font_opts->variations = NULL;
270111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
271111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  if (!*s)
272111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    return true;
273111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
274111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  /* count the variations first, so we can allocate memory */
275111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  p = s;
276111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  do {
277111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    font_opts->num_variations++;
278111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    p = strchr (p, ',');
279111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    if (p)
280111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod      p++;
281111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  } while (p);
282111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
283111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
284111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
285111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  /* now do the actual parsing */
286111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  p = s;
287111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  font_opts->num_variations = 0;
288111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  while (p && *p) {
289111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    char *end = strchr (p, ',');
290111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
291111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod      font_opts->num_variations++;
292111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    p = end ? end + 1 : NULL;
293111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  }
294111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
295111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  return true;
296111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod}
297111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
2983bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2993bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodvoid
300b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodview_options_t::add_options (option_parser_t *parser)
3013bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{
3023bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  GOptionEntry entries[] =
3033bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  {
304bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"annotate",	0, 0, G_OPTION_ARG_NONE,	&this->annotate,		"Annotate output rendering",				NULL},
305d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod    {"background",	0, 0, G_OPTION_ARG_STRING,	&this->back,			"Set background color (default: " DEFAULT_BACK ")",	"rrggbb/rrggbbaa"},
306d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod    {"foreground",	0, 0, G_OPTION_ARG_STRING,	&this->fore,			"Set foreground color (default: " DEFAULT_FORE ")",	"rrggbb/rrggbbaa"},
307bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"line-space",	0, 0, G_OPTION_ARG_DOUBLE,	&this->line_space,		"Set space between lines (default: 0)",			"units"},
3089a34a50daac3563464e0031f377e85f0c704f2d6Behdad Esfahbod    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_margin,	"Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
309109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
310109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
311b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
312b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "view",
313b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "View options:",
314a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for output rendering",
315b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
316109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
3173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
318109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
319b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshape_options_t::add_options (option_parser_t *parser)
320109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
321109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
322109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
323fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod    {"list-shapers",	0, G_OPTION_FLAG_NO_ARG,
324fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &list_shapers,	"List available shapers and quit",	NULL},
3258f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod    {"shaper",		0, G_OPTION_FLAG_HIDDEN,
3268f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Hidden duplicate of --shapers",	NULL},
3278650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"shapers",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Set comma-separated list of shapers to try","list"},
328bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
329bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
330bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod    {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
331407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"bot",		0, 0, G_OPTION_ARG_NONE,	&this->bot,			"Treat text as beginning-of-paragraph",	NULL},
332407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"eot",		0, 0, G_OPTION_ARG_NONE,	&this->eot,			"Treat text as end-of-paragraph",	NULL},
333407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod    {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE,	&this->preserve_default_ignorables,	"Preserve Default-Ignorable characters",	NULL},
33430874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF8 byte indices, not char indices",	NULL},
335376d587f36b4ff10342ee6ca3bacd73532ea44c8Behdad Esfahbod    {"cluster-level",	0, 0, G_OPTION_ARG_INT,		&this->cluster_level,		"Cluster merging level (default: 0)",	"0/1/2"},
33639b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,	&this->normalize_glyphs,	"Rearrange glyph clusters in nominal order",	NULL},
33750067e280f381918b8e90b26df9e7bf20f98f0bdBehdad Esfahbod    {"num-iterations",	0, 0, G_OPTION_ARG_INT,		&this->num_iterations,		"Run shaper N times (default: 1)",	"N"},
338109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
339109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
340b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
341b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "shape",
342b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Shape options:",
343a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for the shaping process",
344b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
3458750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod
34630874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod  const gchar *features_help = "Comma-separated list of font features\n"
3478750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3488750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Features can be enabled or disabled, either globally or limited to\n"
3490de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    specific character ranges.  The format for specifying feature settings\n"
3500de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    follows.  All valid CSS font-feature-settings values other than 'normal'\n"
3510de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod    "    and 'inherited' are also accepted, though, not documented below.\n"
35295cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "\n"
35395cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    The range indices refer to the positions between Unicode characters,\n"
35495cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    unless the --utf8-clusters is provided, in which case range indices\n"
35595cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    refer to UTF-8 byte indices. The position before the first character\n"
35695cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod    "    is always 0.\n"
357d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod    "\n"
358d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod    "    The format is Python-esque.  Here is how it all works:\n"
3598750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3608750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      Syntax:       Value:    Start:    End:\n"
3618750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3628750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Setting value:\n"
3638750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern\"        1         0         ∞         # Turn feature on\n"
3648750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"+kern\"       1         0         ∞         # Turn feature on\n"
3658750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"-kern\"       0         0         ∞         # Turn feature off\n"
3668750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern=0\"      0         0         ∞         # Turn feature off\n"
3678750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern=1\"      1         0         ∞         # Turn feature on\n"
3688750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"aalt=2\"      2         0         ∞         # Choose 2nd alternate\n"
3698750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3708750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Setting index:\n"
3718750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[]\"      1         0         ∞         # Turn feature on\n"
3728750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[:]\"     1         0         ∞         # Turn feature on\n"
3738750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[5:]\"    1         5         ∞         # Turn feature on, partial\n"
3748750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[:5]\"    1         0         5         # Turn feature on, partial\n"
3758750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[3:5]\"   1         3         5         # Turn feature on, range\n"
3768750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "      \"kern[3]\"     1         3         3+1       # Turn feature on, single char\n"
3778750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3788750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "    Mixing it all:\n"
3798750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    "\n"
3803bc22eb7b843c77706bb13fc332009097f247813Behdad Esfahbod    "      \"aalt[3:5]=2\" 2         3         5         # Turn 2nd alternate on for range";
3818750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod
3828750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  GOptionEntry entries2[] =
3838750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  {
3848750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    {"features",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_features,	features_help,	"list"},
3858750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod    {NULL}
3868750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  };
3878750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod  parser->add_group (entries2,
3888750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     "features",
3898750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     "Features options:",
390a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for font features used",
3918750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod		     this);
392109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
3933bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
394cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbodstatic gboolean
395cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbodparse_font_size (const char *name G_GNUC_UNUSED,
396cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod		 const char *arg,
397cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod		 gpointer    data,
398cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod		 GError    **error G_GNUC_UNUSED)
399cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod{
400cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  font_options_t *font_opts = (font_options_t *) data;
401cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  if (0 == strcmp (arg, "upem"))
402cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  {
403cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
404cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    return true;
405cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  }
406a664810e38b479e05ea32cac263cb5730629f9d8Behdad Esfahbod  switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
407cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    case 1: font_opts->font_size_y = font_opts->font_size_x;
408cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    case 2: return true;
409cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    default:
410cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
411cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod		   "%s argument should be one to four space-separated numbers",
412cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod		   name);
413cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod      return false;
414cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  }
415cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod}
416109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
417b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::add_options (option_parser_t *parser)
418109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
4198650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  char *text = NULL;
4208650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod
4218650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
4228650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0);
4238650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    GString *s = g_string_new (NULL);
4248650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n    Supported font function implementations are: %s",
4258650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod		     supported_font_funcs[0].name,
4268650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod		     supported_font_funcs[0].name);
4278650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
4288650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {
4298650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      g_string_append_c (s, '/');
4308650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      g_string_append (s, supported_font_funcs[i].name);
4318650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    }
4328650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    text = g_string_free (s, FALSE);
4338650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    parser->free_later (text);
4348650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
4358650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod
436cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  char *font_size_text;
437cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  if (default_font_size == FONT_SIZE_UPEM)
438cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    font_size_text = (char *) "Font size (default: upem)";
439cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  else
440cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  {
441cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
442cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    parser->free_later (font_size_text);
443cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  }
444cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod
445109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
446109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
4478650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"font-file",	0, 0, G_OPTION_ARG_STRING,	&this->font_file,		"Set font file-name",			"filename"},
4488650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {"face-index",	0, 0, G_OPTION_ARG_INT,		&this->face_index,		"Set face index (default: 0)",		"index"},
449cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    {"font-size",	0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
450cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_font_size,	font_size_text,				"1/2 numbers or 'upem'"},
4515789ca69d7464bab5fa0c5bdf404f3afaa490fafBehdad Esfahbod    {"font-funcs",	0, 0, G_OPTION_ARG_STRING,	&this->font_funcs,		text,					"impl"},
452109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod    {NULL}
453109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  };
454b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
455b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "font",
456b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Font options:",
457a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for the font",
458b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
459111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
460111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  const gchar *variations_help = "Comma-separated list of font variations\n"
461111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    "\n"
462a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "    Variations are set globally. The format for specifying variation settings\n"
463a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "    follows.  All valid CSS font-variation-settings values other than 'normal'\n"
464111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    "    and 'inherited' are also accepted, though, not documented below.\n"
465111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    "\n"
466a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "    The format is a tag, optionally followed by an equals sign, followed by a\n"
467a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "    number. For example:\n"
468111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    "\n"
469a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "      \"wght=500\"\n"
470a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod    "      \"slnt=-7.5\"\n";
471111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
472111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  GOptionEntry entries2[] =
473111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  {
474111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    {"variations",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_variations,	variations_help,	"list"},
475111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod    {NULL}
476111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  };
477111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  parser->add_group (entries2,
478111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod		     "variations",
479111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod		     "Varitions options:",
480a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for font variations used",
481111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod		     this);
482109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod}
4833bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
484109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid
485b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::add_options (option_parser_t *parser)
486109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{
487109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  GOptionEntry entries[] =
488109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod  {
489b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
49078d41d8d69142ae95928b51215c0c0c5e3f5b3f0Behdad 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"},
491321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod    {"text-before",	0, 0, G_OPTION_ARG_STRING,	&this->text_before,		"Set text context before each line",	"string"},
492321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod    {"text-after",	0, 0, G_OPTION_ARG_STRING,	&this->text_after,		"Set text context after each line",	"string"},
4933bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod    {NULL}
4943bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  };
495b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
496b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "text",
497b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "Text options:",
498a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for the input text",
499b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
500b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
501b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
502b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid
503b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoutput_options_t::add_options (option_parser_t *parser)
504b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
5059815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  const char *text;
5069815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod
5079815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  if (NULL == supported_formats)
5081d55ffeb66381889f11ebb9039ca4ec2ca5efbabCollin Fair    text = "Set output serialization format";
5099815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod  else
510ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod  {
511ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod    char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
5122306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod    text = g_strdup_printf ("Set output format\n\n    Supported output formats are: %s", items);
513ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod    g_free (items);
5142306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod    parser->free_later ((char *) text);
515ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod  }
5169815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod
517b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  GOptionEntry entries[] =
518b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  {
519b5afd8f78e6b372f1bbed469329c1554adb20eeaBehdad Esfahbod    {"output-file",	0, 0, G_OPTION_ARG_STRING,	&this->output_file,		"Set output file-name (default: stdout)","filename"},
5209815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod    {"output-format",	0, 0, G_OPTION_ARG_STRING,	&this->output_format,		text,					"format"},
521b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    {NULL}
522b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  };
523b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  parser->add_group (entries,
524b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     "output",
5259ee176ee978e6668c4faf00028811ce70979122eCollin Fair		     "Output destination & format options:",
526a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for the destination & form of the output",
527b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod		     this);
528b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
5293bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
5303bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
5313bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
532b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodhb_font_t *
533b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::get_font (void) const
534b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
535b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  if (font)
536b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    return font;
537b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
538b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_blob_t *blob = NULL;
539b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
540b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  /* Create the blob */
5413bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  {
5424451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    char *font_data;
5434451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    unsigned int len = 0;
544b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    hb_destroy_func_t destroy;
545b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    void *user_data;
546b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    hb_memory_mode_t mm;
547b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
5484451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    /* This is a hell of a lot of code for just reading a file! */
549b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    if (!font_file)
5500594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "No font file set");
551b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
5524451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    if (0 == strcmp (font_file, "-")) {
5534451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      /* read it */
5544451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      GString *gs = g_string_new (NULL);
5554451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      char buf[BUFSIZ];
556e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__)
55721e5d7edc4f28157254ef29a8ac247f3a572f6a6Behdad Esfahbod      setmode (fileno (stdin), O_BINARY);
5584451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod#endif
5594451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      while (!feof (stdin)) {
5604451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	size_t ret = fread (buf, 1, sizeof (buf), stdin);
5614451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (ferror (stdin))
5620594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	  fail (false, "Failed reading font from standard input: %s",
5634451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod		strerror (errno));
5644451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	g_string_append_len (gs, buf, ret);
5654451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
5664451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      len = gs->len;
5670594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      font_data = g_string_free (gs, false);
5684451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      user_data = font_data;
5694451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      destroy = (hb_destroy_func_t) g_free;
5704451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      mm = HB_MEMORY_MODE_WRITABLE;
5714451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    } else {
572f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod      GError *error = NULL;
5730594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
5744451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      if (mf) {
5754451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	font_data = g_mapped_file_get_contents (mf);
5764451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	len = g_mapped_file_get_length (mf);
5774451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (len) {
578c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod	  destroy = (hb_destroy_func_t) g_mapped_file_unref;
5794451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  user_data = (void *) mf;
5804451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
5814451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	} else
582c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod	  g_mapped_file_unref (mf);
583f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod      } else {
5840594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	fail (false, "%s", error->message);
585f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod	//g_error_free (error);
5864451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
5874451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      if (!len) {
5884451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	/* GMappedFile is buggy, it doesn't fail if file isn't regular.
5894451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	 * Try reading.
5904451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
5914451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod        GError *error = NULL;
5924451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	gsize l;
5934451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	if (g_file_get_contents (font_file, &font_data, &l, &error)) {
5944451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  len = l;
5954451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  destroy = (hb_destroy_func_t) g_free;
5964451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  user_data = (void *) font_data;
5974451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  mm = HB_MEMORY_MODE_WRITABLE;
5984451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	} else {
5990594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod	  fail (false, "%s", error->message);
6004451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	  //g_error_free (error);
6014451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod	}
6024451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod      }
6034451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod    }
604b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
605642135f3b2d6d6eb800153c76c4718239733c0e6Behdad Esfahbod    if (debug)
606642135f3b2d6d6eb800153c76c4718239733c0e6Behdad Esfahbod      mm = HB_MEMORY_MODE_DUPLICATE;
607642135f3b2d6d6eb800153c76c4718239733c0e6Behdad Esfahbod
608b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
609b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
610b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
611b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  /* Create the face */
612b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_face_t *face = hb_face_create (blob, face_index);
613b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_blob_destroy (blob);
614b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
615b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
616b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  font = hb_font_create (face);
617b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
618cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  if (font_size_x == FONT_SIZE_UPEM)
619cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    font_size_x = hb_face_get_upem (face);
620cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  if (font_size_y == FONT_SIZE_UPEM)
621cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod    font_size_y = hb_face_get_upem (face);
622cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod
623cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
624cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
625cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  hb_font_set_scale (font, scale_x, scale_y);
626b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  hb_face_destroy (face);
627b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
628111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod  hb_font_set_variations (font, variations, num_variations);
629111f3e55178f7cd5a8ae4e8ae111cb48aea4acb5Behdad Esfahbod
6308650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  void (*set_font_funcs) (hb_font_t *) = NULL;
6318650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  if (!font_funcs)
6328650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
6338650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    set_font_funcs = supported_font_funcs[0].func;
6348650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
6358650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  else
6368650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  {
6378650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
638998e8dda938cfef0146f1bfc4e8973a0e12d7d35Chun-wei Fan      if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
6398650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      {
6408650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	set_font_funcs = supported_font_funcs[i].func;
6418650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	break;
6428650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      }
6438650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    if (!set_font_funcs)
6448650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    {
6458650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      GString *s = g_string_new (NULL);
6468650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
6478650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      {
6488650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod        if (i)
6498650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	  g_string_append_c (s, '/');
6508650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	g_string_append (s, supported_font_funcs[i].name);
6518650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      }
6528650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      char *p = g_string_free (s, FALSE);
6538650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
6548650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    font_funcs,
6558650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    p,
6568650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod	    supported_font_funcs[0].name);
6578650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod      //free (p);
6588650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod    }
6598650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  }
6608650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod  set_font_funcs (font);
661b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
662b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  return font;
663b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod}
664b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
665b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
666b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodconst char *
667b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::get_line (unsigned int *len)
668b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{
66955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (text) {
6703530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod    if (!line) line = text;
6713530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod    if (line_len == (unsigned int) -1)
6723530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      line_len = strlen (line);
67355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
6743530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod    if (!line_len) {
67555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      *len = 0;
67655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      return NULL;
67755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
67855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
6793530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod    const char *ret = line;
6803530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod    const char *p = (const char *) memchr (line, '\n', line_len);
68155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    unsigned int ret_len;
68255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (!p) {
6833530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      ret_len = line_len;
6843530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      line += ret_len;
6853530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      line_len = 0;
68655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    } else {
68755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      ret_len = p - ret;
6883530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      line += ret_len + 1;
6893530cc2d7c3b7102902cb0e38b0bf9f46188078dBehdad Esfahbod      line_len -= ret_len + 1;
69055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
69155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
69255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    *len = ret_len;
69355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    return ret;
69455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  }
69555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod
69655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (!fp) {
697b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod    if (!text_file)
6980594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (true, "At least one of text or text-file must be set");
699b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
70055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (0 != strcmp (text_file, "-"))
70155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      fp = fopen (text_file, "r");
70255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    else
70355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      fp = stdin;
7043bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
70555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    if (!fp)
7060594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod      fail (false, "Failed opening text file `%s': %s",
70755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod	    text_file, strerror (errno));
708b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
70955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    gs = g_string_new (NULL);
710b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod  }
711b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod
71255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  g_string_set_size (gs, 0);
71355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  char buf[BUFSIZ];
71455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  while (fgets (buf, sizeof (buf), fp)) {
71555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    unsigned int bytes = strlen (buf);
71627c36af411c7c4d75dd25d79fc76dd92c6bb9643Behdad Esfahbod    if (bytes && buf[bytes - 1] == '\n') {
71755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      bytes--;
71855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      g_string_append_len (gs, buf, bytes);
71955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      break;
72055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod    }
72155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod      g_string_append_len (gs, buf, bytes);
7223bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod  }
72355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  if (ferror (fp))
7240594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    fail (false, "Failed reading text: %s",
72555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod	  strerror (errno));
72655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  *len = gs->len;
72755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod  return !*len && feof (fp) ? NULL : gs->str;
7283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod}
729a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
730a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
731a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad EsfahbodFILE *
732a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbodoutput_options_t::get_file_handle (void)
733a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod{
734a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (fp)
735a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    return fp;
736a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
737a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (output_file)
738a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    fp = fopen (output_file, "wb");
739a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  else {
740e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__)
74121e5d7edc4f28157254ef29a8ac247f3a572f6a6Behdad Esfahbod    setmode (fileno (stdout), O_BINARY);
742a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod#endif
743a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod    fp = stdout;
744a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  }
745a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  if (!fp)
7460594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    fail (false, "Cannot open output file `%s': %s",
747a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod	  g_filename_display_name (output_file), strerror (errno));
748a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod
749a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod  return fp;
750a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod}
7518b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
752c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodstatic gboolean
753c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodparse_verbose (const char *name G_GNUC_UNUSED,
754c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       const char *arg G_GNUC_UNUSED,
755c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       gpointer    data G_GNUC_UNUSED,
756c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod	       GError    **error G_GNUC_UNUSED)
757c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod{
758c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod  format_options_t *format_opts = (format_options_t *) data;
7590594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
7600594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod  return true;
761c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod}
7628b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
7638b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid
7648b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodformat_options_t::add_options (option_parser_t *parser)
7658b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
7668b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  GOptionEntry entries[] =
7678b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  {
768820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"show-text",	0, 0, G_OPTION_ARG_NONE,	&this->show_text,		"Prefix each line of output with its corresponding input text",		NULL},
769820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"show-unicode",	0, 0, G_OPTION_ARG_NONE,	&this->show_unicode,		"Prefix each line of output with its corresponding input codepoint(s)",	NULL},
770820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"show-line-num",	0, 0, G_OPTION_ARG_NONE,	&this->show_line_num,		"Prefix each line of output with its corresponding input line number",	NULL},
771820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"verbose",		0, G_OPTION_FLAG_NO_ARG,
772820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_verbose,	"Prefix each line of output with all of the above",			NULL},
773820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"no-glyph-names",	0, G_OPTION_FLAG_REVERSE,
774820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod			      G_OPTION_ARG_NONE,	&this->show_glyph_names,	"Output glyph indices instead of names",				NULL},
775820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"no-positions",	0, G_OPTION_FLAG_REVERSE,
776820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod			      G_OPTION_ARG_NONE,	&this->show_positions,		"Do not output glyph positions",					NULL},
777820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod    {"no-clusters",	0, G_OPTION_FLAG_REVERSE,
778820505a186ff60e4bae9d717fe4d7ab2390e6fefBehdad Esfahbod			      G_OPTION_ARG_NONE,	&this->show_clusters,		"Do not output cluster indices",					NULL},
779fdd1770e006ca2d2973c049177ceda87a575e07fBehdad Esfahbod    {"show-extents",	0, 0, G_OPTION_ARG_NONE,	&this->show_extents,		"Output glyph extents",							NULL},
7808b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod    {NULL}
7818b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  };
7828b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  parser->add_group (entries,
7839ee176ee978e6668c4faf00028811ce70979122eCollin Fair		     "output-syntax",
7849ee176ee978e6668c4faf00028811ce70979122eCollin Fair		     "Output syntax:\n"
7859ee176ee978e6668c4faf00028811ce70979122eCollin Fair         "    text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
7869ee176ee978e6668c4faf00028811ce70979122eCollin Fair         "    json: [{\"g\": <glyph name or index>, \"ax\": <horizontal advance>, \"ay\": <vertical advance>, \"dx\": <horizontal displacement>, \"dy\": <vertical displacement>, \"cl\": <glyph cluster index within input>}, ...]\n"
7879ee176ee978e6668c4faf00028811ce70979122eCollin Fair         "\nOutput syntax options:",
788a42909057207cbed1ef95716170434c05beb0002Behdad Esfahbod		     "Options for the syntax of the output",
7898b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     this);
7908b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
7918b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod
7928b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid
793cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_unicode (hb_buffer_t *buffer,
794cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				     GString     *gs)
795cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod{
796cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  unsigned int num_glyphs = hb_buffer_get_length (buffer);
797cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
798cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod
799cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  g_string_append_c (gs, '<');
800cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  for (unsigned int i = 0; i < num_glyphs; i++)
801cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  {
802cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    if (i)
803cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod      g_string_append_c (gs, ',');
804cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    g_string_append_printf (gs, "U+%04X", info->codepoint);
805cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod    info++;
806cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  }
807cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod  g_string_append_c (gs, '>');
808cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod}
809cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod
810cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodvoid
811cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_glyphs (hb_buffer_t *buffer,
812cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				    hb_font_t   *font,
813f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod				    hb_buffer_serialize_format_t output_format,
814f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod				    hb_buffer_serialize_flags_t flags,
815cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod				    GString     *gs)
8168b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
817c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod  g_string_append_c (gs, '[');
818f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  unsigned int num_glyphs = hb_buffer_get_length (buffer);
819f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  unsigned int start = 0;
820f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod
821f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  while (start < num_glyphs) {
822f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    char buf[1024];
823f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    unsigned int consumed;
824f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
825f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					 buf, sizeof (buf), &consumed,
826f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					 font, output_format, flags);
827f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!consumed)
828f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      break;
829f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    g_string_append (gs, buf);
8308b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  }
831c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod  g_string_append_c (gs, ']');
8328b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
833cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid
834cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodformat_options_t::serialize_line_no (unsigned int  line_no,
835cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod				     GString      *gs)
836cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{
837cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_line_num)
838cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_printf (gs, "%d: ", line_no);
839cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod}
840cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid
8415db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
8425db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    unsigned int  line_no,
8435db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    const char   *text,
8445db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    unsigned int  text_len,
8455db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    hb_font_t    *font,
8465db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					    GString      *gs)
847cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{
848cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_text) {
849cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    serialize_line_no (line_no, gs);
850d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod    g_string_append_c (gs, '(');
851cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_len (gs, text, text_len);
852d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod    g_string_append_c (gs, ')');
853cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_c (gs, '\n');
854cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  }
855cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod
856cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  if (show_unicode) {
857cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    serialize_line_no (line_no, gs);
858ae62166519291057316a9d15cea3f1570fcb5eafBehdad Esfahbod    serialize_unicode (buffer, gs);
859cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod    g_string_append_c (gs, '\n');
860cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  }
8615db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod}
8625db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid
8635db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_message (unsigned int  line_no,
8645db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod				     const char   *msg,
8655db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod				     GString      *gs)
8665db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{
8675db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  serialize_line_no (line_no, gs);
8685db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  g_string_append_printf (gs, "%s", msg);
8695db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  g_string_append_c (gs, '\n');
8705db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod}
8715db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid
8725db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
8735db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      unsigned int  line_no,
8745db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      const char   *text,
8755db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      unsigned int  text_len,
8765db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      hb_font_t    *font,
877f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					      hb_buffer_serialize_format_t output_format,
878f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod					      hb_buffer_serialize_flags_t format_flags,
8795db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod					      GString      *gs)
8805db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{
881cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  serialize_line_no (line_no, gs);
882f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  serialize_glyphs (buffer, font, output_format, format_flags, gs);
883cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  g_string_append_c (gs, '\n');
884cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod}
885