2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef FONT_OPTIONS_HH
28 #define FONT_OPTIONS_HH
30 #include "face-options.hh"
37 #define FONT_SIZE_UPEM 0x7FFFFFFF
38 #define FONT_SIZE_NONE 0
40 extern const unsigned DEFAULT_FONT_SIZE;
41 extern const unsigned SUBPIXEL_BITS;
43 struct font_options_t : face_options_t
51 hb_font_destroy (font);
54 void add_options (option_parser_t *parser);
56 void post_parse (GError **error);
58 hb_bool_t sub_font = false;
60 hb_variation_t *variations = nullptr;
61 unsigned int num_variations = 0;
66 double x_embolden = 0.;
67 double y_embolden = 0.;
68 hb_bool_t embolden_in_place = false;
70 unsigned int subpixel_bits = SUBPIXEL_BITS;
71 mutable double font_size_x = DEFAULT_FONT_SIZE;
72 mutable double font_size_y = DEFAULT_FONT_SIZE;
73 char *font_funcs = nullptr;
74 int ft_load_flags = 2;
75 unsigned int named_instance = HB_FONT_NO_VAR_NAMED_INSTANCE;
77 hb_font_t *font = nullptr;
81 static struct supported_font_funcs_t {
83 void (*func) (hb_font_t *);
84 } supported_font_funcs[] =
86 {"ot", hb_ot_font_set_funcs},
88 {"ft", hb_ft_font_set_funcs},
94 font_options_t::post_parse (GError **error)
97 font = hb_font_create (face);
99 if (font_size_x == FONT_SIZE_UPEM)
100 font_size_x = hb_face_get_upem (face);
101 if (font_size_y == FONT_SIZE_UPEM)
102 font_size_y = hb_face_get_upem (face);
104 hb_font_set_ppem (font, x_ppem, y_ppem);
105 hb_font_set_ptem (font, ptem);
107 hb_font_set_synthetic_bold (font,
108 (float) x_embolden, (float) y_embolden,
110 hb_font_set_synthetic_slant (font, slant);
112 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
113 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
114 hb_font_set_scale (font, scale_x, scale_y);
117 hb_font_set_var_named_instance (font, named_instance);
118 hb_font_set_variations (font, variations, num_variations);
121 void (*set_font_funcs) (hb_font_t *) = nullptr;
124 set_font_funcs = supported_font_funcs[0].func;
128 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
129 if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
131 set_font_funcs = supported_font_funcs[i].func;
136 GString *s = g_string_new (nullptr);
137 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
140 g_string_append_c (s, '/');
141 g_string_append (s, supported_font_funcs[i].name);
143 g_string_append_c (s, '\n');
144 char *p = g_string_free (s, FALSE);
145 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
146 "Unknown font function implementation `%s'; supported values are: %s; default is %s",
149 supported_font_funcs[0].name);
154 set_font_funcs (font);
156 hb_ft_font_set_load_flags (font, ft_load_flags);
161 hb_font_t *old_font = font;
162 font = hb_font_create_sub_font (old_font);
163 hb_font_set_scale (old_font, scale_x * 2, scale_y * 2);
164 hb_font_destroy (old_font);
170 parse_variations (const char *name G_GNUC_UNUSED,
173 GError **error G_GNUC_UNUSED)
175 font_options_t *font_opts = (font_options_t *) data;
176 char *s = (char *) arg;
179 font_opts->num_variations = 0;
180 g_free (font_opts->variations);
181 font_opts->variations = nullptr;
186 /* count the variations first, so we can allocate memory */
189 font_opts->num_variations++;
190 p = strpbrk (p, ", ");
195 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
196 if (!font_opts->variations)
199 /* now do the actual parsing */
201 font_opts->num_variations = 0;
203 char *end = strpbrk (p, ", ");
204 if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
205 font_opts->num_variations++;
206 p = end ? end + 1 : nullptr;
214 parse_font_size (const char *name G_GNUC_UNUSED,
217 GError **error G_GNUC_UNUSED)
219 font_options_t *font_opts = (font_options_t *) data;
220 if (0 == strcmp (arg, "upem"))
222 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
225 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
226 case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
229 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
230 "%s argument should be one or two space-separated numbers",
237 parse_font_ppem (const char *name G_GNUC_UNUSED,
240 GError **error G_GNUC_UNUSED)
242 font_options_t *font_opts = (font_options_t *) data;
243 switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
244 case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
247 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
248 "%s argument should be one or two space-separated numbers",
255 parse_font_embolden (const char *name G_GNUC_UNUSED,
258 GError **error G_GNUC_UNUSED)
260 font_options_t *font_opts = (font_options_t *) data;
261 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->x_embolden, &font_opts->y_embolden)) {
262 case 1: font_opts->y_embolden = font_opts->x_embolden; HB_FALLTHROUGH;
265 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
266 "%s argument should be one or two space-separated numbers",
273 parse_font_bold (const char *name G_GNUC_UNUSED,
276 GError **error G_GNUC_UNUSED)
278 font_options_t *font_opts = (font_options_t *) data;
279 font_opts->embolden_in_place = false;
280 return parse_font_embolden ( name, arg, data, error);
284 parse_font_grade (const char *name G_GNUC_UNUSED,
287 GError **error G_GNUC_UNUSED)
289 font_options_t *font_opts = (font_options_t *) data;
290 font_opts->embolden_in_place = true;
291 return parse_font_embolden ( name, arg, data, error);
295 font_options_t::add_options (option_parser_t *parser)
297 face_options_t::add_options (parser);
299 char *text = nullptr;
302 static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
303 "No supported font-funcs found.");
304 GString *s = g_string_new (nullptr);
305 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
306 supported_font_funcs[0].name,
307 supported_font_funcs[0].name);
308 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
310 g_string_append_c (s, '/');
311 g_string_append (s, supported_font_funcs[i].name);
313 text = g_string_free (s, FALSE);
314 parser->free_later (text);
317 char *font_size_text;
318 if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
319 font_size_text = (char *) "Font size (default: upem)";
322 font_size_text = g_strdup_printf ("Font size (default: %u)", DEFAULT_FONT_SIZE);
323 parser->free_later (font_size_text);
326 int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
327 GOptionEntry entries[] =
329 {"font-size", 0, font_size_flags,
330 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
331 {"font-ppem", 0, font_size_flags,
332 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
333 {"font-ptem", 0, font_size_flags,
334 G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
335 {"font-bold", 0, font_size_flags,
336 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_bold, "Set synthetic bold (default: 0)", "1/2 numbers; eg. 0.05"},
337 {"font-grade", 0, font_size_flags,
338 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_grade, "Set synthetic grade (default: 0)", "1/2 numbers; eg. 0.05"},
339 {"font-slant", 0, font_size_flags,
340 G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"},
341 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
342 {"sub-font", 0, G_OPTION_FLAG_HIDDEN,
343 G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"},
344 {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
347 parser->add_group (entries,
349 "Font-instance options:",
350 "Options for the font instance",
352 false /* We add below. */);
355 const gchar *variations_help = "Comma-separated list of font variations\n"
357 " Variations are set globally. The format for specifying variation settings\n"
358 " follows. All valid CSS font-variation-settings values other than 'normal'\n"
359 " and 'inherited' are also accepted, though, not documented below.\n"
361 " The format is a tag, optionally followed by an equals sign, followed by a\n"
362 " number. For example:\n"
367 GOptionEntry entries2[] =
369 {"named-instance", 0, 0, G_OPTION_ARG_INT, &this->named_instance, "Set named-instance index (default: none)", "index"},
370 {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
373 parser->add_group (entries2,
375 "Variations options:",
376 "Options for font variations used",