hb-subset.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Copyright © 2010 Behdad Esfahbod
  3. * Copyright © 2011,2012 Google, Inc.
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Google Author(s): Garret Rieger, Rod Sheeter
  26. */
  27. #include "batch.hh"
  28. #include "face-options.hh"
  29. #include "main-font-text.hh"
  30. #include "output-options.hh"
  31. #include <hb-subset.h>
  32. static hb_face_t* preprocess_face(hb_face_t* face)
  33. {
  34. #ifdef HB_EXPERIMENTAL_API
  35. return hb_subset_preprocess (face);
  36. #else
  37. return hb_face_reference(face);
  38. #endif
  39. }
  40. /*
  41. * Command line interface to the harfbuzz font subsetter.
  42. */
  43. struct subset_main_t : option_parser_t, face_options_t, output_options_t<false>
  44. {
  45. subset_main_t ()
  46. : input (hb_subset_input_create_or_fail ())
  47. {}
  48. ~subset_main_t ()
  49. {
  50. hb_subset_input_destroy (input);
  51. }
  52. void parse_face (int argc, const char * const *argv)
  53. {
  54. option_parser_t parser;
  55. face_options_t face_opts;
  56. face_opts.add_options (&parser);
  57. GOptionEntry entries[] =
  58. {
  59. {G_OPTION_REMAINING, 0, G_OPTION_FLAG_IN_MAIN,
  60. G_OPTION_ARG_CALLBACK, (gpointer) &collect_face, nullptr, "[FONT-FILE] [TEXT]"},
  61. {nullptr}
  62. };
  63. parser.add_main_group (entries, &face_opts);
  64. parser.add_options ();
  65. g_option_context_set_ignore_unknown_options (parser.context, true);
  66. g_option_context_set_help_enabled (parser.context, false);
  67. char **args = (char **)
  68. #if GLIB_CHECK_VERSION (2, 68, 0)
  69. g_memdup2
  70. #else
  71. g_memdup
  72. #endif
  73. (argv, argc * sizeof (*argv));
  74. parser.parse (&argc, &args);
  75. g_free (args);
  76. set_face (face_opts.face);
  77. }
  78. void parse (int argc, char **argv)
  79. {
  80. bool help = false;
  81. for (auto i = 1; i < argc; i++)
  82. if (!strncmp ("--help", argv[i], 6))
  83. {
  84. help = true;
  85. break;
  86. }
  87. if (likely (!help))
  88. {
  89. /* Do a preliminary parse to load font-face, such that we can use it
  90. * during main option parsing. */
  91. parse_face (argc, argv);
  92. }
  93. add_options ();
  94. option_parser_t::parse (&argc, &argv);
  95. }
  96. int operator () (int argc, char **argv)
  97. {
  98. parse (argc, argv);
  99. hb_face_t* orig_face = face;
  100. if (preprocess)
  101. orig_face = preprocess_face (face);
  102. hb_face_t *new_face = nullptr;
  103. for (unsigned i = 0; i < num_iterations; i++)
  104. {
  105. hb_face_destroy (new_face);
  106. new_face = hb_subset_or_fail (face, input);
  107. }
  108. bool success = new_face;
  109. if (success)
  110. {
  111. hb_blob_t *result = hb_face_reference_blob (new_face);
  112. write_file (output_file, result);
  113. hb_blob_destroy (result);
  114. }
  115. hb_face_destroy (new_face);
  116. if (preprocess)
  117. hb_face_destroy (orig_face);
  118. return success ? 0 : 1;
  119. }
  120. bool
  121. write_file (const char *output_file, hb_blob_t *blob)
  122. {
  123. assert (out_fp);
  124. unsigned int size;
  125. const char* data = hb_blob_get_data (blob, &size);
  126. while (size)
  127. {
  128. size_t ret = fwrite (data, 1, size, out_fp);
  129. size -= ret;
  130. data += ret;
  131. if (size && ferror (out_fp))
  132. fail (false, "Failed to write output: %s", strerror (errno));
  133. }
  134. return true;
  135. }
  136. void add_options ();
  137. protected:
  138. static gboolean
  139. collect_face (const char *name,
  140. const char *arg,
  141. gpointer data,
  142. GError **error);
  143. static gboolean
  144. collect_rest (const char *name,
  145. const char *arg,
  146. gpointer data,
  147. GError **error);
  148. public:
  149. unsigned num_iterations = 1;
  150. gboolean preprocess;
  151. hb_subset_input_t *input = nullptr;
  152. };
  153. static gboolean
  154. parse_gids (const char *name G_GNUC_UNUSED,
  155. const char *arg,
  156. gpointer data,
  157. GError **error)
  158. {
  159. subset_main_t *subset_main = (subset_main_t *) data;
  160. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  161. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  162. hb_set_t *gids = hb_subset_input_glyph_set (subset_main->input);
  163. if (!is_remove && !is_add) hb_set_clear (gids);
  164. if (0 == strcmp (arg, "*"))
  165. {
  166. hb_set_clear (gids);
  167. if (!is_remove)
  168. hb_set_invert (gids);
  169. return true;
  170. }
  171. char *s = (char *) arg;
  172. char *p;
  173. while (s && *s)
  174. {
  175. while (*s && strchr (", ", *s))
  176. s++;
  177. if (!*s)
  178. break;
  179. errno = 0;
  180. hb_codepoint_t start_code = strtoul (s, &p, 10);
  181. if (s[0] == '-' || errno || s == p)
  182. {
  183. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  184. "Failed parsing glyph-index at: '%s'", s);
  185. return false;
  186. }
  187. if (p && p[0] == '-') // ranges
  188. {
  189. s = ++p;
  190. hb_codepoint_t end_code = strtoul (s, &p, 10);
  191. if (s[0] == '-' || errno || s == p)
  192. {
  193. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  194. "Failed parsing glyph-index at: '%s'", s);
  195. return false;
  196. }
  197. if (end_code < start_code)
  198. {
  199. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  200. "Invalid glyph-index range %u-%u", start_code, end_code);
  201. return false;
  202. }
  203. if (!is_remove)
  204. hb_set_add_range (gids, start_code, end_code);
  205. else
  206. hb_set_del_range (gids, start_code, end_code);
  207. }
  208. else
  209. {
  210. if (!is_remove)
  211. hb_set_add (gids, start_code);
  212. else
  213. hb_set_del (gids, start_code);
  214. }
  215. s = p;
  216. }
  217. return true;
  218. }
  219. static gboolean
  220. parse_glyphs (const char *name G_GNUC_UNUSED,
  221. const char *arg,
  222. gpointer data,
  223. GError **error G_GNUC_UNUSED)
  224. {
  225. subset_main_t *subset_main = (subset_main_t *) data;
  226. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  227. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  228. hb_set_t *gids = hb_subset_input_glyph_set (subset_main->input);
  229. if (!is_remove && !is_add) hb_set_clear (gids);
  230. if (0 == strcmp (arg, "*"))
  231. {
  232. hb_set_clear (gids);
  233. if (!is_remove)
  234. hb_set_invert (gids);
  235. return true;
  236. }
  237. const char *p = arg;
  238. const char *p_end = arg + strlen (arg);
  239. hb_font_t *font = hb_font_create (subset_main->face);
  240. while (p < p_end)
  241. {
  242. while (p < p_end && (*p == ' ' || *p == ','))
  243. p++;
  244. const char *end = p;
  245. while (end < p_end && *end != ' ' && *end != ',')
  246. end++;
  247. if (p < end)
  248. {
  249. hb_codepoint_t gid;
  250. if (!hb_font_get_glyph_from_name (font, p, end - p, &gid))
  251. {
  252. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  253. "Failed parsing glyph name: '%s'", p);
  254. return false;
  255. }
  256. if (!is_remove)
  257. hb_set_add (gids, gid);
  258. else
  259. hb_set_del (gids, gid);
  260. }
  261. p = end + 1;
  262. }
  263. hb_font_destroy (font);
  264. return true;
  265. }
  266. static gboolean
  267. parse_text (const char *name G_GNUC_UNUSED,
  268. const char *arg,
  269. gpointer data,
  270. GError **error G_GNUC_UNUSED)
  271. {
  272. subset_main_t *subset_main = (subset_main_t *) data;
  273. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  274. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  275. hb_set_t *unicodes = hb_subset_input_unicode_set (subset_main->input);
  276. if (!is_remove && !is_add) hb_set_clear (unicodes);
  277. if (0 == strcmp (arg, "*"))
  278. {
  279. hb_set_clear (unicodes);
  280. if (!is_remove)
  281. hb_set_invert (unicodes);
  282. return true;
  283. }
  284. for (gchar *c = (gchar *) arg;
  285. *c;
  286. c = g_utf8_find_next_char(c, nullptr))
  287. {
  288. gunichar cp = g_utf8_get_char(c);
  289. if (!is_remove)
  290. hb_set_add (unicodes, cp);
  291. else
  292. hb_set_del (unicodes, cp);
  293. }
  294. return true;
  295. }
  296. static gboolean
  297. parse_unicodes (const char *name G_GNUC_UNUSED,
  298. const char *arg,
  299. gpointer data,
  300. GError **error)
  301. {
  302. subset_main_t *subset_main = (subset_main_t *) data;
  303. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  304. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  305. hb_set_t *unicodes = hb_subset_input_unicode_set (subset_main->input);
  306. if (!is_remove && !is_add) hb_set_clear (unicodes);
  307. if (0 == strcmp (arg, "*"))
  308. {
  309. hb_set_clear (unicodes);
  310. if (!is_remove)
  311. hb_set_invert (unicodes);
  312. return true;
  313. }
  314. // XXX TODO Ranges
  315. #define DELIMITERS "<+->{},;&#\\xXuUnNiI\n\t\v\f\r "
  316. char *s = (char *) arg;
  317. char *p;
  318. while (s && *s)
  319. {
  320. while (*s && strchr (DELIMITERS, *s))
  321. s++;
  322. if (!*s)
  323. break;
  324. errno = 0;
  325. hb_codepoint_t start_code = strtoul (s, &p, 16);
  326. if (errno || s == p)
  327. {
  328. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  329. "Failed parsing Unicode at: '%s'", s);
  330. return false;
  331. }
  332. if (p && p[0] == '-') // ranges
  333. {
  334. s = ++p;
  335. hb_codepoint_t end_code = strtoul (s, &p, 16);
  336. if (s[0] == '-' || errno || s == p)
  337. {
  338. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  339. "Failed parsing Unicode at: '%s'", s);
  340. return false;
  341. }
  342. if (end_code < start_code)
  343. {
  344. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  345. "Invalid Unicode range %u-%u", start_code, end_code);
  346. return false;
  347. }
  348. if (!is_remove)
  349. hb_set_add_range (unicodes, start_code, end_code);
  350. else
  351. hb_set_del_range (unicodes, start_code, end_code);
  352. }
  353. else
  354. {
  355. if (!is_remove)
  356. hb_set_add (unicodes, start_code);
  357. else
  358. hb_set_del (unicodes, start_code);
  359. }
  360. s = p;
  361. }
  362. return true;
  363. }
  364. static gboolean
  365. parse_nameids (const char *name,
  366. const char *arg,
  367. gpointer data,
  368. GError **error)
  369. {
  370. subset_main_t *subset_main = (subset_main_t *) data;
  371. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  372. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  373. hb_set_t *name_ids = hb_subset_input_set (subset_main->input, HB_SUBSET_SETS_NAME_ID);
  374. if (!is_remove && !is_add) hb_set_clear (name_ids);
  375. if (0 == strcmp (arg, "*"))
  376. {
  377. hb_set_clear (name_ids);
  378. if (!is_remove)
  379. hb_set_invert (name_ids);
  380. return true;
  381. }
  382. char *s = (char *) arg;
  383. char *p;
  384. while (s && *s)
  385. {
  386. while (*s && strchr (", ", *s))
  387. s++;
  388. if (!*s)
  389. break;
  390. errno = 0;
  391. hb_codepoint_t u = strtoul (s, &p, 10);
  392. if (errno || s == p)
  393. {
  394. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  395. "Failed parsing nameID at: '%s'", s);
  396. return false;
  397. }
  398. if (!is_remove)
  399. {
  400. hb_set_add (name_ids, u);
  401. } else {
  402. hb_set_del (name_ids, u);
  403. }
  404. s = p;
  405. }
  406. return true;
  407. }
  408. static gboolean
  409. parse_name_languages (const char *name,
  410. const char *arg,
  411. gpointer data,
  412. GError **error)
  413. {
  414. subset_main_t *subset_main = (subset_main_t *) data;
  415. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  416. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  417. hb_set_t *name_languages = hb_subset_input_set (subset_main->input, HB_SUBSET_SETS_NAME_LANG_ID);
  418. if (!is_remove && !is_add) hb_set_clear (name_languages);
  419. if (0 == strcmp (arg, "*"))
  420. {
  421. hb_set_clear (name_languages);
  422. if (!is_remove)
  423. hb_set_invert (name_languages);
  424. return true;
  425. }
  426. char *s = (char *) arg;
  427. char *p;
  428. while (s && *s)
  429. {
  430. while (*s && strchr (", ", *s))
  431. s++;
  432. if (!*s)
  433. break;
  434. errno = 0;
  435. hb_codepoint_t u = strtoul (s, &p, 10);
  436. if (errno || s == p)
  437. {
  438. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  439. "Failed parsing name-language code at: '%s'", s);
  440. return false;
  441. }
  442. if (!is_remove)
  443. {
  444. hb_set_add (name_languages, u);
  445. } else {
  446. hb_set_del (name_languages, u);
  447. }
  448. s = p;
  449. }
  450. return true;
  451. }
  452. template <hb_subset_flags_t flag>
  453. static gboolean
  454. set_flag (const char *name,
  455. const char *arg,
  456. gpointer data,
  457. GError **error G_GNUC_UNUSED)
  458. {
  459. subset_main_t *subset_main = (subset_main_t *) data;
  460. hb_subset_input_set_flags (subset_main->input,
  461. hb_subset_input_get_flags (subset_main->input) | flag);
  462. return true;
  463. }
  464. static gboolean
  465. parse_layout_tag_list (hb_subset_sets_t set_type,
  466. const char *name,
  467. const char *arg,
  468. gpointer data,
  469. GError **error G_GNUC_UNUSED)
  470. {
  471. subset_main_t *subset_main = (subset_main_t *) data;
  472. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  473. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  474. hb_set_t *layout_tags = hb_subset_input_set (subset_main->input, set_type);
  475. if (!is_remove && !is_add) hb_set_clear (layout_tags);
  476. if (0 == strcmp (arg, "*"))
  477. {
  478. hb_set_clear (layout_tags);
  479. if (!is_remove)
  480. hb_set_invert (layout_tags);
  481. return true;
  482. }
  483. char *s = strtok((char *) arg, ", ");
  484. while (s)
  485. {
  486. if (strlen (s) > 4) // tags are at most 4 bytes
  487. {
  488. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  489. "Failed parsing table tag at: '%s'", s);
  490. return false;
  491. }
  492. hb_tag_t tag = hb_tag_from_string (s, strlen (s));
  493. if (!is_remove)
  494. hb_set_add (layout_tags, tag);
  495. else
  496. hb_set_del (layout_tags, tag);
  497. s = strtok(nullptr, ", ");
  498. }
  499. return true;
  500. }
  501. static gboolean
  502. parse_layout_features (const char *name,
  503. const char *arg,
  504. gpointer data,
  505. GError **error)
  506. {
  507. return parse_layout_tag_list (HB_SUBSET_SETS_LAYOUT_FEATURE_TAG,
  508. name,
  509. arg,
  510. data,
  511. error);
  512. }
  513. static gboolean
  514. parse_layout_scripts (const char *name,
  515. const char *arg,
  516. gpointer data,
  517. GError **error)
  518. {
  519. return parse_layout_tag_list (HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG,
  520. name,
  521. arg,
  522. data,
  523. error);
  524. }
  525. static gboolean
  526. parse_drop_tables (const char *name,
  527. const char *arg,
  528. gpointer data,
  529. GError **error)
  530. {
  531. subset_main_t *subset_main = (subset_main_t *) data;
  532. hb_bool_t is_remove = (name[strlen (name) - 1] == '-');
  533. hb_bool_t is_add = (name[strlen (name) - 1] == '+');
  534. hb_set_t *drop_tables = hb_subset_input_set (subset_main->input, HB_SUBSET_SETS_DROP_TABLE_TAG);
  535. if (!is_remove && !is_add) hb_set_clear (drop_tables);
  536. if (0 == strcmp (arg, "*"))
  537. {
  538. hb_set_clear (drop_tables);
  539. if (!is_remove)
  540. hb_set_invert (drop_tables);
  541. return true;
  542. }
  543. char *s = strtok((char *) arg, ", ");
  544. while (s)
  545. {
  546. if (strlen (s) > 4) // Table tags are at most 4 bytes.
  547. {
  548. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  549. "Failed parsing table tag at: '%s'", s);
  550. return false;
  551. }
  552. hb_tag_t tag = hb_tag_from_string (s, strlen (s));
  553. if (!is_remove)
  554. hb_set_add (drop_tables, tag);
  555. else
  556. hb_set_del (drop_tables, tag);
  557. s = strtok(nullptr, ", ");
  558. }
  559. return true;
  560. }
  561. #ifdef HB_EXPERIMENTAL_API
  562. #ifndef HB_NO_VAR
  563. static gboolean
  564. parse_instance (const char *name,
  565. const char *arg,
  566. gpointer data,
  567. GError **error)
  568. {
  569. subset_main_t *subset_main = (subset_main_t *) data;
  570. char *s = strtok((char *) arg, "=");
  571. while (s)
  572. {
  573. unsigned len = strlen (s);
  574. if (len > 4) //Axis tags are 4 bytes.
  575. {
  576. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  577. "Failed parsing axis tag at: '%s'", s);
  578. return false;
  579. }
  580. hb_tag_t axis_tag = hb_tag_from_string (s, len);
  581. s = strtok(nullptr, ", ");
  582. if (!s)
  583. {
  584. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  585. "Value not specified for axis: %c%c%c%c", HB_UNTAG (axis_tag));
  586. return false;
  587. }
  588. if (strcmp (s, "drop") == 0)
  589. {
  590. if (!hb_subset_input_pin_axis_to_default (subset_main->input, subset_main->face, axis_tag))
  591. {
  592. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  593. "Cannot pin axis: '%c%c%c%c', not present in fvar", HB_UNTAG (axis_tag));
  594. return false;
  595. }
  596. }
  597. else
  598. {
  599. errno = 0;
  600. char *p;
  601. float axis_value = strtof (s, &p);
  602. if (errno || s == p)
  603. {
  604. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  605. "Failed parsing axis value at: '%s'", s);
  606. return false;
  607. }
  608. if (!hb_subset_input_pin_axis_location (subset_main->input, subset_main->face, axis_tag, axis_value))
  609. {
  610. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  611. "Cannot pin axis: '%c%c%c%c', not present in fvar", HB_UNTAG (axis_tag));
  612. return false;
  613. }
  614. }
  615. s = strtok(nullptr, "=");
  616. }
  617. return true;
  618. }
  619. #endif
  620. #endif
  621. template <GOptionArgFunc line_parser, bool allow_comments=true>
  622. static gboolean
  623. parse_file_for (const char *name,
  624. const char *arg,
  625. gpointer data,
  626. GError **error)
  627. {
  628. FILE *fp = nullptr;
  629. if (0 != strcmp (arg, "-"))
  630. fp = fopen (arg, "r");
  631. else
  632. fp = stdin;
  633. if (!fp)
  634. {
  635. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  636. "Failed opening file `%s': %s",
  637. arg, strerror (errno));
  638. return false;
  639. }
  640. GString *gs = g_string_new (nullptr);
  641. do
  642. {
  643. g_string_set_size (gs, 0);
  644. char buf[BUFSIZ];
  645. while (fgets (buf, sizeof (buf), fp))
  646. {
  647. unsigned bytes = strlen (buf);
  648. if (bytes && buf[bytes - 1] == '\n')
  649. {
  650. bytes--;
  651. g_string_append_len (gs, buf, bytes);
  652. break;
  653. }
  654. g_string_append_len (gs, buf, bytes);
  655. }
  656. if (ferror (fp))
  657. {
  658. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  659. "Failed reading file `%s': %s",
  660. arg, strerror (errno));
  661. return false;
  662. }
  663. g_string_append_c (gs, '\0');
  664. if (allow_comments)
  665. {
  666. char *comment = strchr (gs->str, '#');
  667. if (comment)
  668. *comment = '\0';
  669. }
  670. line_parser ("+", gs->str, data, error);
  671. if (*error)
  672. break;
  673. }
  674. while (!feof (fp));
  675. g_string_free (gs, false);
  676. return true;
  677. }
  678. gboolean
  679. subset_main_t::collect_face (const char *name,
  680. const char *arg,
  681. gpointer data,
  682. GError **error)
  683. {
  684. face_options_t *thiz = (face_options_t *) data;
  685. if (!thiz->font_file)
  686. {
  687. thiz->font_file = g_strdup (arg);
  688. return true;
  689. }
  690. return true;
  691. }
  692. gboolean
  693. subset_main_t::collect_rest (const char *name,
  694. const char *arg,
  695. gpointer data,
  696. GError **error)
  697. {
  698. subset_main_t *thiz = (subset_main_t *) data;
  699. if (!thiz->font_file)
  700. {
  701. thiz->font_file = g_strdup (arg);
  702. return true;
  703. }
  704. parse_text (name, arg, data, error);
  705. return true;
  706. }
  707. void
  708. subset_main_t::add_options ()
  709. {
  710. set_summary ("Subset fonts to specification.");
  711. face_options_t::add_options (this);
  712. GOptionEntry glyphset_entries[] =
  713. {
  714. {"gids", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_gids,
  715. "Specify glyph IDs or ranges to include in the subset.\n"
  716. " "
  717. "Use --gids-=... to subtract codepoints from the current set.", "list of glyph indices/ranges or *"},
  718. {"gids-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_gids, "Specify glyph IDs or ranges to remove from the subset", "list of glyph indices/ranges or *"},
  719. {"gids+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_gids, "Specify glyph IDs or ranges to include in the subset", "list of glyph indices/ranges or *"},
  720. {"gids-file", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_file_for<parse_gids>, "Specify file to read glyph IDs or ranges from", "filename"},
  721. {"glyphs", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_glyphs, "Specify glyph names to include in the subset. Use --glyphs-=... to subtract glyphs from the current set.", "list of glyph names or *"},
  722. {"glyphs+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_glyphs, "Specify glyph names to include in the subset", "list of glyph names"},
  723. {"glyphs-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_glyphs, "Specify glyph names to remove from the subset", "list of glyph names"},
  724. {"glyphs-file", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_file_for<parse_glyphs>, "Specify file to read glyph names from", "filename"},
  725. {"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Specify text to include in the subset. Use --text-=... to subtract codepoints from the current set.", "string"},
  726. {"text-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Specify text to remove from the subset", "string"},
  727. {"text+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Specify text to include in the subset", "string"},
  728. {"text-file", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_file_for<parse_text, false>,"Specify file to read text from", "filename"},
  729. {"unicodes", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes,
  730. "Specify Unicode codepoints or ranges to include in the subset. Use * to include all codepoints.\n"
  731. " "
  732. "--unicodes-=... can be used to subtract codepoints from the current set.\n"
  733. " "
  734. "For example: --unicodes=* --unicodes-=41,42,43 would create a subset with all codepoints\n"
  735. " "
  736. "except for 41, 42, 43.",
  737. "list of hex numbers/ranges or *"},
  738. {"unicodes-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Specify Unicode codepoints or ranges to remove from the subset", "list of hex numbers/ranges or *"},
  739. {"unicodes+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Specify Unicode codepoints or ranges to include in the subset", "list of hex numbers/ranges or *"},
  740. {"unicodes-file", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_file_for<parse_unicodes>,"Specify file to read Unicode codepoints or ranges from", "filename"},
  741. {nullptr}
  742. };
  743. add_group (glyphset_entries,
  744. "subset-glyphset",
  745. "Subset glyph-set option:",
  746. "Subsetting glyph-set options",
  747. this);
  748. GOptionEntry other_entries[] =
  749. {
  750. {"name-IDs", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_nameids, "Subset specified nameids. Use --name-IDs-=... to subtract from the current set.", "list of int numbers or *"},
  751. {"name-IDs-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_nameids, "Subset specified nameids", "list of int numbers or *"},
  752. {"name-IDs+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_nameids, "Subset specified nameids", "list of int numbers or *"},
  753. {"name-languages", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_name_languages, "Subset nameRecords with specified language IDs. Use --name-languages-=... to subtract from the current set.", "list of int numbers or *"},
  754. {"name-languages-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_name_languages, "Subset nameRecords with specified language IDs", "list of int numbers or *"},
  755. {"name-languages+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_name_languages, "Subset nameRecords with specified language IDs", "list of int numbers or *"},
  756. {"layout-features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_features, "Specify set of layout feature tags that will be preserved. Use --layout-features-=... to subtract from the current set.", "list of string table tags or *"},
  757. {"layout-features+",0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_features, "Specify set of layout feature tags that will be preserved", "list of string tags or *"},
  758. {"layout-features-",0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_features, "Specify set of layout feature tags that will be preserved", "list of string tags or *"},
  759. {"layout-scripts", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_scripts, "Specify set of layout script tags that will be preserved. Use --layout-scripts-=... to subtract from the current set.", "list of string table tags or *"},
  760. {"layout-scripts+",0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_scripts, "Specify set of layout script tags that will be preserved", "list of string tags or *"},
  761. {"layout-scripts-",0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_layout_scripts, "Specify set of layout script tags that will be preserved", "list of string tags or *"},
  762. {"drop-tables", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_drop_tables, "Drop the specified tables. Use --drop-tables-=... to subtract from the current set.", "list of string table tags or *"},
  763. {"drop-tables+", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_drop_tables, "Drop the specified tables.", "list of string table tags or *"},
  764. {"drop-tables-", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, (gpointer) &parse_drop_tables, "Drop the specified tables.", "list of string table tags or *"},
  765. #ifdef HB_EXPERIMENTAL_API
  766. #ifndef HB_NO_VAR
  767. {"instance", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_instance,
  768. "(Partially|Fully) Instantiate a variable font. A location consists of the tag of a variation axis, followed by '=', followed by a\n"
  769. "number or the literal string 'drop'\n"
  770. " "
  771. "For example: --instance=\"wdth=100 wght=200\" or --instance=\"wdth=drop\"\n"
  772. "Note: currently only fully instancing to the default location is supported\n",
  773. "list of comma separated axis-locations"},
  774. #endif
  775. #endif
  776. {nullptr}
  777. };
  778. add_group (other_entries,
  779. "subset-other",
  780. "Subset other option:",
  781. "Subsetting other options",
  782. this);
  783. GOptionEntry flag_entries[] =
  784. {
  785. {"no-hinting", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_NO_HINTING>, "Whether to drop hints", nullptr},
  786. {"retain-gids", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_RETAIN_GIDS>, "If set don't renumber glyph ids in the subset.", nullptr},
  787. {"desubroutinize", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_DESUBROUTINIZE>, "Remove CFF/CFF2 use of subroutines", nullptr},
  788. {"name-legacy", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_NAME_LEGACY>, "Keep legacy (non-Unicode) 'name' table entries", nullptr},
  789. {"set-overlaps-flag", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_SET_OVERLAPS_FLAG>, "Set the overlaps flag on each glyph.", nullptr},
  790. {"notdef-outline", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_NOTDEF_OUTLINE>, "Keep the outline of \'.notdef\' glyph", nullptr},
  791. {"no-prune-unicode-ranges", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES>, "Don't change the 'OS/2 ulUnicodeRange*' bits.", nullptr},
  792. {"glyph-names", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_GLYPH_NAMES>, "Keep PS glyph names in TT-flavored fonts. ", nullptr},
  793. {"passthrough-tables", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag<HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED>, "Do not drop tables that the tool does not know how to subset.", nullptr},
  794. {"preprocess-face", 0, 0, G_OPTION_ARG_NONE, &this->preprocess,
  795. "If set preprocesses the face with the add accelerator option before actually subsetting.", nullptr},
  796. {nullptr}
  797. };
  798. add_group (flag_entries,
  799. "subset-flags",
  800. "Subset boolean option:",
  801. "Subsetting boolean options",
  802. this);
  803. GOptionEntry app_entries[] =
  804. {
  805. {"num-iterations", 'n', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
  806. &this->num_iterations,
  807. "Run subsetter N times (default: 1)", "N"},
  808. {nullptr}
  809. };
  810. add_group (app_entries,
  811. "subset-app",
  812. "Subset app option:",
  813. "Subsetting application options",
  814. this);
  815. output_options_t::add_options (this);
  816. GOptionEntry entries[] =
  817. {
  818. {G_OPTION_REMAINING, 0, G_OPTION_FLAG_IN_MAIN,
  819. G_OPTION_ARG_CALLBACK, (gpointer) &collect_rest, nullptr, "[FONT-FILE] [TEXT]"},
  820. {nullptr}
  821. };
  822. add_main_group (entries, this);
  823. option_parser_t::add_options ();
  824. }
  825. int
  826. main (int argc, char **argv)
  827. {
  828. return batch_main<subset_main_t, true> (argc, argv);
  829. }