[util] Remove main_font_text_t class, use bare function

This commit is contained in:
Behdad Esfahbod 2021-08-07 11:04:46 -06:00
parent 58c223357b
commit 5545eea7e5
6 changed files with 55 additions and 64 deletions

View File

@ -113,6 +113,5 @@ struct shape_closure_consumer_t
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
main_font_text_t<shape_closure_consumer_t, font_options_t, text_options_t> driver; return main_font_text<shape_closure_consumer_t, font_options_t, text_options_t> (argc, argv);
return driver.main (argc, argv);
} }

View File

@ -159,7 +159,7 @@ struct output_buffer_t
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
using driver_t = main_font_text_t<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>; auto main_func = main_font_text<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>;
if (argc == 2 && !strcmp (argv[1], "--batch")) if (argc == 2 && !strcmp (argv[1], "--batch"))
{ {
@ -185,13 +185,11 @@ main (int argc, char **argv)
start_offset = argc == 2 && p[0] != '\0' && p[0] != ':' && p[1] == ':' && (p[2] == '\\' || p[2] == '/') ? 2 : 0; start_offset = argc == 2 && p[0] != '\0' && p[0] != ':' && p[1] == ':' && (p[2] == '\\' || p[2] == '/') ? 2 : 0;
} }
driver_t driver; ret |= main_func (argc, args, EOF);
ret |= driver.main (argc, args, EOF);
fflush (stdout); fflush (stdout);
} }
return ret; return ret;
} }
driver_t driver; return main_func (argc, argv, '\n');
return driver.main (argc, argv);
} }

View File

@ -132,7 +132,7 @@ struct subset_consumer_t : subset_options_t
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
using driver_t = main_font_text_t<subset_consumer_t, face_options_t, text_options_t>; auto main_func = main_font_text<subset_consumer_t, face_options_t, text_options_t>;
if (argc == 2 && !strcmp (argv[1], "--batch")) if (argc == 2 && !strcmp (argv[1], "--batch"))
{ {
@ -156,8 +156,7 @@ main (int argc, char **argv)
args[argc++] = p = e; args[argc++] = p = e;
} }
driver_t driver; int result = main_func (argc, args, EOF);
int result = driver.main (argc, args, EOF);
fprintf (stdout, result == 0 ? "success\n" : "failure\n"); fprintf (stdout, result == 0 ? "success\n" : "failure\n");
fflush (stdout); fflush (stdout);
ret |= result; ret |= result;
@ -165,6 +164,5 @@ main (int argc, char **argv)
return ret; return ret;
} }
driver_t driver; return main_func (argc, argv, '\n');
return driver.main (argc, argv);
} }

View File

@ -35,6 +35,5 @@ const unsigned SUBPIXEL_BITS = 6;
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
main_font_text_t<shape_consumer_t<view_cairo_t>, font_options_t, text_options_t> driver; return main_font_text<shape_consumer_t<view_cairo_t>, font_options_t, text_options_t> (argc, argv);
return driver.main (argc, argv);
} }

View File

@ -31,59 +31,41 @@
/* main() body for utilities taking font and processing text.*/ /* main() body for utilities taking font and processing text.*/
static char * template <typename consumer_t, typename font_options_t, typename text_options_t>
locale_to_utf8 (char *s) int
main_font_text (int argc, char **argv, int eol = '\n')
{ {
char *t;
GError *error = nullptr;
t = g_locale_to_utf8 (s, -1, nullptr, nullptr, &error); font_options_t font_opts;
if (!t) text_options_t input;
{ consumer_t consumer;
fail (true, "Failed converting text to UTF-8");
}
return t; 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 && !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 ("-");
consumer.init (&font_opts);
unsigned int text_len;
const char *text;
while ((text = input.get_line (&text_len, eol)))
consumer.consume_line (text, text_len, input.text_before, input.text_after);
consumer.finish (&font_opts);
return consumer.failed ? 1 : 0;
} }
template <typename consumer_t, typename font_options_t, typename text_options_t>
struct main_font_text_t
{
int
main (int argc, char **argv, int eol = '\n')
{
font_options_t font_opts;
text_options_t input;
consumer_t consumer;
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 && !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 ("-");
consumer.init (&font_opts);
unsigned int text_len;
const char *text;
while ((text = input.get_line (&text_len, eol)))
consumer.consume_line (text, text_len, input.text_before, input.text_after);
consumer.finish (&font_opts);
return consumer.failed ? 1 : 0;
}
};
#endif #endif

View File

@ -70,6 +70,21 @@ fail (hb_bool_t suggest_help, const char *format, ...)
exit (1); exit (1);
} }
static inline char *
locale_to_utf8 (char *s)
{
char *t;
GError *error = nullptr;
t = g_locale_to_utf8 (s, -1, nullptr, nullptr, &error);
if (!t)
{
fail (true, "Failed converting text to UTF-8");
}
return t;
}
struct option_parser_t struct option_parser_t
{ {