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

#include <hb.h>
#include <glib.h>
#include <glib/gprintf.h>

51 52 53 54 55 56 57
#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 已提交
58 59 60
void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN;


B
Behdad Esfahbod 已提交
61
extern hb_bool_t debug;
62 63 64 65 66 67 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

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


105 106 107
#define DEFAULT_MARGIN 18
#define DEFAULT_FORE "#000000"
#define DEFAULT_BACK "#FFFFFF"
108
#define DEFAULT_FONT_SIZE 36
109

110
struct view_options_t : option_group_t
B
Behdad Esfahbod 已提交
111
{
112 113
  view_options_t (option_parser_t *parser) {
    annotate = false;
114 115
    fore = DEFAULT_FORE;
    back = DEFAULT_BACK;
116
    line_space = 0;
117
    margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
118
    font_size = DEFAULT_FONT_SIZE;
119 120

    add_options (parser);
B
Behdad Esfahbod 已提交
121 122
  }

123
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
124

B
Behdad Esfahbod 已提交
125
  hb_bool_t annotate;
B
Behdad Esfahbod 已提交
126 127 128 129 130 131
  const char *fore;
  const char *back;
  double line_space;
  struct margin_t {
    double t, r, b, l;
  } margin;
132
  double font_size;
133
};
B
Behdad Esfahbod 已提交
134

135

136
struct shape_options_t : option_group_t
B
Behdad Esfahbod 已提交
137
{
138 139 140 141 142 143 144
  shape_options_t (option_parser_t *parser) {
    direction = language = script = NULL;
    features = NULL;
    num_features = 0;
    shapers = NULL;

    add_options (parser);
B
Behdad Esfahbod 已提交
145
  }
B
Behdad Esfahbod 已提交
146 147 148 149
  ~shape_options_t (void) {
    free (features);
    g_free (shapers);
  }
B
Behdad Esfahbod 已提交
150

151
  void add_options (option_parser_t *parser);
B
Behdad Esfahbod 已提交
152

B
Behdad Esfahbod 已提交
153
  void setup_buffer (hb_buffer_t *buffer) {
B
Behdad Esfahbod 已提交
154 155 156
    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));
B
Behdad Esfahbod 已提交
157 158
  }

B
Behdad Esfahbod 已提交
159 160
  hb_bool_t shape (const char *text, int text_len,
		   hb_font_t *font, hb_buffer_t *buffer) {
161 162
    hb_buffer_reset (buffer);
    hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
163 164 165 166 167 168 169 170 171 172 173 174

    /* Reset cluster values to refer to Unicode character index
     * instead of UTF-8 index.
     * TODO: Add an option for this. */
    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++;
    }

B
Behdad Esfahbod 已提交
175
    setup_buffer (buffer);
176
    return hb_shape_full (font, buffer, features, num_features, shapers);
B
Behdad Esfahbod 已提交
177 178
  }

B
Behdad Esfahbod 已提交
179 180 181 182 183 184
  const char *direction;
  const char *language;
  const char *script;
  hb_feature_t *features;
  unsigned int num_features;
  char **shapers;
185
};
B
Behdad Esfahbod 已提交
186

187

188
struct font_options_t : option_group_t
B
Behdad Esfahbod 已提交
189
{
190 191 192 193 194 195 196 197 198 199
  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 已提交
200 201
  }

202 203 204
  void add_options (option_parser_t *parser);

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

B
Behdad Esfahbod 已提交
206 207 208
  const char *font_file;
  int face_index;

209 210 211 212 213 214 215 216 217 218 219
  private:
  mutable hb_font_t *font;
};


