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 #define DELIMITERS "<+>{},;&#\\xXuUnNiI\n\t\v\f\r "
36 static struct supported_font_funcs_t {
38 void (*func) (hb_font_t *);
39 } supported_font_funcs[] =
42 {"ft", hb_ft_font_set_funcs},
44 {"ot", hb_ot_font_set_funcs},
49 fail (hb_bool_t suggest_help, const char *format, ...)
54 va_start (vap, format);
55 msg = g_strdup_vprintf (format, vap);
57 const char *prgname = g_get_prgname ();
58 g_printerr ("%s: %s\n", prgname, msg);
60 g_printerr ("Try `%s --help' for more information.\n", prgname);
69 GString *shapers = g_string_new (nullptr);
70 const char **shaper_list = hb_shape_list_shapers ();
72 for (; *shaper_list; shaper_list++) {
73 g_string_append (shapers, *shaper_list);
74 g_string_append_c (shapers, ',');
76 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
78 return g_string_free (shapers, false);
81 static G_GNUC_NORETURN gboolean
82 show_version (const char *name G_GNUC_UNUSED,
83 const char *arg G_GNUC_UNUSED,
84 gpointer data G_GNUC_UNUSED,
85 GError **error G_GNUC_UNUSED)
87 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
89 char *shapers = shapers_to_string ();
90 g_printf ("Available shapers: %s\n", shapers);
92 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
93 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
100 option_parser_t::add_main_options ()
102 GOptionEntry entries[] =
104 {"version", 0, G_OPTION_FLAG_NO_ARG,
105 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
108 g_option_context_add_main_entries (context, entries, nullptr);
112 pre_parse (GOptionContext *context G_GNUC_UNUSED,
113 GOptionGroup *group G_GNUC_UNUSED,
117 option_group_t *option_group = (option_group_t *) data;
118 option_group->pre_parse (error);
123 post_parse (GOptionContext *context G_GNUC_UNUSED,
124 GOptionGroup *group G_GNUC_UNUSED,
128 option_group_t *option_group = static_cast<option_group_t *>(data);
129 option_group->post_parse (error);
134 option_parser_t::add_group (GOptionEntry *entries,
136 const gchar *description,
137 const gchar *help_description,
138 option_group_t *option_group)
140 GOptionGroup *group = g_option_group_new (name, description, help_description,
141 static_cast<gpointer>(option_group), nullptr);
142 g_option_group_add_entries (group, entries);
143 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
144 g_option_context_add_group (context, group);
148 option_parser_t::parse (int *argc, char ***argv)
150 setlocale (LC_ALL, "");
152 GError *parse_error = nullptr;
153 if (!g_option_context_parse (context, argc, argv, &parse_error))
157 fail (true, "%s", parse_error->message);
158 //g_error_free (parse_error);
161 fail (true, "Option parse error");
167 parse_font_extents (const char *name G_GNUC_UNUSED,
170 GError **error G_GNUC_UNUSED)
172 view_options_t *view_opts = (view_options_t *) data;
173 view_options_t::font_extents_t &e = view_opts->font_extents;
174 switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf", &e.ascent, &e.descent, &e.line_gap)) {
175 case 1: HB_FALLTHROUGH;
176 case 2: HB_FALLTHROUGH;
178 view_opts->have_font_extents = true;
181 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
182 "%s argument should be one to three space-separated numbers",
189 parse_margin (const char *name G_GNUC_UNUSED,
192 GError **error G_GNUC_UNUSED)
194 view_options_t *view_opts = (view_options_t *) data;
195 view_options_t::margin_t &m = view_opts->margin;
196 switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf", &m.t, &m.r, &m.b, &m.l)) {
197 case 1: m.r = m.t; HB_FALLTHROUGH;
198 case 2: m.b = m.t; HB_FALLTHROUGH;
199 case 3: m.l = m.r; HB_FALLTHROUGH;
202 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
203 "%s argument should be one to four space-separated numbers",
211 parse_shapers (const char *name G_GNUC_UNUSED,
216 shape_options_t *shape_opts = (shape_options_t *) data;
217 char **shapers = g_strsplit (arg, ",", 0);
219 for (char **shaper = shapers; *shaper; shaper++) {
221 for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
222 if (strcmp (*shaper, *hb_shaper) == 0) {
228 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
229 "Unknown or unsupported shaper: %s", *shaper);
230 g_strfreev (shapers);
235 g_strfreev (shape_opts->shapers);
236 shape_opts->shapers = shapers;
240 static G_GNUC_NORETURN gboolean
241 list_shapers (const char *name G_GNUC_UNUSED,
242 const char *arg G_GNUC_UNUSED,
243 gpointer data G_GNUC_UNUSED,
244 GError **error G_GNUC_UNUSED)
246 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
247 g_printf ("%s\n", *shaper);
254 parse_features (const char *name G_GNUC_UNUSED,
257 GError **error G_GNUC_UNUSED)
259 shape_options_t *shape_opts = (shape_options_t *) data;
260 char *s = (char *) arg;
263 shape_opts->num_features = 0;
264 g_free (shape_opts->features);
265 shape_opts->features = nullptr;
270 /* count the features first, so we can allocate memory */
273 shape_opts->num_features++;
279 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
280 if (!shape_opts->features)
283 /* now do the actual parsing */
285 shape_opts->num_features = 0;
287 char *end = strchr (p, ',');
288 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
289 shape_opts->num_features++;
290 p = end ? end + 1 : nullptr;
297 parse_variations (const char *name G_GNUC_UNUSED,
300 GError **error G_GNUC_UNUSED)
302 font_options_t *font_opts = (font_options_t *) data;
303 char *s = (char *) arg;
306 font_opts->num_variations = 0;
307 g_free (font_opts->variations);
308 font_opts->variations = nullptr;
313 /* count the variations first, so we can allocate memory */
316 font_opts->num_variations++;
322 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
323 if (!font_opts->variations)
326 /* now do the actual parsing */
328 font_opts->num_variations = 0;
330 char *end = strchr (p, ',');
331 if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
332 font_opts->num_variations++;
333 p = end ? end + 1 : nullptr;
340 parse_text (const char *name G_GNUC_UNUSED,
343 GError **error G_GNUC_UNUSED)
345 text_options_t *text_opts = (text_options_t *) data;
349 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
350 "Either --text or --unicodes can be provided but not both");
354 text_opts->text_len = -1;
355 text_opts->text = g_strdup (arg);
361 parse_unicodes (const char *name G_GNUC_UNUSED,
364 GError **error G_GNUC_UNUSED)
366 text_options_t *text_opts = (text_options_t *) data;
370 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
371 "Either --text or --unicodes can be provided but not both");
375 GString *gs = g_string_new (nullptr);
376 if (0 == strcmp (arg, "*"))
378 g_string_append_c (gs, '*');
383 char *s = (char *) arg;
388 while (*s && strchr (DELIMITERS, *s))
394 hb_codepoint_t u = strtoul (s, &p, 16);
397 g_string_free (gs, TRUE);
398 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
399 "Failed parsing Unicode values at: '%s'", s);
403 g_string_append_unichar (gs, u);
409 text_opts->text_len = gs->len;
410 text_opts->text = g_string_free (gs, FALSE);
416 view_options_t::add_options (option_parser_t *parser)
418 GOptionEntry entries[] =
420 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", nullptr},
421 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"},
422 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"},
423 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
424 {"font-extents", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_extents, "Set font ascent/descent/line-gap (default: auto)","one to three numbers"},
425 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
428 parser->add_group (entries,
431 "Options for output rendering",
436 shape_options_t::add_options (option_parser_t *parser)
438 GOptionEntry entries[] =
440 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
441 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
442 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
443 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
444 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
445 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
446 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
447 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
448 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
449 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
450 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
451 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
452 {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr},
453 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
454 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
455 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
456 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
457 {"num-iterations", 'n', 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
460 parser->add_group (entries,
463 "Options for the shaping process",
466 const gchar *features_help = "Comma-separated list of font features\n"
468 " Features can be enabled or disabled, either globally or limited to\n"
469 " specific character ranges. The format for specifying feature settings\n"
470 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
471 " and the global values are also accepted, though not documented below.\n"
472 " CSS string escapes are not supported."
474 " The range indices refer to the positions between Unicode characters,\n"
475 " unless the --utf8-clusters is provided, in which case range indices\n"
476 " refer to UTF-8 byte indices. The position before the first character\n"
479 " The format is Python-esque. Here is how it all works:\n"
481 " Syntax: Value: Start: End:\n"
484 " \"kern\" 1 0 ∞ # Turn feature on\n"
485 " \"+kern\" 1 0 ∞ # Turn feature on\n"
486 " \"-kern\" 0 0 ∞ # Turn feature off\n"
487 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
488 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
489 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
492 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
493 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
494 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
495 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
496 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
497 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
501 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
503 GOptionEntry entries2[] =
505 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
508 parser->add_group (entries2,
511 "Options for font features used",
516 parse_font_size (const char *name G_GNUC_UNUSED,
519 GError **error G_GNUC_UNUSED)
521 font_options_t *font_opts = (font_options_t *) data;
522 if (0 == strcmp (arg, "upem"))
524 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
527 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
528 case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
531 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
532 "%s argument should be one or two space-separated numbers",
539 parse_font_ppem (const char *name G_GNUC_UNUSED,
542 GError **error G_GNUC_UNUSED)
544 font_options_t *font_opts = (font_options_t *) data;
545 switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
546 case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
549 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
550 "%s argument should be one or two space-separated numbers",
557 font_options_t::add_options (option_parser_t *parser)
559 char *text = nullptr;
562 static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
563 "No supported font-funcs found.");
564 GString *s = g_string_new (nullptr);
565 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
566 supported_font_funcs[0].name,
567 supported_font_funcs[0].name);
568 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
570 g_string_append_c (s, '/');
571 g_string_append (s, supported_font_funcs[i].name);
573 text = g_string_free (s, FALSE);
574 parser->free_later (text);
577 char *font_size_text;
578 if (default_font_size == FONT_SIZE_UPEM)
579 font_size_text = (char *) "Font size (default: upem)";
582 font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
583 parser->free_later (font_size_text);
586 GOptionEntry entries[] =
588 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
589 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
590 {"font-size", 0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
591 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
592 {"font-ppem", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
593 {"font-ptem", 0, 0, G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
594 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
595 {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
598 parser->add_group (entries,
601 "Options for the font",
604 const gchar *variations_help = "Comma-separated list of font variations\n"
606 " Variations are set globally. The format for specifying variation settings\n"
607 " follows. All valid CSS font-variation-settings values other than 'normal'\n"
608 " and 'inherited' are also accepted, though, not documented below.\n"
610 " The format is a tag, optionally followed by an equals sign, followed by a\n"
611 " number. For example:\n"
616 GOptionEntry entries2[] =
618 {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
621 parser->add_group (entries2,
623 "Variations options:",
624 "Options for font variations used",
629 text_options_t::add_options (option_parser_t *parser)
631 GOptionEntry entries[] =
633 {"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"},
634 {"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"},
635 {"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints", "list of hex numbers"},
636 {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
637 {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
640 parser->add_group (entries,
643 "Options for the input text",
648 output_options_t::add_options (option_parser_t *parser)
652 if (!supported_formats)
653 text = "Set output serialization format";
656 char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
657 text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
659 parser->free_later ((char *) text);
662 GOptionEntry entries[] =
664 {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
665 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"},
668 parser->add_group (entries,
670 "Output destination & format options:",
671 "Options for the destination & form of the output",
678 font_options_t::get_font () const
683 /* Create the blob */
685 fail (true, "No font file set");
687 const char *font_path = font_file;
689 if (0 == strcmp (font_path, "-"))
691 #if defined(_WIN32) || defined(__CYGWIN__)
692 setmode (fileno (stdin), O_BINARY);
695 font_path = "/dev/stdin";
699 blob = hb_blob_create_from_file (font_path);
701 if (blob == hb_blob_get_empty ())
702 fail (false, "Couldn't read or find %s, or it was empty.", font_path);
704 /* Create the face */
705 hb_face_t *face = hb_face_create (blob, face_index);
706 hb_blob_destroy (blob);
709 font = hb_font_create (face);
711 if (font_size_x == FONT_SIZE_UPEM)
712 font_size_x = hb_face_get_upem (face);
713 if (font_size_y == FONT_SIZE_UPEM)
714 font_size_y = hb_face_get_upem (face);
716 hb_font_set_ppem (font, x_ppem, y_ppem);
717 hb_font_set_ptem (font, ptem);
719 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
720 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
721 hb_font_set_scale (font, scale_x, scale_y);
722 hb_face_destroy (face);
724 hb_font_set_variations (font, variations, num_variations);
726 void (*set_font_funcs) (hb_font_t *) = nullptr;
729 set_font_funcs = supported_font_funcs[0].func;
733 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
734 if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
736 set_font_funcs = supported_font_funcs[i].func;
741 GString *s = g_string_new (nullptr);
742 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
745 g_string_append_c (s, '/');
746 g_string_append (s, supported_font_funcs[i].name);
748 char *p = g_string_free (s, FALSE);
749 fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
752 supported_font_funcs[0].name);
756 set_font_funcs (font);
758 hb_ft_font_set_load_flags (font, ft_load_flags);
766 text_options_t::get_line (unsigned int *len)
774 if (line_len == UINT_MAX)
775 line_len = strlen (line);
782 const char *ret = line;
783 const char *p = (const char *) memchr (line, '\n', line_len);
784 unsigned int ret_len;
792 line_len -= ret_len + 1;
801 fail (true, "At least one of text or text-file must be set");
803 if (0 != strcmp (text_file, "-"))
804 fp = fopen (text_file, "r");
809 fail (false, "Failed opening text file `%s': %s",
810 text_file, strerror (errno));
812 gs = g_string_new (nullptr);
815 g_string_set_size (gs, 0);
817 while (fgets (buf, sizeof (buf), fp)) {
818 unsigned int bytes = strlen (buf);
819 if (bytes && buf[bytes - 1] == '\n') {
821 g_string_append_len (gs, buf, bytes);
824 g_string_append_len (gs, buf, bytes);
827 fail (false, "Failed reading text: %s",
830 return !*len && feof (fp) ? nullptr : gs->str;
835 output_options_t::get_file_handle ()
841 fp = fopen (output_file, "wb");
843 #if defined(_WIN32) || defined(__CYGWIN__)
844 setmode (fileno (stdout), O_BINARY);
849 fail (false, "Cannot open output file `%s': %s",
850 g_filename_display_name (output_file), strerror (errno));
856 parse_verbose (const char *name G_GNUC_UNUSED,
857 const char *arg G_GNUC_UNUSED,
858 gpointer data G_GNUC_UNUSED,
859 GError **error G_GNUC_UNUSED)
861 format_options_t *format_opts = (format_options_t *) data;
862 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
867 parse_ned (const char *name G_GNUC_UNUSED,
868 const char *arg G_GNUC_UNUSED,
869 gpointer data G_GNUC_UNUSED,
870 GError **error G_GNUC_UNUSED)
872 format_options_t *format_opts = (format_options_t *) data;
873 format_opts->show_clusters = format_opts->show_advances = false;
878 format_options_t::add_options (option_parser_t *parser)
880 GOptionEntry entries[] =
882 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Prefix each line of output with its corresponding input text", nullptr},
883 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Prefix each line of output with its corresponding input codepoint(s)", nullptr},
884 {"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},
885 {"verbose", 'v', G_OPTION_FLAG_NO_ARG,
886 G_OPTION_ARG_CALLBACK, (gpointer) &parse_verbose, "Prefix each line of output with all of the above", nullptr},
887 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE,
888 G_OPTION_ARG_NONE, &this->show_glyph_names, "Output glyph indices instead of names", nullptr},
889 {"no-positions", 0, G_OPTION_FLAG_REVERSE,
890 G_OPTION_ARG_NONE, &this->show_positions, "Do not output glyph positions", nullptr},
891 {"no-advances", 0, G_OPTION_FLAG_REVERSE,
892 G_OPTION_ARG_NONE, &this->show_advances, "Do not output glyph advances", nullptr},
893 {"no-clusters", 0, G_OPTION_FLAG_REVERSE,
894 G_OPTION_ARG_NONE, &this->show_clusters, "Do not output cluster indices", nullptr},
895 {"show-extents", 0, 0, G_OPTION_ARG_NONE, &this->show_extents, "Output glyph extents", nullptr},
896 {"show-flags", 0, 0, G_OPTION_ARG_NONE, &this->show_flags, "Output glyph flags", nullptr},
897 {"ned", 'v', G_OPTION_FLAG_NO_ARG,
898 G_OPTION_ARG_CALLBACK, (gpointer) &parse_ned, "No Extra Data; Do not output clusters or advances", nullptr},
899 {"trace", 'V', 0, G_OPTION_ARG_NONE, &this->trace, "Output interim shaping results", nullptr},
902 parser->add_group (entries,
905 " text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
906 " 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"
907 "\nOutput syntax options:",
908 "Options for the syntax of the output",
913 format_options_t::serialize_unicode (hb_buffer_t *buffer,
916 unsigned int num_glyphs = hb_buffer_get_length (buffer);
917 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
919 g_string_append_c (gs, '<');
920 for (unsigned int i = 0; i < num_glyphs; i++)
923 g_string_append_c (gs, ',');
924 g_string_append_printf (gs, "U+%04X", info->codepoint);
927 g_string_append_c (gs, '>');
931 format_options_t::serialize_glyphs (hb_buffer_t *buffer,
933 hb_buffer_serialize_format_t output_format,
934 hb_buffer_serialize_flags_t flags,
937 g_string_append_c (gs, '[');
938 unsigned int num_glyphs = hb_buffer_get_length (buffer);
939 unsigned int start = 0;
941 while (start < num_glyphs)
944 unsigned int consumed;
945 start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
946 buf, sizeof (buf), &consumed,
947 font, output_format, flags);
950 g_string_append (gs, buf);
952 g_string_append_c (gs, ']');
955 format_options_t::serialize_line_no (unsigned int line_no,
959 g_string_append_printf (gs, "%d: ", line_no);
962 format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
963 unsigned int line_no,
965 unsigned int text_len,
971 serialize_line_no (line_no, gs);
972 g_string_append_c (gs, '(');
973 g_string_append_len (gs, text, text_len);
974 g_string_append_c (gs, ')');
975 g_string_append_c (gs, '\n');
980 serialize_line_no (line_no, gs);
981 serialize_unicode (buffer, gs);
982 g_string_append_c (gs, '\n');
986 format_options_t::serialize_message (unsigned int line_no,
991 serialize_line_no (line_no, gs);
992 g_string_append_printf (gs, "%s: %s", type, msg);
993 g_string_append_c (gs, '\n');
996 format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
997 unsigned int line_no,
999 unsigned int text_len,
1001 hb_buffer_serialize_format_t output_format,
1002 hb_buffer_serialize_flags_t format_flags,
1005 serialize_line_no (line_no, gs);
1006 serialize_glyphs (buffer, font, output_format, format_flags, gs);
1007 g_string_append_c (gs, '\n');