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 #ifdef HAVE_FREETYPE
33 #include <hb-ft.h>
34 #endif
35 #include <hb-ot.h>
36
37 #define FONT_SIZE_UPEM 0x7FFFFFFF
38 #define FONT_SIZE_NONE 0
39
40 extern const unsigned DEFAULT_FONT_SIZE;
41 extern const unsigned SUBPIXEL_BITS;
42
43 struct font_options_t : face_options_t
44 {
~font_options_tfont_options_t45 ~font_options_t ()
46 {
47 #ifndef HB_NO_VAR
48 free (variations);
49 #endif
50 g_free (font_funcs);
51 hb_font_destroy (font);
52 }
53
54 void add_options (option_parser_t *parser);
55
56 void post_parse (GError **error);
57
58 hb_bool_t sub_font = false;
59 #ifndef HB_NO_VAR
60 hb_variation_t *variations = nullptr;
61 unsigned int num_variations = 0;
62 #endif
63 int x_ppem = 0;
64 int y_ppem = 0;
65 double ptem = 0.;
66 double x_embolden = 0.;
67 double y_embolden = 0.;
68 hb_bool_t embolden_in_place = false;
69 double slant = 0.;
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;
76
77 hb_font_t *font = nullptr;
78 };
79
80
81 static struct supported_font_funcs_t {
82 char name[4];
83 void (*func) (hb_font_t *);
84 } supported_font_funcs[] =
85 {
86 {"ot", hb_ot_font_set_funcs},
87 #ifdef HAVE_FREETYPE
88 {"ft", hb_ft_font_set_funcs},
89 #endif
90 };
91
92
93 void
post_parse(GError ** error)94 font_options_t::post_parse (GError **error)
95 {
96 assert (!font);
97 font = hb_font_create (face);
98
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);
103
104 hb_font_set_ppem (font, x_ppem, y_ppem);
105 hb_font_set_ptem (font, ptem);
106
107 hb_font_set_synthetic_bold (font,
108 (float) x_embolden, (float) y_embolden,
109 embolden_in_place);
110 hb_font_set_synthetic_slant (font, slant);
111
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);
115
116 #ifndef HB_NO_VAR
117 hb_font_set_var_named_instance (font, named_instance);
118 hb_font_set_variations (font, variations, num_variations);
119 #endif
120
121 void (*set_font_funcs) (hb_font_t *) = nullptr;
122 if (!font_funcs)
123 {
124 set_font_funcs = supported_font_funcs[0].func;
125 }
126 else
127 {
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))
130 {
131 set_font_funcs = supported_font_funcs[i].func;
132 break;
133 }
134 if (!set_font_funcs)
135 {
136 GString *s = g_string_new (nullptr);
137 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
138 {
139 if (i)
140 g_string_append_c (s, '/');
141 g_string_append (s, supported_font_funcs[i].name);
142 }
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",
147 font_funcs,
148 p,
149 supported_font_funcs[0].name);
150 free (p);
151 return;
152 }
153 }
154 set_font_funcs (font);
155 #ifdef HAVE_FREETYPE
156 hb_ft_font_set_load_flags (font, ft_load_flags);
157 #endif
158
159 if (sub_font)
160 {
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);
165 }
166 }
167
168 #ifndef HB_NO_VAR
169 static gboolean
parse_variations(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)170 parse_variations (const char *name G_GNUC_UNUSED,
171 const char *arg,
172 gpointer data,
173 GError **error G_GNUC_UNUSED)
174 {
175 font_options_t *font_opts = (font_options_t *) data;
176 char *s = (char *) arg;
177 char *p;
178
179 font_opts->num_variations = 0;
180 g_free (font_opts->variations);
181 font_opts->variations = nullptr;
182
183 if (!*s)
184 return true;
185
186 /* count the variations first, so we can allocate memory */
187 p = s;
188 do {
189 font_opts->num_variations++;
190 p = strpbrk (p, ", ");
191 if (p)
192 p++;
193 } while (p);
194
195 font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
196 if (!font_opts->variations)
197 return false;
198
199 /* now do the actual parsing */
200 p = s;
201 font_opts->num_variations = 0;
202 while (p && *p) {
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;
207 }
208
209 return true;
210 }
211 #endif
212
213 static gboolean
parse_font_size(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)214 parse_font_size (const char *name G_GNUC_UNUSED,
215 const char *arg,
216 gpointer data,
217 GError **error G_GNUC_UNUSED)
218 {
219 font_options_t *font_opts = (font_options_t *) data;
220 if (0 == strcmp (arg, "upem"))
221 {
222 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
223 return true;
224 }
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;
227 case 2: return true;
228 default:
229 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
230 "%s argument should be one or two space-separated numbers",
231 name);
232 return false;
233 }
234 }
235
236 static gboolean
parse_font_ppem(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)237 parse_font_ppem (const char *name G_GNUC_UNUSED,
238 const char *arg,
239 gpointer data,
240 GError **error G_GNUC_UNUSED)
241 {
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;
245 case 2: return true;
246 default:
247 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
248 "%s argument should be one or two space-separated numbers",
249 name);
250 return false;
251 }
252 }
253
254 static gboolean
parse_font_embolden(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)255 parse_font_embolden (const char *name G_GNUC_UNUSED,
256 const char *arg,
257 gpointer data,
258 GError **error G_GNUC_UNUSED)
259 {
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;
263 case 2: return true;
264 default:
265 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
266 "%s argument should be one or two space-separated numbers",
267 name);
268 return false;
269 }
270 }
271
272 static gboolean
parse_font_bold(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)273 parse_font_bold (const char *name G_GNUC_UNUSED,
274 const char *arg,
275 gpointer data,
276 GError **error G_GNUC_UNUSED)
277 {
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);
281 }
282
283 static gboolean
parse_font_grade(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)284 parse_font_grade (const char *name G_GNUC_UNUSED,
285 const char *arg,
286 gpointer data,
287 GError **error G_GNUC_UNUSED)
288 {
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);
292 }
293
294 void
add_options(option_parser_t * parser)295 font_options_t::add_options (option_parser_t *parser)
296 {
297 face_options_t::add_options (parser);
298
299 char *text = nullptr;
300
301 {
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++)
309 {
310 g_string_append_c (s, '/');
311 g_string_append (s, supported_font_funcs[i].name);
312 }
313 text = g_string_free (s, FALSE);
314 parser->free_later (text);
315 }
316
317 char *font_size_text;
318 if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
319 font_size_text = (char *) "Font size (default: upem)";
320 else
321 {
322 font_size_text = g_strdup_printf ("Font size (default: %u)", DEFAULT_FONT_SIZE);
323 parser->free_later (font_size_text);
324 }
325
326 int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
327 GOptionEntry entries[] =
328 {
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"},
345 {nullptr}
346 };
347 parser->add_group (entries,
348 "font",
349 "Font-instance options:",
350 "Options for the font instance",
351 this,
352 false /* We add below. */);
353
354 #ifndef HB_NO_VAR
355 const gchar *variations_help = "Comma-separated list of font variations\n"
356 "\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"
360 "\n"
361 " The format is a tag, optionally followed by an equals sign, followed by a\n"
362 " number. For example:\n"
363 "\n"
364 " \"wght=500\"\n"
365 " \"slnt=-7.5\"";
366
367 GOptionEntry entries2[] =
368 {
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"},
371 {nullptr}
372 };
373 parser->add_group (entries2,
374 "variations",
375 "Variations options:",
376 "Options for font variations used",
377 this);
378 #endif
379 }
380
381 #endif
382