Imported Upstream version 2.6.4
[platform/upstream/harfbuzz.git] / util / options.cc
index 0adc179..43ff90a 100644 (file)
 #ifdef HAVE_FREETYPE
 #include <hb-ft.h>
 #endif
-#ifdef HAVE_OT
-#include <hb-ot-font.h>
-#endif
+#include <hb-ot.h>
+
+#define DELIMITERS "<+>{},;&#\\xXuUnNiI\n\t\v\f\r "
 
-struct supported_font_funcs_t {
+static struct supported_font_funcs_t {
        char name[4];
        void (*func) (hb_font_t *);
 } supported_font_funcs[] =
@@ -41,9 +41,7 @@ struct supported_font_funcs_t {
 #ifdef HAVE_FREETYPE
   {"ft",       hb_ft_font_set_funcs},
 #endif
-#ifdef HAVE_OT
   {"ot",       hb_ot_font_set_funcs},
-#endif
 };
 
 
@@ -55,6 +53,7 @@ fail (hb_bool_t suggest_help, const char *format, ...)
   va_list vap;
   va_start (vap, format);
   msg = g_strdup_vprintf (format, vap);
+  va_end (vap);
   const char *prgname = g_get_prgname ();
   g_printerr ("%s: %s\n", prgname, msg);
   if (suggest_help)
@@ -64,12 +63,10 @@ fail (hb_bool_t suggest_help, const char *format, ...)
 }
 
 
-hb_bool_t debug = false;
-
 static gchar *
