font-options.hh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 FONT_OPTIONS_HH
  27. #define FONT_OPTIONS_HH
  28. #include "face-options.hh"
  29. #include <hb-ot.h>
  30. #ifdef HAVE_FREETYPE
  31. #include <hb-ft.h>
  32. #endif
  33. #ifdef HAVE_CORETEXT
  34. #include <hb-coretext.h>
  35. #endif
  36. #define FONT_SIZE_UPEM 0x7FFFFFFF
  37. #define FONT_SIZE_NONE 0
  38. extern const unsigned DEFAULT_FONT_SIZE;
  39. extern const unsigned SUBPIXEL_BITS;
  40. struct font_options_t : face_options_t
  41. {
  42. ~font_options_t ()
  43. {
  44. #ifndef HB_NO_VAR
  45. free (variations);
  46. #endif
  47. g_free (font_funcs);
  48. hb_font_destroy (font);
  49. }
  50. void add_options (option_parser_t *parser);
  51. void post_parse (GError **error);
  52. hb_bool_t sub_font = false;
  53. #ifndef HB_NO_VAR
  54. hb_variation_t *variations = nullptr;
  55. unsigned int num_variations = 0;
  56. #endif
  57. int x_ppem = 0;
  58. int y_ppem = 0;
  59. double ptem = 0.;
  60. double x_embolden = 0.;
  61. double y_embolden = 0.;
  62. hb_bool_t embolden_in_place = false;
  63. double slant = 0.;
  64. unsigned int subpixel_bits = SUBPIXEL_BITS;
  65. mutable double font_size_x = DEFAULT_FONT_SIZE;
  66. mutable double font_size_y = DEFAULT_FONT_SIZE;
  67. char *font_funcs = nullptr;
  68. int ft_load_flags = 2;
  69. unsigned int named_instance = HB_FONT_NO_VAR_NAMED_INSTANCE;
  70. hb_font_t *font = nullptr;
  71. };
  72. static struct supported_font_funcs_t {
  73. char name[9];
  74. void (*func) (hb_font_t *);
  75. } supported_font_funcs[] =
  76. {
  77. {"ot", hb_ot_font_set_funcs},
  78. #ifdef HAVE_FREETYPE
  79. {"ft", hb_ft_font_set_funcs},
  80. #endif
  81. #ifdef HAVE_CORETEXT
  82. {"coretext", hb_coretext_font_set_funcs},
  83. #endif
  84. };
  85. void
  86. font_options_t::post_parse (GError **error)
  87. {
  88. assert (!font);
  89. font = hb_font_create (face);
  90. if (font_size_x == FONT_SIZE_UPEM)
  91. font_size_x = hb_face_get_upem (face);
  92. if (font_size_y == FONT_SIZE_UPEM)
  93. font_size_y = hb_face_get_upem (face);
  94. hb_font_set_ppem (font, x_ppem, y_ppem);
  95. hb_font_set_ptem (font, ptem);
  96. hb_font_set_synthetic_bold (font,
  97. (float) x_embolden, (float) y_embolden,
  98. embolden_in_place);
  99. hb_font_set_synthetic_slant (font, slant);
  100. int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
  101. int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
  102. hb_font_set_scale (font, scale_x, scale_y);
  103. #ifndef HB_NO_VAR
  104. hb_font_set_var_named_instance (font, named_instance);
  105. hb_font_set_variations (font, variations, num_variations);
  106. #endif
  107. void (*set_font_funcs) (hb_font_t *) = nullptr;
  108. if (!font_funcs)
  109. {
  110. set_font_funcs = supported_font_funcs[0].func;
  111. }
  112. else
  113. {
  114. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
  115. if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
  116. {
  117. set_font_funcs = supported_font_funcs[i].func;
  118. break;
  119. }
  120. if (!set_font_funcs)
  121. {
  122. GString *s = g_string_new (nullptr);
  123. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
  124. {
  125. if (i)
  126. g_string_append_c (s, '/');
  127. g_string_append (s, supported_font_funcs[i].name);
  128. }
  129. g_string_append_c (s, '\n');
  130. char *p = g_string_free (s, FALSE);
  131. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  132. "Unknown font function implementation `%s'; supported values are: %s; default is %s",
  133. font_funcs,
  134. p,
  135. supported_font_funcs[0].name);
  136. free (p);
  137. return;
  138. }
  139. }
  140. set_font_funcs (font);
  141. #ifdef HAVE_FREETYPE
  142. hb_ft_font_set_load_flags (font, ft_load_flags);
  143. #endif
  144. if (sub_font)
  145. {
  146. hb_font_t *old_font = font;
  147. font = hb_font_create_sub_font (old_font);
  148. hb_font_set_scale (old_font, scale_x * 2, scale_y * 2);
  149. hb_font_destroy (old_font);
  150. }
  151. }
  152. #ifndef HB_NO_VAR
  153. static gboolean
  154. parse_variations (const char *name G_GNUC_UNUSED,
  155. const char *arg,
  156. gpointer data,
  157. GError **error G_GNUC_UNUSED)
  158. {
  159. font_options_t *font_opts = (font_options_t *) data;
  160. char *s = (char *) arg;
  161. char *p;
  162. font_opts->num_variations = 0;
  163. g_free (font_opts->variations);
  164. font_opts->variations = nullptr;
  165. if (!*s)
  166. return true;
  167. /* count the variations first, so we can allocate memory */
  168. p = s;
  169. do {
  170. font_opts->num_variations++;
  171. p = strpbrk (p, ", ");
  172. if (p)
  173. p++;
  174. } while (p);
  175. font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
  176. if (!font_opts->variations)
  177. return false;
  178. /* now do the actual parsing */
  179. p = s;
  180. font_opts->num_variations = 0;
  181. while (p && *p) {
  182. char *end = strpbrk (p, ", ");
  183. if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
  184. font_opts->num_variations++;
  185. p = end ? end + 1 : nullptr;
  186. }
  187. return true;
  188. }
  189. #endif
  190. static gboolean
  191. parse_font_size (const char *name G_GNUC_UNUSED,
  192. const char *arg,
  193. gpointer data,
  194. GError **error G_GNUC_UNUSED)
  195. {
  196. font_options_t *font_opts = (font_options_t *) data;
  197. if (0 == strcmp (arg, "upem"))
  198. {
  199. font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
  200. return true;
  201. }
  202. switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
  203. case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
  204. case 2: return true;
  205. default:
  206. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  207. "%s argument should be one or two space-separated numbers",
  208. name);
  209. return false;
  210. }
  211. }
  212. static gboolean
  213. parse_font_ppem (const char *name G_GNUC_UNUSED,
  214. const char *arg,
  215. gpointer data,
  216. GError **error G_GNUC_UNUSED)
  217. {
  218. font_options_t *font_opts = (font_options_t *) data;
  219. switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
  220. case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
  221. case 2: return true;
  222. default:
  223. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  224. "%s argument should be one or two space-separated numbers",
  225. name);
  226. return false;
  227. }
  228. }
  229. static gboolean
  230. parse_font_embolden (const char *name G_GNUC_UNUSED,
  231. const char *arg,
  232. gpointer data,
  233. GError **error G_GNUC_UNUSED)
  234. {
  235. font_options_t *font_opts = (font_options_t *) data;
  236. switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->x_embolden, &font_opts->y_embolden)) {
  237. case 1: font_opts->y_embolden = font_opts->x_embolden; HB_FALLTHROUGH;
  238. case 2: return true;
  239. default:
  240. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  241. "%s argument should be one or two space-separated numbers",
  242. name);
  243. return false;
  244. }
  245. }
  246. static gboolean
  247. parse_font_bold (const char *name G_GNUC_UNUSED,
  248. const char *arg,
  249. gpointer data,
  250. GError **error G_GNUC_UNUSED)
  251. {
  252. font_options_t *font_opts = (font_options_t *) data;
  253. font_opts->embolden_in_place = false;
  254. return parse_font_embolden ( name, arg, data, error);
  255. }
  256. static gboolean
  257. parse_font_grade (const char *name G_GNUC_UNUSED,
  258. const char *arg,
  259. gpointer data,
  260. GError **error G_GNUC_UNUSED)
  261. {
  262. font_options_t *font_opts = (font_options_t *) data;
  263. font_opts->embolden_in_place = true;
  264. return parse_font_embolden ( name, arg, data, error);
  265. }
  266. void
  267. font_options_t::add_options (option_parser_t *parser)
  268. {
  269. face_options_t::add_options (parser);
  270. char *font_funcs_text = nullptr;
  271. {
  272. static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
  273. "No supported font-funcs found.");
  274. GString *s = g_string_new (nullptr);
  275. g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
  276. supported_font_funcs[0].name,
  277. supported_font_funcs[0].name);
  278. for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
  279. {
  280. g_string_append_c (s, '/');
  281. g_string_append (s, supported_font_funcs[i].name);
  282. }
  283. font_funcs_text = g_string_free (s, FALSE);
  284. parser->free_later (font_funcs_text);
  285. }
  286. char *font_size_text;
  287. if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
  288. font_size_text = (char *) "Font size (default: upem)";
  289. else
  290. {
  291. font_size_text = g_strdup_printf ("Font size (default: %u)", DEFAULT_FONT_SIZE);
  292. parser->free_later (font_size_text);
  293. }
  294. int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
  295. GOptionEntry entries[] =
  296. {
  297. {"font-size", 0, font_size_flags,
  298. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
  299. {"font-ppem", 0, font_size_flags,
  300. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
  301. {"font-ptem", 0, font_size_flags,
  302. G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
  303. {"font-bold", 0, font_size_flags,
  304. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_bold, "Set synthetic bold (default: 0)", "1/2 numbers; eg. 0.05"},
  305. {"font-grade", 0, font_size_flags,
  306. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_grade, "Set synthetic grade (default: 0)", "1/2 numbers; eg. 0.05"},
  307. {"font-slant", 0, font_size_flags,
  308. G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"},
  309. {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, font_funcs_text, "impl"},
  310. {"sub-font", 0, G_OPTION_FLAG_HIDDEN,
  311. G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"},
  312. {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
  313. {nullptr}
  314. };
  315. parser->add_group (entries,
  316. "font",
  317. "Font-instance options:",
  318. "Options for the font instance",
  319. this,
  320. false /* We add below. */);
  321. #ifndef HB_NO_VAR
  322. const gchar *variations_help = "Comma-separated list of font variations\n"
  323. "\n"
  324. " Variations are set globally. The format for specifying variation settings\n"
  325. " follows. All valid CSS font-variation-settings values other than 'normal'\n"
  326. " and 'inherited' are also accepted, though, not documented below.\n"
  327. "\n"
  328. " The format is a tag, optionally followed by an equals sign, followed by a\n"
  329. " number. For example:\n"
  330. "\n"
  331. " \"wght=500\"\n"
  332. " \"slnt=-7.5\"";
  333. GOptionEntry entries2[] =
  334. {
  335. {"named-instance", 0, 0, G_OPTION_ARG_INT, &this->named_instance, "Set named-instance index (default: none)", "index"},
  336. {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
  337. {nullptr}
  338. };
  339. parser->add_group (entries2,
  340. "variations",
  341. "Variations options:",
  342. "Options for font variations used",
  343. this);
  344. #endif
  345. }
  346. #endif