Upgrade to latest harfbuzz
[framework/uifw/harfbuzz.git] / src / hb-ot-shape.cc
index 05348d3..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); /* BUFFER: Clobbers var2 */
+  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++)
   {
@@ -129,49 +167,6 @@ hb_ot_shape_setup_masks (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))
-    return;
-
-  c->plan->map.substitute (c->face, c->buffer);
-
-  c->applied_substitute_complex = TRUE;
-  return;
-}
-
-static void
-hb_ot_position_complex (hb_ot_shape_context_t *c)
-{
-
-  if (!hb_ot_layout_has_positioning (c->face))
-    return;
-
-  unsigned int count = c->buffer->len;
-  for (unsigned int i = 0; i < count; i++) {
-    hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
-                                           HB_DIRECTION_LTR,
-                                           &c->buffer->pos[i].x_offset,
-                                           &c->buffer->pos[i].y_offset);
-  }
-
-  c->plan->map.position (c->font, c->buffer);
-
-  for (unsigned int i = 0; i < count; i++) {
-    hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
-                                                HB_DIRECTION_LTR,
-                                                &c->buffer->pos[i].x_offset,
-                                                &c->buffer->pos[i].y_offset);
-  }
-
-  hb_ot_layout_position_finish (c->buffer);
-
-  c->applied_position_complex = TRUE;
-  return;
-}
-
-
 /* Main shaper */
 
 /* Prepare */
@@ -180,8 +175,8 @@ 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
@@ -189,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
@@ -208,20 +203,11 @@ hb_ensure_native_direction (hb_buffer_t *buffer)
   if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
       (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
   {
-    hb_form_clusters (buffer);
     hb_buffer_reverse_clusters (buffer);
     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
   }
 }
 
-static void
-hb_reset_glyph_infos (hb_buffer_t *buffer)
-{
-  unsigned int count = buffer->len;
-  for (unsigned int i = 0; i < count; i++)
-    buffer->info[i].var1.u32 = buffer->info[i].var2.u32 = 0;
-}
-
 
 /* Substitute */
 
@@ -258,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 ();
@@ -277,13 +262,23 @@ hb_map_glyphs (hb_font_t    *font,
 static void
 hb_substitute_default (hb_ot_shape_context_t *c)
 {
+  hb_ot_layout_substitute_start (c->buffer);
+
+  hb_mirror_chars (c);
+
   hb_map_glyphs (c->font, c->buffer);
 }
 
 static void
-hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
+hb_ot_substitute_complex (hb_ot_shape_context_t *c)
 {
-  /* TODO Arabic */
+  if (hb_ot_layout_has_substitution (c->face)) {
+    c->plan->map.substitute (c->face, c->buffer);
+  }
+
+  hb_ot_layout_substitute_finish (c->buffer);
+
+  return;
 }
 
 
@@ -308,6 +303,39 @@ hb_position_default (hb_ot_shape_context_t *c)
 }
 
 static void
+hb_ot_position_complex (hb_ot_shape_context_t *c)
+{
+
+  if (hb_ot_layout_has_positioning (c->face))
+  {
+    /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
+
+    unsigned int count = c->buffer->len;
+    for (unsigned int i = 0; i < count; i++) {
+      hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
+                                             HB_DIRECTION_LTR,
+                                             &c->buffer->pos[i].x_offset,
+                                             &c->buffer->pos[i].y_offset);
+    }
+
+    c->plan->map.position (c->font, c->buffer);
+
+    for (unsigned int i = 0; i < count; i++) {
+      hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
+                                                  HB_DIRECTION_LTR,
+                                                  &c->buffer->pos[i].x_offset,
+                                                  &c->buffer->pos[i].y_offset);
+    }
+
+    c->applied_position_complex = true;
+  }
+
+  hb_ot_layout_position_finish (c->buffer);
+
+  return;
+}
+
+static void
 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
 {
   /* TODO Mark pos */
@@ -345,35 +373,52 @@ 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! */
 
 static void
 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
 {
+  c->buffer->deallocate_var_all ();
+
   /* Save the original direction, we use it later. */
   c->target_direction = c->buffer->props.direction;
 
-  hb_reset_glyph_infos (c->buffer); /* BUFFER: Clear buffer var1 and var2 */
+  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
+  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
+
+  hb_set_unicode_props (c->buffer);
 
-  hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class in var1 */
+  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); /* BUFFER: Clobbers var2 */
+  hb_ot_shape_setup_masks (c);
 
   /* SUBSTITUTE */
   {
-    hb_mirror_chars (c);
-
     hb_substitute_default (c);
 
     hb_ot_substitute_complex (c);
-
-    if (!c->applied_substitute_complex)
-      hb_substitute_complex_fallback (c);
   }
 
   /* POSITION */
@@ -393,7 +438,14 @@ hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
       hb_position_complex_fallback_visual (c);
   }
 
+  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
@@ -405,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);
@@ -423,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));
+}