xref: /aosp_15_r20/external/harfbuzz_ng/util/options.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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 #if defined(_WIN32) || defined(__CYGWIN__)
42 #include <io.h> /* for setmode() under Windows */
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h> /* for isatty() */
46 #endif
47 
48 
49 #include <hb-features.h>
50 #include <hb.h>
51 #include <hb-ot.h>
52 
53 #include <glib.h>
54 #include <glib/gprintf.h>
55 
56 
57 
58 static inline void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
59 
60 static inline void
fail(hb_bool_t suggest_help,const char * format,...)61 fail (hb_bool_t suggest_help, const char *format, ...)
62 {
63   const char *msg;
64 
65   va_list vap;
66   va_start (vap, format);
67   msg = g_strdup_vprintf (format, vap);
68   va_end (vap);
69   const char *prgname = g_get_prgname ();
70   g_printerr ("%s: %s\n", prgname, msg);
71   if (suggest_help)
72     g_printerr ("Try `%s --help' for more information.\n", prgname);
73 
74   exit (1);
75 }
76 
77 struct option_parser_t
78 {
option_parser_toption_parser_t79   option_parser_t (const char *parameter_string = nullptr)
80   : context (g_option_context_new (parameter_string)),
81     to_free (g_ptr_array_new ())
82   {}
83 
_g_free_g_funcoption_parser_t84   static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
85 
~option_parser_toption_parser_t86   ~option_parser_t ()
87   {
88     g_option_context_free (context);
89     g_ptr_array_foreach (to_free, _g_free_g_func, nullptr);
90     g_ptr_array_free (to_free, TRUE);
91   }
92 
93   void add_options ();
94 
95   static void
post_parse_option_parser_t96   post_parse_ (void *thiz, GError **error) {}
97   template <typename Type>
98   static auto
post_parse_option_parser_t99   post_parse_ (Type *thiz, GError **error) -> decltype (thiz->post_parse (error))
100   { thiz->post_parse (error); }
101   template <typename Type>
102   static gboolean
post_parseoption_parser_t103   post_parse (GOptionContext *context G_GNUC_UNUSED,
104 	      GOptionGroup *group G_GNUC_UNUSED,
105 	      gpointer data,
106 	      GError **error)
107   {
108     option_parser_t::post_parse_ (static_cast<Type *> (data), error);
109     return !*error;
110   }
111 
112   template <typename Type>
add_groupoption_parser_t113   void add_group (GOptionEntry   *entries,
114 		  const gchar    *name,
115 		  const gchar    *description,
116 		  const gchar    *help_description,
117 		  Type           *closure,
118 		  bool		  add_parse_hooks = true)
119   {
120     GOptionGroup *group = g_option_group_new (name, description, help_description,
121 					      static_cast<gpointer>(closure), nullptr);
122     g_option_group_add_entries (group, entries);
123     if (add_parse_hooks)
124       g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
125     g_option_context_add_group (context, group);
126   }
127 
128   template <typename Type>
add_main_groupoption_parser_t129   void add_main_group (GOptionEntry   *entries,
130 		       Type           *closure)
131   {
132     GOptionGroup *group = g_option_group_new (nullptr, nullptr, nullptr,
133 					      static_cast<gpointer>(closure), nullptr);
134     g_option_group_add_entries (group, entries);
135     /* https://gitlab.gnome.org/GNOME/glib/-/issues/2460 */
136     //g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
137     g_option_context_set_main_group (context, group);
138   }
139 
set_summaryoption_parser_t140   void set_summary (const char *summary)
141   {
142     g_option_context_set_summary (context, summary);
143   }
set_descriptionoption_parser_t144   void set_description (const char *description)
145   {
146     g_option_context_set_description (context, description);
147   }
148 
free_lateroption_parser_t149   void free_later (char *p) {
150     g_ptr_array_add (to_free, p);
151   }
152 
153   bool parse (int *argc, char ***argv, bool ignore_error = false);
154 
155   GOptionContext *context;
156   protected:
157   GPtrArray *to_free;
158 };
159 
160 
161 static inline gchar *
shapers_to_string()162 shapers_to_string ()
163 {
164   GString *shapers = g_string_new (nullptr);
165   const char **shaper_list = hb_shape_list_shapers ();
166 
167   for (; *shaper_list; shaper_list++) {
168     g_string_append (shapers, *shaper_list);
169     g_string_append_c (shapers, ',');
170   }
171   g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
172 
173   return g_string_free (shapers, false);
174 }
175 
176 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)177 show_version (const char *name G_GNUC_UNUSED,
178 	      const char *arg G_GNUC_UNUSED,
179 	      gpointer    data G_GNUC_UNUSED,
180 	      GError    **error G_GNUC_UNUSED)
181 {
182   g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
183 
184   char *shapers = shapers_to_string ();
185   g_printf ("Available shapers: %s\n", shapers);
186   g_free (shapers);
187   if (strcmp (HB_VERSION_STRING, hb_version_string ()))
188     g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
189 
190   exit(0);
191 }
192 
193 inline void
add_options()194 option_parser_t::add_options ()
195 {
196   GOptionEntry entries[] =
197   {
198     {"version",		0, G_OPTION_FLAG_NO_ARG,
199 			      G_OPTION_ARG_CALLBACK,	(gpointer) &show_version,	"Show version numbers",			nullptr},
200     {nullptr}
201   };
202   g_option_context_add_main_entries (context, entries, nullptr);
203 }
204 
205 inline bool
parse(int * argc,char *** argv,bool ignore_error)206 option_parser_t::parse (int *argc, char ***argv, bool ignore_error)
207 {
208   setlocale (LC_ALL, "");
209 
210   GError *parse_error = nullptr;
211   if (!g_option_context_parse (context, argc, argv, &parse_error))
212   {
213     if (parse_error)
214     {
215       if (!ignore_error)
216 	fail (true, "%s", parse_error->message);
217       g_error_free (parse_error);
218     }
219     else
220     {
221       if (!ignore_error)
222 	fail (true, "Option parse error");
223     }
224     return false;
225   }
226   return true;
227 }
228 
229 
230 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
231 #if defined (_MSC_VER) && (_MSC_VER < 1800)
232 
233 #ifndef FLT_RADIX
234 #define FLT_RADIX 2
235 #endif
236 
scalbn(long double x,int exp)237 __inline long double scalbn (long double x, int exp)
238 {
239   return x * (pow ((long double) FLT_RADIX, exp));
240 }
241 
scalbnf(float x,int exp)242 __inline float scalbnf (float x, int exp)
243 {
244   return x * (pow ((float) FLT_RADIX, exp));
245 }
246 #endif
247 
248 static inline bool
parse_color(const char * s,unsigned & r,unsigned & g,unsigned & b,unsigned & a)249 parse_color (const char *s,
250 	     unsigned &r,
251 	     unsigned &g,
252 	     unsigned &b,
253 	     unsigned &a)
254 {
255   bool ret = false;
256 
257   while (*s == ' ') s++;
258   if (*s == '#') s++;
259 
260   unsigned sr, sg, sb, sa;
261   sa = 255;
262   if (sscanf (s, "%2x%2x%2x%2x", &sr, &sg, &sb, &sa) <= 2)
263   {
264     if (sscanf (s, "%1x%1x%1x%1x", &sr, &sg, &sb, &sa) >= 3)
265     {
266       sr *= 17;
267       sg *= 17;
268       sb *= 17;
269       sa *= 17;
270       ret = true;
271     }
272   }
273   else
274     ret = true;
275 
276   if (ret)
277   {
278     r = sr;
279     g = sg;
280     b = sb;
281     a = sa;
282   }
283 
284   return ret;
285 }
286 
287 
288 #endif
289