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
35 fail (hb_bool_t suggest_help, const char *format, ...)
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);
45 g_printerr ("Try `%s --help' for more information.\n", prgname);
51 hb_bool_t debug = false;
54 shapers_to_string (void)
56 GString *shapers = g_string_new (NULL);
57 const char **shaper_list = hb_shape_list_shapers ();
59 for (; *shaper_list; shaper_list++) {
60 g_string_append (shapers, *shaper_list);
61 g_string_append_c (shapers, ',');
63 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
65 return g_string_free (shapers, false);
68 static G_GNUC_NORETURN gboolean
69 show_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)
74 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
76 char *shapers = shapers_to_string ();
77 g_printf ("Available shapers: %s\n", 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 ());
87 option_parser_t::add_main_options (void)
89 GOptionEntry entries[] =
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},
96 g_option_context_add_main_entries (context, entries, NULL);
100 pre_parse (GOptionContext *context G_GNUC_UNUSED,
101 GOptionGroup *group G_GNUC_UNUSED,
105 option_group_t *option_group = (option_group_t *) data;
106 option_group->pre_parse (error);
107 return *error == NULL;
111 post_parse (GOptionContext *context G_GNUC_UNUSED,
112 GOptionGroup *group G_GNUC_UNUSED,
116 option_group_t *option_group = static_cast<option_group_t *>(data);
117 option_group->post_parse (error);
118 return *error == NULL;
122 option_parser_t::add_group (GOptionEntry *entries,
124 const gchar *description,
125 const gchar *help_description,
126 option_group_t *option_group)
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);
136 option_parser_t::parse (int *argc, char ***argv)
138 setlocale (LC_ALL, "");
140 GError *parse_error = NULL;
141 if (!g_option_context_parse (context, argc, argv, &parse_error))
143 if (parse_error != NULL) {
144 fail (true, "%s", parse_error->message);
145 //g_error_free (parse_error);
147 fail (true, "Option parse error");
153 parse_margin (const char *name G_GNUC_UNUSED,
156 GError **error G_GNUC_UNUSED)
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)) {
166 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
167 "%s argument should be one to four space-separated numbers",
175 parse_shapers (const char *name G_GNUC_UNUSED,
178 GError **error G_GNUC_UNUSED)
180 shape_options_t *shape_opts = (shape_options_t *) data;
181 g_strfreev (shape_opts->shapers);
182 shape_opts->shapers = g_strsplit (arg, ",", 0);
186 static G_GNUC_NORETURN gboolean
187 list_shapers (const char *name G_GNUC_UNUSED,
188 const char *arg G_GNUC_UNUSED,
189 gpointer data G_GNUC_UNUSED,
190 GError **error G_GNUC_UNUSED)
192 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
193 g_printf ("%s\n", *shaper);
201 parse_space (char **pp)
204 #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
205 while (c = **pp, ISSPACE (c))
211 parse_char (char **pp, char c)
223 parse_uint (char **pp, unsigned int *pv)
228 v = strtol (p, pp, 0);
239 parse_feature_value_prefix (char **pp, hb_feature_t *feature)
241 if (parse_char (pp, '-'))
244 parse_char (pp, '+');
252 parse_feature_tag (char **pp, hb_feature_t *feature)
258 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
259 while (c = **pp, ISALNUM(c))
266 feature->tag = hb_tag_from_string (p, *pp - p);
271 parse_feature_indices (char **pp, hb_feature_t *feature)
278 feature->end = (unsigned int) -1;
280 if (!parse_char (pp, '['))
283 has_start = parse_uint (pp, &feature->start);
285 if (parse_char (pp, ':')) {
286 parse_uint (pp, &feature->end);
289 feature->end = feature->start + 1;
292 return parse_char (pp, ']');
296 parse_feature_value_postfix (char **pp, hb_feature_t *feature)
298 return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
303 parse_one_feature (char **pp, hb_feature_t *feature)
305 return parse_feature_value_prefix (pp, feature) &&
306 parse_feature_tag (pp, feature) &&
307 parse_feature_indices (pp, feature) &&
308 parse_feature_value_postfix (pp, feature) &&
309 (parse_char (pp, ',') || **pp == '\0');
313 skip_one_feature (char **pp)
316 e = strchr (*pp, ',');
320 *pp = *pp + strlen (*pp);
324 parse_features (const char *name G_GNUC_UNUSED,
327 GError **error G_GNUC_UNUSED)
329 shape_options_t *shape_opts = (shape_options_t *) data;
330 char *s = (char *) arg;
333 shape_opts->num_features = 0;
334 g_free (shape_opts->features);
335 shape_opts->features = NULL;
340 /* count the features first, so we can allocate memory */
343 shape_opts->num_features++;
349 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
351 /* now do the actual parsing */
353 shape_opts->num_features = 0;
355 if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features]))
356 shape_opts->num_features++;
358 skip_one_feature (&p);
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", NULL},
371 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"},
372 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#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"},
375 {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &this->font_size, "Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"},
378 parser->add_group (entries,
381 "Options controlling output rendering",
386 shape_options_t::add_options (option_parser_t *parser)
388 GOptionEntry entries[] =
390 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
391 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL},
392 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
393 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL},
394 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers to try","list"},
395 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
396 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
397 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
398 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", NULL},
399 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", NULL},
402 parser->add_group (entries,
405 "Options controlling the shaping process",
408 const gchar *features_help = "Comma-separated list of font features\n"
410 " Features can be enabled or disabled, either globally or limited to\n"
411 " specific character ranges.\n"
413 " The range indices refer to the positions between Unicode characters,\n"
414 " unless the --utf8-clusters is provided, in which case range indices\n"
415 " refer to UTF-8 byte indices. The position before the first character\n"
418 " The format is Python-esque. Here is how it all works:\n"
420 " Syntax: Value: Start: End:\n"
423 " \"kern\" 1 0 ∞ # Turn feature on\n"
424 " \"+kern\" 1 0 ∞ # Turn feature on\n"
425 " \"-kern\" 0 0 ∞ # Turn feature off\n"
426 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
427 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
428 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
431 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
432 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
433 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
434 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
435 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
436 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
440 " \"kern[3:5]=0\" 1 3 5 # Turn feature off for range";
442 GOptionEntry entries2[] =
444 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
447 parser->add_group (entries2,
450 "Options controlling font features used",
455 font_options_t::add_options (option_parser_t *parser)
457 GOptionEntry entries[] =
459 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"},
460 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"},
463 parser->add_group (entries,
466 "Options controlling the font",
471 text_options_t::add_options (option_parser_t *parser)
473 GOptionEntry entries[] =
475 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"},
476 {"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.", "filename"},
479 parser->add_group (entries,
482 "Options controlling the input text",
487 output_options_t::add_options (option_parser_t *parser)
489 GOptionEntry entries[] =
491 {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
492 {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, "Set output format", "format"},
495 parser->add_group (entries,
498 "Options controlling the output",
505 font_options_t::get_font (void) const
510 hb_blob_t *blob = NULL;
512 /* Create the blob */
515 unsigned int len = 0;
516 hb_destroy_func_t destroy;
520 /* This is a hell of a lot of code for just reading a file! */
522 fail (true, "No font file set");
524 if (0 == strcmp (font_file, "-")) {
526 GString *gs = g_string_new (NULL);
529 _setmode (fileno (stdin), _O_BINARY);
531 while (!feof (stdin)) {
532 size_t ret = fread (buf, 1, sizeof (buf), stdin);
534 fail (false, "Failed reading font from standard input: %s",
536 g_string_append_len (gs, buf, ret);
539 font_data = g_string_free (gs, false);
540 user_data = font_data;
541 destroy = (hb_destroy_func_t) g_free;
542 mm = HB_MEMORY_MODE_WRITABLE;
544 GError *error = NULL;
545 GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
547 font_data = g_mapped_file_get_contents (mf);
548 len = g_mapped_file_get_length (mf);
550 destroy = (hb_destroy_func_t) g_mapped_file_unref;
551 user_data = (void *) mf;
552 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
554 g_mapped_file_unref (mf);
556 fail (false, "%s", error->message);
557 //g_error_free (error);
560 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
562 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
563 GError *error = NULL;
565 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
567 destroy = (hb_destroy_func_t) g_free;
568 user_data = (void *) font_data;
569 mm = HB_MEMORY_MODE_WRITABLE;
571 fail (false, "%s", error->message);
572 //g_error_free (error);
577 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
580 /* Create the face */
581 hb_face_t *face = hb_face_create (blob, face_index);
582 hb_blob_destroy (blob);
585 font = hb_font_create (face);
587 unsigned int upem = hb_face_get_upem (face);
588 hb_font_set_scale (font, upem, upem);
589 hb_face_destroy (face);
592 hb_ft_font_set_funcs (font);
600 text_options_t::get_line (unsigned int *len)
603 if (text_len == (unsigned int) -1)
604 text_len = strlen (text);
611 const char *ret = text;
612 const char *p = (const char *) memchr (text, '\n', text_len);
613 unsigned int ret_len;
621 text_len -= ret_len + 1;
630 fail (true, "At least one of text or text-file must be set");
632 if (0 != strcmp (text_file, "-"))
633 fp = fopen (text_file, "r");
638 fail (false, "Failed opening text file `%s': %s",
639 text_file, strerror (errno));
641 gs = g_string_new (NULL);
644 g_string_set_size (gs, 0);
646 while (fgets (buf, sizeof (buf), fp)) {
647 unsigned int bytes = strlen (buf);
648 if (bytes && buf[bytes - 1] == '\n') {
650 g_string_append_len (gs, buf, bytes);
653 g_string_append_len (gs, buf, bytes);
656 fail (false, "Failed reading text: %s",
659 return !*len && feof (fp) ? NULL : gs->str;
664 output_options_t::get_file_handle (void)
670 fp = fopen (output_file, "wb");
673 _setmode (fileno (stdout), _O_BINARY);
678 fail (false, "Cannot open output file `%s': %s",
679 g_filename_display_name (output_file), strerror (errno));
685 parse_verbose (const char *name G_GNUC_UNUSED,
686 const char *arg G_GNUC_UNUSED,
687 gpointer data G_GNUC_UNUSED,
688 GError **error G_GNUC_UNUSED)
690 format_options_t *format_opts = (format_options_t *) data;
691 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
696 format_options_t::add_options (option_parser_t *parser)
698 GOptionEntry entries[] =
700 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL},
701 {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL},
702 {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL},
703 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL},
704 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL},
705 {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL},
706 {"verbose", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose, "Show everything", NULL},
709 parser->add_group (entries,
712 "Options controlling the formatting of buffer contents",
717 format_options_t::serialize_unicode (hb_buffer_t *buffer,
720 unsigned int num_glyphs = hb_buffer_get_length (buffer);
721 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
723 g_string_append_c (gs, '<');
724 for (unsigned int i = 0; i < num_glyphs; i++)
727 g_string_append_c (gs, ',');
728 g_string_append_printf (gs, "U+%04X", info->codepoint);
731 g_string_append_c (gs, '>');
735 format_options_t::serialize_glyphs (hb_buffer_t *buffer,
737 hb_bool_t utf8_clusters,
740 unsigned int num_glyphs = hb_buffer_get_length (buffer);
741 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
742 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
744 g_string_append_c (gs, '[');
745 for (unsigned int i = 0; i < num_glyphs; i++)
748 g_string_append_c (gs, '|');
750 char glyph_name[128];
751 if (show_glyph_names) {
752 hb_font_glyph_to_string (font, info->codepoint, glyph_name, sizeof (glyph_name));
753 g_string_append_printf (gs, "%s", glyph_name);
755 g_string_append_printf (gs, "%u", info->codepoint);
758 g_string_append_printf (gs, "=%u", info->cluster);
760 g_string_append (gs, "u8");
763 if (show_positions && (pos->x_offset || pos->y_offset)) {
764 g_string_append_c (gs, '@');
765 if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset);
766 if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset);
768 if (show_positions && (pos->x_advance || pos->y_advance)) {
769 g_string_append_c (gs, '+');
770 if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance);
771 if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance);
777 g_string_append_c (gs, ']');
780 format_options_t::serialize_line_no (unsigned int line_no,
784 g_string_append_printf (gs, "%d: ", line_no);
787 format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
788 unsigned int line_no,
790 unsigned int text_len,
792 hb_bool_t utf8_clusters,
796 serialize_line_no (line_no, gs);
797 g_string_append_c (gs, '(');
798 g_string_append_len (gs, text, text_len);
799 g_string_append_c (gs, ')');
800 g_string_append_c (gs, '\n');
804 serialize_line_no (line_no, gs);
805 serialize_unicode (buffer, gs);
806 g_string_append_c (gs, '\n');
810 format_options_t::serialize_message (unsigned int line_no,
814 serialize_line_no (line_no, gs);
815 g_string_append_printf (gs, "%s", msg);
816 g_string_append_c (gs, '\n');
819 format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
820 unsigned int line_no,
822 unsigned int text_len,
824 hb_bool_t utf8_clusters,
827 serialize_line_no (line_no, gs);
828 serialize_glyphs (buffer, font, utf8_clusters, gs);
829 g_string_append_c (gs, '\n');