options.cc 22.9 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
2
 * Copyright © 2011,2012  Google, Inc.
B
Behdad Esfahbod 已提交
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
 *
 *  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
 */

#include "options.hh"

B
Minor  
Behdad Esfahbod 已提交
29
#ifdef HAVE_FREETYPE
30
#include <hb-ft.h>
31 32
#endif
#ifdef HAVE_OT
33
#include <hb-ot-font.h>
34
#endif
B
Behdad Esfahbod 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48
struct supported_font_funcs_t {
	char name[4];
	void (*func) (hb_font_t *);
} supported_font_funcs[] =
{
#ifdef HAVE_FREETYPE
  {"ft",	hb_ft_font_set_funcs},
#endif
#ifdef HAVE_OT
  {"ot",	hb_ot_font_set_funcs},
#endif
};

B
Behdad Esfahbod 已提交
49

B
Behdad Esfahbod 已提交
50 51 52 53 54 55 56 57
void
fail (hb_bool_t suggest_help, const char *format, ...)
{
  const char *msg;

  va_list vap;
  va_start (vap, format);
  msg = g_strdup_vprintf (format, vap);
B
Behdad Esfahbod 已提交
58
  va_end (vap);
B
Behdad Esfahbod 已提交
59 60 61 62 63 64 65 66 67
  const char *prgname = g_get_prgname ();
  g_printerr ("%s: %s\n", prgname, msg);
  if (suggest_help)
    g_printerr ("Try `%s --help' for more information.\n", prgname);

  exit (1);
}


68
hb_bool_t debug = false;
69 70 71 72 73 74 75 76 77 78 79 80 81

static gchar *
shapers_to_string (void)
{
  GString *shapers = g_string_new (NULL);
  const char **shaper_list = hb_shape_list_shapers ();

  for (; *shaper_list; shaper_list++) {
    g_string_append (shapers, *shaper_list);
    g_string_append_c (shapers, ',');
  }
  g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));

82
  return g_string_free (shapers, false);
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

static G_GNUC_NORETURN gboolean
show_version (const char *name G_GNUC_UNUSED,
	      const char *arg G_GNUC_UNUSED,
	      gpointer    data G_GNUC_UNUSED,
	      GError    **error G_GNUC_UNUSED)
{
  g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);

  char *shapers = shapers_to_string ();
  g_printf ("Available shapers: %s\n", shapers);
  g_free (shapers);
  if (strcmp (HB_VERSION_STRING, hb_version_string ()))
    g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());

  exit(0);
}


void
option_parser_t::add_main_options (void)
{
  GOptionEntry entries[] =
  {
    {"version",		0, G_OPTION_FLAG_NO_ARG,
			      G_OPTION_ARG_CALLBACK,	(gpointer) &show_version,	"Show version numbers",			NULL},
    {"debug",		0, 0, G_OPTION_ARG_NONE,	&debug,				"Free all resources before exit",	NULL},
    {NULL}
  };
  g_option_context_add_main_entries (context, entries, NULL);
}

static gboolean
pre_parse (GOptionContext *context G_GNUC_UNUSED,
	   GOptionGroup *group G_GNUC_UNUSED,
	   gpointer data,
	   GError **error)
{
  option_group_t *option_group = (option_group_t *) data;
  option_group->pre_parse (error);
  return *error == NULL;
}

static gboolean
post_parse (GOptionContext *context G_GNUC_UNUSED,
	    GOptionGroup *group G_GNUC_UNUSED,
	    gpointer data,
	    GError **error)
{
  option_group_t *option_group = static_cast<option_group_t *>(data);
  option_group->post_parse (error);
  return *error == NULL;
}

void
option_parser_t::add_group (GOptionEntry   *entries,
			    const gchar    *name,
			    const gchar    *description,
			    const gchar    *help_description,
			    option_group_t *option_group)
{
  GOptionGroup *group = g_option_group_new (name, description, help_description,
					    static_cast<gpointer>(option_group), NULL);
  g_option_group_add_entries (group, entries);
  g_option_group_set_parse_hooks (group, pre_parse, post_parse);
  g_option_context_add_group (context, group);
}

