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 | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 51 | hb_bool_t debug = FALSE; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 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 | |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 185 | static G_GNUC_NORETURN gboolean |
| 186 | list_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 Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 198 | |
| 199 | static void |
| 200 | parse_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 | |
| 209 | static hb_bool_t |
| 210 | parse_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 | |
| 221 | static hb_bool_t |
| 222 | parse_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 | |
| 237 | static hb_bool_t |
| 238 | parse_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 | |
| 250 | static hb_bool_t |
| 251 | parse_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 Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 265 | feature->tag = hb_tag_from_string (p, *pp - p); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 266 | return TRUE; |
| 267 | } |
| 268 | |
| 269 | static hb_bool_t |
| 270 | parse_feature_indices (char **pp, hb_feature_t *feature) |
| 271 | { |
Behdad Esfahbod | a097043 | 2012-01-14 17:55:51 -0500 | [diff] [blame] | 272 | parse_space (pp); |
| 273 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 274 | 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 | |
| 294 | static hb_bool_t |
| 295 | parse_feature_value_postfix (char **pp, hb_feature_t *feature) |
| 296 | { |
| 297 | return !parse_char (pp, '=') || parse_uint (pp, &feature->value); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static hb_bool_t |
| 302 | parse_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 | |
| 311 | static void |
| 312 | skip_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 | |
| 322 | static gboolean |
| 323 | parse_features (const char *name G_GNUC_UNUSED, |
| 324 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 325 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 326 | GError **error G_GNUC_UNUSED) |
| 327 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 328 | shape_options_t *shape_opts = (shape_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 329 | 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 Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 363 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 364 | view_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 365 | { |
| 366 | GOptionEntry entries[] = |
| 367 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 368 | {"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 Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 372 | {"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] | 373 | {"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] | 374 | {NULL} |
| 375 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 376 | parser->add_group (entries, |
| 377 | "view", |
| 378 | "View options:", |
| 379 | "Options controlling the output rendering", |
| 380 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 384 | shape_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 385 | { |
| 386 | GOptionEntry entries[] = |
| 387 | { |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 388 | {"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 Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 391 | {"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 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 | "shape", |
| 398 | "Shape options:", |
| 399 | "Options controlling the shaping process", |
| 400 | this); |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 401 | |
| 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 Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 445 | font_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 446 | { |
| 447 | GOptionEntry entries[] = |
| 448 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 449 | {"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 Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 451 | {NULL} |
| 452 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 453 | parser->add_group (entries, |
| 454 | "font", |
| 455 | "Font options:", |
| 456 | "Options controlling the font", |
| 457 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 460 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 461 | text_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 462 | { |
| 463 | GOptionEntry entries[] = |
| 464 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 465 | {"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 Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 467 | {NULL} |
| 468 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 469 | parser->add_group (entries, |
| 470 | "text", |
| 471 | "Text options:", |
| 472 | "Options controlling the input text", |
| 473 | this); |
| 474 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 475 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 476 | void |
| 477 | output_options_t::add_options (option_parser_t *parser) |
| 478 | { |
| 479 | GOptionEntry entries[] = |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 480 | { |
Behdad Esfahbod | b5afd8f | 2011-09-19 16:56:21 -0400 | [diff] [blame] | 481 | {"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 Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 483 | {NULL} |
| 484 | }; |
| 485 | parser->add_group (entries, |
| 486 | "output", |
| 487 | "Output options:", |
| 488 | "Options controlling the output", |
| 489 | this); |
| 490 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 491 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 492 | |
| 493 | |
| 494 | hb_font_t * |
| 495 | font_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 Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 504 | char *font_data; |
| 505 | unsigned int len = 0; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 506 | hb_destroy_func_t destroy; |
| 507 | void *user_data; |
| 508 | hb_memory_mode_t mm; |
| 509 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 510 | /* 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] | 511 | if (!font_file) |
| 512 | fail (TRUE, "No font file set"); |
| 513 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 514 | if (0 == strcmp (font_file, "-")) { |
| 515 | /* read it */ |
| 516 | GString *gs = g_string_new (NULL); |
| 517 | char buf[BUFSIZ]; |
Behdad Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 518 | #ifdef HAVE__SETMODE |
| 519 | _setmode (fileno (stdin), _O_BINARY); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 520 | #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 Esfahbod | 674ee58 | 2011-09-16 00:54:05 -0400 | [diff] [blame] | 557 | fail (FALSE, "%s", error->message); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 558 | //g_error_free (error); |
| 559 | } |
| 560 | } |
| 561 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 562 | |
| 563 | blob = hb_blob_create (font_data, len, mm, user_data, destroy); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 566 | /* 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 Esfahbod | 7bf6ecd | 2011-09-16 01:11:30 -0400 | [diff] [blame] | 574 | hb_font_set_scale (font, upem, upem); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 575 | hb_face_destroy (face); |
| 576 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 577 | #ifdef HAVE_FREETYPE |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 578 | hb_ft_font_set_funcs (font); |
| 579 | #endif |
| 580 | |
| 581 | return font; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | const char * |
| 586 | text_options_t::get_line (unsigned int *len) |
| 587 | { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 588 | 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 Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 615 | if (!text_file) |
| 616 | fail (TRUE, "At least one of text or text-file must be set"); |
| 617 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 618 | 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 Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 628 | } |
| 629 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 630 | g_string_set_size (gs, 0); |
| 631 | char buf[BUFSIZ]; |
| 632 | while (fgets (buf, sizeof (buf), fp)) { |
| 633 | unsigned int bytes = strlen (buf); |
Behdad Esfahbod | 27c36af | 2012-01-19 12:30:43 -0500 | [diff] [blame] | 634 | if (bytes && buf[bytes - 1] == '\n') { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 635 | bytes--; |
| 636 | g_string_append_len (gs, buf, bytes); |
| 637 | break; |
| 638 | } |
| 639 | g_string_append_len (gs, buf, bytes); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 640 | } |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 641 | 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 Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 646 | } |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 647 | |
| 648 | |
| 649 | FILE * |
| 650 | output_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 Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 658 | #ifdef HAVE__SETMODE |
| 659 | _setmode (fileno (stdout), _O_BINARY); |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 660 | #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 Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 669 | |
| 670 | |
| 671 | void |
| 672 | format_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 Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 679 | {"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 Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame^] | 681 | {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL}, |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 682 | {NULL} |
| 683 | }; |
| 684 | parser->add_group (entries, |
| 685 | "format", |
| 686 | "Format options:", |
| 687 | "Options controlling the formatting of buffer contents", |
| 688 | this); |
| 689 | } |
| 690 | |
| 691 | void |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 692 | format_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 | |
| 709 | void |
| 710 | format_options_t::serialize_glyphs (hb_buffer_t *buffer, |
| 711 | hb_font_t *font, |
| 712 | GString *gs) |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 713 | { |
| 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 Esfahbod | 4225581 | 2011-09-19 17:57:02 -0400 | [diff] [blame] | 721 | for (unsigned int i = 0; i < num_glyphs; i++) |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 722 | { |
| 723 | if (i) |
| 724 | g_string_append_c (gs, '|'); |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 725 | |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 726 | char glyph_name[30]; |
Behdad Esfahbod | d3f3690 | 2011-09-21 16:41:43 -0400 | [diff] [blame] | 727 | 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 Esfahbod | 5857720 | 2011-09-27 12:36:26 -0400 | [diff] [blame] | 731 | g_string_append_printf (gs, "gid%u", info->codepoint); |
Behdad Esfahbod | d3f3690 | 2011-09-21 16:41:43 -0400 | [diff] [blame] | 732 | } else |
Behdad Esfahbod | 5857720 | 2011-09-27 12:36:26 -0400 | [diff] [blame] | 733 | g_string_append_printf (gs, "%u", info->codepoint); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 734 | |
| 735 | if (show_clusters) |
Behdad Esfahbod | 5857720 | 2011-09-27 12:36:26 -0400 | [diff] [blame] | 736 | g_string_append_printf (gs, "=%u", info->cluster); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 737 | |
| 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 Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame^] | 754 | void |
| 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 |
| 762 | format_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 | } |