2 * Copyright © 2011,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
34 static struct supported_font_funcs_t {
36 void (*func) (hb_font_t *);
37 } supported_font_funcs[] =
40 {"ft", hb_ft_font_set_funcs},
42 {"ot", hb_ot_font_set_funcs},
47 fail (hb_bool_t suggest_help, const char *format, ...)
52 va_start (vap, format);
53 msg = g_strdup_vprintf (format, vap);
55 const char *prgname = g_get_prgname ();
56 g_printerr ("%s: %s\n", prgname, msg);
58 g_printerr ("Try `%s --help' for more information.\n", prgname);
67 GString *shapers = g_string_new (nullptr);
68 const char **shaper_list = hb_shape_list_shapers ();
70 for (; *shaper_list; shaper_list++) {
71 g_string_append (shapers, *shaper_list);
72 g_string_append_c (shapers, ',');
74 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
76 return g_string_free (shapers, false);
79 static G_GNUC_NORETURN gboolean
80 show_version (const char *name G_GNUC_UNUSED,
81 const char *arg G_GNUC_UNUSED,
82 gpointer data G_GNUC_UNUSED,
83 GError **error G_GNUC_UNUSED)
85 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
87 char *shapers = shapers_to_string ();
88 g_printf ("Available shapers: %s\n", shapers);
90 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
91 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
98 option_parser_t::add_main_options ()
100 GOptionEntry entries[] =
102 {"version", 0, G_OPTION_FLAG_NO_ARG,
103 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
106 g_option_context_add_main_entries (context, entries, nullptr);
110 pre_parse (GOptionContext *context G_GNUC_UNUSED,
111 GOptionGroup *group G_GNUC_UNUSED,
115 option_group_t *option_group = (option_group_t *) data;
116 option_group->pre_parse (error);
117 return *error == nullptr;
121 post_parse (GOptionContext *context G_GNUC_UNUSED,
122 GOptionGroup *group G_GNUC_UNUSED,
126 option_group_t *option_group = static_cast<option_group_t *>(data);
127 option_group->post_parse (error);
128 return *error == nullptr;
132 option_parser_t::add_group (GOptionEntry *entries,
134 const gchar *description,
135 const gchar *help_description,
136 option_group_t *option_group)
138 GOptionGroup *group = g_option_group_new (name, description, help_description,
139 static_cast<gpointer>(option_group), nullptr);
140 g_option_group_add_entries (group, entries);
141 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
142 g_option_context_add_group (context, group);
146 option_parser_t::parse (int *argc, char ***argv)
148 setlocale (LC_ALL, "");
150 GError *parse_error = nullptr;
151 if (!g_option_context_parse (context, argc, argv, &parse_error))
153 if (parse_error != nullptr) {
154 fail (true, "%s", parse_error->message);
155 //g_error_free (parse_error);
157 fail (true, "Option parse error");
163 parse_margin (const char *name G_GNUC_UNUSED,
166 GError **error G_GNUC_UNUSED)
168 view_options_t *view_opts = (view_options_t *) data;
169 view_options_t::margin_t &m = view_opts->margin;
170 switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf", &m.t, &m.r, &m.b, &m.l)) {
171 case 1: m.r = m.t; HB_FALLTHROUGH;
172 case 2: m.b = m.t; HB_FALLTHROUGH;
173 case 3: m.l = m.r; HB_FALLTHROUGH;
176 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
177 "%s argument should be one to four space-separated numbers",
185 parse_shapers (const char *name G_GNUC_UNUSED,
190 shape_options_t *shape_opts = (shape_options_t *) data;
191 char **shapers = g_strsplit (arg, ",", 0);
193 for (char **shaper = shapers; *shaper; shaper++) {
195 for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
196 if (strcmp (*shaper, *hb_shaper) == 0) {
202 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
203 "Unknown or unsupported shaper: %s", *shaper);
204 g_strfreev (shapers);
209 g_strfreev (shape_opts->shapers);
210 shape_opts->shapers = shapers;
214 static G_GNUC_NORETURN gboolean
215 list_shapers (const char *name G_GNUC_UNUSED,
216 const char *arg G_GNUC_UNUSED,
217 gpointer data G_GNUC_UNUSED,
218 GError **error G_GNUC_UNUSED)
220 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
221 g_printf ("%s\n", *shaper);
228 parse_features (const char *name G_GNUC_UNUSED,
231 GError **error G_GNUC_UNUSED)
233 shape_options_t *shape_opts = (shape_options_t *) data;
234 char *s = (char *) arg;
237 shape_opts->num_features = 0;
238 g_free (shape_opts->features);
239 shape_opts->features = nullptr;
244 /* count the features first, so we can allocate memory */
247 shape_opts->num_features++;
253 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
254 if (!shape_opts->features)
257 /* now do the actual parsing */
259 shape_opts->num_features = 0;
261 char *end = strchr (p, ',');
262 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
263 shape_opts->num_features++;
264 p = end ? end + 1 : nullptr;
271 parse_variations (const char *name G_GNUC_UNUSED,
274 GError **error G_GNUC_UNUSED)
276 font_options_t *font_opts = (font_options_t *) data;
277 char *s = (char *) arg;
280 font_opts->num_variations = 0;
281 g_free (font_opts->variations);
282 font_opts->variations = nullptr;
287 /* count the variations first, so we can allocate memory */
290 font_opts->num_variations++;
296 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
297 if (!font_opts->variations)
300 /* now do the actual parsing */
302 font_opts->num_variations = 0;
304 char *end = strchr (p, ',');
305 if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
306 font_opts->num_variations++;
307 p = end ? end + 1 : nullptr;
314 parse_text (const char *name G_GNUC_UNUSED,
317 GError **error G_GNUC_UNUSED)
319 text_options_t *text_opts = (text_options_t *) data;
323 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
324 "Either --text or --unicodes can be provided but not both");
328 text_opts->text_len = -1;
329 text_opts->text = g_strdup (arg);
335 parse_unicodes (const char *name G_GNUC_UNUSED,
338 GError **error G_GNUC_UNUSED)
340 text_options_t *text_opts = (text_options_t *) data;
344 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
345 "Either --text or --unicodes can be provided but not both");
349 GString *gs = g_string_new (nullptr);
350 char *s = (char *) arg;
355 while (*s && strchr ("<+>{},;&#\\xXuUnNiI\n\t\v\f\r ", *s))
361 hb_codepoint_t u = strtoul (s, &p, 16);
364 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
365 "Failed parsing Unicode values at: '%s'", s);
369 g_string_append_unichar (gs, u);
374 text_opts->text_len = gs->len;
375 text_opts->text = g_string_free (gs, FALSE);
381 view_options_t::add_options (option_parser_t *parser)
383 GOptionEntry entries[] =
385 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", nullptr},
386 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"},
387 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"},
388 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
389 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
392 parser->add_group (entries,
395 "Options for output rendering",
400 shape_options_t::add_options (option_parser_t *parser)
402 GOptionEntry entries[] =
404 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
405 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
406 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
407 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
408 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
409 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
410 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
411 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
412 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
413 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
414 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
415 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
416 {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr},
417 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
418 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
419 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
420 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
421 {"num-iterations", 'n', 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
424 parser->add_group (entries,
427 "Options for the shaping process",
430 const gchar *features_help = "Comma-separated list of font features\n"
432 " Features can be enabled or disabled, either globally or limited to\n"
433 " specific character ranges. The format for specifying feature settings\n"
434 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
435 " and 'inherited' are also accepted, though, not documented below.\n"
437 " The range indices refer to the positions between Unicode characters,\n"
438 " unless the --utf8-clusters is provided, in which case range indices\n"
439 " refer to UTF-8 byte indices. The position before the first character\n"
442 " The format is Python-esque. Here is how it all works:\n"
444 " Syntax: Value: Start: End:\n"
447 " \"kern\" 1 0 ∞ # Turn feature on\n"
448 " \"+kern\" 1 0 ∞ # Turn feature on\n"
449 " \"-kern\" 0 0 ∞ # Turn feature off\n"
450 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
451 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
452 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
455 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
456 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
457 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
458 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
459 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
460 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
464 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
466 GOptionEntry entries2[] =
468 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
471 parser->add_group (entries2,
474 "Options for font features used",
479 parse_font_size (const char *name G_GNUC_UNUSED,
482 GError **error G_GNUC_UNUSED)
484 font_options_t *font_opts = (font_options_t *) data;
485 if (0 == strcmp (arg, "upem"))
487 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
490 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
491 case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
494 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
495 "%s argument should be one or two space-separated numbers",
502 parse_font_ppem (const char *name G_GNUC_UNUSED,
505 GError **error G_GNUC_UNUSED)
507 font_options_t *font_opts = (font_options_t *) data;
508 switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
509 case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
512 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
513 "%s argument should be one or two space-separated numbers",
520 font_options_t::add_options (option_parser_t *parser)
522 char *text = nullptr;
525 static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
526 "No supported font-funcs found.");
527 GString *s = g_string_new (nullptr);
528 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
529 supported_font_funcs[0].name,
530 supported_font_funcs[0].name);
531 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
533 g_string_append_c (s, '/');
534 g_string_append (s, supported_font_funcs[i].name);
536 text = g_string_free (s, FALSE);
537 parser->free_later (text);
540 char *font_size_text;
541 if (default_font_size == FONT_SIZE_UPEM)
542 font_size_text = (char *) "Font size (default: upem)";
545 font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
546 parser->free_later (font_size_text);
549 GOptionEntry entries[] =
551 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
552 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
553 {"font-size", 0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
554 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
555 {"font-ppem", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
556 {"font-ptem", 0, 0, G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
557 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
558 {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
561 parser->add_group (entries,
564 "Options for the font",
567 const gchar *variations_help = "Comma-separated list of font variations\n"
569 " Variations are set globally. The format for specifying variation settings\n"
570 " follows. All valid CSS font-variation-settings values other than 'normal'\n"
571 " and 'inherited' are also accepted, though, not documented below.\n"
573 " The format is a tag, optionally followed by an equals sign, followed by a\n"
574 " number. For example:\n"
579 GOptionEntry entries2[] =
581 {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
584 parser->add_group (entries2,
586 "Variations options:",
587 "Options for font variations used",
592 text_options_t::add_options (option_parser_t *parser)
594 GOptionEntry entries[] =
596 {"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"},
597 {"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"},
598 {"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints", "list of hex numbers"},
599 {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
600 {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
603 parser->add_group (entries,
606 "Options for the input text",
611 output_options_t::add_options (option_parser_t *parser)
615 if (nullptr == supported_formats)
616 text = "Set output serialization format";
619 char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
620 text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
622 parser->free_later ((char *) text);
625 GOptionEntry entries[] =
627 {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
628 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"},
631 parser->add_group (entries,
633 "Output destination & format options:",
634 "Options for the destination & form of the output",
641 font_options_t::get_font () const
646 /* Create the blob */
648 fail (true, "No font file set");
650 const char *font_path = font_file;
652 if (0 == strcmp (font_path, "-"))
654 #if defined(_WIN32) || defined(__CYGWIN__)
655 setmode (fileno (stdin), O_BINARY);
658 font_path = "/dev/stdin";
662 blob = hb_blob_create_from_file (font_path);
664 if (blob == hb_blob_get_empty ())
665 fail (false, "Couldn't read or find %s, or it was empty.", font_path);
667 /* Create the face */
668 hb_face_t *face = hb_face_create (blob, face_index);
669 hb_blob_destroy (blob);
672 font = hb_font_create (face);
674 if (font_size_x == FONT_SIZE_UPEM)
675 font_size_x = hb_face_get_upem (face);
676 if (font_size_y == FONT_SIZE_UPEM)
677 font_size_y = hb_face_get_upem (face);
679 hb_font_set_ppem (font, x_ppem, y_ppem);
680 hb_font_set_ptem (font, ptem);
682 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
683 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
684 hb_font_set_scale (font, scale_x, scale_y);
685 hb_face_destroy (face);
687 hb_font_set_variations (font, variations, num_variations);
689 void (*set_font_funcs) (hb_font_t *) = nullptr;
692 set_font_funcs = supported_font_funcs[0].func;
696 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
697 if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
699 set_font_funcs = supported_font_funcs[i].func;
704 GString *s = g_string_new (nullptr);
705 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
708 g_string_append_c (s, '/');
709 g_string_append (s, supported_font_funcs[i].name);
711 char *p = g_string_free (s, FALSE);
712 fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
715 supported_font_funcs[0].name);
719 set_font_funcs (font);
721 hb_ft_font_set_load_flags (font, ft_load_flags);
729 text_options_t::get_line (unsigned int *len)
737 if (line_len == (unsigned int) -1)
738 line_len = strlen (line);
745 const char *ret = line;
746 const char *p = (const char *) memchr (line, '\n', line_len);
747 unsigned int ret_len;
755 line_len -= ret_len + 1;
764 fail (true, "At least one of text or text-file must be set");
766 if (0 != strcmp (text_file, "-"))
767 fp = fopen (text_file, "r");
772 fail (false, "Failed opening text file `%s': %s",
773 text_file, strerror (errno));
775 gs = g_string_new (nullptr);
778 g_string_set_size (gs, 0);
780 while (fgets (buf, sizeof (buf), fp)) {
781 unsigned int bytes = strlen (buf);
782 if (bytes && buf[bytes - 1] == '\n') {
784 g_string_append_len (gs, buf, bytes);
787 g_string_append_len (gs, buf, bytes);
790 fail (false, "Failed reading text: %s",
793 return !*len && feof (fp) ? nullptr : gs->str;
798 output_options_t::get_file_handle ()
804 fp = fopen (output_file, "wb");
806 #if defined(_WIN32) || defined(__CYGWIN__)
807 setmode (fileno (stdout), O_BINARY);
812 fail (false, "Cannot open output file `%s': %s",
813 g_filename_display_name (output_file), strerror (errno));
819 parse_verbose (const char *name G_GNUC_UNUSED,
820 const char *arg G_GNUC_UNUSED,
821 gpointer data G_GNUC_UNUSED,
822 GError **error G_GNUC_UNUSED)
824 format_options_t *format_opts = (format_options_t *) data;
825 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
830 parse_ned (const char *name G_GNUC_UNUSED,
831 const char *arg G_GNUC_UNUSED,
832 gpointer data G_GNUC_UNUSED,
833 GError **error G_GNUC_UNUSED)
835 format_options_t *format_opts = (format_options_t *) data;
836 format_opts->show_clusters = format_opts->show_advances = false;
841 format_options_t::add_options (option_parser_t *parser)
843 GOptionEntry entries[] =
845 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Prefix each line of output with its corresponding input text", nullptr},
846 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Prefix each line of output with its corresponding input codepoint(s)", nullptr},
847 {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Prefix each line of output with its corresponding input line number", nullptr},
848 {"verbose", 'v', G_OPTION_FLAG_NO_ARG,
849 G_OPTION_ARG_CALLBACK, (gpointer) &parse_verbose, "Prefix each line of output with all of the above", nullptr},
850 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE,
851 G_OPTION_ARG_NONE, &this->show_glyph_names, "Output glyph indices instead of names", nullptr},
852 {"no-positions", 0, G_OPTION_FLAG_REVERSE,
853 G_OPTION_ARG_NONE, &this->show_positions, "Do not output glyph positions", nullptr},
854 {"no-advances", 0, G_OPTION_FLAG_REVERSE,
855 G_OPTION_ARG_NONE, &this->show_advances, "Do not output glyph advances", nullptr},
856 {"no-clusters", 0, G_OPTION_FLAG_REVERSE,
857 G_OPTION_ARG_NONE, &this->show_clusters, "Do not output cluster indices", nullptr},
858 {"show-extents", 0, 0, G_OPTION_ARG_NONE, &this->show_extents, "Output glyph extents", nullptr},
859 {"show-flags", 0, 0, G_OPTION_ARG_NONE, &this->show_flags, "Output glyph flags", nullptr},
860 {"ned", 'v', G_OPTION_FLAG_NO_ARG,
861 G_OPTION_ARG_CALLBACK, (gpointer) &parse_ned, "No Extra Data; Do not output clusters or advances", nullptr},
862 {"trace", 'V', 0, G_OPTION_ARG_NONE, &this->trace, "Output interim shaping results", nullptr},
865 parser->add_group (entries,
868 " text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
869 " json: [{\"g\": <glyph name or index>, \"ax\": <horizontal advance>, \"ay\": <vertical advance>, \"dx\": <horizontal displacement>, \"dy\": <vertical displacement>, \"cl\": <glyph cluster index within input>}, ...]\n"
870 "\nOutput syntax options:",
871 "Options for the syntax of the output",
876 format_options_t::serialize_unicode (hb_buffer_t *buffer,
879 unsigned int num_glyphs = hb_buffer_get_length (buffer);
880 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
882 g_string_append_c (gs, '<');
883 for (unsigned int i = 0; i < num_glyphs; i++)
886 g_string_append_c (gs, ',');
887 g_string_append_printf (gs, "U+%04X", info->codepoint);
890 g_string_append_c (gs, '>');
894 format_options_t::serialize_glyphs (hb_buffer_t *buffer,
896 hb_buffer_serialize_format_t output_format,
897 hb_buffer_serialize_flags_t flags,
900 g_string_append_c (gs, '[');
901 unsigned int num_glyphs = hb_buffer_get_length (buffer);
902 unsigned int start = 0;
904 while (start < num_glyphs)
907 unsigned int consumed;
908 start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
909 buf, sizeof (buf), &consumed,
910 font, output_format, flags);
913 g_string_append (gs, buf);
915 g_string_append_c (gs, ']');
918 format_options_t::serialize_line_no (unsigned int line_no,
922 g_string_append_printf (gs, "%d: ", line_no);
925 format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
926 unsigned int line_no,
928 unsigned int text_len,
934 serialize_line_no (line_no, gs);
935 g_string_append_c (gs, '(');
936 g_string_append_len (gs, text, text_len);
937 g_string_append_c (gs, ')');
938 g_string_append_c (gs, '\n');
943 serialize_line_no (line_no, gs);
944 serialize_unicode (buffer, gs);
945 g_string_append_c (gs, '\n');
949 format_options_t::serialize_message (unsigned int line_no,
954 serialize_line_no (line_no, gs);
955 g_string_append_printf (gs, "%s: %s", type, msg);
956 g_string_append_c (gs, '\n');
959 format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
960 unsigned int line_no,
962 unsigned int text_len,
964 hb_buffer_serialize_format_t output_format,
965 hb_buffer_serialize_flags_t format_flags,
968 serialize_line_no (line_no, gs);
969 serialize_glyphs (buffer, font, output_format, format_flags, gs);
970 g_string_append_c (gs, '\n');
974 subset_options_t::add_options (option_parser_t *parser)
976 GOptionEntry entries[] =
978 {"layout", 0, 0, G_OPTION_ARG_NONE, &this->keep_layout, "Keep OpenType Layout tables", nullptr},
979 {"no-hinting", 0, 0, G_OPTION_ARG_NONE, &this->drop_hints, "Whether to drop hints", nullptr},
980 {"desubroutinize", 0, 0, G_OPTION_ARG_NONE, &this->desubroutinize, "Remove CFF/CFF2 use of subroutines", nullptr},
984 parser->add_group (entries,
987 "Options subsetting",