void
option_parser_t::parse (int *argc, char ***argv)
{
B
Behdad Esfahbod 已提交
155 156
  setlocale (LC_ALL, "");

157 158 159
  GError *parse_error = NULL;
  if (!g_option_context_parse (context, argc, argv, &parse_error))
  {
B
Behdad Esfahbod 已提交
160
    if (parse_error != NULL) {
161
      fail (true, "%s", parse_error->message);
B
Behdad Esfahbod 已提交
162 163
      //g_error_free (parse_error);
    } else
164
      fail (true, "Option parse error");
165 166
  }
}
B
Behdad Esfahbod 已提交
167 168 169 170 171


static gboolean
parse_margin (const char *name G_GNUC_UNUSED,
	      const char *arg,
B
Behdad Esfahbod 已提交
172
	      gpointer    data,
B
Behdad Esfahbod 已提交
173 174
	      GError    **error G_GNUC_UNUSED)
{
B
Behdad Esfahbod 已提交
175
  view_options_t *view_opts = (view_options_t *) data;
B
Behdad Esfahbod 已提交
176
  view_options_t::margin_t &m = view_opts->margin;
177
  switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
B
Behdad Esfahbod 已提交
178 179 180
    case 1: m.r = m.t;
    case 2: m.b = m.t;
    case 3: m.l = m.r;
181
    case 4: return true;
B
Behdad Esfahbod 已提交
182 183 184 185
    default:
      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
		   "%s argument should be one to four space-separated numbers",
		   name);
186
      return false;
B
Behdad Esfahbod 已提交
187 188 189 190 191 192 193
  }
}


static gboolean
parse_shapers (const char *name G_GNUC_UNUSED,
	       const char *arg,
B
Behdad Esfahbod 已提交
194
	       gpointer    data,
B
Behdad Esfahbod 已提交
195 196
	       GError    **error G_GNUC_UNUSED)
{
B
Behdad Esfahbod 已提交
197
  shape_options_t *shape_opts = (shape_options_t *) data;
B
Behdad Esfahbod 已提交
198
  g_strfreev (shape_opts->shapers);
B
Behdad Esfahbod 已提交
199
  shape_opts->shapers = g_strsplit (arg, ",", 0);
200
  return true;
B
Behdad Esfahbod 已提交
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215
static G_GNUC_NORETURN gboolean
list_shapers (const char *name G_GNUC_UNUSED,
	      const char *arg G_GNUC_UNUSED,
	      gpointer    data G_GNUC_UNUSED,
	      GError    **error G_GNUC_UNUSED)
{
  for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
    g_printf ("%s\n", *shaper);

  exit(0);
}


B
Behdad Esfahbod 已提交
216 217 218
static gboolean
parse_features (const char *name G_GNUC_UNUSED,
	        const char *arg,
B
Behdad Esfahbod 已提交
219
	        gpointer    data,
B
Behdad Esfahbod 已提交
220 221
	        GError    **error G_GNUC_UNUSED)
{
B
Behdad Esfahbod 已提交
222
  shape_options_t *shape_opts = (shape_options_t *) data;
B
Behdad Esfahbod 已提交
223 224 225 226
  char *s = (char *) arg;
  char *p;

  shape_opts->num_features = 0;
227
  g_free (shape_opts->features);
B
Behdad Esfahbod 已提交
228 229 230
  shape_opts->features = NULL;

  if (!*s)
231
    return true;
B
Behdad Esfahbod 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

  /* count the features first, so we can allocate memory */
  p = s;
  do {
    shape_opts->num_features++;
    p = strchr (p, ',');
    if (p)
      p++;
  } while (p);

  shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));

  /* now do the actual parsing */
  p = s;
  shape_opts->num_features = 0;
247 248 249
  while (p && *p) {
    char *end = strchr (p, ',');
    if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
B
Behdad Esfahbod 已提交
250
      shape_opts->num_features++;
251
    p = end ? end + 1 : NULL;
B
Behdad Esfahbod 已提交
252 253
  }

254
  return true;
B
Behdad Esfahbod 已提交
255 256 257 258
}


