options.hh 9.6 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 47 48 49 50
#ifdef HAVE_IO_H
#include <io.h> /* for _setmode() under Windows */
#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>

57 58 59 60 61 62 63
#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; }


B
Behdad Esfahbod 已提交
64 65 66
void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN;


B
Behdad Esfahbod 已提交
67
extern hb_bool_t debug;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

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);

    add_main_options ();
  }
  ~option_parser_t (void) {
    g_option_context_free (context);
  }

  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);

  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);
  }

  const char *usage_str;
  GOptionContext *context;
};


B
Behdad Esfahbod 已提交
111
#define DEFAULT_MARGIN 16
112 113
#define DEFAULT_FORE "#000000"
#define DEFAULT_BACK "#FFFFFF"
114
#define DEFAULT_FONT_SIZE 256
115

116
struct view_options_t : option_group_t
B
Behdad Esfahbod 已提交
117
{
118 119
  view_options_t (option_parser_t *parser) {
    annotate = false;
120 121
    fore = DEFAULT_FORE;
    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);
B
Behdad Esfahbod 已提交
127 128
  }

129
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
130

B
Behdad Esfahbod 已提交
131
  hb_bool_t annotate;
B
Behdad Esfahbod 已提交
132 133 134 135 136 137
  const char *fore;
  const char *back;
  double line_space;
  struct margin_t {
    double t, r, b, l;
  } margin;
138
  double font_size;
139
};
B
Behdad Esfahbod 已提交
140

141

142
struct shape_options_t : option_group_t
B
Behdad Esfahbod 已提交
143
{
B
Behdad Esfahbod 已提交
144 145
  shape_options_t (option_parser_t *parser)
  {
146
    direction = language = script = NULL;
147
    bot = eot = preserve_default_ignorables = false;
148 149 150
    features = NULL;
    num_features = 0;
    shapers = NULL;
B
Behdad Esfahbod 已提交
151
    utf8_clusters = false;
152
    normalize_glyphs = false;
153 154

    add_options (parser);
B
Behdad Esfahbod 已提交
155
  }
B
Behdad Esfahbod 已提交
156 157
  ~shape_options_t (void)
  {
B
Behdad Esfahbod 已提交
158
    free (features);
B
Behdad Esfahbod 已提交
159
    g_strfreev (shapers);
B
Behdad Esfahbod 已提交
160
  }
B
Behdad Esfahbod 已提交
161

162
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
163

B
Behdad Esfahbod 已提交
164 165
  void setup_buffer (hb_buffer_t *buffer)
  {
B
Behdad Esfahbod 已提交
166 167 168
    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));
169 170 171 172
    hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAGS_DEFAULT |
			 (bot ? HB_BUFFER_FLAG_BOT : 0) |
			 (eot ? HB_BUFFER_FLAG_EOT : 0) |
			 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0)));
B
Behdad Esfahbod 已提交
173 174
  }

175 176
  void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
			const char *text_before, const char *text_after)
B
Behdad Esfahbod 已提交
177
  {
178
    hb_buffer_clear_contents (buffer);
179 180 181 182
    if (text_before) {
      unsigned int len = strlen (text_before);
      hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
    }
183
    hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
184 185 186
    if (text_after) {
      hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
    }
187

B
Behdad Esfahbod 已提交
188 189 190 191 192 193 194 195 196 197
    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++;
      }
198 199
    }

B
Behdad Esfahbod 已提交
200
    setup_buffer (buffer);
B
Behdad Esfahbod 已提交
201 202 203 204
  }

  hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
  {
205 206 207 208
    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 已提交
209 210
  }

211 212
  void shape_closure (const char *text, int text_len,
		      hb_font_t *font, hb_buffer_t *buffer,
B
Behdad Esfahbod 已提交
213 214
		      hb_set_t *glyphs)
  {
215 216 217 218 219 220
    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);
  }

221
  /* Buffer properties */
B
Behdad Esfahbod 已提交
222 223 224
  const char *direction;
  const char *language;
  const char *script;
225 226 227 228 229 230

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

B
Behdad Esfahbod 已提交
231 232 233
  hb_feature_t *features;
  unsigned int num_features;
  char **shapers;
B
Behdad Esfahbod 已提交
234
  hb_bool_t utf8_clusters;
235
  hb_bool_t normalize_glyphs;
236
};
B
Behdad Esfahbod 已提交
237

238

