Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / util / font-options.hh
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 {
45   ~font_options_t ()
46   {
47     free (variations);
48     g_free (font_funcs);
49     hb_font_destroy (font);
50   }
51
52   void add_options (option_parser_t *parser);
53
54   void post_parse (GError **error);
55
56   hb_variation_t *variations = nullptr;
57   unsigned int num_variations = 0;
58   int x_ppem = 0;
59   int y_ppem = 0;
60   double ptem = 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;
66
67   hb_font_t *font = nullptr;
68 };
69
70
71 static struct supported_font_funcs_t {
72         char name[4];
73         void (*func) (hb_font_t *);
74 } supported_font_funcs[] =
75 {
76 #ifdef HAVE_FREETYPE
77   {"ft",        hb_ft_font_set_funcs},
78 #endif
79   {"ot",        hb_ot_font_set_funcs},
80 };
81
82
83 void
84 font_options_t::post_parse (GError **error)
85 {
86   assert (!font);
87   font = hb_font_create (face);
88
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);
93
94   hb_font_set_ppem (font, x_ppem, y_ppem);
95   hb_font_set_ptem (font, ptem);
96
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);
100
101   hb_font_set_variations (font, variations, num_variations);
102
103   void (*set_font_funcs) (hb_font_t *) = nullptr;
104   if (!font_funcs)
105   {
106     set_font_funcs = supported_font_funcs[0].func;
107   }
108   else
109   {
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))
112       {
113         set_font_funcs = supported_font_funcs[i].func;
114         break;
115       }
116     if (!set_font_funcs)
117     {
118       GString *s = g_string_new (nullptr);
119       for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
120       {
121         if (i)
122           g_string_append_c (s, '/');
123         g_string_append (s, supported_font_funcs[i].name);
124       }
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",
129                    font_funcs,
130                    p,
131                    supported_font_funcs[0].name);
132       free (p);
133       return;
134     }
135   }
136   set_font_funcs (font);
137 #ifdef HAVE_FREETYPE
138   hb_ft_font_set_load_flags (font, ft_load_flags);
139 #endif
140 }
141
142
143 static gboolean
144 parse_variations (const char *name G_GNUC_UNUSED,
145                   const char *arg,
146                   gpointer    data,
147                   GError    **error G_GNUC_UNUSED)
148 {
149   font_options_t *font_opts = (font_options_t *) data;
150   char *s = (char *) arg;
151   char *p;
152
153   font_opts->num_variations = 0;
154   g_free (font_opts->variations);
155   font_opts->variations = nullptr;
156
157   if (!*s)
158     return true;
159
160   /* count the variations first, so we can allocate memory */
161   p = s;
162   do {
163     font_opts->num_variations++;
164     p = strchr (p, ',');
165     if (p)
166       p++;
167   } while (p);
168
169   font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
170   if (!font_opts->variations)
171     return false;
172
173   /* now do the actual parsing */
174   p = s;
175   font_opts->num_variations = 0;
176   while (p && *p) {
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;
181   }
182
183   return true;
184 }
185
186 static gboolean
187 parse_font_size (const char *name G_GNUC_UNUSED,
188                  const char *arg,
189                  gpointer    data,
190                  GError    **error G_GNUC_UNUSED)
191 {
192   font_options_t *font_opts = (font_options_t *) data;
193   if (0 == strcmp (arg, "upem"))
194   {
195     font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
196     return true;
197   }
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;
200     case 2: return true;
201     default:
202       g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
203                    "%s argument should be one or two space-separated numbers",
204                    name);
205       return false;
206   }
207 }
208
209 static gboolean
210 parse_font_ppem (const char *name G_GNUC_UNUSED,
211                  const char *arg,
212                  gpointer    data,
213                  GError    **error G_GNUC_UNUSED)
214 {
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;
218     case 2: return true;
219     default:
220       g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
221                    "%s argument should be one or two space-separated numbers",
222                    name);
223       return false;
224   }
225 }
226
227 void
228 font_options_t::add_options (option_parser_t *parser)
229 {
230   face_options_t::add_options (parser);
231
232   char *text = nullptr;
233
234   {
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++)
242     {
243       g_string_append_c (s, '/');
244       g_string_append (s, supported_font_funcs[i].name);
245     }
246     text = g_string_free (s, FALSE);
247     parser->free_later (text);
248   }
249
250   char *font_size_text;
251   if (DEFAULT_FONT_SIZE == FONT_SIZE_UPEM)
252     font_size_text = (char *) "Font size (default: upem)";
253   else
254   {
255     font_size_text = g_strdup_printf ("Font size (default: %d)", DEFAULT_FONT_SIZE);
256     parser->free_later (font_size_text);
257   }
258
259   int font_size_flags = DEFAULT_FONT_SIZE == FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0;
260   GOptionEntry entries[] =
261   {
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"},
266     {"font-ptem",       0, 0,
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"},
270     {nullptr}
271   };
272   parser->add_group (entries,
273                      "font",
274                      "Font-instance options:",
275                      "Options for the font instance",
276                      this,
277                      false /* We add below. */);
278
279   const gchar *variations_help = "Comma-separated list of font variations\n"
280     "\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"
284     "\n"
285     "    The format is a tag, optionally followed by an equals sign, followed by a\n"
286     "    number. For example:\n"
287     "\n"
288     "      \"wght=500\"\n"
289     "      \"slnt=-7.5\"";
290
291   GOptionEntry entries2[] =
292   {
293     {"variations",      0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_variations,   variations_help,        "list"},
294     {nullptr}
295   };
296   parser->add_group (entries2,
297                      "variations",
298                      "Variations options:",
299                      "Options for font variations used",
300                      this);
301 }
302
303 #endif