Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 8f8956a | 2012-05-25 14:30:24 -0400 | [diff] [blame] | 2 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 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 | 0594a24 | 2012-06-05 20:35:40 -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 | |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 65 | return g_string_free (shapers, false); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 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 | 0594a24 | 2012-06-05 20:35:40 -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 | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 147 | fail (true, "Option parse error"); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 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; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 164 | case 4: return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 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); |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 169 | return false; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 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 | 8f8956a | 2012-05-25 14:30:24 -0400 | [diff] [blame] | 181 | g_free (shape_opts->shapers); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 182 | shape_opts->shapers = g_strsplit (arg, ",", 0); |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 183 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 186 | static G_GNUC_NORETURN gboolean |
| 187 | list_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 Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 199 | |
| 200 | static void |
| 201 | parse_space (char **pp) |
| 202 | { |
| 203 | char c; |
| 204 | #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v') |
| 205 | while (c = **pp, ISSPACE (c)) |
| 206 | (*pp)++; |
| 207 | #undef ISSPACE |
| 208 | } |
| 209 | |
| 210 | static hb_bool_t |
| 211 | parse_char (char **pp, char c) |
| 212 | { |
| 213 | parse_space (pp); |
| 214 | |
| 215 | if (**pp != c) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 216 | return false; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 217 | |
| 218 | (*pp)++; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 219 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static hb_bool_t |
| 223 | parse_uint (char **pp, unsigned int *pv) |
| 224 | { |
| 225 | char *p = *pp; |
| 226 | unsigned int v; |
| 227 | |
| 228 | v = strtol (p, pp, 0); |
| 229 | |
| 230 | if (p == *pp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 231 | return false; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 232 | |
| 233 | *pv = v; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 234 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
| 238 | static hb_bool_t |
| 239 | parse_feature_value_prefix (char **pp, hb_feature_t *feature) |
| 240 | { |
| 241 | if (parse_char (pp, '-')) |
| 242 | feature->value = 0; |
| 243 | else { |
| 244 | parse_char (pp, '+'); |
| 245 | feature->value = 1; |
| 246 | } |
| 247 | |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 248 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static hb_bool_t |
| 252 | parse_feature_tag (char **pp, hb_feature_t *feature) |
| 253 | { |
| 254 | char *p = *pp, c; |
| 255 | |
| 256 | parse_space (pp); |
| 257 | |
| 258 | #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9')) |
| 259 | while (c = **pp, ISALNUM(c)) |
| 260 | (*pp)++; |
| 261 | #undef ISALNUM |
| 262 | |
| 263 | if (p == *pp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 264 | return false; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 265 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 266 | feature->tag = hb_tag_from_string (p, *pp - p); |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 267 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static hb_bool_t |
| 271 | parse_feature_indices (char **pp, hb_feature_t *feature) |
| 272 | { |
Behdad Esfahbod | a097043 | 2012-01-14 17:55:51 -0500 | [diff] [blame] | 273 | parse_space (pp); |
| 274 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 275 | hb_bool_t has_start; |
| 276 | |
| 277 | feature->start = 0; |
| 278 | feature->end = (unsigned int) -1; |
| 279 | |
| 280 | if (!parse_char (pp, '[')) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 281 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 282 | |
| 283 | has_start = parse_uint (pp, &feature->start); |
| 284 | |
| 285 | if (parse_char (pp, ':')) { |
| 286 | parse_uint (pp, &feature->end); |
| 287 | } else { |
| 288 | if (has_start) |
| 289 | feature->end = feature->start + 1; |
| 290 | } |
| 291 | |
| 292 | return parse_char (pp, ']'); |
| 293 | } |
| 294 | |
| 295 | static hb_bool_t |
| 296 | parse_feature_value_postfix (char **pp, hb_feature_t *feature) |
| 297 | { |
| 298 | return !parse_char (pp, '=') || parse_uint (pp, &feature->value); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | static hb_bool_t |
| 303 | parse_one_feature (char **pp, hb_feature_t *feature) |
| 304 | { |
| 305 | return parse_feature_value_prefix (pp, feature) && |
| 306 | parse_feature_tag (pp, feature) && |
| 307 | parse_feature_indices (pp, feature) && |
| 308 | parse_feature_value_postfix (pp, feature) && |
| 309 | (parse_char (pp, ',') || **pp == '\0'); |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | skip_one_feature (char **pp) |
| 314 | { |
| 315 | char *e; |
| 316 | e = strchr (*pp, ','); |
| 317 | if (e) |
| 318 | *pp = e + 1; |
| 319 | else |
| 320 | *pp = *pp + strlen (*pp); |
| 321 | } |
| 322 | |
| 323 | static gboolean |
| 324 | parse_features (const char *name G_GNUC_UNUSED, |
| 325 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 326 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 327 | GError **error G_GNUC_UNUSED) |
| 328 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 329 | shape_options_t *shape_opts = (shape_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 330 | char *s = (char *) arg; |
| 331 | char *p; |
| 332 | |
| 333 | shape_opts->num_features = 0; |
Behdad Esfahbod | 8f8956a | 2012-05-25 14:30:24 -0400 | [diff] [blame] | 334 | g_free (shape_opts->features); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 335 | shape_opts->features = NULL; |
| 336 | |
| 337 | if (!*s) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 338 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 339 | |
| 340 | /* count the features first, so we can allocate memory */ |
| 341 | p = s; |
| 342 | do { |
| 343 | shape_opts->num_features++; |
| 344 | p = strchr (p, ','); |
| 345 | if (p) |
| 346 | p++; |
| 347 | } while (p); |
| 348 | |
| 349 | shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features)); |
| 350 | |
| 351 | /* now do the actual parsing */ |
| 352 | p = s; |
| 353 | shape_opts->num_features = 0; |
| 354 | while (*p) { |
| 355 | if (parse_one_feature (&p, &shape_opts->features[shape_opts->num_features])) |
| 356 | shape_opts->num_features++; |
| 357 | else |
| 358 | skip_one_feature (&p); |
| 359 | } |
| 360 | |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 361 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 365 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 366 | view_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 367 | { |
| 368 | GOptionEntry entries[] = |
| 369 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 370 | {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL}, |
| 371 | {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"}, |
| 372 | {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"}, |
| 373 | {"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] | 374 | {"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] | 375 | {"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] | 376 | {NULL} |
| 377 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 378 | parser->add_group (entries, |
| 379 | "view", |
| 380 | "View options:", |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 381 | "Options controlling output rendering", |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 382 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 386 | shape_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 387 | { |
| 388 | GOptionEntry entries[] = |
| 389 | { |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 390 | {"list-shapers", 0, G_OPTION_FLAG_NO_ARG, |
| 391 | G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL}, |
Behdad Esfahbod | 8f8956a | 2012-05-25 14:30:24 -0400 | [diff] [blame] | 392 | {"shaper", 0, G_OPTION_FLAG_HIDDEN, |
| 393 | G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL}, |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 394 | {"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] | 395 | {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, |
| 396 | {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"}, |
| 397 | {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 398 | {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", NULL}, |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 399 | {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", NULL}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 400 | {NULL} |
| 401 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 402 | parser->add_group (entries, |
| 403 | "shape", |
| 404 | "Shape options:", |
| 405 | "Options controlling the shaping process", |
| 406 | this); |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 407 | |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 408 | const gchar *features_help = "Comma-separated list of font features\n" |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 409 | "\n" |
| 410 | " Features can be enabled or disabled, either globally or limited to\n" |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 411 | " specific character ranges.\n" |
| 412 | "\n" |
| 413 | " The range indices refer to the positions between Unicode characters,\n" |
| 414 | " unless the --utf8-clusters is provided, in which case range indices\n" |
| 415 | " refer to UTF-8 byte indices. The position before the first character\n" |
| 416 | " is always 0.\n" |
Behdad Esfahbod | d530024 | 2012-01-21 19:07:22 -0500 | [diff] [blame] | 417 | "\n" |
| 418 | " The format is Python-esque. Here is how it all works:\n" |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 419 | "\n" |
| 420 | " Syntax: Value: Start: End:\n" |
| 421 | "\n" |
| 422 | " Setting value:\n" |
| 423 | " \"kern\" 1 0 ∞ # Turn feature on\n" |
| 424 | " \"+kern\" 1 0 ∞ # Turn feature on\n" |
| 425 | " \"-kern\" 0 0 ∞ # Turn feature off\n" |
| 426 | " \"kern=0\" 0 0 ∞ # Turn feature off\n" |
| 427 | " \"kern=1\" 1 0 ∞ # Turn feature on\n" |
| 428 | " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n" |
| 429 | "\n" |
| 430 | " Setting index:\n" |
| 431 | " \"kern[]\" 1 0 ∞ # Turn feature on\n" |
| 432 | " \"kern[:]\" 1 0 ∞ # Turn feature on\n" |
| 433 | " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n" |
| 434 | " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n" |
| 435 | " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n" |
| 436 | " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n" |
| 437 | "\n" |
| 438 | " Mixing it all:\n" |
| 439 | "\n" |
| 440 | " \"kern[3:5]=0\" 1 3 5 # Turn feature off for range"; |
| 441 | |
| 442 | GOptionEntry entries2[] = |
| 443 | { |
| 444 | {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"}, |
| 445 | {NULL} |
| 446 | }; |
| 447 | parser->add_group (entries2, |
| 448 | "features", |
| 449 | "Features options:", |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 450 | "Options controlling font features used", |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 451 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 455 | font_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 456 | { |
| 457 | GOptionEntry entries[] = |
| 458 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 459 | {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"}, |
| 460 | {"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] | 461 | {NULL} |
| 462 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 463 | parser->add_group (entries, |
| 464 | "font", |
| 465 | "Font options:", |
| 466 | "Options controlling the font", |
| 467 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 470 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 471 | text_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 472 | { |
| 473 | GOptionEntry entries[] = |
| 474 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 475 | {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"}, |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 476 | {"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.", "filename"}, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 477 | {NULL} |
| 478 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 479 | parser->add_group (entries, |
| 480 | "text", |
| 481 | "Text options:", |
| 482 | "Options controlling the input text", |
| 483 | this); |
| 484 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 485 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 486 | void |
| 487 | output_options_t::add_options (option_parser_t *parser) |
| 488 | { |
| 489 | GOptionEntry entries[] = |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 490 | { |
Behdad Esfahbod | b5afd8f | 2011-09-19 16:56:21 -0400 | [diff] [blame] | 491 | {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, |
| 492 | {"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] | 493 | {NULL} |
| 494 | }; |
| 495 | parser->add_group (entries, |
| 496 | "output", |
| 497 | "Output options:", |
| 498 | "Options controlling the output", |
| 499 | this); |
| 500 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 501 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 502 | |
| 503 | |
| 504 | hb_font_t * |
| 505 | font_options_t::get_font (void) const |
| 506 | { |
| 507 | if (font) |
| 508 | return font; |
| 509 | |
| 510 | hb_blob_t *blob = NULL; |
| 511 | |
| 512 | /* Create the blob */ |
| 513 | { |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 514 | char *font_data; |
| 515 | unsigned int len = 0; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 516 | hb_destroy_func_t destroy; |
| 517 | void *user_data; |
| 518 | hb_memory_mode_t mm; |
| 519 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 520 | /* 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] | 521 | if (!font_file) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 522 | fail (true, "No font file set"); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 523 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 524 | if (0 == strcmp (font_file, "-")) { |
| 525 | /* read it */ |
| 526 | GString *gs = g_string_new (NULL); |
| 527 | char buf[BUFSIZ]; |
Behdad Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 528 | #ifdef HAVE__SETMODE |
| 529 | _setmode (fileno (stdin), _O_BINARY); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 530 | #endif |
| 531 | while (!feof (stdin)) { |
| 532 | size_t ret = fread (buf, 1, sizeof (buf), stdin); |
| 533 | if (ferror (stdin)) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 534 | fail (false, "Failed reading font from standard input: %s", |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 535 | strerror (errno)); |
| 536 | g_string_append_len (gs, buf, ret); |
| 537 | } |
| 538 | len = gs->len; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 539 | font_data = g_string_free (gs, false); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 540 | user_data = font_data; |
| 541 | destroy = (hb_destroy_func_t) g_free; |
| 542 | mm = HB_MEMORY_MODE_WRITABLE; |
| 543 | } else { |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 544 | GError *error = NULL; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 545 | GMappedFile *mf = g_mapped_file_new (font_file, false, &error); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 546 | if (mf) { |
| 547 | font_data = g_mapped_file_get_contents (mf); |
| 548 | len = g_mapped_file_get_length (mf); |
| 549 | if (len) { |
| 550 | destroy = (hb_destroy_func_t) g_mapped_file_unref; |
| 551 | user_data = (void *) mf; |
| 552 | mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; |
| 553 | } else |
| 554 | g_mapped_file_unref (mf); |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 555 | } else { |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 556 | fail (false, "%s", error->message); |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 557 | //g_error_free (error); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 558 | } |
| 559 | if (!len) { |
| 560 | /* GMappedFile is buggy, it doesn't fail if file isn't regular. |
| 561 | * Try reading. |
| 562 | * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */ |
| 563 | GError *error = NULL; |
| 564 | gsize l; |
| 565 | if (g_file_get_contents (font_file, &font_data, &l, &error)) { |
| 566 | len = l; |
| 567 | destroy = (hb_destroy_func_t) g_free; |
| 568 | user_data = (void *) font_data; |
| 569 | mm = HB_MEMORY_MODE_WRITABLE; |
| 570 | } else { |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 571 | fail (false, "%s", error->message); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 572 | //g_error_free (error); |
| 573 | } |
| 574 | } |
| 575 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 576 | |
| 577 | blob = hb_blob_create (font_data, len, mm, user_data, destroy); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 580 | /* Create the face */ |
| 581 | hb_face_t *face = hb_face_create (blob, face_index); |
| 582 | hb_blob_destroy (blob); |
| 583 | |
| 584 | |
| 585 | font = hb_font_create (face); |
| 586 | |
| 587 | unsigned int upem = hb_face_get_upem (face); |
Behdad Esfahbod | 7bf6ecd | 2011-09-16 01:11:30 -0400 | [diff] [blame] | 588 | hb_font_set_scale (font, upem, upem); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 589 | hb_face_destroy (face); |
| 590 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 591 | #ifdef HAVE_FREETYPE |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 592 | hb_ft_font_set_funcs (font); |
| 593 | #endif |
| 594 | |
| 595 | return font; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | const char * |
| 600 | text_options_t::get_line (unsigned int *len) |
| 601 | { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 602 | if (text) { |
| 603 | if (text_len == (unsigned int) -1) |
| 604 | text_len = strlen (text); |
| 605 | |
| 606 | if (!text_len) { |
| 607 | *len = 0; |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | const char *ret = text; |
| 612 | const char *p = (const char *) memchr (text, '\n', text_len); |
| 613 | unsigned int ret_len; |
| 614 | if (!p) { |
| 615 | ret_len = text_len; |
| 616 | text += ret_len; |
| 617 | text_len = 0; |
| 618 | } else { |
| 619 | ret_len = p - ret; |
| 620 | text += ret_len + 1; |
| 621 | text_len -= ret_len + 1; |
| 622 | } |
| 623 | |
| 624 | *len = ret_len; |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | if (!fp) { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 629 | if (!text_file) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 630 | fail (true, "At least one of text or text-file must be set"); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 631 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 632 | if (0 != strcmp (text_file, "-")) |
| 633 | fp = fopen (text_file, "r"); |
| 634 | else |
| 635 | fp = stdin; |
| 636 | |
| 637 | if (!fp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 638 | fail (false, "Failed opening text file `%s': %s", |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 639 | text_file, strerror (errno)); |
| 640 | |
| 641 | gs = g_string_new (NULL); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 644 | g_string_set_size (gs, 0); |
| 645 | char buf[BUFSIZ]; |
| 646 | while (fgets (buf, sizeof (buf), fp)) { |
| 647 | unsigned int bytes = strlen (buf); |
Behdad Esfahbod | 27c36af | 2012-01-19 12:30:43 -0500 | [diff] [blame] | 648 | if (bytes && buf[bytes - 1] == '\n') { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 649 | bytes--; |
| 650 | g_string_append_len (gs, buf, bytes); |
| 651 | break; |
| 652 | } |
| 653 | g_string_append_len (gs, buf, bytes); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 654 | } |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 655 | if (ferror (fp)) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 656 | fail (false, "Failed reading text: %s", |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 657 | strerror (errno)); |
| 658 | *len = gs->len; |
| 659 | return !*len && feof (fp) ? NULL : gs->str; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 660 | } |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 661 | |
| 662 | |
| 663 | FILE * |
| 664 | output_options_t::get_file_handle (void) |
| 665 | { |
| 666 | if (fp) |
| 667 | return fp; |
| 668 | |
| 669 | if (output_file) |
| 670 | fp = fopen (output_file, "wb"); |
| 671 | else { |
Behdad Esfahbod | 0fe2960 | 2011-09-17 09:59:58 -0400 | [diff] [blame] | 672 | #ifdef HAVE__SETMODE |
| 673 | _setmode (fileno (stdout), _O_BINARY); |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 674 | #endif |
| 675 | fp = stdout; |
| 676 | } |
| 677 | if (!fp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 678 | fail (false, "Cannot open output file `%s': %s", |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 679 | g_filename_display_name (output_file), strerror (errno)); |
| 680 | |
| 681 | return fp; |
| 682 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 683 | |
Behdad Esfahbod | c188548 | 2012-06-04 08:56:00 -0400 | [diff] [blame] | 684 | static gboolean |
| 685 | parse_verbose (const char *name G_GNUC_UNUSED, |
| 686 | const char *arg G_GNUC_UNUSED, |
| 687 | gpointer data G_GNUC_UNUSED, |
| 688 | GError **error G_GNUC_UNUSED) |
| 689 | { |
| 690 | format_options_t *format_opts = (format_options_t *) data; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 691 | format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true; |
| 692 | return true; |
Behdad Esfahbod | c188548 | 2012-06-04 08:56:00 -0400 | [diff] [blame] | 693 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 694 | |
| 695 | void |
| 696 | format_options_t::add_options (option_parser_t *parser) |
| 697 | { |
| 698 | GOptionEntry entries[] = |
| 699 | { |
| 700 | {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL}, |
| 701 | {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL}, |
| 702 | {"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] | 703 | {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL}, |
| 704 | {"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] | 705 | {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL}, |
Behdad Esfahbod | c188548 | 2012-06-04 08:56:00 -0400 | [diff] [blame] | 706 | {"verbose", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose, "Show everything", NULL}, |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 707 | {NULL} |
| 708 | }; |
| 709 | parser->add_group (entries, |
| 710 | "format", |
| 711 | "Format options:", |
| 712 | "Options controlling the formatting of buffer contents", |
| 713 | this); |
| 714 | } |
| 715 | |
| 716 | void |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 717 | format_options_t::serialize_unicode (hb_buffer_t *buffer, |
| 718 | GString *gs) |
| 719 | { |
| 720 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 721 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); |
| 722 | |
| 723 | g_string_append_c (gs, '<'); |
| 724 | for (unsigned int i = 0; i < num_glyphs; i++) |
| 725 | { |
| 726 | if (i) |
| 727 | g_string_append_c (gs, ','); |
| 728 | g_string_append_printf (gs, "U+%04X", info->codepoint); |
| 729 | info++; |
| 730 | } |
| 731 | g_string_append_c (gs, '>'); |
| 732 | } |
| 733 | |
| 734 | void |
| 735 | format_options_t::serialize_glyphs (hb_buffer_t *buffer, |
| 736 | hb_font_t *font, |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 737 | hb_bool_t utf8_clusters, |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 738 | GString *gs) |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 739 | { |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 740 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 741 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); |
| 742 | hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL); |
| 743 | |
Behdad Esfahbod | c91c4fa | 2012-01-19 17:51:57 -0500 | [diff] [blame] | 744 | g_string_append_c (gs, '['); |
Behdad Esfahbod | 4225581 | 2011-09-19 17:57:02 -0400 | [diff] [blame] | 745 | for (unsigned int i = 0; i < num_glyphs; i++) |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 746 | { |
| 747 | if (i) |
| 748 | g_string_append_c (gs, '|'); |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 749 | |
Behdad Esfahbod | bce0955 | 2012-05-27 11:29:21 -0400 | [diff] [blame] | 750 | char glyph_name[32]; |
Behdad Esfahbod | d3f3690 | 2011-09-21 16:41:43 -0400 | [diff] [blame] | 751 | if (show_glyph_names) { |
Behdad Esfahbod | bce0955 | 2012-05-27 11:29:21 -0400 | [diff] [blame] | 752 | hb_font_get_glyph_name (font, info->codepoint, glyph_name, sizeof (glyph_name)); |
| 753 | g_string_append_printf (gs, "%s", glyph_name); |
Behdad Esfahbod | d3f3690 | 2011-09-21 16:41:43 -0400 | [diff] [blame] | 754 | } else |
Behdad Esfahbod | 5857720 | 2011-09-27 12:36:26 -0400 | [diff] [blame] | 755 | g_string_append_printf (gs, "%u", info->codepoint); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 756 | |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 757 | if (show_clusters) { |
Behdad Esfahbod | 5857720 | 2011-09-27 12:36:26 -0400 | [diff] [blame] | 758 | g_string_append_printf (gs, "=%u", info->cluster); |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 759 | if (utf8_clusters) |
| 760 | g_string_append (gs, "u8"); |
| 761 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 762 | |
| 763 | if (show_positions && (pos->x_offset || pos->y_offset)) { |
| 764 | g_string_append_c (gs, '@'); |
| 765 | if (pos->x_offset) g_string_append_printf (gs, "%d", pos->x_offset); |
| 766 | if (pos->y_offset) g_string_append_printf (gs, ",%d", pos->y_offset); |
| 767 | } |
| 768 | if (show_positions && (pos->x_advance || pos->y_advance)) { |
| 769 | g_string_append_c (gs, '+'); |
| 770 | if (pos->x_advance) g_string_append_printf (gs, "%d", pos->x_advance); |
| 771 | if (pos->y_advance) g_string_append_printf (gs, ",%d", pos->y_advance); |
| 772 | } |
| 773 | |
| 774 | info++; |
| 775 | pos++; |
| 776 | } |
Behdad Esfahbod | c91c4fa | 2012-01-19 17:51:57 -0500 | [diff] [blame] | 777 | g_string_append_c (gs, ']'); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 778 | } |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 779 | void |
| 780 | format_options_t::serialize_line_no (unsigned int line_no, |
| 781 | GString *gs) |
| 782 | { |
| 783 | if (show_line_num) |
| 784 | g_string_append_printf (gs, "%d: ", line_no); |
| 785 | } |
| 786 | void |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 787 | format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer, |
| 788 | unsigned int line_no, |
| 789 | const char *text, |
| 790 | unsigned int text_len, |
| 791 | hb_font_t *font, |
| 792 | hb_bool_t utf8_clusters, |
| 793 | GString *gs) |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 794 | { |
| 795 | if (show_text) { |
| 796 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | d8134bc | 2012-01-20 17:18:59 -0500 | [diff] [blame] | 797 | g_string_append_c (gs, '('); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 798 | g_string_append_len (gs, text, text_len); |
Behdad Esfahbod | d8134bc | 2012-01-20 17:18:59 -0500 | [diff] [blame] | 799 | g_string_append_c (gs, ')'); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 800 | g_string_append_c (gs, '\n'); |
| 801 | } |
| 802 | |
| 803 | if (show_unicode) { |
| 804 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 805 | serialize_unicode (buffer, gs); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 806 | g_string_append_c (gs, '\n'); |
| 807 | } |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 808 | } |
| 809 | void |
| 810 | format_options_t::serialize_message (unsigned int line_no, |
| 811 | const char *msg, |
| 812 | GString *gs) |
| 813 | { |
| 814 | serialize_line_no (line_no, gs); |
| 815 | g_string_append_printf (gs, "%s", msg); |
| 816 | g_string_append_c (gs, '\n'); |
| 817 | } |
| 818 | void |
| 819 | format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer, |
| 820 | unsigned int line_no, |
| 821 | const char *text, |
| 822 | unsigned int text_len, |
| 823 | hb_font_t *font, |
| 824 | hb_bool_t utf8_clusters, |
| 825 | GString *gs) |
| 826 | { |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 827 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 828 | serialize_glyphs (buffer, font, utf8_clusters, gs); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 829 | g_string_append_c (gs, '\n'); |
| 830 | } |