Implement gvar table and variable glyf

Split from Ariza's "[subset] TrueType/CFF2 variable font" work on #1594
diff --git a/src/Makefile.sources b/src/Makefile.sources
index eff0ad3..31cd31f 100644
--- a/src/Makefile.sources
+++ b/src/Makefile.sources
@@ -75,6 +75,7 @@
 	hb-ot-hdmx-table.hh \
 	hb-ot-head-table.hh \
 	hb-ot-hhea-table.hh \
+	hb-ot-hmtx-table.cc \
 	hb-ot-hmtx-table.hh \
 	hb-ot-kern-table.hh \
 	hb-ot-layout-base-table.hh \
@@ -136,6 +137,7 @@
 	hb-ot-tag.cc \
 	hb-ot-var-avar-table.hh \
 	hb-ot-var-fvar-table.hh \
+	hb-ot-var-gvar-table.hh \
 	hb-ot-var-hvar-table.hh \
 	hb-ot-var-mvar-table.hh \
 	hb-ot-var.cc \
diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc
index fe1c360..2aa5a79 100644
--- a/src/harfbuzz.cc
+++ b/src/harfbuzz.cc
@@ -14,6 +14,7 @@
 #include "hb-ot-color.cc"
 #include "hb-ot-face.cc"
 #include "hb-ot-font.cc"
+#include "hb-ot-hmtx-table.cc"
 #include "hb-ot-layout.cc"
 #include "hb-ot-map.cc"
 #include "hb-ot-math.cc"
diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index 78ff6d2..4b7446e 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -160,7 +160,7 @@
 #endif
 
   hb_glyph_extents_t extents = {0};
-  if (ot_face->glyf->get_extents (glyph, &extents))
+  if (ot_face->glyf->get_extents (font, glyph, &extents))
   {
     const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
     hb_position_t tsb = vmtx.get_side_bearing (font, glyph);
@@ -188,7 +188,7 @@
 #if !defined(HB_NO_OT_FONT_BITMAP) && !defined(HB_NO_COLOR)
   if (!ret) ret = ot_face->sbix->get_extents (font, glyph, extents);
 #endif
-  if (!ret) ret = ot_face->glyf->get_extents (glyph, extents);
+  if (!ret) ret = ot_face->glyf->get_extents (font, glyph, extents);
 #ifndef HB_NO_OT_FONT_CFF
   if (!ret) ret = ot_face->cff1->get_extents (glyph, extents);
   if (!ret) ret = ot_face->cff2->get_extents (font, glyph, extents);
diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh
index 9a791bb..72c138f 100644
--- a/src/hb-ot-glyf-table.hh
+++ b/src/hb-ot-glyf-table.hh
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2015  Google, Inc.
+ * Copyright © 2019  Adobe Inc.
  * Copyright © 2019  Ebrahim Byagowi
  *
  *  This is part of HarfBuzz, a text shaping library.
@@ -23,6 +24,7 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  * Google Author(s): Behdad Esfahbod, Garret Rieger, Roderick Sheeter
+ * Adobe Author(s): Michiharu Ariza
  */
 
 #ifndef HB_OT_GLYF_TABLE_HH
@@ -30,6 +32,10 @@
 
 #include "hb-open-type.hh"
 #include "hb-ot-head-table.hh"
+#include "hb-ot-hmtx-table.hh"
+#include "hb-ot-var-gvar-table.hh"
+
+#include <float.h>
 
 namespace OT {
 
@@ -54,12 +60,11 @@
   }
 
   protected:
-  UnsizedArrayOf<HBUINT8>
-		dataZ;	/* Location data. */
+  UnsizedArrayOf<HBUINT8>	dataZ;		/* Location data. */
   public:
-  DEFINE_SIZE_MIN (0);  /* In reality, this is UNBOUNDED() type; but since we always
-			 * check the size externally, allow Null() object of it by
-			 * defining it _MIN instead. */
+  DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always
+			* check the size externally, allow Null() object of it by
+			* defining it _MIN instead. */
 };
 
 
@@ -111,7 +116,7 @@
 					    free);
 
     bool result = plan->add_table (HB_OT_TAG_loca, loca_blob)
-		  && _add_head_and_set_loca_version(plan, use_short_loca);
+		  && _add_head_and_set_loca_version (plan, use_short_loca);
 
     hb_blob_destroy (loca_blob);
     return result;
@@ -230,8 +235,7 @@
     if (!glyph_header.is_simple_glyph ()) return;  // only for simple glyphs
 
     unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset ();
-    const HBUINT16 &instruction_len = StructAtOffset<HBUINT16> (&glyph,
-								instruction_len_offset);
+    const HBUINT16 &instruction_len = StructAtOffset<HBUINT16> (&glyph, instruction_len_offset);
     (HBUINT16 &) instruction_len = 0;
   }
 
@@ -284,26 +288,17 @@
     bool is_composite_glyph () const { return numberOfContours < 0; }
     bool is_simple_glyph () const    { return numberOfContours > 0; }
 
-    void get_extents (hb_glyph_extents_t *extents) const
-    {
-      extents->x_bearing = hb_min (xMin, xMax);
-      extents->y_bearing = hb_max (yMin, yMax);
-      extents->width     = hb_max (xMin, xMax) - extents->x_bearing;
-      extents->height    = hb_min (yMin, yMax) - extents->y_bearing;
-    }
-
     bool has_data () const { return numberOfContours; }
 
-    protected:
-    HBINT16	numberOfContours;/* If the number of contours is
-				  * greater than or equal to zero,
-				  * this is a simple glyph; if negative,
-				  * this is a composite glyph. */
-    FWORD	xMin;		 /* Minimum x for coordinate data. */
-    FWORD	yMin;		 /* Minimum y for coordinate data. */
-    FWORD	xMax;		 /* Maximum x for coordinate data. */
-    FWORD	yMax;		 /* Maximum y for coordinate data. */
-    public:
+    HBINT16		numberOfContours;	/* If the number of contours is
+						 * greater than or equal to zero,
+						 * this is a simple glyph; if negative,
+						 * this is a composite glyph. */
+    FWORD		xMin;			/* Minimum x for coordinate data. */
+    FWORD		yMin;			/* Minimum y for coordinate data. */
+    FWORD		xMax;			/* Maximum x for coordinate data. */
+    FWORD		yMax;			/* Maximum y for coordinate data. */
+
     DEFINE_SIZE_STATIC (10);
   };
 
@@ -325,8 +320,8 @@
       UNSCALED_COMPONENT_OFFSET =  0x1000
     };
 
-    HBUINT16 flags;
-    HBGlyphID  glyphIndex;
+    HBUINT16	flags;
+    HBGlyphID	glyphIndex;
 
     unsigned int get_size () const
     {
@@ -346,6 +341,91 @@
       return size;
     }
 
