Imported Upstream version 2.4.0
[platform/upstream/harfbuzz.git] / src / hb-graphite2.cc
index 46fe139..fdb5453 100644 (file)
  * Google Author(s): Behdad Esfahbod
  */
 
-#define HB_SHAPER graphite2
-#include "hb-shaper-impl-private.hh"
+#include "hb-shaper-impl.hh"
 
 #include "hb-graphite2.h"
 
 #include <graphite2/Segment.h>
 
+#include "hb-ot-layout.h"
 
-HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, face)
-HB_SHAPER_DATA_ENSURE_DEFINE(graphite2, font)
+
+/**
+ * SECTION:hb-graphite2
+ * @title: hb-graphite2
+ * @short_description: Graphite2 integration
+ * @include: hb-graphite2.h
+ *
+ * Functions for using HarfBuzz with the Graphite2 fonts.
+ **/
 
 
 /*
  * shaper face data
  */
 
-typedef struct hb_graphite2_tablelist_t {
+typedef struct hb_graphite2_tablelist_t
+{
   struct hb_graphite2_tablelist_t *next;
   hb_blob_t *blob;
   unsigned int tag;
 } hb_graphite2_tablelist_t;
 
-struct hb_graphite2_shaper_face_data_t {
+struct hb_graphite2_face_data_t
+{
   hb_face_t *face;
   gr_face   *grface;
-  hb_graphite2_tablelist_t *tlist;
+  hb_atomic_ptr_t<hb_graphite2_tablelist_t> tlist;
 };
 
 static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len)
 {
-  hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data;
+  hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
   hb_graphite2_tablelist_t *tlist = face_data->tlist;
 
   hb_blob_t *blob = nullptr;
@@ -79,10 +88,12 @@ static const void *hb_graphite2_get_table (const void *data, unsigned int tag, s
     p->blob = blob;
     p->tag = tag;
 
-    /* TODO Not thread-safe, but fairly harmless.
-     * We can do the double-checked pointer cmpexch thing here. */
-    p->next = face_data->tlist;
-    face_data->tlist = p;
+retry:
+    hb_graphite2_tablelist_t *tlist = face_data->tlist;
+    p->next = tlist;
+
+    if (unlikely (!face_data->tlist.cmpexch (tlist, p)))
+      goto retry;
   }
 
   unsigned int tlen;
@@ -91,7 +102,33 @@ static const void *hb_graphite2_get_table (const void *data, unsigned int tag, s
   return d;
 }
 
-hb_graphite2_shaper_face_data_t *
+static void hb_graphite2_release_table(const void *data, const void *table_buffer)
+{
+  hb_graphite2_face_data_t *face_data = (hb_graphite2_face_data_t *) data;
+  hb_graphite2_tablelist_t *tlist = face_data->tlist;
+
+  hb_graphite2_tablelist_t *prev = nullptr;
+  hb_graphite2_tablelist_t *curr = tlist;
+  while (curr)
+  {
+    if (hb_blob_get_data(curr->blob, nullptr) == table_buffer)
+    {
+      if (prev == nullptr)
+        face_data->tlist.cmpexch(tlist, curr->next);
+      else
+        prev->next = curr->next;
+      hb_blob_destroy(curr->blob);
+      free(curr);
+      break;
+    }
+    prev = curr;
+    curr = curr->next;
+  }
+}
+
+static gr_face_ops hb_graphite2_face_ops = { sizeof(gr_face_ops), hb_graphite2_get_table, hb_graphite2_release_table };
+
+hb_graphite2_face_data_t *
 _hb_graphite2_shaper_face_data_create (hb_face_t *face)
 {
   hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF);
@@ -104,12 +141,12 @@ _hb_graphite2_shaper_face_data_create (hb_face_t *face)
   }
   hb_blob_destroy (silf_blob);
 
-  hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t));
+  hb_graphite2_face_data_t *data = (hb_graphite2_face_data_t *) calloc (1, sizeof (hb_graphite2_face_data_t));
   if (unlikely (!data))
     return nullptr;
 
   data->face = face;