void
259
view_options_t::add_options (option_parser_t *parser)
B
Behdad Esfahbod 已提交
260 261 262
{
  GOptionEntry entries[] =
  {
B
Behdad Esfahbod 已提交
263
    {"annotate",	0, 0, G_OPTION_ARG_NONE,	&this->annotate,		"Annotate output rendering",				NULL},
B
Minor  
Behdad Esfahbod 已提交
264 265
    {"background",	0, 0, G_OPTION_ARG_STRING,	&this->back,			"Set background color (default: " DEFAULT_BACK ")",	"rrggbb/rrggbbaa"},
    {"foreground",	0, 0, G_OPTION_ARG_STRING,	&this->fore,			"Set foreground color (default: " DEFAULT_FORE ")",	"rrggbb/rrggbbaa"},
B
Behdad Esfahbod 已提交
266
    {"line-space",	0, 0, G_OPTION_ARG_DOUBLE,	&this->line_space,		"Set space between lines (default: 0)",			"units"},
B
Behdad Esfahbod 已提交
267 268
    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_margin,	"Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
    {"font-size",	0, 0, G_OPTION_ARG_DOUBLE,	&this->font_size,		"Font size (default: " G_STRINGIFY(DEFAULT_FONT_SIZE) ")","size"},
269 270
    {NULL}
  };
271 272 273
  parser->add_group (entries,
		     "view",
		     "View options:",
274
		     "Options controlling output rendering",
275
		     this);
276
}
B
Behdad Esfahbod 已提交
277

278
void
279
shape_options_t::add_options (option_parser_t *parser)
280 281 282
{
  GOptionEntry entries[] =
  {
283 284
    {"list-shapers",	0, G_OPTION_FLAG_NO_ARG,
			      G_OPTION_ARG_CALLBACK,	(gpointer) &list_shapers,	"List available shapers and quit",	NULL},
285 286
    {"shaper",		0, G_OPTION_FLAG_HIDDEN,
			      G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Hidden duplicate of --shapers",	NULL},
287
    {"shapers",		0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_shapers,	"Set comma-separated list of shapers to try","list"},
B
Behdad Esfahbod 已提交
288 289 290
    {"direction",	0, 0, G_OPTION_ARG_STRING,	&this->direction,		"Set text direction (default: auto)",	"ltr/rtl/ttb/btt"},
    {"language",	0, 0, G_OPTION_ARG_STRING,	&this->language,		"Set text language (default: $LANG)",	"langstr"},
    {"script",		0, 0, G_OPTION_ARG_STRING,	&this->script,			"Set text script (default: auto)",	"ISO-15924 tag"},
291 292 293
    {"bot",		0, 0, G_OPTION_ARG_NONE,	&this->bot,			"Treat text as beginning-of-paragraph",	NULL},
    {"eot",		0, 0, G_OPTION_ARG_NONE,	&this->eot,			"Treat text as end-of-paragraph",	NULL},
    {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE,	&this->preserve_default_ignorables,	"Preserve Default-Ignorable characters",	NULL},
294
    {"utf8-clusters",	0, 0, G_OPTION_ARG_NONE,	&this->utf8_clusters,		"Use UTF8 byte indices, not char indices",	NULL},
295
    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,	&this->normalize_glyphs,	"Rearrange glyph clusters in nominal order",	NULL},
B
Behdad Esfahbod 已提交
296
    {"num-iterations",	0, 0, G_OPTION_ARG_INT,		&this->num_iterations,		"Run shaper N times (default: 1)",	"N"},
297 298
    {NULL}
  };
299 300 301 302 303
  parser->add_group (entries,
		     "shape",
		     "Shape options:",
		     "Options controlling the shaping process",
		     this);
B
Behdad Esfahbod 已提交
304

305
  const gchar *features_help = "Comma-separated list of font features\n"
B
Behdad Esfahbod 已提交
306 307
    "\n"
    "    Features can be enabled or disabled, either globally or limited to\n"
308 309 310
    "    specific character ranges.  The format for specifying feature settings\n"
    "    follows.  All valid CSS font-feature-settings values other than 'normal'\n"
    "    and 'inherited' are also accepted, though, not documented below.\n"
B
Behdad Esfahbod 已提交
311 312 313 314 315
    "\n"
    "    The range indices refer to the positions between Unicode characters,\n"
    "    unless the --utf8-clusters is provided, in which case range indices\n"
    "    refer to UTF-8 byte indices. The position before the first character\n"
    "    is always 0.\n"
316 317
    "\n"
    "    The format is Python-esque.  Here is how it all works:\n"
B
Behdad Esfahbod 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    "\n"
    "      Syntax:       Value:    Start:    End:\n"
    "\n"
    "    Setting value:\n"
    "      \"kern\"        1         0         ∞         # Turn feature on\n"
    "      \"+kern\"       1         0         ∞         # Turn feature on\n"
    "      \"-kern\"       0         0         ∞         # Turn feature off\n"
    "      \"kern=0\"      0         0         ∞         # Turn feature off\n"
    "      \"kern=1\"      1         0         ∞         # Turn feature on\n"
    "      \"aalt=2\"      2         0         ∞         # Choose 2nd alternate\n"
    "\n"
    "    Setting index:\n"
    "      \"kern[]\"      1         0         ∞         # Turn feature on\n"
    "      \"kern[:]\"     1         0         ∞         # Turn feature on\n"
    "      \"kern[5:]\"    1         5         ∞         # Turn feature on, partial\n"
    "      \"kern[:5]\"    1         0         5         # Turn feature on, partial\n"
    "      \"kern[3:5]\"   1         3         5         # Turn feature on, range\n"
    "      \"kern[3]\"     1         3         3+1       # Turn feature on, single char\n"
    "\n"
    "    Mixing it all:\n"
    "\n"
B
Minor  
Behdad Esfahbod 已提交
339
    "      \"aalt[3:5]=2\" 2         3         5         # Turn 2nd alternate on for range";
B
Behdad Esfahbod 已提交
340 341 342 343 344 345 346 347 348

  GOptionEntry entries2[] =
  {
    {"features",	0, 0, G_OPTION_ARG_CALLBACK,	(gpointer) &parse_features,	features_help,	"list"},
    {NULL}
  };
  parser->add_group (entries2,
		     "features",
		     "Features options:",
349
		     "Options controlling font features used",
B
Behdad Esfahbod 已提交
350
		     this);
351
}
B
Behdad Esfahbod 已提交
352

