[HarfBuzz] harfbuzz-ng: Branch 'master' - 9 commits

Behdad Esfahbod behdad at kemper.freedesktop.org
Wed Aug 24 10:36:58 PDT 2011


 configure.ac                      |    8 
 contrib/python/lib/harfbuzz.pyx   |   20 --
 contrib/python/runpy              |    2 
 contrib/python/scripts/hbtestfont |    4 
 contrib/python/setup.py           |   18 +
 src/Makefile.am                   |   22 +-
 src/hb-gobject-enums.cc.tmpl      |    2 
 src/hb-graphite2.cc               |  343 ++++++++++++++++++++++++++++++++++++++
 src/hb-graphite2.h                |   45 ++++
 src/hb-ot-hmtx-table.hh           |   86 +++++++++
 src/hb-shape.cc                   |    6 
 src/hb-uniscribe.cc               |   21 +-
 12 files changed, 542 insertions(+), 35 deletions(-)

New commits:
commit 74ef81a0b0f9adddfb42c3cb87f08f8156054519
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 19:16:13 2011 +0200

    Fix make distcheck

diff --git a/src/Makefile.am b/src/Makefile.am
index 95980a2..4170663 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,6 +28,8 @@ HBSOURCES =  \
 	hb-open-file-private.hh \
 	hb-open-type-private.hh \
 	hb-ot-head-table.hh \
+	hb-ot-hhea-table.hh \
+	hb-ot-hmtx-table.hh \
 	hb-ot-maxp-table.hh \
 	hb-ot-name-table.hh \
 	hb-ot-tag.cc \
@@ -48,10 +50,6 @@ HBHEADERS = \
 	hb-version.h \
 	$(NULL)
 
-MAINTAINERCLEANFILES += \
-	$(srcdir)/hb-version.h \
-	$(NULL)
-
 if HAVE_OT
 HBSOURCES += \
 	hb-ot-layout.cc \
@@ -92,14 +90,16 @@ endif
 if HAVE_GOBJECT
 HBCFLAGS += $(GOBJECT_CFLAGS)
 HBLIBS   += $(GOBJECT_LIBS)
-HBSOURCES += hb-gobject-structs.cc hb-gobject-enums.cc
+HBSOURCES += hb-gobject-structs.cc
+nodist_HBSOURCES = hb-gobject-enums.cc
 HBHEADERS += hb-gobject.h
 BUILT_SOURCES += hb-gobject-enums.cc
 EXTRA_DIST += hb-gobject-enums.cc.tmpl
+DISTCLEANFILES += hb-gobject-enums.cc
 
 hb-gobject-enums.cc: hb-gobject-enums.cc.tmpl $(HBHEADERS)
-	$(AM_V_GEN) ( cd $(srcdir) && $(GLIB_MKENUMS) --template $^ ) > \
-	    "$@.tmp" && mv "$@.tmp" "$@" || ( $(RM) "@.tmp" && false )
+	$(AM_V_GEN) $(GLIB_MKENUMS) --template $^ > "$@.tmp" && \
+	mv "$@.tmp" "$@" || ( $(RM) "@.tmp" && false )
 endif
 
 if HAVE_ICU
@@ -132,6 +132,7 @@ endif
 
 CXXLINK = $(LINK)
 libharfbuzz_la_SOURCES = $(HBSOURCES) $(HBHEADERS)
+nodist_libharfbuzz_la_SOURCES = $(nodist_HBSOURCES)
 libharfbuzz_la_CPPFLAGS = $(HBCFLAGS)
 libharfbuzz_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(HB_LIBTOOL_VERSION_INFO) -no-undefined
 libharfbuzz_la_LIBADD = $(HBLIBS)
diff --git a/src/hb-gobject-enums.cc.tmpl b/src/hb-gobject-enums.cc.tmpl
index 688dfff..05abd89 100644
--- a/src/hb-gobject-enums.cc.tmpl
+++ b/src/hb-gobject-enums.cc.tmpl
@@ -45,7 +45,7 @@
 /*** END file-production ***/
 
 /*** BEGIN value-header ***/