+    bool is_anchored () const { return (flags & ARGS_ARE_XY_VALUES) == 0; }
+    void get_anchor_points (unsigned int &point1, unsigned int &point2) const
+    {
+      const HBUINT8 *p = &StructAfter<const HBUINT8> (glyphIndex);
+      if (flags & ARG_1_AND_2_ARE_WORDS)
+      {
+	point1 = ((const HBUINT16 *)p)[0];
+	point2 = ((const HBUINT16 *)p)[1];
+      }
+      else
+      {
+	point1 = p[0];
+	point2 = p[1];
+      }
+    }
+
+    void transform_points (contour_point_vector_t &points) const
+    {
+      float matrix[4];
+      contour_point_t trans;
+      if (get_transformation (matrix, trans))
+      {
+	if (scaled_offsets ())
+	{
+	  points.translate (trans);
+	  points.transform (matrix);
+	}
+	else
+	{
+	  points.transform (matrix);
+	  points.translate (trans);
+	}
+      }
+    }
+
+    protected:
+    bool scaled_offsets () const
+    { return (flags & (SCALED_COMPONENT_OFFSET|UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; }
+
+    bool get_transformation (float (&matrix)[4], contour_point_t &trans) const
+    {
+      matrix[0] = matrix[3] = 1.f;
+      matrix[1] = matrix[2] = 0.f;
+
+      int tx, ty;
+      const HBINT8 *p = &StructAfter<const HBINT8> (glyphIndex);
+      if (flags & ARG_1_AND_2_ARE_WORDS)
+      {
+	tx = *(const HBINT16 *)p;
+	p += HBINT16::static_size;
+	ty = *(const HBINT16 *)p;
+	p += HBINT16::static_size;
+      }
+      else
+      {
+	tx = *p++;
+	ty = *p++;
+      }
+      if (is_anchored ()) tx = ty = 0;
+
+      trans.init ((float)tx, (float)ty);
+
+      if (flags & WE_HAVE_A_SCALE)
+      {
+	matrix[0] = matrix[3] = ((const F2DOT14*)p)->to_float ();
+	return true;
+      }
+      else if (flags & WE_HAVE_AN_X_AND_Y_SCALE)
+      {
+	matrix[0] = ((const F2DOT14*)p)[0].to_float ();
+	matrix[3] = ((const F2DOT14*)p)[1].to_float ();
+	return true;
+      }
+      else if (flags & WE_HAVE_A_TWO_BY_TWO)
+      {
+	matrix[0] = ((const F2DOT14*)p)[0].to_float ();
+	matrix[1] = ((const F2DOT14*)p)[1].to_float ();
+	matrix[2] = ((const F2DOT14*)p)[2].to_float ();
+	matrix[3] = ((const F2DOT14*)p)[3].to_float ();
+	return true;
+      }
+      return tx || ty;
+    }
+
+    public:
     // TODO rewrite using new iterator framework if possible
     struct Iterator
     {
@@ -359,8 +439,7 @@
 	{
 	  const CompositeGlyphHeader *possible =
 	    &StructAfter<CompositeGlyphHeader, CompositeGlyphHeader> (*current);
-	  if (!in_range (possible))
-	    return false;
+	  if (unlikely (!in_range (possible))) return false;
 	  current = possible;
 	  return true;
 	}
@@ -417,12 +496,19 @@
       glyf_table = hb_sanitize_context_t ().reference_table<glyf> (face);
 
       num_glyphs = hb_max (1u, loca_table.get_length () / (short_offset ? 2 : 4)) - 1;
+
+      gvar_accel.init (face);
+      hmtx_accel.init (face);
+      vmtx_accel.init (face);
     }
 
     void fini ()
     {
       loca_table.destroy ();
       glyf_table.destroy ();
+      gvar_accel.fini ();
+      hmtx_accel.fini ();
+      vmtx_accel.fini ();
     }
 
     /*
@@ -457,12 +543,323 @@
       FLAG_RESERVED2 = 0x80
     };
 
+    enum phantom_point_index_t {
+      PHANTOM_LEFT = 0,
+      PHANTOM_RIGHT = 1,
+      PHANTOM_TOP = 2,
+      PHANTOM_BOTTOM = 3,
+      PHANTOM_COUNT = 4
+    };
+
+    protected:
+    const GlyphHeader &get_header (hb_codepoint_t glyph) const
+    {
+      unsigned int start_offset, end_offset;
+      if (!get_offsets (glyph, &start_offset, &end_offset) || end_offset - start_offset < GlyphHeader::static_size)
+	return Null(GlyphHeader);
+
+      return StructAtOffset<GlyphHeader> (glyf_table, start_offset);
+    }
+
+    struct x_setter_t
+    {
+      void set (contour_point_t &point, float v) const { point.x = v; }
+      bool is_short (uint8_t flag) const { return (flag & FLAG_X_SHORT) != 0; }
+      bool is_same (uint8_t flag) const { return (flag & FLAG_X_SAME) != 0; }
+    };
+
+    struct y_setter_t
+    {
+      void set (contour_point_t &point, float v) const { point.y = v; }
+      bool is_short (uint8_t flag) const { return (flag & FLAG_Y_SHORT) != 0; }
+      bool is_same (uint8_t flag) const { return (flag & FLAG_Y_SAME) != 0; }
+    };
+
+    template <typename T>
+    static bool read_points (const HBUINT8 *&p /* IN/OUT */,
+			     contour_point_vector_t &points_ /* IN/OUT */,
+			     const range_checker_t &checker)
+    {
+      T coord_setter;
+      float v = 0;
+      for (unsigned int i = 0; i < points_.length - PHANTOM_COUNT; i++)
+      {
+	uint8_t flag = points_[i].flag;
+	if (coord_setter.is_short (flag))
+	{
+	  if (unlikely (!checker.in_range (p))) return false;
+	  if (coord_setter.is_same (flag))
+	    v += *p++;
+	  else
+	    v -= *p++;
+	}
+	else
+	{
+	  if (!coord_setter.is_same (flag))
+	  {
+	    if (unlikely (!checker.in_range ((const HBUINT16 *)p))) return false;
+	    v += *(const HBINT16 *)p;
+	    p += HBINT16::static_size;
+	  }
+	}
+	coord_setter.set (points_[i], v);
+      }
+      return true;
+    }
+
+    void init_phantom_points (hb_codepoint_t glyph, hb_array_t<contour_point_t> &phantoms /* IN/OUT */) const
+    {
+      const GlyphHeader &header = get_header (glyph);
+      int h_delta = (int)header.xMin - hmtx_accel.get_side_bearing (glyph);
+      int v_orig  = (int)header.yMax + vmtx_accel.get_side_bearing (glyph);
+      unsigned int h_adv = hmtx_accel.get_advance (glyph);
+      unsigned int v_adv = vmtx_accel.get_advance (glyph);
+
+      phantoms[PHANTOM_LEFT].x = h_delta;
+      phantoms[PHANTOM_RIGHT].x = h_adv + h_delta;
+      phantoms[PHANTOM_TOP].y = v_orig;
+      phantoms[PHANTOM_BOTTOM].y = -(int)v_adv + v_orig;
+    }
+
+    /* for a simple glyph, return contour end points, flags, along with coordinate points
+     * for a composite glyph, return pseudo component points
+     * in both cases points trailed with four phantom points
+     */
+    bool get_contour_points (hb_codepoint_t glyph,
+			     contour_point_vector_t &points_ /* OUT */,
+			     hb_vector_t<unsigned int> &end_points_ /* OUT */,
+			     const bool phantom_only=false) const
+    {
+      unsigned int num_points = 0;
+      unsigned int start_offset, end_offset;
+      if (unlikely (!get_offsets (glyph, &start_offset, &end_offset))) return false;
+      if (unlikely (end_offset - start_offset < GlyphHeader::static_size))
+      {
+      	/* empty glyph */
+	points_.resize (PHANTOM_COUNT);
+	for (unsigned int i = 0; i < points_.length; i++) points_[i].init ();
+      	return true;
+      }
+
+      CompositeGlyphHeader::Iterator composite;
+      if (get_composite (glyph, &composite))
+      {
+      	/* For a composite glyph, add one pseudo point for each component */
+	do { num_points++; } while (composite.move_to_next());
+	points_.resize (num_points + PHANTOM_COUNT);
+	for (unsigned int i = 0; i < points_.length; i++) points_[i].init ();
+	return true;
+      }
+
+      const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyf_table, start_offset);
+      int16_t num_contours = (int16_t) glyph_header.numberOfContours;
+      const HBUINT16 *end_pts = &StructAfter<HBUINT16, GlyphHeader> (glyph_header);
+
+      range_checker_t checker (glyf_table, start_offset, end_offset);
+      num_points = 0;
+      if (num_contours > 0)
+      {
+	if (unlikely (!checker.in_range (&end_pts[num_contours + 1]))) return false;
+	num_points = end_pts[num_contours - 1] + 1;
+      }
+      else if (num_contours < 0)
+      {
+	CompositeGlyphHeader::Iterator composite;
+	if (unlikely (!get_composite (glyph, &composite))) return false;
+	do
+	{
+	  num_points++;
+	} while (composite.move_to_next());
+      }
+
+      points_.resize (num_points + PHANTOM_COUNT);
+      for (unsigned int i = 0; i < points_.length; i++) points_[i].init ();
+      if ((num_contours <= 0) || phantom_only) return true;
+
+      /* Read simple glyph points if !phantom_only */
+      end_points_.resize (num_contours);
+
+      for (int16_t i = 0; i < num_contours; i++)
+      	end_points_[i] = end_pts[i];
+
+      /* Skip instructions */
+      const HBUINT8 *p = &StructAtOffset<HBUINT8> (&end_pts[num_contours+1], end_pts[num_contours]);
+
+      /* Read flags */
+      for (unsigned int i = 0; i < num_points; i++)
+      {
+      	if (unlikely (!checker.in_range (p))) return false;
+      	uint8_t flag = *p++;
+	points_[i].flag = flag;
+	if ((flag & FLAG_REPEAT) != 0)
+	{
+	  if (unlikely (!checker.in_range (p))) return false;
+	  unsigned int repeat_count = *p++;
+	  while ((repeat_count-- > 0) && (++i < num_points))
+	    points_[i].flag = flag;
+	}
+      }
+
+      /* Read x & y coordinates */
+      return (read_points<x_setter_t> (p, points_, checker) &&
+	      read_points<y_setter_t> (p, points_, checker));
+    }
+
+    struct contour_bounds_t
+    {
+      contour_bounds_t () { min.x = min.y = FLT_MAX; max.x = max.y = -FLT_MAX; }
+
+      void add (const contour_point_t &p)
+      {
+	min.x = hb_min (min.x, p.x);
+	min.y = hb_min (min.y, p.y);
+	max.x = hb_max (max.x, p.x);
+	max.y = hb_max (max.y, p.y);
+      }
+
+      bool empty () const { return (min.x >= max.x) || (min.y >= max.y); }
+
+      contour_point_t	min;
+      contour_point_t	max;
+    };
+
+    /* Note: Recursively calls itself.
+     * all_points includes phantom points
+     */
+    bool get_points_var (hb_codepoint_t glyph,
+			 const int *coords, unsigned int coord_count,
+			 contour_point_vector_t &all_points /* OUT */,
+			 unsigned int depth=0) const
+    {
+      if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return false;
+      contour_point_vector_t	points;
+      hb_vector_t<unsigned int>	end_points;
+      if (unlikely (!get_contour_points (glyph, points, end_points))) return false;
+      hb_array_t<contour_point_t>	phantoms = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT);
+      init_phantom_points (glyph, phantoms);
+      if (unlikely (!gvar_accel.apply_deltas_to_points (glyph, coords, coord_count, points.as_array (), end_points.as_array ()))) return false;
+
+      unsigned int comp_index = 0;
+      CompositeGlyphHeader::Iterator composite;
+      if (!get_composite (glyph, &composite))
+      {
+      	/* simple glyph */
+	all_points.extend (points.as_array ());
+      }
+      else
+      {
+	/* composite glyph */
+	do
+	{
+	  contour_point_vector_t comp_points;
+	  if (unlikely (!get_points_var (composite.current->glyphIndex, coords, coord_count,
+	  				 comp_points, depth))
+	      		|| comp_points.length < PHANTOM_COUNT) return false;
+
+	  /* Copy phantom points from component if USE_MY_METRICS flag set */
+	  if (composite.current->flags & CompositeGlyphHeader::USE_MY_METRICS)
+	    for (unsigned int i = 0; i < PHANTOM_COUNT; i++)
+	      phantoms[i] = comp_points[comp_points.length - PHANTOM_COUNT + i];
+
+	  /* Apply component transformation & translation */
+	  composite.current->transform_points (comp_points);
+
+	  /* Apply translatation from gvar */
+	  comp_points.translate (points[comp_index]);
+
+	  if (composite.current->is_anchored ())
+	  {
+	    unsigned int p1, p2;
+	    composite.current->get_anchor_points (p1, p2);
+	    if (likely (p1 < all_points.length && p2 < comp_points.length))
+	    {
+	      contour_point_t	delta;
+	      delta.init (all_points[p1].x - comp_points[p2].x,
+	      		  all_points[p1].y - comp_points[p2].y);
+
+	      comp_points.translate (delta);
+	    }
+	  }
+
+	  all_points.extend (comp_points.sub_array (0, comp_points.length - PHANTOM_COUNT));
+
+	  comp_index++;
+	} while (composite.move_to_next());
+
+	all_points.extend (phantoms);
+      }
+
+      return true;
+    }
+
+    bool get_var_extents_and_phantoms (hb_codepoint_t glyph,
+				       const int *coords, unsigned int coord_count,
+				       hb_glyph_extents_t *extents=nullptr /* OUt */,
+				       contour_point_vector_t *phantoms=nullptr /* OUT */) const
+    {
+      contour_point_vector_t all_points;
+      if (unlikely (!get_points_var (glyph, coords, coord_count, all_points) ||
+      		    all_points.length < PHANTOM_COUNT)) return false;
+
+      /* Undocumented rasterizer behavior:
+       * Shift points horizontally by the updated left side bearing
+       */
+      contour_point_t delta;
+      delta.init (-all_points[all_points.length - PHANTOM_COUNT + PHANTOM_LEFT].x, 0.f);
+      if (delta.x != 0.f) all_points.translate (delta);
+
+      if (extents != nullptr)
+      {
+	contour_bounds_t	bounds;
+	for (unsigned int i = 0; i + PHANTOM_COUNT < all_points.length; i++)
+	  bounds.add (all_points[i]);
+
+	if (bounds.min.x > bounds.max.x)
+	{
+	  extents->width = 0;
+	  extents->x_bearing = 0;
+	}
+	else
+	{
+	  extents->x_bearing = (int32_t)floorf (bounds.min.x);
+	  extents->width = (int32_t)ceilf (bounds.max.x) - extents->x_bearing;
+	}
+	if (bounds.min.y > bounds.max.y)
+	{
+	  extents->height = 0;
+	  extents->y_bearing = 0;
+	}
+	else
+	{
+	  extents->y_bearing = (int32_t)ceilf (bounds.max.y);
+	  extents->height = (int32_t)floorf (bounds.min.y) - extents->y_bearing;
+	}
+      }
+      if (phantoms != nullptr)
+      {
+      	for (unsigned int i = 0; i < PHANTOM_COUNT; i++)
+	  (*phantoms)[i] = all_points[all_points.length - PHANTOM_COUNT + i];
+      }
+      return true;
+    }
+
+    bool get_var_metrics (hb_codepoint_t glyph,
+			  const int *coords, unsigned int coord_count,
+			  contour_point_vector_t &phantoms) const
+    { return get_var_extents_and_phantoms (glyph, coords, coord_count, nullptr, &phantoms); }
+
+    bool get_extents_var (hb_codepoint_t glyph,
+			  const int *coords, unsigned int coord_count,
+			  hb_glyph_extents_t *extents) const
+    { return get_var_extents_and_phantoms (glyph, coords, coord_count, extents); }
+
+    public:
     /* based on FontTools _g_l_y_f.py::trim */
     bool remove_padding (unsigned int start_offset,
 			 unsigned int *end_offset) const
     {
-      const char *glyph = ((const char *) glyf_table) + start_offset;
       unsigned int glyph_length = *end_offset - start_offset;
+      const char *glyph = ((const char *) glyf_table) + start_offset;
       const GlyphHeader &glyph_header = *hb_bytes_t (glyph, glyph_length).as<GlyphHeader> ();
       if (!glyph_header.has_data ()) return true;
 
@@ -599,18 +996,15 @@
 	unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset ();
 	if (unlikely (instruction_len_offset + 2 > glyph.length))
 	{
-	  DEBUG_MSG (SUBSET, nullptr, "Glyph size is too short, missing field "
-				      "instructionLength.");
+	  DEBUG_MSG(SUBSET, nullptr, "Glyph size is too short, missing field instructionLength.");
 	  return false;
 	}
 
-	const HBUINT16 &instruction_len = StructAtOffset<HBUINT16> (&glyph,
-								    instruction_len_offset);
+	const HBUINT16 &instruction_len = StructAtOffset<HBUINT16> (&glyph, instruction_len_offset);
 	/* Out of bounds of the current glyph */
 	if (unlikely (glyph_header.simple_length (instruction_len) > glyph.length))
 	{
-	  DEBUG_MSG (SUBSET, nullptr, "The instructions array overruns the "
-				      "glyph's boundaries.");
+	  DEBUG_MSG(SUBSET, nullptr, "The instructions array overruns the glyph's boundaries.");
 	  return false;
 	}
 	*length = (uint16_t) instruction_len;
@@ -618,42 +1012,95 @@
       return true;
     }
 
