hb-subset.cc 29 KB

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