-/* inline static -----> disable these for now... */
+inline static /* TODO(behdad) disable these for now until we fix them... */
 GType
 @enum_name at _get_type (void)
 {
commit 24bcdbcc0639ca9e9c0fde1a71cbbf1c3d2ef98d
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 19:13:15 2011 +0200

    Add hb-ot-hmtx-table.hh
    
    Oops!

diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh
new file mode 100644
index 0000000..b58799c
--- /dev/null
+++ b/src/hb-ot-hmtx-table.hh
@@ -0,0 +1,86 @@
+/*
+ * Copyright © 2011  Google, Inc.
+ *
+ *  This is part of HarfBuzz, a text shaping library.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ * Google Author(s): Behdad Esfahbod
+ */
+
+#ifndef HB_OT_HMTX_TABLE_HH
+#define HB_OT_HMTX_TABLE_HH
+
+#include "hb-open-type-private.hh"
+
+
+
+/*
+ * hmtx -- The Horizontal Metrics Table
+ */
+
+#define HB_OT_TAG_hmtx HB_TAG('h','m','t','x')
+
+
+struct LongHorMetric
+{
+  USHORT	advanceWidth;
+  SHORT		lsb;
+  public:
+  DEFINE_SIZE_STATIC (4);
+};
+
+struct hmtx
+{
+  static const hb_tag_t Tag	= HB_OT_TAG_hmtx;
+
+  inline bool sanitize (hb_sanitize_context_t *c) {
+    TRACE_SANITIZE ();
+    /* We don't check for anything specific here.  The users of the
+     * struct do all the hard work... */
+    return true;
+  }
+
+  private:
+  LongHorMetric	longHorMetric[VAR];	/* Paired advance width and left side
+					 * bearing values for each glyph. The
+					 * value numOfHMetrics comes from
+					 * the 'hhea' table. If the font is
+					 * monospaced, only one entry need
+					 * be in the array, but that entry is
+					 * required. The last entry applies to
+					 * all subsequent glyphs. */
+  SHORT		leftSideBearingX[VAR];	/* Here the advanceWidth is assumed
+					 * to be the same as the advanceWidth
+					 * for the last entry above. The
+					 * number of entries in this array is
+					 * derived from numGlyphs (from 'maxp'
+					 * table) minus numberOfHMetrics. This
+					 * generally is used with a run of
+					 * monospaced glyphs (e.g., Kanji
+					 * fonts or Courier fonts). Only one
+					 * run is allowed and it must be at
+					 * the end. This allows a monospaced
+					 * font to vary the left side bearing
+					 * values for each glyph. */
+  public:
+  DEFINE_SIZE_ARRAY2 (0, longHorMetric, leftSideBearingX);
+};
+
+#endif /* HB_OT_HMTX_TABLE_HH */
commit a3bd8a0e1862212a2d4141b973039bd000a3054f
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 03:22:49 2011 +0200

    [graphite] Rewrite properly

diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc
index 9282892..df97175 100644
--- a/src/hb-graphite2.cc
+++ b/src/hb-graphite2.cc
@@ -1,6 +1,7 @@
 /*
- * Copyright (C) 2011  Martin Hosken
- * Copyright (C) 2011  SIL International
+ * Copyright © 2011  Martin Hosken
+ * Copyright © 2011  SIL International
+ * Copyright © 2011  Google, Inc.
  *
  *  This is part of HarfBuzz, a text shaping library.
  *
@@ -21,6 +22,8 @@
  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ * Google Author(s): Behdad Esfahbod
  */
 
 #include "hb-private.hh"
@@ -35,400 +38,306 @@
 #include <graphite2/Segment.h>
 
 
-typedef struct hbgr_tablelist_t {
-    hb_blob_t   *blob;
-    struct hbgr_tablelist_t *next;
-    unsigned int tag;
-} hbgr_tablelist_t;
-
-typedef struct hbgr_face_t {
-    gr_face             *grface;
-    hbgr_tablelist_t    *tlist;
-    hb_destroy_func_t   destroy;
-    hb_reference_table_func_t get_table;
-    hb_face_t           *face;
-    void                *data;
-} hbgr_face_t;
-
-typedef struct hbgr_font_t {
-    gr_font             *grfont;
-    hb_face_t           *face;
-    hb_font_t           *font;
-    hb_destroy_func_t   destroy;
-    hb_font_funcs_t     *klass;
-    void                *data;
-} hbgr_font_t;
-
-typedef struct hbgr_cluster_t {
-    unsigned int base_char;
-    unsigned int num_chars;
-    unsigned int base_glyph;
-    unsigned int num_glyphs;
-} hbgr_cluster_t;
-
-static const void *hbgr_get_table(const void *face, unsigned int tag, size_t *len);
-static float hbgr_get_advance(const void *font, unsigned short gid);
-static void hbgr_face_destroy(void *data);
-static hb_blob_t *hbgr_face_get_table(hb_face_t *face, hb_tag_t tag, void *data);
-static void hbgr_font_destroy(void *data);
-/* static hb_codepoint_t hbgr_font_get_glyph (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t unicode, hb_codepoint_t variation_selector);
-static void hbgr_font_get_glyph_advance (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t glyph, hb_position_t *x_advance, hb_position_t *y_advance);
-static void hbgr_font_get_glyph_extents (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents);
-static hb_bool_t hbgr_font_get_contour_point (hb_font_t *font, hb_face_t *face, const void *user_data, unsigned int point_index, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y);
-static hb_position_t hbgr_font_get_kerning (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t first_glyph, hb_codepoint_t second_glyph);
-*/
-
-static const void *hbgr_get_table(const void *data, unsigned int tag, size_t *len)
-{
-    const void *res;
-    hbgr_tablelist_t *pl, *p;
-    hbgr_face_t *face = (hbgr_face_t *)data;
-    hbgr_tablelist_t *tlist = face->tlist;
-
-    for (p = tlist; p; p = p->next)
-        if (p->tag == tag)
-            return hb_blob_get_data(p->blob, (unsigned int *)len);
-        else
-            pl = p;
-
-    hb_blob_t *blob = face->get_table(face->face, tag, face->data);
-    if (pl->blob)
-    {
-        p = (hbgr_tablelist_t *)malloc(sizeof(hbgr_tablelist_t));
-        if (!p)
-        {
-            hb_blob_destroy(blob);
-            return NULL;
-        }
-        p->next = NULL;
-        pl->next = p;
-        pl = p;
-    }
-    pl->blob = blob;
-    pl->tag = tag;
-    return hb_blob_get_data(blob, (unsigned int *)len);
-}
+struct hb_gr_cluster_t {
+  unsigned int base_char;
+  unsigned int num_chars;
+  unsigned int base_glyph;
+  unsigned int num_glyphs;
+};
 
-static float hbgr_get_advance(const void *dat, unsigned short gid)
-{
-    hbgr_font_t *font = (hbgr_font_t *)dat;
-    return (float)(font->klass->get.glyph_h_advance(font->font, font->data, gid, font->klass->user_data.glyph_h_advance));
-}
 
-static void hbgr_face_destroy(void *data)
-{
-    hbgr_face_t *fcomp = (hbgr_face_t *)data;
-    hbgr_tablelist_t *tlist = fcomp->tlist;
-    while (tlist)
-    {
-        hbgr_tablelist_t *old = tlist;
-        hb_blob_destroy(tlist->blob);
-        tlist = tlist->next;
-        free(old);
-    }
-    fcomp->destroy(fcomp->data);
-    gr_face_destroy(fcomp->grface);
-}
+typedef struct hb_gr_tablelist_t {
+  hb_blob_t   *blob;
+  struct hb_gr_tablelist_t *next;
+  unsigned int tag;
+} hb_gr_tablelist_t;
 
-static hb_blob_t *hbgr_face_get_table(hb_face_t *face, hb_tag_t tag, void *data)
-{
-    hbgr_face_t *fcomp = (hbgr_face_t *)data;
-    return fcomp->get_table(fcomp->face, tag, fcomp->data);
-}
+static struct hb_gr_face_data_t {
+  hb_face_t         *face;
+  gr_face           *grface;
+  hb_gr_tablelist_t *tlist;
+} _hb_gr_face_data_nil = {NULL, NULL};
 
-static void hbgr_font_destroy(void *data)
-{
-    hbgr_font_t *fcomp = (hbgr_font_t *)data;
-    fcomp->destroy(fcomp->data);
-    gr_font_destroy(fcomp->grfont);
-}
+static struct hb_gr_font_data_t {
+  hb_font_t *font;
+  gr_font   *grfont;
+  gr_face   *grface;
+} _hb_gr_font_data_nil = {NULL, NULL, NULL};
 
-static hb_bool_t hbgr_font_get_glyph (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t unicode,
-        hb_codepoint_t variation_selector,
-        hb_codepoint_t *glyph,
-        void *user_data)
-{
-    return ((hbgr_font_t *)font)->klass->get.glyph(((hbgr_font_t *)font)->font, font_data, unicode, variation_selector, glyph, user_data);
-}
 
-static hb_bool_t hbgr_font_get_glyph_h_advance (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        void *user_data)
+static const void *hb_gr_get_table (const void *data, unsigned int tag, size_t *len)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_h_advance(((hbgr_font_t *)font)->font, font_data, glyph, user_data);
+  hb_gr_tablelist_t *pl = NULL, *p;
+  hb_gr_face_data_t *face = (hb_gr_face_data_t *) data;
+  hb_gr_tablelist_t *tlist = face->tlist;
+
+  for (p = tlist; p; p = p->next)
+    if (p->tag == tag ) {
+      unsigned int tlen;
+      const char *d = hb_blob_get_data (p->blob, &tlen);
+      *len = tlen;
+      return d;
+    } else
+      pl = p;
+
+  if (!face->face)
+    return NULL;
+  hb_blob_t *blob = hb_face_reference_table (face->face, tag);
+
+  if (!pl || pl->blob)
+  {
+    p = (hb_gr_tablelist_t *) malloc (sizeof (hb_gr_tablelist_t));
+    if (!p) {
+      hb_blob_destroy(blob);
+      return NULL;
+    }
+    p->next = NULL;
+    if (pl)
+      pl->next = p;
+    else
+      face->tlist = p;
+    pl = p;
+  }
+  pl->blob = blob;
+  pl->tag = tag;
+
+  unsigned int tlen;
+  const char *d = hb_blob_get_data (blob, &tlen);
+  *len = tlen;
+  return d;
 }
 
-static hb_position_t hbgr_font_get_glyph_v_advance (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        void *user_data)
+static float hb_gr_get_advance (const void *data, unsigned short gid)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_v_advance(((hbgr_font_t *)font)->font, font_data, glyph, user_data);
+  hb_gr_font_data_t *font = (hb_gr_font_data_t *) data;
+  if (!font->font)
+    return 0;
+  return (float) hb_font_get_glyph_h_advance (font->font, gid);
 }
 
-static hb_bool_t hbgr_font_get_glyph_h_origin (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        hb_position_t *x,
-        hb_position_t *y,
-        void *user_data)
+static void _hb_gr_face_data_destroy (void *data)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_h_origin(((hbgr_font_t *)font)->font, font_data, glyph, x, y, user_data);
+  hb_gr_face_data_t *f = (hb_gr_face_data_t *) data;
+  hb_gr_tablelist_t *tlist = f->tlist;
+  while (tlist)
+  {
+    hb_gr_tablelist_t *old = tlist;
+    hb_blob_destroy (tlist->blob);
+    tlist = tlist->next;
+    free(old);
+  }
+  gr_face_destroy (f->grface);
 }
 
-static hb_bool_t hbgr_font_get_glyph_v_origin (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        hb_position_t *x,
-        hb_position_t *y,
-        void *user_data)
+static void _hb_gr_font_data_destroy (void *data)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_v_origin(((hbgr_font_t *)font)->font, font_data, glyph, x, y, user_data);
-}
+  hb_gr_font_data_t *f = (hb_gr_font_data_t *) data;
 
-static hb_position_t hbgr_font_get_glyph_h_kerning (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t first_glyph,
-        hb_codepoint_t second_glyph,
-        void *user_data)
-{
-    return ((hbgr_font_t *)font)->klass->get.glyph_h_kerning(((hbgr_font_t *)font)->font, font_data, first_glyph, second_glyph, user_data);
+  gr_font_destroy (f->grfont);
 }
 
-static hb_position_t hbgr_font_get_glyph_v_kerning (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t first_glyph,
-        hb_codepoint_t second_glyph,
-        void *user_data)
-{
-    return ((hbgr_font_t *)font)->klass->get.glyph_v_kerning(((hbgr_font_t *)font)->font, font_data, first_glyph, second_glyph, user_data);
-}
+static hb_user_data_key_t hb_gr_data_key;
 
-static hb_bool_t hbgr_font_get_glyph_extents (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        hb_glyph_extents_t *extents,
-        void *user_data)
+static hb_gr_face_data_t *
+_hb_gr_face_get_data (hb_face_t *face)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_extents(((hbgr_font_t *)font)->font, font_data, glyph, extents, user_data);
+  hb_gr_face_data_t *data = (hb_gr_face_data_t *) hb_face_get_user_data (face, &hb_gr_data_key);
+  if (likely (data)) return data;
+
+  data = (hb_gr_face_data_t *) calloc (1, sizeof (hb_gr_face_data_t));
+  if (unlikely (!data))
+    return &_hb_gr_face_data_nil;
+
+
+  hb_blob_t *silf_blob = hb_face_reference_table (face, HB_GRAPHITE_TAG_Silf);
+  if (!hb_blob_get_length(silf_blob))
+  {
+    hb_blob_destroy(silf_blob);
+    return &_hb_gr_face_data_nil;
+  }
+
+  data->face = face;
+  data->grface = gr_make_face (data, &hb_gr_get_table, gr_face_preloadGlyphs);
+
+
+  if (unlikely (!hb_face_set_user_data (face, &hb_gr_data_key, data,
+					(hb_destroy_func_t) _hb_gr_face_data_destroy,
+					FALSE)))
+  {
+    _hb_gr_face_data_destroy (data);
+    data = (hb_gr_face_data_t *) hb_face_get_user_data (face, &hb_gr_data_key);
+    if (data)
+      return data;
+    else
+      return &_hb_gr_face_data_nil;
+  }
+
+  return data;
 }
 