-    bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
+    unsigned int get_advance_var (hb_codepoint_t glyph,
+				  const int *coords, unsigned int coord_count,
+				  bool vertical) const
     {
+      bool success = false;
+      contour_point_vector_t	phantoms;
+      phantoms.resize (PHANTOM_COUNT);
+
+      if (likely (coord_count == gvar_accel.get_axis_count ()))
+	success = get_var_metrics (glyph, coords, coord_count, phantoms);
+
+      if (unlikely (!success))
+	return vertical? vmtx_accel.get_advance (glyph): hmtx_accel.get_advance (glyph);
+
+      if (vertical)
+	return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y);
+      else
+	return roundf (phantoms[PHANTOM_RIGHT].x - phantoms[PHANTOM_LEFT].x);
+    }
+
+    int get_side_bearing_var (hb_codepoint_t glyph, const int *coords, unsigned int coord_count, bool vertical) const
+    {
+      hb_glyph_extents_t extents;
+      contour_point_vector_t	phantoms;
+      phantoms.resize (PHANTOM_COUNT);
+
+      if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms)))
+	return vertical? vmtx_accel.get_side_bearing (glyph): hmtx_accel.get_side_bearing (glyph);
+
+      return vertical? (int)ceilf (phantoms[PHANTOM_TOP].y) - extents.y_bearing: (int)floorf (phantoms[PHANTOM_LEFT].x);
+    }
+
+    bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
+    {
+      unsigned int coord_count;
+      const int *coords = hb_font_get_var_coords_normalized (font, &coord_count);
+      if (coords && coord_count > 0 && coord_count == gvar_accel.get_axis_count ())
+	return get_extents_var (glyph, coords, coord_count, extents);
+
       unsigned int start_offset, end_offset;
       if (!get_offsets (glyph, &start_offset, &end_offset))
 	return false;
 
-      hb_bytes_t ((const char *) glyf_table + start_offset,
-		  end_offset - start_offset).as<GlyphHeader> ()->get_extents (extents);
+      if (end_offset - start_offset < GlyphHeader::static_size)
+	return true; /* Empty glyph; zero extents. */
+
+      const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyf_table, start_offset);
+
+      /* Undocumented rasterizer behavior: shift glyph to the left by (lsb - xMin), i.e., xMin = lsb */
+      /* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */
+      extents->x_bearing = hmtx_accel.get_side_bearing (glyph);
+      extents->y_bearing = hb_max (glyph_header.yMin, glyph_header.yMax);
+      extents->width     = hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax);
+      extents->height    = hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing;
+
       return true;
     }
 
