2011-08-11 11:54:31 +02:00
|
|
|
/*
|
|
|
|
* 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 OPTIONS_HH
|
|
|
|
#define OPTIONS_HH
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
2011-09-19 22:41:17 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2017-08-12 04:51:06 +02:00
|
|
|
#include <assert.h>
|
2011-09-19 22:41:17 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-13 02:02:58 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for isatty() */
|
|
|
|
#endif
|
2013-02-12 21:35:32 +01:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
2013-02-01 00:18:05 +01:00
|
|
|
#include <io.h> /* for setmode() under Windows */
|
2011-09-19 22:41:17 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <hb.h>
|
2012-05-16 05:53:18 +02:00
|
|
|
#include <hb-ot.h>
|
2011-09-19 22:41:17 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
|
2014-03-19 23:38:02 +01:00
|
|
|
void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
|
2011-09-19 22:41:17 +02:00
|
|
|
|
2011-09-13 19:30:39 +02:00
|
|
|
|
|
|
|
struct option_parser_t
|
|
|
|
{
|
2018-09-30 12:06:26 +02:00
|
|
|
option_parser_t (const char *usage)
|
2021-08-05 18:33:31 +02:00
|
|
|
: usage_str (usage),
|
|
|
|
context (g_option_context_new (usage)),
|
|
|
|
to_free (g_ptr_array_new ())
|
2018-09-30 12:06:26 +02:00
|
|
|
{
|
2011-09-13 19:30:39 +02:00
|
|
|
add_main_options ();
|
|
|
|
}
|
2019-01-26 14:04:51 +01:00
|
|
|
|
|
|
|
static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
~option_parser_t ()
|
2018-09-30 12:06:26 +02:00
|
|
|
{
|
2011-09-13 19:30:39 +02:00
|
|
|
g_option_context_free (context);
|
2019-01-26 14:04:51 +01:00
|
|
|
g_ptr_array_foreach (to_free, _g_free_g_func, nullptr);
|
2014-07-05 00:09:29 +02:00
|
|
|
g_ptr_array_free (to_free, TRUE);
|
2011-09-13 19:30:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void add_main_options ();
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-05 19:43:25 +02:00
|
|
|
static void
|
|
|
|
post_parse_ (void *thiz, GError **error) {}
|
|
|
|
template <typename Type>
|
|
|
|
static auto
|
|
|
|
post_parse_ (Type *thiz, GError **error) -> decltype (thiz->post_parse (error))
|
|
|
|
{ thiz->post_parse (error); }
|
2021-08-05 19:30:39 +02:00
|
|
|
template <typename Type>
|
|
|
|
static gboolean
|
|
|
|
post_parse (GOptionContext *context G_GNUC_UNUSED,
|
|
|
|
GOptionGroup *group G_GNUC_UNUSED,
|
|
|
|
gpointer data,
|
|
|
|
GError **error)
|
|
|
|
{
|
2021-08-05 19:43:25 +02:00
|
|
|
option_parser_t::post_parse_ (static_cast<Type *> (data), error);
|
2021-08-05 19:30:39 +02:00
|
|
|
return !*error;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
2011-09-13 19:30:39 +02:00
|
|
|
void add_group (GOptionEntry *entries,
|
|
|
|
const gchar *name,
|
|
|
|
const gchar *description,
|
|
|
|
const gchar *help_description,
|
2021-08-05 19:30:39 +02:00
|
|
|
Type *closure)
|
|
|
|
{
|
|
|
|
GOptionGroup *group = g_option_group_new (name, description, help_description,
|
|
|
|
static_cast<gpointer>(closure), nullptr);
|
|
|
|
g_option_group_add_entries (group, entries);
|
|
|
|
g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
|
|
|
|
g_option_context_add_group (context, group);
|
|
|
|
}
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2014-07-05 00:09:29 +02:00
|
|
|
void free_later (char *p) {
|
|
|
|
g_ptr_array_add (to_free, p);
|
|
|
|
}
|
|
|
|
|
2011-09-13 19:30:39 +02:00
|
|
|
void parse (int *argc, char ***argv);
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
G_GNUC_NORETURN void usage () {
|
2011-09-13 19:30:39 +02:00
|
|
|
g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-07-05 00:09:29 +02:00
|
|
|
private:
|
2011-09-13 19:30:39 +02:00
|
|
|
const char *usage_str;
|
|
|
|
GOptionContext *context;
|
2014-07-05 00:09:29 +02:00
|
|
|
GPtrArray *to_free;
|
2011-09-13 19:30:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-20 21:30:45 +01:00
|
|
|
#define FONT_SIZE_UPEM 0x7FFFFFFF
|
|
|
|
#define FONT_SIZE_NONE 0
|
2011-09-08 22:00:04 +02:00
|
|
|
|
2021-08-05 21:48:59 +02:00
|
|
|
extern const unsigned DEFAULT_FONT_SIZE;
|
|
|
|
extern const unsigned SUBPIXEL_BITS;
|
|
|
|
|
2021-08-07 02:09:31 +02:00
|
|
|
struct face_options_t
|
|
|
|
{
|
|
|
|
void add_options (option_parser_t *parser);
|
|
|
|
|
|
|
|
hb_blob_t *get_blob () const;
|
|
|
|
hb_face_t *get_face () const;
|
|
|
|
|
|
|
|
static struct cache_t
|
|
|
|
{
|
|
|
|
~cache_t ()
|
|
|
|
{
|
|
|
|
free ((void *) font_path);
|
|
|
|
hb_blob_destroy (blob);
|
|
|
|
hb_face_destroy (face);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *font_path = nullptr;
|
|
|
|
hb_blob_t *blob = nullptr;
|
|
|
|
unsigned face_index = (unsigned) -1;
|
|
|
|
hb_face_t *face = nullptr;
|
|
|
|
} cache;
|
|
|
|
|
|
|
|
char *font_file = nullptr;
|
|
|
|
unsigned face_index = 0;
|
|
|
|
private:
|
|
|
|
mutable hb_face_t *face = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct font_options_t : face_options_t
|
2011-08-11 11:54:31 +02:00
|
|
|
{
|
2021-08-05 19:44:35 +02:00
|
|
|
~font_options_t ()
|
2018-09-30 12:06:26 +02:00
|
|
|
{
|
2015-11-03 20:34:47 +01:00
|
|
|
g_free (font_file);
|
2017-01-22 02:51:41 +01:00
|
|
|
free (variations);
|
2015-11-03 20:34:47 +01:00
|
|
|
g_free (font_funcs);
|
2011-09-13 19:30:39 +02:00
|
|
|
hb_font_destroy (font);
|
2011-08-11 11:54:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 19:21:20 +02:00
|
|
|
void add_options (option_parser_t *parser);
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_font_t *get_font () const;
|
2011-09-08 23:08:32 +02:00
|
|
|
|
2021-08-05 18:33:31 +02:00
|
|
|
hb_variation_t *variations = nullptr;
|
|
|
|
unsigned int num_variations = 0;
|
|
|
|
int x_ppem = 0;
|
|
|
|
int y_ppem = 0;
|
|
|
|
double ptem = 0.;
|
2021-08-05 21:48:59 +02:00
|
|
|
unsigned int subpixel_bits = SUBPIXEL_BITS;
|
|
|
|
mutable double font_size_x = DEFAULT_FONT_SIZE;
|
|
|
|
mutable double font_size_y = DEFAULT_FONT_SIZE;
|
2021-08-05 18:33:31 +02:00
|
|
|
char *font_funcs = nullptr;
|
|
|
|
int ft_load_flags = 2;
|
2011-08-11 11:54:31 +02:00
|
|
|
|
2018-06-18 17:29:33 +02:00
|
|
|
private:
|
2021-08-05 18:33:31 +02:00
|
|
|
mutable hb_font_t *font = nullptr;
|
2011-09-13 19:30:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-08-05 19:44:35 +02:00
|
|
|
struct text_options_t
|
2011-09-13 19:30:39 +02:00
|
|
|
{
|
2021-08-05 19:44:35 +02:00
|
|
|
~text_options_t ()
|
2018-09-30 12:06:26 +02:00
|
|
|
{
|
2015-11-03 20:34:47 +01:00
|
|
|
g_free (text_before);
|
|
|
|
g_free (text_after);
|
|
|
|
g_free (text);
|
|
|
|
g_free (text_file);
|
2011-09-16 08:08:36 +02:00
|
|
|
if (gs)
|
2012-06-06 02:35:40 +02:00
|
|
|
g_string_free (gs, true);
|
2018-10-30 09:24:23 +01:00
|
|
|
if (fp && fp != stdin)
|
2011-09-16 08:08:36 +02:00
|
|
|
fclose (fp);
|
2011-09-13 19:30:39 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 19:21:20 +02:00
|
|
|
void add_options (option_parser_t *parser);
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-05 19:43:25 +02:00
|
|
|
void post_parse (GError **error G_GNUC_UNUSED)
|
|
|
|
{
|
2011-09-13 19:30:39 +02:00
|
|
|
if (text && text_file)
|
|
|
|
g_set_error (error,
|
|
|
|
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
|
2012-05-12 15:54:27 +02:00
|
|
|
"Only one of text and text-file can be set");
|
2018-10-30 08:59:09 +01:00
|
|
|
}
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-01 15:59:25 +02:00
|
|
|
const char *get_line (unsigned int *len, int eol = '\n');
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-05 18:33:31 +02:00
|
|
|
char *text_before = nullptr;
|
|
|
|
char *text_after = nullptr;
|
2012-11-14 00:12:24 +01:00
|
|
|
|
2021-08-05 18:33:31 +02:00
|
|
|
int text_len = -1;
|
|
|
|
char *text = nullptr;
|
|
|
|
char *text_file = nullptr;
|
2011-09-13 19:30:39 +02:00
|
|
|
|
|
|
|
private:
|
2021-08-05 18:33:31 +02:00
|
|
|
FILE *fp = nullptr;
|
|
|
|
GString *gs = nullptr;
|
|
|
|
char *line = nullptr;
|
|
|
|
unsigned int line_len = UINT_MAX;
|
2011-09-13 19:30:39 +02:00
|
|
|
};
|
|
|
|
|
2021-08-05 19:44:35 +02:00
|
|
|
struct output_options_t
|
2011-09-13 19:30:39 +02:00
|
|
|
{
|
2021-08-05 19:44:35 +02:00
|
|
|
~output_options_t ()
|
2018-09-30 12:06:26 +02:00
|
|
|
{
|
2015-11-03 20:34:47 +01:00
|
|
|
g_free (output_file);
|
|
|
|
g_free (output_format);
|
2018-10-30 09:24:23 +01:00
|
|
|
if (fp && fp != stdout)
|
2011-09-15 23:52:00 +02:00
|
|
|
fclose (fp);
|
|
|
|
}
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-05 19:21:20 +02:00
|
|
|
void add_options (option_parser_t *parser,
|
|
|
|
const char **supported_formats = nullptr);
|
2011-09-13 19:30:39 +02:00
|
|
|
|
2021-08-05 19:43:25 +02:00
|
|
|
void post_parse (GError **error G_GNUC_UNUSED)
|
2011-09-13 19:30:39 +02:00
|
|
|
{
|
2012-12-21 22:01:52 +01:00
|
|
|
if (output_format)
|
|
|
|
explicit_output_format = true;
|
|
|
|
|
2011-09-13 19:30:39 +02:00
|
|
|
if (output_file && !output_format) {
|
|
|
|
output_format = strrchr (output_file, '.');
|
|
|
|
if (output_format)
|
2015-11-10 20:37:01 +01:00
|
|
|
{
|
2011-09-13 19:30:39 +02:00
|
|
|
output_format++; /* skip the dot */
|
2018-10-19 22:21:39 +02:00
|
|
|
output_format = g_strdup (output_format);
|
2015-11-10 20:37:01 +01:00
|
|
|
}
|
2011-09-13 19:30:39 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 23:52:00 +02:00
|
|
|
if (output_file && 0 == strcmp (output_file, "-"))
|
2017-10-15 12:11:08 +02:00
|
|
|
output_file = nullptr; /* STDOUT */
|
2011-09-15 23:52:00 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
FILE *get_file_handle ();
|
2011-08-11 11:54:31 +02:00
|
|
|
|
2021-08-05 18:33:31 +02:00
|
|
|
char *output_file = nullptr;
|
|
|
|
char *output_format = nullptr;
|
|
|
|
bool explicit_output_format = false;
|
2011-09-15 23:52:00 +02:00
|
|
|
|
2021-08-05 18:33:31 +02:00
|
|
|
mutable FILE *fp = nullptr;
|
2011-09-13 19:30:39 +02:00
|
|
|
};
|
2011-08-11 11:54:31 +02:00
|
|
|
|
2011-09-19 22:41:17 +02:00
|
|
|
|
2015-11-03 11:49:34 +01:00
|
|
|
/* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
|
|
|
|
#if defined (_MSC_VER) && (_MSC_VER < 1800)
|
|
|
|
|
|
|
|
#ifndef FLT_RADIX
|
|
|
|
#define FLT_RADIX 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__inline long double scalbn (long double x, int exp)
|
|
|
|
{
|
|
|
|
return x * (pow ((long double) FLT_RADIX, exp));
|
|
|
|
}
|
|
|
|
|
|
|
|
__inline float scalbnf (float x, int exp)
|
|
|
|
{
|
|
|
|
return x * (pow ((float) FLT_RADIX, exp));
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-11 11:54:31 +02:00
|
|
|
|
|
|
|
#endif
|