-static hb_bool_t hbgr_font_get_glyph_contour_point (hb_font_t *font,
-        void *font_data,
-        hb_codepoint_t glyph,
-        unsigned int point_index,
-        hb_position_t *x,
-        hb_position_t *y,
-        void *user_data)
+static hb_gr_font_data_t *
+_hb_gr_font_get_data (hb_font_t *font)
 {
-    return ((hbgr_font_t *)font)->klass->get.glyph_contour_point(((hbgr_font_t *)font)->font, font_data, glyph, point_index, x, y, user_data);
+  hb_gr_font_data_t *data = (hb_gr_font_data_t *) hb_font_get_user_data (font, &hb_gr_data_key);
+  if (likely (data)) return data;
+
+  data = (hb_gr_font_data_t *) calloc (1, sizeof (hb_gr_font_data_t));
+  if (unlikely (!data))
+    return &_hb_gr_font_data_nil;
+
+
+  hb_blob_t *silf_blob = hb_face_reference_table (font->face, HB_GRAPHITE_TAG_Silf);
+  if (!hb_blob_get_length (silf_blob))
+  {
+    hb_blob_destroy(silf_blob);
+    return &_hb_gr_font_data_nil;
+  }
+
+  data->font = font;
+  data->grface = _hb_gr_face_get_data (font->face)->grface;
+  int scale;
+  hb_font_get_scale (font, &scale, NULL);
+  data->grfont = gr_make_font_with_advance_fn (scale, data, &hb_gr_get_advance, data->grface);
+
+
+  if (unlikely (!hb_font_set_user_data (font, &hb_gr_data_key, data,
+					(hb_destroy_func_t) _hb_gr_font_data_destroy,
+					FALSE)))
+  {
+    _hb_gr_font_data_destroy (data);
+    data = (hb_gr_font_data_t *) hb_font_get_user_data (font, &hb_gr_data_key);
+    if (data)
+      return data;
+    else
+      return &_hb_gr_font_data_nil;
+  }
+
+  return data;
 }
 
-hb_font_funcs_t hbgr_klass = {
-    HB_OBJECT_HEADER_STATIC,
-    TRUE,
-    {
-        hbgr_font_get_glyph,
-        hbgr_font_get_glyph_h_advance,
-        hbgr_font_get_glyph_v_advance,
-        hbgr_font_get_glyph_h_origin,
-        hbgr_font_get_glyph_v_origin,
-        hbgr_font_get_glyph_h_kerning,
-        hbgr_font_get_glyph_v_kerning,
-        hbgr_font_get_glyph_extents,
-        hbgr_font_get_glyph_contour_point
-    }
-};
 
