Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / util / options.hh
index 9ed4fd0..467350a 100644 (file)
 #ifndef OPTIONS_HH
 #define OPTIONS_HH
 
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include "hb-private.hh"
 
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
 #include <stdio.h>
+#include <assert.h>
 #include <math.h>
 #include <locale.h>
 #include <errno.h>
 # define g_mapped_file_unref g_mapped_file_free
 #endif
 
-
-/* A few macros copied from hb-private.hh. */
-
-#if __GNUC__ >= 4
-#define HB_UNUSED      __attribute__((unused))
-#else
-#define HB_UNUSED
-#endif
-
-#undef MIN
-template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
-
-#undef MAX
-template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
-
-#undef  ARRAY_LENGTH
-template <typename Type, unsigned int n>
-static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
-/* A const version, but does not detect erratically being called on pointers. */
-#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
-
-#define _ASSERT_STATIC1(_line, _cond)  HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
-#define _ASSERT_STATIC0(_line, _cond)  _ASSERT_STATIC1 (_line, (_cond))
-#define ASSERT_STATIC(_cond)           _ASSERT_STATIC0 (__LINE__, (_cond))
-
-
 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
 
-
 extern hb_bool_t debug;
 
 struct option_group_t
@@ -110,7 +81,7 @@ struct option_parser_t
   }
   ~option_parser_t (void) {
     g_option_context_free (context);
-    g_ptr_array_foreach (to_free, (GFunc) g_free, NULL);
+    g_ptr_array_foreach (to_free, (GFunc) g_free, nullptr);
     g_ptr_array_free (to_free, TRUE);
   }
 
@@ -150,8 +121,8 @@ struct view_options_t : option_group_t
 {
   view_options_t (option_parser_t *parser) {
     annotate = false;
-    fore = NULL;
-    back = NULL;
+    fore = nullptr;
+    back = nullptr;
     line_space = 0;
     margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
 
@@ -179,14 +150,15 @@ struct shape_options_t : option_group_t
 {
   shape_options_t (option_parser_t *parser)
   {
-    direction = language = script = NULL;
-    bot = eot = preserve_default_ignorables = false;
-    features = NULL;
+    direction = language = script = nullptr;
+    bot = eot = preserve_default_ignorables = remove_default_ignorables = false;
+    features = nullptr;
     num_features = 0;
-    shapers = NULL;
+    shapers = nullptr;
     utf8_clusters = false;
     cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
     normalize_glyphs = false;
+    verify = false;
     num_iterations = 1;
 
     add_options (parser);
@@ -207,14 +179,26 @@ struct shape_options_t : option_group_t
     hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
     hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
     hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
-    hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_DEFAULT |
-                        (bot ? HB_BUFFER_FLAG_BOT : 0) |
-                        (eot ? HB_BUFFER_FLAG_EOT : 0) |
-                        (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0)));
+    hb_buffer_set_flags (buffer, (hb_buffer_flags_t)
+                                (HB_BUFFER_FLAG_DEFAULT |
+                                 (bot ? HB_BUFFER_FLAG_BOT : 0) |
+                                 (eot ? HB_BUFFER_FLAG_EOT : 0) |
+                                 (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
+                                 (remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
+                                 0));
     hb_buffer_set_cluster_level (buffer, cluster_level);
     hb_buffer_guess_segment_properties (buffer);
   }
 
+  static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src)
+  {
+    hb_segment_properties_t props;
+    hb_buffer_get_segment_properties (src, &props);
+    hb_buffer_set_segment_properties (dst, &props);
+    hb_buffer_set_flags (dst, hb_buffer_get_flags (src));
+    hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src));
+  }
+
   void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
                        const char *text_before, const char *text_after)
   {
@@ -232,7 +216,7 @@ struct shape_options_t : option_group_t
       /* Reset cluster values to refer to Unicode character index
        * instead of UTF-8 index. */
       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);
       for (unsigned int i = 0; i < num_glyphs; i++)
       {
        info->cluster = i;
@@ -243,12 +227,188 @@ struct shape_options_t : option_group_t
     setup_buffer (buffer);
   }
 
