Upgrade to latest harfbuzz
[framework/uifw/harfbuzz.git] / src / hb-ot-shape.cc
index 1577214..19cf680 100644 (file)
  */
 
 #include "hb-ot-shape-private.hh"
-#include "hb-ot-shape-complex-private.hh"
+#include "hb-ot-shape-normalize-private.hh"
 
 #include "hb-font-private.hh"
+#include "hb-set-private.hh"
 
-HB_BEGIN_DECLS
 
 
 hb_tag_t common_features[] = {
   HB_TAG('c','c','m','p'),
+  HB_TAG('l','i','g','a'),
   HB_TAG('l','o','c','l'),
   HB_TAG('m','a','r','k'),
   HB_TAG('m','k','m','k'),
   HB_TAG('r','l','i','g'),
 };
 
+
 hb_tag_t horizontal_features[] = {
   HB_TAG('c','a','l','t'),
   HB_TAG('c','l','i','g'),
   HB_TAG('c','u','r','s'),
   HB_TAG('k','e','r','n'),
-  HB_TAG('l','i','g','a'),
 };
 
 /* Note:
@@ -63,6 +64,28 @@ hb_tag_t vertical_features[] = {
   HB_TAG('v','r','t','2'),
 };
 
+
+
+struct hb_ot_shape_planner_t
+{
+  hb_ot_map_builder_t map;
+  hb_ot_complex_shaper_t shaper;
+
+  hb_ot_shape_planner_t (void) : map () {}
+  ~hb_ot_shape_planner_t (void) { map.finish (); }
+
+  inline void compile (hb_face_t *face,
+                      const hb_segment_properties_t *props,
+                      struct hb_ot_shape_plan_t &plan)
+  {
+    plan.shaper = shaper;
+    map.compile (face, props, plan.map);
+  }
+
+  private:
+  NO_COPY (hb_ot_shape_planner_t);
+};
+
 static void
 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
                              const hb_segment_properties_t  *props,
@@ -109,13 +132,28 @@ hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
 }
 
 
+struct hb_ot_shape_context_t
+{
+  /* Input to hb_ot_shape_execute() */
+  hb_ot_shape_plan_t *plan;
+  hb_font_t *font;
+  hb_face_t *face;
+  hb_buffer_t  *buffer;
+  const hb_feature_t *user_features;
+  unsigned int        num_user_features;
+
+  /* Transient stuff */
+  hb_direction_t target_direction;
+  hb_bool_t applied_position_complex;
+};
+
 static void
 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
 {
   hb_mask_t global_mask = c->plan->map.get_global_mask ();
   c->buffer->reset_masks (global_mask);
 
-  hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer);
+  hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
 
   for (unsigned int i = 0; i < c->num_user_features; i++)
   {
@@ -133,12 +171,12 @@ hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
 
 /* Prepare */
 
-void
-_hb_set_unicode_props (hb_buffer_t *buffer)
+static void
+hb_set_unicode_props (hb_buffer_t *buffer)
 {
   unsigned int count = buffer->len;
-  for (unsigned int i = 1; i < count; i++)
-    hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
+  for (unsigned int i = 0; i < count; i++)
+    _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
 }
 
 static void
@@ -146,11 +184,11 @@ hb_form_clusters (hb_buffer_t *buffer)
 {
   unsigned int count = buffer->len;
   for (unsigned int i = 1; i < count; i++)
-    if (FLAG (buffer->info[i].general_category()) &
+    if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
        (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
         FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
         FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
-      buffer->info[i].cluster = buffer->info[i - 1].cluster;
+      buffer->info[i].cluster = buffer->info[i - 1].cluster; /* XXX do the min() here */
 }
 
 static void
@@ -206,17 +244,16 @@ hb_map_glyphs (hb_font_t    *font,
 
   unsigned int count = buffer->len - 1;
   for (buffer->idx = 0; buffer->idx < count;) {
-    if (unlikely (is_variation_selector (buffer->info[buffer->idx + 1].codepoint))) {
-      hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, buffer->info[buffer->idx + 1].codepoint, &glyph);
-      buffer->replace_glyph (glyph);
-      buffer->skip_glyph ();
+    if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
+      hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
+      buffer->replace_glyphs (2, 1, &glyph);
     } else {
-      hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph);
+      hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
       buffer->replace_glyph (glyph);
     }
   }
   if (likely (buffer->idx < buffer->len)) {
-    hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph);
+    hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
     buffer->replace_glyph (glyph);
   }
   buffer->swap_buffers ();
@@ -235,21 +272,15 @@ hb_substitute_default (hb_ot_shape_context_t *c)
 static void
 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
 {
-  if (hb_ot_layout_has_substitution (c->face))
+  if (hb_ot_layout_has_substitution (c->face)) {
     c->plan->map.substitute (c->face, c->buffer);
+  }
 
   hb_ot_layout_substitute_finish (c->buffer);
 
-  c->applied_substitute_complex = TRUE;
   return;
 }
 
-static void
-hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
-{
-  /* TODO Arabic */
-}
-
 
 /* Position */
 
@@ -295,11 +326,12 @@ hb_ot_position_complex (hb_ot_shape_context_t *c)
                                                   &c->buffer->pos[i].x_offset,
                                                   &c->buffer->pos[i].y_offset);
     }
