options.cc revision 5857720cd35078d1c3906c8b2db3190b5166b66f
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#include "options.hh" 28 29#ifdef HAVE_FREETYPE 30#include <hb-ft.h> 31#endif 32 33 34void 35fail (hb_bool_t suggest_help, const char *format, ...) 36{ 37 const char *msg; 38 39 va_list vap; 40 va_start (vap, format); 41 msg = g_strdup_vprintf (format, vap); 42 const char *prgname = g_get_prgname (); 43 g_printerr ("%s: %s\n", prgname, msg); 44 if (suggest_help) 45 g_printerr ("Try `%s --help' for more information.\n", prgname); 46 47 exit (1); 48} 49 50 51hb_bool_t debug = FALSE; 52 53static gchar * 54shapers_to_string (void) 55{ 56 GString *shapers = g_string_new (NULL); 57 const char **shaper_list = hb_shape_list_shapers (); 58 59 for (; *shaper_list; shaper_list++) { 60 g_string_append (shapers, *shaper_list); 61 g_string_append_c (shapers, ','); 62 } 63 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1)); 64 65 return g_string_free (shapers, FALSE); 66} 67 68static G_GNUC_NORETURN gboolean 69show_version (const char *name G_GNUC_UNUSED, 70 const char *arg G_GNUC_UNUSED, 71 gpointer data G_GNUC_UNUSED, 72 GError **error G_GNUC_UNUSED) 73{ 74 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION); 75 76 char *shapers = shapers_to_string (); 77 g_printf ("Available shapers: %s\n", shapers); 78 g_free (shapers); 79 if (strcmp (HB_VERSION_STRING, hb_version_string ())) 80 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ()); 81 82 exit(0); 83} 84 85 86void 87option_parser_t::add_main_options (void) 88{ 89 GOptionEntry entries[] = 90 { 91 {"version", 0, G_OPTION_FLAG_NO_ARG, 92 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL}, 93 {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL}, 94 {NULL} 95 }; 96 g_option_context_add_main_entries (context, entries, NULL); 97} 98 99static gboolean 100pre_parse (GOptionContext *context G_GNUC_UNUSED, 101 GOptionGroup *group G_GNUC_UNUSED, 102 gpointer data, 103 GError **error) 104{ 105 option_group_t *option_group = (option_group_t *) data; 106 option_group->pre_parse (error); 107 return *error == NULL; 108} 109 110static gboolean 111post_parse (GOptionContext *context G_GNUC_UNUSED, 112 GOptionGroup *group G_GNUC_UNUSED, 113 gpointer data, 114 GError **error) 115{ 116 option_group_t *option_group = static_cast<option_group_t *>(data); 117 option_group->post_parse (error); 118 return *error == NULL; 119} 120 121void 122option_parser_t::add_group (GOptionEntry *entries, 123 const gchar *name, 124 const gchar *description, 125 const gchar *help_description, 126 option_group_t *option_group) 127{ 128 GOptionGroup *group = g_option_group_new (name, description, help_description, 129 static_cast<gpointer>(option_group), NULL); 130 g_option_group_add_entries (group, entries); 131 g_option_group_set_parse_hooks (group, pre_parse, post_parse); 132 g_option_context_add_group (context, group); 133} 134 135void 136option_parser_t::parse (int *argc, char ***argv) 137{ 138 setlocale (LC_ALL, ""); 139 140 GError *parse_error = NULL; 141 if (!g_option_context_parse (context, argc, argv, &parse_error)) 142 { 143 if (parse_error != NULL) { 144 fail (TRUE, "%s", parse_error->message); 145 //g_error_free (parse_error); 146 } else 147 fail (TRUE, "Option parse error"); 148 } 149} 150 151 152static gboolean 153parse_margin (const char *name G_GNUC_UNUSED, 154 const char *arg, 155 gpointer data, 156 GError **error G_GNUC_UNUSED) 157{ 158 view_options_t *view_opts = (view_options_t *) data; 159 view_options_t::margin_t &m = view_opts->margin; 160 switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) { 161 case 1: m.r = m.t; 162 case 2: m.b = m.t; 163 case 3: m.l = m.r; 164 case 4: return TRUE; 165 default: 166 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, 167 "%s argument should be one to four space-separated numbers", 168 name); 169 return FALSE; 170 } 171} 172 173 174static gboolean 175parse_shapers (const char *name G_GNUC_UNUSED, 176 const char *arg, 177 gpointer data, 178 GError **error G_GNUC_UNUSED) 179{ 180 shape_options_t *shape_opts = (shape_options_t *) data; 181 shape_opts->shapers = g_strsplit (arg, ",", 0); 182 return TRUE; 183} 184 185 186static void 187parse_space (char **pp) 188{ 189 char c; 190#define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v') 191 while (c = **pp, ISSPACE (c)) 192 (*pp)++; 193#undef ISSPACE 194} 195 196static hb_bool_t 197parse_char (char **pp, char c) 198{ 199 parse_space (pp); 200 201 if (**pp != c) 202 return FALSE; 203 204 (*pp)++; 205 return TRUE; 206} 207 208static hb_bool_t 209parse_uint (char **pp, unsigned int *pv) 210{ 211 char *p = *pp; 212 unsigned int v; 213 214 v = strtol (p, pp, 0); 215 216 if (p == *pp) 217 return FALSE; 218 219 *pv = v; 220 return TRUE; 221} 222 223 224static hb_bool_t 225parse_feature_value_prefix (char **pp, hb_feature_t *feature) 226{ 227 if (parse_char (pp, '-')) 228 feature->value = 0; 229 else { 230 parse_char (pp, '+'); 231 feature->value = 1; 232 } 233 234 return TRUE; 235} 236 237static hb_bool_t 238parse_feature_tag (char **pp, hb_feature_t *feature) 239{ 240 char *p = *pp, c; 241 242 parse_space (pp); 243 244#define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9')) 245 while (c = **pp, ISALNUM(c)) 246 (*pp)++; 247#undef ISALNUM 248 249 if (p == *pp) 250 return FALSE; 251 252 feature->tag = hb_tag_from_string (p, *pp - p); 253 return TRUE; 254} 255 256static hb_bool_t 257parse_feature_indices (char **pp, hb_feature_t *feature) 258{ 259 hb_bool_t has_start; 260 261 feature->start = 0; 262 feature->end = (unsigned int) -1; 263 264 if (!parse_char (pp, '[')) 265 return TRUE; 266 267 has_start = parse_uint (pp, &feature->start); 268 269 if (parse_char (pp, ':')) { 270 parse_uint (pp, &feature->end); 271 } else { 272 if (has_start) 273 feature->end = feature->start + 1; 274 } 275 276 return parse_char (pp, ']'); 277} 278 279static hb_bool_t 280parse_feature_value_postfix (char **pp, hb_feature_t *feature) 281{ 282 return !parse_char (pp, '=') || parse_uint (pp, &feature->value); 283} 284 285 286static hb_bool_t 287parse_one_feature (char **pp, hb_feature_t *feature) 288{ 289 return parse_feature_value_prefix (pp, feature) && 290 parse_feature_tag (pp, feature) && 291 parse_feature_indices (pp, feature) && 292 parse_feature_value_postfix (pp, feature) && 293 (parse_char (pp, ',') || **pp == '\0'); 294} 295 296static void 297skip_one_feature (char **pp) 298{ 299 char *e; 300 e = strchr (*pp, ','); 301 if (e) 302 *pp = e + 1; 303 else 304 *pp = *pp + strlen (*pp); 305} 306 307static gboolean 308parse_features (const char *name G_GNUC_UNUSED, 309 const char *arg, 310 gpointer data, 311 GError **error G_GNUC_UNUSED) 312{ 313 shape_options_t *shape_opts = (shape_options_t *) data; 314 char *s = (char *) arg; 315 char *p; 316 317 shape_opts->num_features = 0; 318 shape_opts->features = NULL; 319 320 if (!*s) 321 return TRUE; 322 323 /* count the features first, so we can allocate memory */ 324 p = s; 325 do { 326 shape_opts->num_features++; 327 p = strchr (p, ','); 328 if (p) 329 p++; 330 } while (p); 331 332 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features)); 333 334 /* now do the actual parsing */ 335 p = s; 336 shape_opts->num_features = 0; 337 while (*p) { 338 if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features])) 339 shape_opts->num_features++; 340 else 341 skip_one_feature (&p); 342 } 343 344 return TRUE; 345} 346 347 348void 349view_options_t::add_options (option_parser_t *parser) 350{ 351 GOptionEntry entries[] = 352 { 353 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL}, 354 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"}, 355 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"}, 356 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"}, 357 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"}, 358 {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &this->font_size, "Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"}, 359 {NULL} 360 }; 361 parser->add_group (entries, 362 "view", 363 "View options:", 364 "Options controlling the output rendering", 365 this); 366} 367 368void 369shape_options_t::add_options (option_parser_t *parser) 370{ 371 GOptionEntry entries[] = 372 { 373 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers", "list"}, 374 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, 375 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"}, 376 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, 377 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, "Font features to apply to text", "TODO"}, 378 {NULL} 379 }; 380 parser->add_group (entries, 381 "shape", 382 "Shape options:", 383 "Options controlling the shaping process", 384 this); 385} 386 387void 388font_options_t::add_options (option_parser_t *parser) 389{ 390 GOptionEntry entries[] = 391 { 392 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"}, 393 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"}, 394 {NULL} 395 }; 396 parser->add_group (entries, 397 "font", 398 "Font options:", 399 "Options controlling the font", 400 this); 401} 402 403void 404text_options_t::add_options (option_parser_t *parser) 405{ 406 GOptionEntry entries[] = 407 { 408 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"}, 409 {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"}, 410 {NULL} 411 }; 412 parser->add_group (entries, 413 "text", 414 "Text options:", 415 "Options controlling the input text", 416 this); 417} 418 419void 420output_options_t::add_options (option_parser_t *parser) 421{ 422 GOptionEntry entries[] = 423 { 424 {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, 425 {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, "Set output format", "format"}, 426 {NULL} 427 }; 428 parser->add_group (entries, 429 "output", 430 "Output options:", 431 "Options controlling the output", 432 this); 433} 434 435 436 437hb_font_t * 438font_options_t::get_font (void) const 439{ 440 if (font) 441 return font; 442 443 hb_blob_t *blob = NULL; 444 445 /* Create the blob */ 446 { 447 char *font_data; 448 unsigned int len = 0; 449 hb_destroy_func_t destroy; 450 void *user_data; 451 hb_memory_mode_t mm; 452 453 /* This is a hell of a lot of code for just reading a file! */ 454 if (!font_file) 455 fail (TRUE, "No font file set"); 456 457 if (0 == strcmp (font_file, "-")) { 458 /* read it */ 459 GString *gs = g_string_new (NULL); 460 char buf[BUFSIZ]; 461#ifdef HAVE__SETMODE 462 _setmode (fileno (stdin), _O_BINARY); 463#endif 464 while (!feof (stdin)) { 465 size_t ret = fread (buf, 1, sizeof (buf), stdin); 466 if (ferror (stdin)) 467 fail (FALSE, "Failed reading font from standard input: %s", 468 strerror (errno)); 469 g_string_append_len (gs, buf, ret); 470 } 471 len = gs->len; 472 font_data = g_string_free (gs, FALSE); 473 user_data = font_data; 474 destroy = (hb_destroy_func_t) g_free; 475 mm = HB_MEMORY_MODE_WRITABLE; 476 } else { 477 GMappedFile *mf = g_mapped_file_new (font_file, FALSE, NULL); 478 if (mf) { 479 font_data = g_mapped_file_get_contents (mf); 480 len = g_mapped_file_get_length (mf); 481 if (len) { 482 destroy = (hb_destroy_func_t) g_mapped_file_unref; 483 user_data = (void *) mf; 484 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; 485 } else 486 g_mapped_file_unref (mf); 487 } 488 if (!len) { 489 /* GMappedFile is buggy, it doesn't fail if file isn't regular. 490 * Try reading. 491 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */ 492 GError *error = NULL; 493 gsize l; 494 if (g_file_get_contents (font_file, &font_data, &l, &error)) { 495 len = l; 496 destroy = (hb_destroy_func_t) g_free; 497 user_data = (void *) font_data; 498 mm = HB_MEMORY_MODE_WRITABLE; 499 } else { 500 fail (FALSE, "%s", error->message); 501 //g_error_free (error); 502 } 503 } 504 } 505 506 blob = hb_blob_create (font_data, len, mm, user_data, destroy); 507 } 508 509 /* Create the face */ 510 hb_face_t *face = hb_face_create (blob, face_index); 511 hb_blob_destroy (blob); 512 513 514 font = hb_font_create (face); 515 516 unsigned int upem = hb_face_get_upem (face); 517 hb_font_set_scale (font, upem, upem); 518 hb_face_destroy (face); 519 520#ifdef HAVE_FREETYPE 521 hb_ft_font_set_funcs (font); 522#endif 523 524 return font; 525} 526 527 528const char * 529text_options_t::get_line (unsigned int *len) 530{ 531 if (text) { 532 if (text_len == (unsigned int) -1) 533 text_len = strlen (text); 534 535 if (!text_len) { 536 *len = 0; 537 return NULL; 538 } 539 540 const char *ret = text; 541 const char *p = (const char *) memchr (text, '\n', text_len); 542 unsigned int ret_len; 543 if (!p) { 544 ret_len = text_len; 545 text += ret_len; 546 text_len = 0; 547 } else { 548 ret_len = p - ret; 549 text += ret_len + 1; 550 text_len -= ret_len + 1; 551 } 552 553 *len = ret_len; 554 return ret; 555 } 556 557 if (!fp) { 558 if (!text_file) 559 fail (TRUE, "At least one of text or text-file must be set"); 560 561 if (0 != strcmp (text_file, "-")) 562 fp = fopen (text_file, "r"); 563 else 564 fp = stdin; 565 566 if (!fp) 567 fail (FALSE, "Failed opening text file `%s': %s", 568 text_file, strerror (errno)); 569 570 gs = g_string_new (NULL); 571 } 572 573 g_string_set_size (gs, 0); 574 char buf[BUFSIZ]; 575 while (fgets (buf, sizeof (buf), fp)) { 576 unsigned int bytes = strlen (buf); 577 if (buf[bytes - 1] == '\n') { 578 bytes--; 579 g_string_append_len (gs, buf, bytes); 580 break; 581 } 582 g_string_append_len (gs, buf, bytes); 583 } 584 if (ferror (fp)) 585 fail (FALSE, "Failed reading text: %s", 586 strerror (errno)); 587 *len = gs->len; 588 return !*len && feof (fp) ? NULL : gs->str; 589} 590 591 592FILE * 593output_options_t::get_file_handle (void) 594{ 595 if (fp) 596 return fp; 597 598 if (output_file) 599 fp = fopen (output_file, "wb"); 600 else { 601#ifdef HAVE__SETMODE 602 _setmode (fileno (stdout), _O_BINARY); 603#endif 604 fp = stdout; 605 } 606 if (!fp) 607 fail (FALSE, "Cannot open output file `%s': %s", 608 g_filename_display_name (output_file), strerror (errno)); 609 610 return fp; 611} 612 613 614void 615format_options_t::add_options (option_parser_t *parser) 616{ 617 GOptionEntry entries[] = 618 { 619 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL}, 620 {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL}, 621 {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL}, 622 {NULL} 623 }; 624 parser->add_group (entries, 625 "format", 626 "Format options:", 627 "Options controlling the formatting of buffer contents", 628 this); 629} 630 631void 632format_options_t::serialize (hb_buffer_t *buffer, 633 hb_font_t *font, 634 GString *gs) 635{ 636 FT_Face ft_face = show_glyph_names ? hb_ft_font_get_face (font) : NULL; 637 638 unsigned int num_glyphs = hb_buffer_get_length (buffer); 639 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); 640 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL); 641 642 g_string_append_c (gs, '<'); 643 for (unsigned int i = 0; i < num_glyphs; i++) 644 { 645 if (i) 646 g_string_append_c (gs, '|'); 647 648 char glyph_name[30]; 649 if (show_glyph_names) { 650 if (!FT_Get_Glyph_Name (ft_face, info->codepoint, glyph_name, sizeof (glyph_name))) 651 g_string_append_printf (gs, "%s", glyph_name); 652 else 653 g_string_append_printf (gs, "gid%u", info->codepoint); 654 } else 655 g_string_append_printf (gs, "%u", info->codepoint); 656 657 if (show_clusters) 658 g_string_append_printf (gs, "=%u", info->cluster); 659 660 if (show_positions && (pos->x_offset || pos->y_offset)) { 661 g_string_append_c (gs, '@'); 662 if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset); 663 if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset); 664 } 665 if (show_positions && (pos->x_advance || pos->y_advance)) { 666 g_string_append_c (gs, '+'); 667 if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance); 668 if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance); 669 } 670 671 info++; 672 pos++; 673 } 674 g_string_append_c (gs, '>'); 675} 676