-shapers_to_string (void)
+shapers_to_string ()
 {
-  GString *shapers = g_string_new (NULL);
+  GString *shapers = g_string_new (nullptr);
   const char **shaper_list = hb_shape_list_shapers ();
 
   for (; *shaper_list; shaper_list++) {
@@ -100,16 +97,15 @@ show_version (const char *name G_GNUC_UNUSED,
 
 
 void
-option_parser_t::add_main_options (void)
+option_parser_t::add_main_options ()
 {
   GOptionEntry entries[] =
   {
     {"version",                0, G_OPTION_FLAG_NO_ARG,
-                             G_OPTION_ARG_CALLBACK,    (gpointer) &show_version,       "Show version numbers",                 NULL},
-    {"debug",          0, 0, G_OPTION_ARG_NONE,        &debug,                         "Free all resources before exit",       NULL},
-    {NULL}
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &show_version,       "Show version numbers",                 nullptr},
+    {nullptr}
   };
-  g_option_context_add_main_entries (context, entries, NULL);
+  g_option_context_add_main_entries (context, entries, nullptr);
 }
 
 static gboolean
@@ -120,7 +116,7 @@ pre_parse (GOptionContext *context G_GNUC_UNUSED,
 {
   option_group_t *option_group = (option_group_t *) data;
   option_group->pre_parse (error);
-  return *error == NULL;
+  return *error == nullptr;
 }
 
 static gboolean
@@ -131,7 +127,7 @@ post_parse (GOptionContext *context G_GNUC_UNUSED,
 {
   option_group_t *option_group = static_cast<option_group_t *>(data);
   option_group->post_parse (error);
-  return *error == NULL;
+  return *error == nullptr;
 }
 
 void
@@ -142,7 +138,7 @@ option_parser_t::add_group (GOptionEntry   *entries,
                            option_group_t *option_group)
 {
   GOptionGroup *group = g_option_group_new (name, description, help_description,
-                                           static_cast<gpointer>(option_group), NULL);
+                                           static_cast<gpointer>(option_group), nullptr);
   g_option_group_add_entries (group, entries);
   g_option_group_set_parse_hooks (group, pre_parse, post_parse);
   g_option_context_add_group (context, group);
@@ -153,10 +149,10 @@ option_parser_t::parse (int *argc, char ***argv)
 {
   setlocale (LC_ALL, "");
 
-  GError *parse_error = NULL;
+  GError *parse_error = nullptr;
   if (!g_option_context_parse (context, argc, argv, &parse_error))
   {
-    if (parse_error != NULL) {
+    if (parse_error != nullptr) {
       fail (true, "%s", parse_error->message);
       //g_error_free (parse_error);
     } else
@@ -173,10 +169,10 @@ parse_margin (const char *name G_GNUC_UNUSED,
 {
   view_options_t *view_opts = (view_options_t *) data;
   view_options_t::margin_t &m = view_opts->margin;
-  switch (sscanf (arg, "%lf %lf %lf %lf", &m.t, &m.r, &m.b, &m.l)) {
-    case 1: m.r = m.t;
-    case 2: m.b = m.t;
-    case 3: m.l = m.r;
+  switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf", &m.t, &m.r, &m.b, &m.l)) {
+    case 1: m.r = m.t; HB_FALLTHROUGH;
+    case 2: m.b = m.t; HB_FALLTHROUGH;
+    case 3: m.l = m.r; HB_FALLTHROUGH;
     case 4: return true;
     default:
       g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
@@ -191,11 +187,29 @@ static gboolean
 parse_shapers (const char *name G_GNUC_UNUSED,
               const char *arg,
               gpointer    data,
-              GError    **error G_GNUC_UNUSED)
+              GError    **error)
 {
   shape_options_t *shape_opts = (shape_options_t *) data;
+  char **shapers = g_strsplit (arg, ",", 0);
+
+  for (char **shaper = shapers; *shaper; shaper++) {
+    bool found = false;
+    for (const char **hb_shaper = hb_shape_list_shapers (); *hb_shaper; hb_shaper++) {
+      if (strcmp (*shaper, *hb_shaper) == 0) {
+       found = true;
+       break;
+      }
+    }
+    if (!found) {
+      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                  "Unknown or unsupported shaper: %s", *shaper);
+      g_strfreev (shapers);
+      return false;
+    }
+  }
+
   g_strfreev (shape_opts->shapers);
-  shape_opts->shapers = g_strsplit (arg, ",", 0);
+  shape_opts->shapers = shapers;
   return true;
 }
 
@@ -214,9 +228,9 @@ list_shapers (const char *name G_GNUC_UNUSED,
 
 static gboolean
 parse_features (const char *name G_GNUC_UNUSED,
-               const char *arg,
-               gpointer    data,
-               GError    **error G_GNUC_UNUSED)
+               const char *arg,
+               gpointer    data,
+               GError    **error G_GNUC_UNUSED)
 {
   shape_options_t *shape_opts = (shape_options_t *) data;
   char *s = (char *) arg;
@@ -224,7 +238,7 @@ parse_features (const char *name G_GNUC_UNUSED,
 
   shape_opts->num_features = 0;
   g_free (shape_opts->features);
-  shape_opts->features = NULL;
+  shape_opts->features = nullptr;
 
   if (!*s)
     return true;
@@ -239,6 +253,8 @@ parse_features (const char *name G_GNUC_UNUSED,
   } while (p);
 
   shape_opts->features = (hb_feature_t *) calloc (shape_opts->num_features, sizeof (*shape_opts->features));
+  if (!shape_opts->features)
+    return false;
 
   /* now do the actual parsing */
   p = s;
@@ -247,9 +263,127 @@ parse_features (const char *name G_GNUC_UNUSED,
     char *end = strchr (p, ',');
     if (hb_feature_from_string (p, end ? end - p : -1, &shape_opts->features[shape_opts->num_features]))
       shape_opts->num_features++;
-    p = end ? end + 1 : NULL;
+    p = end ? end + 1 : nullptr;
+  }
+
+  return true;
+}
+
+static gboolean
+parse_variations (const char *name G_GNUC_UNUSED,
+                 const char *arg,
+                 gpointer    data,
+                 GError    **error G_GNUC_UNUSED)
+{
+  font_options_t *font_opts = (font_options_t *) data;
+  char *s = (char *) arg;
+  char *p;
+
+  font_opts->num_variations = 0;
+  g_free (font_opts->variations);
+  font_opts->variations = nullptr;
+
+  if (!*s)
+    return true;
+
+  /* count the variations first, so we can allocate memory */
+  p = s;
+  do {
+    font_opts->num_variations++;
+    p = strchr (p, ',');
+    if (p)
+      p++;
+  } while (p);
+
+  font_opts->variations = (hb_variation_t *) calloc (font_opts->num_variations, sizeof (*font_opts->variations));
+  if (!font_opts->variations)
+    return false;
+
+  /* now do the actual parsing */
+  p = s;
+  font_opts->num_variations = 0;
+  while (p && *p) {
+    char *end = strchr (p, ',');
+    if (hb_variation_from_string (p, end ? end - p : -1, &font_opts->variations[font_opts->num_variations]))
+      font_opts->num_variations++;
+    p = end ? end + 1 : nullptr;
+  }
+
+  return true;
+}
+
+static gboolean
+parse_text (const char *name G_GNUC_UNUSED,
+           const char *arg,
+           gpointer    data,
+           GError    **error G_GNUC_UNUSED)
+{
+  text_options_t *text_opts = (text_options_t *) data;
+
+  if (text_opts->text)
+  {
+    g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                "Either --text or --unicodes can be provided but not both");
+    return false;
   }
 
+  text_opts->text_len = -1;
+  text_opts->text = g_strdup (arg);
+  return true;
+}
+
+
+static gboolean
+parse_unicodes (const char *name G_GNUC_UNUSED,
+               const char *arg,
+               gpointer    data,
+               GError    **error G_GNUC_UNUSED)
+{
+  text_options_t *text_opts = (text_options_t *) data;
+
+  if (text_opts->text)
+  {
+    g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                "Either --text or --unicodes can be provided but not both");
+    return false;
+  }
+
+  GString *gs = g_string_new (nullptr);
+  if (0 == strcmp (arg, "*"))
+  {
+    g_string_append_c (gs, '*');
+  }
+  else
+  {
+
+    char *s = (char *) arg;
+    char *p;
+
+    while (s && *s)
+    {
+      while (*s && strchr (DELIMITERS, *s))
+       s++;
+      if (!*s)
+       break;
+
+      errno = 0;
+      hb_codepoint_t u = strtoul (s, &p, 16);
+      if (errno || s == p)
+      {
+       g_string_free (gs, TRUE);
+       g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                  "Failed parsing Unicode values at: '%s'", s);
+       return false;
+      }
+
+      g_string_append_unichar (gs, u);
+
+      s = p;
+    }
+  }
+
+  text_opts->text_len = gs->len;
+  text_opts->text = g_string_free (gs, FALSE);
   return true;
 }
 
@@ -259,18 +393,17 @@ view_options_t::add_options (option_parser_t *parser)
 {
   GOptionEntry entries[] =
   {
-    {"annotate",       0, 0, G_OPTION_ARG_NONE,        &this->annotate,                "Annotate output rendering",                            NULL},
+    {"annotate",       0, 0, G_OPTION_ARG_NONE,        &this->annotate,                "Annotate output rendering",                            nullptr},
     {"background",     0, 0, G_OPTION_ARG_STRING,      &this->back,                    "Set background color (default: " DEFAULT_BACK ")",     "rrggbb/rrggbbaa"},
     {"foreground",     0, 0, G_OPTION_ARG_STRING,      &this->fore,                    "Set foreground color (default: " DEFAULT_FORE ")",     "rrggbb/rrggbbaa"},
     {"line-space",     0, 0, G_OPTION_ARG_DOUBLE,      &this->line_space,              "Set space between lines (default: 0)",                 "units"},
     {"margin",         0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_margin,       "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
-    {"font-size",      0, 0, G_OPTION_ARG_DOUBLE,      &this->font_size,               "Font size (default: " G_STRINGIFY(DEFAULT_FONT_SIZE) ")","size"},
-    {NULL}
+    {nullptr}
   };
   parser->add_group (entries,
                     "view",
                     "View options:",
-                    "Options controlling output rendering",
+                    "Options for output rendering",
                     this);
 }
 
@@ -280,25 +413,29 @@ shape_options_t::add_options (option_parser_t *parser)
   GOptionEntry entries[] =
   {
     {"list-shapers",   0, G_OPTION_FLAG_NO_ARG,
-                             G_OPTION_ARG_CALLBACK,    (gpointer) &list_shapers,       "List available shapers and quit",      NULL},
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &list_shapers,       "List available shapers and quit",      nullptr},
     {"shaper",         0, G_OPTION_FLAG_HIDDEN,
-                             G_OPTION_ARG_CALLBACK,    (gpointer) &parse_shapers,      "Hidden duplicate of --shapers",        NULL},
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &parse_shapers,      "Hidden duplicate of --shapers",        nullptr},
     {"shapers",                0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_shapers,      "Set comma-separated list of shapers to try","list"},
     {"direction",      0, 0, G_OPTION_ARG_STRING,      &this->direction,               "Set text direction (default: auto)",   "ltr/rtl/ttb/btt"},
     {"language",       0, 0, G_OPTION_ARG_STRING,      &this->language,                "Set text language (default: $LANG)",   "langstr"},
     {"script",         0, 0, G_OPTION_ARG_STRING,      &this->script,                  "Set text script (default: auto)",      "ISO-15924 tag"},
-    {"bot",            0, 0, G_OPTION_ARG_NONE,        &this->bot,                     "Treat text as beginning-of-paragraph", NULL},
-    {"eot",            0, 0, G_OPTION_ARG_NONE,        &this->eot,                     "Treat text as end-of-paragraph",       NULL},
-    {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE,    &this->preserve_default_ignorables,     "Preserve Default-Ignorable characters",        NULL},
-    {"utf8-clusters",  0, 0, G_OPTION_ARG_NONE,        &this->utf8_clusters,           "Use UTF8 byte indices, not char indices",      NULL},
-    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,       &this->normalize_glyphs,        "Rearrange glyph clusters in nominal order",    NULL},
-    {"num-iterations", 0, 0, G_OPTION_ARG_INT,         &this->num_iterations,          "Run shaper N times (default: 1)",      "N"},
-    {NULL}
+    {"bot",            0, 0, G_OPTION_ARG_NONE,        &this->bot,                     "Treat text as beginning-of-paragraph", nullptr},
+    {"eot",            0, 0, G_OPTION_ARG_NONE,        &this->eot,                     "Treat text as end-of-paragraph",       nullptr},
+    {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE,    &this->preserve_default_ignorables,     "Preserve Default-Ignorable characters",        nullptr},
+    {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE,      &this->remove_default_ignorables,       "Remove Default-Ignorable characters",  nullptr},
+    {"invisible-glyph",        0, 0, G_OPTION_ARG_INT,         &this->invisible_glyph,         "Glyph value to replace Default-Ignorables with",       nullptr},
+    {"utf8-clusters",  0, 0, G_OPTION_ARG_NONE,        &this->utf8_clusters,           "Use UTF8 byte indices, not char indices",      nullptr},
+    {"cluster-level",  0, 0, G_OPTION_ARG_INT,         &this->cluster_level,           "Cluster merging level (default: 0)",   "0/1/2"},
+    {"normalize-glyphs",0, 0, G_OPTION_ARG_NONE,       &this->normalize_glyphs,        "Rearrange glyph clusters in nominal order",    nullptr},
+    {"verify",         0, 0, G_OPTION_ARG_NONE,        &this->verify,                  "Perform sanity checks on shaping results",     nullptr},
+    {"num-iterations", 'n', 0, G_OPTION_ARG_INT,               &this->num_iterations,          "Run shaper N times (default: 1)",      "N"},
+    {nullptr}
   };
   parser->add_group (entries,
                     "shape",
                     "Shape options:",
-                    "Options controlling the shaping process",
+                    "Options for the shaping process",
                     this);
 
   const gchar *features_help = "Comma-separated list of font features\n"
@@ -306,7 +443,8 @@ shape_options_t::add_options (option_parser_t *parser)
     "    Features can be enabled or disabled, either globally or limited to\n"
     "    specific character ranges.  The format for specifying feature settings\n"
     "    follows.  All valid CSS font-feature-settings values other than 'normal'\n"
-    "    and 'inherited' are also accepted, though, not documented below.\n"
+    "    and the global values are also accepted, though not documented below.\n"
+    "    CSS string escapes are not supported."
     "\n"
     "    The range indices refer to the positions between Unicode characters,\n"
     "    unless the --utf8-clusters is provided, in which case range indices\n"
@@ -340,23 +478,65 @@ shape_options_t::add_options (option_parser_t *parser)
   GOptionEntry entries2[] =
   {
     {"features",       0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_features,     features_help,  "list"},
-    {NULL}
+    {nullptr}
   };
   parser->add_group (entries2,
                     "features",
                     "Features options:",
-                    "Options controlling font features used",
+                    "Options for font features used",
                     this);
 }
 
+static gboolean
+parse_font_size (const char *name G_GNUC_UNUSED,
+                const char *arg,
+                gpointer    data,
+                GError    **error G_GNUC_UNUSED)
+{
+  font_options_t *font_opts = (font_options_t *) data;
+  if (0 == strcmp (arg, "upem"))
+  {
+    font_opts->font_size_y = font_opts->font_size_x = FONT_SIZE_UPEM;
+    return true;
+  }
+  switch (sscanf (arg, "%lf%*[ ,]%lf", &font_opts->font_size_x, &font_opts->font_size_y)) {
+    case 1: font_opts->font_size_y = font_opts->font_size_x; HB_FALLTHROUGH;
+    case 2: return true;
+    default:
+      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                  "%s argument should be one or two space-separated numbers",
+                  name);
+      return false;
+  }
+}
+
+static gboolean
+parse_font_ppem (const char *name G_GNUC_UNUSED,
+                const char *arg,
+                gpointer    data,
+                GError    **error G_GNUC_UNUSED)
+{
+  font_options_t *font_opts = (font_options_t *) data;
+  switch (sscanf (arg, "%d%*[ ,]%d", &font_opts->x_ppem, &font_opts->y_ppem)) {
+    case 1: font_opts->y_ppem = font_opts->x_ppem; HB_FALLTHROUGH;
+    case 2: return true;
+    default:
+      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                  "%s argument should be one or two space-separated numbers",
+                  name);
+      return false;
+  }
+}
+
 void
 font_options_t::add_options (option_parser_t *parser)
 {
-  char *text = NULL;
+  char *text = nullptr;
 
   {
-    ASSERT_STATIC (ARRAY_LENGTH_CONST (supported_font_funcs) > 0);
-    GString *s = g_string_new (NULL);
+    static_assert ((ARRAY_LENGTH_CONST (supported_font_funcs) > 0),
+                  "No supported font-funcs found.");
+    GString *s = g_string_new (nullptr);
     g_string_printf (s, "Set font functions implementation to use (default: %s)\n\n    Supported font function implementations are: %s",
                     supported_font_funcs[0].name,
                     supported_font_funcs[0].name);
@@ -369,17 +549,54 @@ font_options_t::add_options (option_parser_t *parser)
     parser->free_later (text);
   }
 
+  char *font_size_text;
+  if (default_font_size == FONT_SIZE_UPEM)
+    font_size_text = (char *) "Font size (default: upem)";
+  else
+  {
+    font_size_text = g_strdup_printf ("Font size (default: %d)", default_font_size);
+    parser->free_later (font_size_text);
+  }
+
   GOptionEntry entries[] =
   {
-    {"font-file",      0, 0, G_OPTION_ARG_STRING,      &this->font_file,               "Set font file-name",                   "filename"},
-    {"face-index",     0, 0, G_OPTION_ARG_INT,         &this->face_index,              "Set face index (default: 0)",          "index"},
-    {"font-funcs",     0, 0, G_OPTION_ARG_STRING,      &this->font_funcs,              text,                                   "format"},
-    {NULL}
+    {"font-file",      0, 0, G_OPTION_ARG_STRING,      &this->font_file,               "Set font file-name",                           "filename"},
+    {"face-index",     0, 0, G_OPTION_ARG_INT,         &this->face_index,              "Set face index (default: 0)",                  "index"},
+    {"font-size",      0, default_font_size ? 0 : G_OPTION_FLAG_HIDDEN,
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &parse_font_size,    font_size_text,                                 "1/2 integers or 'upem'"},
+    {"font-ppem",      0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_font_ppem,    "Set x,y pixels per EM (default: 0; disabled)", "1/2 integers"},
+    {"font-ptem",      0, 0, G_OPTION_ARG_DOUBLE,      &this->ptem,                    "Set font point-size (default: 0; disabled)",   "point-size"},
+    {"font-funcs",     0, 0, G_OPTION_ARG_STRING,      &this->font_funcs,              text,                                           "impl"},
+    {"ft-load-flags",  0, 0, G_OPTION_ARG_INT,         &this->ft_load_flags,           "Set FreeType load-flags (default: 2)",         "integer"},
+    {nullptr}
   };
   parser->add_group (entries,
                     "font",
                     "Font options:",
-                    "Options controlling the font",
+                    "Options for the font",
+                    this);
+
+  const gchar *variations_help = "Comma-separated list of font variations\n"
+    "\n"
+    "    Variations are set globally. The format for specifying variation settings\n"
+    "    follows.  All valid CSS font-variation-settings values other than 'normal'\n"
+    "    and 'inherited' are also accepted, though, not documented below.\n"
+    "\n"
+    "    The format is a tag, optionally followed by an equals sign, followed by a\n"
+    "    number. For example:\n"
+    "\n"
+    "      \"wght=500\"\n"
+    "      \"slnt=-7.5\"\n";
+
+  GOptionEntry entries2[] =
+  {
+    {"variations",     0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_variations,   variations_help,        "list"},
+    {nullptr}
+  };
+  parser->add_group (entries2,
+                    "variations",
+                    "Variations options:",
+                    "Options for font variations used",
                     this);
 }
 
@@ -388,16 +605,17 @@ text_options_t::add_options (option_parser_t *parser)
 {
   GOptionEntry entries[] =
   {
-    {"text",           0, 0, G_OPTION_ARG_STRING,      &this->text,                    "Set input text",                       "string"},
+    {"text",           0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_text,         "Set input text",                       "string"},
     {"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"},
+    {"unicodes",      'u', 0, G_OPTION_ARG_CALLBACK,   (gpointer) &parse_unicodes,             "Set input Unicode codepoints",         "list of hex numbers"},
     {"text-before",    0, 0, G_OPTION_ARG_STRING,      &this->text_before,             "Set text context before each line",    "string"},
     {"text-after",     0, 0, G_OPTION_ARG_STRING,      &this->text_after,              "Set text context after each line",     "string"},
-    {NULL}
+    {nullptr}
   };
   parser->add_group (entries,
                     "text",
                     "Text options:",
-                    "Options controlling the input text",
+                    "Options for the input text",
                     this);
 }
 
@@ -406,8 +624,8 @@ output_options_t::add_options (option_parser_t *parser)
 {
   const char *text;
 
-  if (NULL == supported_formats)
-    text = "Set output format";
+  if (nullptr == supported_formats)
+    text = "Set output serialization format";
   else
   {
     char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
@@ -418,95 +636,46 @@ output_options_t::add_options (option_parser_t *parser)
 
   GOptionEntry entries[] =
   {
-    {"output-file",    0, 0, G_OPTION_ARG_STRING,      &this->output_file,             "Set output file-name (default: stdout)","filename"},
-    {"output-format",  0, 0, G_OPTION_ARG_STRING,      &this->output_format,           text,                                   "format"},
-    {NULL}
+    {"output-file",   'o', 0, G_OPTION_ARG_STRING,     &this->output_file,             "Set output file-name (default: stdout)","filename"},
+    {"output-format", 'O', 0, G_OPTION_ARG_STRING,     &this->output_format,           text,                                   "format"},
+    {nullptr}
   };
   parser->add_group (entries,
                     "output",
-                    "Output options:",
-                    "Options controlling the output",
+                    "Output destination & format options:",
+                    "Options for the destination & form of the output",
                     this);
 }
 
 
 
 hb_font_t *
-font_options_t::get_font (void) const
+font_options_t::get_font () const
 {
   if (font)
     return font;
 
-  hb_blob_t *blob = NULL;
-
   /* Create the blob */
+  if (!font_file)
+    fail (true, "No font file set");
+
+  const char *font_path = font_file;
+
+  if (0 == strcmp (font_path, "-"))
   {
-    char *font_data;
-    unsigned int len = 0;
-    hb_destroy_func_t destroy;
-    void *user_data;
-    hb_memory_mode_t mm;
-
-    /* This is a hell of a lot of code for just reading a file! */
-    if (!font_file)
-      fail (true, "No font file set");
-
-    if (0 == strcmp (font_file, "-")) {
-      /* read it */
-      GString *gs = g_string_new (NULL);
-      char buf[BUFSIZ];
 #if defined(_WIN32) || defined(__CYGWIN__)
-      setmode (fileno (stdin), _O_BINARY);
+    setmode (fileno (stdin), O_BINARY);
+    font_path = "STDIN";
+#else
+    font_path = "/dev/stdin";
 #endif
-      while (!feof (stdin)) {
-       size_t ret = fread (buf, 1, sizeof (buf), stdin);
-       if (ferror (stdin))
-         fail (false, "Failed reading font from standard input: %s",
-               strerror (errno));
-       g_string_append_len (gs, buf, ret);
-      }
-      len = gs->len;
-      font_data = g_string_free (gs, false);
-      user_data = font_data;
-      destroy = (hb_destroy_func_t) g_free;
-      mm = HB_MEMORY_MODE_WRITABLE;
-    } else {
-      GError *error = NULL;
-      GMappedFile *mf = g_mapped_file_new (font_file, false, &error);
-      if (mf) {
-       font_data = g_mapped_file_get_contents (mf);
-       len = g_mapped_file_get_length (mf);
-       if (len) {
-         destroy = (hb_destroy_func_t) g_mapped_file_unref;
-         user_data = (void *) mf;
-         mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE;
-       } else
-         g_mapped_file_unref (mf);
-      } else {
-       fail (false, "%s", error->message);
-       //g_error_free (error);
-      }
-      if (!len) {
-       /* GMappedFile is buggy, it doesn't fail if file isn't regular.
-        * Try reading.
-        * https://bugzilla.gnome.org/show_bug.cgi?id=659212 */
-        GError *error = NULL;
-       gsize l;
-       if (g_file_get_contents (font_file, &font_data, &l, &error)) {
-         len = l;
-         destroy = (hb_destroy_func_t) g_free;
-         user_data = (void *) font_data;
-         mm = HB_MEMORY_MODE_WRITABLE;
-       } else {
-         fail (false, "%s", error->message);
-         //g_error_free (error);
-       }
-      }
-    }
-
-    blob = hb_blob_create (font_data, len, mm, user_data, destroy);
   }
 
+  blob = hb_blob_create_from_file (font_path);
+
+  if (blob == hb_blob_get_empty ())
+    fail (false, "Couldn't read or find %s, or it was empty.", font_path);
+
   /* Create the face */
   hb_face_t *face = hb_face_create (blob, face_index);
   hb_blob_destroy (blob);
@@ -514,11 +683,22 @@ font_options_t::get_font (void) const
 
   font = hb_font_create (face);
 
-  unsigned int upem = hb_face_get_upem (face);
-  hb_font_set_scale (font, upem, upem);
+  if (font_size_x == FONT_SIZE_UPEM)
+    font_size_x = hb_face_get_upem (face);
+  if (font_size_y == FONT_SIZE_UPEM)
+    font_size_y = hb_face_get_upem (face);
+
+  hb_font_set_ppem (font, x_ppem, y_ppem);
+  hb_font_set_ptem (font, ptem);
+
+  int scale_x = (int) scalbnf (font_size_x, subpixel_bits);
+  int scale_y = (int) scalbnf (font_size_y, subpixel_bits);
+  hb_font_set_scale (font, scale_x, scale_y);
   hb_face_destroy (face);
 
-  void (*set_font_funcs) (hb_font_t *) = NULL;
+  hb_font_set_variations (font, variations, num_variations);
+
+  void (*set_font_funcs) (hb_font_t *) = nullptr;
   if (!font_funcs)
   {
     set_font_funcs = supported_font_funcs[0].func;
@@ -526,17 +706,17 @@ font_options_t::get_font (void) const
   else
   {
     for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
-      if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name))
+      if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name))
       {
        set_font_funcs = supported_font_funcs[i].func;
        break;
       }
     if (!set_font_funcs)
     {
-      GString *s = g_string_new (NULL);
+      GString *s = g_string_new (nullptr);
       for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++)
       {
-        if (i)
+       if (i)
          g_string_append_c (s, '/');
        g_string_append (s, supported_font_funcs[i].name);
       }
@@ -549,6 +729,9 @@ font_options_t::get_font (void) const
     }
   }
   set_font_funcs (font);
