[util] Refactor to accommodate for upcoming new tool

This commit is contained in:
Behdad Esfahbod 2012-05-15 23:10:39 -04:00
parent 1d6846db9e
commit 45675e589e
5 changed files with 135 additions and 70 deletions

View File

@ -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

View File

@ -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<output_buffer_t>::main (argc, argv);
main_font_text_t<shape_consumer_t<output_buffer_t> > driver;
return driver.main (argc, argv);
}

View File

@ -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<view_cairo_t>::main (argc, argv);
main_font_text_t<shape_consumer_t<view_cairo_t> > driver;
return driver.main (argc, argv);
}

View File

@ -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 <typename output_t>
struct hb_view_t
/* main() body for utilities taking font and processing text.*/
template <typename consumer_t>
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

69
util/shape-consumer.hh Normal file
View File

@ -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 <typename output_t>
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