13bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod/* 28f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod * Copyright © 2011,2012 Google, Inc. 33bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * 43bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * This is part of HarfBuzz, a text shaping library. 53bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * 63bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Permission is hereby granted, without written agreement and without 73bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this 83bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * software and its documentation for any purpose, provided that the 93bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * above copyright notice and the following two paragraphs appear in 103bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * all copies of this software. 113bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * 123bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 133bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 143bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 153bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * DAMAGE. 173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * 183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 193bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 213bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 223bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * 243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod * Google Author(s): Behdad Esfahbod 253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod */ 263bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 273bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod#include "options.hh" 283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 295ddd9cc499f2470eca239ae357a5c8a3626c0809Behdad Esfahbod#ifdef HAVE_FREETYPE 30b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod#include <hb-ft.h> 318650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif 328650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_OT 33903648437c180c7b039801cdb0672e0f8e14afd4Behdad Esfahbod#include <hb-ot-font.h> 34b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod#endif 353bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 368650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbodstruct supported_font_funcs_t { 378650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod char name[4]; 388650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod void (*func) (hb_font_t *); 398650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod} supported_font_funcs[] = 408650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod{ 418650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_FREETYPE 428650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod {"ft", hb_ft_font_set_funcs}, 438650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif 448650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#ifdef HAVE_OT 458650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod {"ot", hb_ot_font_set_funcs}, 468650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod#endif 478650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod}; 488650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod 493bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 508b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid 518b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodfail (hb_bool_t suggest_help, const char *format, ...) 528b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{ 538b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod const char *msg; 548b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod va_list vap; 568b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod va_start (vap, format); 578b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod msg = g_strdup_vprintf (format, vap); 58c36c4a9924609df648e62e47fa017b19b844fd98Behdad Esfahbod va_end (vap); 598b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod const char *prgname = g_get_prgname (); 608b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod g_printerr ("%s: %s\n", prgname, msg); 618b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod if (suggest_help) 628b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod g_printerr ("Try `%s --help' for more information.\n", prgname); 638b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 648b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod exit (1); 658b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod} 668b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 678b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 680594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbodhb_bool_t debug = false; 69b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 70b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gchar * 71b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshapers_to_string (void) 72b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 73b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GString *shapers = g_string_new (NULL); 74b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod const char **shaper_list = hb_shape_list_shapers (); 75b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 76b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod for (; *shaper_list; shaper_list++) { 77b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_string_append (shapers, *shaper_list); 78b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_string_append_c (shapers, ','); 79b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod } 80b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1)); 81b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 820594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return g_string_free (shapers, false); 83b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 84b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 85b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic G_GNUC_NORETURN gboolean 86b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshow_version (const char *name G_GNUC_UNUSED, 87b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod const char *arg G_GNUC_UNUSED, 88b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod gpointer data G_GNUC_UNUSED, 89b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GError **error G_GNUC_UNUSED) 90b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 91b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION); 92b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 93b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod char *shapers = shapers_to_string (); 94b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_printf ("Available shapers: %s\n", shapers); 95b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_free (shapers); 96b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod if (strcmp (HB_VERSION_STRING, hb_version_string ())) 97b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ()); 98b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 99b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod exit(0); 100b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 101b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 102b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 103b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid 104b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_main_options (void) 105b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 106b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GOptionEntry entries[] = 107b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod { 108b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod {"version", 0, G_OPTION_FLAG_NO_ARG, 109b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL}, 110b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod {"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL}, 111b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod {NULL} 112b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod }; 113b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_option_context_add_main_entries (context, entries, NULL); 114b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 115b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 116b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean 117b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpre_parse (GOptionContext *context G_GNUC_UNUSED, 118b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GOptionGroup *group G_GNUC_UNUSED, 119b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod gpointer data, 120b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GError **error) 121b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 122b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod option_group_t *option_group = (option_group_t *) data; 123b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod option_group->pre_parse (error); 124b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod return *error == NULL; 125b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 126b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 127b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodstatic gboolean 128b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodpost_parse (GOptionContext *context G_GNUC_UNUSED, 129b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GOptionGroup *group G_GNUC_UNUSED, 130b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod gpointer data, 131b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GError **error) 132b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 133b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod option_group_t *option_group = static_cast<option_group_t *>(data); 134b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod option_group->post_parse (error); 135b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod return *error == NULL; 136b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 137b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 138b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid 139b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::add_group (GOptionEntry *entries, 140b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod const gchar *name, 141b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod const gchar *description, 142b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod const gchar *help_description, 143b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod option_group_t *option_group) 144b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 145b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GOptionGroup *group = g_option_group_new (name, description, help_description, 146b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod static_cast<gpointer>(option_group), NULL); 147b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_option_group_add_entries (group, entries); 148b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_option_group_set_parse_hooks (group, pre_parse, post_parse); 149b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod g_option_context_add_group (context, group); 150b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 151b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 152b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid 153b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoption_parser_t::parse (int *argc, char ***argv) 154b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 1558b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod setlocale (LC_ALL, ""); 1568b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 157b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GError *parse_error = NULL; 158b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod if (!g_option_context_parse (context, argc, argv, &parse_error)) 159b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod { 160a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod if (parse_error != NULL) { 1610594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (true, "%s", parse_error->message); 162a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod //g_error_free (parse_error); 163a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod } else 1640594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (true, "Option parse error"); 165b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod } 166b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 1673bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 1683bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 1693bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean 1703bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_margin (const char *name G_GNUC_UNUSED, 1713bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod const char *arg, 172bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod gpointer data, 1733bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod GError **error G_GNUC_UNUSED) 1743bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{ 175bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod view_options_t *view_opts = (view_options_t *) data; 1763bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod view_options_t::margin_t &m = view_opts->margin; 17797796453aab56873809a15b5e316cba8acea7449Behdad Esfahbod switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) { 1783bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod case 1: m.r = m.t; 1793bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod case 2: m.b = m.t; 1803bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod case 3: m.l = m.r; 1810594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod case 4: return true; 1823bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod default: 1833bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, 1843bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod "%s argument should be one to four space-separated numbers", 1853bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod name); 1860594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return false; 1873bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod } 1883bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod} 1893bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 1903bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 1913bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean 1923bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_shapers (const char *name G_GNUC_UNUSED, 1933bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod const char *arg, 194bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod gpointer data, 1953bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod GError **error G_GNUC_UNUSED) 1963bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{ 197bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod shape_options_t *shape_opts = (shape_options_t *) data; 198ade7459ea7c75b4f33f7cfa43dd5bdfa0c18d6d5Behdad Esfahbod g_strfreev (shape_opts->shapers); 1993bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->shapers = g_strsplit (arg, ",", 0); 2000594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return true; 2013bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod} 2023bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 203fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodstatic G_GNUC_NORETURN gboolean 204fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbodlist_shapers (const char *name G_GNUC_UNUSED, 205fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod const char *arg G_GNUC_UNUSED, 206fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod gpointer data G_GNUC_UNUSED, 207fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod GError **error G_GNUC_UNUSED) 208fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod{ 209fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod for (const char **shaper = hb_shape_list_shapers (); *shaper; shaper++) 210fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod g_printf ("%s\n", *shaper); 211fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod 212fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod exit(0); 213fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod} 214fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod 215fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod 2163bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodstatic gboolean 2173bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodparse_features (const char *name G_GNUC_UNUSED, 2183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod const char *arg, 219bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod gpointer data, 2203bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod GError **error G_GNUC_UNUSED) 2213bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{ 222bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod shape_options_t *shape_opts = (shape_options_t *) data; 2233bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod char *s = (char *) arg; 2243bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod char *p; 2253bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2263bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->num_features = 0; 2278f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod g_free (shape_opts->features); 2283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->features = NULL; 2293bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2303bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod if (!*s) 2310594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return true; 2323bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2333bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod /* count the features first, so we can allocate memory */ 2343bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod p = s; 2353bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod do { 2363bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->num_features++; 2373bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod p = strchr (p, ','); 2383bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod if (p) 2393bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod p++; 2403bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod } while (p); 2413bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2423bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features)); 2433bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2443bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod /* now do the actual parsing */ 2453bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod p = s; 2463bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->num_features = 0; 247e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod while (p && *p) { 248e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod char *end = strchr (p, ','); 249e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features])) 2503bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod shape_opts->num_features++; 251e30ebd2794b37bd8ec716a258d5cb017fb1dfadcBehdad Esfahbod p = end ? end + 1 : NULL; 2523bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod } 2533bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2540594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return true; 2553bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod} 2563bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2573bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 2583bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbodvoid 259b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodview_options_t::add_options (option_parser_t *parser) 2603bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod{ 2613bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod GOptionEntry entries[] = 2623bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod { 263bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", NULL}, 264d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"}, 265d688475ea9cd236bc9b9e0153489fac0025f07f0Behdad Esfahbod {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"}, 266bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"}, 2679a34a50daac3563464e0031f377e85f0c704f2d6Behdad Esfahbod {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"}, 268109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod {NULL} 269109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod }; 270b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod parser->add_group (entries, 271b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "view", 272b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "View options:", 27330874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod "Options controlling output rendering", 274b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod this); 275109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod} 2763bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 277109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid 278b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodshape_options_t::add_options (option_parser_t *parser) 279109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{ 280109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod GOptionEntry entries[] = 281109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod { 282fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod {"list-shapers", 0, G_OPTION_FLAG_NO_ARG, 283fd528c17b7b5ac912f1ac980e1d9981f561c3b46Behdad Esfahbod G_OPTION_ARG_CALLBACK, (gpointer) &list_shapers, "List available shapers and quit", NULL}, 2848f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod {"shaper", 0, G_OPTION_FLAG_HIDDEN, 2858f8956a55fff95e5ad529d2f124c9528d1f4f81dBehdad Esfahbod G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Hidden duplicate of --shapers", NULL}, 2868650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod {"shapers", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_shapers, "Set comma-separated list of shapers to try","list"}, 287bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod {"direction", 0, 0, G_OPTION_ARG_STRING, &this->direction, "Set text direction (default: auto)", "ltr/rtl/ttb/btt"}, 288bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod {"language", 0, 0, G_OPTION_ARG_STRING, &this->language, "Set text language (default: $LANG)", "langstr"}, 289bc4b07b05ea9e39eb9f966eb2c3e1c737efa77ffBehdad Esfahbod {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, 290407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", NULL}, 291407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", NULL}, 292407f80d62589774f845ef1a6a0a7d841b09d57c6Behdad Esfahbod {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", NULL}, 29330874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod {"utf8-clusters", 0, 0, G_OPTION_ARG_NONE, &this->utf8_clusters, "Use UTF8 byte indices, not char indices", NULL}, 29439b17837b4064d59c18cebb49c1c0b5b8cc0c117Behdad Esfahbod {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE, &this->normalize_glyphs, "Rearrange glyph clusters in nominal order", NULL}, 29550067e280f381918b8e90b26df9e7bf20f98f0bdBehdad Esfahbod {"num-iterations", 0, 0, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"}, 296109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod {NULL} 297109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod }; 298b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod parser->add_group (entries, 299b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "shape", 300b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Shape options:", 301b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Options controlling the shaping process", 302b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod this); 3038750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod 30430874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod const gchar *features_help = "Comma-separated list of font features\n" 3058750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3068750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " Features can be enabled or disabled, either globally or limited to\n" 3070de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod " specific character ranges. The format for specifying feature settings\n" 3080de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod " follows. All valid CSS font-feature-settings values other than 'normal'\n" 3090de25d4184d2d92b1a2ebb6fa054275aaae4c316Behdad Esfahbod " and 'inherited' are also accepted, though, not documented below.\n" 31095cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod "\n" 31195cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod " The range indices refer to the positions between Unicode characters,\n" 31295cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod " unless the --utf8-clusters is provided, in which case range indices\n" 31395cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod " refer to UTF-8 byte indices. The position before the first character\n" 31495cefdf96efe43a44133aa8a186155cf4e63e2b7Behdad Esfahbod " is always 0.\n" 315d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod "\n" 316d5300241680844f5625f32792f7dd7181ed05f9bBehdad Esfahbod " The format is Python-esque. Here is how it all works:\n" 3178750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3188750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " Syntax: Value: Start: End:\n" 3198750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3208750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " Setting value:\n" 3218750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern\" 1 0 ∞ # Turn feature on\n" 3228750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"+kern\" 1 0 ∞ # Turn feature on\n" 3238750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"-kern\" 0 0 ∞ # Turn feature off\n" 3248750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern=0\" 0 0 ∞ # Turn feature off\n" 3258750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern=1\" 1 0 ∞ # Turn feature on\n" 3268750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"aalt=2\" 2 0 ∞ # Choose 2nd alternate\n" 3278750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3288750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " Setting index:\n" 3298750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[]\" 1 0 ∞ # Turn feature on\n" 3308750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[:]\" 1 0 ∞ # Turn feature on\n" 3318750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[5:]\" 1 5 ∞ # Turn feature on, partial\n" 3328750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[:5]\" 1 0 5 # Turn feature on, partial\n" 3338750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[3:5]\" 1 3 5 # Turn feature on, range\n" 3348750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " \"kern[3]\" 1 3 3+1 # Turn feature on, single char\n" 3358750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3368750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod " Mixing it all:\n" 3378750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "\n" 3383bc22eb7b843c77706bb13fc332009097f247813Behdad Esfahbod " \"aalt[3:5]=2\" 2 3 5 # Turn 2nd alternate on for range"; 3398750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod 3408750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod GOptionEntry entries2[] = 3418750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod { 3428750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod {"features", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_features, features_help, "list"}, 3438750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod {NULL} 3448750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod }; 3458750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod parser->add_group (entries2, 3468750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "features", 3478750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod "Features options:", 34830874b4819a99cc84fa39e794266685e1b8735d2Behdad Esfahbod "Options controlling font features used", 3498750abaf8410005facbea8c886c592bead7f959bBehdad Esfahbod this); 350109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod} 3513bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 352cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbodstatic gboolean 353cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbodparse_font_size (const char *name G_GNUC_UNUSED, 354cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod const char *arg, 355cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod gpointer data, 356cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod GError **error G_GNUC_UNUSED) 357cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod{ 358cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_options_t *font_opts = (font_options_t *) data; 359cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod if (0 == strcmp (arg, "upem")) 360cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod { 361cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM; 362cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod return true; 363cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod } 364cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod switch (sscanf (arg, "%lf %lf", &font_opts->font_size_x, &font_opts->font_size_y)) { 365cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod case 1: font_opts->font_size_y = font_opts->font_size_x; 366cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod case 2: return true; 367cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod default: 368cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, 369cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod "%s argument should be one to four space-separated numbers", 370cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod name); 371cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod return false; 372cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod } 373cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod} 374109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid 375b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::add_options (option_parser_t *parser) 376109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{ 3778650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod char *text = NULL; 3788650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod 3798650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 3808650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0); 3818650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod GString *s = g_string_new (NULL); 3828650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n Supported font function implementations are: %s", 3838650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod supported_font_funcs[0].name, 3848650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod supported_font_funcs[0].name); 3858650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod for (unsigned int i = 1; i < ARRAY_LENGTH (supported_font_funcs); i++) 3868650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 3878650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod g_string_append_c (s, '/'); 3888650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod g_string_append (s, supported_font_funcs[i].name); 3898650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 3908650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod text = g_string_free (s, FALSE); 3918650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod parser->free_later (text); 3928650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 3938650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod 394cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod char *font_size_text; 395cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod if (default_font_size == FONT_SIZE_UPEM) 396cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_size_text = (char *) "Font size (default: upem)"; 397cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod else 398cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod { 399cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size); 400cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod parser->free_later (font_size_text); 401cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod } 402cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod 403109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod GOptionEntry entries[] = 404109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod { 4058650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"}, 4068650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"}, 407cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod {"font-size", 0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN, 408cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_size, font_size_text, "1/2 numbers or 'upem'"}, 4095789ca69d7464bab5fa0c5bdf404f3afaa490fafBehdad Esfahbod {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"}, 410109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod {NULL} 411109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod }; 412b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod parser->add_group (entries, 413b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "font", 414b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Font options:", 415b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Options controlling the font", 416b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod this); 417109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod} 4183bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 419109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbodvoid 420b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::add_options (option_parser_t *parser) 421109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod{ 422109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod GOptionEntry entries[] = 423109cb382898f491eed733dba4ef5ba12de94aaf6Behdad Esfahbod { 424b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod {"text", 0, 0, G_OPTION_ARG_STRING, &this->text, "Set input text", "string"}, 42578d41d8d69142ae95928b51215c0c0c5e3f5b3f0Behdad Esfahbod {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name\n\n If no text is provided, standard input is used for input.\n", "filename"}, 426321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod {"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"}, 427321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod {"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"}, 4283bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod {NULL} 4293bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod }; 430b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod parser->add_group (entries, 431b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "text", 432b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Text options:", 433b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Options controlling the input text", 434b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod this); 435b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 436b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 437b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodvoid 438b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodoutput_options_t::add_options (option_parser_t *parser) 439b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 4409815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod const char *text; 4419815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod 4429815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod if (NULL == supported_formats) 4439815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod text = "Set output format"; 4449815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod else 445ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod { 446ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod char *items = g_strjoinv ("/", const_cast<char **> (supported_formats)); 4472306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items); 448ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod g_free (items); 4492306ad46dce1c53b0b1bfabdc04d70e3b99eabb7Behdad Esfahbod parser->free_later ((char *) text); 450ea5e8a02eb83ad19f3009b0008893f77ce113118Behdad Esfahbod } 4519815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod 452b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod GOptionEntry entries[] = 453b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod { 454b5afd8f78e6b372f1bbed469329c1554adb20eeaBehdad Esfahbod {"output-file", 0, 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"}, 4559815a88111072aa51e78e258c5f660993b47d4dfBehdad Esfahbod {"output-format", 0, 0, G_OPTION_ARG_STRING, &this->output_format, text, "format"}, 456b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod {NULL} 457b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod }; 458b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod parser->add_group (entries, 459b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "output", 460b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Output options:", 461b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod "Options controlling the output", 462b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod this); 463b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 4643bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 4653bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 4663bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 467b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodhb_font_t * 468b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodfont_options_t::get_font (void) const 469b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 470b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod if (font) 471b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod return font; 472b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 473b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_blob_t *blob = NULL; 474b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 475b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod /* Create the blob */ 4763bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod { 4774451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod char *font_data; 4784451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod unsigned int len = 0; 479b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_destroy_func_t destroy; 480b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod void *user_data; 481b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_memory_mode_t mm; 482b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 4834451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod /* This is a hell of a lot of code for just reading a file! */ 484b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod if (!font_file) 4850594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (true, "No font file set"); 486b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 4874451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (0 == strcmp (font_file, "-")) { 4884451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod /* read it */ 4894451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod GString *gs = g_string_new (NULL); 4904451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod char buf[BUFSIZ]; 491e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__) 492bc76449f51f20491a4540b4f240f5a3e0dcb20feBehdad Esfahbod setmode (fileno (stdin), _O_BINARY); 4934451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod#endif 4944451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod while (!feof (stdin)) { 4954451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod size_t ret = fread (buf, 1, sizeof (buf), stdin); 4964451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (ferror (stdin)) 4970594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "Failed reading font from standard input: %s", 4984451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod strerror (errno)); 4994451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod g_string_append_len (gs, buf, ret); 5004451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } 5014451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod len = gs->len; 5020594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod font_data = g_string_free (gs, false); 5034451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod user_data = font_data; 5044451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod destroy = (hb_destroy_func_t) g_free; 5054451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod mm = HB_MEMORY_MODE_WRITABLE; 5064451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } else { 507f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod GError *error = NULL; 5080594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod GMappedFile *mf = g_mapped_file_new (font_file, false, &error); 5094451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (mf) { 5104451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod font_data = g_mapped_file_get_contents (mf); 5114451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod len = g_mapped_file_get_length (mf); 5124451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (len) { 513c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod destroy = (hb_destroy_func_t) g_mapped_file_unref; 5144451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod user_data = (void *) mf; 5154451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; 5164451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } else 517c2bc818706df56022c8bb922df2b741cb120f7e4Behdad Esfahbod g_mapped_file_unref (mf); 518f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod } else { 5190594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "%s", error->message); 520f51e167436a51b890ffe3f7f7920498fa287acd9Behdad Esfahbod //g_error_free (error); 5214451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } 5224451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (!len) { 5234451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod /* GMappedFile is buggy, it doesn't fail if file isn't regular. 5244451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod * Try reading. 5254451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */ 5264451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod GError *error = NULL; 5274451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod gsize l; 5284451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod if (g_file_get_contents (font_file, &font_data, &l, &error)) { 5294451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod len = l; 5304451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod destroy = (hb_destroy_func_t) g_free; 5314451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod user_data = (void *) font_data; 5324451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod mm = HB_MEMORY_MODE_WRITABLE; 5334451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } else { 5340594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "%s", error->message); 5354451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod //g_error_free (error); 5364451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } 5374451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } 5384451168e5d1ea26560899e9a9733b3a3f1853050Behdad Esfahbod } 539b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 540b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod blob = hb_blob_create (font_data, len, mm, user_data, destroy); 541b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod } 542b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 543b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod /* Create the face */ 544b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_face_t *face = hb_face_create (blob, face_index); 545b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_blob_destroy (blob); 546b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 547b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 548b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod font = hb_font_create (face); 549b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 550cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod if (font_size_x == FONT_SIZE_UPEM) 551cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_size_x = hb_face_get_upem (face); 552cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod if (font_size_y == FONT_SIZE_UPEM) 553cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod font_size_y = hb_face_get_upem (face); 554cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod 555cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod int scale_x = (int) scalbnf (font_size_x, subpixel_bits); 556cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod int scale_y = (int) scalbnf (font_size_y, subpixel_bits); 557cd4eb96abb90a84b42e9b288e39bad759e4411a0Behdad Esfahbod hb_font_set_scale (font, scale_x, scale_y); 558b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod hb_face_destroy (face); 559b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 5608650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod void (*set_font_funcs) (hb_font_t *) = NULL; 5618650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod if (!font_funcs) 5628650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 5638650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod set_font_funcs = supported_font_funcs[0].func; 5648650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 5658650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod else 5668650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 5678650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++) 5688650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name)) 5698650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 5708650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod set_font_funcs = supported_font_funcs[i].func; 5718650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod break; 5728650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 5738650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod if (!set_font_funcs) 5748650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 5758650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod GString *s = g_string_new (NULL); 5768650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++) 5778650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod { 5788650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod if (i) 5798650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod g_string_append_c (s, '/'); 5808650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod g_string_append (s, supported_font_funcs[i].name); 5818650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 5828650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod char *p = g_string_free (s, FALSE); 5838650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod fail (false, "Unknown font function implementation `%s'; supported values are: %s; default is %s", 5848650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod font_funcs, 5858650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod p, 5868650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod supported_font_funcs[0].name); 5878650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod //free (p); 5888650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 5898650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod } 5908650def73500204b79c651f58b1be3f94a41973dBehdad Esfahbod set_font_funcs (font); 591b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 592b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod return font; 593b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod} 594b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 595b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 596b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodconst char * 597b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbodtext_options_t::get_line (unsigned int *len) 598b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod{ 59955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (text) { 60055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (text_len == (unsigned int) -1) 60155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text_len = strlen (text); 60255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod 60355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (!text_len) { 60455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod *len = 0; 60555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod return NULL; 60655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod } 60755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod 60855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod const char *ret = text; 60955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod const char *p = (const char *) memchr (text, '\n', text_len); 61055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod unsigned int ret_len; 61155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (!p) { 61255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod ret_len = text_len; 61355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text += ret_len; 61455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text_len = 0; 61555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod } else { 61655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod ret_len = p - ret; 61755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text += ret_len + 1; 61855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text_len -= ret_len + 1; 61955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod } 62055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod 62155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod *len = ret_len; 62255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod return ret; 62355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod } 62455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod 62555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (!fp) { 626b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod if (!text_file) 6270594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (true, "At least one of text or text-file must be set"); 628b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 62955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (0 != strcmp (text_file, "-")) 63055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod fp = fopen (text_file, "r"); 63155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod else 63255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod fp = stdin; 6333bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod 63455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (!fp) 6350594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "Failed opening text file `%s': %s", 63655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod text_file, strerror (errno)); 637b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 63855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod gs = g_string_new (NULL); 639b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod } 640b9b10ad78b1f977494a3a42b58f8040fe16505a3Behdad Esfahbod 64155aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod g_string_set_size (gs, 0); 64255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod char buf[BUFSIZ]; 64355aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod while (fgets (buf, sizeof (buf), fp)) { 64455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod unsigned int bytes = strlen (buf); 64527c36af411c7c4d75dd25d79fc76dd92c6bb9643Behdad Esfahbod if (bytes && buf[bytes - 1] == '\n') { 64655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod bytes--; 64755aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod g_string_append_len (gs, buf, bytes); 64855aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod break; 64955aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod } 65055aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod g_string_append_len (gs, buf, bytes); 6513bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod } 65255aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod if (ferror (fp)) 6530594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "Failed reading text: %s", 65455aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod strerror (errno)); 65555aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod *len = gs->len; 65655aeb0490454cc1ba93a42f307ed1230f59dee4bBehdad Esfahbod return !*len && feof (fp) ? NULL : gs->str; 6573bb300ee78a40f9ded21ab19283863b733aeb677Behdad Esfahbod} 658a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod 659a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod 660a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad EsfahbodFILE * 661a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbodoutput_options_t::get_file_handle (void) 662a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod{ 663a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod if (fp) 664a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod return fp; 665a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod 666a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod if (output_file) 667a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod fp = fopen (output_file, "wb"); 668a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod else { 669e2aab4b5db88c368c11cee81368b9ad41399e585Behdad Esfahbod#if defined(_WIN32) || defined(__CYGWIN__) 670ceeae30f47b9d628484ae398bc38f3f60ee8eab4Behdad Esfahbod setmode (fileno (stdout), _O_BINARY); 671a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod#endif 672a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod fp = stdout; 673a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod } 674a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod if (!fp) 6750594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod fail (false, "Cannot open output file `%s': %s", 676a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod g_filename_display_name (output_file), strerror (errno)); 677a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod 678a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod return fp; 679a75c1b125159f6cfb6b652a9ec40803f7c7e3f71Behdad Esfahbod} 6808b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 681c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodstatic gboolean 682c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbodparse_verbose (const char *name G_GNUC_UNUSED, 683c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod const char *arg G_GNUC_UNUSED, 684c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod gpointer data G_GNUC_UNUSED, 685c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod GError **error G_GNUC_UNUSED) 686c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod{ 687c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod format_options_t *format_opts = (format_options_t *) data; 6880594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true; 6890594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod return true; 690c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod} 6918b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 6928b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid 6938b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodformat_options_t::add_options (option_parser_t *parser) 6948b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{ 6958b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod GOptionEntry entries[] = 6968b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod { 6978b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", NULL}, 6988b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod {"no-positions", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_positions, "Do not show glyph positions", NULL}, 6998b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod {"no-clusters", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_clusters, "Do not show cluster mapping", NULL}, 700cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Show input text", NULL}, 701cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Show input Unicode codepoints", NULL}, 702cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Show line numbers", NULL}, 703c1885483120d4b686b2fe95b217dce7248e040b1Behdad Esfahbod {"verbose", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose, "Show everything", NULL}, 7048b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod {NULL} 7058b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod }; 7068b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod parser->add_group (entries, 7078b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod "format", 7088b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod "Format options:", 7098b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod "Options controlling the formatting of buffer contents", 7108b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod this); 7118b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod} 7128b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod 7138b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbodvoid 714cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_unicode (hb_buffer_t *buffer, 715cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod GString *gs) 716cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod{ 717cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod unsigned int num_glyphs = hb_buffer_get_length (buffer); 718cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); 719cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod 720cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod g_string_append_c (gs, '<'); 721cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod for (unsigned int i = 0; i < num_glyphs; i++) 722cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod { 723cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod if (i) 724cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod g_string_append_c (gs, ','); 725cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod g_string_append_printf (gs, "U+%04X", info->codepoint); 726cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod info++; 727cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod } 728cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod g_string_append_c (gs, '>'); 729cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod} 730cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod 731cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodvoid 732cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbodformat_options_t::serialize_glyphs (hb_buffer_t *buffer, 733cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod hb_font_t *font, 734f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod hb_buffer_serialize_format_t output_format, 735f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod hb_buffer_serialize_flags_t flags, 736cc4d9810d6318ca2e4de3b8d62f03b51cc21ee05Behdad Esfahbod GString *gs) 7378b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod{ 738c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod g_string_append_c (gs, '['); 739f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod unsigned int num_glyphs = hb_buffer_get_length (buffer); 740f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod unsigned int start = 0; 741f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod 742f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod while (start < num_glyphs) { 743f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod char buf[1024]; 744f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod unsigned int consumed; 745f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs, 746f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod buf, sizeof (buf), &consumed, 747f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod font, output_format, flags); 748f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod if (!consumed) 749f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod break; 750f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod g_string_append (gs, buf); 7518b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod } 752c91c4fa47140c0d6191241a832fc534b1c1514ceBehdad Esfahbod g_string_append_c (gs, ']'); 7538b8b19056decaf09e4e0ccd9412ee1aeb30f4de7Behdad Esfahbod} 754cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid 755cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodformat_options_t::serialize_line_no (unsigned int line_no, 756cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod GString *gs) 757cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{ 758cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod if (show_line_num) 759cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod g_string_append_printf (gs, "%d: ", line_no); 760cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod} 761cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbodvoid 7625db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_text (hb_buffer_t *buffer, 7635db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod unsigned int line_no, 7645db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod const char *text, 7655db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod unsigned int text_len, 7665db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod hb_font_t *font, 7675db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod GString *gs) 768cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod{ 769cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod if (show_text) { 770cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod serialize_line_no (line_no, gs); 771d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod g_string_append_c (gs, '('); 772cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod g_string_append_len (gs, text, text_len); 773d8134bc017ca3383e0978ddee57070eb3aab8964Behdad Esfahbod g_string_append_c (gs, ')'); 774cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod g_string_append_c (gs, '\n'); 775cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod } 776cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod 777cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod if (show_unicode) { 778cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod serialize_line_no (line_no, gs); 779ae62166519291057316a9d15cea3f1570fcb5eafBehdad Esfahbod serialize_unicode (buffer, gs); 780cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod g_string_append_c (gs, '\n'); 781cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod } 7825db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod} 7835db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid 7845db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_message (unsigned int line_no, 7855db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod const char *msg, 7865db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod GString *gs) 7875db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{ 7885db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod serialize_line_no (line_no, gs); 7895db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod g_string_append_printf (gs, "%s", msg); 7905db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod g_string_append_c (gs, '\n'); 7915db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod} 7925db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodvoid 7935db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbodformat_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer, 7945db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod unsigned int line_no, 7955db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod const char *text, 7965db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod unsigned int text_len, 7975db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod hb_font_t *font, 798f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod hb_buffer_serialize_format_t output_format, 799f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod hb_buffer_serialize_flags_t format_flags, 8005db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod GString *gs) 8015db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod{ 802cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod serialize_line_no (line_no, gs); 803f9edf1672511c3bcc3796cc79578ffea43b2bda1Behdad Esfahbod serialize_glyphs (buffer, font, output_format, format_flags, gs); 804cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod g_string_append_c (gs, '\n'); 805cdc673d97c5ffedb386865a81f54a5cedcbad27cBehdad Esfahbod} 806