13bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod/*
28b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod * Copyright © 2010  Behdad Esfahbod
345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * Copyright © 2011,2012  Google, Inc.
43bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
53bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
63bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
73bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Permission is hereby granted, without written agreement and without
83bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
93bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * software and its documentation for any purpose, provided that the
103bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
113bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * all copies of this software.
123bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
133bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
143bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
153bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DAMAGE.
183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
193bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
213bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
223bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod *
253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Google Author(s): Behdad Esfahbod
263bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod */
273bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
2845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#include "main-font-text.hh"
2945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#include "shape-consumer.hh"
303bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
315db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodstruct output_buffer_t
328b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
338b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  output_buffer_t (option_parser_t *parser)
34ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod		  : options (parser, hb_buffer_serialize_list_formats ()),
357235f33f9e5e031622a00a84f4b2e98f16803579Behdad Esfahbod		    format (parser),
367235f33f9e5e031622a00a84f4b2e98f16803579Behdad Esfahbod		    gs (NULL),
377235f33f9e5e031622a00a84f4b2e98f16803579Behdad Esfahbod		    line_no (0),
387235f33f9e5e031622a00a84f4b2e98f16803579Behdad Esfahbod		    font (NULL) {}
393bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
4045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  void init (const font_options_t *font_opts)
4145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
425db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    options.get_file_handle ();
4345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    gs = g_string_new (NULL);
4445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    line_no = 0;
4545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    font = hb_font_reference (font_opts->get_font ());
46f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod
47f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!options.output_format)
48f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      output_format = HB_BUFFER_SERIALIZE_FORMAT_TEXT;
49f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    else
50f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      output_format = hb_buffer_serialize_format_from_string (options.output_format, -1);
519109f1e944dd4bd4ae8057b75c8a31e9d31797aaBehdad Esfahbod    /* An empty "output_format" parameter basically skips output generating.
529109f1e944dd4bd4ae8057b75c8a31e9d31797aaBehdad Esfahbod     * Useful for benchmarking. */
530afedaa96c81b63774a4a0ef9b4cb4995d24ec9eBehdad Esfahbod    if ((!options.output_format || *options.output_format) &&
549109f1e944dd4bd4ae8057b75c8a31e9d31797aaBehdad Esfahbod	!hb_buffer_serialize_format_to_string (output_format))
55a4bef84e375b090c1bc7e4221b8e0ee435dfcbbaBehdad Esfahbod    {
566bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod      if (options.explicit_output_format)
576bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod	fail (false, "Unknown output format `%s'; supported formats are: %s",
58ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod	      options.output_format,
59ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod	      g_strjoinv ("/", const_cast<char**> (options.supported_formats)));
606bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod      else
616bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod	/* Just default to TEXT if not explicitly requested and the
626bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod	 * file extension is not recognized. */
636bad092aa87bc3e6bbf0b9faf07693e6ebbe55f2Behdad Esfahbod	output_format = HB_BUFFER_SERIALIZE_FORMAT_TEXT;
64a4bef84e375b090c1bc7e4221b8e0ee435dfcbbaBehdad Esfahbod    }
65f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod
664dc798de19c767b91fd3025c85593fa6809d1340Behdad Esfahbod    unsigned int flags = HB_BUFFER_SERIALIZE_FLAG_DEFAULT;
67f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!format.show_glyph_names)
68f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      flags |= HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES;
69f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!format.show_clusters)
70f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      flags |= HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS;
71f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    if (!format.show_positions)
72f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod      flags |= HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS;
73f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    format_flags = (hb_buffer_serialize_flags_t) flags;
7445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
755db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  void new_line (void)
765db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  {
775db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    line_no++;
785db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  }
795db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  void consume_text (hb_buffer_t  *buffer,
808b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod		     const char   *text,
8195cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod		     unsigned int  text_len,
8245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod		     hb_bool_t     utf8_clusters)
8345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
8445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    g_string_set_size (gs, 0);
85f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    format.serialize_buffer_of_text (buffer, line_no, text, text_len, font, gs);
865db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    fprintf (options.fp, "%s", gs->str);
875db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  }
885db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  void shape_failed (hb_buffer_t  *buffer,
895db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		     const char   *text,
905db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		     unsigned int  text_len,
915db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		     hb_bool_t     utf8_clusters)
925db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  {
935db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    g_string_set_size (gs, 0);
945db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    format.serialize_message (line_no, "msg: all shapers failed", gs);
955db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    fprintf (options.fp, "%s", gs->str);
965db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  }
975db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  void consume_glyphs (hb_buffer_t  *buffer,
985db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		       const char   *text,
995db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		       unsigned int  text_len,
1005db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod		       hb_bool_t     utf8_clusters)
1015db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  {
1025db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    g_string_set_size (gs, 0);
103f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod    format.serialize_buffer_of_glyphs (buffer, line_no, text, text_len, font,
104f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod				       output_format, format_flags, gs);
1055db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    fprintf (options.fp, "%s", gs->str);
10645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
10745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  void finish (const font_options_t *font_opts)
10845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
10945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    hb_font_destroy (font);
1100594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod    g_string_free (gs, true);
11145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    gs = NULL;
11245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    font = NULL;
11345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
1143bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod
1158b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  protected:
1165db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  output_options_t options;
11745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  format_options_t format;
11845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
1198b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod  GString *gs;
120cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod  unsigned int line_no;
12145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  hb_font_t *font;
122f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  hb_buffer_serialize_format_t output_format;
123f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod  hb_buffer_serialize_flags_t format_flags;
1248b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod};
1254451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod
1268b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodint
1278b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodmain (int argc, char **argv)
1288b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{
129cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod  main_font_text_t<shape_consumer_t<output_buffer_t>, FONT_SIZE_UPEM, 0> driver;
13045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  return driver.main (argc, argv);
1318b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod}
132