1 /*
2 * Copyright © 2010 Behdad Esfahbod
3 * Copyright © 2011,2012 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Google Author(s): Behdad Esfahbod
26 */
27
28 #include "main-font-text.hh"
29 #include "shape-consumer.hh"
30
31 struct output_buffer_t
32 {
output_buffer_toutput_buffer_t33 output_buffer_t (option_parser_t *parser)
34 : options (parser, hb_buffer_serialize_list_formats ()),
35 format (parser),
36 gs (nullptr),
37 line_no (0),
38 font (nullptr),
39 output_format (HB_BUFFER_SERIALIZE_FORMAT_INVALID),
40 format_flags (HB_BUFFER_SERIALIZE_FLAG_DEFAULT) {}
41
initoutput_buffer_t42 void init (hb_buffer_t *buffer, const font_options_t *font_opts)
43 {
44 options.get_file_handle ();
45 gs = g_string_new (nullptr);
46 line_no = 0;
47 font = hb_font_reference (font_opts->get_font ());
48
49 if (!options.output_format)
50 output_format = HB_BUFFER_SERIALIZE_FORMAT_TEXT;
51 else
52 output_format = hb_buffer_serialize_format_from_string (options.output_format, -1);
53 /* An empty "output_format" parameter basically skips output generating.
54 * Useful for benchmarking. */
55 if ((!options.output_format || *options.output_format) &&
56 !hb_buffer_serialize_format_to_string (output_format))
57 {
58 if (options.explicit_output_format)
59 fail (false, "Unknown output format `%s'; supported formats are: %s",
60 options.output_format,
61 g_strjoinv ("/", const_cast<char**> (options.supported_formats)));
62 else
63 /* Just default to TEXT if not explicitly requested and the
64 * file extension is not recognized. */
65 output_format = HB_BUFFER_SERIALIZE_FORMAT_TEXT;
66 }
67
68 unsigned int flags = HB_BUFFER_SERIALIZE_FLAG_DEFAULT;
69 if (!format.show_glyph_names)
70 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES;
71 if (!format.show_clusters)
72 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS;
73 if (!format.show_positions)
74 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS;
75 if (!format.show_advances)
76 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES;
77 if (format.show_extents)
78 flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS;
79 if (format.show_flags)
80 flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_FLAGS;
81 format_flags = (hb_buffer_serialize_flags_t) flags;
82
83 if (format.trace)
84 hb_buffer_set_message_func (buffer, message_func, this, nullptr);
85 }
new_lineoutput_buffer_t86 void new_line () { line_no++; }
consume_textoutput_buffer_t87 void consume_text (hb_buffer_t *buffer,
88 const char *text,
89 unsigned int text_len,
90 hb_bool_t utf8_clusters)
91 {
92 g_string_set_size (gs, 0);
93 format.serialize_buffer_of_text (buffer, line_no, text, text_len, font, gs);
94 fprintf (options.fp, "%s", gs->str);
95 }
erroroutput_buffer_t96 void error (const char *message)
97 {
98 g_string_set_size (gs, 0);
99 format.serialize_message (line_no, "error", message, gs);
100 fprintf (options.fp, "%s", gs->str);
101 }
consume_glyphsoutput_buffer_t102 void consume_glyphs (hb_buffer_t *buffer,
103 const char *text,
104 unsigned int text_len,
105 hb_bool_t utf8_clusters)
106 {
107 g_string_set_size (gs, 0);
108 format.serialize_buffer_of_glyphs (buffer, line_no, text, text_len, font,
109 output_format, format_flags, gs);
110 fprintf (options.fp, "%s", gs->str);
111 }
finishoutput_buffer_t112 void finish (hb_buffer_t *buffer, const font_options_t *font_opts)
113 {
114 hb_buffer_set_message_func (buffer, nullptr, nullptr, nullptr);
115 hb_font_destroy (font);
116 g_string_free (gs, true);
117 gs = nullptr;
118 font = nullptr;
119 }
120
121 static hb_bool_t
message_funcoutput_buffer_t122 message_func (hb_buffer_t *buffer,
123 hb_font_t *font,
124 const char *message,
125 void *user_data)
126 {
127 output_buffer_t *that = (output_buffer_t *) user_data;
128 that->trace (buffer, font, message);
129 return true;
130 }
131
132 void
traceoutput_buffer_t133 trace (hb_buffer_t *buffer,
134 hb_font_t *font,
135 const char *message)
136 {
137 g_string_set_size (gs, 0);
138 format.serialize_line_no (line_no, gs);
139 g_string_append_printf (gs, "trace: %s buffer: ", message);
140 format.serialize (buffer, font, output_format, format_flags, gs);
141 g_string_append_c (gs, '\n');
142 fprintf (options.fp, "%s", gs->str);
143 }
144
145
146 protected:
147 output_options_t options;
148 format_options_t format;
149
150 GString *gs;
151 unsigned int line_no;
152 hb_font_t *font;
153 hb_buffer_serialize_format_t output_format;
154 hb_buffer_serialize_flags_t format_flags;
155 };
156
157 int
main(int argc,char ** argv)158 main (int argc, char **argv)
159 {
160 if (argc == 2 && !strcmp (argv[1], "--batch"))
161 {
162 unsigned int ret = 0;
163 char buf[4092];
164 while (fgets (buf, sizeof (buf), stdin))
165 {
166 size_t l = strlen (buf);
167 if (l && buf[l - 1] == '\n') buf[l - 1] = '\0';
168 main_font_text_t<shape_consumer_t<output_buffer_t>, FONT_SIZE_UPEM, 0> driver;
169 char *args[32];
170 argc = 0;
171 char *p = buf, *e;
172 args[argc++] = p;
173 unsigned start_offset = 0;
174 while ((e = strchr (p + start_offset, ':')) && argc < (int) ARRAY_LENGTH (args))
175 {
176 *e++ = '\0';
177 while (*e == ':')
178 e++;
179 args[argc++] = p = e;
180 /* Skip 2 first bytes on first argument if is Windows path, "C:\..." */
181 start_offset = argc == 2 && p[0] != '\0' && p[0] != ':' && p[1] == ':' && (p[2] == '\\' || p[2] == '/') ? 2 : 0;
182 }
183 ret |= driver.main (argc, args);
184 fflush (stdout);
185
186 if (ret)
187 break;
188 }
189 return ret;
190 }
191 main_font_text_t<shape_consumer_t<output_buffer_t>, FONT_SIZE_UPEM, 0> driver;
192 return driver.main (argc, argv);
193 }
194