shape-options.hh 12 KB

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