2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
42 #include <unistd.h> /* for isatty() */
44 #if defined(_WIN32) || defined(__CYGWIN__)
45 #include <io.h> /* for setmode() under Windows */
51 #include <glib/gprintf.h>
54 static inline void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
57 fail (hb_bool_t suggest_help, const char *format, ...)
62 va_start (vap, format);
63 msg = g_strdup_vprintf (format, vap);
65 const char *prgname = g_get_prgname ();
66 g_printerr ("%s: %s\n", prgname, msg);
68 g_printerr ("Try `%s --help' for more information.\n", prgname);
73 struct option_parser_t
75 option_parser_t (const char *parameter_string = nullptr)
76 : context (g_option_context_new (parameter_string)),
77 to_free (g_ptr_array_new ())
80 static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
84 g_option_context_free (context);
85 g_ptr_array_foreach (to_free, _g_free_g_func, nullptr);
86 g_ptr_array_free (to_free, TRUE);
92 post_parse_ (void *thiz, GError **error) {}
93 template <typename Type>
95 post_parse_ (Type *thiz, GError **error) -> decltype (thiz->post_parse (error))
96 { thiz->post_parse (error); }
97 template <typename Type>
99 post_parse (GOptionContext *context G_GNUC_UNUSED,
100 GOptionGroup *group G_GNUC_UNUSED,
104 option_parser_t::post_parse_ (static_cast<Type *> (data), error);
108 template <typename Type>
109 void add_group (GOptionEntry *entries,
111 const gchar *description,
112 const gchar *help_description,
114 bool add_parse_hooks = true)
116 GOptionGroup *group = g_option_group_new (name, description, help_description,
117 static_cast<gpointer>(closure), nullptr);
118 g_option_group_add_entries (group, entries);
120 g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
121 g_option_context_add_group (context, group);
124 template <typename Type>
125 void add_main_group (GOptionEntry *entries,
128 GOptionGroup *group = g_option_group_new (nullptr, nullptr, nullptr,
129 static_cast<gpointer>(closure), nullptr);
130 g_option_group_add_entries (group, entries);
131 /* https://gitlab.gnome.org/GNOME/glib/-/issues/2460 */
132 //g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
133 g_option_context_set_main_group (context, group);
136 void set_summary (const char *summary)
138 g_option_context_set_summary (context, summary);
140 void set_description (const char *description)
142 g_option_context_set_description (context, description);
145 void free_later (char *p) {
146 g_ptr_array_add (to_free, p);
149 bool parse (int *argc, char ***argv, bool ignore_error = false);
151 GOptionContext *context;
157 static inline gchar *
160 GString *shapers = g_string_new (nullptr);
161 const char **shaper_list = hb_shape_list_shapers ();
163 for (; *shaper_list; shaper_list++) {
164 g_string_append (shapers, *shaper_list);
165 g_string_append_c (shapers, ',');
167 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
169 return g_string_free (shapers, false);
172 static G_GNUC_NORETURN gboolean
173 show_version (const char *name G_GNUC_UNUSED,
174 const char *arg G_GNUC_UNUSED,
175 gpointer data G_GNUC_UNUSED,
176 GError **error G_GNUC_UNUSED)
178 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
180 char *shapers = shapers_to_string ();
181 g_printf ("Available shapers: %s\n", shapers);
183 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
184 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
190 option_parser_t::add_options ()
192 GOptionEntry entries[] =
194 {"version", 0, G_OPTION_FLAG_NO_ARG,
195 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
198 g_option_context_add_main_entries (context, entries, nullptr);
202 option_parser_t::parse (int *argc, char ***argv, bool ignore_error)
204 setlocale (LC_ALL, "");
206 GError *parse_error = nullptr;
207 if (!g_option_context_parse (context, argc, argv, &parse_error))
212 fail (true, "%s", parse_error->message);
213 g_error_free (parse_error);
218 fail (true, "Option parse error");
226 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
227 #if defined (_MSC_VER) && (_MSC_VER < 1800)
233 __inline long double scalbn (long double x, int exp)
235 return x * (pow ((long double) FLT_RADIX, exp));
238 __inline float scalbnf (float x, int exp)
240 return x * (pow ((float) FLT_RADIX, exp));