-    hb_bytes_t bytes_for_glyph (const char *glyf, hb_codepoint_t gid)
+  hb_bytes_t bytes_for_glyph (const char * glyf, hb_codepoint_t gid)
+  {
+    unsigned int start_offset, end_offset;
+    if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) &&
+      remove_padding (start_offset, &end_offset))))
     {
-      unsigned int start_offset, end_offset;
-      if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) &&
-		      remove_padding (start_offset, &end_offset))))
-      {
-	DEBUG_MSG (SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid);
-	return hb_bytes_t ();
-      }
-      hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset);
-      if (!glyph_bytes.as<GlyphHeader> ()->has_data ())
-      {
-	DEBUG_MSG (SUBSET, nullptr, "Empty or invalid glyph size, %d", gid);
-	return hb_bytes_t ();
-      }
-      return glyph_bytes;
+      DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid);
+      return hb_bytes_t ();
     }
+    hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset);
+    if (!glyph_bytes.as<GlyphHeader> ()->has_data ())
+    {
+      DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid);
+      return hb_bytes_t ();
+    }
+    return glyph_bytes;
+  }
 
     private:
     bool short_offset;
     unsigned int num_glyphs;
     hb_blob_ptr_t<loca> loca_table;
     hb_blob_ptr_t<glyf> glyf_table;
+
+    /* variable font support */
+    gvar::accelerator_t		gvar_accel;
+    hmtx::accelerator_t		hmtx_accel;
+    vmtx::accelerator_t		vmtx_accel;
   };
 
+
   struct SubsetGlyph
   {
     hb_codepoint_t new_gid;
@@ -662,38 +1109,38 @@
     hb_bytes_t dest_start;  /* region of source_glyph to copy first */
     hb_bytes_t dest_end;    /* region of source_glyph to copy second */
 
-    bool serialize (hb_serialize_context_t *c,
-		    const hb_subset_plan_t *plan) const
+
+  bool serialize (hb_serialize_context_t *c,
+		  const hb_subset_plan_t *plan) const
+  {
+    TRACE_SERIALIZE (this);
+
+    hb_bytes_t dest_glyph = dest_start.copy (c);
+    dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy(c).length);
+    unsigned int pad_length = padding ();
+    DEBUG_MSG(SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length  + pad_length, pad_length);
+
+    HBUINT8 pad;
+    pad = 0;
+    while (pad_length > 0)
     {
-      TRACE_SERIALIZE (this);
-
-      hb_bytes_t dest_glyph = dest_start.copy (c);
-      dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy (c).length);
-      unsigned int pad_length = padding ();
-      DEBUG_MSG (SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d",
-		 dest_glyph.length, dest_glyph.length  + pad_length, pad_length);
-
-      HBUINT8 pad;
-      pad = 0;
-      while (pad_length > 0)
-      {
-	c->embed (pad);
-	pad_length--;
-      }
-
-      if (dest_glyph.length)
-      {
-	_fix_component_gids (plan, dest_glyph);
-	if (plan->drop_hints)
-	{
-	  _zero_instruction_length (dest_glyph);
-	  c->check_success (_remove_composite_instruction_flag (dest_glyph));
-	}
-      }
-
-      return_trace (true);
+      c->embed(pad);
+      pad_length--;
     }
 
+    if (dest_glyph.length)
+    {
+      _fix_component_gids (plan, dest_glyph);
+      if (plan->drop_hints)
+      {
+	_zero_instruction_length (dest_glyph);
+	c->check_success (_remove_composite_instruction_flag (dest_glyph));
+      }
+    }
+
+    return_trace (true);
+  }
+
     void drop_hints (const OT::glyf::accelerator_t& glyf)
     {
       if (source_glyph.length == 0) return;
@@ -703,13 +1150,12 @@
       {
 	DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d",
 		   new_gid);
-	return ;
+	return;
       }
 
       const GlyphHeader& header = *source_glyph.as<GlyphHeader> ();
-      DEBUG_MSG (SUBSET, nullptr, "new_gid %d drop %d instruction bytes "
-				  "from %d byte source glyph",
-		 new_gid, instruction_len, source_glyph.length);
+      DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d",
+		 new_gid);
       if (header.is_composite_glyph ())
       {
 	/* just chop instructions off the end for composite glyphs */
@@ -728,9 +1174,9 @@
       }
     }
 
-    unsigned int length () const      { return dest_start.length + dest_end.length; }
+    unsigned int      length () const { return dest_start.length + dest_end.length; }
     /* pad to 2 to ensure 2-byte loca will be ok */
-    unsigned int padding () const     { return length () % 2; }
+    unsigned int     padding () const { return length () % 2; }
     unsigned int padded_size () const { return length () + padding (); }
   };
 
diff --git a/src/hb-ot-hmtx-table.cc b/src/hb-ot-hmtx-table.cc
new file mode 100644
index 0000000..012a9f2
--- /dev/null
+++ b/src/hb-ot-hmtx-table.cc
@@ -0,0 +1,54 @@
+/*
+ * Copyright © 2019 Adobe 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.
+ *
+ * Adobe Author(s): Michiharu Ariza
+ */
+
+#include "hb-ot-hmtx-table.hh"
+#include "hb-ot-glyf-table.hh"
+
+namespace OT {
+
+int hmtxvmtx_accelerator_base_t::get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical)
+{
+  glyf::accelerator_t glyf_accel;
+  glyf_accel.init (font->face);
+
+  int side_bearing = glyf_accel.get_side_bearing_var (glyph, font->coords, font->num_coords, vertical);
+  glyf_accel.fini ();
+
+  return side_bearing;
+}
+
+unsigned int hmtxvmtx_accelerator_base_t::get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical)
+{
+  glyf::accelerator_t glyf_accel;
+  glyf_accel.init (font->face);
+
+  unsigned int advance = glyf_accel.get_advance_var (glyph, font->coords, font->num_coords, vertical);
+  glyf_accel.fini ();
+
+  return advance;
+}
+
+}
diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh
index f8788b2..90c9c48 100644
--- a/src/hb-ot-hmtx-table.hh
+++ b/src/hb-ot-hmtx-table.hh
@@ -55,14 +55,8 @@
 
 struct hmtxvmtx_accelerator_base_t
 {
-  static int get_side_bearing_var_tt (hb_font_t *font HB_UNUSED,
-				      hb_codepoint_t glyph HB_UNUSED,
-				      bool is_vertical HB_UNUSED)
-  { return 0; } /* Not implemented yet */
-  static unsigned int get_advance_var_tt (hb_font_t *font HB_UNUSED,
-					  hb_codepoint_t glyph HB_UNUSED,
-					  bool is_vertical HB_UNUSED)
-  { return 0; } /* Not implemented yet */
+  HB_INTERNAL static int get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical);
+  HB_INTERNAL static unsigned int get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical);
 };
 
 template <typename T, typename H>