-  hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer)
+  hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
   {
-    hb_bool_t res = hb_shape_full (font, buffer, features, num_features, shapers);
+    hb_buffer_t *text_buffer = nullptr;
+    if (verify)
+    {
+      text_buffer = hb_buffer_create ();
+      hb_buffer_append (text_buffer, buffer, 0, -1);
+    }
+
+    if (!hb_shape_full (font, buffer, features, num_features, shapers))
+    {
+      if (error)
+        *error = "all shapers failed.";
+      goto fail;
+    }
+
     if (normalize_glyphs)
       hb_buffer_normalize_glyphs (buffer);
-    return res;
+
+    if (verify && !verify_buffer (buffer, text_buffer, font, error))
+      goto fail;
+
+    if (text_buffer)
+      hb_buffer_destroy (text_buffer);
+
+    return true;
+
+  fail:
+    if (text_buffer)
+      hb_buffer_destroy (text_buffer);
+
+    return false;
+  }
+
+  bool verify_buffer (hb_buffer_t  *buffer,
+                     hb_buffer_t  *text_buffer,
+                     hb_font_t    *font,
+                     const char  **error=nullptr)
+  {
+    if (!verify_buffer_monotone (buffer, error))
+      return false;
+    if (!verify_buffer_safe_to_break (buffer, text_buffer, font, error))
+      return false;
+    return true;
+  }
+
+  bool verify_buffer_monotone (hb_buffer_t *buffer, const char **error=nullptr)
+  {
+    /* Check that clusters are monotone. */
+    if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES ||
+       cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
+    {
+      bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
+
+      unsigned int num_glyphs;
+      hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
+
+      for (unsigned int i = 1; i < num_glyphs; i++)
+       if (info[i-1].cluster != info[i].cluster &&
+           (info[i-1].cluster < info[i].cluster) != is_forward)
+       {
+         if (error)
+           *error = "clusters are not monotone.";
+         return false;
+       }
+    }
+
+    return true;
+  }
+
+  bool verify_buffer_safe_to_break (hb_buffer_t  *buffer,
+                                   hb_buffer_t  *text_buffer,
+                                   hb_font_t    *font,
+                                   const char  **error=nullptr)
+  {
+    if (cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&
+       cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
+    {
+      /* Cannot perform this check without monotone clusters.
+       * Then again, unsafe-to-break flag is much harder to use without
+       * monotone clusters. */
+      return true;
+    }
+
+    /* Check that breaking up shaping at safe-to-break is indeed safe. */
+
+    hb_buffer_t *fragment = hb_buffer_create ();
+    hb_buffer_t *reconstruction = hb_buffer_create ();
+    copy_buffer_properties (reconstruction, buffer);
+
+    unsigned int num_glyphs;
+    hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
+
+    unsigned int num_chars;
+    hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
+
+    /* Chop text and shape fragments. */
+    bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
+    unsigned int start = 0;
+    unsigned int text_start = forward ? 0 : num_chars;
+    unsigned int text_end = text_start;
+    for (unsigned int end = 1; end < num_glyphs + 1; end++)
+    {
+      if (end < num_glyphs &&
+         (info[end].cluster == info[end-1].cluster ||
+          info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))
+         continue;
+
+      /* Shape segment corresponding to glyphs start..end. */
+      if (end == num_glyphs)
+      {
+        if (forward)
+         text_end = num_chars;
+       else
+         text_start = 0;
+      }
+      else
+      {
+       if (forward)
+       {
+         unsigned int cluster = info[end].cluster;
+         while (text_end < num_chars && text[text_end].cluster < cluster)
+           text_end++;
+       }
+       else
+       {
+         unsigned int cluster = info[end - 1].cluster;
+         while (text_start && text[text_start - 1].cluster >= cluster)
+           text_start--;
+       }
+      }
+      assert (text_start < text_end);
+
+      if (0)
+       printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);
+
+      hb_buffer_clear_contents (fragment);
+      copy_buffer_properties (fragment, buffer);
+
+      /* TODO: Add pre/post context text. */
+      hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
+      if (0 < text_start)
+        flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
+      if (text_end < num_chars)
+        flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
+      hb_buffer_set_flags (fragment, flags);
+
+      hb_buffer_append (fragment, text_buffer, text_start, text_end);
+      if (!hb_shape_full (font, fragment, features, num_features, shapers))
+      {
+       if (error)
+         *error = "all shapers failed while shaping fragment.";
+       hb_buffer_destroy (reconstruction);
+       hb_buffer_destroy (fragment);
+       return false;
+      }
+      hb_buffer_append (reconstruction, fragment, 0, -1);
+
+      start = end;
+      if (forward)
+       text_start = text_end;
+      else
+       text_end = text_start;
+    }
+
+    bool ret = true;
+    hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
+    if (diff)
+    {
+      if (error)
+       *error = "Safe-to-break test failed.";
+      ret = false;
+
+      /* Return the reconstructed result instead so it can be inspected. */
+      hb_buffer_set_length (buffer, 0);
+      hb_buffer_append (buffer, reconstruction, 0, -1);
+    }
+
+    hb_buffer_destroy (reconstruction);
+    hb_buffer_destroy (fragment);
+
+    return ret;
   }
 
   void shape_closure (const char *text, int text_len,
@@ -270,6 +430,7 @@ struct shape_options_t : option_group_t
   hb_bool_t bot;
   hb_bool_t eot;
   hb_bool_t preserve_default_ignorables;
+  hb_bool_t remove_default_ignorables;
 
   hb_feature_t *features;
   unsigned int num_features;
@@ -277,6 +438,7 @@ struct shape_options_t : option_group_t
   hb_bool_t utf8_clusters;
   hb_buffer_cluster_level_t cluster_level;
   hb_bool_t normalize_glyphs;
+  hb_bool_t verify;
   unsigned int num_iterations;
 };
 