353
void
354
font_options_t::add_options (option_parser_t *parser)
355
{
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  char *text = NULL;

  {
    ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0);
    GString *s = g_string_new (NULL);
    g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n    Supported font function implementations are: %s",
		     supported_font_funcs[0].name,
		     supported_font_funcs[0].name);
    for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
    {
      g_string_append_c (s, '/');
      g_string_append (s, supported_font_funcs[i].name);
    }
    text = g_string_free (s, FALSE);
    parser->free_later (text);
  }

373 374
  GOptionEntry entries[] =
  {
375 376
    {"font-file",	0, 0, G_OPTION_ARG_STRING,	&this->font_file,		"Set font file-name",			"filename"},
    {"face-index",	0, 0, G_OPTION_ARG_INT,		&this->face_index,		"Set face index (default: 0)",		"index"},
B
Behdad Esfahbod 已提交
377
    {"font-funcs",	0, 0, G_OPTION_ARG_STRING,	&this->font_funcs,		text,					"impl"},
378 379
    {NULL}
  };
380 381 382 383 384
  parser->add_group (entries,
		     "font",
		     "Font options:",
		     "Options controlling the font",
		     this);
385
}
B
Behdad Esfahbod 已提交
386

387
void
388
text_options_t::add_options (option_parser_t *parser)
389 390 391
{
  GOptionEntry entries[] =
  {
392
    {"text",		0, 0, G_OPTION_ARG_STRING,	&this->text,			"Set input text",			"string"},
B
Minor  
Behdad Esfahbod 已提交
393
    {"text-file",	0, 0, G_OPTION_ARG_STRING,	&this->text_file,		"Set input text file-name\n\n    If no text is provided, standard input is used for input.\n",		"filename"},
394 395
    {"text-before",	0, 0, G_OPTION_ARG_STRING,	&this->text_before,		"Set text context before each line",	"string"},
    {"text-after",	0, 0, G_OPTION_ARG_STRING,	&this->text_after,		"Set text context after each line",	"string"},
B
Behdad Esfahbod 已提交
396 397
    {NULL}
  };
398 399 400 401 402 403 404 405 406 407
  parser->add_group (entries,
		     "text",
		     "Text options:",
		     "Options controlling the input text",
		     this);
}

