1 /*
2 * Copyright © 2011 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27 #ifndef SHAPE_OPTIONS_HH
28 #define SHAPE_OPTIONS_HH
29
30 #include "options.hh"
31
32 struct shape_options_t
33 {
~shape_options_tshape_options_t34 ~shape_options_t ()
35 {
36 g_free (direction);
37 g_free (language);
38 g_free (script);
39 free (features);
40 g_strfreev (shapers);
41 }
42
43 void add_options (option_parser_t *parser);
44
setup_buffershape_options_t45 void setup_buffer (hb_buffer_t *buffer)
46 {
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 (unsafe_to_concat ? HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT : 0) |
56 (safe_to_insert_tatweel ? HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL : 0) |
57 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
58 (remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
59 0));
60 hb_buffer_set_invisible_glyph (buffer, invisible_glyph);
61 hb_buffer_set_not_found_glyph (buffer, not_found_glyph);
62 hb_buffer_set_cluster_level (buffer, cluster_level);
63 hb_buffer_guess_segment_properties (buffer);
64 }
65
populate_buffershape_options_t66 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
67 const char *text_before, const char *text_after)
68 {
69 hb_buffer_clear_contents (buffer);
70 if (text_before) {
71 unsigned int len = strlen (text_before);
72 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
73 }
74 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
75 if (text_after) {
76 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
77 }
78
79 if (!utf8_clusters) {
80 /* Reset cluster values to refer to Unicode character index
81 * instead of UTF-8 index. */
82 unsigned int num_glyphs = hb_buffer_get_length (buffer);
83 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
84 for (unsigned int i = 0; i < num_glyphs; i++)
85 {
86 info->cluster = i;
87 info++;
88 }
89 }
90
91 setup_buffer (buffer);
92 }
93
shapeshape_options_t94 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
95 {
96 if (!hb_shape_full (font, buffer, features, num_features, shapers))
97 {
98 if (error)
99 *error = "Shaping failed.";
100 goto fail;
101 }
102
103 if (normalize_glyphs)
104 hb_buffer_normalize_glyphs (buffer);
105
106 return true;
107
108 fail:
109 return false;
110 }
111
shape_closureshape_options_t112 void shape_closure (const char *text, int text_len,
113 hb_font_t *font, hb_buffer_t *buffer,
114 hb_set_t *glyphs)
115 {
116 hb_buffer_reset (buffer);
117 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
118 setup_buffer (buffer);
119 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
120 }
121
122 /* Buffer properties */
123 char *direction = nullptr;
124 char *language = nullptr;
125 char *script = nullptr;
126
127 /* Buffer flags */
128 hb_bool_t bot = false;
129 hb_bool_t eot = false;
130 hb_bool_t preserve_default_ignorables = false;
131 hb_bool_t remove_default_ignorables = false;
132
133 hb_feature_t *features = nullptr;
134 unsigned int num_features = 0;
135 char **shapers = nullptr;
136 hb_bool_t utf8_clusters = false;
137 hb_codepoint_t invisible_glyph = 0;
138 hb_codepoint_t not_found_glyph = 0;
139 hb_buffer_cluster_level_t cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
140 hb_bool_t normalize_glyphs = false;
141 hb_bool_t verify = false;
142 hb_bool_t unsafe_to_concat = false;
143 hb_bool_t safe_to_insert_tatweel = false;
144 unsigned int num_iterations = 1;
145 };
146
147
148 static gboolean
parse_shapers(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error)149 parse_shapers (const char *name G_GNUC_UNUSED,
150 const char *arg,
151 gpointer data,
152 GError **error)
153 {
154 shape_options_t *shape_opts = (shape_options_t *) data;
155 char **shapers = g_strsplit (arg, ",", 0);
156
157 for (char **shaper = shapers; *shaper; shaper++)
158 {
159 bool found = false;
160 for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
161 if (strcmp (*shaper, *hb_shaper) == 0)
162 {
163 found = true;
164 break;
165 }
166 }
167 if (!found)
168 {
169 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
170 "Unknown or unsupported shaper: %s", *shaper);
171 g_strfreev (shapers);
172 return false;
173 }
174 }
175
176 g_strfreev (shape_opts->shapers);
177 shape_opts->shapers = shapers;
178 return true;
179 }
180
181 static G_GNUC_NORETURN gboolean
list_shapers(const char * name G_GNUC_UNUSED,const char * arg G_GNUC_UNUSED,gpointer data G_GNUC_UNUSED,GError ** error G_GNUC_UNUSED)182 list_shapers (const char *name G_GNUC_UNUSED,
183 const char *arg G_GNUC_UNUSED,
184 gpointer data G_GNUC_UNUSED,
185 GError **error G_GNUC_UNUSED)
186 {
187 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
188 g_printf ("%s\n", *shaper);
189
190 exit(0);
191 }
192
193
194 static gboolean
parse_features(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)195 parse_features (const char *name G_GNUC_UNUSED,
196 const char *arg,
197 gpointer data,
198 GError **error G_GNUC_UNUSED)
199 {
200 shape_options_t *shape_opts = (shape_options_t *) data;
201 char *s = (char *) arg;
202 size_t l = strlen (s);
203 char *p;
204
205 shape_opts->num_features = 0;
206 g_free (shape_opts->features);
207 shape_opts->features = nullptr;
208
209 /* if the string is quoted, strip the quotes */
210 if (s[0] == s[l - 1] && (s[0] == '\"' || s[0] == '\''))
211 {
212 s[l - 1] = '\0';
213 s++;
214 }
215
216 if (!*s)
217 return true;
218
219 /* count the features first, so we can allocate memory */
220 p = s;
221 do {
222 shape_opts->num_features++;
223 p = strpbrk (p, ", ");
224 if (p)
225 p++;
226 } while (p);
227
228 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
229 if (!shape_opts->features)
230 return false;
231
232 /* now do the actual parsing */
233 p = s;
234 shape_opts->num_features = 0;
235 while (p && *p) {
236 char *end = strpbrk (p, ", ");
237 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
238 shape_opts->num_features++;
239 p = end ? end + 1 : nullptr;
240 }
241
242 return true;
243 }
244
245 void
add_options(option_parser_t * parser)246 shape_options_t::add_options (option_parser_t *parser)
247 {
248 GOptionEntry entries[] =
249 {
250 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
251 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
252 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
253 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
254 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
255 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
256 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "BCP 47 tag"},
257 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
258 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
259 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
260 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
261 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
262 {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr},
263 {"not-found-glyph", 0, 0, G_OPTION_ARG_INT, &this->not_found_glyph, "Glyph value to replace not-found characters with", nullptr},
264 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
265 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
266 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
267 {"unsafe-to-concat",0, 0, G_OPTION_ARG_NONE, &this->unsafe_to_concat, "Produce unsafe-to-concat glyph flag", nullptr},
268 {"safe-to-insert-tatweel",0, 0, G_OPTION_ARG_NONE, &this->safe_to_insert_tatweel, "Produce safe-to-insert-tatweel glyph flag", nullptr},
269 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
270 {"num-iterations", 'n', G_OPTION_FLAG_IN_MAIN,
271 G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
272 {nullptr}
273 };
274 parser->add_group (entries,
275 "shape",
276 "Shape options:",
277 "Options for the shaping process",
278 this);
279
280 const gchar *features_help = "Comma-separated list of font features\n"
281 "\n"
282 " Features can be enabled or disabled, either globally or limited to\n"
283 " specific character ranges. The format for specifying feature settings\n"
284 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
285 " and the global values are also accepted, though not documented below.\n"
286 " CSS string escapes are not supported."
287 "\n"
288 " The range indices refer to the positions between Unicode characters,\n"
289 " unless the --utf8-clusters is provided, in which case range indices\n"
290 " refer to UTF-8 byte indices. The position before the first character\n"
291 " is always 0.\n"
292 "\n"
293 " The format is Python-esque. Here is how it all works:\n"
294 "\n"
295 " Syntax: Value: Start: End:\n"
296 "\n"
297 " Setting value:\n"
298 " \"kern\" 1 0 ∞ # Turn feature on\n"
299 " \"+kern\" 1 0 ∞ # Turn feature on\n"
300 " \"-kern\" 0 0 ∞ # Turn feature off\n"
301 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
302 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
303 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
304 "\n"
305 " Setting index:\n"
306 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
307 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
308 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
309 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
310 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
311 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
312 "\n"
313 " Mixing it all:\n"
314 "\n"
315 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
316
317 GOptionEntry entries2[] =
318 {
319 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
320 {nullptr}
321 };
322 parser->add_group (entries2,
323 "features",
324 "Features options:",
325 "Options for font features used",
326 this);
327 }
328
329 #endif
330