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