diff --git a/util/hb-shape.cc b/util/hb-shape.cc index 0e5633d3b..0e8263be2 100644 --- a/util/hb-shape.cc +++ b/util/hb-shape.cc @@ -36,7 +36,7 @@ const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; const unsigned SUBPIXEL_BITS = 0; -struct output_buffer_t : output_options_t +struct output_buffer_t : output_options_t<> { void add_options (option_parser_t *parser) { diff --git a/util/hb-subset.cc b/util/hb-subset.cc index 903321a21..0683d7e4b 100644 --- a/util/hb-subset.cc +++ b/util/hb-subset.cc @@ -38,7 +38,7 @@ * Command line interface to the harfbuzz font subsetter. */ -struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_options_t, output_options_t +struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_options_t, output_options_t { int operator () (int argc, char **argv) { @@ -92,8 +92,7 @@ struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_o bool write_file (const char *output_file, hb_blob_t *blob) { - if (!output_file) - fail (true, "No output file was specified"); + assert (out_fp); unsigned int size; const char* data = hb_blob_get_data (blob, &size); diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 3c4857c79..b8d461224 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -426,6 +426,8 @@ static const char *helper_cairo_supported_formats[] = nullptr }; +template static inline cairo_t * helper_cairo_create_context (double w, double h, view_options_t *view_opts, diff --git a/util/output-options.hh b/util/output-options.hh index 9d7b3c723..270e9f1e4 100644 --- a/util/output-options.hh +++ b/util/output-options.hh @@ -29,6 +29,7 @@ #include "options.hh" +template struct output_options_t { ~output_options_t () @@ -40,9 +41,71 @@ struct output_options_t } void add_options (option_parser_t *parser, - const char **supported_formats = nullptr); + const char **supported_formats = nullptr) + { + const char *text = nullptr; - void post_parse (GError **error G_GNUC_UNUSED); + if (supported_formats) + { + char *items = g_strjoinv ("/", const_cast (supported_formats)); + text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items); + g_free (items); + parser->free_later ((char *) text); + } + + GOptionEntry entries[] = + { + {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, + {"output-format", 'O', supported_formats ? 0 : G_OPTION_FLAG_HIDDEN, + G_OPTION_ARG_STRING, &this->output_format, text, "format"}, + {nullptr} + }; + parser->add_group (entries, + "output", + "Output destination & format options:", + "Options for the destination & form of the output", + this); + } + + void post_parse (GError **error) + { + if (output_format) + explicit_output_format = true; + + if (output_file && !output_format) + { + output_format = strrchr (output_file, '.'); + if (output_format) + { + output_format++; /* skip the dot */ + output_format = g_strdup (output_format); + } + } + + if (output_file && 0 != strcmp (output_file, "-")) + out_fp = fopen (output_file, "wb"); + else + { + if (!default_stdout && !output_file) + { + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, + "No output file was specified"); + return; + } + +#if defined(_WIN32) || defined(__CYGWIN__) + setmode (fileno (stdout), O_BINARY); +#endif + out_fp = stdout; + } + if (!out_fp) + { + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, + "Cannot open output file `%s': %s", + g_filename_display_name (output_file), strerror (errno)); + return; + } + } char *output_file = nullptr; char *output_format = nullptr; @@ -51,69 +114,4 @@ struct output_options_t FILE *out_fp = nullptr; }; -void -output_options_t::post_parse (GError **error) -{ - if (output_format) - explicit_output_format = true; - - if (output_file && !output_format) - { - output_format = strrchr (output_file, '.'); - if (output_format) - { - output_format++; /* skip the dot */ - output_format = g_strdup (output_format); - } - } - - if (output_file && 0 == strcmp (output_file, "-")) - output_file = nullptr; /* STDOUT */ - - if (output_file) - out_fp = fopen (output_file, "wb"); - else - { -#if defined(_WIN32) || defined(__CYGWIN__) - setmode (fileno (stdout), O_BINARY); -#endif - out_fp = stdout; - } - if (!out_fp) - { - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, - "Cannot open output file `%s': %s", - g_filename_display_name (output_file), strerror (errno)); - return; - } -} - -void -output_options_t::add_options (option_parser_t *parser, - const char **supported_formats) -{ - const char *text = nullptr; - - if (supported_formats) - { - char *items = g_strjoinv ("/", const_cast (supported_formats)); - text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items); - g_free (items); - parser->free_later ((char *) text); - } - - GOptionEntry entries[] = - { - {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, - {"output-format", 'O', supported_formats ? 0 : G_OPTION_FLAG_HIDDEN, - G_OPTION_ARG_STRING, &this->output_format, text, "format"}, - {nullptr} - }; - parser->add_group (entries, - "output", - "Output destination & format options:", - "Options for the destination & form of the output", - this); -} - #endif diff --git a/util/view-cairo.hh b/util/view-cairo.hh index 430f6f9fd..e48d9d508 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -31,7 +31,7 @@ #include "output-options.hh" #include "helper-cairo.hh" -struct view_cairo_t : view_options_t, output_options_t +struct view_cairo_t : view_options_t, output_options_t<> { ~view_cairo_t () {