1 /*
2 * Copyright © 2011,2012 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
29 #ifdef HAVE_FREETYPE
30 #include <hb-ft.h>
31 #endif
32 #ifdef HAVE_OT
33 #include <hb-ot-font.h>
34 #endif
35
36 struct supported_font_funcs_t {
37 char name[4];
38 void (*func) (hb_font_t *);
39 } supported_font_funcs[] =
40 {
41 #ifdef HAVE_FREETYPE
42 {"ft", hb_ft_font_set_funcs},
43 #endif
44 #ifdef HAVE_OT
45 {"ot", hb_ot_font_set_funcs},
46 #endif
47 };
48
49
50 void
fail(hb_bool_t suggest_help,const char * format,...)51 fail (hb_bool_t suggest_help, const char *format, ...)
52 {
53 const char *msg;
54
55 va_list vap;
56 va_start (vap, format);
57 msg = g_strdup_vprintf (format, vap);
58 va_end (vap);
59 const char *prgname = g_get_prgname ();
60 g_printerr ("%s: %s\n", prgname, msg);
61 if (suggest_help)
62 g_printerr ("Try `%s --help' for more information.\n", prgname);
63
64 exit (1);
65 }
66
67
68 hb_bool_t debug = false;
69
70 static gchar *
shapers_to_string(void)71 shapers_to_string (void)
72 {
73 GString *shapers = g_string_new (NULL);
74 const char **shaper_list = hb_shape_list_shapers ();
75
76 for (; *shaper_list; shaper_list++) {
77 g_string_append (shapers, *shaper_list);
78 g_string_append_c (shapers, ',');
79 }
80 g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
81
82 return g_string_free (shapers, false);
83 }
84
85 static G_GNUC_NORETURN gboolean
show_version(const char * name G_GNUC_UNUSED,const char * arg G_GNUC_UNUSED,gpointer data G_GNUC_UNUSED,GError ** error G_GNUC_UNUSED)86 show_version (const char *name G_GNUC_UNUSED,
87 const char *arg G_GNUC_UNUSED,
88 gpointer data G_GNUC_UNUSED,
89 GError **error G_GNUC_UNUSED)
90 {
91 g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
92
93 char *shapers = shapers_to_string ();
94 g_printf ("Available shapers: %s\n", shapers);
95 g_free (shapers);
96 if (strcmp (HB_VERSION_STRING, hb_version_string ()))
97 g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
98
99 exit(0);
100 }
101
102
103 void
add_main_options(void)104 option_parser_t::add_main_options (void)
105 {
106 GOptionEntry entries[] =
107 {
108 {"version", 0, G_OPTION_FLAG_NO_ARG,
109 G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL},
110 {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL},
111 {NULL}
112 };
113 g_option_context_add_main_entries (context, entries, NULL);
114 }
115
116 static gboolean
pre_parse(GOptionContext * context G_GNUC_UNUSED,GOptionGroup * group G_GNUC_UNUSED,gpointer data,GError ** error)117 pre_parse (GOptionContext *context G_GNUC_UNUSED,
118 GOptionGroup *group G_GNUC_UNUSED,
119 gpointer data,
120 GError **error)
121 {
122 option_group_t *option_group = (option_group_t *) data;
123 option_group->pre_parse (error);
124 return *error == NULL;
125 }
126
127 static gboolean
post_parse(GOptionContext * context G_GNUC_UNUSED,GOptionGroup * group G_GNUC_UNUSED,gpointer data,GError ** error)128 post_parse (GOptionContext *context G_GNUC_UNUSED,
129 GOptionGroup *group G_GNUC_UNUSED,
130 gpointer data,
131 GError **error)
132 {
133 option_group_t *option_group = static_cast<option_group_t *>(data);
134 option_group->post_parse (error);
135 return *error == NULL;
136 }
137
138 void
add_group(GOptionEntry * entries,const gchar * name,const gchar * description,const gchar * help_description,option_group_t * option_group)139 option_parser_t::add_group (GOptionEntry *entries,
140 const gchar *name,
141 const gchar *description,
142 const gchar *help_description,
143 option_group_t *option_group)
144 {
145 GOptionGroup *group = g_option_group_new (name, description, help_description,
146 static_cast<gpointer>(option_group), NULL);
147 g_option_group_add_entries (group, entries);
148 g_option_group_set_parse_hooks (group, pre_parse, post_parse);
149 g_option_context_add_group (context, group);
150 }
151
152 void
parse(int * argc,char *** argv)153 option_parser_t::parse (int *argc, char ***argv)
154 {
155 setlocale (LC_ALL, "");
156
157 GError *parse_error = NULL;
158 if (!g_option_context_parse (context, argc, argv, &parse_error))
159 {
160 if (parse_error != NULL) {
161 fail (true, "%s", parse_error->message);
162 //g_error_free (parse_error);
163 } else
164 fail (true, "Option parse error");
165 }
166 }
167
168
169 static gboolean
parse_margin(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)170 parse_margin (const char *name G_GNUC_UNUSED,
171 const char *arg,
172 gpointer data,
173 GError **error G_GNUC_UNUSED)
174 {
175 view_options_t *view_opts = (view_options_t *) data;
176 view_options_t::margin_t &m = view_opts->margin;
177 switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
178 case 1: m.r = m.t;
179 case 2: m.b = m.t;
180 case 3: m.l = m.r;
181 case 4: return true;
182 default:
183 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
184 "%s argument should be one to four space-separated numbers",
185 name);
186 return false;
187 }
188 }
189
190
191 static gboolean
parse_shapers(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)192 parse_shapers (const char *name G_GNUC_UNUSED,
193 const char *arg,
194 gpointer data,
195 GError **error G_GNUC_UNUSED)
196 {
197 shape_options_t *shape_opts = (shape_options_t *) data;
198 g_strfreev (shape_opts->shapers);
199 shape_opts->shapers = g_strsplit (arg, ",", 0);
200 return true;
201 }
202
203 static G_GNUC_NORETURN gboolean
list_shapers(const char * name G_GNUC_UNUSED,const char * arg G_GNUC_UNUSED,gpointer data G_GNUC_UNUSED,GError ** error G_GNUC_UNUSED)204 list_shapers (const char *name G_GNUC_UNUSED,
205 const char *arg G_GNUC_UNUSED,
206 gpointer data G_GNUC_UNUSED,
207 GError **error G_GNUC_UNUSED)
208 {
209 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
210 g_printf ("%s\n", *shaper);
211
212 exit(0);
213 }
214
215
216 static gboolean
parse_features(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)217 parse_features (const char *name G_GNUC_UNUSED,
218 const char *arg,
219 gpointer data,
220 GError **error G_GNUC_UNUSED)
221 {
222 shape_options_t *shape_opts = (shape_options_t *) data;
223 char *s = (char *) arg;
224 char *p;
225
226 shape_opts->num_features = 0;
227 g_free (shape_opts->features);
228 shape_opts->features = NULL;
229
230 if (!*s)
231 return true;
232
233 /* count the features first, so we can allocate memory */
234 p = s;
235 do {
236 shape_opts->num_features++;
237 p = strchr (p, ',');
238 if (p)
239 p++;
240 } while (p);
241
242 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
243
244 /* now do the actual parsing */
245 p = s;
246 shape_opts->num_features = 0;
247 while (p && *p) {
248 char *end = strchr (p, ',');
249 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
250 shape_opts->num_features++;
251 p = end ? end + 1 : NULL;
252 }
253
254 return true;
255 }
256
257
258 void
add_options(option_parser_t * parser)259 view_options_t::add_options (option_parser_t *parser)
260 {
261 GOptionEntry entries[] =
262 {
263 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL},
264 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"},
265 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"},
266 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
267 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
268 {NULL}
269 };
270 parser->add_group (entries,
271 "view",
272 "View options:",
273 "Options controlling output rendering",
274 this);
275 }
276
277 void
add_options(option_parser_t * parser)278 shape_options_t::add_options (option_parser_t *parser)
279 {
280 GOptionEntry entries[] =
281 {
282 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
283 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL},
284 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
285 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL},
286 {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"},
287 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
288 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
289 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
290 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", NULL},
291 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", NULL},
292 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", NULL},
293 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", NULL},
294 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", NULL},
295 {"num-iterations", 0, 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
296 {NULL}
297 };
298 parser->add_group (entries,
299 "shape",
300 "Shape options:",
301 "Options controlling the shaping process",
302 this);
303
304 const gchar *features_help = "Comma-separated list of font features\n"
305 "\n"
306 " Features can be enabled or disabled, either globally or limited to\n"
307 " specific character ranges. The format for specifying feature settings\n"
308 " follows. All valid CSS font-feature-settings values other than 'normal'\n"
309 " and 'inherited' are also accepted, though, not documented below.\n"
310 "\n"
311 " The range indices refer to the positions between Unicode characters,\n"
312 " unless the --utf8-clusters is provided, in which case range indices\n"
313 " refer to UTF-8 byte indices. The position before the first character\n"
314 " is always 0.\n"
315 "\n"
316 " The format is Python-esque. Here is how it all works:\n"
317 "\n"
318 " Syntax: Value: Start: End:\n"
319 "\n"
320 " Setting value:\n"
321 " \"kern\" 1 0 ∞ # Turn feature on\n"
322 " \"+kern\" 1 0 ∞ # Turn feature on\n"
323 " \"-kern\" 0 0 ∞ # Turn feature off\n"
324 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
325 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
326 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
327 "\n"
328 " Setting index:\n"
329 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
330 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
331 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
332 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
333 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
334 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
335 "\n"
336 " Mixing it all:\n"
337 "\n"
338 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
339
340 GOptionEntry entries2[] =
341 {
342 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
343 {NULL}
344 };
345 parser->add_group (entries2,
346 "features",
347 "Features options:",
348 "Options controlling font features used",
349 this);
350 }
351
352 static gboolean
parse_font_size(const char * name G_GNUC_UNUSED,const char * arg,gpointer data,GError ** error G_GNUC_UNUSED)353 parse_font_size (const char *name G_GNUC_UNUSED,
354 const char *arg,
355 gpointer data,
356 GError **error G_GNUC_UNUSED)
357 {
358 font_options_t *font_opts = (font_options_t *) data;
359 if (0 == strcmp (arg, "upem"))
360 {
361 font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
362 return true;
363 }
364 switch (sscanf (arg, "%lf %lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
365 case 1: font_opts->font_size_y = font_opts->font_size_x;
366 case 2: return true;
367 default:
368 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
369 "%s argument should be one to four space-separated numbers",
370 name);
371 return false;
372 }
373 }
374 void
add_options(option_parser_t * parser)375 font_options_t::add_options (option_parser_t *parser)
376 {
377 char *text = NULL;
378
379 {
380 ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0);
381 GString *s = g_string_new (NULL);
382 g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s",
383 supported_font_funcs[0].name,
384 supported_font_funcs[0].name);
385 for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++)
386 {
387 g_string_append_c (s, '/');
388 g_string_append (s, supported_font_funcs[i].name);
389 }
390 text = g_string_free (s, FALSE);
391 parser->free_later (text);
392 }
393
394 char *font_size_text;
395 if (default_font_size == FONT_SIZE_UPEM)
396 font_size_text = (char *) "Font size (default: upem)";
397 else
398 {
399 font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
400 parser->free_later (font_size_text);
401 }
402
403 GOptionEntry entries[] =
404 {
405 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
406 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
407 {"font-size", 0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
408 G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 numbers or 'upem'"},
409 {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"},
410 {NULL}
411 };
412 parser->add_group (entries,
413 "font",
414 "Font options:",
415 "Options controlling the font",
416 this);
417 }
418
419 void
add_options(option_parser_t * parser)420 text_options_t::add_options (option_parser_t *parser)
421 {
422 GOptionEntry entries[] =
423 {
424 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"},
425 {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name\n\n If no text is provided, standard input is used for input.\n", "filename"},
426 {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
427 {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
428 {NULL}
429 };
430 parser->add_group (entries,
431 "text",
432 "Text options:",
433 "Options controlling the input text",
434 this);
435 }
436
437 void
add_options(option_parser_t * parser)438 output_options_t::add_options (option_parser_t *parser)
439 {
440 const char *text;
441
442 if (NULL == supported_formats)
443 text = "Set output format";
444 else
445 {
446 char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
447 text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
448 g_free (items);
449 parser->free_later ((char *) text);
450 }
451
452 GOptionEntry entries[] =
453 {
454 {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
455 {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"},
456 {NULL}
457 };
458 parser->add_group (entries,
459 "output",
460 "Output options:",
461 "Options controlling the output",
462 this);
463 }
464
465
466
467 hb_font_t *
get_font(void) const468 font_options_t::get_font (void) const
469 {
470 if (font)
471 return font;
472
473 hb_blob_t *blob = NULL;
474
475 /* Create the blob */
476 {
477 char *font_data;
478 unsigned int len = 0;
479 hb_destroy_func_t destroy;
480 void *user_data;
481 hb_memory_mode_t mm;
482
483 /* This is a hell of a lot of code for just reading a file! */
484 if (!font_file)
485 fail (true, "No font file set");
486
487 if (0 == strcmp (font_file, "-")) {
488 /* read it */
489 GString *gs = g_string_new (NULL);
490 char buf[BUFSIZ];
491 #if defined(_WIN32) || defined(__CYGWIN__)
492 setmode (fileno (stdin), _O_BINARY);
493 #endif
494 while (!feof (stdin)) {
495 size_t ret = fread (buf, 1, sizeof (buf), stdin);
496 if (ferror (stdin))
497 fail (false, "Failed reading font from standard input: %s",
498 strerror (errno));
499 g_string_append_len (gs, buf, ret);
500 }
501 len = gs->len;
502 font_data = g_string_free (gs, false);
503 user_data = font_data;
504 destroy = (hb_destroy_func_t) g_free;
505 mm = HB_MEMORY_MODE_WRITABLE;
506 } else {
507 GError *error = NULL;
508 GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
509 if (mf) {
510 font_data = g_mapped_file_get_contents (mf);
511 len = g_mapped_file_get_length (mf);
512 if (len) {
513 destroy = (hb_destroy_func_t) g_mapped_file_unref;
514 user_data = (void *) mf;
515 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
516 } else
517 g_mapped_file_unref (mf);
518 } else {
519 fail (false, "%s", error->message);
520 //g_error_free (error);
521 }
522 if (!len) {
523 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
524 * Try reading.
525 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
526 GError *error = NULL;
527 gsize l;
528 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
529 len = l;
530 destroy = (hb_destroy_func_t) g_free;
531 user_data = (void *) font_data;
532 mm = HB_MEMORY_MODE_WRITABLE;
533 } else {
534 fail (false, "%s", error->message);
535 //g_error_free (error);
536 }
537 }
538 }
539
540 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
541 }
542
543 /* Create the face */
544 hb_face_t *face = hb_face_create (blob, face_index);
545 hb_blob_destroy (blob);
546
547
548 font = hb_font_create (face);
549
550 if (font_size_x == FONT_SIZE_UPEM)
551 font_size_x = hb_face_get_upem (face);
552 if (font_size_y == FONT_SIZE_UPEM)
553 font_size_y = hb_face_get_upem (face);
554
555 int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
556 int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
557 hb_font_set_scale (font, scale_x, scale_y);
558 hb_face_destroy (face);
559
560 void (*set_font_funcs) (hb_font_t *) = NULL;
561 if (!font_funcs)
562 {
563 set_font_funcs = supported_font_funcs[0].func;
564 }
565 else
566 {
567 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
568 if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name))
569 {
570 set_font_funcs = supported_font_funcs[i].func;
571 break;
572 }
573 if (!set_font_funcs)
574 {
575 GString *s = g_string_new (NULL);
576 for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
577 {
578 if (i)
579 g_string_append_c (s, '/');
580 g_string_append (s, supported_font_funcs[i].name);
581 }
582 char *p = g_string_free (s, FALSE);
583 fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s",
584 font_funcs,
585 p,
586 supported_font_funcs[0].name);
587 //free (p);
588 }
589 }
590 set_font_funcs (font);
591
592 return font;
593 }
594
595
596 const char *
get_line(unsigned int * len)597 text_options_t::get_line (unsigned int *len)
598 {
599 if (text) {
600 if (text_len == (unsigned int) -1)
601 text_len = strlen (text);
602
603 if (!text_len) {
604 *len = 0;
605 return NULL;
606 }
607
608 const char *ret = text;
609 const char *p = (const char *) memchr (text, '\n', text_len);
610 unsigned int ret_len;
611 if (!p) {
612 ret_len = text_len;
613 text += ret_len;
614 text_len = 0;
615 } else {
616 ret_len = p - ret;
617 text += ret_len + 1;
618 text_len -= ret_len + 1;
619 }
620
621 *len = ret_len;
622 return ret;
623 }
624
625 if (!fp) {
626 if (!text_file)
627 fail (true, "At least one of text or text-file must be set");
628
629 if (0 != strcmp (text_file, "-"))
630 fp = fopen (text_file, "r");
631 else
632 fp = stdin;
633
634 if (!fp)
635 fail (false, "Failed opening text file `%s': %s",
636 text_file, strerror (errno));
637
638 gs = g_string_new (NULL);
639 }
640
641 g_string_set_size (gs, 0);
642 char buf[BUFSIZ];
643 while (fgets (buf, sizeof (buf), fp)) {
644 unsigned int bytes = strlen (buf);
645 if (bytes && buf[bytes - 1] == '\n') {
646 bytes--;
647 g_string_append_len (gs, buf, bytes);
648 break;
649 }
650 g_string_append_len (gs, buf, bytes);
651 }
652 if (ferror (fp))
653 fail (false, "Failed reading text: %s",
654 strerror (errno));
655 *len = gs->len;
656 return !*len && feof (fp) ? NULL : gs->str;
657 }
658
659
660 FILE *
get_file_handle(void)661 output_options_t::get_file_handle (void)
662 {
663 if (fp)
664 return fp;
665
666 if (output_file)
667 fp = fopen (output_file, "wb");
668 else {
669 #if defined(_WIN32) || defined(__CYGWIN__)
670 setmode (fileno (stdout), _O_BINARY);
671 #endif
672 fp = stdout;
673 }
674 if (!fp)
675 fail (false, "Cannot open output file `%s': %s",
676 g_filename_display_name (output_file), strerror (errno));
677
678 return fp;
679 }
680
681 static gboolean
parse_verbose(const char * name G_GNUC_UNUSED,const char * arg G_GNUC_UNUSED,gpointer data G_GNUC_UNUSED,GError ** error G_GNUC_UNUSED)682 parse_verbose (const char *name G_GNUC_UNUSED,
683 const char *arg G_GNUC_UNUSED,
684 gpointer data G_GNUC_UNUSED,
685 GError **error G_GNUC_UNUSED)
686 {
687 format_options_t *format_opts = (format_options_t *) data;
688 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
689 return true;
690 }
691
692 void
add_options(option_parser_t * parser)693 format_options_t::add_options (option_parser_t *parser)
694 {
695 GOptionEntry entries[] =
696 {
697 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL},
698 {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL},
699 {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL},
700 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL},
701 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL},
702 {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL},
703 {"verbose", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose, "Show everything", NULL},
704 {NULL}
705 };
706 parser->add_group (entries,
707 "format",
708 "Format options:",
709 "Options controlling the formatting of buffer contents",
710 this);
711 }
712
713 void
serialize_unicode(hb_buffer_t * buffer,GString * gs)714 format_options_t::serialize_unicode (hb_buffer_t *buffer,
715 GString *gs)
716 {
717 unsigned int num_glyphs = hb_buffer_get_length (buffer);
718 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
719
720 g_string_append_c (gs, '<');
721 for (unsigned int i = 0; i < num_glyphs; i++)
722 {
723 if (i)
724 g_string_append_c (gs, ',');
725 g_string_append_printf (gs, "U+%04X", info->codepoint);
726 info++;
727 }
728 g_string_append_c (gs, '>');
729 }
730
731 void
serialize_glyphs(hb_buffer_t * buffer,hb_font_t * font,hb_buffer_serialize_format_t output_format,hb_buffer_serialize_flags_t flags,GString * gs)732 format_options_t::serialize_glyphs (hb_buffer_t *buffer,
733 hb_font_t *font,
734 hb_buffer_serialize_format_t output_format,
735 hb_buffer_serialize_flags_t flags,
736 GString *gs)
737 {
738 g_string_append_c (gs, '[');
739 unsigned int num_glyphs = hb_buffer_get_length (buffer);
740 unsigned int start = 0;
741
742 while (start < num_glyphs) {
743 char buf[1024];
744 unsigned int consumed;
745 start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
746 buf, sizeof (buf), &consumed,
747 font, output_format, flags);
748 if (!consumed)
749 break;
750 g_string_append (gs, buf);
751 }
752 g_string_append_c (gs, ']');
753 }
754 void
serialize_line_no(unsigned int line_no,GString * gs)755 format_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 }
761 void
serialize_buffer_of_text(hb_buffer_t * buffer,unsigned int line_no,const char * text,unsigned int text_len,hb_font_t * font,GString * gs)762 format_options_t::serialize_buffer_of_text (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_c (gs, '(');
772 g_string_append_len (gs, text, text_len);
773 g_string_append_c (gs, ')');
774 g_string_append_c (gs, '\n');
775 }
776
777 if (show_unicode) {
778 serialize_line_no (line_no, gs);
779 serialize_unicode (buffer, gs);
780 g_string_append_c (gs, '\n');
781 }
782 }
783 void
serialize_message(unsigned int line_no,const char * msg,GString * gs)784 format_options_t::serialize_message (unsigned int line_no,
785 const char *msg,
786 GString *gs)
787 {
788 serialize_line_no (line_no, gs);
789 g_string_append_printf (gs, "%s", msg);
790 g_string_append_c (gs, '\n');
791 }
792 void
serialize_buffer_of_glyphs(hb_buffer_t * buffer,unsigned int line_no,const char * text,unsigned int text_len,hb_font_t * font,hb_buffer_serialize_format_t output_format,hb_buffer_serialize_flags_t format_flags,GString * gs)793 format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
794 unsigned int line_no,
795 const char *text,
796 unsigned int text_len,
797 hb_font_t *font,
798 hb_buffer_serialize_format_t output_format,
799 hb_buffer_serialize_flags_t format_flags,
800 GString *gs)
801 {
802 serialize_line_no (line_no, gs);
803 serialize_glyphs (buffer, font, output_format, format_flags, gs);
804 g_string_append_c (gs, '\n');
805 }
806