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 | ade7459 | 2012-08-06 19:42:47 -0700 | [diff] [blame] | 181 | g_strfreev (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 | static gboolean |
| 200 | parse_features (const char *name G_GNUC_UNUSED, |
| 201 | const char *arg, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 202 | gpointer data, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 203 | GError **error G_GNUC_UNUSED) |
| 204 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 205 | shape_options_t *shape_opts = (shape_options_t *) data; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 206 | char *s = (char *) arg; |
| 207 | char *p; |
| 208 | |
| 209 | shape_opts->num_features = 0; |
Behdad Esfahbod | 8f8956a | 2012-05-25 14:30:24 -0400 | [diff] [blame] | 210 | g_free (shape_opts->features); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 211 | shape_opts->features = NULL; |
| 212 | |
| 213 | if (!*s) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 214 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 215 | |
| 216 | /* count the features first, so we can allocate memory */ |
| 217 | p = s; |
| 218 | do { |
| 219 | shape_opts->num_features++; |
| 220 | p = strchr (p, ','); |
| 221 | if (p) |
| 222 | p++; |
| 223 | } while (p); |
| 224 | |
| 225 | shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features)); |
| 226 | |
| 227 | /* now do the actual parsing */ |
| 228 | p = s; |
| 229 | shape_opts->num_features = 0; |
Behdad Esfahbod | e30ebd2 | 2012-09-06 22:09:06 -0400 | [diff] [blame] | 230 | while (p && *p) { |
| 231 | char *end = strchr (p, ','); |
| 232 | if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features])) |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 233 | shape_opts->num_features++; |
Behdad Esfahbod | e30ebd2 | 2012-09-06 22:09:06 -0400 | [diff] [blame] | 234 | p = end ? end + 1 : NULL; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 237 | return true; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 241 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 242 | view_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 243 | { |
| 244 | GOptionEntry entries[] = |
| 245 | { |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 246 | {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL}, |
Behdad Esfahbod | 9a34a50 | 2012-12-05 19:18:18 -0500 | [diff] [blame] | 247 | {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "red/#rrggbb/#rrggbbaa"}, |
| 248 | {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "red/#rrggbb/#rrggbbaa"}, |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 249 | {"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] | 250 | {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"}, |
| 251 | {"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &this->font_size, "Font size (default: " G_STRINGIFY(DEFAULT_FONT_SIZE) ")","size"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 252 | {NULL} |
| 253 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 254 | parser->add_group (entries, |
| 255 | "view", |
| 256 | "View options:", |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 257 | "Options controlling output rendering", |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 258 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 262 | shape_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 263 | { |
| 264 | GOptionEntry entries[] = |
| 265 | { |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 266 | {"list-shapers", 0, G_OPTION_FLAG_NO_ARG, |
| 267 | 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] | 268 | {"shaper", 0, G_OPTION_FLAG_HIDDEN, |
| 269 | G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL}, |
Behdad Esfahbod | fd528c1 | 2011-10-12 15:03:58 -0400 | [diff] [blame] | 270 | {"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] | 271 | {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, |
| 272 | {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"}, |
| 273 | {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, |
Behdad Esfahbod | 407f80d | 2012-11-13 15:33:27 -0800 | [diff] [blame] | 274 | {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", NULL}, |
| 275 | {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", NULL}, |
| 276 | {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", NULL}, |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 277 | {"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] | 278 | {"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] | 279 | {"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] | 280 | {NULL} |
| 281 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 282 | parser->add_group (entries, |
| 283 | "shape", |
| 284 | "Shape options:", |
| 285 | "Options controlling the shaping process", |
| 286 | this); |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 287 | |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 288 | const gchar *features_help = "Comma-separated list of font features\n" |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 289 | "\n" |
| 290 | " Features can be enabled or disabled, either globally or limited to\n" |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 291 | " specific character ranges.\n" |
| 292 | "\n" |
| 293 | " The range indices refer to the positions between Unicode characters,\n" |
| 294 | " unless the --utf8-clusters is provided, in which case range indices\n" |
| 295 | " refer to UTF-8 byte indices. The position before the first character\n" |
| 296 | " is always 0.\n" |
Behdad Esfahbod | d530024 | 2012-01-21 19:07:22 -0500 | [diff] [blame] | 297 | "\n" |
| 298 | " The format is Python-esque. Here is how it all works:\n" |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 299 | "\n" |
| 300 | " Syntax: Value: Start: End:\n" |
| 301 | "\n" |
| 302 | " Setting value:\n" |
| 303 | " \"kern\" 1 0 ∞ # Turn feature on\n" |
| 304 | " \"+kern\" 1 0 ∞ # Turn feature on\n" |
| 305 | " \"-kern\" 0 0 ∞ # Turn feature off\n" |
| 306 | " \"kern=0\" 0 0 ∞ # Turn feature off\n" |
| 307 | " \"kern=1\" 1 0 ∞ # Turn feature on\n" |
| 308 | " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n" |
| 309 | "\n" |
| 310 | " Setting index:\n" |
| 311 | " \"kern[]\" 1 0 ∞ # Turn feature on\n" |
| 312 | " \"kern[:]\" 1 0 ∞ # Turn feature on\n" |
| 313 | " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n" |
| 314 | " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n" |
| 315 | " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n" |
| 316 | " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n" |
| 317 | "\n" |
| 318 | " Mixing it all:\n" |
| 319 | "\n" |
Behdad Esfahbod | 3bc22eb | 2012-11-12 10:07:28 -0800 | [diff] [blame] | 320 | " \"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] | 321 | |
| 322 | GOptionEntry entries2[] = |
| 323 | { |
| 324 | {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"}, |
| 325 | {NULL} |
| 326 | }; |
| 327 | parser->add_group (entries2, |
| 328 | "features", |
| 329 | "Features options:", |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 330 | "Options controlling font features used", |
Behdad Esfahbod | 8750aba | 2012-01-18 22:47:44 -0500 | [diff] [blame] | 331 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 335 | font_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 336 | { |
| 337 | GOptionEntry entries[] = |
| 338 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 339 | {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Font file-name", "filename"}, |
| 340 | {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Face index (default: 0)", "index"}, |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 341 | {NULL} |
| 342 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 343 | parser->add_group (entries, |
| 344 | "font", |
| 345 | "Font options:", |
| 346 | "Options controlling the font", |
| 347 | this); |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 348 | } |
| 349 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 350 | void |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 351 | text_options_t::add_options (option_parser_t *parser) |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 352 | { |
| 353 | GOptionEntry entries[] = |
| 354 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 355 | {"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] | 356 | {"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] | 357 | {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"}, |
| 358 | {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"}, |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 359 | {NULL} |
| 360 | }; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 361 | parser->add_group (entries, |
| 362 | "text", |
| 363 | "Text options:", |
| 364 | "Options controlling the input text", |
| 365 | this); |
| 366 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 367 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 368 | void |
| 369 | output_options_t::add_options (option_parser_t *parser) |
| 370 | { |
Behdad Esfahbod | 9815a88 | 2012-12-21 16:46:53 -0500 | [diff] [blame] | 371 | const char *text; |
| 372 | |
| 373 | if (NULL == supported_formats) |
| 374 | text = "Set output format"; |
| 375 | else |
| 376 | text = g_strdup_printf ("Set output format\n\n Supported formats are: %s", supported_formats); |
| 377 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 378 | GOptionEntry entries[] = |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 379 | { |
Behdad Esfahbod | b5afd8f | 2011-09-19 16:56:21 -0400 | [diff] [blame] | 380 | {"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] | 381 | {"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] | 382 | {NULL} |
| 383 | }; |
| 384 | parser->add_group (entries, |
| 385 | "output", |
| 386 | "Output options:", |
| 387 | "Options controlling the output", |
| 388 | this); |
| 389 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 390 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 391 | |
| 392 | |
| 393 | hb_font_t * |
| 394 | font_options_t::get_font (void) const |
| 395 | { |
| 396 | if (font) |
| 397 | return font; |
| 398 | |
| 399 | hb_blob_t *blob = NULL; |
| 400 | |
| 401 | /* Create the blob */ |
| 402 | { |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 403 | char *font_data; |
| 404 | unsigned int len = 0; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 405 | hb_destroy_func_t destroy; |
| 406 | void *user_data; |
| 407 | hb_memory_mode_t mm; |
| 408 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 409 | /* 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] | 410 | if (!font_file) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 411 | fail (true, "No font file set"); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 412 | |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 413 | if (0 == strcmp (font_file, "-")) { |
| 414 | /* read it */ |
| 415 | GString *gs = g_string_new (NULL); |
| 416 | char buf[BUFSIZ]; |
Behdad Esfahbod | e2aab4b | 2013-02-12 15:35:32 -0500 | [diff] [blame] | 417 | #if defined(_WIN32) || defined(__CYGWIN__) |
Behdad Esfahbod | bc76449 | 2013-01-31 18:18:05 -0500 | [diff] [blame] | 418 | setmode (fileno (stdin), _O_BINARY); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 419 | #endif |
| 420 | while (!feof (stdin)) { |
| 421 | size_t ret = fread (buf, 1, sizeof (buf), stdin); |
| 422 | if (ferror (stdin)) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 423 | fail (false, "Failed reading font from standard input: %s", |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 424 | strerror (errno)); |
| 425 | g_string_append_len (gs, buf, ret); |
| 426 | } |
| 427 | len = gs->len; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 428 | font_data = g_string_free (gs, false); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 429 | user_data = font_data; |
| 430 | destroy = (hb_destroy_func_t) g_free; |
| 431 | mm = HB_MEMORY_MODE_WRITABLE; |
| 432 | } else { |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 433 | GError *error = NULL; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 434 | GMappedFile *mf = g_mapped_file_new (font_file, false, &error); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 435 | if (mf) { |
| 436 | font_data = g_mapped_file_get_contents (mf); |
| 437 | len = g_mapped_file_get_length (mf); |
| 438 | if (len) { |
Behdad Esfahbod | c2bc818 | 2013-10-27 23:36:35 +0100 | [diff] [blame] | 439 | destroy = (hb_destroy_func_t) g_mapped_file_unref; |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 440 | user_data = (void *) mf; |
| 441 | mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; |
| 442 | } else |
Behdad Esfahbod | c2bc818 | 2013-10-27 23:36:35 +0100 | [diff] [blame] | 443 | g_mapped_file_unref (mf); |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 444 | } else { |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 445 | fail (false, "%s", error->message); |
Behdad Esfahbod | f51e167 | 2012-01-30 09:48:33 -0500 | [diff] [blame] | 446 | //g_error_free (error); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 447 | } |
| 448 | if (!len) { |
| 449 | /* GMappedFile is buggy, it doesn't fail if file isn't regular. |
| 450 | * Try reading. |
| 451 | * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */ |
| 452 | GError *error = NULL; |
| 453 | gsize l; |
| 454 | if (g_file_get_contents (font_file, &font_data, &l, &error)) { |
| 455 | len = l; |
| 456 | destroy = (hb_destroy_func_t) g_free; |
| 457 | user_data = (void *) font_data; |
| 458 | mm = HB_MEMORY_MODE_WRITABLE; |
| 459 | } else { |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 460 | fail (false, "%s", error->message); |
Behdad Esfahbod | 4451168 | 2011-09-16 00:38:19 -0400 | [diff] [blame] | 461 | //g_error_free (error); |
| 462 | } |
| 463 | } |
| 464 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 465 | |
| 466 | blob = hb_blob_create (font_data, len, mm, user_data, destroy); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 469 | /* Create the face */ |
| 470 | hb_face_t *face = hb_face_create (blob, face_index); |
| 471 | hb_blob_destroy (blob); |
| 472 | |
| 473 | |
| 474 | font = hb_font_create (face); |
| 475 | |
| 476 | unsigned int upem = hb_face_get_upem (face); |
Behdad Esfahbod | 7bf6ecd | 2011-09-16 01:11:30 -0400 | [diff] [blame] | 477 | hb_font_set_scale (font, upem, upem); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 478 | hb_face_destroy (face); |
| 479 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 480 | #ifdef HAVE_FREETYPE |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 481 | hb_ft_font_set_funcs (font); |
| 482 | #endif |
| 483 | |
| 484 | return font; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | const char * |
| 489 | text_options_t::get_line (unsigned int *len) |
| 490 | { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 491 | if (text) { |
| 492 | if (text_len == (unsigned int) -1) |
| 493 | text_len = strlen (text); |
| 494 | |
| 495 | if (!text_len) { |
| 496 | *len = 0; |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | const char *ret = text; |
| 501 | const char *p = (const char *) memchr (text, '\n', text_len); |
| 502 | unsigned int ret_len; |
| 503 | if (!p) { |
| 504 | ret_len = text_len; |
| 505 | text += ret_len; |
| 506 | text_len = 0; |
| 507 | } else { |
| 508 | ret_len = p - ret; |
| 509 | text += ret_len + 1; |
| 510 | text_len -= ret_len + 1; |
| 511 | } |
| 512 | |
| 513 | *len = ret_len; |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | if (!fp) { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 518 | if (!text_file) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 519 | 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] | 520 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 521 | if (0 != strcmp (text_file, "-")) |
| 522 | fp = fopen (text_file, "r"); |
| 523 | else |
| 524 | fp = stdin; |
| 525 | |
| 526 | if (!fp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 527 | fail (false, "Failed opening text file `%s': %s", |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 528 | text_file, strerror (errno)); |
| 529 | |
| 530 | gs = g_string_new (NULL); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 533 | g_string_set_size (gs, 0); |
| 534 | char buf[BUFSIZ]; |
| 535 | while (fgets (buf, sizeof (buf), fp)) { |
| 536 | unsigned int bytes = strlen (buf); |
Behdad Esfahbod | 27c36af | 2012-01-19 12:30:43 -0500 | [diff] [blame] | 537 | if (bytes && buf[bytes - 1] == '\n') { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 538 | bytes--; |
| 539 | g_string_append_len (gs, buf, bytes); |
| 540 | break; |
| 541 | } |
| 542 | g_string_append_len (gs, buf, bytes); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 543 | } |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 544 | if (ferror (fp)) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 545 | fail (false, "Failed reading text: %s", |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 546 | strerror (errno)); |
| 547 | *len = gs->len; |
| 548 | return !*len && feof (fp) ? NULL : gs->str; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 549 | } |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 550 | |
| 551 | |
| 552 | FILE * |
| 553 | output_options_t::get_file_handle (void) |
| 554 | { |
| 555 | if (fp) |
| 556 | return fp; |
| 557 | |
| 558 | if (output_file) |
| 559 | fp = fopen (output_file, "wb"); |
| 560 | else { |
Behdad Esfahbod | e2aab4b | 2013-02-12 15:35:32 -0500 | [diff] [blame] | 561 | #if defined(_WIN32) || defined(__CYGWIN__) |
Behdad Esfahbod | ceeae30 | 2013-01-31 19:27:36 -0500 | [diff] [blame] | 562 | setmode (fileno (stdout), _O_BINARY); |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 563 | #endif |
| 564 | fp = stdout; |
| 565 | } |
| 566 | if (!fp) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 567 | fail (false, "Cannot open output file `%s': %s", |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 568 | g_filename_display_name (output_file), strerror (errno)); |
| 569 | |
| 570 | return fp; |
| 571 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 572 | |
Behdad Esfahbod | c188548 | 2012-06-04 08:56:00 -0400 | [diff] [blame] | 573 | static gboolean |
| 574 | parse_verbose (const char *name G_GNUC_UNUSED, |
| 575 | const char *arg G_GNUC_UNUSED, |
| 576 | gpointer data G_GNUC_UNUSED, |
| 577 | GError **error G_GNUC_UNUSED) |
| 578 | { |
| 579 | format_options_t *format_opts = (format_options_t *) data; |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 580 | format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true; |
| 581 | return true; |
Behdad Esfahbod | c188548 | 2012-06-04 08:56:00 -0400 | [diff] [blame] | 582 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 583 | |
| 584 | void |
| 585 | format_options_t::add_options (option_parser_t *parser) |
| 586 | { |
| 587 | GOptionEntry entries[] = |
| 588 | { |
| 589 | {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL}, |
| 590 | {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL}, |
| 591 | {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL}, |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 592 | {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL}, |
| 593 | {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL}, |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 594 | {"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] | 595 | {"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] | 596 | {NULL} |
| 597 | }; |
| 598 | parser->add_group (entries, |
| 599 | "format", |
| 600 | "Format options:", |
| 601 | "Options controlling the formatting of buffer contents", |
| 602 | this); |
| 603 | } |
| 604 | |
| 605 | void |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 606 | format_options_t::serialize_unicode (hb_buffer_t *buffer, |
| 607 | GString *gs) |
| 608 | { |
| 609 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 610 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); |
| 611 | |
| 612 | g_string_append_c (gs, '<'); |
| 613 | for (unsigned int i = 0; i < num_glyphs; i++) |
| 614 | { |
| 615 | if (i) |
| 616 | g_string_append_c (gs, ','); |
| 617 | g_string_append_printf (gs, "U+%04X", info->codepoint); |
| 618 | info++; |
| 619 | } |
| 620 | g_string_append_c (gs, '>'); |
| 621 | } |
| 622 | |
| 623 | void |
| 624 | format_options_t::serialize_glyphs (hb_buffer_t *buffer, |
| 625 | hb_font_t *font, |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 626 | hb_buffer_serialize_format_t output_format, |
| 627 | hb_buffer_serialize_flags_t flags, |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 628 | GString *gs) |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 629 | { |
Behdad Esfahbod | c91c4fa | 2012-01-19 17:51:57 -0500 | [diff] [blame] | 630 | g_string_append_c (gs, '['); |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 631 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 632 | unsigned int start = 0; |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 633 | |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 634 | while (start < num_glyphs) { |
| 635 | char buf[1024]; |
| 636 | unsigned int consumed; |
| 637 | start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs, |
| 638 | buf, sizeof (buf), &consumed, |
| 639 | font, output_format, flags); |
| 640 | if (!consumed) |
| 641 | break; |
| 642 | g_string_append (gs, buf); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 643 | } |
Behdad Esfahbod | c91c4fa | 2012-01-19 17:51:57 -0500 | [diff] [blame] | 644 | g_string_append_c (gs, ']'); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 645 | } |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 646 | void |
| 647 | format_options_t::serialize_line_no (unsigned int line_no, |
| 648 | GString *gs) |
| 649 | { |
| 650 | if (show_line_num) |
| 651 | g_string_append_printf (gs, "%d: ", line_no); |
| 652 | } |
| 653 | void |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 654 | format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer, |
| 655 | unsigned int line_no, |
| 656 | const char *text, |
| 657 | unsigned int text_len, |
| 658 | hb_font_t *font, |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 659 | GString *gs) |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 660 | { |
| 661 | if (show_text) { |
| 662 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | d8134bc | 2012-01-20 17:18:59 -0500 | [diff] [blame] | 663 | g_string_append_c (gs, '('); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 664 | g_string_append_len (gs, text, text_len); |
Behdad Esfahbod | d8134bc | 2012-01-20 17:18:59 -0500 | [diff] [blame] | 665 | g_string_append_c (gs, ')'); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 666 | g_string_append_c (gs, '\n'); |
| 667 | } |
| 668 | |
| 669 | if (show_unicode) { |
| 670 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 671 | serialize_unicode (buffer, gs); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 672 | g_string_append_c (gs, '\n'); |
| 673 | } |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 674 | } |
| 675 | void |
| 676 | format_options_t::serialize_message (unsigned int line_no, |
| 677 | const char *msg, |
| 678 | GString *gs) |
| 679 | { |
| 680 | serialize_line_no (line_no, gs); |
| 681 | g_string_append_printf (gs, "%s", msg); |
| 682 | g_string_append_c (gs, '\n'); |
| 683 | } |
| 684 | void |
| 685 | format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer, |
| 686 | unsigned int line_no, |
| 687 | const char *text, |
| 688 | unsigned int text_len, |
| 689 | hb_font_t *font, |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 690 | hb_buffer_serialize_format_t output_format, |
| 691 | hb_buffer_serialize_flags_t format_flags, |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 692 | GString *gs) |
| 693 | { |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 694 | serialize_line_no (line_no, gs); |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 695 | serialize_glyphs (buffer, font, output_format, format_flags, gs); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 696 | g_string_append_c (gs, '\n'); |
| 697 | } |