shape-options.hh 15 KB

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