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
49 hb_font_destroy (font);
52 void add_options (option_parser_t *parser);
54 void post_parse (GError **error);
56 hb_variation_t *variations = nullptr;
57 unsigned int num_variations = 0;
61 unsigned int subpixel_bits = SUBPIXEL_BITS;
62 mutable double font_size_x = DEFAULT_FONT_SIZE;
63 mutable double font_size_y = DEFAULT_FONT_SIZE;
64 char *font_funcs = nullptr;
65 int ft_load_flags = 2;
67 hb_font_t *font = nullptr;
71 static struct supported_font_funcs_t {
73 void (*func) (hb_font_t *);
74 } supported_font_funcs[] =
77 {"ft", hb_ft_font_set_funcs},
79 {"ot", hb_ot_font_set_funcs},
84 font_options_t::post_parse (GError **error)
87 font = hb_font_create (face);
89 if (font_size_x == FONT_SIZE_UPEM)
90 font_size_x = hb_face_get_upem (face);
91 if (font_size_y == FONT_SIZE_UPEM)
92 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);
97 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
98 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
99 hb_font_set_scale (font, scale_x, scale_y);
101 hb_font_set_variations (font, variations, num_variations);
103 void (*set_font_funcs) (hb_font_t *) = nullptr;
106 set_font_funcs = supported_font_funcs[0].func;
110 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
111 if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
113 set_font_funcs = supported_font_funcs[i].func;
118 GString *s = g_string_new (nullptr);
119 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
122 g_string_append_c (s, '/');
123 g_string_append (s, supported_font_funcs[i].name);
125 g_string_append_c (s, '\n');
126 char *p = g_string_free (s, FALSE);
127 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
128 "Unknown font function implementation `%s'; supported values are: %s; default is %s",
131 supported_font_funcs[0].name);
136 set_font_funcs (font);
138 hb_ft_font_set_load_flags (font, ft_load_flags);
144 parse_variations (const char *name G_GNUC_UNUSED,
147 GError **error G_GNUC_UNUSED)
149 font_options_t *font_opts = (font_options_t *) data;
150 char *s = (char *) arg;
153 font_opts->num_variations = 0;
154 g_free (font_opts->variations);
155 font_opts->variations = nullptr;
160 /* count the variations first, so we can allocate memory */
163 font_opts->num_variations++;
169 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
170 if (!font_opts->variations)
173 /* now do the actual parsing */
175 font_opts->num_variations = 0;
177 char *end = strchr (p, ',');
178 if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
179 font_opts->num_variations++;
180 p = end ? end + 1 : nullptr;
187 parse_font_size (const char *name G_GNUC_UNUSED,
190 GError **error G_GNUC_UNUSED)
192 font_options_t *font_opts = (font_options_t *) data;
193 if (0 == strcmp (arg, "upem"))
195 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
198 switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
199 case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
202 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
203 "%s argument should be one or two space-separated numbers",
210 parse_font_ppem (const char *name G_GNUC_UNUSED,
213 GError **error G_GNUC_UNUSED)
215 font_options_t *font_opts = (font_options_t *) data;
216 switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
217 case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
220 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
221 "%s argument should be one or two space-separated numbers",
228 font_options_t::add_options (option_parser_t *parser)
230 face_options_t::add_options (parser);
232 char *text = nullptr;
235 static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
236 "No supported font-funcs found.");
237 GString *s = g_string_new (nullptr);
238 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
239 supported_font_funcs[0].name,
240 supported_font_funcs[0].name);
241 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
243 g_string_append_c (s, '/');
244 g_string_append (s, supported_font_funcs[i].name);
246 text = g_string_free (s, FALSE);
247 parser->free_later (text);
250 char *font_size_text;
251 if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
252 font_size_text = (char *) "Font size (default: upem)";
255 font_size_text = g_strdup_printf ("Font size (default: %d)", DEFAULT_FONT_SIZE);
256 parser->free_later (font_size_text);
259 int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
260 GOptionEntry entries[] =
262 {"font-size", 0, font_size_flags,
263 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 integers or 'upem'"},
264 {"font-ppem", 0, font_size_flags,
265 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_ppem, "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
267 G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"},
268 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
269 {"ft-load-flags", 0, 0, G_OPTION_ARG_INT, &this->ft_load_flags, "Set FreeType load-flags (default: 2)", "integer"},
272 parser->add_group (entries,
274 "Font-instance options:",
275 "Options for the font instance",
277 false /* We add below. */);
279 const gchar *variations_help = "Comma-separated list of font variations\n"
281 " Variations are set globally. The format for specifying variation settings\n"
282 " follows. All valid CSS font-variation-settings values other than 'normal'\n"
283 " and 'inherited' are also accepted, though, not documented below.\n"
285 " The format is a tag, optionally followed by an equals sign, followed by a\n"
286 " number. For example:\n"
291 GOptionEntry entries2[] =
293 {"variations", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_variations, variations_help, "list"},
296 parser->add_group (entries2,
298 "Variations options:",
299 "Options for font variations used",