blob: 66b5e151cafcf283db4536ee380c074313d354b6 [file] [log] [blame]
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +02001/*
Behdad Esfahbod8f8956a2012-05-25 14:30:24 -04002 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +02003 *
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 Esfahbod0594a242012-06-05 20:35:40 -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
Behdad Esfahbod0594a242012-06-05 20:35:40 -040065 return g_string_free (shapers, false);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040066}
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 Esfahbod0594a242012-06-05 20:35:40 -0400144 fail (true, "%s", parse_error->message);
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400145 //g_error_free (parse_error);
146 } else
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400147 fail (true, "Option parse error");
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400148 }
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;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400164 case 4: return true;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200165 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);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400169 return false;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200170 }
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 Esfahbodade74592012-08-06 19:42:47 -0700181 g_strfreev (shape_opts->shapers);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200182 shape_opts->shapers = g_strsplit (arg, ",", 0);
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400183 return true;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200184}
185
Behdad Esfahbodfd528c12011-10-12 15:03:58 -0400186static G_GNUC_NORETURN gboolean
187list_shapers (const char *name G_GNUC_UNUSED,
188 const char *arg G_GNUC_UNUSED,
189 gpointer data G_GNUC_UNUSED,
190 GError **error G_GNUC_UNUSED)
191{
192 for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++)
193 g_printf ("%s\n", *shaper);
194
195 exit(0);
196}
197
198
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200199static gboolean
200parse_features (const char *name G_GNUC_UNUSED,
201 const char *arg,
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400202 gpointer data,
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200203 GError **error G_GNUC_UNUSED)
204{
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400205 shape_options_t *shape_opts = (shape_options_t *) data;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200206 char *s = (char *) arg;
207 char *p;
208
209 shape_opts->num_features = 0;
Behdad Esfahbod8f8956a2012-05-25 14:30:24 -0400210 g_free (shape_opts->features);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200211 shape_opts->features = NULL;
212
213 if (!*s)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400214 return true;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200215
216 /* count the features first, so we can allocate memory */
217 p = s;
218 do {
219 shape_opts->num_features++;
220 p = strchr (p, ',');
221 if (p)
222 p++;
223 } while (p);
224
225 shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
226
227 /* now do the actual parsing */
228 p = s;
229 shape_opts->num_features = 0;
Behdad Esfahbode30ebd22012-09-06 22:09:06 -0400230 while (p && *p) {
231 char *end = strchr (p, ',');
232 if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200233 shape_opts->num_features++;
Behdad Esfahbode30ebd22012-09-06 22:09:06 -0400234 p = end ? end + 1 : NULL;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200235 }
236
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400237 return true;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200238}
239
240
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400241void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400242view_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400243{
244 GOptionEntry entries[] =
245 {
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400246 {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL},
Behdad Esfahbod9a34a502012-12-05 19:18:18 -0500247 {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "red/#rrggbb/#rrggbbaa"},
248 {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "red/#rrggbb/#rrggbbaa"},
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400249 {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
Behdad Esfahbod9a34a502012-12-05 19:18:18 -0500250 {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
251 {"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 -0400252 {NULL}
253 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400254 parser->add_group (entries,
255 "view",
256 "View options:",
Behdad Esfahbod30874b42012-05-12 15:54:27 +0200257 "Options controlling output rendering",
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400258 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400259}
260
261void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400262shape_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400263{
264 GOptionEntry entries[] =
265 {
Behdad Esfahbodfd528c12011-10-12 15:03:58 -0400266 {"list-shapers", 0, G_OPTION_FLAG_NO_ARG,
267 G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL},
Behdad Esfahbod8f8956a2012-05-25 14:30:24 -0400268 {"shaper", 0, G_OPTION_FLAG_HIDDEN,
269 G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL},
Behdad Esfahbodfd528c12011-10-12 15:03:58 -0400270 {"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 -0400271 {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"},
272 {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"},
273 {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"},
Behdad Esfahbod407f80d2012-11-13 15:33:27 -0800274 {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", NULL},
275 {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", NULL},
276 {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", NULL},
Behdad Esfahbod30874b42012-05-12 15:54:27 +0200277 {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", NULL},
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400278 {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", NULL},
Behdad Esfahbod50067e22013-04-11 16:31:01 -0400279 {"num-iterations", 0, 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400280 {NULL}
281 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400282 parser->add_group (entries,
283 "shape",
284 "Shape options:",
285 "Options controlling the shaping process",
286 this);
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500287
Behdad Esfahbod30874b42012-05-12 15:54:27 +0200288 const gchar *features_help = "Comma-separated list of font features\n"
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500289 "\n"
290 " Features can be enabled or disabled, either globally or limited to\n"
Behdad Esfahbod95cefdf2012-04-16 18:08:20 -0400291 " specific character ranges.\n"
292 "\n"
293 " The range indices refer to the positions between Unicode characters,\n"
294 " unless the --utf8-clusters is provided, in which case range indices\n"
295 " refer to UTF-8 byte indices. The position before the first character\n"
296 " is always 0.\n"
Behdad Esfahbodd5300242012-01-21 19:07:22 -0500297 "\n"
298 " The format is Python-esque. Here is how it all works:\n"
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500299 "\n"
300 " Syntax: Value: Start: End:\n"
301 "\n"
302 " Setting value:\n"
303 " \"kern\" 1 0 ∞ # Turn feature on\n"
304 " \"+kern\" 1 0 ∞ # Turn feature on\n"
305 " \"-kern\" 0 0 ∞ # Turn feature off\n"
306 " \"kern=0\" 0 0 ∞ # Turn feature off\n"
307 " \"kern=1\" 1 0 ∞ # Turn feature on\n"
308 " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n"
309 "\n"
310 " Setting index:\n"
311 " \"kern[]\" 1 0 ∞ # Turn feature on\n"
312 " \"kern[:]\" 1 0 ∞ # Turn feature on\n"
313 " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n"
314 " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n"
315 " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n"
316 " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n"
317 "\n"
318 " Mixing it all:\n"
319 "\n"
Behdad Esfahbod3bc22eb2012-11-12 10:07:28 -0800320 " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range";
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500321
322 GOptionEntry entries2[] =
323 {
324 {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"},
325 {NULL}
326 };
327 parser->add_group (entries2,
328 "features",
329 "Features options:",
Behdad Esfahbod30874b42012-05-12 15:54:27 +0200330 "Options controlling font features used",
Behdad Esfahbod8750aba2012-01-18 22:47:44 -0500331 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400332}
333
334void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400335font_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400336{
337 GOptionEntry entries[] =
338 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400339 {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"},
340 {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"},
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400341 {NULL}
342 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400343 parser->add_group (entries,
344 "font",
345 "Font options:",
346 "Options controlling the font",
347 this);
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400348}
349
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200350void
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400351text_options_t::add_options (option_parser_t *parser)
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200352{
353 GOptionEntry entries[] =
354 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400355 {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"},
Behdad Esfahbod78d41d82012-11-13 15:15:09 -0800356 {"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"},
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800357 {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
358 {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200359 {NULL}
360 };
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400361 parser->add_group (entries,
362 "text",
363 "Text options:",
364 "Options controlling the input text",
365 this);
366}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200367
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400368void
369output_options_t::add_options (option_parser_t *parser)
370{
Behdad Esfahbod9815a882012-12-21 16:46:53 -0500371 const char *text;
372
373 if (NULL == supported_formats)
374 text = "Set output format";
375 else
376 text = g_strdup_printf ("Set output format\n\n Supported formats are: %s", supported_formats);
377
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400378 GOptionEntry entries[] =
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200379 {
Behdad Esfahbodb5afd8f2011-09-19 16:56:21 -0400380 {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
Behdad Esfahbod9815a882012-12-21 16:46:53 -0500381 {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"},
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400382 {NULL}
383 };
384 parser->add_group (entries,
385 "output",
386 "Output options:",
387 "Options controlling the output",
388 this);
389}
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200390
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400391
392
393hb_font_t *
394font_options_t::get_font (void) const
395{
396 if (font)
397 return font;
398
399 hb_blob_t *blob = NULL;
400
401 /* Create the blob */
402 {
Behdad Esfahbod44511682011-09-16 00:38:19 -0400403 char *font_data;
404 unsigned int len = 0;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400405 hb_destroy_func_t destroy;
406 void *user_data;
407 hb_memory_mode_t mm;
408
Behdad Esfahbod44511682011-09-16 00:38:19 -0400409 /* This is a hell of a lot of code for just reading a file! */
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400410 if (!font_file)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400411 fail (true, "No font file set");
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400412
Behdad Esfahbod44511682011-09-16 00:38:19 -0400413 if (0 == strcmp (font_file, "-")) {
414 /* read it */
415 GString *gs = g_string_new (NULL);
416 char buf[BUFSIZ];
Behdad Esfahbode2aab4b2013-02-12 15:35:32 -0500417#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahbodbc764492013-01-31 18:18:05 -0500418 setmode (fileno (stdin), _O_BINARY);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400419#endif
420 while (!feof (stdin)) {
421 size_t ret = fread (buf, 1, sizeof (buf), stdin);
422 if (ferror (stdin))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400423 fail (false, "Failed reading font from standard input: %s",
Behdad Esfahbod44511682011-09-16 00:38:19 -0400424 strerror (errno));
425 g_string_append_len (gs, buf, ret);
426 }
427 len = gs->len;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400428 font_data = g_string_free (gs, false);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400429 user_data = font_data;
430 destroy = (hb_destroy_func_t) g_free;
431 mm = HB_MEMORY_MODE_WRITABLE;
432 } else {
Behdad Esfahbodf51e1672012-01-30 09:48:33 -0500433 GError *error = NULL;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400434 GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400435 if (mf) {
436 font_data = g_mapped_file_get_contents (mf);
437 len = g_mapped_file_get_length (mf);
438 if (len) {
Behdad Esfahbodc2bc8182013-10-27 23:36:35 +0100439 destroy = (hb_destroy_func_t) g_mapped_file_unref;
Behdad Esfahbod44511682011-09-16 00:38:19 -0400440 user_data = (void *) mf;
441 mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
442 } else
Behdad Esfahbodc2bc8182013-10-27 23:36:35 +0100443 g_mapped_file_unref (mf);
Behdad Esfahbodf51e1672012-01-30 09:48:33 -0500444 } else {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400445 fail (false, "%s", error->message);
Behdad Esfahbodf51e1672012-01-30 09:48:33 -0500446 //g_error_free (error);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400447 }
448 if (!len) {
449 /* GMappedFile is buggy, it doesn't fail if file isn't regular.
450 * Try reading.
451 * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
452 GError *error = NULL;
453 gsize l;
454 if (g_file_get_contents (font_file, &font_data, &l, &error)) {
455 len = l;
456 destroy = (hb_destroy_func_t) g_free;
457 user_data = (void *) font_data;
458 mm = HB_MEMORY_MODE_WRITABLE;
459 } else {
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400460 fail (false, "%s", error->message);
Behdad Esfahbod44511682011-09-16 00:38:19 -0400461 //g_error_free (error);
462 }
463 }
464 }
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400465
466 blob = hb_blob_create (font_data, len, mm, user_data, destroy);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200467 }
468
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400469 /* Create the face */
470 hb_face_t *face = hb_face_create (blob, face_index);
471 hb_blob_destroy (blob);
472
473
474 font = hb_font_create (face);
475
476 unsigned int upem = hb_face_get_upem (face);
Behdad Esfahbod7bf6ecd2011-09-16 01:11:30 -0400477 hb_font_set_scale (font, upem, upem);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400478 hb_face_destroy (face);
479
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -0400480#ifdef HAVE_FREETYPE
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400481 hb_ft_font_set_funcs (font);
482#endif
483
484 return font;
485}
486
487
488const char *
489text_options_t::get_line (unsigned int *len)
490{
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400491 if (text) {
492 if (text_len == (unsigned int) -1)
493 text_len = strlen (text);
494
495 if (!text_len) {
496 *len = 0;
497 return NULL;
498 }
499
500 const char *ret = text;
501 const char *p = (const char *) memchr (text, '\n', text_len);
502 unsigned int ret_len;
503 if (!p) {
504 ret_len = text_len;
505 text += ret_len;
506 text_len = 0;
507 } else {
508 ret_len = p - ret;
509 text += ret_len + 1;
510 text_len -= ret_len + 1;
511 }
512
513 *len = ret_len;
514 return ret;
515 }
516
517 if (!fp) {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400518 if (!text_file)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400519 fail (true, "At least one of text or text-file must be set");
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400520
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400521 if (0 != strcmp (text_file, "-"))
522 fp = fopen (text_file, "r");
523 else
524 fp = stdin;
525
526 if (!fp)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400527 fail (false, "Failed opening text file `%s': %s",
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400528 text_file, strerror (errno));
529
530 gs = g_string_new (NULL);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400531 }
532
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400533 g_string_set_size (gs, 0);
534 char buf[BUFSIZ];
535 while (fgets (buf, sizeof (buf), fp)) {
536 unsigned int bytes = strlen (buf);
Behdad Esfahbod27c36af2012-01-19 12:30:43 -0500537 if (bytes && buf[bytes - 1] == '\n') {
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400538 bytes--;
539 g_string_append_len (gs, buf, bytes);
540 break;
541 }
542 g_string_append_len (gs, buf, bytes);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400543 }
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400544 if (ferror (fp))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400545 fail (false, "Failed reading text: %s",
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400546 strerror (errno));
547 *len = gs->len;
548 return !*len && feof (fp) ? NULL : gs->str;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200549}
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400550
551
552FILE *
553output_options_t::get_file_handle (void)
554{
555 if (fp)
556 return fp;
557
558 if (output_file)
559 fp = fopen (output_file, "wb");
560 else {
Behdad Esfahbode2aab4b2013-02-12 15:35:32 -0500561#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahbodceeae302013-01-31 19:27:36 -0500562 setmode (fileno (stdout), _O_BINARY);
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400563#endif
564 fp = stdout;
565 }
566 if (!fp)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400567 fail (false, "Cannot open output file `%s': %s",
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400568 g_filename_display_name (output_file), strerror (errno));
569
570 return fp;
571}
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400572
Behdad Esfahbodc1885482012-06-04 08:56:00 -0400573static gboolean
574parse_verbose (const char *name G_GNUC_UNUSED,
575 const char *arg G_GNUC_UNUSED,
576 gpointer data G_GNUC_UNUSED,
577 GError **error G_GNUC_UNUSED)
578{
579 format_options_t *format_opts = (format_options_t *) data;
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400580 format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
581 return true;
Behdad Esfahbodc1885482012-06-04 08:56:00 -0400582}
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400583
584void
585format_options_t::add_options (option_parser_t *parser)
586{
587 GOptionEntry entries[] =
588 {
589 {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL},
590 {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL},
591 {"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 -0500592 {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL},
593 {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL},
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500594 {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL},
Behdad Esfahbodc1885482012-06-04 08:56:00 -0400595 {"verbose", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose, "Show everything", NULL},
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400596 {NULL}
597 };
598 parser->add_group (entries,
599 "format",
600 "Format options:",
601 "Options controlling the formatting of buffer contents",
602 this);
603}
604
605void
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500606format_options_t::serialize_unicode (hb_buffer_t *buffer,
607 GString *gs)
608{
609 unsigned int num_glyphs = hb_buffer_get_length (buffer);
610 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
611
612 g_string_append_c (gs, '<');
613 for (unsigned int i = 0; i < num_glyphs; i++)
614 {
615 if (i)
616 g_string_append_c (gs, ',');
617 g_string_append_printf (gs, "U+%04X", info->codepoint);
618 info++;
619 }
620 g_string_append_c (gs, '>');
621}
622
623void
624format_options_t::serialize_glyphs (hb_buffer_t *buffer,
625 hb_font_t *font,
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800626 hb_buffer_serialize_format_t output_format,
627 hb_buffer_serialize_flags_t flags,
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500628 GString *gs)
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400629{
Behdad Esfahbodc91c4fa2012-01-19 17:51:57 -0500630 g_string_append_c (gs, '[');
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800631 unsigned int num_glyphs = hb_buffer_get_length (buffer);
632 unsigned int start = 0;
Behdad Esfahbod088c1e22011-09-20 14:43:55 -0400633
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800634 while (start < num_glyphs) {
635 char buf[1024];
636 unsigned int consumed;
637 start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
638 buf, sizeof (buf), &consumed,
639 font, output_format, flags);
640 if (!consumed)
641 break;
642 g_string_append (gs, buf);
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400643 }
Behdad Esfahbodc91c4fa2012-01-19 17:51:57 -0500644 g_string_append_c (gs, ']');
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400645}
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500646void
647format_options_t::serialize_line_no (unsigned int line_no,
648 GString *gs)
649{
650 if (show_line_num)
651 g_string_append_printf (gs, "%d: ", line_no);
652}
653void
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400654format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
655 unsigned int line_no,
656 const char *text,
657 unsigned int text_len,
658 hb_font_t *font,
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400659 GString *gs)
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500660{
661 if (show_text) {
662 serialize_line_no (line_no, gs);
Behdad Esfahbodd8134bc2012-01-20 17:18:59 -0500663 g_string_append_c (gs, '(');
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500664 g_string_append_len (gs, text, text_len);
Behdad Esfahbodd8134bc2012-01-20 17:18:59 -0500665 g_string_append_c (gs, ')');
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500666 g_string_append_c (gs, '\n');
667 }
668
669 if (show_unicode) {
670 serialize_line_no (line_no, gs);
Behdad Esfahbodae621662012-06-02 12:21:19 -0400671 serialize_unicode (buffer, gs);
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500672 g_string_append_c (gs, '\n');
673 }
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400674}
675void
676format_options_t::serialize_message (unsigned int line_no,
677 const char *msg,
678 GString *gs)
679{
680 serialize_line_no (line_no, gs);
681 g_string_append_printf (gs, "%s", msg);
682 g_string_append_c (gs, '\n');
683}
684void
685format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
686 unsigned int line_no,
687 const char *text,
688 unsigned int text_len,
689 hb_font_t *font,
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800690 hb_buffer_serialize_format_t output_format,
691 hb_buffer_serialize_flags_t format_flags,
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400692 GString *gs)
693{
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500694 serialize_line_no (line_no, gs);
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800695 serialize_glyphs (buffer, font, output_format, format_flags, gs);
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500696 g_string_append_c (gs, '\n');
697}