struct text_options_t : option_group_t
{
  text_options_t (option_parser_t *parser) {
    text = NULL;
    text_file = NULL;

B
Behdad Esfahbod 已提交
220 221
    fp = NULL;
    gs = NULL;
222 223 224 225 226
    text_len = (unsigned int) -1;

    add_options (parser);
  }
  ~text_options_t (void) {
B
Behdad Esfahbod 已提交
227 228 229 230
    if (gs)
      g_string_free (gs, TRUE);
    if (fp)
      fclose (fp);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  }

  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,
		   "Only one of text and text-file must be set");

  };

  const char *get_line (unsigned int *len);

  const char *text;
  const char *text_file;

  private:
B
Behdad Esfahbod 已提交
249 250 251
  FILE *fp;
  GString *gs;
  unsigned int text_len;
252 253 254 255 256 257 258 259
};

struct output_options_t : option_group_t
{
  output_options_t (option_parser_t *parser) {
    output_file = NULL;
    output_format = NULL;

260 261
    fp = NULL;

262 263
    add_options (parser);
  }
264
  ~output_options_t (void) {
B
Behdad Esfahbod 已提交
265
    if (fp)
266 267
      fclose (fp);
  }
268 269 270 271 272 273 274 275 276 277 278

  void add_options (option_parser_t *parser);

  void post_parse (GError **error G_GNUC_UNUSED)
  {
    if (output_file && !output_format) {
      output_format = strrchr (output_file, '.');
      if (output_format)
	  output_format++; /* skip the dot */
    }

279 280 281 282
    if (output_file && 0 == strcmp (output_file, "-"))
      output_file = NULL; /* STDOUT */
  }

B
Behdad Esfahbod 已提交
283
  FILE *get_file_handle (void);
B
Behdad Esfahbod 已提交
284

285 286 287 288 289
  virtual void init (const font_options_t *font_opts) = 0;
  virtual void consume_line (hb_buffer_t  *buffer,
			     const char   *text,
			     unsigned int  text_len) = 0;
  virtual void finish (const font_options_t *font_opts) = 0;
B
Behdad Esfahbod 已提交
290

291 292
  const char *output_file;
  const char *output_format;
293

B
Behdad Esfahbod 已提交
294 295
  protected:

296
  mutable FILE *fp;
297
};
B
Behdad Esfahbod 已提交
298

B
Behdad Esfahbod 已提交
299 300 301 302 303 304
struct format_options_t : option_group_t
{
  format_options_t (option_parser_t *parser) {
    show_glyph_names = true;
    show_positions = true;
    show_clusters = true;
305 306
    show_text = false;
    show_unicode = false;
B
Behdad Esfahbod 已提交
307 308
    show_line_num = false;
    scratch = hb_buffer_create ();
B
Behdad Esfahbod 已提交
309 310 311 312

    add_options (parser);
  }
  ~format_options_t (void) {
B
Behdad Esfahbod 已提交
313
    hb_buffer_destroy (scratch);
B
Behdad Esfahbod 已提交
314 315 316 317
  }

  void add_options (option_parser_t *parser);

B
Behdad Esfahbod 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331
  void serialize_unicode (hb_buffer_t  *buffer,
			  GString      *gs);
  void serialize_glyphs (hb_buffer_t  *buffer,
			 hb_font_t    *font,
			 GString      *gs);
  void serialize_line_no (unsigned int  line_no,
			  GString      *gs);
  void serialize_line (hb_buffer_t  *buffer,
		       unsigned int  line_no,
		       const char   *text,
		       unsigned int  text_len,
		       hb_font_t    *font,
		       GString      *gs);

B
Behdad Esfahbod 已提交
332 333

  protected:
B
Behdad Esfahbod 已提交
334 335 336
  hb_bool_t show_glyph_names;
  hb_bool_t show_positions;
  hb_bool_t show_clusters;
337 338
  hb_bool_t show_text;
  hb_bool_t show_unicode;
B
Behdad Esfahbod 已提交
339 340 341
  hb_bool_t show_line_num;
  private:
  hb_buffer_t *scratch;
B
Behdad Esfahbod 已提交
342 343
};

B
Behdad Esfahbod 已提交
344 345

#endif