blob: 89440671fb47cb39798fef0262d74671c9b508f5 [file] [log] [blame]
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +02001/*
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 Esfahbod3bb300e2011-08-11 11:54:31 +020027#ifndef OPTIONS_HH
28#define OPTIONS_HH
29
30
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040031#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <stdlib.h>
36#include <stddef.h>
37#include <string.h>
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040038#include <stdio.h>
39#include <math.h>
40#include <locale.h>
41#include <errno.h>
42#include <fcntl.h>
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020043#ifdef HAVE_UNISTD_H
44#include <unistd.h> /* for isatty() */
45#endif
Behdad Esfahbode2aab4b2013-02-12 15:35:32 -050046#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahbodbc764492013-01-31 18:18:05 -050047#include <io.h> /* for setmode() under Windows */
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040048#endif
49
50#include <hb.h>
Behdad Esfahbodc87b3172012-05-15 23:53:18 -040051#ifdef HAVE_OT
52#include <hb-ot.h>
53#endif
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040054#include <glib.h>
55#include <glib/gprintf.h>
56
Behdad Esfahbod69b84a82012-04-12 15:50:40 -040057#undef MIN
58template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
59
60#undef MAX
61template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
62
63
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -040064void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN;
65
66
Behdad Esfahbod088c1e22011-09-20 14:43:55 -040067extern hb_bool_t debug;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -040068
69struct option_group_t
70{
71 virtual void add_options (struct option_parser_t *parser) = 0;
72
73 virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
74 virtual void post_parse (GError **error G_GNUC_UNUSED) {};
75};
76
77
78struct option_parser_t
79{
80 option_parser_t (const char *usage) {
81 memset (this, 0, sizeof (*this));
82 usage_str = usage;
83 context = g_option_context_new (usage);
84
85 add_main_options ();
86 }
87 ~option_parser_t (void) {
88 g_option_context_free (context);
89 }
90
91 void add_main_options (void);
92
93 void add_group (GOptionEntry *entries,
94 const gchar *name,
95 const gchar *description,
96 const gchar *help_description,
97 option_group_t *option_group);
98
99 void parse (int *argc, char ***argv);
100
101 G_GNUC_NORETURN void usage (void) {
102 g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
103 exit (1);
104 }
105
106 const char *usage_str;
107 GOptionContext *context;
108};
109
110
Behdad Esfahbod912c5ff2012-05-13 12:51:02 +0200111#define DEFAULT_MARGIN 16
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400112#define DEFAULT_FORE "#000000"
113#define DEFAULT_BACK "#FFFFFF"
Behdad Esfahbod20979512012-05-12 15:41:48 +0200114#define DEFAULT_FONT_SIZE 256
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400115
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400116struct view_options_t : option_group_t
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200117{
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400118 view_options_t (option_parser_t *parser) {
119 annotate = false;
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400120 fore = DEFAULT_FORE;
121 back = DEFAULT_BACK;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400122 line_space = 0;
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400123 margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
Behdad Esfahbod11e51992011-09-19 09:58:55 -0400124 font_size = DEFAULT_FONT_SIZE;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400125
126 add_options (parser);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200127 }
128
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400129 void add_options (option_parser_t *parser);
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400130
Behdad Esfahbod088c1e22011-09-20 14:43:55 -0400131 hb_bool_t annotate;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200132 const char *fore;
133 const char *back;
134 double line_space;
135 struct margin_t {
136 double t, r, b, l;
137 } margin;
Behdad Esfahbod11e51992011-09-19 09:58:55 -0400138 double font_size;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400139};
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200140
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400141
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400142struct shape_options_t : option_group_t
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200143{
Behdad Esfahbodae621662012-06-02 12:21:19 -0400144 shape_options_t (option_parser_t *parser)
145 {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400146 direction = language = script = NULL;
Behdad Esfahbod407f80d2012-11-13 15:33:27 -0800147 bot = eot = preserve_default_ignorables = false;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400148 features = NULL;
149 num_features = 0;
150 shapers = NULL;
Behdad Esfahbod95cefdf2012-04-16 18:08:20 -0400151 utf8_clusters = false;
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400152 normalize_glyphs = false;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400153
154 add_options (parser);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200155 }
Behdad Esfahbodae621662012-06-02 12:21:19 -0400156 ~shape_options_t (void)
157 {
Behdad Esfahbod90e312c2011-09-08 16:42:37 -0400158 free (features);
Behdad Esfahbodade74592012-08-06 19:42:47 -0700159 g_strfreev (shapers);
Behdad Esfahbod90e312c2011-09-08 16:42:37 -0400160 }
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200161
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400162 void add_options (option_parser_t *parser);
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400163
Behdad Esfahbodae621662012-06-02 12:21:19 -0400164 void setup_buffer (hb_buffer_t *buffer)
165 {
Behdad Esfahbod516857e2011-09-08 16:50:24 -0400166 hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
167 hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
168 hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
Behdad Esfahbod407f80d2012-11-13 15:33:27 -0800169 hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAGS_DEFAULT |
170 (bot ? HB_BUFFER_FLAG_BOT : 0) |
171 (eot ? HB_BUFFER_FLAG_EOT : 0) |
172 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0)));
Behdad Esfahbod4f4b1142011-09-08 16:49:02 -0400173 }
174
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800175 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
176 const char *text_before, const char *text_after)
Behdad Esfahbodae621662012-06-02 12:21:19 -0400177 {
Behdad Esfahbod1172dc72013-01-07 16:46:37 -0600178 hb_buffer_clear_contents (buffer);
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800179 if (text_before) {
180 unsigned int len = strlen (text_before);
181 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
182 }
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400183 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800184 if (text_after) {
185 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
186 }
Behdad Esfahbodd5300242012-01-21 19:07:22 -0500187
Behdad Esfahbod95cefdf2012-04-16 18:08:20 -0400188 if (!utf8_clusters) {
189 /* Reset cluster values to refer to Unicode character index
190 * instead of UTF-8 index. */
191 unsigned int num_glyphs = hb_buffer_get_length (buffer);
192 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
193 for (unsigned int i = 0; i < num_glyphs; i++)
194 {
195 info->cluster = i;
196 info++;
197 }
Behdad Esfahbodd5300242012-01-21 19:07:22 -0500198 }
199
Behdad Esfahbod4f4b1142011-09-08 16:49:02 -0400200 setup_buffer (buffer);
Behdad Esfahbodae621662012-06-02 12:21:19 -0400201 }
202
203 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
204 {
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400205 hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
206 if (normalize_glyphs)
207 hb_buffer_normalize_glyphs (buffer);
208 return res;
Behdad Esfahbod4f4b1142011-09-08 16:49:02 -0400209 }
210
Behdad Esfahbodc87b3172012-05-15 23:53:18 -0400211 void shape_closure (const char *text, int text_len,
212 hb_font_t *font, hb_buffer_t *buffer,
Behdad Esfahbodae621662012-06-02 12:21:19 -0400213 hb_set_t *glyphs)
214 {
Behdad Esfahbodc87b3172012-05-15 23:53:18 -0400215 hb_buffer_reset (buffer);
216 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
217 setup_buffer (buffer);
218 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
219 }
220
Behdad Esfahbod407f80d2012-11-13 15:33:27 -0800221 /* Buffer properties */
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200222 const char *direction;
223 const char *language;
224 const char *script;
Behdad Esfahbod407f80d2012-11-13 15:33:27 -0800225
226 /* Buffer flags */
227 hb_bool_t bot;
228 hb_bool_t eot;
229 hb_bool_t preserve_default_ignorables;
230
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200231 hb_feature_t *features;
232 unsigned int num_features;
233 char **shapers;
Behdad Esfahbod95cefdf2012-04-16 18:08:20 -0400234 hb_bool_t utf8_clusters;
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400235 hb_bool_t normalize_glyphs;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400236};
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200237
Behdad Esfahbod109cb382011-09-08 16:00:04 -0400238
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400239struct font_options_t : option_group_t
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200240{
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400241 font_options_t (option_parser_t *parser) {
242 font_file = NULL;
243 face_index = 0;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400244
245 font = NULL;
246
247 add_options (parser);
248 }
249 ~font_options_t (void) {
250 hb_font_destroy (font);
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200251 }
252
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400253 void add_options (option_parser_t *parser);
254
255 hb_font_t *get_font (void) const;
Behdad Esfahbodbc4b07b2011-09-08 17:08:32 -0400256
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200257 const char *font_file;
258 int face_index;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400259
260 private:
261 mutable hb_font_t *font;
262};
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200263
264
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400265struct text_options_t : option_group_t
266{
267 text_options_t (option_parser_t *parser) {
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800268 text_before = NULL;
269 text_after = NULL;
270
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400271 text = NULL;
272 text_file = NULL;
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200273
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400274 fp = NULL;
275 gs = NULL;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400276 text_len = (unsigned int) -1;
277
278 add_options (parser);
279 }
280 ~text_options_t (void) {
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400281 if (gs)
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400282 g_string_free (gs, true);
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400283 if (fp)
284 fclose (fp);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400285 }
286
287 void add_options (option_parser_t *parser);
288
289 void post_parse (GError **error G_GNUC_UNUSED) {
290 if (text && text_file)
291 g_set_error (error,
292 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
Behdad Esfahbod30874b42012-05-12 15:54:27 +0200293 "Only one of text and text-file can be set");
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400294
295 };
296
297 const char *get_line (unsigned int *len);
298
Behdad Esfahbod321f73c2012-11-13 15:12:24 -0800299 const char *text_before;
300 const char *text_after;
301
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400302 const char *text;
303 const char *text_file;
304
305 private:
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400306 FILE *fp;
307 GString *gs;
308 unsigned int text_len;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400309};
310
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400311struct output_options_t : option_group_t
312{
Behdad Esfahbod9815a882012-12-21 16:46:53 -0500313 output_options_t (option_parser_t *parser,
314 const char *supported_formats_ = NULL) {
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400315 output_file = NULL;
316 output_format = NULL;
Behdad Esfahbod9815a882012-12-21 16:46:53 -0500317 supported_formats = supported_formats_;
Behdad Esfahbod6bad0922012-12-21 16:01:52 -0500318 explicit_output_format = false;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400319
Behdad Esfahbodf7e2ef72011-09-15 17:52:00 -0400320 fp = NULL;
321
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400322 add_options (parser);
323 }
Behdad Esfahbodf7e2ef72011-09-15 17:52:00 -0400324 ~output_options_t (void) {
Behdad Esfahbod55aeb042011-09-16 02:08:36 -0400325 if (fp)
Behdad Esfahbodf7e2ef72011-09-15 17:52:00 -0400326 fclose (fp);
327 }
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400328
329 void add_options (option_parser_t *parser);
330
331 void post_parse (GError **error G_GNUC_UNUSED)
332 {
Behdad Esfahbod6bad0922012-12-21 16:01:52 -0500333 if (output_format)
334 explicit_output_format = true;
335
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400336 if (output_file && !output_format) {
337 output_format = strrchr (output_file, '.');
338 if (output_format)
339 output_format++; /* skip the dot */
340 }
341
Behdad Esfahbodf7e2ef72011-09-15 17:52:00 -0400342 if (output_file && 0 == strcmp (output_file, "-"))
343 output_file = NULL; /* STDOUT */
344 }
345
Behdad Esfahboda75c1b12011-09-16 01:16:41 -0400346 FILE *get_file_handle (void);
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400347
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400348 const char *output_file;
349 const char *output_format;
Behdad Esfahbod9815a882012-12-21 16:46:53 -0500350 const char *supported_formats;
Behdad Esfahbod6bad0922012-12-21 16:01:52 -0500351 bool explicit_output_format;
Behdad Esfahbodf7e2ef72011-09-15 17:52:00 -0400352
353 mutable FILE *fp;
Behdad Esfahbodb9b10ad2011-09-13 13:30:39 -0400354};
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200355
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400356struct format_options_t : option_group_t
357{
358 format_options_t (option_parser_t *parser) {
359 show_glyph_names = true;
360 show_positions = true;
361 show_clusters = true;
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500362 show_text = false;
363 show_unicode = false;
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500364 show_line_num = false;
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400365
366 add_options (parser);
367 }
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400368
369 void add_options (option_parser_t *parser);
370
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500371 void serialize_unicode (hb_buffer_t *buffer,
372 GString *gs);
373 void serialize_glyphs (hb_buffer_t *buffer,
374 hb_font_t *font,
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800375 hb_buffer_serialize_format_t format,
376 hb_buffer_serialize_flags_t flags,
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500377 GString *gs);
378 void serialize_line_no (unsigned int line_no,
379 GString *gs);
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400380 void serialize_buffer_of_text (hb_buffer_t *buffer,
381 unsigned int line_no,
382 const char *text,
383 unsigned int text_len,
384 hb_font_t *font,
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400385 GString *gs);
386 void serialize_message (unsigned int line_no,
387 const char *msg,
388 GString *gs);
389 void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
390 unsigned int line_no,
391 const char *text,
392 unsigned int text_len,
393 hb_font_t *font,
Behdad Esfahbodf9edf162012-11-15 12:14:09 -0800394 hb_buffer_serialize_format_t output_format,
395 hb_buffer_serialize_flags_t format_flags,
Behdad Esfahbod5db06832012-06-02 12:13:08 -0400396 GString *gs);
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500397
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400398
Behdad Esfahbod088c1e22011-09-20 14:43:55 -0400399 hb_bool_t show_glyph_names;
400 hb_bool_t show_positions;
401 hb_bool_t show_clusters;
Behdad Esfahbodcc4d9812012-01-19 12:32:20 -0500402 hb_bool_t show_text;
403 hb_bool_t show_unicode;
Behdad Esfahbodcdc673d2012-01-19 12:46:18 -0500404 hb_bool_t show_line_num;
Behdad Esfahbod8b8b1902011-09-19 16:41:17 -0400405};
406
Behdad Esfahbod3bb300e2011-08-11 11:54:31 +0200407
408#endif