From 45675e589e6ef9f81b2a4199cf33e3e7778433a8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 15 May 2012 23:10:39 -0400 Subject: [PATCH] [util] Refactor to accommodate for upcoming new tool --- util/Makefile.am | 10 ++-- util/hb-shape.cc | 70 ++++++++++++-------------- util/hb-view.cc | 8 +-- util/{hb-view.hh => main-font-text.hh} | 48 +++++++++--------- util/shape-consumer.hh | 69 +++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 70 deletions(-) rename util/{hb-view.hh => main-font-text.hh} (74%) create mode 100644 util/shape-consumer.hh diff --git a/util/Makefile.am b/util/Makefile.am index 9b4b34ac6..1a336b9a9 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -27,11 +27,12 @@ if HAVE_FREETYPE if HAVE_CAIRO_FT hb_view_SOURCES = \ hb-view.cc \ - hb-view.hh \ - ansi-print.cc \ - ansi-print.hh \ options.cc \ options.hh \ + main-font-text.hh \ + shape-consumer.hh \ + ansi-print.cc \ + ansi-print.hh \ helper-cairo.cc \ helper-cairo.hh \ helper-cairo-ansi.cc \ @@ -53,9 +54,10 @@ if HAVE_GLIB if HAVE_FREETYPE hb_shape_SOURCES = \ hb-shape.cc \ - hb-view.hh \ options.cc \ options.hh \ + main-font-text.hh \ + shape-consumer.hh \ $(NULL) bin_PROGRAMS += hb-shape endif diff --git a/util/hb-shape.cc b/util/hb-shape.cc index b22bc1fd1..3758be0db 100644 --- a/util/hb-shape.cc +++ b/util/hb-shape.cc @@ -1,6 +1,6 @@ /* * Copyright © 2010 Behdad Esfahbod - * Copyright © 2011 Google, Inc. + * Copyright © 2011,2012 Google, Inc. * * This is part of HarfBuzz, a text shaping library. * @@ -25,59 +25,51 @@ * Google Author(s): Behdad Esfahbod */ -#include "hb-view.hh" +#include "main-font-text.hh" +#include "shape-consumer.hh" -struct output_buffer_t : output_options_t, format_options_t +struct output_buffer_t : output_options_t { output_buffer_t (option_parser_t *parser) : output_options_t (parser), - format_options_t (parser) {} + format (parser) {} - void init (const font_options_t *font_opts); + void init (const font_options_t *font_opts) + { + get_file_handle (); + gs = g_string_new (NULL); + line_no = 0; + font = hb_font_reference (font_opts->get_font ()); + } void consume_line (hb_buffer_t *buffer, const char *text, unsigned int text_len, - hb_bool_t utf8_clusters); - void finish (const font_options_t *font_opts); + hb_bool_t utf8_clusters) + { + line_no++; + g_string_set_size (gs, 0); + format.serialize_line (buffer, line_no, text, text_len, font, utf8_clusters, gs); + fprintf (fp, "%s", gs->str); + } + void finish (const font_options_t *font_opts) + { + hb_font_destroy (font); + g_string_free (gs, TRUE); + gs = NULL; + font = NULL; + } protected: + format_options_t format; + GString *gs; - hb_font_t *font; unsigned int line_no; + hb_font_t *font; }; -void -output_buffer_t::init (const font_options_t *font_opts) -{ - get_file_handle (); - font = hb_font_reference (font_opts->get_font ()); - gs = g_string_new (NULL); - line_no = 0; -} - -void -output_buffer_t::consume_line (hb_buffer_t *buffer, - const char *text, - unsigned int text_len, - hb_bool_t utf8_clusters) -{ - line_no++; - g_string_set_size (gs, 0); - serialize_line (buffer, line_no, text, text_len, font, utf8_clusters, gs); - fprintf (fp, "%s", gs->str); -} - -void -output_buffer_t::finish (const font_options_t *font_opts) -{ - g_string_free (gs, TRUE); - gs = NULL; - hb_font_destroy (font); - font = NULL; -} - int main (int argc, char **argv) { - return hb_view_t::main (argc, argv); + main_font_text_t > driver; + return driver.main (argc, argv); } diff --git a/util/hb-view.cc b/util/hb-view.cc index 3bd1364c6..26fad6641 100644 --- a/util/hb-view.cc +++ b/util/hb-view.cc @@ -1,6 +1,6 @@ /* * Copyright © 2010 Behdad Esfahbod - * Copyright © 2011 Google, Inc. + * Copyright © 2011,2012 Google, Inc. * * This is part of HarfBuzz, a text shaping library. * @@ -25,11 +25,13 @@ * Google Author(s): Behdad Esfahbod */ -#include "hb-view.hh" +#include "main-font-text.hh" +#include "shape-consumer.hh" #include "view-cairo.hh" int main (int argc, char **argv) { - return hb_view_t::main (argc, argv); + main_font_text_t > driver; + return driver.main (argc, argv); } diff --git a/util/hb-view.hh b/util/main-font-text.hh similarity index 74% rename from util/hb-view.hh rename to util/main-font-text.hh index 61af2a8d0..1a9739f1f 100644 --- a/util/hb-view.hh +++ b/util/main-font-text.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2011 Google, Inc. + * Copyright © 2011,2012 Google, Inc. * * This is part of HarfBuzz, a text shaping library. * @@ -26,23 +26,23 @@ #include "options.hh" -#ifndef HB_VIEW_HH -#define HB_VIEW_HH +#ifndef HB_MAIN_FONT_TEXT_HH +#define HB_MAIN_FONT_TEXT_HH -template -struct hb_view_t +/* main() body for utilities taking font and processing text.*/ + +template +struct main_font_text_t { - static int + main_font_text_t (void) + : options ("[FONT-FILE] [TEXT]"), + font_opts (&options), + input (&options), + consumer (&options) {} + + int main (int argc, char **argv) { - option_parser_t options ("[FONT-FILE] [TEXT]"); - - shape_options_t shaper (&options); - font_options_t font_opts (&options); - text_options_t input (&options); - - output_t output (&options); - options.parse (&argc, &argv); argc--, argv++; @@ -55,26 +55,26 @@ struct hb_view_t if (!input.text && !input.text_file) input.text_file = "-"; - output.init (&font_opts); + consumer.init (&font_opts); hb_buffer_t *buffer = hb_buffer_create (); unsigned int text_len; const char *text; while ((text = input.get_line (&text_len))) - { - if (!shaper.shape (text, text_len, - font_opts.get_font (), - buffer)) - fail (FALSE, "All shapers failed"); - - output.consume_line (buffer, text, text_len, shaper.utf8_clusters); - } + consumer.consume_line (buffer, text, text_len); hb_buffer_destroy (buffer); - output.finish (&font_opts); + consumer.finish (&font_opts); return 0; } + + protected: + option_parser_t options; + font_options_t font_opts; + text_options_t input; + consumer_t consumer; }; #endif + diff --git a/util/shape-consumer.hh b/util/shape-consumer.hh new file mode 100644 index 000000000..d395b24a4 --- /dev/null +++ b/util/shape-consumer.hh @@ -0,0 +1,69 @@ +/* + * Copyright © 2011,2012 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include "options.hh" + +#ifndef HB_SHAPE_CONSUMER_HH +#define HB_SHAPE_CONSUMER_HH + + +template +struct shape_consumer_t +{ + shape_consumer_t (option_parser_t *parser) + : shaper (parser), + output (parser) {} + + void init (const font_options_t *font_opts) + { + font = hb_font_reference (font_opts->get_font ()); + output.init (font_opts); + } + void consume_line (hb_buffer_t *buffer, + const char *text, + unsigned int text_len) + { + if (!shaper.shape (text, text_len, font, buffer)) + fail (FALSE, "All shapers failed"); + + output.consume_line (buffer, text, text_len, shaper.utf8_clusters); + } + void finish (const font_options_t *font_opts) + { + output.finish (font_opts); + hb_font_destroy (font); + font = NULL; + } + + protected: + shape_options_t shaper; + output_t output; + + hb_font_t *font; +}; + + +#endif