blob: ff596d0404c63fe32ed659e904bad0456700ea5b [file] [log] [blame]
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +02001/*
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#include "options.hh"
28
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040029#if HAVE_FREETYPE
30#include <hb-ft.h>
31#endif
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +020032
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +020033
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040034bool debug = FALSE;
35
36static gchar *
37shapers_to_string (void)
38{
39 GString *shapers = g_string_new (NULL);
40 const char **shaper_list = hb_shape_list_shapers ();
41
42 for (; *shaper_list; shaper_list++) {
43 g_string_append (shapers, *shaper_list);
44 g_string_append_c (shapers, ',');
45 }
46 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
47
48 return g_string_free (shapers, FALSE);
49}
50
51static G_GNUC_NORETURN gboolean
52show_version (const char *name G_GNUC_UNUSED,
53 const char *arg G_GNUC_UNUSED,
54 gpointer data G_GNUC_UNUSED,
55 GError **error G_GNUC_UNUSED)
56{
57 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
58
59 char *shapers = shapers_to_string ();
60 g_printf ("Available shapers: %s\n", shapers);
61 g_free (shapers);
62 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
63 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
64
65 exit(0);
66}
67
68
69void
70option_parser_t::add_main_options (void)
71{
72 GOptionEntry entries[] =
73 {
74 {"version", 0, G_OPTION_FLAG_NO_ARG,
75 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL},
76 {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL},
77 {NULL}
78 };
79 g_option_context_add_main_entries (context, entries, NULL);
80}
81
82static gboolean
83pre_parse (GOptionContext *context G_GNUC_UNUSED,
84 GOptionGroup *group G_GNUC_UNUSED,
85 gpointer data,
86 GError **error)
87{
88 option_group_t *option_group = (option_group_t *) data;
89 option_group->pre_parse (error);
90 return *error == NULL;
91}
92
93static gboolean
94post_parse (GOptionContext *context G_GNUC_UNUSED,
95 GOptionGroup *group G_GNUC_UNUSED,
96 gpointer data,
97 GError **error)
98{
99 option_group_t *option_group = static_cast<option_group_t *>(data);
100 option_group->post_parse (error);
101 return *error == NULL;
102}
103
104void
105option_parser_t::add_group (GOptionEntry *entries,
106 const gchar *name,
107 const gchar *description,
108 const gchar *help_description,
109 option_group_t *option_group)
110{
111 GOptionGroup *group = g_option_group_new (name, description, help_description,
112 static_cast<gpointer>(option_group), NULL);
113 g_option_group_add_entries (group, entries);
114 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
115 g_option_context_add_group (context, group);
116}
117
118void
119option_parser_t::parse (int *argc, char ***argv)
120{
121 GError *parse_error = NULL;
122 if (!g_option_context_parse (context, argc, argv, &parse_error))
123 {
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400124 if (parse_error != NULL) {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400125 fail (TRUE, "%s", parse_error->message);
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400126 //g_error_free (parse_error);
127 } else
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400128 fail (TRUE, "Option parse error");
129 }
130}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200131
132
133static gboolean
134parse_margin (const char *name G_GNUC_UNUSED,
135 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400136 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200137 GError **error G_GNUC_UNUSED)
138{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400139 view_options_t *view_opts = (view_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200140 view_options_t::margin_t &m = view_opts->margin;
Behdad Esfahbod97796452011-08-15 19:03:43 +0200141 switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200142 case 1: m.r = m.t;
143 case 2: m.b = m.t;
144 case 3: m.l = m.r;
145 case 4: return TRUE;
146 default:
147 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
148 "%s argument should be one to four space-separated numbers",
149 name);
150 return FALSE;
151 }
152}
153
154
155static gboolean
156parse_shapers (const char *name G_GNUC_UNUSED,
157 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400158 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200159 GError **error G_GNUC_UNUSED)
160{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400161 shape_options_t *shape_opts = (shape_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200162 shape_opts->shapers = g_strsplit (arg, ",", 0);
163 return TRUE;
164}
165
166
167static void
168parse_space (char **pp)
169{
170 char c;
171#define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
172 while (c = **pp, ISSPACE (c))
173 (*pp)++;
174#undef ISSPACE
175}
176
177static hb_bool_t
178parse_char (char **pp, char c)
179{
180 parse_space (pp);
181
182 if (**pp != c)
183 return FALSE;
184
185 (*pp)++;
186 return TRUE;
187}
188
189static hb_bool_t
190parse_uint (char **pp, unsigned int *pv)
191{
192 char *p = *pp;
193 unsigned int v;
194
195 v = strtol (p, pp, 0);
196
197 if (p == *pp)
198 return FALSE;
199
200 *pv = v;
201 return TRUE;
202}
203
204
205static hb_bool_t
206parse_feature_value_prefix (char **pp, hb_feature_t *feature)
207{
208 if (parse_char (pp, '-'))
209 feature->value = 0;
210 else {
211 parse_char (pp, '+');
212 feature->value = 1;
213 }
214
215 return TRUE;
216}
217
218static hb_bool_t
219parse_feature_tag (char **pp, hb_feature_t *feature)
220{
221 char *p = *pp, c;
222
223 parse_space (pp);
224
225#define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
226 while (c = **pp, ISALNUM(c))
227 (*pp)++;
228#undef ISALNUM
229
230 if (p == *pp)
231 return FALSE;
232
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200233 feature->tag = hb_tag_from_string (p, *pp - p);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200234 return TRUE;
235}
236
237static hb_bool_t
238parse_feature_indices (char **pp, hb_feature_t *feature)
239{
240 hb_bool_t has_start;
241
242 feature->start = 0;
243 feature->end = (unsigned int) -1;
244
245 if (!parse_char (pp, '['))
246 return TRUE;
247
248 has_start = parse_uint (pp, &feature->start);
249
250 if (parse_char (pp, ':')) {
251 parse_uint (pp, &feature->end);
252 } else {
253 if (has_start)
254 feature->end = feature->start + 1;
255 }
256
257 return parse_char (pp, ']');
258}
259
260static hb_bool_t
261parse_feature_value_postfix (char **pp, hb_feature_t *feature)
262{
263 return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
264}
265
266
267static hb_bool_t
268parse_one_feature (char **pp, hb_feature_t *feature)
269{
270 return parse_feature_value_prefix (pp, feature) &&
271 parse_feature_tag (pp, feature) &&
272 parse_feature_indices (pp, feature) &&
273 parse_feature_value_postfix (pp, feature) &&
274 (parse_char (pp, ',') || **pp == '\0');
275}
276
277static void
278skip_one_feature (char **pp)
279{
280 char *e;
281 e = strchr (*pp, ',');
282 if (e)
283 *pp = e + 1;
284 else
285 *pp = *pp + strlen (*pp);
286}
287
288static gboolean
289parse_features (const char *name G_GNUC_UNUSED,
290 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400291 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200292 GError **error G_GNUC_UNUSED)
293{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400294 shape_options_t *shape_opts = (shape_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200295 char *s = (char *) arg;
296 char *p;
297
298 shape_opts->num_features = 0;
299 shape_opts->features = NULL;
300
301 if (!*s)
302 return TRUE;
303
304 /* count the features first, so we can allocate memory */
305 p = s;
306 do {
307 shape_opts->num_features++;
308 p = strchr (p, ',');
309 if (p)
310 p++;
311 } while (p);
312
313 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
314
315 /* now do the actual parsing */
316 p = s;
317 shape_opts->num_features = 0;
318 while (*p) {
319 if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features]))
320 shape_opts->num_features++;
321 else
322 skip_one_feature (&p);
323 }
324
325 return TRUE;
326}
327
328
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400329void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400330view_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400331{
332 GOptionEntry entries[] =
333 {
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400334 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL},
335 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"},
336 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"},
337 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400338 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"},
339 {NULL}
340 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400341 parser->add_group (entries,
342 "view",
343 "View options:",
344 "Options controlling the output rendering",
345 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400346}
347
348void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400349shape_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400350{
351 GOptionEntry entries[] =
352 {
353 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers", "list"},
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400354 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
355 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
356 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400357 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, "Font features to apply to text", "TODO"},
358 {NULL}
359 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400360 parser->add_group (entries,
361 "shape",
362 "Shape options:",
363 "Options controlling the shaping process",
364 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400365}
366
367void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400368font_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400369{
370 GOptionEntry entries[] =
371 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400372 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"},
373 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"},
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400374 {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &this->font_size, "Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400375 {NULL}
376 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400377 parser->add_group (entries,
378 "font",
379 "Font options:",
380 "Options controlling the font",
381 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400382}
383
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200384void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400385text_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200386{
387 GOptionEntry entries[] =
388 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400389 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"},
390 {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"},
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200391 {NULL}
392 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400393 parser->add_group (entries,
394 "text",
395 "Text options:",
396 "Options controlling the input text",
397 this);
398}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200399
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400400void
401output_options_t::add_options (option_parser_t *parser)
402{
403 GOptionEntry entries[] =
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200404 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400405 {"output", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
406 {"format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, "Set output format", "format"},
407 {NULL}
408 };
409 parser->add_group (entries,
410 "output",
411 "Output options:",
412 "Options controlling the output",
413 this);
414}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200415
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400416
417
418hb_font_t *
419font_options_t::get_font (void) const
420{
421 if (font)
422 return font;
423
424 hb_blob_t *blob = NULL;
425
426 /* Create the blob */
427 {
Behdad Esfahbod44511682011-09-16 00:38:19 -0400428 char *font_data;
429 unsigned int len = 0;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400430 hb_destroy_func_t destroy;
431 void *user_data;
432 hb_memory_mode_t mm;
433
Behdad Esfahbod44511682011-09-16 00:38:19 -0400434 /* This is a hell of a lot of code for just reading a file! */
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400435 if (!font_file)
436 fail (TRUE, "No font file set");
437
Behdad Esfahbod44511682011-09-16 00:38:19 -0400438 if (0 == strcmp (font_file, "-")) {
439 /* read it */
440 GString *gs = g_string_new (NULL);
441 char buf[BUFSIZ];
442#if HAVE_IO_H
443 _setmode (fileno (stdin), O_BINARY);
444#endif
445 while (!feof (stdin)) {
446 size_t ret = fread (buf, 1, sizeof (buf), stdin);
447 if (ferror (stdin))
448 fail (FALSE, "Failed reading font from standard input: %s",
449 strerror (errno));
450 g_string_append_len (gs, buf, ret);
451 }
452 len = gs->len;
453 font_data = g_string_free (gs, FALSE);
454 user_data = font_data;
455 destroy = (hb_destroy_func_t) g_free;
456 mm = HB_MEMORY_MODE_WRITABLE;
457 } else {
458 GMappedFile *mf = g_mapped_file_new (font_file, FALSE, NULL);
459 if (mf) {
460 font_data = g_mapped_file_get_contents (mf);
461 len = g_mapped_file_get_length (mf);
462 if (len) {
463 destroy = (hb_destroy_func_t) g_mapped_file_unref;
464 user_data = (void *) mf;
465 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
466 } else
467 g_mapped_file_unref (mf);
468 }
469 if (!len) {
470 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
471 * Try reading.
472 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
473 GError *error = NULL;
474 gsize l;
475 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
476 len = l;
477 destroy = (hb_destroy_func_t) g_free;
478 user_data = (void *) font_data;
479 mm = HB_MEMORY_MODE_WRITABLE;
480 } else {
Behdad Esfahbod674ee582011-09-16 00:54:05 -0400481 fail (FALSE, "%s", error->message);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400482 //g_error_free (error);
483 }
484 }
485 }
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400486
487 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200488 }
489
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400490 /* Create the face */
491 hb_face_t *face = hb_face_create (blob, face_index);
492 hb_blob_destroy (blob);
493
494
495 font = hb_font_create (face);
496
497 unsigned int upem = hb_face_get_upem (face);
Behdad Esfahbod7bf6ecd2011-09-16 01:11:30 -0400498 hb_font_set_scale (font, upem, upem);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400499 hb_face_destroy (face);
500
501#if HAVE_FREETYPE
502 hb_ft_font_set_funcs (font);
503#endif
504
505 return font;
506}
507
508
509const char *
510text_options_t::get_line (unsigned int *len)
511{
512 if (!text) {
513 if (!text_file)
514 fail (TRUE, "At least one of text or text-file must be set");
515
516 GMappedFile *mf = g_mapped_file_new (text_file, FALSE, NULL);
517 if (!mf)
Behdad Esfahbod639b5952011-09-15 18:09:49 -0400518 fail (FALSE, "Failed opening text file `%s'", g_filename_display_name (text_file));
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400519 text = g_mapped_file_get_contents (mf);
520 text_len = g_mapped_file_get_length (mf);
521 }
522
523 if (text_len == (unsigned int) -1)
524 text_len = strlen (text);
525
526 if (!text_len) {
527 *len = 0;
528 return NULL;
529 }
530
531 const char *ret = text;
532 const char *p = (const char *) memchr (text, '\n', text_len);
533 unsigned int ret_len;
534 if (!p) {
535 ret_len = text_len;
536 text += ret_len;
537 text_len = 0;
538 } else {
539 ret_len = p - ret;
540 text += ret_len + 1;
541 text_len -= ret_len + 1;
542 }
543
544 *len = ret_len;
545 return ret;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200546}
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400547
548
549FILE *
550output_options_t::get_file_handle (void)
551{
552 if (fp)
553 return fp;
554
555 if (output_file)
556 fp = fopen (output_file, "wb");
557 else {
558#if HAVE_IO_H
559 _setmode (fileno (stdout), O_BINARY);
560#endif
561 fp = stdout;
562 }
563 if (!fp)
564 fail (FALSE, "Cannot open output file `%s': %s",
565 g_filename_display_name (output_file), strerror (errno));
566
567 return fp;
568}