+#ifdef HAVE_FREETYPE
+  hb_ft_font_set_load_flags (font, ft_load_flags);
+#endif
 
   return font;
 }
@@ -558,25 +741,30 @@ const char *
 text_options_t::get_line (unsigned int *len)
 {
   if (text) {
-    if (text_len == (unsigned int) -1)
-      text_len = strlen (text);
+    if (!line)
+    {
+      line = text;
+      line_len = text_len;
+    }
+    if (line_len == (unsigned int) -1)
+      line_len = strlen (line);
 
-    if (!text_len) {
+    if (!line_len) {
       *len = 0;
-      return NULL;
+      return nullptr;
     }
 
-    const char *ret = text;
-    const char *p = (const char *) memchr (text, '\n', text_len);
+    const char *ret = line;
+    const char *p = (const char *) memchr (line, '\n', line_len);
     unsigned int ret_len;
     if (!p) {
-      ret_len = text_len;
-      text += ret_len;
-      text_len = 0;
+      ret_len = line_len;
+      line += ret_len;
+      line_len = 0;
     } else {
       ret_len = p - ret;
-      text += ret_len + 1;
-      text_len -= ret_len + 1;
+      line += ret_len + 1;
+      line_len -= ret_len + 1;
     }
 
     *len = ret_len;
@@ -596,7 +784,7 @@ text_options_t::get_line (unsigned int *len)
       fail (false, "Failed opening text file `%s': %s",
            text_file, strerror (errno));
 
-    gs = g_string_new (NULL);
+    gs = g_string_new (nullptr);
   }
 
   g_string_set_size (gs, 0);
@@ -614,12 +802,12 @@ text_options_t::get_line (unsigned int *len)
     fail (false, "Failed reading text: %s",
          strerror (errno));
   *len = gs->len;
-  return !*len && feof (fp) ? NULL : gs->str;
+  return !*len && feof (fp) ? nullptr : gs->str;
 }
 
 
 FILE *
-output_options_t::get_file_handle (void)
+output_options_t::get_file_handle ()
 {
   if (fp)
     return fp;
@@ -628,7 +816,7 @@ output_options_t::get_file_handle (void)
     fp = fopen (output_file, "wb");
   else {
 #if defined(_WIN32) || defined(__CYGWIN__)
-    setmode (fileno (stdout), _O_BINARY);
+    setmode (fileno (stdout), O_BINARY);
 #endif
     fp = stdout;
   }
@@ -650,24 +838,49 @@ parse_verbose (const char *name G_GNUC_UNUSED,
   return true;
 }
 
+static gboolean
+parse_ned (const char *name G_GNUC_UNUSED,
+          const char *arg G_GNUC_UNUSED,
+          gpointer    data G_GNUC_UNUSED,
+          GError    **error G_GNUC_UNUSED)
+{
+  format_options_t *format_opts = (format_options_t *) data;
+  format_opts->show_clusters = format_opts->show_advances = false;
+  return true;
+}
+
 void
 format_options_t::add_options (option_parser_t *parser)
 {
   GOptionEntry entries[] =
   {
-    {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,    &this->show_glyph_names,        "Use glyph indices instead of names",   NULL},
-    {"no-positions",   0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,    &this->show_positions,          "Do not show glyph positions",          NULL},
-    {"no-clusters",    0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,    &this->show_clusters,           "Do not show cluster mapping",          NULL},
-    {"show-text",      0, 0,                     G_OPTION_ARG_NONE,    &this->show_text,               "Show input text",                      NULL},
-    {"show-unicode",   0, 0,                     G_OPTION_ARG_NONE,    &this->show_unicode,            "Show input Unicode codepoints",        NULL},
-    {"show-line-num",  0, 0,                     G_OPTION_ARG_NONE,    &this->show_line_num,           "Show line numbers",                    NULL},
-    {"verbose",                0, G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,(gpointer) &parse_verbose,      "Show everything",                      NULL},
-    {NULL}
+    {"show-text",      0, 0, G_OPTION_ARG_NONE,        &this->show_text,               "Prefix each line of output with its corresponding input text",         nullptr},
+    {"show-unicode",   0, 0, G_OPTION_ARG_NONE,        &this->show_unicode,            "Prefix each line of output with its corresponding input codepoint(s)", nullptr},
+    {"show-line-num",  0, 0, G_OPTION_ARG_NONE,        &this->show_line_num,           "Prefix each line of output with its corresponding input line number",  nullptr},
+    {"verbose",              'v', G_OPTION_FLAG_NO_ARG,
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &parse_verbose,      "Prefix each line of output with all of the above",                     nullptr},
+    {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE,
+                             G_OPTION_ARG_NONE,        &this->show_glyph_names,        "Output glyph indices instead of names",                                nullptr},
+    {"no-positions",   0, G_OPTION_FLAG_REVERSE,
+                             G_OPTION_ARG_NONE,        &this->show_positions,          "Do not output glyph positions",                                        nullptr},
+    {"no-advances",    0, G_OPTION_FLAG_REVERSE,
+                             G_OPTION_ARG_NONE,        &this->show_advances,           "Do not output glyph advances",                                         nullptr},
+    {"no-clusters",    0, G_OPTION_FLAG_REVERSE,
+                             G_OPTION_ARG_NONE,        &this->show_clusters,           "Do not output cluster indices",                                        nullptr},
+    {"show-extents",   0, 0, G_OPTION_ARG_NONE,        &this->show_extents,            "Output glyph extents",                                                 nullptr},
+    {"show-flags",     0, 0, G_OPTION_ARG_NONE,        &this->show_flags,              "Output glyph flags",                                                   nullptr},
+    {"ned",          'v', G_OPTION_FLAG_NO_ARG,
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &parse_ned,          "No Extra Data; Do not output clusters or advances",                    nullptr},
+    {"trace",        'V', 0, G_OPTION_ARG_NONE,        &this->trace,                   "Output interim shaping results",                                       nullptr},
+    {nullptr}
   };
   parser->add_group (entries,
-                    "format",
-                    "Format options:",
-                    "Options controlling the formatting of buffer contents",
+                    "output-syntax",
+                    "Output syntax:\n"
+        "    text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
+        "    json: [{\"g\": <glyph name or index>, \"ax\": <horizontal advance>, \"ay\": <vertical advance>, \"dx\": <horizontal displacement>, \"dy\": <vertical displacement>, \"cl\": <glyph cluster index within input>}, ...]\n"
+        "\nOutput syntax options:",
+                    "Options for the syntax of the output",
                     this);
 }
 
@@ -676,7 +889,7 @@ format_options_t::serialize_unicode (hb_buffer_t *buffer,
                                     GString     *gs)
 {
   unsigned int num_glyphs = hb_buffer_get_length (buffer);
-  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
+  hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
 
   g_string_append_c (gs, '<');
   for (unsigned int i = 0; i < num_glyphs; i++)
@@ -700,7 +913,8 @@ format_options_t::serialize_glyphs (hb_buffer_t *buffer,
   unsigned int num_glyphs = hb_buffer_get_length (buffer);
   unsigned int start = 0;
 
-  while (start < num_glyphs) {
+  while (start < num_glyphs)
+  {
     char buf[1024];
     unsigned int consumed;
     start += hb_buffer_serialize_glyphs (buffer, start, num_glyphs,
@@ -727,7 +941,8 @@ format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
                                            hb_font_t    *font,
                                            GString      *gs)
 {
-  if (show_text) {
+  if (show_text)
+  {
     serialize_line_no (line_no, gs);
     g_string_append_c (gs, '(');
     g_string_append_len (gs, text, text_len);
@@ -735,7 +950,8 @@ format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
     g_string_append_c (gs, '\n');
   }
 
-  if (show_unicode) {
+  if (show_unicode)
+  {
     serialize_line_no (line_no, gs);
     serialize_unicode (buffer, gs);
     g_string_append_c (gs, '\n');
@@ -743,11 +959,12 @@ format_options_t::serialize_buffer_of_text (hb_buffer_t  *buffer,
 }
 void
 format_options_t::serialize_message (unsigned int  line_no,
+                                    const char   *type,
                                     const char   *msg,
                                     GString      *gs)
 {
   serialize_line_no (line_no, gs);
-  g_string_append_printf (gs, "%s", msg);
+  g_string_append_printf (gs, "%s: %s", type, msg);
   g_string_append_c (gs, '\n');
 }
 void