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