Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / util / shape-options.hh
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 {
34   ~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
45   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                                   (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
56                                   (remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
57                                   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);
62   }
63
64   void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
65                         const char *text_before, const char *text_after)
66   {
67     hb_buffer_clear_contents (buffer);
68     if (text_before) {
69       unsigned int len = strlen (text_before);
70       hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
71     }
72     hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
73     if (text_after) {
74       hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
75     }
76
77     if (!utf8_clusters) {
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++)
83       {
84         info->cluster = i;
85         info++;
86       }
87     }
88
89     setup_buffer (buffer);
90   }
91
92   hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
93   {
94     if (!hb_shape_full (font, buffer, features, num_features, shapers))
95     {
96       if (error)
97         *error = "Shaping failed.";
98       goto fail;
99     }
100
101     if (normalize_glyphs)
102       hb_buffer_normalize_glyphs (buffer);
103
104     return true;
105
106   fail:
107     return false;
108   }
109
110   void shape_closure (const char *text, int text_len,
111                       hb_font_t *font, hb_buffer_t *buffer,
112                       hb_set_t *glyphs)
113   {
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);
118   }
119
120   /* Buffer properties */
121   char *direction = nullptr;
122   char *language = nullptr;
123   char *script = nullptr;
124
125   /* Buffer flags */
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;
130
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;
141 };
142
143
144 static gboolean
145 parse_shapers (const char *name G_GNUC_UNUSED,
146                const char *arg,
147                gpointer    data,
148                GError    **error)
149 {
150   shape_options_t *shape_opts = (shape_options_t *) data;
151   char **shapers = g_strsplit (arg, ",", 0);
152
153   for (char **shaper = shapers; *shaper; shaper++)
154   {
155     bool found = false;
156     for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
157       if (strcmp (*shaper, *hb_shaper) == 0)
158       {
159         found = true;
160         break;
161       }
162     }
163     if (!found)
164     {
165       g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
166                    "Unknown or unsupported shaper: %s", *shaper);
167       g_strfreev (shapers);
168       return false;
169     }
170   }
171
172   g_strfreev (shape_opts->shapers);
173   shape_opts->shapers = shapers;
174   return true;
175 }
176
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)
182 {
183   for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
184     g_printf ("%s\n", *shaper);
185
186   exit(0);
187 }
188
189
190 static gboolean
191 parse_features (const char *name G_GNUC_UNUSED,
192                 const char *arg,
193                 gpointer    data,
194                 GError    **error G_GNUC_UNUSED)
195 {
196   shape_options_t *shape_opts = (shape_options_t *) data;
197   char *s = (char *) arg;
198   size_t l = strlen (s);
199   char *p;
200
201   shape_opts->num_features = 0;
202   g_free (shape_opts->features);
203   shape_opts->features = nullptr;
204
205   /* if the string is quoted, strip the quotes */
206   if (s[0] == s[l - 1] && (s[0] == '\"' || s[0] == '\''))
207   {
208     s[l - 1] = '\0';
209     s++;
210   }
211
212   if (!*s)
213     return true;
214
215   /* count the features first, so we can allocate memory */
216   p = s;
217   do {
218     shape_opts->num_features++;
219     p = strchr (p, ',');
220     if (p)
221       p++;
222   } while (p);
223
224   shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
225   if (!shape_opts->features)
226     return false;
227
228   /* now do the actual parsing */
229   p = s;
230   shape_opts->num_features = 0;
231   while (p && *p) {
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;
236   }
237
238   return true;
239 }
240
241 void
242 shape_options_t::add_options (option_parser_t *parser)
243 {
244   GOptionEntry entries[] =
245   {
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"},
266     {nullptr}
267   };
268   parser->add_group (entries,
269                      "shape",
270                      "Shape options:",
271                      "Options for the shaping process",
272                      this);
273
274   const gchar *features_help = "Comma-separated list of font features\n"
275     "\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."
281     "\n"
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"
285     "    is always 0.\n"
286     "\n"
287     "    The format is Python-esque.  Here is how it all works:\n"
288     "\n"
289     "      Syntax:       Value:    Start:    End:\n"
290     "\n"
291     "    Setting value:\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"
298     "\n"
299     "    Setting index:\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"
306     "\n"
307     "    Mixing it all:\n"
308     "\n"
309     "      \"aalt[3:5]=2\" 2         3         5         # Turn 2nd alternate on for range";
310
311   GOptionEntry entries2[] =
312   {
313     {"features",        0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_features,     features_help,  "list"},
314     {nullptr}
315   };
316   parser->add_group (entries2,
317                      "features",
318                      "Features options:",
319                      "Options for font features used",
320                      this);
321 }
322
323 #endif