@@ -140,13 +134,12 @@
     auto it =
     + hb_range (c->plan->num_output_glyphs ())
     | hb_map ([c, &_mtx] (unsigned _)
-	{
-	  hb_codepoint_t old_gid;
-	  if (c->plan->old_gid_for_new_gid (_, &old_gid))
-	    return hb_pair (_mtx.get_advance (old_gid), _mtx.get_side_bearing (old_gid));
-	  else
-	    return hb_pair (0u, 0);
-	})
+	      {
+		hb_codepoint_t old_gid;
+		if (!c->plan->old_gid_for_new_gid (_, &old_gid))
+		  return hb_pair (0u, 0);
+		return hb_pair (_mtx.get_advance (old_gid), _mtx.get_side_bearing (old_gid));
+	      })
     ;
 
     table_prime->serialize (c->serializer, it, num_advances);
@@ -158,9 +151,7 @@
 
     // Amend header num hmetrics
     if (unlikely (!subset_update_header (c->plan, num_advances)))
-    {
       return_trace (false);
-    }
 
     return_trace (true);
   }
@@ -221,8 +212,8 @@
       if (unlikely (glyph >= num_metrics) || !font->num_coords)
 	return side_bearing;
 
-//       if (var_table.get_blob () == &Null (hb_blob_t))
-// 	return get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
+      if (var_table.get_blob () == &Null (hb_blob_t))
+	return get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
 
       return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?!
     }
@@ -251,8 +242,8 @@
       if (unlikely (glyph >= num_metrics) || !font->num_coords)
 	return advance;
 
-//       if (var_table.get_blob () == &Null (hb_blob_t))
-// 	return get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
+      if (var_table.get_blob () == &Null (hb_blob_t))
+	return get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
 
       return advance + roundf (var_table->get_advance_var (glyph, font->coords, font->num_coords)); // TODO Optimize?!
     }
diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh
new file mode 100644
index 0000000..630249b
--- /dev/null
+++ b/src/hb-ot-var-gvar-table.hh
@@ -0,0 +1,734 @@
+/*
+ * Copyright © 2019 Adobe 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.
+ *
+ * Adobe Author(s): Michiharu Ariza
+ */
+
+#ifndef HB_OT_VAR_GVAR_TABLE_HH
+#define HB_OT_VAR_GVAR_TABLE_HH
+
+#include "hb-open-type.hh"
+#include "hb-ot-glyf-table.hh"
+#include "hb-ot-var-fvar-table.hh"
+
+/*
+ * gvar -- Glyph Variation Table
+ * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
+ */
+#define HB_OT_TAG_gvar HB_TAG('g','v','a','r')
+
+namespace OT {
+
+struct contour_point_t
+{
+  void init (float x_=0.f, float y_=0.f) { flag = 0; x = x_; y = y_; }
+
+  void translate (const contour_point_t &p) { x += p.x; y += p.y; }
+
+  uint8_t	flag;
+  float		x, y;
+};
+
+struct contour_point_vector_t : hb_vector_t<contour_point_t>
+{
+  void extend (const hb_array_t<contour_point_t> &a)
+  {
+    unsigned int old_len = length;
+    resize (old_len + a.length);
+    for (unsigned int i = 0; i < a.length; i++)
+      (*this)[old_len + i] = a[i];
+  }
+
+  void transform (const float (&matrix)[4])
+  {
+    for (unsigned int i = 0; i < length; i++)
+    {
+      contour_point_t &p = (*this)[i];
+      float x_ = p.x * matrix[0] + p.y * matrix[2];
+	   p.y = p.x * matrix[1] + p.y * matrix[3];
+      p.x = x_;
+    }
+  }
+
+  void translate (const contour_point_t& delta)
+  {
+    for (unsigned int i = 0; i < length; i++)
+      (*this)[i].translate (delta);
+  }
+};
+
+struct range_checker_t
+{
+  range_checker_t (const void *table_, unsigned int start_offset_, unsigned int end_offset_)
+    : table ((const char*)table_), start_offset (start_offset_), end_offset (end_offset_) {}
+
+  template <typename T>
+  bool in_range (const T *p) const
+  {
+    return ((const char *) p) >= table + start_offset
+	&& ((const char *) p + T::static_size) <= table + end_offset;
+  }
+
+  protected:
+  const char *table;
+  const unsigned int start_offset;
+  const unsigned int end_offset;
+};
+
+struct Tuple : UnsizedArrayOf<F2DOT14> {};
+
+struct TuppleIndex : HBUINT16
+{
+  enum Flags {
+    EmbeddedPeakTuple	= 0x8000u,
+    IntermediateRegion	= 0x4000u,
+    PrivatePointNumbers	= 0x2000u,
+    TupleIndexMask	= 0x0FFFu
+  };
+
+  DEFINE_SIZE_STATIC (2);
+};
+
+struct TupleVarHeader
+{
+  unsigned int get_size (unsigned int axis_count) const
+  {
+    return min_size +
+    	   (has_peak ()? get_peak_tuple ().get_size (axis_count): 0) +
+    	   (has_intermediate ()? (get_start_tuple (axis_count).get_size (axis_count) +
+				  get_end_tuple (axis_count).get_size (axis_count)): 0);
+  }
+
+  const TupleVarHeader &get_next (unsigned int axis_count) const
+  { return StructAtOffset<TupleVarHeader> (this, get_size (axis_count)); }
+
+  float calculate_scalar (const int *coords, unsigned int coord_count,
+  			  const hb_array_t<const F2DOT14> shared_tuples) const
+  {
+    const F2DOT14 *peak_tuple;
+
+    if (has_peak ())
+      peak_tuple = &(get_peak_tuple ()[0]);
+    else
+    {
+      unsigned int index = get_index ();
+      if (unlikely (index * coord_count >= shared_tuples.length))
+      	return 0.f;
+      peak_tuple = &shared_tuples[coord_count * index];
+    }
+
+    const F2DOT14 *start_tuple = nullptr;
+    const F2DOT14 *end_tuple = nullptr;
+    if (has_intermediate ())
+    {
+      start_tuple = get_start_tuple (coord_count);
+      end_tuple = get_end_tuple (coord_count);
+    }
+
+    float scalar = 1.f;
+    for (unsigned int i = 0; i < coord_count; i++)
+    {
+      int v = coords[i];
+      int peak = peak_tuple[i];
+      if (!peak || v == peak) continue;
+
+      if (has_intermediate ())
+      {
+      	int start = start_tuple[i];
+      	int end = end_tuple[i];
+	if (unlikely (start > peak || peak > end ||
+		      (start < 0 && end > 0 && peak))) continue;
+	if (v < start || v > end) return 0.f;
+	if (v < peak)
+	{ if (peak != start) scalar *= (float)(v - start) / (peak - start); }
+	else
+	{ if (peak != end) scalar *= (float)(end - v) / (end - peak); }
+      }
+      else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f;
+      else
+      	scalar *= (float)v / peak;
+    }
+    return scalar;
+  }
+
+  unsigned int get_data_size () const { return varDataSize; }
+
+  bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); }
+  bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); }
+  bool has_private_points () const { return (tupleIndex & TuppleIndex::PrivatePointNumbers); }
+  unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); }
+
+  protected:
+  const Tuple &get_peak_tuple () const
+  { return StructAfter<Tuple> (tupleIndex); }
+  const Tuple &get_start_tuple (unsigned int axis_count) const
+  { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? axis_count: 0]; }
+  const Tuple &get_end_tuple (unsigned int axis_count) const
+  { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? (axis_count * 2): axis_count]; }
+
+  HBUINT16		varDataSize;
+  TuppleIndex		tupleIndex;
+  /* UnsizedArrayOf<F2DOT14> peakTuple - optional */
+  /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
+  /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */
+
+  public:
+  DEFINE_SIZE_MIN (4);
+};
+
+struct TupleVarCount : HBUINT16
+{
+  bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
+  unsigned int get_count () const { return (*this) & CountMask; }
+
+  protected:
+  enum Flags {
+    SharedPointNumbers	= 0x8000u,
+    CountMask		= 0x0FFFu
+  };
+
+  public:
+  DEFINE_SIZE_STATIC (2);
+};
+
+struct GlyphVarData
+{
+  const TupleVarHeader &get_tuple_var_header (void) const
+  { return StructAfter<TupleVarHeader>(data); }
+
+  struct tuple_iterator_t
+  {
+    void init (const GlyphVarData *var_data_, unsigned int length_, unsigned int axis_count_)
+    {
+      var_data = var_data_;
+      length = length_;
+      index = 0;
+      axis_count = axis_count_;
+      current_tuple = &var_data->get_tuple_var_header ();
+      data_offset = 0;
+    }
+
+    bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
+    {
+      if (var_data->has_shared_point_numbers ())
+      {
+	range_checker_t checker (var_data, 0, length);
+	const HBUINT8 *base = &(var_data+var_data->data);
+	const HBUINT8 *p = base;
+	if (!unpack_points (p, shared_indices, checker)) return false;
+	data_offset = p - base;
+      }
+      return true;
+    }
+
+    bool is_valid () const
+    {
+      return (index < var_data->tupleVarCount.get_count ()) &&
+	     in_range (current_tuple) &&
+	     current_tuple->get_size (axis_count);
+    }
+
+    bool move_to_next ()
+    {
+      data_offset += current_tuple->get_data_size ();
+      current_tuple = &current_tuple->get_next (axis_count);
+      index++;
+      return is_valid ();
+    }
+
+    bool in_range (const void *p, unsigned int l) const
+    { return (const char*)p >= (const char*)var_data && (const char*)p+l <= (const char*)var_data + length; }
+
+    template <typename T> bool in_range (const T *p) const { return in_range (p, sizeof (*p)); }
+
+    const HBUINT8 *get_serialized_data () const
+    { return &(var_data+var_data->data) + data_offset; }
+
+    private:
+    const GlyphVarData		*var_data;
+    unsigned int		length;
+    unsigned int		index;
+    unsigned int		axis_count;
+    unsigned int		data_offset;
+
+    public:
+    const TupleVarHeader	*current_tuple;
+  };
+
+  static bool get_tuple_iterator (const GlyphVarData *var_data,
+  				  unsigned int length,
+  				  unsigned int axis_count,
+  				  hb_vector_t<unsigned int> &shared_indices /* OUT */,
+  				  tuple_iterator_t *iterator /* OUT */)
+  {
+    iterator->init (var_data, length, axis_count);
+    if (!iterator->get_shared_indices (shared_indices))
+      return false;
+    return iterator->is_valid ();
+  }
+
+  bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); }
+
+  static bool unpack_points (const HBUINT8 *&p /* IN/OUT */,
+			     hb_vector_t<unsigned int> &points /* OUT */,
+			     const range_checker_t &check)
+  {
+    enum packed_point_flag_t
+    {
+      POINTS_ARE_WORDS = 0x80,
+      POINT_RUN_COUNT_MASK = 0x7F
+    };
+
+    if (!check.in_range (p)) return false;
+    uint16_t count = *p++;
+    if (count & POINTS_ARE_WORDS)
+    {
+      if (!check.in_range (p)) return false;
+      count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
+    }
+    points.resize (count);
+
+    unsigned int n = 0;
+    uint16_t i = 0;
+    while (i < count)
+    {
+      if (!check.in_range (p)) return false;
+      uint16_t j;
+      uint8_t control = *p++;
+      uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1;
+      if (control & POINTS_ARE_WORDS)
+      {
+	for (j = 0; j < run_count && i < count; j++, i++)
+	{
+	  if (!check.in_range ((const HBUINT16 *)p)) return false;
+	  n += *(const HBUINT16 *)p;
+	  points[i] = n;
+	  p += HBUINT16::static_size;
+	}
+      }
+      else
+      {
+	for (j = 0; j < run_count && i < count; j++, i++)
+	{
+	  if (!check.in_range (p)) return false;
+	  n += *p++;
+	  points[i] = n;
+	}
+      }
+      if (j < run_count) return false;
+    }
+    return true;
+  }
+
+  static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */,
+			     hb_vector_t<int> &deltas /* IN/OUT */,
+			     const range_checker_t &check)
+  {
+    enum packed_delta_flag_t
+    {
+      DELTAS_ARE_ZERO	= 0x80,
+      DELTAS_ARE_WORDS = 0x40,
+      DELTA_RUN_COUNT_MASK = 0x3F
+    };
+
+    unsigned int i = 0;
+    unsigned int count = deltas.length;
+    while (i < count)
+    {
+      if (!check.in_range (p)) return false;
+      uint16_t j;
+      uint8_t control = *p++;
+      uint16_t run_count = (control & DELTA_RUN_COUNT_MASK) + 1;
+      if (control & DELTAS_ARE_ZERO)
+      {
+	for (j = 0; j < run_count && i < count; j++, i++)
+	  deltas[i] = 0;
+      }
+      else if (control & DELTAS_ARE_WORDS)
+      {
+	for (j = 0; j < run_count && i < count; j++, i++)
+	{
+	  if (!check.in_range ((const HBUINT16 *)p))
+	    return false;
+	  deltas[i] = *(const HBINT16 *)p;
+	  p += HBUINT16::static_size;
+	}
+      }
+      else
+      {
+	for (j = 0; j < run_count && i < count; j++, i++)
+	{
+	  if (!check.in_range (p))
+	    return false;
+	  deltas[i] = *(const HBINT8 *)p++;
+	}
+      }
+      if (j < run_count)
+	return false;
+    }
+    return true;
+  }
+
+  protected:
+  TupleVarCount		tupleVarCount;
+  OffsetTo<HBUINT8>	data;
+  /* TupleVarHeader tupleVarHeaders[] */
+
+  public:
+  DEFINE_SIZE_MIN (4);
+};
+
+struct gvar
+{
+  static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar;
+
+  bool sanitize_shallow (hb_sanitize_context_t *c) const
+  {
+    TRACE_SANITIZE (this);
+    return_trace (c->check_struct (this) && (version.major == 1) &&
+    		 (glyphCount == c->get_num_glyphs ()) &&
+    		 c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) &&
+    		 (is_long_offset ()?
+		    c->check_array (get_long_offset_array (), glyphCount+1):
+		    c->check_array (get_short_offset_array (), glyphCount+1)) &&
+		 c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0),
+				 get_offset (glyphCount) - get_offset (0)));
+  }
+
+  /* GlyphVarData not sanitized here; must be checked while accessing each glyph varation data */
+  bool sanitize (hb_sanitize_context_t *c) const
+  { return sanitize_shallow (c); }
+
+  bool subset (hb_subset_context_t *c) const
+  {
+    TRACE_SUBSET (this);
+
+    gvar *out = c->serializer->allocate_min<gvar> ();
+    if (unlikely (!out)) return_trace (false);
+
+    out->version.major = 1;
+    out->version.minor = 0;
+    out->axisCount = axisCount;
+    out->sharedTupleCount = sharedTupleCount;
+
+    unsigned int num_glyphs = c->plan->num_output_glyphs ();
+    out->glyphCount = num_glyphs;
+
+    unsigned int subset_data_size = 0;
+    for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
+    {
+      hb_codepoint_t old_gid;
+      if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue;
+      subset_data_size += get_glyph_var_data_length (old_gid);
+    }
+
+    bool long_offset = subset_data_size & ~0xFFFFu;
+    out->flags = long_offset? 1: 0;
+
+    HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset? 4: 2) * (num_glyphs+1));
+    if (!subset_offsets) return_trace (false);
+
+    /* shared tuples */
+    if (!sharedTupleCount || !sharedTuples)
+      out->sharedTuples = 0;
+    else
+    {
+      unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
+      F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
+      if (!tuples) return_trace (false);
+      out->sharedTuples = (char *)tuples - (char *)out;
+      memcpy (tuples, &(this+sharedTuples), shared_tuple_size);
+    }
+
+    char *subset_data = c->serializer->allocate_size<char>(subset_data_size);
+    if (!subset_data) return_trace (false);
+    out->dataZ = subset_data - (char *)out;
+
+    unsigned int glyph_offset = 0;
+    for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
+    {
+      hb_codepoint_t old_gid;
+      unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid)? get_glyph_var_data_length (old_gid): 0;
+
+      if (long_offset)
+	((HBUINT32 *)subset_offsets)[gid] = glyph_offset;
+      else
+      	((HBUINT16 *)subset_offsets)[gid] = glyph_offset / 2;
+
+      if (length > 0) memcpy (subset_data, get_glyph_var_data (old_gid), length);
+      subset_data += length;
+      glyph_offset += length;
+    }
+    if (long_offset)
+      ((HBUINT32 *)subset_offsets)[num_glyphs] = glyph_offset;
+    else
+      ((HBUINT16 *)subset_offsets)[num_glyphs] = glyph_offset / 2;
+
+    return_trace (true);
+  }
+
+  protected:
+  const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const
+  {
+    unsigned int start_offset = get_offset (glyph);
+    unsigned int end_offset = get_offset (glyph+1);
+
+    if ((start_offset == end_offset) ||
+	unlikely ((start_offset > get_offset (glyphCount)) ||
+		  (start_offset + GlyphVarData::min_size > end_offset)))
+      return &Null(GlyphVarData);
+    return &(((unsigned char *)this+start_offset)+dataZ);
+  }
+
+  bool is_long_offset () const { return (flags & 1)!=0; }
+
+  unsigned int get_offset (unsigned int i) const
+  {
+    if (is_long_offset ())
+      return get_long_offset_array ()[i];
+    else
+      return get_short_offset_array ()[i] * 2;
+  }
+
+  unsigned int get_glyph_var_data_length (unsigned int glyph) const
+  {
+    unsigned int end_offset = get_offset (glyph+1);
+    unsigned int start_offset = get_offset (glyph);
+    if (unlikely (start_offset > end_offset || end_offset > get_offset(glyphCount)))
+      return 0;
+    return end_offset - start_offset;
+  }
+
+  const HBUINT32 *get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; }
+  const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *)&offsetZ; }
+
+  public:
+  struct accelerator_t
+  {
+    void init (hb_face_t *face)
+    {
+      memset (this, 0, sizeof (accelerator_t));
+
+      gvar_table = hb_sanitize_context_t ().reference_table<gvar> (face);
+      hb_blob_ptr_t<fvar> fvar_table = hb_sanitize_context_t ().reference_table<fvar> (face);
+      unsigned int axis_count = fvar_table->get_axis_count ();
+      fvar_table.destroy ();
+
+      if (unlikely ((gvar_table->glyphCount != face->get_num_glyphs ()) ||
+		    (gvar_table->axisCount != axis_count)))
+      	fini ();
+
+      unsigned int num_shared_coord = gvar_table->sharedTupleCount * gvar_table->axisCount;
+      shared_tuples.resize (num_shared_coord);
+      for (unsigned int i = 0; i < num_shared_coord; i++)
+      	shared_tuples[i] = (&(gvar_table+gvar_table->sharedTuples))[i];
+    }
+
+    void fini ()
+    {
+      gvar_table.destroy ();
+      shared_tuples.fini ();
+    }
+
+    private:
+    struct x_getter { static float get (const contour_point_t &p) { return p.x; } };
+    struct y_getter { static float get (const contour_point_t &p) { return p.y; } };
+
+    template <typename T>
+    static float infer_delta (const hb_array_t<contour_point_t> points,
+			      const hb_array_t<contour_point_t> deltas,
+			      unsigned int target, unsigned int prev, unsigned int next)
+    {
+      float target_val = T::get (points[target]);
+      float prev_val = T::get (points[prev]);
+      float next_val = T::get (points[next]);
+      float prev_delta = T::get (deltas[prev]);
+      float next_delta = T::get (deltas[next]);
+
+      if (prev_val == next_val)
+      	return (prev_delta == next_delta)? prev_delta: 0.f;
+      else if (target_val <= hb_min (prev_val, next_val))
+      	return (prev_val < next_val) ? prev_delta: next_delta;
+      else if (target_val >= hb_max (prev_val, next_val))
+      	return (prev_val > next_val)? prev_delta: next_delta;
+
+      /* linear interpolation */
+      float r = (target_val - prev_val) / (next_val - prev_val);
+      return (1.f - r) * prev_delta + r * next_delta;
+    }
+
+    static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
+    { return (i >= end)? start: (i + 1); }
+
+    public:
+    bool apply_deltas_to_points (hb_codepoint_t glyph,
+				 const int *coords, unsigned int coord_count,
+				 const hb_array_t<contour_point_t> points,
+				 const hb_array_t<unsigned int> end_points) const
+    {
+      if (unlikely (coord_count != gvar_table->axisCount)) return false;
+
+      const GlyphVarData *var_data = gvar_table->get_glyph_var_data (glyph);
+      if (var_data == &Null(GlyphVarData)) return true;
+      hb_vector_t <unsigned int> shared_indices;
+      GlyphVarData::tuple_iterator_t iterator;
+      if (!GlyphVarData::get_tuple_iterator (var_data,
+					     gvar_table->get_glyph_var_data_length (glyph),
+					     gvar_table->axisCount,
+					     shared_indices,
+					     &iterator))
+	return false;
+
+      /* Save original points for inferred delta calculation */
+      contour_point_vector_t orig_points;
+      orig_points.resize (points.length);
+      for (unsigned int i = 0; i < orig_points.length; i++)
+	orig_points[i] = points[i];
+
+      contour_point_vector_t	deltas;	/* flag is used to indicate referenced point */
+      deltas.resize (points.length);
+
+      do {
+	float scalar = iterator.current_tuple->calculate_scalar (coords, coord_count, shared_tuples.as_array ());
+	if (scalar == 0.f) continue;
+	const HBUINT8 *p = iterator.get_serialized_data ();
+	unsigned int length = iterator.current_tuple->get_data_size ();
+	if (unlikely (!iterator.in_range (p, length)))
+	  return false;
+
+	range_checker_t checker (p, 0, length);
+	hb_vector_t <unsigned int>	private_indices;
+	if (iterator.current_tuple->has_private_points () &&
+	    !GlyphVarData::unpack_points (p, private_indices, checker))
+	  return false;
+	const hb_array_t<unsigned int> &indices = private_indices.length? private_indices: shared_indices;
+
+      	bool apply_to_all = (indices.length == 0);
+	unsigned int num_deltas = apply_to_all? points.length: indices.length;
+	hb_vector_t <int>	x_deltas;
+	x_deltas.resize (num_deltas);
+	if (!GlyphVarData::unpack_deltas (p, x_deltas, checker))
+	  return false;
+	hb_vector_t <int>	y_deltas;
+	y_deltas.resize (num_deltas);
+	if (!GlyphVarData::unpack_deltas (p, y_deltas, checker))
+	  return false;
+
+	for (unsigned int i = 0; i < deltas.length; i++)
+	  deltas[i].init ();
+	for (unsigned int i = 0; i < num_deltas; i++)
+	{
+	  unsigned int pt_index = apply_to_all? i: indices[i];
+	  deltas[pt_index].flag = 1;	/* this point is referenced, i.e., explicit deltas specified */
+	  deltas[pt_index].x += x_deltas[i] * scalar;
+	  deltas[pt_index].y += y_deltas[i] * scalar;
+	}
+
+	/* infer deltas for unreferenced points */
+	unsigned int start_point = 0;
+	for (unsigned int c = 0; c < end_points.length; c++)
+	{
+	  unsigned int end_point = end_points[c];
+	  unsigned int i, j;
+
+	  /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
+	  unsigned int unref_count = 0;
+	  for (i = start_point; i <= end_point; i++)
+	    if (!deltas[i].flag) unref_count++;
+	  if (unref_count == 0 || unref_count > end_point - start_point)
+	    goto no_more_gaps;
+
+	  j = start_point;
+	  for (;;)
+	  {
+	    /* Locate the next gap of unreferenced points between two referenced points prev and next.
+	     * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
+	     */
+	    unsigned int prev, next;
+	    for (;;) {
+	      i = j;
+	      j = next_index (i, start_point, end_point);
+	      if (deltas[i].flag && !deltas[j].flag) break;
+	    }
+	    prev = j = i;
+	    for (;;) {
+	      i = j;
+	      j = next_index (i, start_point, end_point);
+	      if (!deltas[i].flag && deltas[j].flag) break;
+	    }
+	    next = j;
+	    /* Infer deltas for all unref points in the gap between prev and next */
+	    i = prev;
+	    for (;;) {
+	      i = next_index (i, start_point, end_point);
+	      if (i == next) break;
+	      deltas[i].x = infer_delta<x_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
+	      deltas[i].y = infer_delta<y_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
+	      if (--unref_count == 0) goto no_more_gaps;
+	    }
+	  }
+no_more_gaps:
+	  start_point = end_point + 1;
+	}
+
+	/* apply specified / inferred deltas to points */
+	for (unsigned int i = 0; i < points.length; i++)
+	{
+	  points[i].x += (float) roundf (deltas[i].x);
+	  points[i].y += (float) roundf (deltas[i].y);
+	}
+      } while (iterator.move_to_next ());
+
+      return true;
+    }
+
+    unsigned int get_axis_count () const { return gvar_table->axisCount; }
+
+    protected:
+    const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const
+    { return gvar_table->get_glyph_var_data (glyph); }
+
+    private:
+    hb_blob_ptr_t<gvar>		gvar_table;
+    hb_vector_t<F2DOT14>	shared_tuples;
+  };
+
+  protected:
+  FixedVersion<>		version;		/* Version of gvar table. Set to 0x00010000u. */
+  HBUINT16			axisCount;
+  HBUINT16			sharedTupleCount;
+  LOffsetTo<F2DOT14>		sharedTuples;		/* LOffsetTo<UnsizedArrayOf<Tupple>> */
+  HBUINT16			glyphCount;
+  HBUINT16			flags;
+  LOffsetTo<GlyphVarData>	dataZ;			/* Array of GlyphVarData */
+  UnsizedArrayOf<HBUINT8>	offsetZ;		/* Array of 16-bit or 32-bit (glyphCount+1) offsets */
+
+  public:
+  DEFINE_SIZE_MIN (20);
+};
+
+struct gvar_accelerator_t : gvar::accelerator_t {};
+
+} /* namespace OT */
+
+#endif /* HB_OT_VAR_GVAR_TABLE_HH */
diff --git a/test/api/Makefile.am b/test/api/Makefile.am
index 2386e53..9d4084b 100644
--- a/test/api/Makefile.am
+++ b/test/api/Makefile.am
@@ -88,6 +88,7 @@
 	test-ot-metrics \
 	test-ot-tag \
 	test-ot-extents-cff \
