Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 27 | #ifndef OPTIONS_HH |
| 28 | #define OPTIONS_HH |
| 29 | |
| 30 | |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include "config.h" |
| 33 | #endif |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <stddef.h> |
| 37 | #include <string.h> |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <math.h> |
| 40 | #include <locale.h> |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
Behdad Esfahbod | 52e7b14 | 2012-05-13 02:02:58 +0200 | [diff] [blame] | 43 | #ifdef HAVE_UNISTD_H |
| 44 | #include <unistd.h> /* for isatty() */ |
| 45 | #endif |
Behdad Esfahbod | e2aab4b | 2013-02-12 15:35:32 -0500 | [diff] [blame] | 46 | #if defined(_WIN32) || defined(__CYGWIN__) |
Behdad Esfahbod | bc76449 | 2013-01-31 18:18:05 -0500 | [diff] [blame] | 47 | #include <io.h> /* for setmode() under Windows */ |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | #include <hb.h> |
Behdad Esfahbod | c87b317 | 2012-05-15 23:53:18 -0400 | [diff] [blame] | 51 | #ifdef HAVE_OT |
| 52 | #include <hb-ot.h> |
| 53 | #endif |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 54 | #include <glib.h> |
| 55 | #include <glib/gprintf.h> |
| 56 | |
Behdad Esfahbod | c2bc818 | 2013-10-27 23:36:35 +0100 | [diff] [blame] | 57 | #if !GLIB_CHECK_VERSION (2, 22, 0) |
| 58 | # define g_mapped_file_unref g_mapped_file_free |
| 59 | #endif |
| 60 | |
Behdad Esfahbod | 8650def | 2014-07-05 15:50:18 -0400 | [diff] [blame] | 61 | |
| 62 | /* A few macros copied from hb-private.hh. */ |
| 63 | |
| 64 | #if __GNUC__ >= 4 |
| 65 | #define HB_UNUSED __attribute__((unused)) |
| 66 | #else |
| 67 | #define HB_UNUSED |
| 68 | #endif |
| 69 | |
Behdad Esfahbod | 69b84a8 | 2012-04-12 15:50:40 -0400 | [diff] [blame] | 70 | #undef MIN |
| 71 | template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } |
| 72 | |
| 73 | #undef MAX |
| 74 | template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } |
| 75 | |
Behdad Esfahbod | 8650def | 2014-07-05 15:50:18 -0400 | [diff] [blame] | 76 | #undef ARRAY_LENGTH |
| 77 | template <typename Type, unsigned int n> |
| 78 | static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } |
| 79 | /* A const version, but does not detect erratically being called on pointers. */ |
| 80 | #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) |
| 81 | |
| 82 | #define _ASSERT_STATIC1(_line, _cond) HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] |
| 83 | #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) |
| 84 | #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) |
| 85 | |
Behdad Esfahbod | 69b84a8 | 2012-04-12 15:50:40 -0400 | [diff] [blame] | 86 | |
Behdad Esfahbod | ea5e8a0 | 2014-03-19 15:38:02 -0700 | [diff] [blame] | 87 | void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3); |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 88 | |
| 89 | |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 90 | extern hb_bool_t debug; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 91 | |
| 92 | struct option_group_t |
| 93 | { |
| 94 | virtual void add_options (struct option_parser_t *parser) = 0; |
| 95 | |
| 96 | virtual void pre_parse (GError **error G_GNUC_UNUSED) {}; |
| 97 | virtual void post_parse (GError **error G_GNUC_UNUSED) {}; |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | struct option_parser_t |
| 102 | { |
| 103 | option_parser_t (const char *usage) { |
| 104 | memset (this, 0, sizeof (*this)); |
| 105 | usage_str = usage; |
| 106 | context = g_option_context_new (usage); |
Behdad Esfahbod | 2306ad4 | 2014-07-04 18:09:29 -0400 | [diff] [blame] | 107 | to_free = g_ptr_array_new (); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 108 | |
| 109 | add_main_options (); |
| 110 | } |
| 111 | ~option_parser_t (void) { |
| 112 | g_option_context_free (context); |
Behdad Esfahbod | 2306ad4 | 2014-07-04 18:09:29 -0400 | [diff] [blame] | 113 | g_ptr_array_foreach (to_free, (GFunc) g_free, NULL); |
| 114 | g_ptr_array_free (to_free, TRUE); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void add_main_options (void); |
| 118 | |
| 119 | void add_group (GOptionEntry *entries, |
| 120 | const gchar *name, |
| 121 | const gchar *description, |
| 122 | const gchar *help_description, |
| 123 | option_group_t *option_group); |
| 124 | |
Behdad Esfahbod | 2306ad4 | 2014-07-04 18:09:29 -0400 | [diff] [blame] | 125 | void free_later (char *p) { |
| 126 | g_ptr_array_add (to_free, p); |
| 127 | } |
| 128 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 129 | void parse (int *argc, char ***argv); |
| 130 | |
| 131 | G_GNUC_NORETURN void usage (void) { |
| 132 | g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str); |
| 133 | exit (1); |
| 134 | } |
| 135 | |
Behdad Esfahbod | 2306ad4 | 2014-07-04 18:09:29 -0400 | [diff] [blame] | 136 | private: |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 137 | const char *usage_str; |
| 138 | GOptionContext *context; |
Behdad Esfahbod | 2306ad4 | 2014-07-04 18:09:29 -0400 | [diff] [blame] | 139 | GPtrArray *to_free; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | |
Behdad Esfahbod | 912c5ff | 2012-05-13 12:51:02 +0200 | [diff] [blame] | 143 | #define DEFAULT_MARGIN 16 |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 144 | #define DEFAULT_FORE "#000000" |
| 145 | #define DEFAULT_BACK "#FFFFFF" |
Behdad Esfahbod | cd4eb96 | 2015-01-20 12:30:45 -0800 | [diff] [blame] | 146 | #define FONT_SIZE_UPEM 0x7FFFFFFF |
| 147 | #define FONT_SIZE_NONE 0 |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 148 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 149 | struct view_options_t : option_group_t |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 150 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 151 | view_options_t (option_parser_t *parser) { |
| 152 | annotate = false; |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 153 | fore = DEFAULT_FORE; |
| 154 | back = DEFAULT_BACK; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 155 | line_space = 0; |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 156 | margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 157 | |
| 158 | add_options (parser); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 161 | void add_options (option_parser_t *parser); |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 162 | |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 163 | hb_bool_t annotate; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 164 | const char *fore; |
| 165 | const char *back; |
| 166 | double line_space; |
| 167 | struct margin_t { |
| 168 | double t, r, b, l; |
| 169 | } margin; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 170 | }; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 171 | |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 172 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 173 | struct shape_options_t : option_group_t |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 174 | { |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 175 | shape_options_t (option_parser_t *parser) |
| 176 | { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 177 | direction = language = script = NULL; |
Behdad Esfahbod | 407f80d | 2012-11-13 15:33:27 -0800 | [diff] [blame] | 178 | bot = eot = preserve_default_ignorables = false; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 179 | features = NULL; |
| 180 | num_features = 0; |
| 181 | shapers = NULL; |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 182 | utf8_clusters = false; |
Behdad Esfahbod | 376d587 | 2015-07-22 16:51:12 +0100 | [diff] [blame^] | 183 | cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT; |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 184 | normalize_glyphs = false; |
Behdad Esfahbod | 50067e2 | 2013-04-11 16:31:01 -0400 | [diff] [blame] | 185 | num_iterations = 1; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 186 | |
| 187 | add_options (parser); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 188 | } |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 189 | ~shape_options_t (void) |
| 190 | { |
Behdad Esfahbod | 90e312c | 2011-09-08 16:42:37 -0400 | [diff] [blame] | 191 | free (features); |
Behdad Esfahbod | ade7459 | 2012-08-06 19:42:47 -0700 | [diff] [blame] | 192 | g_strfreev (shapers); |
Behdad Esfahbod | 90e312c | 2011-09-08 16:42:37 -0400 | [diff] [blame] | 193 | } |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 194 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 195 | void add_options (option_parser_t *parser); |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 196 | |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 197 | void setup_buffer (hb_buffer_t *buffer) |
| 198 | { |
Behdad Esfahbod | 516857e | 2011-09-08 16:50:24 -0400 | [diff] [blame] | 199 | hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1)); |
| 200 | hb_buffer_set_script (buffer, hb_script_from_string (script, -1)); |
| 201 | hb_buffer_set_language (buffer, hb_language_from_string (language, -1)); |
Behdad Esfahbod | 4dc798d | 2013-08-26 20:39:00 -0400 | [diff] [blame] | 202 | hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_DEFAULT | |
Behdad Esfahbod | 407f80d | 2012-11-13 15:33:27 -0800 | [diff] [blame] | 203 | (bot ? HB_BUFFER_FLAG_BOT : 0) | |
| 204 | (eot ? HB_BUFFER_FLAG_EOT : 0) | |
| 205 | (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0))); |
Behdad Esfahbod | 376d587 | 2015-07-22 16:51:12 +0100 | [diff] [blame^] | 206 | hb_buffer_set_cluster_level (buffer, cluster_level); |
Behdad Esfahbod | c462b32 | 2013-02-15 07:51:47 -0500 | [diff] [blame] | 207 | hb_buffer_guess_segment_properties (buffer); |
Behdad Esfahbod | 4f4b114 | 2011-09-08 16:49:02 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Behdad Esfahbod | 321f73c | 2012-11-13 15:12:24 -0800 | [diff] [blame] | 210 | void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len, |
| 211 | const char *text_before, const char *text_after) |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 212 | { |
Behdad Esfahbod | 1172dc7 | 2013-01-07 16:46:37 -0600 | [diff] [blame] | 213 | hb_buffer_clear_contents (buffer); |
Behdad Esfahbod | 321f73c | 2012-11-13 15:12:24 -0800 | [diff] [blame] | 214 | if (text_before) { |
| 215 | unsigned int len = strlen (text_before); |
| 216 | hb_buffer_add_utf8 (buffer, text_before, len, len, 0); |
| 217 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 218 | hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len); |
Behdad Esfahbod | 321f73c | 2012-11-13 15:12:24 -0800 | [diff] [blame] | 219 | if (text_after) { |
| 220 | hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0); |
| 221 | } |
Behdad Esfahbod | d530024 | 2012-01-21 19:07:22 -0500 | [diff] [blame] | 222 | |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 223 | if (!utf8_clusters) { |
| 224 | /* Reset cluster values to refer to Unicode character index |
| 225 | * instead of UTF-8 index. */ |
| 226 | unsigned int num_glyphs = hb_buffer_get_length (buffer); |
| 227 | hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); |
| 228 | for (unsigned int i = 0; i < num_glyphs; i++) |
| 229 | { |
| 230 | info->cluster = i; |
| 231 | info++; |
| 232 | } |
Behdad Esfahbod | d530024 | 2012-01-21 19:07:22 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Behdad Esfahbod | 4f4b114 | 2011-09-08 16:49:02 -0400 | [diff] [blame] | 235 | setup_buffer (buffer); |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer) |
| 239 | { |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 240 | hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers); |
| 241 | if (normalize_glyphs) |
| 242 | hb_buffer_normalize_glyphs (buffer); |
| 243 | return res; |
Behdad Esfahbod | 4f4b114 | 2011-09-08 16:49:02 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Behdad Esfahbod | c87b317 | 2012-05-15 23:53:18 -0400 | [diff] [blame] | 246 | void shape_closure (const char *text, int text_len, |
| 247 | hb_font_t *font, hb_buffer_t *buffer, |
Behdad Esfahbod | ae62166 | 2012-06-02 12:21:19 -0400 | [diff] [blame] | 248 | hb_set_t *glyphs) |
| 249 | { |
Behdad Esfahbod | c87b317 | 2012-05-15 23:53:18 -0400 | [diff] [blame] | 250 | hb_buffer_reset (buffer); |
| 251 | hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len); |
| 252 | setup_buffer (buffer); |
| 253 | hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs); |
| 254 | } |
| 255 | |
Behdad Esfahbod | 407f80d | 2012-11-13 15:33:27 -0800 | [diff] [blame] | 256 | /* Buffer properties */ |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 257 | const char *direction; |
| 258 | const char *language; |
| 259 | const char *script; |
Behdad Esfahbod | 407f80d | 2012-11-13 15:33:27 -0800 | [diff] [blame] | 260 | |
| 261 | /* Buffer flags */ |
| 262 | hb_bool_t bot; |
| 263 | hb_bool_t eot; |
| 264 | hb_bool_t preserve_default_ignorables; |
| 265 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 266 | hb_feature_t *features; |
| 267 | unsigned int num_features; |
| 268 | char **shapers; |
Behdad Esfahbod | 95cefdf | 2012-04-16 18:08:20 -0400 | [diff] [blame] | 269 | hb_bool_t utf8_clusters; |
Behdad Esfahbod | 376d587 | 2015-07-22 16:51:12 +0100 | [diff] [blame^] | 270 | hb_buffer_cluster_level_t cluster_level; |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 271 | hb_bool_t normalize_glyphs; |
Behdad Esfahbod | 50067e2 | 2013-04-11 16:31:01 -0400 | [diff] [blame] | 272 | unsigned int num_iterations; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 273 | }; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 274 | |
Behdad Esfahbod | 109cb38 | 2011-09-08 16:00:04 -0400 | [diff] [blame] | 275 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 276 | struct font_options_t : option_group_t |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 277 | { |
Behdad Esfahbod | cd4eb96 | 2015-01-20 12:30:45 -0800 | [diff] [blame] | 278 | font_options_t (option_parser_t *parser, |
| 279 | int default_font_size_, |
| 280 | unsigned int subpixel_bits_) { |
| 281 | default_font_size = default_font_size_; |
| 282 | subpixel_bits = subpixel_bits_; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 283 | font_file = NULL; |
| 284 | face_index = 0; |
Behdad Esfahbod | cd4eb96 | 2015-01-20 12:30:45 -0800 | [diff] [blame] | 285 | font_size_x = font_size_y = default_font_size; |
Behdad Esfahbod | 8650def | 2014-07-05 15:50:18 -0400 | [diff] [blame] | 286 | font_funcs = NULL; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 287 | |
| 288 | font = NULL; |
| 289 | |
| 290 | add_options (parser); |
| 291 | } |
| 292 | ~font_options_t (void) { |
| 293 | hb_font_destroy (font); |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 296 | void add_options (option_parser_t *parser); |
| 297 | |
| 298 | hb_font_t *get_font (void) const; |
Behdad Esfahbod | bc4b07b | 2011-09-08 17:08:32 -0400 | [diff] [blame] | 299 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 300 | const char *font_file; |
| 301 | int face_index; |
Behdad Esfahbod | cd4eb96 | 2015-01-20 12:30:45 -0800 | [diff] [blame] | 302 | int default_font_size; |
| 303 | unsigned int subpixel_bits; |
| 304 | mutable double font_size_x; |
| 305 | mutable double font_size_y; |
Behdad Esfahbod | 8650def | 2014-07-05 15:50:18 -0400 | [diff] [blame] | 306 | const char *font_funcs; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 307 | |
| 308 | private: |
| 309 | mutable hb_font_t *font; |
| 310 | }; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 311 | |
| 312 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 313 | struct text_options_t : option_group_t |
| 314 | { |
| 315 | text_options_t (option_parser_t *parser) { |
Behdad Esfahbod | 321f73c | 2012-11-13 15:12:24 -0800 | [diff] [blame] | 316 | text_before = NULL; |
| 317 | text_after = NULL; |
| 318 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 319 | text = NULL; |
| 320 | text_file = NULL; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 321 | |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 322 | fp = NULL; |
| 323 | gs = NULL; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 324 | text_len = (unsigned int) -1; |
| 325 | |
| 326 | add_options (parser); |
| 327 | } |
| 328 | ~text_options_t (void) { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 329 | if (gs) |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 330 | g_string_free (gs, true); |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 331 | if (fp) |
| 332 | fclose (fp); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void add_options (option_parser_t *parser); |
| 336 | |
| 337 | void post_parse (GError **error G_GNUC_UNUSED) { |
| 338 | if (text && text_file) |
| 339 | g_set_error (error, |
| 340 | G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, |
Behdad Esfahbod | 30874b4 | 2012-05-12 15:54:27 +0200 | [diff] [blame] | 341 | "Only one of text and text-file can be set"); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 342 | |
| 343 | }; |
| 344 | |
| 345 | const char *get_line (unsigned int *len); |
| 346 | |
Behdad Esfahbod | 321f73c | 2012-11-13 15:12:24 -0800 | [diff] [blame] | 347 | const char *text_before; |
| 348 | const char *text_after; |
| 349 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 350 | const char *text; |
| 351 | const char *text_file; |
| 352 | |
| 353 | private: |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 354 | FILE *fp; |
| 355 | GString *gs; |
| 356 | unsigned int text_len; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 357 | }; |
| 358 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 359 | struct output_options_t : option_group_t |
| 360 | { |
Behdad Esfahbod | 9815a88 | 2012-12-21 16:46:53 -0500 | [diff] [blame] | 361 | output_options_t (option_parser_t *parser, |
Behdad Esfahbod | ea5e8a0 | 2014-03-19 15:38:02 -0700 | [diff] [blame] | 362 | const char **supported_formats_ = NULL) { |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 363 | output_file = NULL; |
| 364 | output_format = NULL; |
Behdad Esfahbod | 9815a88 | 2012-12-21 16:46:53 -0500 | [diff] [blame] | 365 | supported_formats = supported_formats_; |
Behdad Esfahbod | 6bad092 | 2012-12-21 16:01:52 -0500 | [diff] [blame] | 366 | explicit_output_format = false; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 367 | |
Behdad Esfahbod | f7e2ef7 | 2011-09-15 17:52:00 -0400 | [diff] [blame] | 368 | fp = NULL; |
| 369 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 370 | add_options (parser); |
| 371 | } |
Behdad Esfahbod | f7e2ef7 | 2011-09-15 17:52:00 -0400 | [diff] [blame] | 372 | ~output_options_t (void) { |
Behdad Esfahbod | 55aeb04 | 2011-09-16 02:08:36 -0400 | [diff] [blame] | 373 | if (fp) |
Behdad Esfahbod | f7e2ef7 | 2011-09-15 17:52:00 -0400 | [diff] [blame] | 374 | fclose (fp); |
| 375 | } |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 376 | |
| 377 | void add_options (option_parser_t *parser); |
| 378 | |
| 379 | void post_parse (GError **error G_GNUC_UNUSED) |
| 380 | { |
Behdad Esfahbod | 6bad092 | 2012-12-21 16:01:52 -0500 | [diff] [blame] | 381 | if (output_format) |
| 382 | explicit_output_format = true; |
| 383 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 384 | if (output_file && !output_format) { |
| 385 | output_format = strrchr (output_file, '.'); |
| 386 | if (output_format) |
| 387 | output_format++; /* skip the dot */ |
| 388 | } |
| 389 | |
Behdad Esfahbod | f7e2ef7 | 2011-09-15 17:52:00 -0400 | [diff] [blame] | 390 | if (output_file && 0 == strcmp (output_file, "-")) |
| 391 | output_file = NULL; /* STDOUT */ |
| 392 | } |
| 393 | |
Behdad Esfahbod | a75c1b1 | 2011-09-16 01:16:41 -0400 | [diff] [blame] | 394 | FILE *get_file_handle (void); |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 395 | |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 396 | const char *output_file; |
| 397 | const char *output_format; |
Behdad Esfahbod | ea5e8a0 | 2014-03-19 15:38:02 -0700 | [diff] [blame] | 398 | const char **supported_formats; |
Behdad Esfahbod | 6bad092 | 2012-12-21 16:01:52 -0500 | [diff] [blame] | 399 | bool explicit_output_format; |
Behdad Esfahbod | f7e2ef7 | 2011-09-15 17:52:00 -0400 | [diff] [blame] | 400 | |
| 401 | mutable FILE *fp; |
Behdad Esfahbod | b9b10ad | 2011-09-13 13:30:39 -0400 | [diff] [blame] | 402 | }; |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 403 | |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 404 | struct format_options_t : option_group_t |
| 405 | { |
| 406 | format_options_t (option_parser_t *parser) { |
| 407 | show_glyph_names = true; |
| 408 | show_positions = true; |
| 409 | show_clusters = true; |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 410 | show_text = false; |
| 411 | show_unicode = false; |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 412 | show_line_num = false; |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 413 | |
| 414 | add_options (parser); |
| 415 | } |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 416 | |
| 417 | void add_options (option_parser_t *parser); |
| 418 | |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 419 | void serialize_unicode (hb_buffer_t *buffer, |
| 420 | GString *gs); |
| 421 | void serialize_glyphs (hb_buffer_t *buffer, |
| 422 | hb_font_t *font, |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 423 | hb_buffer_serialize_format_t format, |
| 424 | hb_buffer_serialize_flags_t flags, |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 425 | GString *gs); |
| 426 | void serialize_line_no (unsigned int line_no, |
| 427 | GString *gs); |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 428 | void serialize_buffer_of_text (hb_buffer_t *buffer, |
| 429 | unsigned int line_no, |
| 430 | const char *text, |
| 431 | unsigned int text_len, |
| 432 | hb_font_t *font, |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 433 | GString *gs); |
| 434 | void serialize_message (unsigned int line_no, |
| 435 | const char *msg, |
| 436 | GString *gs); |
| 437 | void serialize_buffer_of_glyphs (hb_buffer_t *buffer, |
| 438 | unsigned int line_no, |
| 439 | const char *text, |
| 440 | unsigned int text_len, |
| 441 | hb_font_t *font, |
Behdad Esfahbod | f9edf16 | 2012-11-15 12:14:09 -0800 | [diff] [blame] | 442 | hb_buffer_serialize_format_t output_format, |
| 443 | hb_buffer_serialize_flags_t format_flags, |
Behdad Esfahbod | 5db0683 | 2012-06-02 12:13:08 -0400 | [diff] [blame] | 444 | GString *gs); |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 445 | |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 446 | |
Behdad Esfahbod | 088c1e2 | 2011-09-20 14:43:55 -0400 | [diff] [blame] | 447 | hb_bool_t show_glyph_names; |
| 448 | hb_bool_t show_positions; |
| 449 | hb_bool_t show_clusters; |
Behdad Esfahbod | cc4d981 | 2012-01-19 12:32:20 -0500 | [diff] [blame] | 450 | hb_bool_t show_text; |
| 451 | hb_bool_t show_unicode; |
Behdad Esfahbod | cdc673d | 2012-01-19 12:46:18 -0500 | [diff] [blame] | 452 | hb_bool_t show_line_num; |
Behdad Esfahbod | 8b8b190 | 2011-09-19 16:41:17 -0400 | [diff] [blame] | 453 | }; |
| 454 | |
Behdad Esfahbod | 3bb300e | 2011-08-11 11:54:31 +0200 | [diff] [blame] | 455 | |
| 456 | #endif |