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 hb_font_t *font)
69 {
70 hb_buffer_clear_contents (buffer);
71
72 if (glyphs)
73 {
74 /* Call the setup_buffer first while the buffer is empty,
75 * as guess_segment_properties doesn't like glyphs in the buffer. */
76
77 setup_buffer (buffer);
78 char *glyphs_text = (char *) text;
79 int glyphs_len = text_len;
80 if (glyphs_len < 0)
81 glyphs_len = strlen (glyphs_text);
82
83 if (glyphs_len && glyphs_text[glyphs_len - 1] != ']')
84 {
85 glyphs_text = g_strdup_printf ("%*s]", glyphs_len, glyphs_text);
86 glyphs_len = -1;
87 }
88
89 hb_buffer_deserialize_glyphs (buffer,
90 glyphs_text, glyphs_len,
91 nullptr,
92 font,
93 HB_BUFFER_SERIALIZE_FORMAT_TEXT);
94
95 if (!strchr (glyphs_text, '+'))
96 {
97 scale_advances = false;
98 unsigned count;
99 hb_direction_t direction = hb_buffer_get_direction (buffer);
100 hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, &count);
101 hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, &count);
102 for (unsigned i = 0; i < count; i++)
103 hb_font_get_glyph_advance_for_direction (font,
104 infos[i].codepoint,
105 direction,
106 &positions[i].x_advance,
107 &positions[i].y_advance);
108 }
109
110 if (glyphs_text != text)
111 g_free (glyphs_text);
112
113 return;
114 }
115
116 if (text_before) {
117 unsigned int len = strlen (text_before);
118 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
119 }
120 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
121 if (text_after) {
122 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
123 }
124
125 if (!utf8_clusters) {
126 /* Reset cluster values to refer to Unicode character index
127 * instead of UTF-8 index. */
128 unsigned int num_glyphs = hb_buffer_get_length (buffer);
129 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
130 for (unsigned int i = 0; i < num_glyphs; i++)
131 {
132 info->cluster = i;
133 info++;
134 }
135 }
136
137 setup_buffer (buffer);
138 }
139
shapeshape_options_t140 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
141 {
142 if (glyphs)
143 {
144 /* Scale positions. */
145 int x_scale, y_scale;
146 hb_font_get_scale (font, &x_scale, &y_scale);
147 unsigned upem = hb_face_get_upem (hb_font_get_face (font));
148 unsigned count;
149 auto *positions = hb_buffer_get_glyph_positions (buffer, &count);
150 for (unsigned i = 0; i < count; i++)
151 {
152 auto &pos = positions[i];
153 pos.x_offset = pos.x_offset * x_scale / upem;
154 pos.y_offset = pos.y_offset * y_scale / upem;
155 if (scale_advances)
156 {
157 pos.x_advance = pos.x_advance * x_scale / upem;
158 pos.y_advance = pos.y_advance * y_scale / upem;
159 }
160 }
161 }
162 else
163 {
164 if (advance <= 0)
165 {
166 if (!hb_shape_full (font, buffer, features, num_features, shapers))
167 {
168 if (error)
169 *error = "Shaping failed.";
170 goto fail;
171 }
172
173 if (advance < 0)
174 {
175 float unit = (1 << SUBPIXEL_BITS);
176
177 /* Calculate buffer advance */
178 float w = 0;
179 unsigned count = 0;
180 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count);
181 if (HB_DIRECTION_IS_HORIZONTAL (hb_buffer_get_direction (buffer)))
182 for (unsigned i = 0; i < count; i++)
183 w += pos[i].x_advance;
184 else
185 for (unsigned i = 0; i < count; i++)
186 w += pos[i].y_advance;
187
188 printf ("Default size: %u\n", (unsigned) roundf (w / unit));
189 exit (0);
190 }
191 }
192 #ifdef HB_EXPERIMENTAL_API
193 else
194 {
195 float unit = (1 << SUBPIXEL_BITS);
196 float target_advance = advance * unit;
197 float w = 0;
198 hb_tag_t var_tag;
199 float var_value;
200 if (!hb_shape_justify (font, buffer, features, num_features, shapers,
201 target_advance - unit * 0.5f, target_advance + unit * 0.5f,
202 &w, &var_tag, &var_value))
203 {
204 if (error)
205 *error = "Shaping failed.";
206 goto fail;
207 }
208 }
209 #endif
210 }
211
212 if (normalize_glyphs)
213 hb_buffer_normalize_glyphs (buffer);
214
215 return true;
216
217 fail:
218 return false;
219 }
220
shape_closureshape_options_t221 void shape_closure (const char *text, int text_len,
222 hb_font_t *font, hb_buffer_t *buffer,
223 hb_set_t *glyphs)
224 {
225 hb_buffer_reset (buffer);
226 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
227 setup_buffer (buffer);
228 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
229 }
230
231 /* Buffer properties */
232 char *direction = nullptr;
233 char *language = nullptr;
234 char *script = nullptr;
235
236 /* Buffer flags */
237 hb_bool_t bot = false;
238 hb_bool_t eot = false;
239 hb_bool_t preserve_default_ignorables = false;
240 hb_bool_t remove_default_ignorables = false;
241
242 hb_feature_t *features = nullptr;
243 unsigned int num_features = 0;
244 char **shapers = nullptr;
245 signed advance = 0;
246 hb_bool_t utf8_clusters = false;
247 hb_codepoint_t invisible_glyph = 0;
248 hb_codepoint_t not_found_glyph = 0;
249 hb_buffer_cluster_level_t cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
250 hb_bool_t normalize_glyphs = false;
251 hb_bool_t glyphs = false;
252 bool scale_advances = true;
253 hb_bool_t verify = false;
254 hb_bool_t unsafe_to_concat = false;
255 hb_bool_t safe_to_insert_tatweel = false;
256 unsigned int num_iterations = 1;
257 };
258
259
260 static gboolean
parse_shapers(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error)261 parse_shapers (const char *name G_GNUC_UNUSED,
262 const char *arg,
263 gpointer data,
264 GError **error)
265 {
266 shape_options_t *shape_opts = (shape_options_t *) data;
267 char **shapers = g_strsplit (arg, ",", 0);
268
269 for (char **shaper = shapers; *shaper; shaper++)
270 {
271 bool found = false;
272 for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
273 if (strcmp (*shaper, *hb_shaper) == 0)
274 {
275 found = true;
276 break;
277 }
278 }
279 if (!found)
280 {
281 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
282 "Unknown or unsupported shaper: %s", *shaper);
283 g_strfreev (shapers);
284 return false;
285 }
286 }
287
288 g_strfreev (shape_opts->shapers);
289 shape_opts->shapers = shapers;
290 return true;
291 }
292
293 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)294 list_shapers (const char *name G_GNUC_UNUSED,
295 const char *arg G_GNUC_UNUSED,
296 gpointer data G_GNUC_UNUSED,
297 GError **error G_GNUC_UNUSED)
298 {
299 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
300 g_printf ("%s\n", *shaper);
301
302 exit(0);
303 }
304
305
306 static gboolean
parse_features(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)307 parse_features (const char *name G_GNUC_UNUSED,
308 const char *arg,
309 gpointer data,
310 GError **error G_GNUC_UNUSED)
311 {
312 shape_options_t *shape_opts = (shape_options_t *) data;
313 char *s = (char *) arg;
314 size_t l = strlen (s);
315 char *p;
316
317 shape_opts->num_features = 0;
318 g_free (shape_opts->features);
319 shape_opts->features = nullptr;
320
321 /* if the string is quoted, strip the quotes */
322 if (s[0] == s[l - 1] && (s[0] == '\"' || s[0] == '\''))
323 {
324 s[l - 1] = '\0';
325 s++;
326 }
327
328 if (!*s)
329 return true;
330
331 /* count the features first, so we can allocate memory */
332 p = s;
333 do {
334 shape_opts->num_features++;
335 p = strpbrk (p, ", ");
336 if (p)
337 p++;
338 } while (p);
339
340 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
341 if (!shape_opts->features)
342 return false;
343
344 /* now do the actual parsing */
345 p = s;
346 shape_opts->num_features = 0;
347 while (p && *p) {
348 char *end = strpbrk (p, ", ");
349 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
350 shape_opts->num_features++;
351 p = end ? end + 1 : nullptr;
352 }
353
354 return true;
355 }
356
357 void
add_options(option_parser_t * parser)358 shape_options_t::add_options (option_parser_t *parser)
359 {
360 GOptionEntry entries[] =
361 {
362 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
363 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", nullptr},
364 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
365 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", nullptr},
366 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
367 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
368 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "BCP 47 tag"},
369 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
370 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr},
371 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr},
372 #ifdef HB_EXPERIMENTAL_API
373 {"justify-to", 0, 0,
374 G_OPTION_ARG_INT, &this->advance, "Target size to justify to", "SIZE, or -1"},
375 #endif
376 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr},
377 {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},
378 {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr},
379 {"not-found-glyph", 0, 0, G_OPTION_ARG_INT, &this->not_found_glyph, "Glyph value to replace not-found characters with", nullptr},
380 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", nullptr},
381 {"cluster-level", 0, 0, G_OPTION_ARG_INT, &this->cluster_level, "Cluster merging level (default: 0)", "0/1/2"},
382 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", nullptr},
383 {"unsafe-to-concat",0, 0, G_OPTION_ARG_NONE, &this->unsafe_to_concat, "Produce unsafe-to-concat glyph flag", nullptr},
384 {"safe-to-insert-tatweel",0, 0, G_OPTION_ARG_NONE, &this->safe_to_insert_tatweel, "Produce safe-to-insert-tatweel glyph flag", nullptr},
385 {"glyphs", 0, 0, G_OPTION_ARG_NONE, &this->glyphs, "Interpret input as glyph string", nullptr},
386 {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr},
387 {"num-iterations", 'n',G_OPTION_FLAG_IN_MAIN,
388 G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
389 {nullptr}
390 };
391 parser->add_group (entries,
392 "shape",
393 "Shape options:",
394 "Options for the shaping process",
395 this);
396
397 const gchar *features_help = "Comma-separated list of font features\n"
398 "\n"
399 " Features can be enabled or disabled, either globally or limited to\n"
400 " specific character ranges. The format for specifying feature settings\n"
401 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
402 " and the global values are also accepted, though not documented below.\n"
403 " CSS string escapes are not supported."
404 "\n"
405 " The range indices refer to the positions between Unicode characters,\n"
406 " unless the --utf8-clusters is provided, in which case range indices\n"
407 " refer to UTF-8 byte indices. The position before the first character\n"
408 " is always 0.\n"
409 "\n"
410 " The format is Python-esque. Here is how it all works:\n"
411 "\n"
412 " Syntax: Value: Start: End:\n"
413 "\n"
414 " Setting value:\n"
415 " \"kern\" 1 0 ∞ # Turn feature on\n"
416 " \"+kern\" 1 0 ∞ # Turn feature on\n"
417 " \"-kern\" 0 0 ∞ # Turn feature off\n"
418 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
419 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
420 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
421 "\n"
422 " Setting index:\n"
423 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
424 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
425 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
426 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
427 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
428 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
429 "\n"
430 " Mixing it all:\n"
431 "\n"
432 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
433
434 GOptionEntry entries2[] =
435 {
436 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
437 {nullptr}
438 };
439 parser->add_group (entries2,
440 "features",
441 "Features options:",
442 "Options for font features used",
443 this);
444 }
445
446 #endif
447