options.hh 12.0 KB
Newer Older
B
Behdad Esfahbod 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright © 2011  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifndef OPTIONS_HH
#define OPTIONS_HH


B
Behdad Esfahbod 已提交
31 32 33 34 35 36 37 38 39 40 41 42
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <locale.h>
#include <errno.h>
#include <fcntl.h>
43 44 45
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for isatty() */
#endif
B
Behdad Esfahbod 已提交
46
#if defined(_WIN32) || defined(__CYGWIN__)
47
#include <io.h> /* for setmode() under Windows */
B
Behdad Esfahbod 已提交
48 49 50
#endif

#include <hb.h>
51 52 53
#ifdef HAVE_OT
#include <hb-ot.h>
#endif
B
Behdad Esfahbod 已提交
54 55 56
#include <glib.h>
#include <glib/gprintf.h>

B
Behdad Esfahbod 已提交
57 58 59 60
#if !GLIB_CHECK_VERSION (2, 22, 0)
# define g_mapped_file_unref g_mapped_file_free
#endif

61 62 63 64 65 66 67 68 69

/* A few macros copied from hb-private.hh. */

#if __GNUC__ >= 4
#define HB_UNUSED	__attribute__((unused))
#else
#define HB_UNUSED
#endif

70 71 72 73 74 75
#undef MIN
template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }

#undef MAX
template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }

76 77 78 79 80 81 82 83 84 85
#undef  ARRAY_LENGTH
template <typename Type, unsigned int n>
static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
/* A const version, but does not detect erratically being called on pointers. */
#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))

#define _ASSERT_STATIC1(_line, _cond)	HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
#define _ASSERT_STATIC0(_line, _cond)	_ASSERT_STATIC1 (_line, (_cond))
#define ASSERT_STATIC(_cond)		_ASSERT_STATIC0 (__LINE__, (_cond))

86

B
Behdad Esfahbod 已提交
87
void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
B
Behdad Esfahbod 已提交
88 89


B
Behdad Esfahbod 已提交
90
extern hb_bool_t debug;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

struct option_group_t
{
  virtual void add_options (struct option_parser_t *parser) = 0;

  virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
  virtual void post_parse (GError **error G_GNUC_UNUSED) {};
};


struct option_parser_t
{
  option_parser_t (const char *usage) {
    memset (this, 0, sizeof (*this));
    usage_str = usage;
    context = g_option_context_new (usage);
B
Behdad Esfahbod 已提交
107
    to_free = g_ptr_array_new ();
108 109 110 111 112

    add_main_options ();
  }
  ~option_parser_t (void) {
    g_option_context_free (context);
B
Behdad Esfahbod 已提交
113 114
    g_ptr_array_foreach (to_free, (GFunc) g_free, NULL);
    g_ptr_array_free (to_free, TRUE);
115 116 117 118 119 120 121 122 123 124
  }

  void add_main_options (void);

  void add_group (GOptionEntry   *entries,
		  const gchar    *name,
		  const gchar    *description,
		  const gchar    *help_description,
		  option_group_t *option_group);

B
Behdad Esfahbod 已提交
125 126 127 128
  void free_later (char *p) {
    g_ptr_array_add (to_free, p);
  }

129 130 131 132 133 134 135
  void parse (int *argc, char ***argv);

  G_GNUC_NORETURN void usage (void) {
    g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
    exit (1);
  }

B
Behdad Esfahbod 已提交
136
  private:
137 138
  const char *usage_str;
  GOptionContext *context;
B
Behdad Esfahbod 已提交
139
  GPtrArray *to_free;
140 141 142
};


B
Behdad Esfahbod 已提交
143
#define DEFAULT_MARGIN 16
144 145
#define DEFAULT_FORE "#000000"
#define DEFAULT_BACK "#FFFFFF"
146 147
#define FONT_SIZE_UPEM 0x7FFFFFFF
#define FONT_SIZE_NONE 0
148