+
+    c->applied_position_complex = true;
   }
 
   hb_ot_layout_position_finish (c->buffer);
 
-  c->applied_position_complex = TRUE;
   return;
 }
 
@@ -341,6 +373,23 @@ hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
   hb_truetype_kern (c);
 }
 
+static void
+hb_hide_zerowidth (hb_ot_shape_context_t *c)
+{
+  /* TODO Save the space character in the font? */
+  hb_codepoint_t space;
+  if (!hb_font_get_glyph (c->font, ' ', 0, &space))
+    return; /* No point! */
+
+  unsigned int count = c->buffer->len;
+  for (unsigned int i = 0; i < count; i++)
+    if (unlikely (_hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
+      c->buffer->info[i].codepoint = space;
+      c->buffer->pos[i].x_advance = 0;
+      c->buffer->pos[i].y_advance = 0;
+    }
+}
+
 
 /* Do it! */
 
@@ -352,16 +401,16 @@ hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
   /* Save the original direction, we use it later. */
   c->target_direction = c->buffer->props.direction;
 
-  HB_BUFFER_ALLOCATE_VAR (c->buffer, general_category);
-  HB_BUFFER_ALLOCATE_VAR (c->buffer, combining_class);
+  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
+  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
 
-  _hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class */
+  hb_set_unicode_props (c->buffer);
 
   hb_form_clusters (c->buffer);
 
   hb_ensure_native_direction (c->buffer);
 
-  _hb_ot_shape_normalize (c);
+  _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper));
 
   hb_ot_shape_setup_masks (c);
 
@@ -370,9 +419,6 @@ hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
     hb_substitute_default (c);
 
     hb_ot_substitute_complex (c);
-
-    if (!c->applied_substitute_complex)
-      hb_substitute_complex_fallback (c);
   }
 
   /* POSITION */
@@ -392,10 +438,14 @@ hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
       hb_position_complex_fallback_visual (c);
   }
 
-  HB_BUFFER_DEALLOCATE_VAR (c->buffer, combining_class);
-  HB_BUFFER_DEALLOCATE_VAR (c->buffer, general_category);
+  hb_hide_zerowidth (c);
+
+  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
+  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
 
   c->buffer->props.direction = c->target_direction;
+
+  c->buffer->deallocate_var_all ();
 }
 
 static void
@@ -407,6 +457,8 @@ hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
 {
   hb_ot_shape_planner_t planner;
 
+  assert (HB_DIRECTION_IS_VALID (props->direction));
+
   planner.shaper = hb_ot_shape_complex_categorize (props);
 
   hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
@@ -425,17 +477,52 @@ hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
   hb_ot_shape_execute_internal (&c);
 }
 
-void
-hb_ot_shape (hb_font_t          *font,
-            hb_buffer_t        *buffer,
-            const hb_feature_t *features,
-            unsigned int        num_features)
+hb_bool_t
+_hb_ot_shape (hb_font_t          *font,
+             hb_buffer_t        *buffer,
+             const hb_feature_t *features,
+             unsigned int        num_features)
 {
   hb_ot_shape_plan_t plan;
 
+  buffer->guess_properties ();
+
   hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
   hb_ot_shape_execute (&plan, font, buffer, features, num_features);
+
+  return true;
 }
 
 
-HB_END_DECLS
+void
+hb_ot_shape_glyphs_closure (hb_font_t          *font,
+                           hb_buffer_t        *buffer,
+                           const hb_feature_t *features,
+                           unsigned int        num_features,
+                           hb_set_t           *glyphs)
+{
+  hb_ot_shape_plan_t plan;
+
+  buffer->guess_properties ();
+
+  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
+
+  /* TODO: normalization? have shapers do closure()? */
+  /* TODO: Deal with mirrored chars? */
+  hb_map_glyphs (font, buffer);
+
+  /* Seed it.  It's user's responsibility to have cleard glyphs
+   * if that's what they desire. */
+  unsigned int count = buffer->len;
+  for (unsigned int i = 0; i < count; i++)
+    hb_set_add (glyphs, buffer->info[i].codepoint);
+
+  /* And find transitive closure. */
+  hb_set_t copy;
+  copy.init ();
+
+  do {
+    copy.set (glyphs);
+    plan.map.substitute_closure (font->face, glyphs);
+  } while (!copy.equal (glyphs));
+}