-// returns 0 = success, 1 = fallback
-static int hbgr_make_fonts(hb_font_t *font, hb_face_t *face)
+hb_bool_t
+hb_graphite_shape (hb_font_t          *font,
+		   hb_buffer_t        *buffer,
+		   const hb_feature_t *features,
+		   unsigned int        num_features,
+		   const char * const *shaper_options)
 {
-    // check face for having already added gr_face (based on destroy fn)
-    if (face->destroy != &hbgr_face_destroy)
-    {
-        hb_blob_t *silf_blob;
-        silf_blob = hb_face_reference_table (font->face, HB_GRAPHITE_TAG_Silf);
-        if (!hb_blob_get_length(silf_blob))     // return as early as possible if no Silf table
-        {
-            hb_blob_destroy(silf_blob);
-            return 1;
-        }
-        hbgr_tablelist_t *flist = (hbgr_tablelist_t *)malloc(sizeof(hbgr_tablelist_t));
-        hbgr_face_t *fcomp = (hbgr_face_t *)malloc(sizeof(hbgr_face_t));
-        if (!flist || !fcomp) return 1;
-        flist->next = NULL;
-        flist->tag = 0;
-        flist->blob = NULL;
-        fcomp->tlist = flist;
-        fcomp->get_table = face->reference_table;
-        fcomp->destroy = face->destroy;
-        fcomp->data = face->user_data;
-        fcomp->grface = gr_make_face(fcomp, &hbgr_get_table, gr_face_preloadGlyphs);
-        if (fcomp->grface)
-        {
-            face->user_data = fcomp;
-            face->reference_table = &hbgr_face_get_table;
-            face->destroy = &hbgr_face_destroy;
-        }
-        else
-        {
-            free(flist);
-            free(fcomp);
-            return 1;
-        }
-    }
 
-    // check font for having already added gr_font (based on destroy fn)
-    if (font->destroy != &hbgr_font_destroy)
+  buffer->guess_properties ();
+
+  hb_gr_font_data_t *data = _hb_gr_font_get_data (font);
+
+  unsigned int charlen;
+  hb_glyph_info_t *bufferi = hb_buffer_get_glyph_infos (buffer, &charlen);
+
+  int success = 0;
+
+  if (!charlen) return TRUE;
+
+  /* XXX(behdad): Do we need OT lang tag here? */
+  const char *lang = hb_language_to_string (hb_buffer_get_language (buffer));
+  gr_feature_val *feats = gr_face_featureval_for_lang (data->grface, lang ? hb_tag_from_string (lang) : 0);
+
+  while (num_features--)
+  {
+    const gr_feature_ref *fref = gr_face_find_fref (data->grface, features->tag);
+    if (fref)
+      gr_fref_set_feature_value (fref, features->value, feats);
+    features++;
+  }
+
+  unsigned short *gids = NULL;
+  hb_gr_cluster_t *clusters = NULL;
+  gr_segment *seg = NULL;
+  uint32_t *text = NULL;
+  unsigned short *pg;
+  const gr_slot *is;
+  unsigned int ci = 0, ic = 0;
+  float curradvx = 0., curradvy = 0.;
+  unsigned int glyphlen = 0;
+  unsigned int *p;
+
+  text = (uint32_t *) malloc ((charlen + 1) * sizeof (uint32_t));
+  if (!text) goto dieout;
+
+  p = text;
+  for (unsigned int i = 0; i < charlen; ++i)
+    *p++ = bufferi++->codepoint;
+  *p = 0;
+
+  hb_tag_t script_tag[2];
+  hb_ot_tags_from_script (hb_buffer_get_script(buffer), &script_tag[0], &script_tag[1]);
+
+  seg = gr_make_seg (data->grfont, data->grface,
+		     script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1],
+		     feats,
+		     gr_utf32, text, charlen,
+		     2 | (hb_buffer_get_direction(buffer) == HB_DIRECTION_RTL ? 1 : 0));
+  if (!seg) goto dieout;
+
+  glyphlen = gr_seg_n_slots (seg);
+  clusters = (hb_gr_cluster_t *) calloc (charlen, sizeof (hb_gr_cluster_t));
+  if (!glyphlen || !clusters) goto dieout;
+
+  gids = (unsigned short *) malloc (glyphlen * sizeof (unsigned short));
+  if (!gids) goto dieout;
+
+  pg = gids;
+  for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++)
+  {
+    unsigned int before = gr_slot_before (is);
+    unsigned int after = gr_slot_after (is);
+    *pg = gr_slot_gid (is);
+    *pg = hb_be_uint16 (*pg); // XXX insane: swap bytes so be16 can swap them back
+    pg++;
+    while (clusters[ci].base_char > before && ci)
     {
-        hbgr_font_t *fcomp = (hbgr_font_t *)malloc(sizeof(hbgr_font_t));
-        if (fcomp)
-        {
-            int scale;
-            gr_face *grface = ((hbgr_face_t *)(face->user_data))->grface;
-            hb_font_get_scale(font, &scale, NULL);
-            fcomp->face = face;
-            fcomp->font = font;
-            fcomp->destroy = font->destroy;
-            fcomp->klass = font->klass;
-            fcomp->data = font->user_data;
-            fcomp->grfont = gr_make_font_with_advance_fn(scale, fcomp, &hbgr_get_advance, grface);
-            if (fcomp->grfont)
-            {
-                font->destroy = &hbgr_font_destroy;
-                font->klass = &hbgr_klass;
-                font->user_data = fcomp;
-                return 0;
-            }
-        }
-        // something went wrong, back out of the whole thing
-        hbgr_face_t *facecomp = (hbgr_face_t *)(face->user_data);
-        free(facecomp->tlist);
-        face->reference_table = facecomp->get_table;
-        face->destroy = facecomp->destroy;
-        face->user_data = facecomp->data;
-        free(facecomp);
-        if (fcomp) free(fcomp);
-        return 1;
-    // create gr_font and put into struct along with stuff in font
-    // replace font component with struct and fns
+      clusters[ci-1].num_chars += clusters[ci].num_chars;
+      clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
+      --ci;
     }
-    return 0;
-}
-
-hb_bool_t hb_graphite_shape (hb_font_t *font,
-			hb_buffer_t  *buffer,
-			const hb_feature_t *features,
-			unsigned int  num_features,
-            const char * const *shaper_options)
-{
-    hb_face_t *face = font->face;
-    gr_face *grface = NULL;
-    gr_font *grfont = NULL;
-    gr_segment *seg = NULL;
-    unsigned int *text = NULL;
-    hbgr_cluster_t *clusters = NULL;
-    unsigned short *gids = NULL;
-    unsigned int charlen;
-    hb_glyph_info_t *bufferi = hb_buffer_get_glyph_infos(buffer, &charlen);
-
-    int success = 0;
-    unsigned int *p;
-    const gr_slot *is;
-    unsigned int glyphlen;
-    unsigned short *pg;
-    unsigned int ci = 0, ic = 0;
-    float curradvx = 0., curradvy = 0.;
-
-    if (!charlen || hbgr_make_fonts(font, face)) return 0;
-    grface = ((hbgr_face_t *)face->user_data)->grface;
-    grfont = ((hbgr_font_t *)font->user_data)->grfont;
-
-    const char *lang = hb_language_to_string(hb_buffer_get_language(buffer));
-    gr_feature_val *feats = gr_face_featureval_for_lang(grface, lang ? hb_tag_from_string(lang) : 0);
-
-    while (num_features--)
-    {
-        const gr_feature_ref *fref = gr_face_find_fref(grface, features->tag);
-        if (fref)
-            gr_fref_set_feature_value(fref, features->value, feats);
-        features++;
-    }
-    text = (unsigned int *)malloc((charlen + 1) * sizeof(unsigned int));
-    if (!text) goto dieout;
-    p = text;
-    for (unsigned int i = 0; i < charlen; ++i)
-        *p++ = bufferi++->codepoint;
-    *p = 0;
-
-    hb_tag_t    script_tag_1, script_tag_2;
-    hb_ot_tags_from_script(hb_buffer_get_script(buffer), &script_tag_1, &script_tag_2);
-    seg = gr_make_seg(grfont, grface, script_tag_2 == HB_TAG_NONE ? script_tag_1 : script_tag_2,
-            feats, gr_utf32, text, charlen, hb_buffer_get_direction(buffer) == HB_DIRECTION_RTL ? 3 : 0);
-    if (!seg) goto dieout;
-    glyphlen = gr_seg_n_slots(seg);
-    clusters = (hbgr_cluster_t *)malloc(charlen * sizeof(hbgr_cluster_t));
-    if (!glyphlen || !clusters) goto dieout;
-    memset(clusters, 0, charlen * sizeof(hbgr_cluster_t));
-    gids = (unsigned short *)malloc(glyphlen * sizeof(unsigned short));
-    if (!gids) goto dieout;
-    pg = gids;
-    for (is = gr_seg_first_slot(seg), ic = 0; is; is = gr_slot_next_in_segment(is), ic++)
-    {
-        unsigned int before = gr_slot_before(is);
-        unsigned int after = gr_slot_after(is);
-        *pg = gr_slot_gid(is);
-        *pg++ = hb_be_uint16(*pg);        // insane: swap bytes so be16 can swap them back
-        while (clusters[ci].base_char > before && ci)
-        {
-            clusters[ci-1].num_chars += clusters[ci].num_chars;
-            clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
-            --ci;
-        }
-
-        if (gr_slot_can_insert_before(is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
-        {
-            hbgr_cluster_t *c = clusters + ci + 1;
-            c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
-            c->num_chars = before - c->base_char;
-            c->base_glyph = ic;
-            c->num_glyphs = 0;
-            ++ci;
-        }
-        ++clusters[ci].num_glyphs;
-
-        if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
-            clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
-    }
-
-    buffer->clear_output();
-    for (unsigned int i = 0; i <= ci; ++i)
-        buffer->replace_glyphs_be16(clusters[i].num_chars, clusters[i].num_glyphs, gids + clusters[i].base_glyph);
-    buffer->swap_buffers();
 
-    hb_glyph_position_t *pPos;
-    for (pPos = hb_buffer_get_glyph_positions(buffer, NULL), is = gr_seg_first_slot(seg);
-            is; pPos++, is = gr_slot_next_in_segment(is))
+    if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
     {
-        pPos->x_offset = gr_slot_origin_X(is) - curradvx;
-        pPos->y_offset = gr_slot_origin_Y(is) - curradvy;
-        pPos->x_advance = gr_slot_advance_X(is, grface, grfont);
-        pPos->y_advance = gr_slot_advance_Y(is, grface, grfont);
-//        if (pPos->x_advance < 0 && gr_slot_attached_to(is))
-//            pPos->x_advance = 0;
-        curradvx += pPos->x_advance;
-        curradvy += pPos->y_advance;
+      hb_gr_cluster_t *c = clusters + ci + 1;
+      c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
+      c->num_chars = before - c->base_char;
+      c->base_glyph = ic;
+      c->num_glyphs = 0;
+      ++ci;
     }
-    pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
-    success = 1;
+    ++clusters[ci].num_glyphs;
+
+    if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
+	clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
+  }
+
+  buffer->clear_output();
+  for (unsigned int i = 0; i <= ci; ++i)
+    buffer->replace_glyphs_be16(clusters[i].num_chars, clusters[i].num_glyphs, gids + clusters[i].base_glyph);
+  buffer->swap_buffers();
+
+  hb_glyph_position_t *pPos;
+  for (pPos = hb_buffer_get_glyph_positions(buffer, NULL), is = gr_seg_first_slot(seg);
+       is; pPos++, is = gr_slot_next_in_segment(is))
+  {
+    pPos->x_offset = gr_slot_origin_X(is) - curradvx;
+    pPos->y_offset = gr_slot_origin_Y(is) - curradvy;
+    pPos->x_advance = gr_slot_advance_X(is, data->grface, data->grfont);
+    pPos->y_advance = gr_slot_advance_Y(is, data->grface, data->grfont);
+//    if (pPos->x_advance < 0 && gr_slot_attached_to(is))
+//      pPos->x_advance = 0;
+    curradvx += pPos->x_advance;
+    curradvy += pPos->y_advance;
+  }
+  pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
+  success = 1;
 
 dieout:
-    if (gids) free(gids);
-    if (clusters) free(clusters);
-    if (seg) gr_seg_destroy(seg);
-    if (text) free(text);
-    return success;
+  if (gids) free (gids);
+  if (clusters) free (clusters);
+  if (seg) gr_seg_destroy (seg);
+  if (text) free (text);
+  return success;
 }
diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc
index a1eabfd..9026bb0 100644
--- a/src/hb-uniscribe.cc
+++ b/src/hb-uniscribe.cc
@@ -117,6 +117,7 @@ _hb_uniscribe_face_get_data (hb_face_t *face)
   if (unlikely (!data))
     return &_hb_uniscribe_face_data_nil;
 
+
   hb_blob_t *blob = hb_face_reference_blob (face);
   unsigned int blob_length;
   const char *blob_data = hb_blob_get_data (blob, &blob_length);
@@ -129,6 +130,7 @@ _hb_uniscribe_face_get_data (hb_face_t *face)
   if (unlikely (!data->fh))
     DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
 
+
   if (unlikely (!hb_face_set_user_data (face, &hb_uniscribe_data_key, data,
 					(hb_destroy_func_t) _hb_uniscribe_face_data_destroy,
 					FALSE)))
commit 5072934c35bddc23d6bcb07a41010da51eb1b090
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 02:24:27 2011 +0200

    Minor

diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc
index 72b3b0a..a1eabfd 100644
--- a/src/hb-uniscribe.cc
+++ b/src/hb-uniscribe.cc
@@ -92,7 +92,7 @@ populate_log_font (LOGFONTW  *lf,
 }
 
 
-static hb_user_data_key_t uniscribe_data_key;
+static hb_user_data_key_t hb_uniscribe_data_key;
 
 
 static struct hb_uniscribe_face_data_t {
@@ -110,7 +110,7 @@ _hb_uniscribe_face_data_destroy (hb_uniscribe_face_data_t *data)
 static hb_uniscribe_face_data_t *
 _hb_uniscribe_face_get_data (hb_face_t *face)
 {
-  hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_data_key);
+  hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &hb_uniscribe_data_key);
   if (likely (data)) return data;
 
   data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
@@ -129,12 +129,12 @@ _hb_uniscribe_face_get_data (hb_face_t *face)
   if (unlikely (!data->fh))
     DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
 
-  if (unlikely (!hb_face_set_user_data (face, &uniscribe_data_key, data,
+  if (unlikely (!hb_face_set_user_data (face, &hb_uniscribe_data_key, data,
 					(hb_destroy_func_t) _hb_uniscribe_face_data_destroy,
 					FALSE)))
   {
     _hb_uniscribe_face_data_destroy (data);
-    data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_data_key);
+    data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &hb_uniscribe_data_key);
     if (data)
       return data;
     else
@@ -167,7 +167,7 @@ _hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data)
 static hb_uniscribe_font_data_t *
 _hb_uniscribe_font_get_data (hb_font_t *font)
 {
-  hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_data_key);
+  hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &hb_uniscribe_data_key);
   if (likely (data)) return data;
 
   data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
@@ -186,12 +186,12 @@ _hb_uniscribe_font_get_data (hb_font_t *font)
       DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
   }
 