149
struct view_options_t : option_group_t
B
Behdad Esfahbod 已提交
150
{
151 152
  view_options_t (option_parser_t *parser) {
    annotate = false;
B
Behdad Esfahbod 已提交
153 154
    fore = NULL;
    back = NULL;
155
    line_space = 0;
156
    margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
157 158

    add_options (parser);
B
Behdad Esfahbod 已提交
159
  }
B
Behdad Esfahbod 已提交
160 161 162 163 164
  ~view_options_t (void)
  {
    g_free (fore);
    g_free (back);
  }
B
Behdad Esfahbod 已提交
165

166
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
167

B
Behdad Esfahbod 已提交
168
  hb_bool_t annotate;
B
Behdad Esfahbod 已提交
169 170
  char *fore;
  char *back;
B
Behdad Esfahbod 已提交
171 172 173 174
  double line_space;
  struct margin_t {
    double t, r, b, l;
  } margin;
175
};
B
Behdad Esfahbod 已提交
176

177

178
struct shape_options_t : option_group_t
B
Behdad Esfahbod 已提交
179
{
B
Behdad Esfahbod 已提交
180 181
  shape_options_t (option_parser_t *parser)
  {
182
    direction = language = script = NULL;
183
    bot = eot = preserve_default_ignorables = false;
184 185 186
    features = NULL;
    num_features = 0;
    shapers = NULL;
B
Behdad Esfahbod 已提交
187
    utf8_clusters = false;
188
    cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
189
    normalize_glyphs = false;
B
Behdad Esfahbod 已提交
190
    num_iterations = 1;
191 192

    add_options (parser);
B
Behdad Esfahbod 已提交
193
  }
B
Behdad Esfahbod 已提交
194 195
  ~shape_options_t (void)
  {
B
Behdad Esfahbod 已提交
196 197 198
    g_free (direction);
    g_free (language);
    g_free (script);
B
Behdad Esfahbod 已提交
199
    free (features);
B
Behdad Esfahbod 已提交
200
    g_strfreev (shapers);
B
Behdad Esfahbod 已提交
201
  }
B
Behdad Esfahbod 已提交
202

203
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
204

B
Behdad Esfahbod 已提交
205 206
  void setup_buffer (hb_buffer_t *buffer)
  {
B
Behdad Esfahbod 已提交
207 208 209
    hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
    hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
    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 212 213
			 (bot ? HB_BUFFER_FLAG_BOT : 0) |
			 (eot ? HB_BUFFER_FLAG_EOT : 0) |
			 (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);
B
Behdad Esfahbod 已提交
216 217
  }

218 219
  void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
			const char *text_before, const char *text_after)
B
Behdad Esfahbod 已提交
220
  {
221
    hb_buffer_clear_contents (buffer);
222 223 224 225
    if (text_before) {
      unsigned int len = strlen (text_before);
      hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
    }
226
    hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
227 228 229
    if (text_after) {
      hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
    }
230

B
Behdad Esfahbod 已提交
231 232 233 234 235 236 237 238 239 240
    if (!utf8_clusters) {
      /* Reset cluster values to refer to Unicode character index
       * instead of UTF-8 index. */
      unsigned int num_glyphs = hb_buffer_get_length (buffer);
      hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
      for (unsigned int i = 0; i < num_glyphs; i++)
      {
	info->cluster = i;
	info++;
      }
241 242
    }

B
Behdad Esfahbod 已提交
243
    setup_buffer (buffer);
B
Behdad Esfahbod 已提交
244 245 246 247
  }

  hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
  {
248 249 250 251
    hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
    if (normalize_glyphs)
      hb_buffer_normalize_glyphs (buffer);
    return res;
B
Behdad Esfahbod 已提交
252 253
  }

254 255
  void shape_closure (const char *text, int text_len,
		      hb_font_t *font, hb_buffer_t *buffer,
B
Behdad Esfahbod 已提交
256 257
		      hb_set_t *glyphs)
  {
258 259 260 261 262 263
    hb_buffer_reset (buffer);
    hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
    setup_buffer (buffer);
    hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
  }

264
  /* Buffer properties */
B
Behdad Esfahbod 已提交
265 266 267
  char *direction;
  char *language;
  char *script;
