Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #include "options.hh" |
| 28 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 29 | #ifdef HAVE_FREETYPE |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 30 | #include <hb-ft.h> |
| 31 | #endif |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 32 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 33 | |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 34 | void |
| 35 | fail (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 Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 51 | bool debug = FALSE; |
| 52 | |
| 53 | static gchar * |
| 54 | shapers_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 | |
| 68 | static G_GNUC_NORETURN gboolean |
| 69 | show_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 | |
| 86 | void |
| 87 | option_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 | |
| 99 | static gboolean |
| 100 | pre_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 | |
| 110 | static gboolean |
| 111 | post_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 | |
| 121 | void |
| 122 | option_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 | |
| 135 | void |
| 136 | option_parser_t::parse (int *argc, char ***argv) |
| 137 | { |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 138 | setlocale (LC_ALL, ""); |
| 139 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 140 | GError *parse_error = NULL; |
| 141 | if (!g_option_context_parse (context, argc, argv, &parse_error)) |
| 142 | { |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 143 | if (parse_error != NULL) { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 144 | fail (TRUE, "%s", parse_error->message); |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 145 | //g_error_free (parse_error); |
| 146 | } else |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 147 | fail (TRUE, "Option parse error"); |
| 148 | } |
| 149 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 150 | |
| 151 | |
| 152 | static gboolean |
| 153 | parse_margin (const char *name G_GNUC_UNUSED, |
| 154 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 155 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 156 | GError **error G_GNUC_UNUSED) |
| 157 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 158 | view_options_t *view_opts = (view_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 159 | view_options_t::margin_t &m = view_opts->margin; |
Behdad Esfahbod | 9779645 | 2011-08-15 19:03:43 +0200 | [diff] [blame] | 160 | switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) { |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 161 | 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 | |
| 174 | static gboolean |
| 175 | parse_shapers (const char *name G_GNUC_UNUSED, |
| 176 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 177 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 178 | GError **error G_GNUC_UNUSED) |
| 179 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 180 | shape_options_t *shape_opts = (shape_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 181 | shape_opts->shapers = g_strsplit (arg, ",", 0); |
| 182 | return TRUE; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static void |
| 187 | parse_space (char **pp) |
| 188 | { |
| 189 | char c; |
| 190 | #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v') |
| 191 | while (c = **pp, ISSPACE (c)) |
| 192 | (*pp)++; |
| 193 | #undef ISSPACE |
| 194 | } |
| 195 | |
| 196 | static hb_bool_t |
| 197 | parse_char (char **pp, char c) |
| 198 | { |
| 199 | parse_space (pp); |
| 200 | |
| 201 | if (**pp != c) |
| 202 | return FALSE; |
| 203 | |
| 204 | (*pp)++; |
| 205 | return TRUE; |
| 206 | } |
| 207 | |
| 208 | static hb_bool_t |
| 209 | parse_uint (char **pp, unsigned int *pv) |
| 210 | { |
| 211 | char *p = *pp; |
| 212 | unsigned int v; |
| 213 | |
| 214 | v = strtol (p, pp, 0); |
| 215 | |
| 216 | if (p == *pp) |
| 217 | return FALSE; |
| 218 | |
| 219 | *pv = v; |
| 220 | return TRUE; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static hb_bool_t |
| 225 | parse_feature_value_prefix (char **pp, hb_feature_t *feature) |
| 226 | { |
| 227 | if (parse_char (pp, '-')) |
| 228 | feature->value = 0; |
| 229 | else { |
| 230 | parse_char (pp, '+'); |
| 231 | feature->value = 1; |
| 232 | } |
| 233 | |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | static hb_bool_t |
| 238 | parse_feature_tag (char **pp, hb_feature_t *feature) |
| 239 | { |
| 240 | char *p = *pp, c; |
| 241 | |
| 242 | parse_space (pp); |
| 243 | |
| 244 | #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9')) |
| 245 | while (c = **pp, ISALNUM(c)) |
| 246 | (*pp)++; |
| 247 | #undef ISALNUM |
| 248 | |
| 249 | if (p == *pp) |
| 250 | return FALSE; |
| 251 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 252 | feature->tag = hb_tag_from_string (p, *pp - p); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | static hb_bool_t |
| 257 | parse_feature_indices (char **pp, hb_feature_t *feature) |
| 258 | { |
| 259 | hb_bool_t has_start; |
| 260 | |
| 261 | feature->start = 0; |
| 262 | feature->end = (unsigned int) -1; |
| 263 | |
| 264 | if (!parse_char (pp, '[')) |
| 265 | return TRUE; |
| 266 | |
| 267 | has_start = parse_uint (pp, &feature->start); |
| 268 | |
| 269 | if (parse_char (pp, ':')) { |
| 270 | parse_uint (pp, &feature->end); |
| 271 | } else { |
| 272 | if (has_start) |
| 273 | feature->end = feature->start + 1; |
| 274 | } |
| 275 | |
| 276 | return parse_char (pp, ']'); |
| 277 | } |
| 278 | |
| 279 | static hb_bool_t |
| 280 | parse_feature_value_postfix (char **pp, hb_feature_t *feature) |
| 281 | { |
| 282 | return !parse_char (pp, '=') || parse_uint (pp, &feature->value); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | static hb_bool_t |
| 287 | parse_one_feature (char **pp, hb_feature_t *feature) |
| 288 | { |
| 289 | return parse_feature_value_prefix (pp, feature) && |
| 290 | parse_feature_tag (pp, feature) && |
| 291 | parse_feature_indices (pp, feature) && |
| 292 | parse_feature_value_postfix (pp, feature) && |
| 293 | (parse_char (pp, ',') || **pp == '\0'); |
| 294 | } |
| 295 | |
| 296 | static void |
| 297 | skip_one_feature (char **pp) |
| 298 | { |
| 299 | char *e; |
| 300 | e = strchr (*pp, ','); |
| 301 | if (e) |
| 302 | *pp = e + 1; |
| 303 | else |
| 304 | *pp = *pp + strlen (*pp); |
| 305 | } |
| 306 | |
| 307 | static gboolean |
| 308 | parse_features (const char *name G_GNUC_UNUSED, |
| 309 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 310 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 311 | GError **error G_GNUC_UNUSED) |
| 312 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 313 | shape_options_t *shape_opts = (shape_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 314 | char *s = (char *) arg; |
| 315 | char *p; |
| 316 | |
| 317 | shape_opts->num_features = 0; |
| 318 | shape_opts->features = NULL; |
| 319 | |
| 320 | if (!*s) |
| 321 | return TRUE; |
| 322 | |
| 323 | /* count the features first, so we can allocate memory */ |
| 324 | p = s; |
| 325 | do { |
| 326 | shape_opts->num_features++; |
| 327 | p = strchr (p, ','); |
| 328 | if (p) |
| 329 | p++; |
| 330 | } while (p); |
| 331 | |
| 332 | shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features)); |
| 333 | |
| 334 | /* now do the actual parsing */ |
| 335 | p = s; |
| 336 | shape_opts->num_features = 0; |
| 337 | while (*p) { |
| 338 | if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features])) |
| 339 | shape_opts->num_features++; |
| 340 | else |
| 341 | skip_one_feature (&p); |
| 342 | } |
| 343 | |
| 344 | return TRUE; |
| 345 | } |
| 346 | |
| 347 | |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 348 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 349 | view_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 350 | { |
| 351 | GOptionEntry entries[] = |
| 352 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 353 | {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL}, |
| 354 | {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"}, |
| 355 | {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"}, |
| 356 | {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 357 | {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"}, |
Behdad Esfahbod | 11e5199 | 2011-09-19 09:58:55 -0400 | [diff] [blame] | 358 | {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &this->font_size, "Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 359 | {NULL} |
| 360 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 361 | parser->add_group (entries, |
| 362 | "view", |
| 363 | "View options:", |
| 364 | "Options controlling the output rendering", |
| 365 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 369 | shape_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 370 | { |
| 371 | GOptionEntry entries[] = |
| 372 | { |
| 373 | {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Comma-separated list of shapers", "list"}, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 374 | {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, |
| 375 | {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"}, |
| 376 | {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 377 | {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, "Font features to apply to text", "TODO"}, |
| 378 | {NULL} |
| 379 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 380 | parser->add_group (entries, |
| 381 | "shape", |
| 382 | "Shape options:", |
| 383 | "Options controlling the shaping process", |
| 384 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 388 | font_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 389 | { |
| 390 | GOptionEntry entries[] = |
| 391 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 392 | {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"}, |
| 393 | {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 394 | {NULL} |
| 395 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 396 | parser->add_group (entries, |
| 397 | "font", |
| 398 | "Font options:", |
| 399 | "Options controlling the font", |
| 400 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 403 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 404 | text_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 405 | { |
| 406 | GOptionEntry entries[] = |
| 407 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 408 | {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"}, |
| 409 | {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"}, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 410 | {NULL} |
| 411 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 412 | parser->add_group (entries, |
| 413 | "text", |
| 414 | "Text options:", |
| 415 | "Options controlling the input text", |
| 416 | this); |
| 417 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 418 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 419 | void |
| 420 | output_options_t::add_options (option_parser_t *parser) |
| 421 | { |
| 422 | GOptionEntry entries[] = |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 423 | { |
Behdad Esfahbod | b5afd8f | 2011-09-19 16:56:21 -0400 | [diff] [blame^] | 424 | {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, |
| 425 | {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, "Set output format", "format"}, |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 426 | {NULL} |
| 427 | }; |
| 428 | parser->add_group (entries, |
| 429 | "output", |
| 430 | "Output options:", |
| 431 | "Options controlling the output", |
| 432 | this); |
| 433 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 434 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 435 | |
| 436 | |
| 437 | hb_font_t * |
| 438 | font_options_t::get_font (void) const |
| 439 | { |
| 440 | if (font) |
| 441 | return font; |
| 442 | |
| 443 | hb_blob_t *blob = NULL; |
| 444 | |
| 445 | /* Create the blob */ |
| 446 | { |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 447 | char *font_data; |
| 448 | unsigned int len = 0; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 449 | hb_destroy_func_t destroy; |
| 450 | void *user_data; |
| 451 | hb_memory_mode_t mm; |
| 452 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 453 | /* This is a hell of a lot of code for just reading a file! */ |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 454 | if (!font_file) |
| 455 | fail (TRUE, "No font file set"); |
| 456 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 457 | if (0 == strcmp (font_file, "-")) { |
| 458 | /* read it */ |
| 459 | GString *gs = g_string_new (NULL); |
| 460 | char buf[BUFSIZ]; |
Behdad Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 461 | #ifdef HAVE__SETMODE |
| 462 | _setmode (fileno (stdin), _O_BINARY); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 463 | #endif |
| 464 | while (!feof (stdin)) { |
| 465 | size_t ret = fread (buf, 1, sizeof (buf), stdin); |
| 466 | if (ferror (stdin)) |
| 467 | fail (FALSE, "Failed reading font from standard input: %s", |
| 468 | strerror (errno)); |
| 469 | g_string_append_len (gs, buf, ret); |
| 470 | } |
| 471 | len = gs->len; |
| 472 | font_data = g_string_free (gs, FALSE); |
| 473 | user_data = font_data; |
| 474 | destroy = (hb_destroy_func_t) g_free; |
| 475 | mm = HB_MEMORY_MODE_WRITABLE; |
| 476 | } else { |
| 477 | GMappedFile *mf = g_mapped_file_new (font_file, FALSE, NULL); |
| 478 | if (mf) { |
| 479 | font_data = g_mapped_file_get_contents (mf); |
| 480 | len = g_mapped_file_get_length (mf); |
| 481 | if (len) { |
| 482 | destroy = (hb_destroy_func_t) g_mapped_file_unref; |
| 483 | user_data = (void *) mf; |
| 484 | mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; |
| 485 | } else |
| 486 | g_mapped_file_unref (mf); |
| 487 | } |
| 488 | if (!len) { |
| 489 | /* GMappedFile is buggy, it doesn't fail if file isn't regular. |
| 490 | * Try reading. |
| 491 | * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */ |
| 492 | GError *error = NULL; |
| 493 | gsize l; |
| 494 | if (g_file_get_contents (font_file, &font_data, &l, &error)) { |
| 495 | len = l; |
| 496 | destroy = (hb_destroy_func_t) g_free; |
| 497 | user_data = (void *) font_data; |
| 498 | mm = HB_MEMORY_MODE_WRITABLE; |
| 499 | } else { |
Behdad Esfahbod | 674ee58 | 2011-09-16 00:54:05 -0400 | [diff] [blame] | 500 | fail (FALSE, "%s", error->message); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 501 | //g_error_free (error); |
| 502 | } |
| 503 | } |
| 504 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 505 | |
| 506 | blob = hb_blob_create (font_data, len, mm, user_data, destroy); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 509 | /* Create the face */ |
| 510 | hb_face_t *face = hb_face_create (blob, face_index); |
| 511 | hb_blob_destroy (blob); |
| 512 | |
| 513 | |
| 514 | font = hb_font_create (face); |
| 515 | |
| 516 | unsigned int upem = hb_face_get_upem (face); |
Behdad Esfahbod | 7bf6ecd | 2011-09-16 01:11:30 -0400 | [diff] [blame] | 517 | hb_font_set_scale (font, upem, upem); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 518 | hb_face_destroy (face); |
| 519 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 520 | #ifdef HAVE_FREETYPE |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 521 | hb_ft_font_set_funcs (font); |
| 522 | #endif |
| 523 | |
| 524 | return font; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | const char * |
| 529 | text_options_t::get_line (unsigned int *len) |
| 530 | { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 531 | if (text) { |
| 532 | if (text_len == (unsigned int) -1) |
| 533 | text_len = strlen (text); |
| 534 | |
| 535 | if (!text_len) { |
| 536 | *len = 0; |
| 537 | return NULL; |
| 538 | } |
| 539 | |
| 540 | const char *ret = text; |
| 541 | const char *p = (const char *) memchr (text, '\n', text_len); |
| 542 | unsigned int ret_len; |
| 543 | if (!p) { |
| 544 | ret_len = text_len; |
| 545 | text += ret_len; |
| 546 | text_len = 0; |
| 547 | } else { |
| 548 | ret_len = p - ret; |
| 549 | text += ret_len + 1; |
| 550 | text_len -= ret_len + 1; |
| 551 | } |
| 552 | |
| 553 | *len = ret_len; |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | if (!fp) { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 558 | if (!text_file) |
| 559 | fail (TRUE, "At least one of text or text-file must be set"); |
| 560 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 561 | if (0 != strcmp (text_file, "-")) |
| 562 | fp = fopen (text_file, "r"); |
| 563 | else |
| 564 | fp = stdin; |
| 565 | |
| 566 | if (!fp) |
| 567 | fail (FALSE, "Failed opening text file `%s': %s", |
| 568 | text_file, strerror (errno)); |
| 569 | |
| 570 | gs = g_string_new (NULL); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 571 | } |
| 572 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 573 | g_string_set_size (gs, 0); |
| 574 | char buf[BUFSIZ]; |
| 575 | while (fgets (buf, sizeof (buf), fp)) { |
| 576 | unsigned int bytes = strlen (buf); |
| 577 | if (buf[bytes - 1] == '\n') { |
| 578 | bytes--; |
| 579 | g_string_append_len (gs, buf, bytes); |
| 580 | break; |
| 581 | } |
| 582 | g_string_append_len (gs, buf, bytes); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 583 | } |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 584 | if (ferror (fp)) |
| 585 | fail (FALSE, "Failed reading text: %s", |
| 586 | strerror (errno)); |
| 587 | *len = gs->len; |
| 588 | return !*len && feof (fp) ? NULL : gs->str; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 589 | } |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 590 | |
| 591 | |
| 592 | FILE * |
| 593 | output_options_t::get_file_handle (void) |
| 594 | { |
| 595 | if (fp) |
| 596 | return fp; |
| 597 | |
| 598 | if (output_file) |
| 599 | fp = fopen (output_file, "wb"); |
| 600 | else { |
Behdad Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 601 | #ifdef HAVE__SETMODE |
| 602 | _setmode (fileno (stdout), _O_BINARY); |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 603 | #endif |
| 604 | fp = stdout; |
| 605 | } |
| 606 | if (!fp) |
| 607 | fail (FALSE, "Cannot open output file `%s': %s", |
| 608 | g_filename_display_name (output_file), strerror (errno)); |
| 609 | |
| 610 | return fp; |
| 611 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 612 | |
| 613 | |
| 614 | void |
| 615 | format_options_t::add_options (option_parser_t *parser) |
| 616 | { |
| 617 | GOptionEntry entries[] = |
| 618 | { |
| 619 | {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL}, |
| 620 | {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL}, |
| 621 | {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL}, |
| 622 | {NULL} |
| 623 | }; |
| 624 | parser->add_group (entries, |
| 625 | "format", |
| 626 | "Format options:", |
| 627 | "Options controlling the formatting of buffer contents", |
| 628 | this); |
| 629 | } |
| 630 | |
| 631 | void |
| 632 | format_options_t::serialize (hb_buffer_t *buffer, |
| 633 | hb_font_t *font, |
| 634 | GString *gs) |
| 635 | { |
| 636 | FT_Face ft_face = show_glyph_names ? hb_ft_font_get_face (font) : NULL; |
| 637 | |
| 638 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 639 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); |
| 640 | hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL); |
| 641 | |
| 642 | g_string_append_c (gs, '<'); |
| 643 | for (unsigned int i = 0; i < (int) num_glyphs; i++) |
| 644 | { |
| 645 | if (i) |
| 646 | g_string_append_c (gs, '|'); |
| 647 | char glyph_name[30]; |
| 648 | if (show_glyph_names && !FT_Get_Glyph_Name (ft_face, info->codepoint, glyph_name, sizeof (glyph_name))) |
| 649 | g_string_append_printf (gs, "%s", glyph_name); |
| 650 | else |
| 651 | g_string_append_printf (gs, "gid%d", info->codepoint); |
| 652 | |
| 653 | if (show_clusters) |
| 654 | g_string_append_printf (gs, "=%d", info->cluster); |
| 655 | |
| 656 | if (show_positions && (pos->x_offset || pos->y_offset)) { |
| 657 | g_string_append_c (gs, '@'); |
| 658 | if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset); |
| 659 | if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset); |
| 660 | } |
| 661 | if (show_positions && (pos->x_advance || pos->y_advance)) { |
| 662 | g_string_append_c (gs, '+'); |
| 663 | if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance); |
| 664 | if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance); |
| 665 | } |
| 666 | |
| 667 | info++; |
| 668 | pos++; |
| 669 | } |
| 670 | g_string_append_c (gs, '>'); |
| 671 | } |