-  if (unlikely (!hb_font_set_user_data (font, &uniscribe_data_key, data,
+  if (unlikely (!hb_font_set_user_data (font, &hb_uniscribe_data_key, data,
 					(hb_destroy_func_t) _hb_uniscribe_font_data_destroy,
 					FALSE)))
   {
     _hb_uniscribe_font_data_destroy (data);
-    data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_data_key);
+    data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &hb_uniscribe_data_key);
     if (data)
       return data;
     else
commit 46377396accf6b43792ffba553dcd9847608aa86
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 02:12:05 2011 +0200

    [configure] Fix graphite bits

diff --git a/configure.ac b/configure.ac
index 1bbe598..2dc9d7d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,14 +134,15 @@ if $have_icu; then
 fi
 AM_CONDITIONAL(HAVE_ICU, $have_icu)
 
-AC_ARG_ENABLE(graphite,
-    AC_HELP_STRING([--disable-graphite], [build without graphite support]))
+dnl ==========================================================================
 
 PKG_CHECK_MODULES(GRAPHITE, graphite2, have_graphite=true, have_graphite=false)
-if test "x$enable_graphite" != xno -a $have_graphite; then
+if $have_graphite; then
     AC_DEFINE(HAVE_GRAPHITE, 1, [Have Graphite library])
 fi
-AM_CONDITIONAL(HAVE_GRAPHITE, [test "x$enable_graphite" != xno -a $have_graphite])
+AM_CONDITIONAL(HAVE_GRAPHITE, $have_graphite)
+
+dnl ==========================================================================
 
 PKG_CHECK_MODULES(FREETYPE, freetype2 >= 2.3.8, have_freetype=true, have_freetype=false)
 if $have_freetype; then
commit 71388b3ee71c7d3b79f842db7588bd683691797c
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 02:09:04 2011 +0200

    [uniscribe] Minor

diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc
index 77b0f00..72b3b0a 100644
--- a/src/hb-uniscribe.cc
+++ b/src/hb-uniscribe.cc
@@ -92,12 +92,13 @@ populate_log_font (LOGFONTW  *lf,
 }
 
 
+static hb_user_data_key_t uniscribe_data_key;
+
+
 static struct hb_uniscribe_face_data_t {
   HANDLE fh;
 } _hb_uniscribe_face_data_nil = {0};
 
-static hb_user_data_key_t uniscribe_face_data_key;
-
 static void
 _hb_uniscribe_face_data_destroy (hb_uniscribe_face_data_t *data)
 {
@@ -109,7 +110,7 @@ _hb_uniscribe_face_data_destroy (hb_uniscribe_face_data_t *data)
 static hb_uniscribe_face_data_t *
 _hb_uniscribe_face_get_data (hb_face_t *face)
 {
-  hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key);
+  hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_data_key);
   if (likely (data)) return data;
 
   data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
@@ -128,12 +129,12 @@ _hb_uniscribe_face_get_data (hb_face_t *face)
   if (unlikely (!data->fh))
     DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
 
-  if (unlikely (!hb_face_set_user_data (face, &uniscribe_face_data_key, data,
+  if (unlikely (!hb_face_set_user_data (face, &uniscribe_data_key, data,
 					(hb_destroy_func_t) _hb_uniscribe_face_data_destroy,
 					FALSE)))
   {
     _hb_uniscribe_face_data_destroy (data);
-    data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key);
+    data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_data_key);
     if (data)
       return data;
     else
@@ -151,8 +152,6 @@ static struct hb_uniscribe_font_data_t {
   SCRIPT_CACHE script_cache;
 } _hb_uniscribe_font_data_nil = {NULL, NULL, NULL};
 
-static hb_user_data_key_t uniscribe_font_data_key;
-
 static void
 _hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data)
 {
@@ -168,7 +167,7 @@ _hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data)
 static hb_uniscribe_font_data_t *
 _hb_uniscribe_font_get_data (hb_font_t *font)
 {
-  hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key);
+  hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_data_key);
   if (likely (data)) return data;
 
   data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
@@ -187,12 +186,12 @@ _hb_uniscribe_font_get_data (hb_font_t *font)
       DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
   }
 
-  if (unlikely (!hb_font_set_user_data (font, &uniscribe_font_data_key, data,
+  if (unlikely (!hb_font_set_user_data (font, &uniscribe_data_key, data,
 					(hb_destroy_func_t) _hb_uniscribe_font_data_destroy,
 					FALSE)))
   {
     _hb_uniscribe_font_data_destroy (data);
-    data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key);
+    data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_data_key);
     if (data)
       return data;
     else
commit cd2b901027bd154e31aa509c0cb2d86633e36398
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 01:47:25 2011 +0200

    [graphite] Minor

diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc
index 493a9db..9282892 100644
--- a/src/hb-graphite2.cc
+++ b/src/hb-graphite2.cc
@@ -24,16 +24,16 @@
  */
 
 #include "hb-private.hh"
+
+#include "hb-graphite2.h"
+
 #include "hb-buffer-private.hh"
 #include "hb-font-private.hh"
 #include "hb-ot-tag.h"
+
 #include <graphite2/Font.h>
 #include <graphite2/Segment.h>
-#include "hb-graphite2.h"
-#include <stdlib.h>
-#include <string.h>
 
-HB_BEGIN_DECLS
 
 typedef struct hbgr_tablelist_t {
     hb_blob_t   *blob;
@@ -432,5 +432,3 @@ dieout:
     if (text) free(text);
     return success;
 }
-
-HB_END_DECLS
diff --git a/src/hb-shape.cc b/src/hb-shape.cc
index 2e86c3c..9357f81 100644
--- a/src/hb-shape.cc
+++ b/src/hb-shape.cc
@@ -31,7 +31,6 @@
 #include "hb-buffer-private.hh"
 
 #ifdef HAVE_GRAPHITE
-#include "hb-font-private.hh"
 #include "hb-graphite2.h"
 #endif
 #ifdef HAVE_UNISCRIBE
commit 834af3b48a1aca3e53811d1eb4ca09b582b8e598
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 01:45:47 2011 +0200

    [graphite] Remove hb_graphite2_feature_check()
    
    I don't see how this function can be useful.

diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc
index ccac015..493a9db 100644
--- a/src/hb-graphite2.cc
+++ b/src/hb-graphite2.cc
@@ -433,14 +433,4 @@ dieout:
     return success;
 }
 
-int hb_graphite2_feature_check(const hb_feature_t *feats, unsigned int num_feats)
-{
-    while (num_feats--)
-    {
-        if (feats[num_feats].tag == HB_TAG(' ', 'R', 'N', 'D'))
-            return (feats[num_feats].value == 1);
-    }
-    return 1;
-}
-
 HB_END_DECLS
diff --git a/src/hb-graphite2.h b/src/hb-graphite2.h
index 0d471d5..f3b7a68 100644
--- a/src/hb-graphite2.h
+++ b/src/hb-graphite2.h
@@ -40,8 +40,6 @@ hb_bool_t hb_graphite_shape (hb_font_t    *font,
 			unsigned int  num_features,
             const char * const *shaper_options);
 
-int hb_graphite2_feature_check(const hb_feature_t *feats, unsigned int num_feats);
-
 HB_END_DECLS
 
 #endif /* HB_GRAPHITE2_H */
commit 1f49cf32c96cb45a4d8ba2c210aeb7a8076b4762
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Wed Aug 24 01:29:25 2011 +0200

    Add graphite2 integration from Martin Hosken
    
    To be modified, a lot.

diff --git a/configure.ac b/configure.ac
index 5871675..1bbe598 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,7 +134,14 @@ if $have_icu; then
 fi
 AM_CONDITIONAL(HAVE_ICU, $have_icu)
 
-dnl ==========================================================================
+AC_ARG_ENABLE(graphite,
+    AC_HELP_STRING([--disable-graphite], [build without graphite support]))
+
+PKG_CHECK_MODULES(GRAPHITE, graphite2, have_graphite=true, have_graphite=false)
+if test "x$enable_graphite" != xno -a $have_graphite; then
+    AC_DEFINE(HAVE_GRAPHITE, 1, [Have Graphite library])
+fi
+AM_CONDITIONAL(HAVE_GRAPHITE, [test "x$enable_graphite" != xno -a $have_graphite])
 
 PKG_CHECK_MODULES(FREETYPE, freetype2 >= 2.3.8, have_freetype=true, have_freetype=false)
 if $have_freetype; then
diff --git a/contrib/python/lib/harfbuzz.pyx b/contrib/python/lib/harfbuzz.pyx
index f483fd6..3e3774e 100644
--- a/contrib/python/lib/harfbuzz.pyx
+++ b/contrib/python/lib/harfbuzz.pyx
@@ -29,17 +29,15 @@ cdef extern from "hb-common.h" :
     ctypedef unsigned long hb_tag_t
     hb_tag_t hb_tag_from_string (char *s)
     ctypedef void (*hb_destroy_func_t) (void *user_data)
+    ctypedef void *hb_language_t
+    hb_language_t hb_language_from_string(char *str)
+    char * hb_language_to_string(hb_language_t language)
 
 cdef extern from "hb-unicode.h" :
 # there must be a better way of syncing this list with the true source
     ctypedef enum hb_script_t :
         HB_SCRIPT_COMMON = 0
 
-cdef extern from "hb-language.h" :
-    ctypedef void *hb_language_t
-    hb_language_t hb_language_from_string(char *str)
-    char * hb_language_to_string(hb_language_t language)
-
 cdef extern from "hb-ot-tag.h" :
     hb_script_t hb_ot_tag_to_script (hb_tag_t tag)
 
@@ -80,8 +78,8 @@ cdef extern from "hb-buffer.h" :
     void hb_buffer_add_glyph(hb_buffer_t *buffer, hb_codepoint_t codepoint, hb_mask_t mask, unsigned int cluster)
     void hb_buffer_add_utf8(hb_buffer_t *buffer, char *text, unsigned int text_length, unsigned int item_offset, unsigned int item_length)
     unsigned int hb_buffer_get_length(hb_buffer_t *buffer)
-    hb_glyph_info_t *hb_buffer_get_glyph_infos(hb_buffer_t *buffer)
-    hb_glyph_position_t *hb_buffer_get_glyph_positions(hb_buffer_t *buffer)
+    hb_glyph_info_t *hb_buffer_get_glyph_infos(hb_buffer_t *buffer, unsigned int *len)
+    hb_glyph_position_t *hb_buffer_get_glyph_positions(hb_buffer_t *buffer, unsigned int *len)
 
 cdef extern from "hb-blob.h" :
     cdef struct hb_blob_t :
@@ -111,7 +109,7 @@ cdef extern from "hb-shape.h" :
         unsigned int start
         unsigned int end
 
-    void hb_shape (hb_font_t *font, hb_face_t *face, hb_buffer_t *buffer, hb_feature_t *features, unsigned int num_features)
+    void hb_shape (hb_font_t *font, hb_buffer_t *buffer, hb_feature_t *features, unsigned int num_features)
 
 cdef extern from "hb-ft.h" :
     hb_face_t *hb_ft_face_create (FT_Face ft_face, hb_destroy_func_t destroy)
@@ -156,8 +154,8 @@ cdef class buffer :
         res = []
 
         num = hb_buffer_get_length(self.buffer)
-        infos = hb_buffer_get_glyph_infos(self.buffer)
-        positions = hb_buffer_get_glyph_positions(self.buffer)
+        infos = hb_buffer_get_glyph_infos(self.buffer, &num)
+        positions = hb_buffer_get_glyph_positions(self.buffer, &num)
         for 0 <= i < num :
             temp = glyphinfo(infos[i].codepoint, infos[i].cluster, (positions[i].x_advance / scale, positions[i].y_advance / scale), (positions[i].x_offset / scale, positions[i].y_offset / scale), positions[i].var.u32)
             res.append(temp)
@@ -210,6 +208,6 @@ cdef class ft :
             aFeat.start = 0
             aFeat.end = -1
             aFeat += 1
-        hb_shape(self.hbfont, self.hbface, aBuffer.buffer, feats, len(features))
+        hb_shape(self.hbfont, aBuffer.buffer, feats, len(features))
 
 
diff --git a/contrib/python/runpy b/contrib/python/runpy
deleted file mode 100755
index b39db1b..0000000
--- a/contrib/python/runpy
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-LD_LIBRARY_PATH=../../src/.libs PYTHONPATH=build/lib.`python -c 'import distutils.util, sys; print distutils.util.get_platform()+"-"+str(sys.version_info[0])+"."+str(sys.version_info[1])'` "$@"
diff --git a/contrib/python/scripts/hbtestfont b/contrib/python/scripts/hbtestfont
index 7736ae6..4437105 100755
--- a/contrib/python/scripts/hbtestfont
+++ b/contrib/python/scripts/hbtestfont
@@ -100,9 +100,9 @@ if not opts.nogui :
     glyphs = []
     org = [0, 0]
     for g in res :
-        glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1]))
+        glyphs.append((g.gid, org[0] + g.offset[0], org[1] - g.offset[1]))
         org[0] += g.advance[0]