void
output_options_t::add_options (option_parser_t *parser)
{
408 409 410 411 412
  const char *text;

  if (NULL == supported_formats)
    text = "Set output format";
  else
B
Behdad Esfahbod 已提交
413 414
  {
    char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
B
Behdad Esfahbod 已提交
415
    text = g_strdup_printf ("Set output format\n\n    Supported output formats are: %s", items);
B
Behdad Esfahbod 已提交
416
    g_free (items);
B
Behdad Esfahbod 已提交
417
    parser->free_later ((char *) text);
B
Behdad Esfahbod 已提交
418
  }
419

420 421
  GOptionEntry entries[] =
  {
422
    {"output-file",	0, 0, G_OPTION_ARG_STRING,	&this->output_file,		"Set output file-name (default: stdout)","filename"},
423
    {"output-format",	0, 0, G_OPTION_ARG_STRING,	&this->output_format,		text,					"format"},
424 425 426 427 428 429 430 431
    {NULL}
  };
  parser->add_group (entries,
		     "output",
		     "Output options:",
		     "Options controlling the output",
		     this);
}
B
Behdad Esfahbod 已提交
432 433 434



435 436 437 438 439 440 441 442 443
hb_font_t *
font_options_t::get_font (void) const
{
  if (font)
    return font;

  hb_blob_t *blob = NULL;

  /* Create the blob */
B
Behdad Esfahbod 已提交
444
  {
445 446
    char *font_data;
    unsigned int len = 0;
447 448 449 450
    hb_destroy_func_t destroy;
    void *user_data;
    hb_memory_mode_t mm;

451
    /* This is a hell of a lot of code for just reading a file! */
452
    if (!font_file)
453
      fail (true, "No font file set");
454

455 456 457 458
    if (0 == strcmp (font_file, "-")) {
      /* read it */
      GString *gs = g_string_new (NULL);
      char buf[BUFSIZ];
B
Behdad Esfahbod 已提交
459
#if defined(_WIN32) || defined(__CYGWIN__)
460
      setmode (fileno (stdin), _O_BINARY);
461 462 463 464
#endif
      while (!feof (stdin)) {
	size_t ret = fread (buf, 1, sizeof (buf), stdin);
	if (ferror (stdin))
465
	  fail (false, "Failed reading font from standard input: %s",
466 467 468 469
		strerror (errno));
	g_string_append_len (gs, buf, ret);
      }
      len = gs->len;
470
      font_data = g_string_free (gs, false);
471 472 473 474
      user_data = font_data;
      destroy = (hb_destroy_func_t) g_free;
      mm = HB_MEMORY_MODE_WRITABLE;
    } else {
B
Behdad Esfahbod 已提交
475
      GError *error = NULL;
476
      GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
477 478 479 480
      if (mf) {
	font_data = g_mapped_file_get_contents (mf);
	len = g_mapped_file_get_length (mf);
	if (len) {
B
Behdad Esfahbod 已提交
481
	  destroy = (hb_destroy_func_t) g_mapped_file_unref;
482 483 484
	  user_data = (void *) mf;
	  mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
	} else
B
Behdad Esfahbod 已提交
485
	  g_mapped_file_unref (mf);
B
Behdad Esfahbod 已提交
486
      } else {
487
	fail (false, "%s", error->message);
B
Behdad Esfahbod 已提交
488
	//g_error_free (error);
489 490 491 492 493 494 495 496 497 498 499 500 501
      }
      if (!len) {
	/* GMappedFile is buggy, it doesn't fail if file isn't regular.
	 * Try reading.
	 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
        GError *error = NULL;
	gsize l;
	if (g_file_get_contents (font_file, &font_data, &l, &error)) {
	  len = l;
	  destroy = (hb_destroy_func_t) g_free;
	  user_data = (void *) font_data;
	  mm = HB_MEMORY_MODE_WRITABLE;
	} else {
502
	  fail (false, "%s", error->message);
503 504 505 506
	  //g_error_free (error);
	}
      }
    }
507 508 509 510 511 512 513 514 515 516 517 518

    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
  }

  /* Create the face */
  hb_face_t *face = hb_face_create (blob, face_index);
  hb_blob_destroy (blob);


  font = hb_font_create (face);

  unsigned int upem = hb_face_get_upem (face);
B
Behdad Esfahbod 已提交
519
  hb_font_set_scale (font, upem, upem);
520 521
  hb_face_destroy (face);

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  void (*set_font_funcs) (hb_font_t *) = NULL;
  if (!font_funcs)
  {
    set_font_funcs = supported_font_funcs[0].func;
  }
  else
  {
    for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
      if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name))
      {
	set_font_funcs = supported_font_funcs[i].func;
	break;
      }
    if (!set_font_funcs)
    {
      GString *s = g_string_new (NULL);
      for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
      {
        if (i)
	  g_string_append_c (s, '/');
	g_string_append (s, supported_font_funcs[i].name);
      }
      char *p = g_string_free (s, FALSE);
      fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
	    font_funcs,
	    p,
	    supported_font_funcs[0].name);
      //free (p);
    }
  }
  set_font_funcs (font);
553 554 555 556 557 558 559 560

  return font;
}