268 269 270 271 272 273

  /* Buffer flags */
  hb_bool_t bot;
  hb_bool_t eot;
  hb_bool_t preserve_default_ignorables;

B
Behdad Esfahbod 已提交
274 275 276
  hb_feature_t *features;
  unsigned int num_features;
  char **shapers;
B
Behdad Esfahbod 已提交
277
  hb_bool_t utf8_clusters;
278
  hb_buffer_cluster_level_t cluster_level;
279
  hb_bool_t normalize_glyphs;
B
Behdad Esfahbod 已提交
280
  unsigned int num_iterations;
281
};
B
Behdad Esfahbod 已提交
282

283

284
struct font_options_t : option_group_t
B
Behdad Esfahbod 已提交
285
{
286 287 288 289 290
  font_options_t (option_parser_t *parser,
		  int default_font_size_,
		  unsigned int subpixel_bits_) {
    default_font_size = default_font_size_;
    subpixel_bits = subpixel_bits_;
291 292
    font_file = NULL;
    face_index = 0;
293
    font_size_x = font_size_y = default_font_size;
294
    font_funcs = NULL;
295 296 297 298 299 300

    font = NULL;

    add_options (parser);
  }
  ~font_options_t (void) {
B
Behdad Esfahbod 已提交
301 302
    g_free (font_file);
    g_free (font_funcs);
303
    hb_font_destroy (font);
B
Behdad Esfahbod 已提交
304 305
  }

306 307 308
  void add_options (option_parser_t *parser);

  hb_font_t *get_font (void) const;
B
Behdad Esfahbod 已提交
309

B
Behdad Esfahbod 已提交
310
  char *font_file;
B
Behdad Esfahbod 已提交
311
  int face_index;
312 313 314 315
  int default_font_size;
  unsigned int subpixel_bits;
  mutable double font_size_x;
  mutable double font_size_y;
B
Behdad Esfahbod 已提交
316
  char *font_funcs;
B
Behdad Esfahbod 已提交
317

318 319 320 321 322 323 324 325
  private:
  mutable hb_font_t *font;
};


struct text_options_t : option_group_t
{
  text_options_t (option_parser_t *parser) {
326 327 328
    text_before = NULL;
    text_after = NULL;

329 330 331
    text = NULL;
    text_file = NULL;

B
Behdad Esfahbod 已提交
332 333
    fp = NULL;
    gs = NULL;
B
Behdad Esfahbod 已提交
334 335
    line = NULL;
    line_len = (unsigned int) -1;
336 337 338 339

    add_options (parser);
  }
  ~text_options_t (void) {
B
Behdad Esfahbod 已提交
340 341 342 343
    g_free (text_before);
    g_free (text_after);
    g_free (text);
    g_free (text_file);
B
Behdad Esfahbod 已提交
344
    if (gs)
345
      g_string_free (gs, true);
B
Behdad Esfahbod 已提交
346 347
    if (fp)
      fclose (fp);
348 349 350 351 352 353 354 355
  }

  void add_options (option_parser_t *parser);

  void post_parse (GError **error G_GNUC_UNUSED) {
    if (text && text_file)
      g_set_error (error,
		   G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
356
		   "Only one of text and text-file can be set");
357 358 359 360
  };

  const char *get_line (unsigned int *len);

B
Behdad Esfahbod 已提交
361 362
  char *text_before;
  char *text_after;
363

B
Behdad Esfahbod 已提交
364 365
  char *text;
  char *text_file;
366 367

  private:
B
Behdad Esfahbod 已提交
368 369
  FILE *fp;
  GString *gs;
B
Behdad Esfahbod 已提交
370 371
  char *line;
  unsigned int line_len;
372 373 374 375
};

struct output_options_t : option_group_t
{
376
  output_options_t (option_parser_t *parser,
B
Behdad Esfahbod 已提交
377
		    const char **supported_formats_ = NULL) {
378 379
    output_file = NULL;
    output_format = NULL;
380
    supported_formats = supported_formats_;
381
    explicit_output_format = false;
382

383 384
    fp = NULL;

385 386
    add_options (parser);
  }
387
  ~output_options_t (void) {
B
Behdad Esfahbod 已提交
388 389
    g_free (output_file);
    g_free (output_format);
B
Behdad Esfahbod 已提交
390
    if (fp)
391 392
      fclose (fp);
  }
393 394 395 396 397

