9b7baa7167495fb9a9c64491845797e0425943af
[framework/uifw/harfbuzz.git] / util / options.hh
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
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <math.h>
40 #include <locale.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h> /* for isatty() */
45 #endif
46 #ifdef HAVE_IO_H
47 #include <io.h> /* for _setmode() under Windows */
48 #endif
49
50 #include <hb.h>
51 #ifdef HAVE_OT
52 #include <hb-ot.h>
53 #endif
54 #include <glib.h>
55 #include <glib/gprintf.h>
56
57 #undef MIN
58 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
59
60 #undef MAX
61 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
62
63
64 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN;
65
66
67 extern hb_bool_t debug;
68
69 struct option_group_t
70 {
71   virtual void add_options (struct option_parser_t *parser) = 0;
72
73   virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
74   virtual void post_parse (GError **error G_GNUC_UNUSED) {};
75 };
76
77
78 struct option_parser_t
79 {
80   option_parser_t (const char *usage) {
81     memset (this, 0, sizeof (*this));
82     usage_str = usage;
83     context = g_option_context_new (usage);
84
85     add_main_options ();
86   }
87   ~option_parser_t (void) {
88     g_option_context_free (context);
89   }
90
91   void add_main_options (void);
92
93   void add_group (GOptionEntry   *entries,
94                   const gchar    *name,
95                   const gchar    *description,
96                   const gchar    *help_description,
97                   option_group_t *option_group);
98
99   void parse (int *argc, char ***argv);
100
101   G_GNUC_NORETURN void usage (void) {
102     g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
103     exit (1);
104   }
105
106   const char *usage_str;
107   GOptionContext *context;
108 };
109
110
111 #define DEFAULT_MARGIN 16
112 #define DEFAULT_FORE "#000000"
113 #define DEFAULT_BACK "#FFFFFF"
114 #define DEFAULT_FONT_SIZE 256
115
116 struct view_options_t : option_group_t
117 {
118   view_options_t (option_parser_t *parser) {
119     annotate = false;
120     fore = DEFAULT_FORE;
121     back = DEFAULT_BACK;
122     line_space = 0;
123     margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
124     font_size = DEFAULT_FONT_SIZE;
125
126     add_options (parser);
127   }
128
129   void add_options (option_parser_t *parser);
130
131   hb_bool_t annotate;
132   const char *fore;
133   const char *back;
134   double line_space;
135   struct margin_t {
136     double t, r, b, l;
137   } margin;
138   double font_size;
139 };
140
141
142 struct shape_options_t : option_group_t
143 {
144   shape_options_t (option_parser_t *parser)
145   {
146     direction = language = script = NULL;
147     features = NULL;
148     num_features = 0;
149     shapers = NULL;
150     utf8_clusters = false;
151
152     add_options (parser);
153   }
154   ~shape_options_t (void)
155   {
156     free (features);
157     g_free (shapers);
158   }
159
160   void add_options (option_parser_t *parser);
161
162   void setup_buffer (hb_buffer_t *buffer)
163   {
164     hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
165     hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
166     hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
167   }
168
169   void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len)
170   {
171     hb_buffer_reset (buffer);
172     hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
173
174     if (!utf8_clusters) {
175       /* Reset cluster values to refer to Unicode character index
176        * instead of UTF-8 index. */
177       unsigned int num_glyphs = hb_buffer_get_length (buffer);
178       hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
179       for (unsigned int i = 0; i < num_glyphs; i++)
180       {
181         info->cluster = i;
182         info++;
183       }
184     }
185
186     setup_buffer (buffer);
187   }
188
189   hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
190   {
191     return hb_shape_full (font, buffer, features, num_features, shapers);
192   }
193
194   void shape_closure (const char *text, int text_len,
195                       hb_font_t *font, hb_buffer_t *buffer,
196                       hb_set_t *glyphs)
197   {
198     hb_buffer_reset (buffer);
199     hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
200     setup_buffer (buffer);
201     hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
202   }
203
204   const char *direction;
205   const char *language;
206   const char *script;
207   hb_feature_t *features;
208   unsigned int num_features;
209   char **shapers;
210   hb_bool_t utf8_clusters;
211 };
212
213
214 struct font_options_t : option_group_t
215 {
216   font_options_t (option_parser_t *parser) {
217     font_file = NULL;
218     face_index = 0;
219
220     font = NULL;
221
222     add_options (parser);
223   }
224   ~font_options_t (void) {
225     hb_font_destroy (font);
226   }
227
228   void add_options (option_parser_t *parser);
229
230   hb_font_t *get_font (void) const;
231
232   const char *font_file;
233   int face_index;
234
235   private:
236   mutable hb_font_t *font;
237 };
238
239
240 struct text_options_t : option_group_t
241 {
242   text_options_t (option_parser_t *parser) {
243     text = NULL;
244     text_file = NULL;
245
246     fp = NULL;
247     gs = NULL;
248     text_len = (unsigned int) -1;
249
250     add_options (parser);
251   }
252   ~text_options_t (void) {
253     if (gs)
254       g_string_free (gs, true);
255     if (fp)
256       fclose (fp);
257   }
258
259   void add_options (option_parser_t *parser);
260
261   void post_parse (GError **error G_GNUC_UNUSED) {
262     if (text && text_file)
263       g_set_error (error,
264                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
265                    "Only one of text and text-file can be set");
266
267   };
268
269   const char *get_line (unsigned int *len);
270
271   const char *text;
272   const char *text_file;
273
274   private:
275   FILE *fp;
276   GString *gs;
277   unsigned int text_len;
278 };
279
280 struct output_options_t : option_group_t
281 {
282   output_options_t (option_parser_t *parser) {
283     output_file = NULL;
284     output_format = NULL;
285
286     fp = NULL;
287
288     add_options (parser);
289   }
290   ~output_options_t (void) {
291     if (fp)
292       fclose (fp);
293   }
294
295   void add_options (option_parser_t *parser);
296
297   void post_parse (GError **error G_GNUC_UNUSED)
298   {
299     if (output_file && !output_format) {
300       output_format = strrchr (output_file, '.');
301       if (output_format)
302           output_format++; /* skip the dot */
303     }
304
305     if (output_file && 0 == strcmp (output_file, "-"))
306       output_file = NULL; /* STDOUT */
307   }
308
309   FILE *get_file_handle (void);
310
311   const char *output_file;
312   const char *output_format;
313
314   mutable FILE *fp;
315 };
316
317 struct format_options_t : option_group_t
318 {
319   format_options_t (option_parser_t *parser) {
320     show_glyph_names = true;
321     show_positions = true;
322     show_clusters = true;
323     show_text = false;
324     show_unicode = false;
325     show_line_num = false;
326
327     add_options (parser);
328   }
329
330   void add_options (option_parser_t *parser);
331
332   void serialize_unicode (hb_buffer_t  *buffer,
333                           GString      *gs);
334   void serialize_glyphs (hb_buffer_t  *buffer,
335                          hb_font_t    *font,
336                          hb_bool_t    utf8_clusters,
337                          GString      *gs);
338   void serialize_line_no (unsigned int  line_no,
339                           GString      *gs);
340   void serialize_buffer_of_text (hb_buffer_t  *buffer,
341                                  unsigned int  line_no,
342                                  const char   *text,
343                                  unsigned int  text_len,
344                                  hb_font_t    *font,
345                                  hb_bool_t     utf8_clusters,
346                                  GString      *gs);
347   void serialize_message (unsigned int  line_no,
348                           const char   *msg,
349                           GString      *gs);
350   void serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
351                                    unsigned int  line_no,
352                                    const char   *text,
353                                    unsigned int  text_len,
354                                    hb_font_t    *font,
355                                    hb_bool_t     utf8_clusters,
356                                    GString      *gs);
357
358
359   hb_bool_t show_glyph_names;
360   hb_bool_t show_positions;
361   hb_bool_t show_clusters;
362   hb_bool_t show_text;
363   hb_bool_t show_unicode;
364   hb_bool_t show_line_num;
365 };
366
367
368 #endif