From 9d8bbe3e531ded6419b2573c8b813596fff69745 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Aug 2021 11:53:32 -0600 Subject: [PATCH] [util] Make main_font_text a struct again Going to move to G_OPTION_REMAINING. --- util/batch.hh | 8 ++--- util/hb-ot-shape-closure.cc | 2 +- util/hb-shape.cc | 4 +-- util/hb-subset.cc | 4 +-- util/hb-view.cc | 2 +- util/main-font-text.hh | 59 +++++++++++++++++++------------------ 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/util/batch.hh b/util/batch.hh index e3dd10fc3..2cbb12077 100644 --- a/util/batch.hh +++ b/util/batch.hh @@ -27,9 +27,9 @@ typedef int (*main_func_t) (int argc, char **argv); -template +template int -batch_main (main_func_t main_func, int argc, char **argv) +batch_main (int argc, char **argv) { if (argc == 2 && !strcmp (argv[1], "--batch")) { @@ -53,7 +53,7 @@ batch_main (main_func_t main_func, int argc, char **argv) args[argc++] = p = e; } - int result = main_func (argc, args); + int result = main_t () (argc, args); if (report_status) fprintf (stdout, result == 0 ? "success\n" : "failure\n"); @@ -64,7 +64,7 @@ batch_main (main_func_t main_func, int argc, char **argv) return ret; } - return main_func (argc, argv); + return main_t () (argc, argv); } #endif diff --git a/util/hb-ot-shape-closure.cc b/util/hb-ot-shape-closure.cc index 41d18b549..9686d1d3a 100644 --- a/util/hb-ot-shape-closure.cc +++ b/util/hb-ot-shape-closure.cc @@ -114,5 +114,5 @@ struct shape_closure_consumer_t int main (int argc, char **argv) { - return main_font_text (argc, argv); + return main_font_text () (argc, argv); } diff --git a/util/hb-shape.cc b/util/hb-shape.cc index d19e582fe..0209426ba 100644 --- a/util/hb-shape.cc +++ b/util/hb-shape.cc @@ -162,6 +162,6 @@ struct output_buffer_t : output_options_t int main (int argc, char **argv) { - auto main_func = main_font_text, font_options_t, text_options_t>; - return batch_main<> (main_func, argc, argv); + using main_t = main_font_text, font_options_t, text_options_t>; + return batch_main (argc, argv); } diff --git a/util/hb-subset.cc b/util/hb-subset.cc index b596752e2..b1a9c0d69 100644 --- a/util/hb-subset.cc +++ b/util/hb-subset.cc @@ -129,6 +129,6 @@ struct subset_consumer_t : subset_options_t, output_options_t int main (int argc, char **argv) { - auto main_func = main_font_text; - return batch_main (main_func, argc, argv); + using main_t = main_font_text; + return batch_main (argc, argv); } diff --git a/util/hb-view.cc b/util/hb-view.cc index 5d2dfe52a..3a9ecff47 100644 --- a/util/hb-view.cc +++ b/util/hb-view.cc @@ -37,5 +37,5 @@ const unsigned SUBPIXEL_BITS = 6; int main (int argc, char **argv) { - return main_font_text, font_options_t, text_options_t> (argc, argv); + return main_font_text, font_options_t, text_options_t> () (argc, argv); } diff --git a/util/main-font-text.hh b/util/main-font-text.hh index b8c023a5b..9d7e2094e 100644 --- a/util/main-font-text.hh +++ b/util/main-font-text.hh @@ -32,40 +32,43 @@ /* main() body for utilities taking font and processing text.*/ template -int -main_font_text (int argc, char **argv) +struct main_font_text : font_options_t, text_options_t, consumer_t { + void add_options (struct option_parser_t *parser) + { + font_options_t::add_options (parser); + text_options_t::add_options (parser); + consumer_t::add_options (parser); + } - font_options_t font_opts; - text_options_t input; - consumer_t consumer; + int + operator () (int argc, char **argv) + { + option_parser_t options ("[FONT-FILE] [TEXT]"); + add_options (&options); + options.parse (&argc, &argv); - option_parser_t options ("[FONT-FILE] [TEXT]"); - font_opts.add_options (&options); - input.add_options (&options); - consumer.add_options (&options); - options.parse (&argc, &argv); + argc--, argv++; + if (argc && !this->font_file) this->font_file = locale_to_utf8 (argv[0]), argc--, argv++; + if (argc && !this->text && !this->text_file) this->text = locale_to_utf8 (argv[0]), argc--, argv++; + if (argc) + fail (true, "Too many arguments on the command line"); + if (!this->font_file) + options.usage (); + if (!this->text && !this->text_file) + this->text_file = g_strdup ("-"); - argc--, argv++; - if (argc && !font_opts.font_file) font_opts.font_file = locale_to_utf8 (argv[0]), argc--, argv++; - if (argc && !input.text && !input.text_file) input.text = locale_to_utf8 (argv[0]), argc--, argv++; - if (argc) - fail (true, "Too many arguments on the command line"); - if (!font_opts.font_file) - options.usage (); - if (!input.text && !input.text_file) - input.text_file = g_strdup ("-"); + this->init (this); - consumer.init (&font_opts); + unsigned int line_len; + const char *line; + while ((line = this->get_line (&line_len))) + this->consume_line (line, line_len, this->text_before, this->text_after); - unsigned int text_len; - const char *text; - while ((text = input.get_line (&text_len))) - consumer.consume_line (text, text_len, input.text_before, input.text_after); + this->finish (this); - consumer.finish (&font_opts); - - return consumer.failed ? 1 : 0; -} + return this->failed ? 1 : 0; + } +}; #endif