font-options.hh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef FONT_OPTIONS_HH
  27. #define FONT_OPTIONS_HH
  28. #include "face-options.hh"
  29. #ifdef HAVE_FREETYPE
  30. #include <hb-ft.h>
  31. #endif
  32. #include <hb-ot.h>
  33. #define FONT_SIZE_UPEM 0x7FFFFFFF
  34. #define FONT_SIZE_NONE 0
  35. extern const unsigned DEFAULT_FONT_SIZE;
  36. extern const unsigned SUBPIXEL_BITS;
  37. struct font_options_t : face_options_t
  38. {
  39. ~font_options_t ()
  40. {
  41. #ifndef HB_NO_VAR
  42. free (variations);
  43. #endif
  44. g_free (font_funcs);
  45. hb_font_destroy (font);
  46. }
  47. void add_options (option_parser_t *parser);
  48. void post_parse (GError **error);
  49. hb_bool_t sub_font = false;
  50. #ifndef HB_NO_VAR
  51. hb_variation_t *variations = nullptr;
  52. unsigned int num_variations = 0;
  53. #endif
  54. int x_ppem = 0;
  55. int y_ppem = 0;
  56. double ptem = 0.;
  57. double slant = 0.;
  58. unsigned int subpixel_bits = SUBPIXEL_BITS;
  59. mutable double font_size_x = DEFAULT_FONT_SIZE;
  60. mutable double font_size_y = DEFAULT_FONT_SIZE;
  61. char *font_funcs = nullptr;
  62. int ft_load_flags = 2;
  63. hb_font_t *font = nullptr;
  64. };
  65. static struct supported_font_funcs_t {
  66. char name[4];
  67. void (*func) (hb_font_t *);
  68. } supported_font_funcs[] =
  69. {
  70. {"ot", hb_ot_font_set_funcs},
  71. #ifdef HAVE_FREETYPE
  72. {"ft", hb_ft_font_set_funcs},
  73. #endif
  74. };
  75. void
  76. font_options_t::post_parse (GError **error)
  77. {
  78. assert (!font);
  79. font = hb_font_create (face);
  80. if (font_size_x == FONT_SIZE_UPEM)
  81. font_size_x = hb_face_get_upem (face);
  82. if (font_size_y == FONT_SIZE_UPEM)
  83. font_size_y = hb_face_get_upem (face);
  84. hb_font_set_ppem (font, x_ppem, y_ppem);
  85. hb_font_set_ptem (font, ptem);
  86. hb_font_set_synthetic_slant (font, slant);
  87. int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
  88. int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
  89. hb_font_set_scale (font, scale_x, scale_y);
  90. #ifndef HB_NO_VAR
  91. hb_font_set_variations (font, variations, num_variations);
  92. #endif
  93. void (*set_font_funcs) (hb_font_t *) = nullptr;
  94. if (!font_funcs)
  95. {
  96. set_font_funcs = supported_font_funcs[0].func;
  97. }
  98. else
  99. {
  100. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
  101. if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
  102. {
  103. set_font_funcs = supported_font_funcs[i].func;
  104. break;
  105. }
  106. if (!set_font_funcs)
  107. {
  108. GString *s = g_string_new (nullptr);
  109. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
  110. {
  111. if (i)
  112. g_string_append_c (s, '/');
  113. g_string_append (s, supported_font_funcs[i].name);
  114. }
  115. g_string_append_c (s, '\n');
  116. char *p = g_string_free (s, FALSE);
  117. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  118. "Unknown font function implementation `%s'; supported values are: %s; default is %s",
  119. font_funcs,
  120. p,
  121. supported_font_funcs[0].name);
  122. free (p);
  123. return;
  124. }
  125. }
  126. set_font_funcs (font);
  127. #ifdef HAVE_FREETYPE
  128. hb_ft_font_set_load_flags (font, ft_load_flags);
  129. #endif
  130. if (sub_font)
  131. {
  132. hb_font_t *old_font = font;
  133. font = hb_font_create_sub_font (old_font);
  134. hb_font_set_scale (old_font, scale_x * 2, scale_y * 2);
  135. hb_font_destroy (old_font);
  136. }
  137. }
  138. #ifndef HB_NO_VAR
  139. static gboolean
  140. parse_variations (const char *name G_GNUC_UNUSED,
  141. const char *arg,
  142. gpointer data,
  143. GError **error G_GNUC_UNUSED)
  144. {
  145. font_options_t *font_opts = (font_options_t *) data;
  146. char *s = (char *) arg;
  147. char *p;
  148. font_opts->num_variations = 0;
  149. g_free (font_opts->variations);
  150. font_opts->variations = nullptr;
  151. if (!*s)
  152. return true;
  153. /* count the variations first, so we can allocate memory */
  154. p = s;
  155. do {
  156. font_opts->num_variations++;
  157. p = strpbrk (p, ", ");
  158. if (p)
  159. p++;
  160. } while (p);
  161. font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
  162. if (!font_opts->variations)
  163. return false;
  164. /* now do the actual parsing */
  165. p = s;
  166. font_opts->num_variations = 0;
  167. while (p && *p) {
  168. char *end = strpbrk (p, ", ");
  169. if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
  170. font_opts->num_variations++;
  171. p = end ? end + 1 : nullptr;
  172. }
  173. return true;
  174. }
  175. #endif
  176. static gboolean
  177. parse_font_size (const char *name G_GNUC_UNUSED,
  178. const char *arg,
  179. gpointer data,
  180. GError **error G_GNUC_UNUSED)
  181. {
  182. font_options_t *font_opts = (font_options_t *) data;
  183. if (0 == strcmp (arg, "upem"))
  184. {
  185. font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
  186. return true;
  187. }
  188. switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
  189. case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
  190. case 2: return true;
  191. default:
  192. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  193. "%s argument should be one or two space-separated numbers",
  194. name);
  195. return false;
  196. }
  197. }
  198. static gboolean
  199. parse_font_ppem (const char *name G_GNUC_UNUSED,
  200. const char *arg,
  201. gpointer data,
  202. GError **error G_GNUC_UNUSED)
  203. {
  204. font_options_t *font_opts = (font_options_t *) data;
  205. switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
  206. case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
  207. case 2: return true;
  208. default:
  209. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  210. "%s argument should be one or two space-separated numbers",
  211. name);
  212. return false;
  213. }
  214. }
  215. void
  216. font_options_t::add_options (option_parser_t *parser)
  217. {
  218. face_options_t::add_options (parser);
  219. char *text = nullptr;
  220. {
  221. static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
  222. "No supported font-funcs found.");
  223. GString *s = g_string_new (nullptr);
  224. g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
  225. supported_font_funcs[0].name,
  226. supported_font_funcs[0].name);
  227. for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
  228. {
  229. g_string_append_c (s, '/');
  230. g_string_append (s, supported_font_funcs[i].name);
  231. }
  232. text = g_string_free (s, FALSE);
  233. parser->free_later (text);
  234. }
  235. char *font_size_text;
  236. if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
  237. font_size_text = (char *) "Font size (default: upem)";
  238. else
  239. {
  240. font_size_text = g_strdup_printf ("Font size (default: %d)", DEFAULT_FONT_SIZE);
  241. parser->free_later (font_size_text);
  242. }
  243. int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
  244. GOptionEntry entries[] =
  245. {
  246. {"font-size", 0, font_size_flags,
  247. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
  248. {"font-ppem", 0, font_size_flags,
  249. G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
  250. {"font-ptem", 0, 0,
  251. G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
  252. {"font-slant", 0, 0,
  253. G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"},
  254. {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
  255. {"sub-font", 0, G_OPTION_FLAG_HIDDEN,
  256. G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"},
  257. {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
  258. {nullptr}
  259. };
  260. parser->add_group (entries,
  261. "font",
  262. "Font-instance options:",
  263. "Options for the font instance",
  264. this,
  265. false /* We add below. */);
  266. #ifndef HB_NO_VAR
  267. const gchar *variations_help = "Comma-separated list of font variations\n"
  268. "\n"
  269. " Variations are set globally. The format for specifying variation settings\n"
  270. " follows. All valid CSS font-variation-settings values other than 'normal'\n"
  271. " and 'inherited' are also accepted, though, not documented below.\n"
  272. "\n"
  273. " The format is a tag, optionally followed by an equals sign, followed by a\n"
  274. " number. For example:\n"
  275. "\n"
  276. " \"wght=500\"\n"
  277. " \"slnt=-7.5\"";
  278. GOptionEntry entries2[] =
  279. {
  280. {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
  281. {nullptr}
  282. };
  283. parser->add_group (entries2,
  284. "variations",
  285. "Variations options:",
  286. "Options for font variations used",
  287. this);
  288. #endif
  289. }
  290. #endif