const char *
text_options_t::get_line (unsigned int *len)
{
B
Behdad Esfahbod 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  if (text) {
    if (text_len == (unsigned int) -1)
      text_len = strlen (text);

    if (!text_len) {
      *len = 0;
      return NULL;
    }

    const char *ret = text;
    const char *p = (const char *) memchr (text, '\n', text_len);
    unsigned int ret_len;
    if (!p) {
      ret_len = text_len;
      text += ret_len;
      text_len = 0;
    } else {
      ret_len = p - ret;
      text += ret_len + 1;
      text_len -= ret_len + 1;
    }

    *len = ret_len;
    return ret;
  }

  if (!fp) {
588
    if (!text_file)
589
      fail (true, "At least one of text or text-file must be set");
590

B
Behdad Esfahbod 已提交
591 592 593 594
    if (0 != strcmp (text_file, "-"))
      fp = fopen (text_file, "r");
    else
      fp = stdin;
B
Behdad Esfahbod 已提交
595

B
Behdad Esfahbod 已提交
596
    if (!fp)
597
      fail (false, "Failed opening text file `%s': %s",
B
Behdad Esfahbod 已提交
598
	    text_file, strerror (errno));
599

B
Behdad Esfahbod 已提交
600
    gs = g_string_new (NULL);
601 602
  }

B
Behdad Esfahbod 已提交
603 604 605 606
  g_string_set_size (gs, 0);
  char buf[BUFSIZ];
  while (fgets (buf, sizeof (buf), fp)) {
    unsigned int bytes = strlen (buf);
B
Behdad Esfahbod 已提交
607
    if (bytes && buf[bytes - 1] == '\n') {
B
Behdad Esfahbod 已提交
608 609 610 611 612
      bytes--;
      g_string_append_len (gs, buf, bytes);
      break;
    }
      g_string_append_len (gs, buf, bytes);
B
Behdad Esfahbod 已提交
613
  }
B
Behdad Esfahbod 已提交
614
  if (ferror (fp))
615
    fail (false, "Failed reading text: %s",
B
Behdad Esfahbod 已提交
616 617 618
	  strerror (errno));
  *len = gs->len;
  return !*len && feof (fp) ? NULL : gs->str;
B
Behdad Esfahbod 已提交
619
}
B
Behdad Esfahbod 已提交
620 621 622 623 624 625 626 627 628 629 630


FILE *
output_options_t::get_file_handle (void)
{
  if (fp)
    return fp;

  if (output_file)
    fp = fopen (output_file, "wb");
  else {
B
Behdad Esfahbod 已提交
631
#if defined(_WIN32) || defined(__CYGWIN__)
B
Behdad Esfahbod 已提交
632
    setmode (fileno (stdout), _O_BINARY);
B
Behdad Esfahbod 已提交
633 634 635 636
#endif
    fp = stdout;
  }
  if (!fp)
637
    fail (false, "Cannot open output file `%s': %s",
B
Behdad Esfahbod 已提交
638 639 640 641
	  g_filename_display_name (output_file), strerror (errno));

  return fp;
}
B
Behdad Esfahbod 已提交
642

B
Behdad Esfahbod 已提交
643 644 645 646 647 648 649
static gboolean
parse_verbose (const char *name G_GNUC_UNUSED,
	       const char *arg G_GNUC_UNUSED,
	       gpointer    data G_GNUC_UNUSED,
	       GError    **error G_GNUC_UNUSED)
{
  format_options_t *format_opts = (format_options_t *) data;
650 651
  format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
  return true;
B
Behdad Esfahbod 已提交
652
}
B
Behdad Esfahbod 已提交
653 654 655 656 657 658 659 660 661

