1 /*
2 * Copyright © 2011 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27 #ifndef OPTIONS_HH
28 #define OPTIONS_HH
29
30 #include "hb.hh"
31
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <math.h>
38 #include <locale.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h> /* for isatty() */
43 #endif
44 #if defined(_WIN32) || defined(__CYGWIN__)
45 #include <io.h> /* for setmode() under Windows */
46 #endif
47
48 #include <hb.h>
49 #include <hb-ot.h>
50 #include <glib.h>
51 #include <glib/gprintf.h>
52
53
54 static inline void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
55
56 static inline void
fail(hb_bool_t suggest_help,const char * format,...)57 fail (hb_bool_t suggest_help, const char *format, ...)
58 {
59 const char *msg;
60
61 va_list vap;
62 va_start (vap, format);
63 msg = g_strdup_vprintf (format, vap);
64 va_end (vap);
65 const char *prgname = g_get_prgname ();
66 g_printerr ("%s: %s\n", prgname, msg);
67 if (suggest_help)
68 g_printerr ("Try `%s --help' for more information.\n", prgname);
69
70 exit (1);
71 }
72
73 struct option_parser_t
74 {
option_parser_toption_parser_t75 option_parser_t (const char *parameter_string = nullptr)
76 : context (g_option_context_new (parameter_string)),
77 to_free (g_ptr_array_new ())
78 {}
79
_g_free_g_funcoption_parser_t80 static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
81
~option_parser_toption_parser_t82 ~option_parser_t ()
83 {
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);
87 }
88
89 void add_options ();
90
91 static void
post_parse_option_parser_t92 post_parse_ (void *thiz, GError **error) {}
93 template <typename Type>
94 static auto
post_parse_option_parser_t95 post_parse_ (Type *thiz, GError **error) -> decltype (thiz->post_parse (error))
96 { thiz->post_parse (error); }
97 template <typename Type>
98 static gboolean
post_parseoption_parser_t99 post_parse (GOptionContext *context G_GNUC_UNUSED,
100 GOptionGroup *group G_GNUC_UNUSED,
101 gpointer data,
102 GError **error)
103 {
104 option_parser_t::post_parse_ (static_cast<Type *> (data), error);
105 return !*error;
106 }
107
108 template <typename Type>
add_groupoption_parser_t109 void add_group (GOptionEntry *entries,
110 const gchar *name,
111 const gchar *description,
112 const gchar *help_description,
113 Type *closure,
114 bool add_parse_hooks = true)
115 {
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);
119 if (add_parse_hooks)
120 g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
121 g_option_context_add_group (context, group);
122 }
123
124 template <typename Type>
add_main_groupoption_parser_t125 void add_main_group (GOptionEntry *entries,
126 Type *closure)
127 {
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);
134 }
135
set_summaryoption_parser_t136 void set_summary (const char *summary)
137 {
138 g_option_context_set_summary (context, summary);
139 }
set_descriptionoption_parser_t140 void set_description (const char *description)
141 {
142 g_option_context_set_description (context, description);
143 }
144
free_lateroption_parser_t145 void free_later (char *p) {
146 g_ptr_array_add (to_free, p);
147 }
148
149 bool parse (int *argc, char ***argv, bool ignore_error = false);
150
151 GOptionContext *context;
152 protected:
153 GPtrArray *to_free;
154 };
155
156
157 static inline gchar *
shapers_to_string()158 shapers_to_string ()
159 {
160 GString *shapers = g_string_new (nullptr);
161 const char **shaper_list = hb_shape_list_shapers ();
162
163 for (; *shaper_list; shaper_list++) {
164 g_string_append (shapers, *shaper_list);
165 g_string_append_c (shapers, ',');
166 }
167 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
168
169 return g_string_free (shapers, false);
170 }
171
172 static G_GNUC_NORETURN gboolean
show_version(const char * name G_GNUC_UNUSED,const char * arg G_GNUC_UNUSED,gpointer data G_GNUC_UNUSED,GError ** error G_GNUC_UNUSED)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)
177 {
178 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
179
180 char *shapers = shapers_to_string ();
181 g_printf ("Available shapers: %s\n", shapers);
182 g_free (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 ());
185
186 exit(0);
187 }
188
189 inline void
add_options()190 option_parser_t::add_options ()
191 {
192 GOptionEntry entries[] =
193 {
194 {"version", 0, G_OPTION_FLAG_NO_ARG,
195 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
196 {nullptr}
197 };
198 g_option_context_add_main_entries (context, entries, nullptr);
199 }
200
201 inline bool
parse(int * argc,char *** argv,bool ignore_error)202 option_parser_t::parse (int *argc, char ***argv, bool ignore_error)
203 {
204 setlocale (LC_ALL, "");
205
206 GError *parse_error = nullptr;
207 if (!g_option_context_parse (context, argc, argv, &parse_error))
208 {
209 if (parse_error)
210 {
211 if (!ignore_error)
212 fail (true, "%s", parse_error->message);
213 g_error_free (parse_error);
214 }
215 else
216 {
217 if (!ignore_error)
218 fail (true, "Option parse error");
219 }
220 return false;
221 }
222 return true;
223 }
224
225
226 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
227 #if defined (_MSC_VER) && (_MSC_VER < 1800)
228
229 #ifndef FLT_RADIX
230 #define FLT_RADIX 2
231 #endif
232
scalbn(long double x,int exp)233 __inline long double scalbn (long double x, int exp)
234 {
235 return x * (pow ((long double) FLT_RADIX, exp));
236 }
237
scalbnf(float x,int exp)238 __inline float scalbnf (float x, int exp)
239 {
240 return x * (pow ((float) FLT_RADIX, exp));
241 }
242 #endif
243
244 #endif
245