  void add_options (option_parser_t *parser);

  void post_parse (GError **error G_GNUC_UNUSED)
  {
398 399 400
    if (output_format)
      explicit_output_format = true;

401 402 403
    if (output_file && !output_format) {
      output_format = strrchr (output_file, '.');
      if (output_format)
404
      {
405
	  output_format++; /* skip the dot */
406 407
	  output_format = strdup (output_format);
      }
408 409
    }

410 411 412 413
    if (output_file && 0 == strcmp (output_file, "-"))
      output_file = NULL; /* STDOUT */
  }

B
Behdad Esfahbod 已提交
414
  FILE *get_file_handle (void);
B
Behdad Esfahbod 已提交
415

B
Behdad Esfahbod 已提交
416 417
  char *output_file;
  char *output_format;
B
Behdad Esfahbod 已提交
418
  const char **supported_formats;
419
  bool explicit_output_format;
420 421

  mutable FILE *fp;
422
};
B
Behdad Esfahbod 已提交
423

B
Behdad Esfahbod 已提交
424 425 426 427 428 429
struct format_options_t : option_group_t
{
  format_options_t (option_parser_t *parser) {
    show_glyph_names = true;
    show_positions = true;
    show_clusters = true;
430 431
    show_text = false;
    show_unicode = false;
B
Behdad Esfahbod 已提交
432
    show_line_num = false;
433
    show_extents = false;
B
Behdad Esfahbod 已提交
434 435 436 437 438 439

    add_options (parser);
  }

  void add_options (option_parser_t *parser);

B
Behdad Esfahbod 已提交
440 441 442 443
  void serialize_unicode (hb_buffer_t  *buffer,
			  GString      *gs);
  void serialize_glyphs (hb_buffer_t  *buffer,
			 hb_font_t    *font,
444 445
			 hb_buffer_serialize_format_t format,
			 hb_buffer_serialize_flags_t flags,
B
Behdad Esfahbod 已提交
446 447 448
			 GString      *gs);
  void serialize_line_no (unsigned int  line_no,
			  GString      *gs);
449 450 451 452 453 454 455 456 457 458 459 460 461 462
  void serialize_buffer_of_text (hb_buffer_t  *buffer,
				 unsigned int  line_no,
				 const char   *text,
				 unsigned int  text_len,
				 hb_font_t    *font,
				 GString      *gs);
  void serialize_message (unsigned int  line_no,
			  const char   *msg,
			  GString      *gs);
  void serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
				   unsigned int  line_no,
				   const char   *text,
				   unsigned int  text_len,
				   hb_font_t    *font,
463 464
				   hb_buffer_serialize_format_t output_format,
				   hb_buffer_serialize_flags_t format_flags,
465
				   GString      *gs);
B
Behdad Esfahbod 已提交
466

B
Behdad Esfahbod 已提交
467

B
Behdad Esfahbod 已提交
468 469 470
  hb_bool_t show_glyph_names;
  hb_bool_t show_positions;
  hb_bool_t show_clusters;
471 472
  hb_bool_t show_text;
  hb_bool_t show_unicode;
B
Behdad Esfahbod 已提交
473
  hb_bool_t show_line_num;
474
  hb_bool_t show_extents;
B
Behdad Esfahbod 已提交
475 476
};

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
#if defined (_MSC_VER) && (_MSC_VER < 1800)

#ifndef FLT_RADIX
#define FLT_RADIX 2
#endif

__inline long double scalbn (long double x, int exp)
{
  return x * (pow ((long double) FLT_RADIX, exp));
}

__inline float scalbnf (float x, int exp)
{
  return x * (pow ((float) FLT_RADIX, exp));
}
#endif
B
Behdad Esfahbod 已提交
494 495

#endif