void
format_options_t::add_options (option_parser_t *parser)
{
  GOptionEntry entries[] =
  {
    {"no-glyph-names",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_glyph_names,	"Use glyph indices instead of names",	NULL},
    {"no-positions",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_positions,		"Do not show glyph positions",		NULL},
    {"no-clusters",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&this->show_clusters,		"Do not show cluster mapping",		NULL},
662 663
    {"show-text",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_text,		"Show input text",			NULL},
    {"show-unicode",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_unicode,		"Show input Unicode codepoints",	NULL},
B
Behdad Esfahbod 已提交
664
    {"show-line-num",	0, 0,			  G_OPTION_ARG_NONE,	&this->show_line_num,		"Show line numbers",			NULL},
B
Behdad Esfahbod 已提交
665
    {"verbose",		0, G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose,	"Show everything",			NULL},
B
Behdad Esfahbod 已提交
666 667 668 669 670 671 672 673 674 675
    {NULL}
  };
  parser->add_group (entries,
		     "format",
		     "Format options:",
		     "Options controlling the formatting of buffer contents",
		     this);
}

void
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
format_options_t::serialize_unicode (hb_buffer_t *buffer,
				     GString     *gs)
{
  unsigned int num_glyphs = hb_buffer_get_length (buffer);
  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);

  g_string_append_c (gs, '<');
  for (unsigned int i = 0; i < num_glyphs; i++)
  {
    if (i)
      g_string_append_c (gs, ',');
    g_string_append_printf (gs, "U+%04X", info->codepoint);
    info++;
  }
  g_string_append_c (gs, '>');
}

void
format_options_t::serialize_glyphs (hb_buffer_t *buffer,
				    hb_font_t   *font,
696 697
				    hb_buffer_serialize_format_t output_format,
				    hb_buffer_serialize_flags_t flags,
698
				    GString     *gs)
B
Behdad Esfahbod 已提交
699
{
700
  g_string_append_c (gs, '[');
701 702 703 704 705 706 707 708 709 710 711 712
  unsigned int num_glyphs = hb_buffer_get_length (buffer);
  unsigned int start = 0;

  while (start < num_glyphs) {
    char buf[1024];
    unsigned int consumed;
    start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
					 buf, sizeof (buf), &consumed,
					 font, output_format, flags);
    if (!consumed)
      break;
    g_string_append (gs, buf);
B
Behdad Esfahbod 已提交
713
  }
714
  g_string_append_c (gs, ']');
B
Behdad Esfahbod 已提交
715
}
B
Behdad Esfahbod 已提交
716 717 718 719 720 721 722 723
void
format_options_t::serialize_line_no (unsigned int  line_no,
				     GString      *gs)
{
  if (show_line_num)
    g_string_append_printf (gs, "%d: ", line_no);
}
void
724 725 726 727 728 729
format_options_t::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)
B
Behdad Esfahbod 已提交
730 731 732
{
  if (show_text) {
    serialize_line_no (line_no, gs);
733
    g_string_append_c (gs, '(');
B
Behdad Esfahbod 已提交
734
    g_string_append_len (gs, text, text_len);
735
    g_string_append_c (gs, ')');
B
Behdad Esfahbod 已提交
736 737 738 739 740
    g_string_append_c (gs, '\n');
  }

  if (show_unicode) {
    serialize_line_no (line_no, gs);
B
Behdad Esfahbod 已提交
741
    serialize_unicode (buffer, gs);
B
Behdad Esfahbod 已提交
742 743
    g_string_append_c (gs, '\n');
  }
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
}
void
format_options_t::serialize_message (unsigned int  line_no,
				     const char   *msg,
				     GString      *gs)
{
  serialize_line_no (line_no, gs);
  g_string_append_printf (gs, "%s", msg);
  g_string_append_c (gs, '\n');
}
void
format_options_t::serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
					      unsigned int  line_no,
					      const char   *text,
					      unsigned int  text_len,
					      hb_font_t    *font,
760 761
					      hb_buffer_serialize_format_t output_format,
					      hb_buffer_serialize_flags_t format_flags,
762 763
					      GString      *gs)
{
B
Behdad Esfahbod 已提交
764
  serialize_line_no (line_no, gs);
765
  serialize_glyphs (buffer, font, output_format, format_flags, gs);
B
Behdad Esfahbod 已提交
766 767
  g_string_append_c (gs, '\n');
}