75c3793b5abb92e0ea41994bfea7b02f7a5e4188
[platform/upstream/harfbuzz.git] / util / hb-shape.cc
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 {
33   output_buffer_t (option_parser_t *parser)
34                   : options (parser, hb_buffer_serialize_list_formats ()),
35                     format (parser),
36                     gs (NULL),
37                     line_no (0),
38                     font (NULL),
39                     output_format (HB_BUFFER_SERIALIZE_FORMAT_INVALID),
40                     format_flags (HB_BUFFER_SERIALIZE_FLAG_DEFAULT) {}
41
42   void init (const font_options_t *font_opts)
43   {
44     options.get_file_handle ();
45     gs = g_string_new (NULL);
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_extents)
76       flags |= HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS;
77     format_flags = (hb_buffer_serialize_flags_t) flags;
78   }
79   void new_line (void)
80   {
81     line_no++;
82   }
83   void consume_text (hb_buffer_t  *buffer,
84                      const char   *text,
85                      unsigned int  text_len,
86                      hb_bool_t     utf8_clusters)
87   {
88     g_string_set_size (gs, 0);
89     format.serialize_buffer_of_text (buffer, line_no, text, text_len, font, gs);
90     fprintf (options.fp, "%s", gs->str);
91   }
92   void shape_failed (hb_buffer_t  *buffer,
93                      const char   *text,
94                      unsigned int  text_len,
95                      hb_bool_t     utf8_clusters)
96   {
97     g_string_set_size (gs, 0);
98     format.serialize_message (line_no, "msg: all shapers failed", gs);
99     fprintf (options.fp, "%s", gs->str);
100   }
101   void consume_glyphs (hb_buffer_t  *buffer,
102                        const char   *text,
103                        unsigned int  text_len,
104                        hb_bool_t     utf8_clusters)
105   {
106     g_string_set_size (gs, 0);
107     format.serialize_buffer_of_glyphs (buffer, line_no, text, text_len, font,
108                                        output_format, format_flags, gs);
109     fprintf (options.fp, "%s", gs->str);
110   }
111   void finish (const font_options_t *font_opts)
112   {
113     hb_font_destroy (font);
114     g_string_free (gs, true);
115     gs = NULL;
116     font = NULL;
117   }
118
119   protected:
120   output_options_t options;
121   format_options_t format;
122
123   GString *gs;
124   unsigned int line_no;
125   hb_font_t *font;
126   hb_buffer_serialize_format_t output_format;
127   hb_buffer_serialize_flags_t format_flags;
128 };
129
130 int
131 main (int argc, char **argv)
132 {
133   main_font_text_t<shape_consumer_t<output_buffer_t>, FONT_SIZE_UPEM, 0> driver;
134   return driver.main (argc, argv);
135 }