-  data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll);
+  data->grface = gr_make_face_with_ops (data, &hb_graphite2_face_ops, gr_face_preloadAll);
 
   if (unlikely (!data->grface)) {
     free (data);
@@ -120,7 +157,7 @@ _hb_graphite2_shaper_face_data_create (hb_face_t *face)
 }
 
 void
-_hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
+_hb_graphite2_shaper_face_data_destroy (hb_graphite2_face_data_t *data)
 {
   hb_graphite2_tablelist_t *tlist = data->tlist;
 
@@ -143,8 +180,8 @@ _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data)
 gr_face *
 hb_graphite2_face_get_gr_face (hb_face_t *face)
 {
-  if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return nullptr;
-  return HB_SHAPER_DATA_GET (face)->grface;
+  const hb_graphite2_face_data_t *data = face->data.graphite2;
+  return data ? data->grface : nullptr;
 }
 
 
@@ -152,52 +189,33 @@ hb_graphite2_face_get_gr_face (hb_face_t *face)
  * shaper font data
  */
 
-struct hb_graphite2_shaper_font_data_t {};
+struct hb_graphite2_font_data_t {};
 
-hb_graphite2_shaper_font_data_t *
+hb_graphite2_font_data_t *
 _hb_graphite2_shaper_font_data_create (hb_font_t *font HB_UNUSED)
 {
-  return (hb_graphite2_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
+  return (hb_graphite2_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
 }
 
 void
-_hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data HB_UNUSED)
+_hb_graphite2_shaper_font_data_destroy (hb_graphite2_font_data_t *data HB_UNUSED)
 {
 }
 
-/*
+/**
+ * hb_graphite2_font_get_gr_font:
+ *
  * Since: 0.9.10
+ * Deprecated: 1.4.2
  */
 gr_font *
-hb_graphite2_font_get_gr_font (hb_font_t *font)
+hb_graphite2_font_get_gr_font (hb_font_t *font HB_UNUSED)
 {
   return nullptr;
 }
 
 
 /*
- * shaper shape_plan data
- */
-
-struct hb_graphite2_shaper_shape_plan_data_t {};
-
-hb_graphite2_shaper_shape_plan_data_t *
-_hb_graphite2_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
-                                            const hb_feature_t *user_features HB_UNUSED,
-                                            unsigned int        num_user_features HB_UNUSED,
-                                            const int          *coords HB_UNUSED,
-                                            unsigned int        num_coords HB_UNUSED)
-{
-  return (hb_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
-}
-
-void
-_hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_shaper_shape_plan_data_t *data HB_UNUSED)
-{
-}
-
-
-/*
  * shaper
  */
 
@@ -207,18 +225,18 @@ struct hb_graphite2_cluster_t {
   unsigned int base_glyph;
   unsigned int num_glyphs;
   unsigned int cluster;
-  float advance;
+  unsigned int advance;
 };
 
 hb_bool_t
-_hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
+_hb_graphite2_shape (hb_shape_plan_t    *shape_plan HB_UNUSED,
                     hb_font_t          *font,
                     hb_buffer_t        *buffer,
                     const hb_feature_t *features,
                     unsigned int        num_features)
 {
   hb_face_t *face = font->face;
-  gr_face *grface = HB_SHAPER_DATA_GET (face)->grface;
+  gr_face *grface = face->data.graphite2->grface;
 
   const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
   const char *lang_end = lang ? strchr (lang, '-') : nullptr;
@@ -235,7 +253,7 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
   gr_segment *seg = nullptr;
   const gr_slot *is;
   unsigned int ci = 0, ic = 0;
-  float curradvx = 0., curradvy = 0.;
+  unsigned int curradvx = 0, curradvy = 0;
 
   unsigned int scratch_size;
   hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
@@ -247,11 +265,16 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
 
   /* TODO ensure_native_direction. */
 
-  hb_tag_t script_tag[2];
-  hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]);
+  hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT];
+  unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT;
+  hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer),
+                                      HB_LANGUAGE_INVALID,
+                                      &count,
+                                      script_tag,
+                                      nullptr, nullptr);
 
   seg = gr_make_seg (nullptr, grface,
-                    script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
+                    count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT,
                     feats,
                     gr_utf32, chars, buffer->len,
                     2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0));