-        org[1] += g.advance[1]
+        org[1] -= g.advance[1]
 
     gobject.type_register(GlyphsWindow)
     win = gtk.Window()
diff --git a/contrib/python/setup.py b/contrib/python/setup.py
index f592728..681c53b 100755
--- a/contrib/python/setup.py
+++ b/contrib/python/setup.py
@@ -1,10 +1,23 @@
 #!/usr/bin/python
 
 from distutils.core import setup
+from optparse import OptionParser
 from glob import glob
 from Pyrex.Distutils.extension import Extension
 from Pyrex.Distutils import build_ext
 
+parser = OptionParser()
+parser.add_option('-b','--build', help='Build directory in which libraries are found. Relative to project root')
+parser.disable_interspersed_args()
+
+(opts, args) = parser.parse_args()
+
+rfile = file("runpy", "w")
+rfile.write("""#!/bin/sh
+LD_LIBRARY_PATH=../../%s/src/.libs PYTHONPATH=build/lib.`python -c 'import distutils.util, sys; print distutils.util.get_platform()+"-"+str(sys.version_info[0])+"."+str(sys.version_info[1])'` "$@"
+""" % opts.build)
+rfile.close()
+
 setup(name='harfbuzz',
     version='0.0.1',
     description='Harfbuzz compatibility layer',
@@ -13,13 +26,14 @@ setup(name='harfbuzz',
     maintainer_email='martin_hosken at sil.org',
     packages=['harfbuzz'],
     ext_modules = [
-        Extension("harfbuzz", ["lib/harfbuzz.pyx"], libraries=["harfbuzz"], library_dirs=["../../src/.libs"], include_dirs=["/usr/include/freetype2", "../../src"]),
+        Extension("harfbuzz", ["lib/harfbuzz.pyx"], libraries=["harfbuzz"], library_dirs=["../../%s/src/.libs" % opts.build], include_dirs=["/usr/include/freetype2", "../../src", "../../%s/src" % opts.build]),
         Extension("fontconfig", ["lib/fontconfig.pyx"], libraries=["fontconfig"])
         ],
     cmdclass = {'build_ext' : build_ext},
     scripts = glob('scripts/*'),
     license = 'LGPL',
     platforms = ['Linux', 'Win32', 'Mac OS X'],
-    package_dir = {'harfbuzz' : 'lib'}
+    package_dir = {'harfbuzz' : 'lib'},
+    script_args = args
 )
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 0b20709..95980a2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -116,6 +116,13 @@ HBSOURCES += hb-ft.cc
 HBHEADERS += hb-ft.h
 endif
 
+if HAVE_GRAPHITE
+HBCFLAGS += $(GRAPHITE_CFLAGS)
+HBLIBS   += $(GRAPHITE_LIBS)
+HBSOURCES += hb-graphite2.cc
+HBHEADERS += hb-graphite2.h
+endif
+
 if HAVE_UNISCRIBE
 HBCFLAGS += $(UNISCRIBE_CFLAGS)
 HBLIBS   += $(UNISCRIBE_LIBS)
diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc
new file mode 100644
index 0000000..ccac015
--- /dev/null
+++ b/src/hb-graphite2.cc
@@ -0,0 +1,446 @@
+/*
+ * Copyright (C) 2011  Martin Hosken
+ * Copyright (C) 2011  SIL International
+ *
+ *  This is part of HarfBuzz, a text shaping library.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#include "hb-private.hh"
+#include "hb-buffer-private.hh"
+#include "hb-font-private.hh"
+#include "hb-ot-tag.h"
+#include <graphite2/Font.h>
+#include <graphite2/Segment.h>
+#include "hb-graphite2.h"
+#include <stdlib.h>
+#include <string.h>
+
+HB_BEGIN_DECLS
+
+typedef struct hbgr_tablelist_t {
+    hb_blob_t   *blob;
+    struct hbgr_tablelist_t *next;
+    unsigned int tag;
+} hbgr_tablelist_t;
+
+typedef struct hbgr_face_t {
+    gr_face             *grface;
+    hbgr_tablelist_t    *tlist;
+    hb_destroy_func_t   destroy;
+    hb_reference_table_func_t get_table;
+    hb_face_t           *face;
+    void                *data;
+} hbgr_face_t;
+
+typedef struct hbgr_font_t {
+    gr_font             *grfont;
+    hb_face_t           *face;
+    hb_font_t           *font;
+    hb_destroy_func_t   destroy;
+    hb_font_funcs_t     *klass;
+    void                *data;
+} hbgr_font_t;
+
+typedef struct hbgr_cluster_t {
+    unsigned int base_char;
+    unsigned int num_chars;
+    unsigned int base_glyph;
+    unsigned int num_glyphs;
+} hbgr_cluster_t;
+
+static const void *hbgr_get_table(const void *face, unsigned int tag, size_t *len);
+static float hbgr_get_advance(const void *font, unsigned short gid);
+static void hbgr_face_destroy(void *data);
+static hb_blob_t *hbgr_face_get_table(hb_face_t *face, hb_tag_t tag, void *data);
+static void hbgr_font_destroy(void *data);
+/* static hb_codepoint_t hbgr_font_get_glyph (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t unicode, hb_codepoint_t variation_selector);
+static void hbgr_font_get_glyph_advance (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t glyph, hb_position_t *x_advance, hb_position_t *y_advance);
+static void hbgr_font_get_glyph_extents (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents);
+static hb_bool_t hbgr_font_get_contour_point (hb_font_t *font, hb_face_t *face, const void *user_data, unsigned int point_index, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y);
+static hb_position_t hbgr_font_get_kerning (hb_font_t *font, hb_face_t *face, const void *user_data, hb_codepoint_t first_glyph, hb_codepoint_t second_glyph);
+*/
+
+static const void *hbgr_get_table(const void *data, unsigned int tag, size_t *len)
+{
+    const void *res;
+    hbgr_tablelist_t *pl, *p;
+    hbgr_face_t *face = (hbgr_face_t *)data;
+    hbgr_tablelist_t *tlist = face->tlist;
+
+    for (p = tlist; p; p = p->next)
+        if (p->tag == tag)
+            return hb_blob_get_data(p->blob, (unsigned int *)len);
+        else
+            pl = p;
+
+    hb_blob_t *blob = face->get_table(face->face, tag, face->data);
+    if (pl->blob)
+    {
+        p = (hbgr_tablelist_t *)malloc(sizeof(hbgr_tablelist_t));
+        if (!p)
+        {
+            hb_blob_destroy(blob);
+            return NULL;
+        }
+        p->next = NULL;
+        pl->next = p;
+        pl = p;
+    }
+    pl->blob = blob;
+    pl->tag = tag;
+    return hb_blob_get_data(blob, (unsigned int *)len);
+}
+
+static float hbgr_get_advance(const void *dat, unsigned short gid)
+{
+    hbgr_font_t *font = (hbgr_font_t *)dat;
+    return (float)(font->klass->get.glyph_h_advance(font->font, font->data, gid, font->klass->user_data.glyph_h_advance));
+}
+
+static void hbgr_face_destroy(void *data)
+{
+    hbgr_face_t *fcomp = (hbgr_face_t *)data;
+    hbgr_tablelist_t *tlist = fcomp->tlist;
+    while (tlist)
+    {
+        hbgr_tablelist_t *old = tlist;
+        hb_blob_destroy(tlist->blob);
+        tlist = tlist->next;
+        free(old);
+    }
+    fcomp->destroy(fcomp->data);
+    gr_face_destroy(fcomp->grface);
+}
+
+static hb_blob_t *hbgr_face_get_table(hb_face_t *face, hb_tag_t tag, void *data)
+{
+    hbgr_face_t *fcomp = (hbgr_face_t *)data;
+    return fcomp->get_table(fcomp->face, tag, fcomp->data);
+}
+
+static void hbgr_font_destroy(void *data)
+{
+    hbgr_font_t *fcomp = (hbgr_font_t *)data;
+    fcomp->destroy(fcomp->data);
+    gr_font_destroy(fcomp->grfont);
+}
+
+static hb_bool_t hbgr_font_get_glyph (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t unicode,
+        hb_codepoint_t variation_selector,
+        hb_codepoint_t *glyph,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph(((hbgr_font_t *)font)->font, font_data, unicode, variation_selector, glyph, user_data);
+}
+
+static hb_bool_t hbgr_font_get_glyph_h_advance (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_h_advance(((hbgr_font_t *)font)->font, font_data, glyph, user_data);
+}
+
+static hb_position_t hbgr_font_get_glyph_v_advance (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_v_advance(((hbgr_font_t *)font)->font, font_data, glyph, user_data);
+}
+
+static hb_bool_t hbgr_font_get_glyph_h_origin (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        hb_position_t *x,
+        hb_position_t *y,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_h_origin(((hbgr_font_t *)font)->font, font_data, glyph, x, y, user_data);
+}
+
+static hb_bool_t hbgr_font_get_glyph_v_origin (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        hb_position_t *x,
+        hb_position_t *y,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_v_origin(((hbgr_font_t *)font)->font, font_data, glyph, x, y, user_data);
+}
+
+static hb_position_t hbgr_font_get_glyph_h_kerning (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t first_glyph,
+        hb_codepoint_t second_glyph,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_h_kerning(((hbgr_font_t *)font)->font, font_data, first_glyph, second_glyph, user_data);
+}
+
+static hb_position_t hbgr_font_get_glyph_v_kerning (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t first_glyph,
+        hb_codepoint_t second_glyph,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_v_kerning(((hbgr_font_t *)font)->font, font_data, first_glyph, second_glyph, user_data);
+}
+
+static hb_bool_t hbgr_font_get_glyph_extents (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        hb_glyph_extents_t *extents,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_extents(((hbgr_font_t *)font)->font, font_data, glyph, extents, user_data);
+}
+
+static hb_bool_t hbgr_font_get_glyph_contour_point (hb_font_t *font,
+        void *font_data,
+        hb_codepoint_t glyph,
+        unsigned int point_index,
+        hb_position_t *x,
+        hb_position_t *y,
+        void *user_data)
+{
+    return ((hbgr_font_t *)font)->klass->get.glyph_contour_point(((hbgr_font_t *)font)->font, font_data, glyph, point_index, x, y, user_data);
+}
+
+hb_font_funcs_t hbgr_klass = {
+    HB_OBJECT_HEADER_STATIC,
+    TRUE,
+    {
+        hbgr_font_get_glyph,
+        hbgr_font_get_glyph_h_advance,
+        hbgr_font_get_glyph_v_advance,
+        hbgr_font_get_glyph_h_origin,
+        hbgr_font_get_glyph_v_origin,
+        hbgr_font_get_glyph_h_kerning,
+        hbgr_font_get_glyph_v_kerning,
+        hbgr_font_get_glyph_extents,
+        hbgr_font_get_glyph_contour_point
+    }
+};
+
+// returns 0 = success, 1 = fallback
+static int hbgr_make_fonts(hb_font_t *font, hb_face_t *face)
+{
+    // check face for having already added gr_face (based on destroy fn)
+    if (face->destroy != &hbgr_face_destroy)
+    {
+        hb_blob_t *silf_blob;
+        silf_blob = hb_face_reference_table (font->face, HB_GRAPHITE_TAG_Silf);
+        if (!hb_blob_get_length(silf_blob))     // return as early as possible if no Silf table
+        {
+            hb_blob_destroy(silf_blob);
+            return 1;
+        }
+        hbgr_tablelist_t *flist = (hbgr_tablelist_t *)malloc(sizeof(hbgr_tablelist_t));
+        hbgr_face_t *fcomp = (hbgr_face_t *)malloc(sizeof(hbgr_face_t));
+        if (!flist || !fcomp) return 1;
+        flist->next = NULL;
+        flist->tag = 0;
+        flist->blob = NULL;
+        fcomp->tlist = flist;
+        fcomp->get_table = face->reference_table;
+        fcomp->destroy = face->destroy;
+        fcomp->data = face->user_data;
+        fcomp->grface = gr_make_face(fcomp, &hbgr_get_table, gr_face_preloadGlyphs);
+        if (fcomp->grface)
+        {
+            face->user_data = fcomp;
+            face->reference_table = &hbgr_face_get_table;
+            face->destroy = &hbgr_face_destroy;
+        }
+        else
+        {
+            free(flist);
+            free(fcomp);
+            return 1;
+        }
+    }
+
+    // check font for having already added gr_font (based on destroy fn)
+    if (font->destroy != &hbgr_font_destroy)
+    {
+        hbgr_font_t *fcomp = (hbgr_font_t *)malloc(sizeof(hbgr_font_t));
+        if (fcomp)
+        {
+            int scale;
+            gr_face *grface = ((hbgr_face_t *)(face->user_data))->grface;
+            hb_font_get_scale(font, &scale, NULL);
+            fcomp->face = face;
+            fcomp->font = font;
+            fcomp->destroy = font->destroy;
+            fcomp->klass = font->klass;
+            fcomp->data = font->user_data;
+            fcomp->grfont = gr_make_font_with_advance_fn(scale, fcomp, &hbgr_get_advance, grface);
+            if (fcomp->grfont)
+            {
+                font->destroy = &hbgr_font_destroy;
+                font->klass = &hbgr_klass;
+                font->user_data = fcomp;
+                return 0;
+            }
+        }
+        // something went wrong, back out of the whole thing
+        hbgr_face_t *facecomp = (hbgr_face_t *)(face->user_data);
+        free(facecomp->tlist);
+        face->reference_table = facecomp->get_table;
+        face->destroy = facecomp->destroy;
+        face->user_data = facecomp->data;
+        free(facecomp);
+        if (fcomp) free(fcomp);
+        return 1;
+    // create gr_font and put into struct along with stuff in font
+    // replace font component with struct and fns
+    }
+    return 0;
+}
+
+hb_bool_t hb_graphite_shape (hb_font_t *font,
+			hb_buffer_t  *buffer,
+			const hb_feature_t *features,
+			unsigned int  num_features,
+            const char * const *shaper_options)
+{
+    hb_face_t *face = font->face;
+    gr_face *grface = NULL;
+    gr_font *grfont = NULL;
+    gr_segment *seg = NULL;
+    unsigned int *text = NULL;
+    hbgr_cluster_t *clusters = NULL;
+    unsigned short *gids = NULL;
+    unsigned int charlen;
+    hb_glyph_info_t *bufferi = hb_buffer_get_glyph_infos(buffer, &charlen);
+
+    int success = 0;
+    unsigned int *p;
+    const gr_slot *is;
+    unsigned int glyphlen;
+    unsigned short *pg;
+    unsigned int ci = 0, ic = 0;
+    float curradvx = 0., curradvy = 0.;
+
+    if (!charlen || hbgr_make_fonts(font, face)) return 0;
+    grface = ((hbgr_face_t *)face->user_data)->grface;
+    grfont = ((hbgr_font_t *)font->user_data)->grfont;
+
+    const char *lang = hb_language_to_string(hb_buffer_get_language(buffer));
+    gr_feature_val *feats = gr_face_featureval_for_lang(grface, lang ? hb_tag_from_string(lang) : 0);
+
+    while (num_features--)
+    {
+        const gr_feature_ref *fref = gr_face_find_fref(grface, features->tag);
+        if (fref)
+            gr_fref_set_feature_value(fref, features->value, feats);
+        features++;
+    }
+    text = (unsigned int *)malloc((charlen + 1) * sizeof(unsigned int));
+    if (!text) goto dieout;
+    p = text;
+    for (unsigned int i = 0; i < charlen; ++i)
+        *p++ = bufferi++->codepoint;
+    *p = 0;
+
+    hb_tag_t    script_tag_1, script_tag_2;
+    hb_ot_tags_from_script(hb_buffer_get_script(buffer), &script_tag_1, &script_tag_2);
+    seg = gr_make_seg(grfont, grface, script_tag_2 == HB_TAG_NONE ? script_tag_1 : script_tag_2,
+            feats, gr_utf32, text, charlen, hb_buffer_get_direction(buffer) == HB_DIRECTION_RTL ? 3 : 0);
+    if (!seg) goto dieout;
+    glyphlen = gr_seg_n_slots(seg);
+    clusters = (hbgr_cluster_t *)malloc(charlen * sizeof(hbgr_cluster_t));
+    if (!glyphlen || !clusters) goto dieout;
+    memset(clusters, 0, charlen * sizeof(hbgr_cluster_t));
+    gids = (unsigned short *)malloc(glyphlen * sizeof(unsigned short));
+    if (!gids) goto dieout;
+    pg = gids;
+    for (is = gr_seg_first_slot(seg), ic = 0; is; is = gr_slot_next_in_segment(is), ic++)
+    {
+        unsigned int before = gr_slot_before(is);
+        unsigned int after = gr_slot_after(is);
+        *pg = gr_slot_gid(is);
+        *pg++ = hb_be_uint16(*pg);        // insane: swap bytes so be16 can swap them back
+        while (clusters[ci].base_char > before && ci)
+        {
+            clusters[ci-1].num_chars += clusters[ci].num_chars;
+            clusters[ci-1].num_glyphs += clusters[ci].num_glyphs;
+            --ci;
+        }
+
+        if (gr_slot_can_insert_before(is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars)
+        {
+            hbgr_cluster_t *c = clusters + ci + 1;
+            c->base_char = clusters[ci].base_char + clusters[ci].num_chars;
+            c->num_chars = before - c->base_char;
+            c->base_glyph = ic;
+            c->num_glyphs = 0;
+            ++ci;
+        }
+        ++clusters[ci].num_glyphs;
+
+        if (clusters[ci].base_char + clusters[ci].num_chars < after + 1)
+            clusters[ci].num_chars = after + 1 - clusters[ci].base_char;
+    }
+
+    buffer->clear_output();
+    for (unsigned int i = 0; i <= ci; ++i)
+        buffer->replace_glyphs_be16(clusters[i].num_chars, clusters[i].num_glyphs, gids + clusters[i].base_glyph);
+    buffer->swap_buffers();
+
+    hb_glyph_position_t *pPos;
+    for (pPos = hb_buffer_get_glyph_positions(buffer, NULL), is = gr_seg_first_slot(seg);
+            is; pPos++, is = gr_slot_next_in_segment(is))
+    {
+        pPos->x_offset = gr_slot_origin_X(is) - curradvx;
+        pPos->y_offset = gr_slot_origin_Y(is) - curradvy;
+        pPos->x_advance = gr_slot_advance_X(is, grface, grfont);
+        pPos->y_advance = gr_slot_advance_Y(is, grface, grfont);
+//        if (pPos->x_advance < 0 && gr_slot_attached_to(is))
+//            pPos->x_advance = 0;
+        curradvx += pPos->x_advance;
+        curradvy += pPos->y_advance;
+    }
+    pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx;
+    success = 1;
+
+dieout:
+    if (gids) free(gids);
+    if (clusters) free(clusters);
+    if (seg) gr_seg_destroy(seg);
+    if (text) free(text);
+    return success;
+}
+
+int hb_graphite2_feature_check(const hb_feature_t *feats, unsigned int num_feats)
+{
+    while (num_feats--)
+    {
+        if (feats[num_feats].tag == HB_TAG(' ', 'R', 'N', 'D'))
+            return (feats[num_feats].value == 1);
+    }
+    return 1;
+}
+
+HB_END_DECLS
diff --git a/src/hb-graphite2.h b/src/hb-graphite2.h
new file mode 100644
index 0000000..0d471d5
--- /dev/null
+++ b/src/hb-graphite2.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2011  Martin Hosken
+ * Copyright (C) 2011  SIL International
+ *
+ *  This is part of HarfBuzz, a text shaping library.
+ *
+ * Permission is hereby granted, without written agreement and without
+ * license or royalty fees, to use, copy, modify, and distribute this
+ * software and its documentation for any purpose, provided that the
+ * above copyright notice and the following two paragraphs appear in
+ * all copies of this software.
+ *
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
+ * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#ifndef HB_GRAPHITE2_H
+#define HB_GRAPHITE2_H
+
+#include "hb.h"
+#include "hb-shape.h"
+
+HB_BEGIN_DECLS
+
+
+#define HB_GRAPHITE_TAG_Silf HB_TAG('S','i','l','f')
+
+hb_bool_t hb_graphite_shape (hb_font_t    *font,
+			hb_buffer_t  *buffer,
+			const hb_feature_t *features,
+			unsigned int  num_features,
+            const char * const *shaper_options);
+
+int hb_graphite2_feature_check(const hb_feature_t *feats, unsigned int num_feats);
+
+HB_END_DECLS
+
+#endif /* HB_GRAPHITE2_H */
diff --git a/src/hb-shape.cc b/src/hb-shape.cc
index d42be6c..2e86c3c 100644
--- a/src/hb-shape.cc
+++ b/src/hb-shape.cc
@@ -30,6 +30,10 @@
 
 #include "hb-buffer-private.hh"
 
+#ifdef HAVE_GRAPHITE
+#include "hb-font-private.hh"
+#include "hb-graphite2.h"
+#endif
 #ifdef HAVE_UNISCRIBE
 # include "hb-uniscribe.h"
 #endif
@@ -50,6 +54,9 @@ static struct hb_shaper_pair_t {
   hb_shape_func_t func;
 } shapers[] = {
   /* v--- Add new shapers in the right place here */
+#ifdef HAVE_GRAPHITE
+  HB_SHAPER_IMPLEMENT (graphite),
+#endif
 #ifdef HAVE_UNISCRIBE
   HB_SHAPER_IMPLEMENT (uniscribe),
 #endif



More information about the HarfBuzz mailing list