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 #if defined(_WIN32) || defined(__CYGWIN__)
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 #if !GLIB_CHECK_VERSION (2, 22, 0)
58 # define g_mapped_file_unref g_mapped_file_free
59 #endif
60
61
62 /* A few macros copied from hb-private.hh. */
63
64 #if __GNUC__ >= 4
65 #define HB_UNUSED __attribute__((unused))
66 #else
67 #define HB_UNUSED
68 #endif
69
70 #undef MIN
MIN(const Type & a,const Type & b)71 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
72
73 #undef MAX
MAX(const Type & a,const Type & b)74 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
75
76 #undef ARRAY_LENGTH
77 template <typename Type, unsigned int n>
ARRAY_LENGTH(const Type (&)[n])78 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
79 /* A const version, but does not detect erratically being called on pointers. */
80 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
81
82 #define _ASSERT_STATIC1(_line, _cond) HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
83 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
84 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
85
86
87 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
88
89
90 extern hb_bool_t debug;
91
92 struct option_group_t
93 {
94 virtual void add_options (struct option_parser_t *parser) = 0;
95
pre_parseoption_group_t96 virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
post_parseoption_group_t97 virtual void post_parse (GError **error G_GNUC_UNUSED) {};
98 };
99
100
101 struct option_parser_t
102 {
option_parser_toption_parser_t103 option_parser_t (const char *usage) {
104 memset (this, 0, sizeof (*this));
105 usage_str = usage;
106 context = g_option_context_new (usage);
107 to_free = g_ptr_array_new ();
108
109 add_main_options ();
110 }
~option_parser_toption_parser_t111 ~option_parser_t (void) {
112 g_option_context_free (context);
113 g_ptr_array_foreach (to_free, (GFunc) g_free, NULL);
114 g_ptr_array_free (to_free, TRUE);
115 }
116
117 void add_main_options (void);
118
119 void add_group (GOptionEntry *entries,
120 const gchar *name,
121 const gchar *description,
122 const gchar *help_description,
123 option_group_t *option_group);
124
free_lateroption_parser_t125 void free_later (char *p) {
126 g_ptr_array_add (to_free, p);
127 }
128
129 void parse (int *argc, char ***argv);
130
usageoption_parser_t131 G_GNUC_NORETURN void usage (void) {
132 g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
133 exit (1);
134 }
135
136 private:
137 const char *usage_str;
138 GOptionContext *context;
139 GPtrArray *to_free;
140 };
141
142
143 #define DEFAULT_MARGIN 16
144 #define DEFAULT_FORE "#000000"
145 #define DEFAULT_BACK "#FFFFFF"
146 #define FONT_SIZE_UPEM 0x7FFFFFFF
147 #define FONT_SIZE_NONE 0
148
149 struct view_options_t : option_group_t
150 {
view_options_tview_options_t151 view_options_t (option_parser_t *parser) {
152 annotate = false;
153 fore = NULL;
154 back = NULL;
155 line_space = 0;
156 margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
157
158 add_options (parser);
159 }
~view_options_tview_options_t160 ~view_options_t (void)
161 {
162 g_free (fore);
163 g_free (back);
164 }
165
166 void add_options (option_parser_t *parser);
167
168 hb_bool_t annotate;
169 char *fore;
170 char *back;
171 double line_space;
172 struct margin_t {
173 double t, r, b, l;
174 } margin;
175 };
176
177
178 struct shape_options_t : option_group_t
179 {
shape_options_tshape_options_t180 shape_options_t (option_parser_t *parser)
181 {
182 direction = language = script = NULL;
183 bot = eot = preserve_default_ignorables = false;
184 features = NULL;
185 num_features = 0;
186 shapers = NULL;
187 utf8_clusters = false;
188 cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
189 normalize_glyphs = false;
190 num_iterations = 1;
191
192 add_options (parser);
193 }
~shape_options_tshape_options_t194 ~shape_options_t (void)
195 {
196 g_free (direction);
197 g_free (language);
198 g_free (script);
199 free (features);
200 g_strfreev (shapers);
201 }
202
203 void add_options (option_parser_t *parser);
204
setup_buffershape_options_t205 void setup_buffer (hb_buffer_t *buffer)
206 {
207 hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
208 hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
209 hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
210 hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_DEFAULT |
211 (bot ? HB_BUFFER_FLAG_BOT : 0) |
212 (eot ? HB_BUFFER_FLAG_EOT : 0) |
213 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0)));
214 hb_buffer_set_cluster_level (buffer, cluster_level);
215 hb_buffer_guess_segment_properties (buffer);
216 }
217
populate_buffershape_options_t218 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
219 const char *text_before, const char *text_after)
220 {
221 hb_buffer_clear_contents (buffer);
222 if (text_before) {
223 unsigned int len = strlen (text_before);
224 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
225 }
226 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
227 if (text_after) {
228 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
229 }
230
231 if (!utf8_clusters) {
232 /* Reset cluster values to refer to Unicode character index
233 * instead of UTF-8 index. */
234 unsigned int num_glyphs = hb_buffer_get_length (buffer);
235 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
236 for (unsigned int i = 0; i < num_glyphs; i++)
237 {
238 info->cluster = i;
239 info++;
240 }
241 }
242
243 setup_buffer (buffer);
244 }
245
shapeshape_options_t246 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
247 {
248 hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
249 if (normalize_glyphs)
250 hb_buffer_normalize_glyphs (buffer);
251 return res;
252 }
253
shape_closureshape_options_t254 void shape_closure (const char *text, int text_len,
255 hb_font_t *font, hb_buffer_t *buffer,
256 hb_set_t *glyphs)
257 {
258 hb_buffer_reset (buffer);
259 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
260 setup_buffer (buffer);
261 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
262 }
263
264 /* Buffer properties */
265 char *direction;
266 char *language;
267 char *script;
268
269 /* Buffer flags */
270 hb_bool_t bot;
271 hb_bool_t eot;
272 hb_bool_t preserve_default_ignorables;
273
274 hb_feature_t *features;
275 unsigned int num_features;
276 char **shapers;
277 hb_bool_t utf8_clusters;
278 hb_buffer_cluster_level_t cluster_level;
279 hb_bool_t normalize_glyphs;
280 unsigned int num_iterations;
281 };
282
283
284 struct font_options_t : option_group_t
285 {
font_options_tfont_options_t286 font_options_t (option_parser_t *parser,
287 int default_font_size_,
288 unsigned int subpixel_bits_)
289 {
290 variations = NULL;
291 num_variations = 0;
292 default_font_size = default_font_size_;
293 subpixel_bits = subpixel_bits_;
294 font_file = NULL;
295 face_index = 0;
296 font_size_x = font_size_y = default_font_size;
297 font_funcs = NULL;
298
299 font = NULL;
300
301 add_options (parser);
302 }
~font_options_tfont_options_t303 ~font_options_t (void) {
304 g_free (font_file);
305 free (variations);
306 g_free (font_funcs);
307 hb_font_destroy (font);
308 }
309
310 void add_options (option_parser_t *parser);
311
312 hb_font_t *get_font (void) const;
313
314 char *font_file;
315 int face_index;
316 hb_variation_t *variations;
317 unsigned int num_variations;
318 int default_font_size;
319 unsigned int subpixel_bits;
320 mutable double font_size_x;
321 mutable double font_size_y;
322 char *font_funcs;
323
324 private:
325 mutable hb_font_t *font;
326 };
327
328
329 struct text_options_t : option_group_t
330 {
text_options_ttext_options_t331 text_options_t (option_parser_t *parser) {
332 text_before = NULL;
333 text_after = NULL;
334
335 text = NULL;
336 text_file = NULL;
337
338 fp = NULL;
339 gs = NULL;
340 line = NULL;
341 line_len = (unsigned int) -1;
342
343 add_options (parser);
344 }
~text_options_ttext_options_t345 ~text_options_t (void) {
346 g_free (text_before);
347 g_free (text_after);
348 g_free (text);
349 g_free (text_file);
350 if (gs)
351 g_string_free (gs, true);
352 if (fp)
353 fclose (fp);
354 }
355
356 void add_options (option_parser_t *parser);
357
post_parsetext_options_t358 void post_parse (GError **error G_GNUC_UNUSED) {
359 if (text && text_file)
360 g_set_error (error,
361 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
362 "Only one of text and text-file can be set");
363 };
364
365 const char *get_line (unsigned int *len);
366
367 char *text_before;
368 char *text_after;
369
370 char *text;
371 char *text_file;
372
373 private:
374 FILE *fp;
375 GString *gs;
376 char *line;
377 unsigned int line_len;
378 };
379
380 struct output_options_t : option_group_t
381 {
output_options_toutput_options_t382 output_options_t (option_parser_t *parser,
383 const char **supported_formats_ = NULL) {
384 output_file = NULL;
385 output_format = NULL;
386 supported_formats = supported_formats_;
387 explicit_output_format = false;
388
389 fp = NULL;
390
391 add_options (parser);
392 }
~output_options_toutput_options_t393 ~output_options_t (void) {
394 g_free (output_file);
395 g_free (output_format);
396 if (fp)
397 fclose (fp);
398 }
399
400 void add_options (option_parser_t *parser);
401
post_parseoutput_options_t402 void post_parse (GError **error G_GNUC_UNUSED)
403 {
404 if (output_format)
405 explicit_output_format = true;
406
407 if (output_file && !output_format) {
408 output_format = strrchr (output_file, '.');
409 if (output_format)
410 {
411 output_format++; /* skip the dot */
412 output_format = strdup (output_format);
413 }
414 }
415
416 if (output_file && 0 == strcmp (output_file, "-"))
417 output_file = NULL; /* STDOUT */
418 }
419
420 FILE *get_file_handle (void);
421
422 char *output_file;
423 char *output_format;
424 const char **supported_formats;
425 bool explicit_output_format;
426
427 mutable FILE *fp;
428 };
429
430 struct format_options_t : option_group_t
431 {
format_options_tformat_options_t432 format_options_t (option_parser_t *parser) {
433 show_glyph_names = true;
434 show_positions = true;
435 show_clusters = true;
436 show_text = false;
437 show_unicode = false;
438 show_line_num = false;
439 show_extents = false;
440
441 add_options (parser);
442 }
443
444 void add_options (option_parser_t *parser);
445
446 void serialize_unicode (hb_buffer_t *buffer,
447 GString *gs);
448 void serialize_glyphs (hb_buffer_t *buffer,
449 hb_font_t *font,
450 hb_buffer_serialize_format_t format,
451 hb_buffer_serialize_flags_t flags,
452 GString *gs);
453 void serialize_line_no (unsigned int line_no,
454 GString *gs);
455 void serialize_buffer_of_text (hb_buffer_t *buffer,
456 unsigned int line_no,
457 const char *text,
458 unsigned int text_len,
459 hb_font_t *font,
460 GString *gs);
461 void serialize_message (unsigned int line_no,
462 const char *msg,
463 GString *gs);
464 void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
465 unsigned int line_no,
466 const char *text,
467 unsigned int text_len,
468 hb_font_t *font,
469 hb_buffer_serialize_format_t output_format,
470 hb_buffer_serialize_flags_t format_flags,
471 GString *gs);
472
473
474 hb_bool_t show_glyph_names;
475 hb_bool_t show_positions;
476 hb_bool_t show_clusters;
477 hb_bool_t show_text;
478 hb_bool_t show_unicode;
479 hb_bool_t show_line_num;
480 hb_bool_t show_extents;
481 };
482
483 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
484 #if defined (_MSC_VER) && (_MSC_VER < 1800)
485
486 #ifndef FLT_RADIX
487 #define FLT_RADIX 2
488 #endif
489
scalbn(long double x,int exp)490 __inline long double scalbn (long double x, int exp)
491 {
492 return x * (pow ((long double) FLT_RADIX, exp));
493 }
494
scalbnf(float x,int exp)495 __inline float scalbnf (float x, int exp)
496 {
497 return x * (pow ((float) FLT_RADIX, exp));
498 }
499 #endif
500
501 #endif
502