@@ -301,11 +324,15 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
 
   hb_codepoint_t *pg = gids;
   clusters[0].cluster = buffer->info[0].cluster;
-  float curradv = 0.;
+  unsigned int upem = hb_face_get_upem (face);
+  float xscale = (float) font->x_scale / upem;
+  float yscale = (float) font->y_scale / upem;
+  yscale *= yscale / xscale;
+  unsigned int curradv = 0;
   if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
   {
-    curradv = gr_slot_origin_X(gr_seg_first_slot(seg));
-    clusters[0].advance = gr_seg_advance_X(seg) - curradv;
+    curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale;
+    clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv;
   }
   else
     clusters[0].advance = 0;
@@ -332,14 +359,17 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
       c->base_glyph = ic;
       c->num_glyphs = 0;
       if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
-        c->advance = curradv - gr_slot_origin_X(is);
+      {
+        c->advance = curradv - gr_slot_origin_X(is) * xscale;
+        curradv -= c->advance;
+      }
       else
       {
         c->advance = 0;
-        clusters[ci].advance += gr_slot_origin_X(is) - curradv;
+        clusters[ci].advance += gr_slot_origin_X(is) * xscale - curradv;
+        curradv += clusters[ci].advance;
       }
       ci++;
-      curradv = gr_slot_origin_X(is);
     }
     clusters[ci].num_glyphs++;
 
@@ -350,7 +380,7 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
   if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction))
     clusters[ci].advance += curradv;
   else
-    clusters[ci].advance += gr_seg_advance_X(seg) - curradv;
+    clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv;
   ci++;
 
   for (unsigned int i = 0; i < ci; ++i)
@@ -365,10 +395,6 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
   }
   buffer->len = glyph_count;
 
-  unsigned int upem = hb_face_get_upem (face);
-  float xscale = (float) font->x_scale / upem;
-  float yscale = (float) font->y_scale / upem;
-  yscale *= yscale / xscale;
   /* Positioning. */
   unsigned int currclus = (unsigned int) -1;
   const hb_glyph_info_t *info = buffer->info;
@@ -381,11 +407,11 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
       pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx;
       pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
       if (info->cluster != currclus) {
-        pPos->x_advance = info->var1.i32 * xscale;
-        curradvx += pPos->x_advance;
-        currclus = info->cluster;
+       pPos->x_advance = info->var1.i32;
+       curradvx += pPos->x_advance;
+       currclus = info->cluster;
       } else
-        pPos->x_advance = 0.;
+       pPos->x_advance = 0.;
 
       pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
       curradvy += pPos->y_advance;
@@ -398,15 +424,15 @@ _hb_graphite2_shape (hb_shape_plan_t    *shape_plan,
     {
       if (info->cluster != currclus)
       {
-        pPos->x_advance = info->var1.i32 * xscale;
-        curradvx -= pPos->x_advance;
-        currclus = info->cluster;
+       pPos->x_advance = info->var1.i32;
+       curradvx -= pPos->x_advance;
+       currclus = info->cluster;
       } else
-        pPos->x_advance = 0.;
+       pPos->x_advance = 0.;
 
       pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale;
       curradvy -= pPos->y_advance;
-      pPos->x_offset = (gr_slot_origin_X (is) - info->var1.i32) * xscale - curradvx + pPos->x_advance;
+      pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1.i32 - curradvx + pPos->x_advance;
       pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy;
     }
     hb_buffer_reverse_clusters (buffer);