@@ -287,16 +449,19 @@ struct font_options_t : option_group_t
                  int default_font_size_,
                  unsigned int subpixel_bits_)
   {
-    variations = NULL;
+    variations = nullptr;
     num_variations = 0;
     default_font_size = default_font_size_;
+    x_ppem = 0;
+    y_ppem = 0;
+    ptem = 0.;
     subpixel_bits = subpixel_bits_;
-    font_file = NULL;
+    font_file = nullptr;
     face_index = 0;
     font_size_x = font_size_y = default_font_size;
-    font_funcs = NULL;
+    font_funcs = nullptr;
 
-    font = NULL;
+    font = nullptr;
 
     add_options (parser);
   }
@@ -316,6 +481,9 @@ struct font_options_t : option_group_t
   hb_variation_t *variations;
   unsigned int num_variations;
   int default_font_size;
+  int x_ppem;
+  int y_ppem;
+  double ptem;
   unsigned int subpixel_bits;
   mutable double font_size_x;
   mutable double font_size_y;
@@ -329,15 +497,15 @@ struct font_options_t : option_group_t
 struct text_options_t : option_group_t
 {
   text_options_t (option_parser_t *parser) {
-    text_before = NULL;
-    text_after = NULL;
+    text_before = nullptr;
+    text_after = nullptr;
 
-    text = NULL;
-    text_file = NULL;
+    text = nullptr;
+    text_file = nullptr;
 
-    fp = NULL;
-    gs = NULL;
-    line = NULL;
+    fp = nullptr;
+    gs = nullptr;
+    line = nullptr;
     line_len = (unsigned int) -1;
 
     add_options (parser);
@@ -380,13 +548,13 @@ struct text_options_t : option_group_t
 struct output_options_t : option_group_t
 {
   output_options_t (option_parser_t *parser,
-                   const char **supported_formats_ = NULL) {
-    output_file = NULL;
-    output_format = NULL;
+                   const char **supported_formats_ = nullptr) {
+    output_file = nullptr;
+    output_format = nullptr;
     supported_formats = supported_formats_;
     explicit_output_format = false;
 
-    fp = NULL;
+    fp = nullptr;
 
     add_options (parser);
   }
@@ -414,7 +582,7 @@ struct output_options_t : option_group_t
     }
 
     if (output_file && 0 == strcmp (output_file, "-"))
-      output_file = NULL; /* STDOUT */
+      output_file = nullptr; /* STDOUT */
   }
 
   FILE *get_file_handle (void);
@@ -432,11 +600,14 @@ struct format_options_t : option_group_t
   format_options_t (option_parser_t *parser) {
     show_glyph_names = true;
     show_positions = true;
+    show_advances = true;
     show_clusters = true;
     show_text = false;
     show_unicode = false;
     show_line_num = false;
     show_extents = false;
+    show_flags = false;
+    trace = false;
 
     add_options (parser);
   }
@@ -459,6 +630,7 @@ struct format_options_t : option_group_t
                                 hb_font_t    *font,
                                 GString      *gs);
   void serialize_message (unsigned int  line_no,
+                         const char   *type,
                          const char   *msg,
                          GString      *gs);
   void serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
@@ -473,11 +645,28 @@ struct format_options_t : option_group_t
 
   hb_bool_t show_glyph_names;
   hb_bool_t show_positions;
+  hb_bool_t show_advances;
   hb_bool_t show_clusters;
   hb_bool_t show_text;
   hb_bool_t show_unicode;
   hb_bool_t show_line_num;
   hb_bool_t show_extents;
+  hb_bool_t show_flags;
+  hb_bool_t trace;
+};
+
+struct subset_options_t : option_group_t
+{
+  subset_options_t (option_parser_t *parser)
+  {
+    drop_hints = false;
+
+    add_options (parser);
+  }
+
+  void add_options (option_parser_t *parser);
+
+  hb_bool_t drop_hints;
 };
 
 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */