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