239
struct font_options_t : option_group_t
B
Behdad Esfahbod 已提交
240
{
241 242 243 244 245 246 247 248 249 250
  font_options_t (option_parser_t *parser) {
    font_file = NULL;
    face_index = 0;

    font = NULL;

    add_options (parser);
  }
  ~font_options_t (void) {
    hb_font_destroy (font);
B
Behdad Esfahbod 已提交
251 252
  }

253 254 255
  void add_options (option_parser_t *parser);

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

B
Behdad Esfahbod 已提交
257 258 259
  const char *font_file;
  int face_index;

260 261 262 263 264 265 266 267
  private:
  mutable hb_font_t *font;
};


struct text_options_t : option_group_t
{
  text_options_t (option_parser_t *parser) {
268 269 270
    text_before = NULL;
    text_after = NULL;

271 272 273
    text = NULL;
    text_file = NULL;

B
Behdad Esfahbod 已提交
274 275
    fp = NULL;
    gs = NULL;
276 277 278 279 280
    text_len = (unsigned int) -1;

    add_options (parser);
  }
  ~text_options_t (void) {
B
Behdad Esfahbod 已提交
281
    if (gs)
282
      g_string_free (gs, true);
B
Behdad Esfahbod 已提交
283 284
    if (fp)
      fclose (fp);
285 286 287 288 289 290 291 292
  }

  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,
293
		   "Only one of text and text-file can be set");
294 295 296 297 298

  };

  const char *get_line (unsigned int *len);

299 300 301
  const char *text_before;
  const char *text_after;

302 303 304 305
  const char *text;
  const char *text_file;

  private:
B
Behdad Esfahbod 已提交
306 307 308
  FILE *fp;
  GString *gs;
  unsigned int text_len;
309 310 311 312
};

struct output_options_t : option_group_t
{
313 314
  output_options_t (option_parser_t *parser,
		    const char *supported_formats_ = NULL) {
315 316
    output_file = NULL;
    output_format = NULL;
317
    supported_formats = supported_formats_;
318
    explicit_output_format = false;
319

320 321
    fp = NULL;

322 323
    add_options (parser);
  }
324
  ~output_options_t (void) {
B
Behdad Esfahbod 已提交
325
    if (fp)
326 327
      fclose (fp);
  }
328 329 330 331 332

  void add_options (option_parser_t *parser);

  void post_parse (GError **error G_GNUC_UNUSED)
  {
333 334 335
    if (output_format)
      explicit_output_format = true;

336 337 338 339 340 341
    if (output_file && !output_format) {
      output_format = strrchr (output_file, '.');
      if (output_format)
	  output_format++; /* skip the dot */
    }

342 343 344 345
    if (output_file && 0 == strcmp (output_file, "-"))
      output_file = NULL; /* STDOUT */
  }

B
Behdad Esfahbod 已提交
346
  FILE *get_file_handle (void);
B
Behdad Esfahbod 已提交
347

348 349
  const char *output_file;
  const char *output_format;
350
  const char *supported_formats;
351
  bool explicit_output_format;
352 353

  mutable FILE *fp;
354
};
B
Behdad Esfahbod 已提交
355

B
Behdad Esfahbod 已提交
356 357 358 359 360 361
struct format_options_t : option_group_t
{
  format_options_t (option_parser_t *parser) {
    show_glyph_names = true;
    show_positions = true;
    show_clusters = true;
362 363
    show_text = false;
    show_unicode = false;
B
Behdad Esfahbod 已提交
364
    show_line_num = false;
B
Behdad Esfahbod 已提交
365 366 367 368 369 370

    add_options (parser);
  }

  void add_options (option_parser_t *parser);

B
Behdad Esfahbod 已提交
371 372 373 374
  void serialize_unicode (hb_buffer_t  *buffer,
			  GString      *gs);
  void serialize_glyphs (hb_buffer_t  *buffer,
			 hb_font_t    *font,
375 376
			 hb_buffer_serialize_format_t format,
			 hb_buffer_serialize_flags_t flags,
B
Behdad Esfahbod 已提交
377 378 379
			 GString      *gs);
  void serialize_line_no (unsigned int  line_no,
			  GString      *gs);
380 381 382 383 384 385 386 387 388 389 390 391 392 393
  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,
394 395
				   hb_buffer_serialize_format_t output_format,
				   hb_buffer_serialize_flags_t format_flags,
396
				   GString      *gs);
B
Behdad Esfahbod 已提交
397

B
Behdad Esfahbod 已提交
398

B
Behdad Esfahbod 已提交
399 400 401
  hb_bool_t show_glyph_names;
  hb_bool_t show_positions;
  hb_bool_t show_clusters;
402 403
  hb_bool_t show_text;
  hb_bool_t show_unicode;
B
Behdad Esfahbod 已提交
404
  hb_bool_t show_line_num;
B
Behdad Esfahbod 已提交
405 406
};

B
Behdad Esfahbod 已提交
407 408

#endif