+	test-ot-metrics-tt-var \
 	$(NULL)
 
 
diff --git a/test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf b/test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf
new file mode 100644
index 0000000..dc237e7
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSansVariable-Roman.anchor.ttf b/test/api/fonts/SourceSansVariable-Roman.anchor.ttf
new file mode 100644
index 0000000..4e8dc9d
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman.anchor.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSansVariable-Roman.modcomp.ttf b/test/api/fonts/SourceSansVariable-Roman.modcomp.ttf
new file mode 100644
index 0000000..c75041f
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman.modcomp.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf b/test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf
new file mode 100644
index 0000000..7d94abc
--- /dev/null
+++ b/test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf
Binary files differ
diff --git a/test/api/test-ot-metrics-tt-var.c b/test/api/test-ot-metrics-tt-var.c
new file mode 100644
index 0000000..2305a95
--- /dev/null
+++ b/test/api/test-ot-metrics-tt-var.c
@@ -0,0 +1,250 @@
+/*
+ * Copyright © 2019 Adobe 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.
+ *
+ * Adobe Author(s): Michiharu Ariza
+ */
+
+#include "hb-test.h"
+#include <hb-ot.h>
+
+/* Unit tests for glyph advance widths and extents of TrueType variable fonts */
+
+static void
+test_extents_tt_var (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  hb_glyph_extents_t  extents;
+  hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 10);
+  g_assert_cmpint (extents.y_bearing, ==, 846);
+  g_assert_cmpint (extents.width, ==, 500);
+  g_assert_cmpint (extents.height, ==, -846);
+
+  float coords[1] = { 500.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+  result = hb_font_get_glyph_extents (font, 2, &extents);
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 0);
+  g_assert_cmpint (extents.y_bearing, ==, 874);
+  g_assert_cmpint (extents.width, ==, 551);
+  g_assert_cmpint (extents.height, ==, -874);
+
+  hb_font_destroy (font);
+}
+
+static void
+test_advance_tt_var_nohvar (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  hb_position_t x, y;
+  hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
+
+  g_assert_cmpint (x, ==, 520);
+  g_assert_cmpint (y, ==, 0);
+
+  hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
+
+  g_assert_cmpint (x, ==, 0);
+  g_assert_cmpint (y, ==, -1000);
+
+  float coords[1] = { 500.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+  hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
+
+  g_assert_cmpint (x, ==, 551);
+  g_assert_cmpint (y, ==, 0);
+
+  hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
+
+  g_assert_cmpint (x, ==, 0);
+  g_assert_cmpint (y, ==, -1000);
+
+  hb_font_destroy (font);
+}
+
+static void
+test_advance_tt_var_hvarvvar (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  hb_position_t x, y;
+  hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
+
+  g_assert_cmpint (x, ==, 508);
+  g_assert_cmpint (y, ==, 0);
+
+  hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
+
+  g_assert_cmpint (x, ==, 0);
+  g_assert_cmpint (y, ==, -1000);
+
+  float coords[1] = { 700.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+  hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
+
+  g_assert_cmpint (x, ==, 531);
+  g_assert_cmpint (y, ==, 0);
+
+  hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
+
+  g_assert_cmpint (x, ==, 0);
+  g_assert_cmpint (y, ==, -1012);
+
+  hb_font_destroy (font);
+}
+
+static void
+test_advance_tt_var_anchor (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.anchor.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  hb_glyph_extents_t  extents;
+  hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 56);
+  g_assert_cmpint (extents.y_bearing, ==, 672);
+  g_assert_cmpint (extents.width, ==, 556);
+  g_assert_cmpint (extents.height, ==, -684);
+
+  float coords[1] = { 500.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+  result = hb_font_get_glyph_extents (font, 2, &extents);
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 50);
+  g_assert_cmpint (extents.y_bearing, ==, 667);
+  g_assert_cmpint (extents.width, ==, 593);
+  g_assert_cmpint (extents.height, ==, -679);
+
+  hb_font_destroy (font);
+}
+
+static void
+test_extents_tt_var_comp (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  hb_glyph_extents_t  extents;
+  float coords[1] = { 800.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+
+  hb_bool_t result;
+  result = hb_font_get_glyph_extents (font, 2, &extents);	/* Ccedilla, cedilla y-scaled by 0.8, with unscaled component offset */
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 19);
+  g_assert_cmpint (extents.y_bearing, ==, 663);
+  g_assert_cmpint (extents.width, ==, 519);
+  g_assert_cmpint (extents.height, ==, -895);
+
+  result = hb_font_get_glyph_extents (font, 3, &extents);	/* Cacute, acute y-scaled by 0.8, with unscaled component offset (default) */
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 19);
+  g_assert_cmpint (extents.y_bearing, ==, 909);
+  g_assert_cmpint (extents.width, ==, 519);
+  g_assert_cmpint (extents.height, ==, -921);
+
+  result = hb_font_get_glyph_extents (font, 4, &extents);	/* Ccaron, caron y-scaled by 0.8, with scaled component offset */
+  g_assert (result);
+
+  g_assert_cmpint (extents.x_bearing, ==, 19);
+  g_assert_cmpint (extents.y_bearing, ==, 866);
+  g_assert_cmpint (extents.width, ==, 519);
+  g_assert_cmpint (extents.height, ==, -878);
+
+  hb_font_destroy (font);
+}
+
+static void
+test_advance_tt_var_comp_v (void)
+{
+  hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
+  g_assert (face);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+  g_assert (font);
+  hb_ot_font_set_funcs (font);
+
+  float coords[1] = { 800.0f };
+  hb_font_set_var_coords_design (font, coords, 1);
+
+  hb_position_t x, y;
+  hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);	/* No VVAR; 'C' in composite Ccedilla determines metrics */
+
+  g_assert_cmpint (x, ==, 0);
+  g_assert_cmpint (y, ==, -991);
+
+  hb_font_get_glyph_origin_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
+
+  g_assert_cmpint (x, ==, 292);
+  g_assert_cmpint (y, ==, 1013);
+
+  hb_font_destroy (font);
+}
+
+int
+main (int argc, char **argv)
+{
+  hb_test_init (&argc, &argv);
+
+  hb_test_add (test_extents_tt_var);
+  hb_test_add (test_advance_tt_var_nohvar);
+  hb_test_add (test_advance_tt_var_hvarvvar);
+  hb_test_add (test_advance_tt_var_anchor);
+  hb_test_add (test_extents_tt_var_comp);
+  hb_test_add (test_advance_tt_var_comp_v);
+
+  return hb_test_run ();
+}