blob: 2bf2c586ac6f3c415d6b4b166b9a549a1cfeecbb [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 Esfahbod5ddd9cc2011-09-16 16:40:44 -040029#ifdef HAVE_FREETYPE
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040030#include <hb-ft.h>
31#endif
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +020032
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +020033
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040034void
35fail (hb_bool_t suggest_help, const char *format, ...)
36{
37 const char *msg;
38
39 va_list vap;
40 va_start (vap, format);
41 msg = g_strdup_vprintf (format, vap);
42 const char *prgname = g_get_prgname ();
43 g_printerr ("%s: %s\n", prgname, msg);
44 if (suggest_help)
45 g_printerr ("Try `%s --help' for more information.\n", prgname);
46
47 exit (1);
48}
49
50
Behdad Esfahbod088c1e22011-09-20 14:43:55 -040051hb_bool_t debug = FALSE;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040052
53static gchar *
54shapers_to_string (void)
55{
56 GString *shapers = g_string_new (NULL);
57 const char **shaper_list = hb_shape_list_shapers ();
58
59 for (; *shaper_list; shaper_list++) {
60 g_string_append (shapers, *shaper_list);
61 g_string_append_c (shapers, ',');
62 }
63 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
64
65 return g_string_free (shapers, FALSE);
66}
67
68static G_GNUC_NORETURN gboolean
69show_version (const char *name G_GNUC_UNUSED,
70 const char *arg G_GNUC_UNUSED,
71 gpointer data G_GNUC_UNUSED,
72 GError **error G_GNUC_UNUSED)
73{
74 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
75
76 char *shapers = shapers_to_string ();
77 g_printf ("Available shapers: %s\n", shapers);
78 g_free (shapers);
79 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
80 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
81
82 exit(0);
83}
84
85
86void
87option_parser_t::add_main_options (void)
88{
89 GOptionEntry entries[] =
90 {
91 {"version", 0, G_OPTION_FLAG_NO_ARG,
92 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL},
93 {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL},
94 {NULL}
95 };
96 g_option_context_add_main_entries (context, entries, NULL);
97}
98
99static gboolean
100pre_parse (GOptionContext *context G_GNUC_UNUSED,
101 GOptionGroup *group G_GNUC_UNUSED,
102 gpointer data,
103 GError **error)
104{
105 option_group_t *option_group = (option_group_t *) data;
106 option_group->pre_parse (error);
107 return *error == NULL;
108}
109
110static gboolean
111post_parse (GOptionContext *context G_GNUC_UNUSED,
112 GOptionGroup *group G_GNUC_UNUSED,
113 gpointer data,
114 GError **error)
115{
116 option_group_t *option_group = static_cast<option_group_t *>(data);
117 option_group->post_parse (error);
118 return *error == NULL;
119}
120
121void
122option_parser_t::add_group (GOptionEntry *entries,
123 const gchar *name,
124 const gchar *description,
125 const gchar *help_description,
126 option_group_t *option_group)
127{
128 GOptionGroup *group = g_option_group_new (name, description, help_description,
129 static_cast<gpointer>(option_group), NULL);
130 g_option_group_add_entries (group, entries);
131 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
132 g_option_context_add_group (context, group);
133}
134
135void
136option_parser_t::parse (int *argc, char ***argv)
137{
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400138 setlocale (LC_ALL, "");
139
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400140 GError *parse_error = NULL;
141 if (!g_option_context_parse (context, argc, argv, &parse_error))
142 {
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400143 if (parse_error != NULL) {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400144 fail (TRUE, "%s", parse_error->message);
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400145 //g_error_free (parse_error);
146 } else
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400147 fail (TRUE, "Option parse error");
148 }
149}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200150
151
152static gboolean
153parse_margin (const char *name G_GNUC_UNUSED,
154 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400155 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200156 GError **error G_GNUC_UNUSED)
157{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400158 view_options_t *view_opts = (view_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200159 view_options_t::margin_t &m = view_opts->margin;
Behdad Esfahbod97796452011-08-15 19:03:43 +0200160 switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200161 case 1: m.r = m.t;
162 case 2: m.b = m.t;
163 case 3: m.l = m.r;
164 case 4: return TRUE;
165 default:
166 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
167 "%s argument should be one to four space-separated numbers",
168 name);
169 return FALSE;
170 }
171}
172
173
174static gboolean
175parse_shapers (const char *name G_GNUC_UNUSED,
176 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400177 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200178 GError **error G_GNUC_UNUSED)
179{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400180 shape_options_t *shape_opts = (shape_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200181 shape_opts->shapers = g_strsplit (arg, ",", 0);
182 return TRUE;
183}
184
Behdad Esfahbodfd528c12011-10-12 15:03:58 -0400185static G_GNUC_NORETURN gboolean
186list_shapers (const char *name G_GNUC_UNUSED,
187 const char *arg G_GNUC_UNUSED,
188 gpointer data G_GNUC_UNUSED,
189 GError **error G_GNUC_UNUSED)
190{
191 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
192 g_printf ("%s\n", *shaper);
193
194 exit(0);
195}
196
197
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200198
199static void
200parse_space (char **pp)
201{
202 char c;
203#define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
204 while (c = **pp, ISSPACE (c))
205 (*pp)++;
206#undef ISSPACE
207}
208
209static hb_bool_t
210parse_char (char **pp, char c)
211{
212 parse_space (pp);
213
214 if (**pp != c)
215 return FALSE;
216
217 (*pp)++;
218 return TRUE;
219}
220
221static hb_bool_t
222parse_uint (char **pp, unsigned int *pv)
223{
224 char *p = *pp;
225 unsigned int v;
226
227 v = strtol (p, pp, 0);
228
229 if (p == *pp)
230 return FALSE;
231
232 *pv = v;
233 return TRUE;
234}
235
236
237static hb_bool_t
238parse_feature_value_prefix (char **pp, hb_feature_t *feature)
239{
240 if (parse_char (pp, '-'))
241 feature->value = 0;
242 else {
243 parse_char (pp, '+');
244 feature->value = 1;
245 }
246
247 return TRUE;
248}
249
250static hb_bool_t
251parse_feature_tag (char **pp, hb_feature_t *feature)
252{
253 char *p = *pp, c;
254
255 parse_space (pp);
256
257#define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
258 while (c = **pp, ISALNUM(c))
259 (*pp)++;
260#undef ISALNUM
261
262 if (p == *pp)
263 return FALSE;
264
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200265 feature->tag = hb_tag_from_string (p, *pp - p);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200266 return TRUE;
267}
268
269static hb_bool_t
270parse_feature_indices (char **pp, hb_feature_t *feature)
271{
Behdad Esfahboda0970432012-01-14 17:55:51 -0500272 parse_space (pp);
273
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200274 hb_bool_t has_start;
275
276 feature->start = 0;
277 feature->end = (unsigned int) -1;
278
279 if (!parse_char (pp, '['))
280 return TRUE;
281
282 has_start = parse_uint (pp, &feature->start);
283
284 if (parse_char (pp, ':')) {
285 parse_uint (pp, &feature->end);
286 } else {
287 if (has_start)
288 feature->end = feature->start + 1;
289 }
290
291 return parse_char (pp, ']');
292}
293
294static hb_bool_t
295parse_feature_value_postfix (char **pp, hb_feature_t *feature)
296{
297 return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
298}
299
300
301static hb_bool_t
302parse_one_feature (char **pp, hb_feature_t *feature)
303{
304 return parse_feature_value_prefix (pp, feature) &&
305 parse_feature_tag (pp, feature) &&
306 parse_feature_indices (pp, feature) &&
307 parse_feature_value_postfix (pp, feature) &&
308 (parse_char (pp, ',') || **pp == '\0');
309}
310
311static void
312skip_one_feature (char **pp)
313{
314 char *e;
315 e = strchr (*pp, ',');
316 if (e)
317 *pp = e + 1;
318 else
319 *pp = *pp + strlen (*pp);
320}
321
322static gboolean
323parse_features (const char *name G_GNUC_UNUSED,
324 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400325 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200326 GError **error G_GNUC_UNUSED)
327{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400328 shape_options_t *shape_opts = (shape_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200329 char *s = (char *) arg;
330 char *p;
331
332 shape_opts->num_features = 0;
333 shape_opts->features = NULL;
334
335 if (!*s)
336 return TRUE;
337
338 /* count the features first, so we can allocate memory */
339 p = s;
340 do {
341 shape_opts->num_features++;
342 p = strchr (p, ',');
343 if (p)
344 p++;
345 } while (p);
346
347 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
348
349 /* now do the actual parsing */
350 p = s;
351 shape_opts->num_features = 0;
352 while (*p) {
353 if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features]))
354 shape_opts->num_features++;
355 else
356 skip_one_feature (&p);
357 }
358
359 return TRUE;
360}
361
362
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400363void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400364view_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400365{
366 GOptionEntry entries[] =
367 {
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400368 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL},
369 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"},
370 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"},
371 {"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 -0400372 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"},
Behdad Esfahbod11e51992011-09-19 09:58:55 -0400373 {"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 -0400374 {NULL}
375 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400376 parser->add_group (entries,
377 "view",
378 "View options:",
379 "Options controlling the output rendering",
380 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400381}
382
383void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400384shape_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400385{
386 GOptionEntry entries[] =
387 {
Behdad Esfahbodfd528c12011-10-12 15:03:58 -0400388 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
389 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL},
390 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers to try","list"},
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400391 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
392 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
393 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400394 {NULL}
395 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400396 parser->add_group (entries,
397 "shape",
398 "Shape options:",
399 "Options controlling the shaping process",
400 this);
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500401
402 const gchar *features_help = "\n"
403 "\n"
404 " Comma-separated list of font features to apply to text\n"
405 "\n"
406 " Features can be enabled or disabled, either globally or limited to\n"
407 " specific byte ranges. The format is Python-esque. Here is how it all\n"
408 " works:\n"
409 "\n"
410 " Syntax: Value: Start: End:\n"
411 "\n"
412 " Setting value:\n"
413 " \"kern\" 1 0 ∞ # Turn feature on\n"
414 " \"+kern\" 1 0 ∞ # Turn feature on\n"
415 " \"-kern\" 0 0 ∞ # Turn feature off\n"
416 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
417 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
418 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
419 "\n"
420 " Setting index:\n"
421 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
422 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
423 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
424 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
425 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
426 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
427 "\n"
428 " Mixing it all:\n"
429 "\n"
430 " \"kern[3:5]=0\" 1 3 5 # Turn feature off for range";
431
432 GOptionEntry entries2[] =
433 {
434 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
435 {NULL}
436 };
437 parser->add_group (entries2,
438 "features",
439 "Features options:",
440 "Options controlling the OpenType font features applied",
441 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400442}
443
444void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400445font_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400446{
447 GOptionEntry entries[] =
448 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400449 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"},
450 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400451 {NULL}
452 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400453 parser->add_group (entries,
454 "font",
455 "Font options:",
456 "Options controlling the font",
457 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400458}
459
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200460void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400461text_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200462{
463 GOptionEntry entries[] =
464 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400465 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"},
466 {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"},
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200467 {NULL}
468 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400469 parser->add_group (entries,
470 "text",
471 "Text options:",
472 "Options controlling the input text",
473 this);
474}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200475
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400476void
477output_options_t::add_options (option_parser_t *parser)
478{
479 GOptionEntry entries[] =
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200480 {
Behdad Esfahbodb5afd8f2011-09-19 16:56:21 -0400481 {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
482 {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, "Set output format", "format"},
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400483 {NULL}
484 };
485 parser->add_group (entries,
486 "output",
487 "Output options:",
488 "Options controlling the output",
489 this);
490}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200491
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400492
493
494hb_font_t *
495font_options_t::get_font (void) const
496{
497 if (font)
498 return font;
499
500 hb_blob_t *blob = NULL;
501
502 /* Create the blob */
503 {
Behdad Esfahbod44511682011-09-16 00:38:19 -0400504 char *font_data;
505 unsigned int len = 0;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400506 hb_destroy_func_t destroy;
507 void *user_data;
508 hb_memory_mode_t mm;
509
Behdad Esfahbod44511682011-09-16 00:38:19 -0400510 /* This is a hell of a lot of code for just reading a file! */
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400511 if (!font_file)
512 fail (TRUE, "No font file set");
513
Behdad Esfahbod44511682011-09-16 00:38:19 -0400514 if (0 == strcmp (font_file, "-")) {
515 /* read it */
516 GString *gs = g_string_new (NULL);
517 char buf[BUFSIZ];
Behdad Esfahbod0fe29602011-09-17 09:59:58 -0400518#ifdef HAVE__SETMODE
519 _setmode (fileno (stdin), _O_BINARY);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400520#endif
521 while (!feof (stdin)) {
522 size_t ret = fread (buf, 1, sizeof (buf), stdin);
523 if (ferror (stdin))
524 fail (FALSE, "Failed reading font from standard input: %s",
525 strerror (errno));
526 g_string_append_len (gs, buf, ret);
527 }
528 len = gs->len;
529 font_data = g_string_free (gs, FALSE);
530 user_data = font_data;
531 destroy = (hb_destroy_func_t) g_free;
532 mm = HB_MEMORY_MODE_WRITABLE;
533 } else {
534 GMappedFile *mf = g_mapped_file_new (font_file, FALSE, NULL);
535 if (mf) {
536 font_data = g_mapped_file_get_contents (mf);
537 len = g_mapped_file_get_length (mf);
538 if (len) {
539 destroy = (hb_destroy_func_t) g_mapped_file_unref;
540 user_data = (void *) mf;
541 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
542 } else
543 g_mapped_file_unref (mf);
544 }
545 if (!len) {
546 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
547 * Try reading.
548 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
549 GError *error = NULL;
550 gsize l;
551 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
552 len = l;
553 destroy = (hb_destroy_func_t) g_free;
554 user_data = (void *) font_data;
555 mm = HB_MEMORY_MODE_WRITABLE;
556 } else {
Behdad Esfahbod674ee582011-09-16 00:54:05 -0400557 fail (FALSE, "%s", error->message);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400558 //g_error_free (error);
559 }
560 }
561 }
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400562
563 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200564 }
565
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400566 /* Create the face */
567 hb_face_t *face = hb_face_create (blob, face_index);
568 hb_blob_destroy (blob);
569
570
571 font = hb_font_create (face);
572
573 unsigned int upem = hb_face_get_upem (face);
Behdad Esfahbod7bf6ecd2011-09-16 01:11:30 -0400574 hb_font_set_scale (font, upem, upem);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400575 hb_face_destroy (face);
576
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -0400577#ifdef HAVE_FREETYPE
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400578 hb_ft_font_set_funcs (font);
579#endif
580
581 return font;
582}
583
584
585const char *
586text_options_t::get_line (unsigned int *len)
587{
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400588 if (text) {
589 if (text_len == (unsigned int) -1)
590 text_len = strlen (text);
591
592 if (!text_len) {
593 *len = 0;
594 return NULL;
595 }
596
597 const char *ret = text;
598 const char *p = (const char *) memchr (text, '\n', text_len);
599 unsigned int ret_len;
600 if (!p) {
601 ret_len = text_len;
602 text += ret_len;
603 text_len = 0;
604 } else {
605 ret_len = p - ret;
606 text += ret_len + 1;
607 text_len -= ret_len + 1;
608 }
609
610 *len = ret_len;
611 return ret;
612 }
613
614 if (!fp) {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400615 if (!text_file)
616 fail (TRUE, "At least one of text or text-file must be set");
617
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400618 if (0 != strcmp (text_file, "-"))
619 fp = fopen (text_file, "r");
620 else
621 fp = stdin;
622
623 if (!fp)
624 fail (FALSE, "Failed opening text file `%s': %s",
625 text_file, strerror (errno));
626
627 gs = g_string_new (NULL);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400628 }
629
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400630 g_string_set_size (gs, 0);
631 char buf[BUFSIZ];
632 while (fgets (buf, sizeof (buf), fp)) {
633 unsigned int bytes = strlen (buf);
Behdad Esfahbod27c36af2012-01-19 12:30:43 -0500634 if (bytes && buf[bytes - 1] == '\n') {
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400635 bytes--;
636 g_string_append_len (gs, buf, bytes);
637 break;
638 }
639 g_string_append_len (gs, buf, bytes);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400640 }
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400641 if (ferror (fp))
642 fail (FALSE, "Failed reading text: %s",
643 strerror (errno));
644 *len = gs->len;
645 return !*len && feof (fp) ? NULL : gs->str;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200646}
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400647
648
649FILE *
650output_options_t::get_file_handle (void)
651{
652 if (fp)
653 return fp;
654
655 if (output_file)
656 fp = fopen (output_file, "wb");
657 else {
Behdad Esfahbod0fe29602011-09-17 09:59:58 -0400658#ifdef HAVE__SETMODE
659 _setmode (fileno (stdout), _O_BINARY);
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400660#endif
661 fp = stdout;
662 }
663 if (!fp)
664 fail (FALSE, "Cannot open output file `%s': %s",
665 g_filename_display_name (output_file), strerror (errno));
666
667 return fp;
668}
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400669
670
671void
672format_options_t::add_options (option_parser_t *parser)
673{
674 GOptionEntry entries[] =
675 {
676 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL},
677 {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL},
678 {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL},
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500679 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL},
680 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL},
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500681 {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL},
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400682 {NULL}
683 };
684 parser->add_group (entries,
685 "format",
686 "Format options:",
687 "Options controlling the formatting of buffer contents",
688 this);
689}
690
691void
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500692format_options_t::serialize_unicode (hb_buffer_t *buffer,
693 GString *gs)
694{
695 unsigned int num_glyphs = hb_buffer_get_length (buffer);
696 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
697
698 g_string_append_c (gs, '<');
699 for (unsigned int i = 0; i < num_glyphs; i++)
700 {
701 if (i)
702 g_string_append_c (gs, ',');
703 g_string_append_printf (gs, "U+%04X", info->codepoint);
704 info++;
705 }
706 g_string_append_c (gs, '>');
707}
708
709void
710format_options_t::serialize_glyphs (hb_buffer_t *buffer,
711 hb_font_t *font,
712 GString *gs)
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400713{
714 FT_Face ft_face = show_glyph_names ? hb_ft_font_get_face (font) : NULL;
715
716 unsigned int num_glyphs = hb_buffer_get_length (buffer);
717 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
718 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
719
720 g_string_append_c (gs, '<');
Behdad Esfahbod42255812011-09-19 17:57:02 -0400721 for (unsigned int i = 0; i < num_glyphs; i++)
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400722 {
723 if (i)
724 g_string_append_c (gs, '|');
Behdad Esfahbod088c1e22011-09-20 14:43:55 -0400725
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400726 char glyph_name[30];
Behdad Esfahbodd3f36902011-09-21 16:41:43 -0400727 if (show_glyph_names) {
728 if (!FT_Get_Glyph_Name (ft_face, info->codepoint, glyph_name, sizeof (glyph_name)))
729 g_string_append_printf (gs, "%s", glyph_name);
730 else
Behdad Esfahbod58577202011-09-27 12:36:26 -0400731 g_string_append_printf (gs, "gid%u", info->codepoint);
Behdad Esfahbodd3f36902011-09-21 16:41:43 -0400732 } else
Behdad Esfahbod58577202011-09-27 12:36:26 -0400733 g_string_append_printf (gs, "%u", info->codepoint);
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400734
735 if (show_clusters)
Behdad Esfahbod58577202011-09-27 12:36:26 -0400736 g_string_append_printf (gs, "=%u", info->cluster);
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400737
738 if (show_positions && (pos->x_offset || pos->y_offset)) {
739 g_string_append_c (gs, '@');
740 if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset);
741 if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset);
742 }
743 if (show_positions && (pos->x_advance || pos->y_advance)) {
744 g_string_append_c (gs, '+');
745 if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance);
746 if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance);
747 }
748
749 info++;
750 pos++;
751 }
752 g_string_append_c (gs, '>');
753}
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500754void
755format_options_t::serialize_line_no (unsigned int line_no,
756 GString *gs)
757{
758 if (show_line_num)
759 g_string_append_printf (gs, "%d: ", line_no);
760}
761void
762format_options_t::serialize_line (hb_buffer_t *buffer,
763 unsigned int line_no,
764 const char *text,
765 unsigned int text_len,
766 hb_font_t *font,
767 GString *gs)
768{
769 if (show_text) {
770 serialize_line_no (line_no, gs);
771 g_string_append_len (gs, text, text_len);
772 g_string_append_c (gs, '\n');
773 }
774
775 if (show_unicode) {
776 serialize_line_no (line_no, gs);
777 hb_buffer_reset (scratch);
778 hb_buffer_add_utf8 (scratch, text, text_len, 0, -1);
779 serialize_unicode (buffer, gs);
780 g_string_append_c (gs, '\n');
781 }
782
783 serialize_line_no (line_no, gs);
784 serialize_glyphs (buffer, font, gs);
785 g_string_append_c (gs, '\n');
786}