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() */
47 #include <io.h> /* for _setmode() under Windows */
55 #include <glib/gprintf.h>
58 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
61 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
64 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN;
67 extern hb_bool_t debug;
71 virtual void add_options (struct option_parser_t *parser) = 0;
73 virtual void pre_parse (GError **error G_GNUC_UNUSED) {};
74 virtual void post_parse (GError **error G_GNUC_UNUSED) {};
78 struct option_parser_t
80 option_parser_t (const char *usage) {
81 memset (this, 0, sizeof (*this));
83 context = g_option_context_new (usage);
87 ~option_parser_t (void) {
88 g_option_context_free (context);
91 void add_main_options (void);
93 void add_group (GOptionEntry *entries,
95 const gchar *description,
96 const gchar *help_description,
97 option_group_t *option_group);
99 void parse (int *argc, char ***argv);
101 G_GNUC_NORETURN void usage (void) {
102 g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
106 const char *usage_str;
107 GOptionContext *context;
111 #define DEFAULT_MARGIN 16
112 #define DEFAULT_FORE "#000000"
113 #define DEFAULT_BACK "#FFFFFF"
114 #define DEFAULT_FONT_SIZE 256
116 struct view_options_t : option_group_t
118 view_options_t (option_parser_t *parser) {
123 margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
124 font_size = DEFAULT_FONT_SIZE;
126 add_options (parser);
129 void add_options (option_parser_t *parser);
142 struct shape_options_t : option_group_t
144 shape_options_t (option_parser_t *parser)
146 direction = language = script = NULL;
150 utf8_clusters = false;
151 normalize_glyphs = false;
153 add_options (parser);
155 ~shape_options_t (void)
158 g_strfreev (shapers);
161 void add_options (option_parser_t *parser);
163 void setup_buffer (hb_buffer_t *buffer)
165 hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
166 hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
167 hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
170 void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len)
172 hb_buffer_reset (buffer);
173 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
175 if (!utf8_clusters) {
176 /* Reset cluster values to refer to Unicode character index
177 * instead of UTF-8 index. */
178 unsigned int num_glyphs = hb_buffer_get_length (buffer);
179 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
180 for (unsigned int i = 0; i < num_glyphs; i++)
187 setup_buffer (buffer);
190 hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
192 hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
193 if (normalize_glyphs)
194 hb_buffer_normalize_glyphs (buffer);
198 void shape_closure (const char *text, int text_len,
199 hb_font_t *font, hb_buffer_t *buffer,
202 hb_buffer_reset (buffer);
203 hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
204 setup_buffer (buffer);
205 hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
208 const char *direction;
209 const char *language;
211 hb_feature_t *features;
212 unsigned int num_features;
214 hb_bool_t utf8_clusters;
215 hb_bool_t normalize_glyphs;
219 struct font_options_t : option_group_t
221 font_options_t (option_parser_t *parser) {
227 add_options (parser);
229 ~font_options_t (void) {
230 hb_font_destroy (font);
233 void add_options (option_parser_t *parser);
235 hb_font_t *get_font (void) const;
237 const char *font_file;
241 mutable hb_font_t *font;
245 struct text_options_t : option_group_t
247 text_options_t (option_parser_t *parser) {
253 text_len = (unsigned int) -1;
255 add_options (parser);
257 ~text_options_t (void) {
259 g_string_free (gs, true);
264 void add_options (option_parser_t *parser);
266 void post_parse (GError **error G_GNUC_UNUSED) {
267 if (text && text_file)
269 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
270 "Only one of text and text-file can be set");
274 const char *get_line (unsigned int *len);
277 const char *text_file;
282 unsigned int text_len;
285 struct output_options_t : option_group_t
287 output_options_t (option_parser_t *parser) {
289 output_format = NULL;
293 add_options (parser);
295 ~output_options_t (void) {
300 void add_options (option_parser_t *parser);
302 void post_parse (GError **error G_GNUC_UNUSED)
304 if (output_file && !output_format) {
305 output_format = strrchr (output_file, '.');
307 output_format++; /* skip the dot */
310 if (output_file && 0 == strcmp (output_file, "-"))
311 output_file = NULL; /* STDOUT */
314 FILE *get_file_handle (void);
316 const char *output_file;
317 const char *output_format;
322 struct format_options_t : option_group_t
324 format_options_t (option_parser_t *parser) {
325 show_glyph_names = true;
326 show_positions = true;
327 show_clusters = true;
329 show_unicode = false;
330 show_line_num = false;
332 add_options (parser);
335 void add_options (option_parser_t *parser);
337 void serialize_unicode (hb_buffer_t *buffer,
339 void serialize_glyphs (hb_buffer_t *buffer,
341 hb_bool_t utf8_clusters,
343 void serialize_line_no (unsigned int line_no,
345 void serialize_buffer_of_text (hb_buffer_t *buffer,
346 unsigned int line_no,
348 unsigned int text_len,
350 hb_bool_t utf8_clusters,
352 void serialize_message (unsigned int line_no,
355 void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
356 unsigned int line_no,
358 unsigned int text_len,
360 hb_bool_t utf8_clusters,
364 hb_bool_t show_glyph_names;
365 hb_bool_t show_positions;
366 hb_bool_t show_clusters;
368 hb_bool_t show_unicode;
369 hb_bool_t show_line_num;