diff --git a/util/Makefile.sources b/util/Makefile.sources index 200d3f673..56c455661 100644 --- a/util/Makefile.sources +++ b/util/Makefile.sources @@ -8,6 +8,7 @@ HB_VIEW_sources = \ main-font-text.hh \ options.cc \ options.hh \ + output-options.hh \ shape-consumer.hh \ shape-options.hh \ view-cairo.hh \ @@ -21,11 +22,22 @@ HB_SHAPE_sources = \ main-font-text.hh \ options.cc \ options.hh \ + output-options.hh \ shape-consumer.hh \ shape-format.hh \ shape-options.hh \ $(NULL) +HB_SUBSET_CLI_sources = \ + face-options.hh \ + hb-subset.cc \ + main-font-text.hh \ + options.cc \ + options.hh \ + output-options.hh \ + subset-options.hh \ + $(NULL) + HB_OT_SHAPE_CLOSURE_sources = \ face-options.hh \ font-options.hh \ @@ -34,12 +46,3 @@ HB_OT_SHAPE_CLOSURE_sources = \ options.cc \ options.hh \ $(NULL) - -HB_SUBSET_CLI_sources = \ - face-options.hh \ - hb-subset.cc \ - main-font-text.hh \ - options.cc \ - options.hh \ - subset-options.hh \ - $(NULL) diff --git a/util/hb-shape.cc b/util/hb-shape.cc index 776afcdb4..29f99176b 100644 --- a/util/hb-shape.cc +++ b/util/hb-shape.cc @@ -27,6 +27,7 @@ #include "shape-consumer.hh" #include "shape-format.hh" +#include "output-options.hh" #include "main-font-text.hh" const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; diff --git a/util/hb-subset.cc b/util/hb-subset.cc index 704070757..b5f3f7f0a 100644 --- a/util/hb-subset.cc +++ b/util/hb-subset.cc @@ -28,6 +28,7 @@ #include #include "subset-options.hh" +#include "output-options.hh" #include "face-options.hh" #include "main-font-text.hh" diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 9a1cd4c5a..e219e646b 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -27,8 +27,8 @@ #ifndef HELPER_CAIRO_HH #define HELPER_CAIRO_HH -#include "hb.hh" -#include "options.hh" +#include "view-options.hh" +#include "output-options.hh" #include #include diff --git a/util/options.cc b/util/options.cc index 5cbd4a1b6..66ddaa8e6 100644 --- a/util/options.cc +++ b/util/options.cc @@ -121,34 +121,6 @@ text_options_t::add_options (option_parser_t *parser) this); } -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); -} - const char * text_options_t::get_line (unsigned int *len, int eol) { @@ -217,25 +189,3 @@ text_options_t::get_line (unsigned int *len, int eol) *len = gs->len; return !*len && feof (fp) ? nullptr : gs->str; } - - -FILE * -output_options_t::get_file_handle () -{ - if (fp) - return fp; - - if (output_file) - fp = fopen (output_file, "wb"); - else { -#if defined(_WIN32) || defined(__CYGWIN__) - setmode (fileno (stdout), O_BINARY); -#endif - fp = stdout; - } - if (!fp) - fail (false, "Cannot open output file `%s': %s", - g_filename_display_name (output_file), strerror (errno)); - - return fp; -} diff --git a/util/options.hh b/util/options.hh index ee9041845..246a7c6df 100644 --- a/util/options.hh +++ b/util/options.hh @@ -261,46 +261,6 @@ struct text_options_t unsigned int line_len = UINT_MAX; }; -struct output_options_t -{ - ~output_options_t () - { - g_free (output_file); - g_free (output_format); - if (fp && fp != stdout) - fclose (fp); - } - - void add_options (option_parser_t *parser, - const char **supported_formats = nullptr); - - void post_parse (GError **error G_GNUC_UNUSED) - { - 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 */ - } - - FILE *get_file_handle (); - - char *output_file = nullptr; - char *output_format = nullptr; - bool explicit_output_format = false; - - mutable FILE *fp = nullptr; -}; - /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */ #if defined (_MSC_VER) && (_MSC_VER < 1800) diff --git a/util/output-options.hh b/util/output-options.hh new file mode 100644 index 000000000..f3fda61e3 --- /dev/null +++ b/util/output-options.hh @@ -0,0 +1,123 @@ +/* + * Copyright © 2011 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 + */ + +#ifndef OUTPUT_OPTIONS_HH +#define OUTPUT_OPTIONS_HH + +#include "options.hh" + +struct output_options_t +{ + ~output_options_t () + { + g_free (output_file); + g_free (output_format); + if (fp && fp != stdout) + fclose (fp); + } + + void add_options (option_parser_t *parser, + const char **supported_formats = nullptr); + + void post_parse (GError **error G_GNUC_UNUSED) + { + 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 */ + } + + FILE *get_file_handle (); + + char *output_file = nullptr; + char *output_format = nullptr; + bool explicit_output_format = false; + + mutable FILE *fp = nullptr; +}; + + +FILE * +output_options_t::get_file_handle () +{ + if (fp) + return fp; + + if (output_file) + fp = fopen (output_file, "wb"); + else { +#if defined(_WIN32) || defined(__CYGWIN__) + setmode (fileno (stdout), O_BINARY); +#endif + fp = stdout; + } + if (!fp) + fail (false, "Cannot open output file `%s': %s", + g_filename_display_name (output_file), strerror (errno)); + + return fp; +} + + +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 47fbf35f4..bfe4e2a0c 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -28,6 +28,7 @@ #define VIEW_CAIRO_HH #include "view-options.hh" +#include "output-options.hh" #include "helper-cairo.hh" struct view_cairo_t : view_options_t, output_options_t