2 * Copyright © 2011 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
27 #ifndef SHAPE_OPTIONS_HH
28 #define SHAPE_OPTIONS_HH
32 struct shape_options_t
43 void add_options (option_parser_t *parser);
45 void setup_buffer (hb_buffer_t *buffer)
47 hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
48 hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
49 hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
50 hb_buffer_set_flags (buffer, (hb_buffer_flags_t)
51 (HB_BUFFER_FLAG_DEFAULT |
52 (bot ? HB_BUFFER_FLAG_BOT : 0) |
53 (eot ? HB_BUFFER_FLAG_EOT : 0) |
54 (verify ? HB_BUFFER_FLAG_VERIFY : 0) |
55 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
56 (remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
58 hb_buffer_set_invisible_glyph (buffer, invisible_glyph);
59 hb_buffer_set_not_found_glyph (buffer, not_found_glyph);
60 hb_buffer_set_cluster_level (buffer, cluster_level);
61 hb_buffer_guess_segment_properties (buffer);
64 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
65 const char *text_before, const char *text_after)
67 hb_buffer_clear_contents (buffer);
69 unsigned int len = strlen (text_before);
70 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
72 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
74 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
78 /* Reset cluster values to refer to Unicode character index
79 * instead of UTF-8 index. */
80 unsigned int num_glyphs = hb_buffer_get_length (buffer);
81 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
82 for (unsigned int i = 0; i < num_glyphs; i++)
89 setup_buffer (buffer);
92 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
94 if (!hb_shape_full (font, buffer, features, num_features, shapers))
97 *error = "Shaping failed.";
101 if (normalize_glyphs)
102 hb_buffer_normalize_glyphs (buffer);
110 void shape_closure (const char *text, int text_len,
111 hb_font_t *font, hb_buffer_t *buffer,
114 hb_buffer_reset (buffer);
115 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
116 setup_buffer (buffer);
117 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
120 /* Buffer properties */
121 char *direction = nullptr;
122 char *language = nullptr;
123 char *script = nullptr;
126 hb_bool_t bot = false;
127 hb_bool_t eot = false;
128 hb_bool_t preserve_default_ignorables = false;
129 hb_bool_t remove_default_ignorables = false;
131 hb_feature_t *features = nullptr;
132 unsigned int num_features = 0;
133 char **shapers = nullptr;
134 hb_bool_t utf8_clusters = false;
135 hb_codepoint_t invisible_glyph = 0;
136 hb_codepoint_t not_found_glyph = 0;
137 hb_buffer_cluster_level_t cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
138 hb_bool_t normalize_glyphs = false;
139 hb_bool_t verify = false;
140 unsigned int num_iterations = 1;
145 parse_shapers (const char *name G_GNUC_UNUSED,
150 shape_options_t *shape_opts = (shape_options_t *) data;
151 char **shapers = g_strsplit (arg, ",", 0);
153 for (char **shaper = shapers; *shaper; shaper++)
156 for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
157 if (strcmp (*shaper, *hb_shaper) == 0)
165 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
166 "Unknown or unsupported shaper: %s", *shaper);
167 g_strfreev (shapers);
172 g_strfreev (shape_opts->shapers);
173 shape_opts->shapers = shapers;
177 static G_GNUC_NORETURN gboolean
178 list_shapers (const char *name G_GNUC_UNUSED,
179 const char *arg G_GNUC_UNUSED,
180 gpointer data G_GNUC_UNUSED,
181 GError **error G_GNUC_UNUSED)
183 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
184 g_printf ("%s\n", *shaper);
191 parse_features (const char *name G_GNUC_UNUSED,
194 GError **error G_GNUC_UNUSED)
196 shape_options_t *shape_opts = (shape_options_t *) data;
197 char *s = (char *) arg;
198 size_t l = strlen (s);
201 shape_opts->num_features = 0;
202 g_free (shape_opts->features);
203 shape_opts->features = nullptr;
205 /* if the string is quoted, strip the quotes */
206 if (s[0] == s[l - 1] && (s[0] == '\"' || s[0] == '\''))
215 /* count the features first, so we can allocate memory */
218 shape_opts->num_features++;
224 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
225 if (!shape_opts->features)
228 /* now do the actual parsing */
230 shape_opts->num_features = 0;
232 char *end = strchr (p, ',');
233 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
234 shape_opts->num_features++;
235 p = end ? end + 1 : nullptr;
242 shape_options_t::add_options (option_parser_t *parser)
244 GOptionEntry entries[] =
246 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
247 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
248 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
249 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
250 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
251 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
252 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "BCP 47 tag"},
253 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
254 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
255 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
256 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
257 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
258 {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr},
259 {"not-found-glyph", 0, 0, G_OPTION_ARG_INT, &this->not_found_glyph, "Glyph value to replace not-found characters with", nullptr},
260 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
261 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
262 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
263 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
264 {"num-iterations", 'n', G_OPTION_FLAG_IN_MAIN,
265 G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
268 parser->add_group (entries,
271 "Options for the shaping process",
274 const gchar *features_help = "Comma-separated list of font features\n"
276 " Features can be enabled or disabled, either globally or limited to\n"
277 " specific character ranges. The format for specifying feature settings\n"
278 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
279 " and the global values are also accepted, though not documented below.\n"
280 " CSS string escapes are not supported."
282 " The range indices refer to the positions between Unicode characters,\n"
283 " unless the --utf8-clusters is provided, in which case range indices\n"
284 " refer to UTF-8 byte indices. The position before the first character\n"
287 " The format is Python-esque. Here is how it all works:\n"
289 " Syntax: Value: Start: End:\n"
292 " \"kern\" 1 0 ∞ # Turn feature on\n"
293 " \"+kern\" 1 0 ∞ # Turn feature on\n"
294 " \"-kern\" 0 0 ∞ # Turn feature off\n"
295 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
296 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
297 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
300 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
301 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
302 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
303 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
304 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
305 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
309 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
311 GOptionEntry entries2[] =
313 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
316 parser->add_group (entries2,
319 "Options for font features used",