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
36 struct supported_font_funcs_t {
38 void (*func) (hb_font_t *);
39 } supported_font_funcs[] =
42 {"ft", hb_ft_font_set_funcs},
45 {"ot", hb_ot_font_set_funcs},
51 fail (hb_bool_t suggest_help, const char *format, ...)
56 va_start (vap, format);
57 msg = g_strdup_vprintf (format, vap);
59 const char *prgname = g_get_prgname ();
60 g_printerr ("%s: %s\n", prgname, msg);
62 g_printerr ("Try `%s --help' for more information.\n", prgname);
68 hb_bool_t debug = false;
71 shapers_to_string (void)
73 GString *shapers = g_string_new (nullptr);
74 const char **shaper_list = hb_shape_list_shapers ();
76 for (; *shaper_list; shaper_list++) {
77 g_string_append (shapers, *shaper_list);
78 g_string_append_c (shapers, ',');
80 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
82 return g_string_free (shapers, false);
85 static G_GNUC_NORETURN gboolean
86 show_version (const char *name G_GNUC_UNUSED,
87 const char *arg G_GNUC_UNUSED,
88 gpointer data G_GNUC_UNUSED,
89 GError **error G_GNUC_UNUSED)
91 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
93 char *shapers = shapers_to_string ();
94 g_printf ("Available shapers: %s\n", shapers);
96 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
97 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
104 option_parser_t::add_main_options (void)
106 GOptionEntry entries[] =
108 {"version", 0, G_OPTION_FLAG_NO_ARG,
109 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
110 {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", nullptr},
113 g_option_context_add_main_entries (context, entries, nullptr);
117 pre_parse (GOptionContext *context G_GNUC_UNUSED,
118 GOptionGroup *group G_GNUC_UNUSED,
122 option_group_t *option_group = (option_group_t *) data;
123 option_group->pre_parse (error);
124 return *error == nullptr;
128 post_parse (GOptionContext *context G_GNUC_UNUSED,
129 GOptionGroup *group G_GNUC_UNUSED,
133 option_group_t *option_group = static_cast<option_group_t *>(data);
134 option_group->post_parse (error);
135 return *error == nullptr;
139 option_parser_t::add_group (GOptionEntry *entries,
141 const gchar *description,
142 const gchar *help_description,
143 option_group_t *option_group)
145 GOptionGroup *group = g_option_group_new (name, description, help_description,
146 static_cast<gpointer>(option_group), nullptr);
147 g_option_group_add_entries (group, entries);
148 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
149 g_option_context_add_group (context, group);
153 option_parser_t::parse (int *argc, char ***argv)
155 setlocale (LC_ALL, "");
157 GError *parse_error = nullptr;
158 if (!g_option_context_parse (context, argc, argv, &parse_error))
160 if (parse_error != nullptr) {
161 fail (true, "%s", parse_error->message);
162 //g_error_free (parse_error);
164 fail (true, "Option parse error");
170 parse_margin (const char *name G_GNUC_UNUSED,
173 GError **error G_GNUC_UNUSED)
175 view_options_t *view_opts = (view_options_t *) data;
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)) {
183 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
184 "%s argument should be one to four space-separated numbers",
192 parse_shapers (const char *name G_GNUC_UNUSED,
195 GError **error G_GNUC_UNUSED)
197 shape_options_t *shape_opts = (shape_options_t *) data;
198 g_strfreev (shape_opts->shapers);
199 shape_opts->shapers = g_strsplit (arg, ",", 0);
203 static G_GNUC_NORETURN gboolean
204 list_shapers (const char *name G_GNUC_UNUSED,
205 const char *arg G_GNUC_UNUSED,
206 gpointer data G_GNUC_UNUSED,
207 GError **error G_GNUC_UNUSED)
209 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
210 g_printf ("%s\n", *shaper);
217 parse_features (const char *name G_GNUC_UNUSED,
220 GError **error G_GNUC_UNUSED)
222 shape_options_t *shape_opts = (shape_options_t *) data;
223 char *s = (char *) arg;
226 shape_opts->num_features = 0;
227 g_free (shape_opts->features);
228 shape_opts->features = nullptr;
233 /* count the features first, so we can allocate memory */
236 shape_opts->num_features++;
242 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
243 if (!shape_opts->features)
246 /* now do the actual parsing */
248 shape_opts->num_features = 0;
250 char *end = strchr (p, ',');
251 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
252 shape_opts->num_features++;
253 p = end ? end + 1 : nullptr;
260 parse_variations (const char *name G_GNUC_UNUSED,
263 GError **error G_GNUC_UNUSED)
265 font_options_t *font_opts = (font_options_t *) data;
266 char *s = (char *) arg;
269 font_opts->num_variations = 0;
270 g_free (font_opts->variations);
271 font_opts->variations = nullptr;
276 /* count the variations first, so we can allocate memory */
279 font_opts->num_variations++;
285 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
286 if (!font_opts->variations)
289 /* now do the actual parsing */
291 font_opts->num_variations = 0;
293 char *end = strchr (p, ',');
294 if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
295 font_opts->num_variations++;
296 p = end ? end + 1 : nullptr;
303 parse_text (const char *name G_GNUC_UNUSED,
306 GError **error G_GNUC_UNUSED)
308 text_options_t *text_opts = (text_options_t *) data;
312 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
313 "Either --text or --unicodes can be provided but not both");
317 text_opts->text = g_strdup (arg);
323 parse_unicodes (const char *name G_GNUC_UNUSED,
326 GError **error G_GNUC_UNUSED)
328 text_options_t *text_opts = (text_options_t *) data;
332 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
333 "Either --text or --unicodes can be provided but not both");
337 GString *gs = g_string_new (nullptr);
338 char *s = (char *) arg;
343 while (*s && strchr ("<+>{},;&#\\xXuUnNiI\n\t", *s))
347 hb_codepoint_t u = strtoul (s, &p, 16);
350 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
351 "Failed parsing Unicode values at: '%s'", s);
355 g_string_append_unichar (gs, u);
360 text_opts->text = g_string_free (gs, FALSE);
366 view_options_t::add_options (option_parser_t *parser)
368 GOptionEntry entries[] =
370 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", nullptr},
371 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"},
372 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"},
373 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
374 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
377 parser->add_group (entries,
380 "Options for output rendering",
385 shape_options_t::add_options (option_parser_t *parser)
387 GOptionEntry entries[] =
389 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
390 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
391 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
392 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
393 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
394 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
395 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
396 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
397 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
398 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
399 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
400 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
401 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
402 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
403 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
404 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
405 {"num-iterations", 0, 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
408 parser->add_group (entries,
411 "Options for the shaping process",
414 const gchar *features_help = "Comma-separated list of font features\n"
416 " Features can be enabled or disabled, either globally or limited to\n"
417 " specific character ranges. The format for specifying feature settings\n"
418 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
419 " and 'inherited' are also accepted, though, not documented below.\n"
421 " The range indices refer to the positions between Unicode characters,\n"
422 " unless the --utf8-clusters is provided, in which case range indices\n"
423 " refer to UTF-8 byte indices. The position before the first character\n"
426 " The format is Python-esque. Here is how it all works:\n"
428 " Syntax: Value: Start: End:\n"
431 " \"kern\" 1 0 ∞ # Turn feature on\n"
432 " \"+kern\" 1 0 ∞ # Turn feature on\n"
433 " \"-kern\" 0 0 ∞ # Turn feature off\n"
434 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
435 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
436 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
439 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
440 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
441 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
442 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
443 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
444 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
448 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
450 GOptionEntry entries2[] =
452 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
455 parser->add_group (entries2,
458 "Options for font features used",
463 parse_font_size (const char *name G_GNUC_UNUSED,
466 GError **error G_GNUC_UNUSED)
468 font_options_t *font_opts = (font_options_t *) data;
469 if (0 == strcmp (arg, "upem"))
471 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
474 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
475 case 1: font_opts->font_size_y = font_opts->font_size_x;
478 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
479 "%s argument should be one or two space-separated numbers",
486 parse_font_ppem (const char *name G_GNUC_UNUSED,
489 GError **error G_GNUC_UNUSED)
491 font_options_t *font_opts = (font_options_t *) data;
492 switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
493 case 1: font_opts->y_ppem = font_opts->x_ppem;
496 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
497 "%s argument should be one or two space-separated numbers",
504 font_options_t::add_options (option_parser_t *parser)
506 char *text = nullptr;
509 static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
510 "No supported font-funcs found.");
511 GString *s = g_string_new (nullptr);
512 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
513 supported_font_funcs[0].name,
514 supported_font_funcs[0].name);
515 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
517 g_string_append_c (s, '/');
518 g_string_append (s, supported_font_funcs[i].name);
520 text = g_string_free (s, FALSE);
521 parser->free_later (text);
524 char *font_size_text;
525 if (default_font_size == FONT_SIZE_UPEM)
526 font_size_text = (char *) "Font size (default: upem)";
529 font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
530 parser->free_later (font_size_text);
533 GOptionEntry entries[] =
535 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
536 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
537 {"font-size", 0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
538 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
539 {"font-ppem", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
540 {"font-ptem", 0, 0, G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
541 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
544 parser->add_group (entries,
547 "Options for the font",
550 const gchar *variations_help = "Comma-separated list of font variations\n"
552 " Variations are set globally. The format for specifying variation settings\n"
553 " follows. All valid CSS font-variation-settings values other than 'normal'\n"
554 " and 'inherited' are also accepted, though, not documented below.\n"
556 " The format is a tag, optionally followed by an equals sign, followed by a\n"
557 " number. For example:\n"
562 GOptionEntry entries2[] =
564 {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
567 parser->add_group (entries2,
569 "Variations options:",
570 "Options for font variations used",
575 text_options_t::add_options (option_parser_t *parser)
577 GOptionEntry entries[] =
579 {"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"},
580 {"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"},
581 {"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints", "list of hex numbers"},
582 {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
583 {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
586 parser->add_group (entries,
589 "Options for the input text",
594 output_options_t::add_options (option_parser_t *parser)
598 if (nullptr == supported_formats)
599 text = "Set output serialization format";
602 char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
603 text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
605 parser->free_later ((char *) text);
608 GOptionEntry entries[] =
610 {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
611 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"},
614 parser->add_group (entries,
616 "Output destination & format options:",
617 "Options for the destination & form of the output",
624 font_options_t::get_font (void) const
629 hb_blob_t *blob = nullptr;
631 /* Create the blob */
634 unsigned int len = 0;
635 hb_destroy_func_t destroy;
639 /* This is a hell of a lot of code for just reading a file! */
641 fail (true, "No font file set");
643 if (0 == strcmp (font_file, "-")) {
645 GString *gs = g_string_new (nullptr);
647 #if defined(_WIN32) || defined(__CYGWIN__)
648 setmode (fileno (stdin), O_BINARY);
650 while (!feof (stdin)) {
651 size_t ret = fread (buf, 1, sizeof (buf), stdin);
653 fail (false, "Failed reading font from standard input: %s",
655 g_string_append_len (gs, buf, ret);
658 font_data = g_string_free (gs, false);
659 user_data = font_data;
660 destroy = (hb_destroy_func_t) g_free;
661 mm = HB_MEMORY_MODE_WRITABLE;
663 GError *error = nullptr;
664 GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
666 font_data = g_mapped_file_get_contents (mf);
667 len = g_mapped_file_get_length (mf);
669 destroy = (hb_destroy_func_t) g_mapped_file_unref;
670 user_data = (void *) mf;
671 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
673 g_mapped_file_unref (mf);
675 fail (false, "%s", error->message);
676 //g_error_free (error);
679 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
681 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
682 GError *error = nullptr;
684 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
686 destroy = (hb_destroy_func_t) g_free;
687 user_data = (void *) font_data;
688 mm = HB_MEMORY_MODE_WRITABLE;
690 fail (false, "%s", error->message);
691 //g_error_free (error);
697 mm = HB_MEMORY_MODE_DUPLICATE;
699 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
702 /* Create the face */
703 hb_face_t *face = hb_face_create (blob, face_index);
704 hb_blob_destroy (blob);
707 font = hb_font_create (face);
709 if (font_size_x == FONT_SIZE_UPEM)
710 font_size_x = hb_face_get_upem (face);
711 if (font_size_y == FONT_SIZE_UPEM)
712 font_size_y = hb_face_get_upem (face);
714 hb_font_set_ppem (font, x_ppem, y_ppem);
715 hb_font_set_ptem (font, ptem);
717 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
718 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
719 hb_font_set_scale (font, scale_x, scale_y);
720 hb_face_destroy (face);
722 hb_font_set_variations (font, variations, num_variations);
724 void (*set_font_funcs) (hb_font_t *) = nullptr;
727 set_font_funcs = supported_font_funcs[0].func;
731 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
732 if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
734 set_font_funcs = supported_font_funcs[i].func;
739 GString *s = g_string_new (nullptr);
740 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
743 g_string_append_c (s, '/');
744 g_string_append (s, supported_font_funcs[i].name);
746 char *p = g_string_free (s, FALSE);
747 fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
750 supported_font_funcs[0].name);
754 set_font_funcs (font);
761 text_options_t::get_line (unsigned int *len)
764 if (!line) line = text;
765 if (line_len == (unsigned int) -1)
766 line_len = strlen (line);
773 const char *ret = line;
774 const char *p = (const char *) memchr (line, '\n', line_len);
775 unsigned int ret_len;
783 line_len -= ret_len + 1;
792 fail (true, "At least one of text or text-file must be set");
794 if (0 != strcmp (text_file, "-"))
795 fp = fopen (text_file, "r");
800 fail (false, "Failed opening text file `%s': %s",
801 text_file, strerror (errno));
803 gs = g_string_new (nullptr);
806 #ifdef HAVE_SETLINEBUF
809 setvbuf(fp, NULL, _IOLBF, BUFSIZ);
812 g_string_set_size (gs, 0);
814 while (fgets (buf, sizeof (buf), fp)) {
815 unsigned int bytes = strlen (buf);
816 if (bytes && buf[bytes - 1] == '\n') {
818 g_string_append_len (gs, buf, bytes);
821 g_string_append_len (gs, buf, bytes);
824 fail (false, "Failed reading text: %s",
827 return !*len && feof (fp) ? nullptr : gs->str;
832 output_options_t::get_file_handle (void)
838 fp = fopen (output_file, "wb");
840 #if defined(_WIN32) || defined(__CYGWIN__)
841 setmode (fileno (stdout), O_BINARY);
846 fail (false, "Cannot open output file `%s': %s",
847 g_filename_display_name (output_file), strerror (errno));
849 #ifdef HAVE_SETLINEBUF
852 setvbuf(fp, NULL, _IOLBF, BUFSIZ);
859 parse_verbose (const char *name G_GNUC_UNUSED,
860 const char *arg G_GNUC_UNUSED,
861 gpointer data G_GNUC_UNUSED,
862 GError **error G_GNUC_UNUSED)
864 format_options_t *format_opts = (format_options_t *) data;
865 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
870 parse_ned (const char *name G_GNUC_UNUSED,
871 const char *arg G_GNUC_UNUSED,
872 gpointer data G_GNUC_UNUSED,
873 GError **error G_GNUC_UNUSED)
875 format_options_t *format_opts = (format_options_t *) data;
876 format_opts->show_clusters = format_opts->show_advances = false;
881 format_options_t::add_options (option_parser_t *parser)
883 GOptionEntry entries[] =
885 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Prefix each line of output with its corresponding input text", nullptr},
886 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Prefix each line of output with its corresponding input codepoint(s)", nullptr},
887 {"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},
888 {"verbose", 'v', G_OPTION_FLAG_NO_ARG,
889 G_OPTION_ARG_CALLBACK, (gpointer) &parse_verbose, "Prefix each line of output with all of the above", nullptr},
890 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE,
891 G_OPTION_ARG_NONE, &this->show_glyph_names, "Output glyph indices instead of names", nullptr},
892 {"no-positions", 0, G_OPTION_FLAG_REVERSE,
893 G_OPTION_ARG_NONE, &this->show_positions, "Do not output glyph positions", nullptr},
894 {"no-advances", 0, G_OPTION_FLAG_REVERSE,
895 G_OPTION_ARG_NONE, &this->show_advances, "Do not output glyph advances", nullptr},
896 {"no-clusters", 0, G_OPTION_FLAG_REVERSE,
897 G_OPTION_ARG_NONE, &this->show_clusters, "Do not output cluster indices", nullptr},
898 {"show-extents", 0, 0, G_OPTION_ARG_NONE, &this->show_extents, "Output glyph extents", nullptr},
899 {"show-flags", 0, 0, G_OPTION_ARG_NONE, &this->show_flags, "Output glyph flags", nullptr},
900 {"ned", 'v', G_OPTION_FLAG_NO_ARG,
901 G_OPTION_ARG_CALLBACK, (gpointer) &parse_ned, "No Extra Data; Do not output clusters or advances", nullptr},
902 {"trace", 'V', 0, G_OPTION_ARG_NONE, &this->trace, "Output interim shaping results", nullptr},
905 parser->add_group (entries,
908 " text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
909 " 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"
910 "\nOutput syntax options:",
911 "Options for the syntax of the output",
916 format_options_t::serialize_unicode (hb_buffer_t *buffer,
919 unsigned int num_glyphs = hb_buffer_get_length (buffer);
920 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
922 g_string_append_c (gs, '<');
923 for (unsigned int i = 0; i < num_glyphs; i++)
926 g_string_append_c (gs, ',');
927 g_string_append_printf (gs, "U+%04X", info->codepoint);
930 g_string_append_c (gs, '>');
934 format_options_t::serialize_glyphs (hb_buffer_t *buffer,
936 hb_buffer_serialize_format_t output_format,
937 hb_buffer_serialize_flags_t flags,
940 g_string_append_c (gs, '[');
941 unsigned int num_glyphs = hb_buffer_get_length (buffer);
942 unsigned int start = 0;
944 while (start < num_glyphs)
947 unsigned int consumed;
948 start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
949 buf, sizeof (buf), &consumed,
950 font, output_format, flags);
953 g_string_append (gs, buf);
955 g_string_append_c (gs, ']');
958 format_options_t::serialize_line_no (unsigned int line_no,
962 g_string_append_printf (gs, "%d: ", line_no);
965 format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
966 unsigned int line_no,
968 unsigned int text_len,
974 serialize_line_no (line_no, gs);
975 g_string_append_c (gs, '(');
976 g_string_append_len (gs, text, text_len);
977 g_string_append_c (gs, ')');
978 g_string_append_c (gs, '\n');
983 serialize_line_no (line_no, gs);
984 serialize_unicode (buffer, gs);
985 g_string_append_c (gs, '\n');
989 format_options_t::serialize_message (unsigned int line_no,
994 serialize_line_no (line_no, gs);
995 g_string_append_printf (gs, "%s: %s", type, msg);
996 g_string_append_c (gs, '\n');
999 format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
1000 unsigned int line_no,
1002 unsigned int text_len,
1004 hb_buffer_serialize_format_t output_format,
1005 hb_buffer_serialize_flags_t format_flags,
1008 serialize_line_no (line_no, gs);
1009 serialize_glyphs (buffer, font, output_format, format_flags, gs);
1010 g_string_append_c (gs, '\n');
1014 subset_options_t::add_options (option_parser_t *parser)
1016 GOptionEntry entries[] =
1018 {"no-hinting", 0, 0, G_OPTION_ARG_NONE, &this->drop_hints, "Whether to drop hints", nullptr},
1021 parser->add_group (entries,
1024 "Options subsetting",