2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
44 #include <unistd.h> /* for isatty() */
46 #if defined(_WIN32) || defined(__CYGWIN__)
47 #include <io.h> /* for setmode() under Windows */
55 #include <glib/gprintf.h>
57 #if !GLIB_CHECK_VERSION (2, 22, 0)
58 # define g_mapped_file_unref g_mapped_file_free
62 /* A few macros copied from hb-private.hh. */
65 #define HB_UNUSED __attribute__((unused))
71 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
74 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
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])))
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))
87 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
90 extern hb_bool_t debug;
94 virtual void add_options (struct option_parser_t *parser) = 0;
96 virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
97 virtual void post_parse (GError **error G_GNUC_UNUSED) {};
101 struct option_parser_t
103 option_parser_t (const char *usage) {
104 memset (this, 0, sizeof (*this));
106 context = g_option_context_new (usage);
107 to_free = g_ptr_array_new ();
111 ~option_parser_t (void) {
112 g_option_context_free (context);
113 g_ptr_array_foreach (to_free, (GFunc) g_free, NULL);
114 g_ptr_array_free (to_free, TRUE);
117 void add_main_options (void);
119 void add_group (GOptionEntry *entries,
121 const gchar *description,
122 const gchar *help_description,
123 option_group_t *option_group);
125 void free_later (char *p) {
126 g_ptr_array_add (to_free, p);
129 void parse (int *argc, char ***argv);
131 G_GNUC_NORETURN void usage (void) {
132 g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
137 const char *usage_str;
138 GOptionContext *context;
143 #define DEFAULT_MARGIN 16
144 #define DEFAULT_FORE "#000000"
145 #define DEFAULT_BACK "#FFFFFF"
146 #define FONT_SIZE_UPEM 0x7FFFFFFF
147 #define FONT_SIZE_NONE 0
149 struct view_options_t : option_group_t
151 view_options_t (option_parser_t *parser) {
156 margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
158 add_options (parser);
161 void add_options (option_parser_t *parser);
173 struct shape_options_t : option_group_t
175 shape_options_t (option_parser_t *parser)
177 direction = language = script = NULL;
178 bot = eot = preserve_default_ignorables = false;
182 utf8_clusters = false;
183 normalize_glyphs = false;
186 add_options (parser);
188 ~shape_options_t (void)
191 g_strfreev (shapers);
194 void add_options (option_parser_t *parser);
196 void setup_buffer (hb_buffer_t *buffer)
198 hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
199 hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
200 hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
201 hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_DEFAULT |
202 (bot ? HB_BUFFER_FLAG_BOT : 0) |
203 (eot ? HB_BUFFER_FLAG_EOT : 0) |
204 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0)));
205 hb_buffer_guess_segment_properties (buffer);
208 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
209 const char *text_before, const char *text_after)
211 hb_buffer_clear_contents (buffer);
213 unsigned int len = strlen (text_before);
214 hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
216 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
218 hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
221 if (!utf8_clusters) {
222 /* Reset cluster values to refer to Unicode character index
223 * instead of UTF-8 index. */
224 unsigned int num_glyphs = hb_buffer_get_length (buffer);
225 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
226 for (unsigned int i = 0; i < num_glyphs; i++)
233 setup_buffer (buffer);
236 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
238 hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
239 if (normalize_glyphs)
240 hb_buffer_normalize_glyphs (buffer);
244 void shape_closure (const char *text, int text_len,
245 hb_font_t *font, hb_buffer_t *buffer,
248 hb_buffer_reset (buffer);
249 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
250 setup_buffer (buffer);
251 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
254 /* Buffer properties */
255 const char *direction;
256 const char *language;
262 hb_bool_t preserve_default_ignorables;
264 hb_feature_t *features;
265 unsigned int num_features;
267 hb_bool_t utf8_clusters;
268 hb_bool_t normalize_glyphs;
269 unsigned int num_iterations;
273 struct font_options_t : option_group_t
275 font_options_t (option_parser_t *parser,
276 int default_font_size_,
277 unsigned int subpixel_bits_) {
278 default_font_size = default_font_size_;
279 subpixel_bits = subpixel_bits_;
282 font_size_x = font_size_y = default_font_size;
287 add_options (parser);
289 ~font_options_t (void) {
290 hb_font_destroy (font);
293 void add_options (option_parser_t *parser);
295 hb_font_t *get_font (void) const;
297 const char *font_file;
299 int default_font_size;
300 unsigned int subpixel_bits;
301 mutable double font_size_x;
302 mutable double font_size_y;
303 const char *font_funcs;
306 mutable hb_font_t *font;
310 struct text_options_t : option_group_t
312 text_options_t (option_parser_t *parser) {
321 text_len = (unsigned int) -1;
323 add_options (parser);
325 ~text_options_t (void) {
327 g_string_free (gs, true);
332 void add_options (option_parser_t *parser);
334 void post_parse (GError **error G_GNUC_UNUSED) {
335 if (text && text_file)
337 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
338 "Only one of text and text-file can be set");
342 const char *get_line (unsigned int *len);
344 const char *text_before;
345 const char *text_after;
348 const char *text_file;
353 unsigned int text_len;
356 struct output_options_t : option_group_t
358 output_options_t (option_parser_t *parser,
359 const char **supported_formats_ = NULL) {
361 output_format = NULL;
362 supported_formats = supported_formats_;
363 explicit_output_format = false;
367 add_options (parser);
369 ~output_options_t (void) {
374 void add_options (option_parser_t *parser);
376 void post_parse (GError **error G_GNUC_UNUSED)
379 explicit_output_format = true;
381 if (output_file && !output_format) {
382 output_format = strrchr (output_file, '.');
384 output_format++; /* skip the dot */
387 if (output_file && 0 == strcmp (output_file, "-"))
388 output_file = NULL; /* STDOUT */
391 FILE *get_file_handle (void);
393 const char *output_file;
394 const char *output_format;
395 const char **supported_formats;
396 bool explicit_output_format;
401 struct format_options_t : option_group_t
403 format_options_t (option_parser_t *parser) {
404 show_glyph_names = true;
405 show_positions = true;
406 show_clusters = true;
408 show_unicode = false;
409 show_line_num = false;
411 add_options (parser);
414 void add_options (option_parser_t *parser);
416 void serialize_unicode (hb_buffer_t *buffer,
418 void serialize_glyphs (hb_buffer_t *buffer,
420 hb_buffer_serialize_format_t format,
421 hb_buffer_serialize_flags_t flags,
423 void serialize_line_no (unsigned int line_no,
425 void serialize_buffer_of_text (hb_buffer_t *buffer,
426 unsigned int line_no,
428 unsigned int text_len,
431 void serialize_message (unsigned int line_no,
434 void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
435 unsigned int line_no,
437 unsigned int text_len,
439 hb_buffer_serialize_format_t output_format,
440 hb_buffer_serialize_flags_t format_flags,
444 hb_bool_t show_glyph_names;
445 hb_bool_t show_positions;
446 hb_bool_t show_clusters;
448 hb_bool_t show_unicode;
449 hb_bool_t show_line_num;