Merge branch 'master' into var-subset

rebase master
diff --git a/src/Makefile.sources b/src/Makefile.sources
index 8eb235b..c698f21 100644
--- a/src/Makefile.sources
+++ b/src/Makefile.sources
@@ -41,6 +41,7 @@
 	hb-machinery.hh \
 	hb-map.cc \
 	hb-map.hh \
+	hb-bimap.hh \
 	hb-meta.hh \
 	hb-mutex.hh \
 	hb-null.hh \
@@ -68,6 +69,7 @@
 	hb-ot-head-table.hh \
 	hb-ot-hhea-table.hh \
 	hb-ot-hmtx-table.hh \
+	hb-ot-hmtx-table.cc \
 	hb-ot-kern-table.hh \
 	hb-ot-layout-base-table.hh \
 	hb-ot-layout-common.hh \
@@ -126,6 +128,7 @@
 	hb-ot-var-fvar-table.hh \
 	hb-ot-var-hvar-table.hh \
 	hb-ot-var-mvar-table.hh \
+	hb-ot-var-gvar-table.hh \
 	hb-ot-var.cc \
 	hb-ot-vorg-table.hh \
 	hb-set-digest.hh \
diff --git a/src/hb-bimap.hh b/src/hb-bimap.hh
new file mode 100644
index 0000000..6ed3e1f
--- /dev/null
+++ b/src/hb-bimap.hh
@@ -0,0 +1,109 @@
+/*
+ * 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_BIMAP_HH
+#define HB_BIMAP_HH
+
+#include "hb.hh"
+
+/* Bi-directional map.
+ * new ids are assigned incrementally & contiguously to old ids
+ * which may be added randomly & sparsely
+ * all mappings are 1-to-1 in both directions */
+struct hb_bimap_t
+{
+  hb_bimap_t () { init (); }
+  ~hb_bimap_t () { fini (); }
+
+  void init ()
+  {
+    count = 0;
+    old_to_new.init ();
+    new_to_old.init ();
+  }
+
+  void fini ()
+  {
+    old_to_new.fini ();
+    new_to_old.fini ();
+  }
+
+  bool has (hb_codepoint_t _old) const { return old_to_new.has (_old); }
+
+  hb_codepoint_t add (hb_codepoint_t _old)
+  {
+    hb_codepoint_t	_new = old_to_new[_old];
+    if (_new == HB_MAP_VALUE_INVALID)
+    {
+      _new = count++;
+      old_to_new.set (_old, _new);
+      new_to_old.resize (count);
+      new_to_old[_new] = _old;
+    }
+    return _new;
+  }
+
+  /* returns HB_MAP_VALUE_INVALID if unmapped */
+  hb_codepoint_t operator [] (hb_codepoint_t _old) const { return to_new (_old); }
+  hb_codepoint_t to_new (hb_codepoint_t _old) const { return old_to_new[_old]; }
+  hb_codepoint_t to_old (hb_codepoint_t _new) const { return (_new >= count)? HB_MAP_VALUE_INVALID: new_to_old[_new]; }
+
+  bool identity (unsigned int size)
+  {
+    hb_codepoint_t i;
+    old_to_new.clear ();
+    new_to_old.resize (size);
+    for (i = 0; i < size; i++)
+    {
+      old_to_new.set (i, i);
+      new_to_old[i] = i;
+    }
+    count = i;
+    return old_to_new.successful && !new_to_old.in_error ();
+  }
+
+  static int cmp_id (const void* a, const void* b)
+  { return (int)*(const hb_codepoint_t *)a - (int)*(const hb_codepoint_t *)b; }
+
+  /* Optional: after finished adding all mappings in a random order,
+   * reassign new ids to old ids so that both are in the same order. */
+  void reorder ()
+  {
+    new_to_old.qsort (cmp_id);
+    for (hb_codepoint_t _new = 0; _new < count; _new++)
+      old_to_new.set (to_old (_new), _new);
+  }
+
+  unsigned int get_count () const { return count; }
+
+  protected:
+  unsigned int  count;
+  hb_map_t	old_to_new;
+  hb_vector_t<hb_codepoint_t>
+  		new_to_old;
+};
+
+#endif /* HB_BIMAP_HH */
diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh
index 0cd05d0..cd446dc 100644
--- a/src/hb-ot-cff-common.hh
+++ b/src/hb-ot-cff-common.hh
@@ -414,57 +414,6 @@
   unsigned int    offSize;
 };
 
-/* used to remap font index or SID from fullset to subset.
- * set to CFF_UNDEF_CODE if excluded from subset */
-struct remap_t : hb_vector_t<hb_codepoint_t>
-{
-  void init () { SUPER::init (); }
-
-  void fini () { SUPER::fini (); }
-
-  bool reset (unsigned int size)
-  {
-    if (unlikely (!SUPER::resize (size)))
-      return false;
-    for (unsigned int i = 0; i < length; i++)
-      (*this)[i] = CFF_UNDEF_CODE;
-    count = 0;
-    return true;
-  }
-
-  bool identity (unsigned int size)
-  {
-    if (unlikely (!SUPER::resize (size)))
-      return false;
-    unsigned int i;
-    for (i = 0; i < length; i++)
-      (*this)[i] = i;
-    count = i;
-    return true;
-  }
-
-  bool excludes (hb_codepoint_t id) const
-  { return (id < length) && ((*this)[id] == CFF_UNDEF_CODE); }
-
-  bool includes (hb_codepoint_t id) const
-  { return !excludes (id); }
-
-  unsigned int add (unsigned int i)
-  {
-    if ((*this)[i] == CFF_UNDEF_CODE)
-      (*this)[i] = count++;
-    return (*this)[i];
-  }
-
-  hb_codepoint_t get_count () const { return count; }
-
-  protected:
-  hb_codepoint_t  count;
-
-  private:
-  typedef hb_vector_t<hb_codepoint_t> SUPER;
-};
-
 template <typename COUNT>
 struct FDArray : CFFIndexOf<COUNT, FontDict>
 {
@@ -508,7 +457,7 @@
 		  unsigned int offSize_,
 		  const hb_vector_t<DICTVAL> &fontDicts,
 		  unsigned int fdCount,
-		  const remap_t &fdmap,
+		  const hb_bimap_t &fdmap,
 		  OP_SERIALIZER& opszr,
 		  const hb_vector_t<table_info_t> &privateInfos)
   {
@@ -523,7 +472,7 @@
     unsigned int  offset = 1;
     unsigned int  fid = 0;
     for (unsigned i = 0; i < fontDicts.length; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
       	if (unlikely (fid >= fdCount)) return_trace (false);
 	CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset);
@@ -533,7 +482,7 @@
 
     /* serialize font dicts */
     for (unsigned int i = 0; i < fontDicts.length; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
 	FontDict *dict = c->start_embed<FontDict> ();
 	if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]])))
@@ -547,12 +496,12 @@
   static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
 						 const hb_vector_t<DICTVAL> &fontDicts,
 						 unsigned int fdCount,
-						 const remap_t &fdmap,
+						 const hb_bimap_t &fdmap,
 						 OP_SERIALIZER& opszr)
   {
     unsigned int dictsSize = 0;
     for (unsigned int i = 0; i < fontDicts.len; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
 	dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr);
 
     offSize_ = calcOffSize (dictsSize);
diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh
index 7fbda90..18cbafa 100644
--- a/src/hb-ot-cff1-table.hh
+++ b/src/hb-ot-cff1-table.hh
@@ -570,7 +570,7 @@
 struct CFF1StringIndex : CFF1Index
 {
   bool serialize (hb_serialize_context_t *c, const CFF1StringIndex &strings,
-		  unsigned int offSize_, const remap_t &sidmap)
+		  unsigned int offSize_, const hb_bimap_t &sidmap)
   {
     TRACE_SERIALIZE (this);
     if (unlikely ((strings.count == 0) || (sidmap.get_count () == 0)))
@@ -588,7 +588,7 @@
     for (unsigned int i = 0; i < strings.count; i++)
     {
       hb_codepoint_t  j = sidmap[i];
-      if (j != CFF_UNDEF_CODE)
+      if (j != HB_MAP_VALUE_INVALID)
 	bytesArray[j] = strings[i];
     }
 
@@ -598,7 +598,7 @@
   }
 
   /* in parallel to above */
-  unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const remap_t &sidmap) const
+  unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const hb_bimap_t &sidmap) const
   {
     offSize = 0;
     if ((count == 0) || (sidmap.get_count () == 0))
@@ -606,7 +606,7 @@
 
     unsigned int dataSize = 0;
     for (unsigned int i = 0; i < count; i++)
-      if (sidmap[i] != CFF_UNDEF_CODE)
+      if (sidmap[i] != HB_MAP_VALUE_INVALID)
 	dataSize += length_at (i);
 
     offSize = calcOffSize(dataSize);
diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index 94a9fdc..d03371c 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -42,6 +42,7 @@
 #include "hb-ot-post-table.hh"
 #include "hb-ot-stat-table.hh" // Just so we compile it; unused otherwise.
 #include "hb-ot-vorg-table.hh"
+#include "hb-ot-var-gvar-table.hh"
 #include "hb-ot-color-cbdt-table.hh"
 #include "hb-ot-color-sbix-table.hh"
 
@@ -157,10 +158,10 @@
   }
 
   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 (glyph);
+    hb_position_t tsb = vmtx.get_side_bearing (font, glyph);
     *y = font->em_scale_y (extents.y_bearing + tsb);
     return true;
   }
@@ -182,7 +183,7 @@
   const hb_ot_face_t *ot_face = (const hb_ot_face_t *) font_data;
   bool ret = ot_face->sbix->get_extents (font, glyph, extents);
   if (!ret)
-    ret = ot_face->glyf->get_extents (glyph, extents);
+    ret = ot_face->glyf->get_extents (font, glyph, extents);
   if (!ret)
     ret = ot_face->cff1->get_extents (glyph, extents);
   if (!ret)
diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh
index b595e5e..7436d5b 100644
--- a/src/hb-ot-glyf-table.hh
+++ b/src/hb-ot-glyf-table.hh
@@ -22,6 +22,7 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  * Google Author(s): Behdad Esfahbod
+ * Adobe Author(s): Michiharu Ariza
  */
 
 #ifndef HB_OT_GLYF_TABLE_HH
@@ -29,8 +30,12 @@
 
 #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 "hb-subset-glyf.hh"
 
+#include <float.h>
+
 namespace OT {
 
 
@@ -171,6 +176,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:
     struct Iterator
     {
       const char *glyph_start;
@@ -183,8 +273,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;
 	}
@@ -242,12 +331,19 @@
       glyf_table = hb_sanitize_context_t ().reference_table<glyf> (face);
 
       num_glyphs = 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 ();
     }
 
     /*
@@ -281,6 +377,324 @@
       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_delta = (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_delta;
+      phantoms[PHANTOM_BOTTOM].y = -v_adv + v_delta;
+    }
+
+    /* 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));
+    }
+
+    /* Note: Recursively calls itself. */
+    bool get_var_metrics (hb_codepoint_t glyph,
+			  const int *coords, unsigned int coord_count,
+			  contour_point_vector_t &phantoms /* 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, true/*phantom_only*/))) return false;
+      hb_array_t<contour_point_t>	phantoms_array = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT);
+      init_phantom_points (glyph, phantoms_array);
+      if (unlikely (!gvar_accel.apply_deltas_to_points (glyph, coords, coord_count,
+      							points.as_array (), end_points.as_array ()))) return false;
+
+      for (unsigned int i = 0; i < PHANTOM_COUNT; i++)
+      	phantoms[i] = points[points.length - PHANTOM_COUNT + i];
+
+      CompositeGlyphHeader::Iterator composite;
+      if (!get_composite (glyph, &composite)) return true;	/* simple glyph */
+      do
+      {
+	if (composite.current->flags & CompositeGlyphHeader::USE_MY_METRICS)
+	{
+	  if (unlikely (!get_var_metrics (composite.current->glyphIndex, coords, coord_count,
+					  phantoms, depth))) return false;
+
+	  composite.current->transform_points (phantoms);
+	}
+      } while (composite.move_to_next());
+      return true;
+    }
+
+    struct contour_bounds_t
+    {
+      contour_bounds_t () { min.x = min.y = FLT_MAX; max.x = max.y = FLT_MIN; }
+
+      void add (const contour_point_t &p)
+      {
+      	min.x = MIN (min.x, p.x);
+      	min.y = MIN (min.y, p.y);
+      	max.x = MAX (max.x, p.x);
+      	max.y = 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 does not include 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_array = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT);
+      init_phantom_points (glyph, phantoms_array);
+      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 */
+      	if (likely (points.length >= PHANTOM_COUNT))
+	  all_points.extend (points.sub_array (0, points.length - PHANTOM_COUNT));
+      }
+      else
+      {
+	/* composite glyph */
+	do
+	{
+	  contour_point_vector_t comp_points;
+	  if (unlikely (!get_points_var (composite.current->glyphIndex, coords, coord_count,
+	  				 comp_points))) return false;
+
+	  /* 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.as_array ());
+
+	  comp_index++;
+	} while (composite.move_to_next());
+      }
+
+      {
+	/* Undocumented rasterizer behavior:
+	 * Shift points horizontally by the updated left side bearing
+	 */
+	contour_point_t delta;
+	delta.init (points[points.length - PHANTOM_COUNT + PHANTOM_LEFT].x, 0.f);
+	if (delta.x != 0.f) all_points.translate (delta);
+      }
+
+      return true;
+    }
+
+    bool get_extents_var (hb_codepoint_t glyph,
+			  const int *coords, unsigned int coord_count,
+			  hb_glyph_extents_t *extents) const
+    {
+      contour_point_vector_t all_points;
+      if (unlikely (!get_points_var (glyph, coords, coord_count, all_points))) return false;
+
+      contour_bounds_t	bounds;
+      for (unsigned int i = 0; i < 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;
+      }
+      return true;
+    }
+
+    public:
     /* based on FontTools _g_l_y_f.py::trim */
     bool remove_padding (unsigned int start_offset,
 				unsigned int *end_offset) const
@@ -440,8 +854,44 @@
       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 (unsigned int)roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y);
+      else
+      	return (unsigned int)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
+    {
+      contour_point_vector_t	phantoms;
+      phantoms.resize (PHANTOM_COUNT);
+
+      if (unlikely (!get_var_metrics (glyph, coords, coord_count, phantoms)))
+	return vertical? vmtx_accel.get_side_bearing (glyph): hmtx_accel.get_side_bearing (glyph);
+
+      return (int)(vertical? -ceilf (phantoms[PHANTOM_TOP].y): 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;
@@ -464,6 +914,11 @@
     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;
   };
 
   protected:
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 5dff981..9df03f9 100644
--- a/src/hb-ot-hmtx-table.hh
+++ b/src/hb-ot-hmtx-table.hh
@@ -53,6 +53,12 @@
   DEFINE_SIZE_STATIC (4);
 };
 
+struct hmtxvmtx_accelerator_base_t
+{
+  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>
 struct hmtxvmtx
 {
@@ -114,7 +120,7 @@
     bool failed = false;
     for (unsigned int i = 0; i < num_output_glyphs; i++)
     {
-      unsigned int side_bearing = 0;
+      int side_bearing = 0;
       unsigned int advance = 0;
       hb_codepoint_t old_gid;
       if (plan->old_gid_for_new_gid (i, &old_gid))
@@ -156,13 +162,14 @@
     return success;
   }
 
-  struct accelerator_t
+  struct accelerator_t : hmtxvmtx_accelerator_base_t
   {
     friend struct hmtxvmtx;
 
     void init (hb_face_t *face,
                unsigned int default_advance_ = 0)
     {
+      memset (this, 0, sizeof (*this));
       default_advance = default_advance_ ? default_advance_ : hb_face_get_upem (face);
 
       bool got_font_extents = false;
@@ -214,8 +221,7 @@
       var_table.destroy ();
     }
 
-    /* TODO Add variations version. */
-    unsigned int get_side_bearing (hb_codepoint_t glyph) const
+    int get_side_bearing (hb_codepoint_t glyph) const
     {
       if (glyph < num_advances)
         return table->longMetricZ[glyph].sb;
@@ -227,6 +233,22 @@
       return bearings[glyph - num_advances];
     }
 
+    int get_side_bearing (hb_font_t *font, hb_codepoint_t glyph) const
+    {
+      int side_bearing = get_side_bearing (glyph);
+      if (likely (glyph < num_metrics))
+      {
+	if (font->num_coords)
+	{
+	  if (var_table.get_blob () != hb_blob_get_empty ())
+	    side_bearing += var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?!
+	  else
+	    side_bearing = get_side_bearing_var_tt (font, glyph, T::tableTag==HB_OT_TAG_vmtx);
+	}
+      }
+      return side_bearing;
+    }
+
     unsigned int get_advance (hb_codepoint_t glyph) const
     {
       if (unlikely (glyph >= num_metrics))
@@ -249,7 +271,13 @@
       unsigned int advance = get_advance (glyph);
       if (likely (glyph < num_metrics))
       {
-	advance += (font->num_coords ? var_table->get_advance_var (glyph, font->coords, font->num_coords) : 0); // TODO Optimize?!
+      	if (font->num_coords)
+      	{
+	  if (var_table.get_blob () != hb_blob_get_empty ())
+	    advance += var_table->get_advance_var (glyph, font->coords, font->num_coords); // TODO Optimize?!
+	  else
+	    advance = get_advance_var_tt (font, glyph, T::tableTag==HB_OT_TAG_vmtx);
+	}
       }
       return advance;
     }
diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh
index e2ba28a..4cbf1da 100644
--- a/src/hb-ot-layout-common.hh
+++ b/src/hb-ot-layout-common.hh
@@ -33,6 +33,7 @@
 #include "hb-ot-layout.hh"
 #include "hb-open-type.hh"
 #include "hb-set.hh"
+#include "hb-bimap.hh"
 
 
 #ifndef HB_MAX_NESTING_LEVEL
@@ -1682,6 +1683,16 @@
 		  axesZ.sanitize (c, (unsigned int) axisCount * (unsigned int) regionCount));
   }
 
+  bool serialize (hb_serialize_context_t *c, const VarRegionList *src)
+  {
+    TRACE_SERIALIZE (this);
+    unsigned int size = src->get_size ();
+    if (unlikely (!c->allocate_size<VarRegionList> (size))) return_trace (false);
+    memcpy (this, src, size);
+    return_trace (true);
+  }
+
+  unsigned int get_size () const { return min_size + VarRegionAxis::static_size * axisCount * regionCount; }
   unsigned int get_region_count () const { return regionCount; }
 
   protected:
@@ -1698,9 +1709,6 @@
   unsigned int get_region_index_count () const
   { return regionIndices.len; }
 
-  unsigned int get_row_size () const
-  { return shortCount + regionIndices.len; }
-
   unsigned int get_size () const
   { return itemCount * get_row_size (); }
 
@@ -1714,7 +1722,7 @@
    unsigned int count = regionIndices.len;
    unsigned int scount = shortCount;
 
-   const HBUINT8 *bytes = &StructAfter<HBUINT8> (regionIndices);
+   const HBUINT8 *bytes = get_delta_bytes ();
    const HBUINT8 *row = bytes + inner * (scount + count);
 
    float delta = 0.;
@@ -1754,11 +1762,79 @@
     return_trace (c->check_struct (this) &&
 		  regionIndices.sanitize (c) &&
 		  shortCount <= regionIndices.len &&
-		  c->check_range (&StructAfter<HBUINT8> (regionIndices),
+		  c->check_range (get_delta_bytes (),
 				  itemCount,
 				  get_row_size ()));
   }
 
+  bool serialize (hb_serialize_context_t *c,
+		  const VarData *src,
+		  const hb_bimap_t &remap)
+  {
+    TRACE_SUBSET (this);
+    if (unlikely (!c->extend_min (*this))) return_trace (false);
+    itemCount.set (remap.get_count ());
+    
+    /* Optimize short count */
+    unsigned int short_count = src->shortCount;
+    for (; short_count > 0; short_count--)
+      for (unsigned int i = 0; i < remap.get_count (); i++)
+      {
+	unsigned int old = remap.to_old (i);
+	if (unlikely (old >= src->itemCount)) return_trace (false);
+	int16_t delta = src->get_item_delta (old, short_count - 1);
+	if (delta < -128 || 127 < delta) goto found_short;
+      }
+    
+found_short:
+    shortCount.set (short_count);
+    regionIndices.len.set (src->regionIndices.len);
+
+    unsigned int size = src->regionIndices.get_size () - HBUINT16::static_size/*regionIndices.len*/ + (get_row_size () * itemCount);
+    if (unlikely (!c->allocate_size<HBUINT8> (size)))
+      return_trace (false);
+
+    memcpy (&regionIndices[0], &src->regionIndices[0], src->regionIndices.get_size ()-HBUINT16::static_size);
+
+    for (unsigned int i = 0; i < itemCount; i++)
+      for (unsigned int r = 0; r < regionIndices.len; r++)
+      {
+      	hb_codepoint_t	old = remap.to_old (i);
+      	if (unlikely (old >= src->itemCount)) return_trace (false);
+      	set_item_delta (i, r, src->get_item_delta (old, r));
+      }
+
+    return_trace (true);
+  }
+
+  protected:
+  unsigned int get_row_size () const
+  { return shortCount + regionIndices.len; }
+
+  const HBUINT8 *get_delta_bytes () const
+  { return &StructAfter<HBUINT8> (regionIndices); }
+
+  HBUINT8 *get_delta_bytes ()
+  { return &StructAfter<HBUINT8> (regionIndices); }
+
+  int16_t get_item_delta (unsigned int item, unsigned int region) const
+  {
+    const HBINT8 *p = (const HBINT8 *)get_delta_bytes () + item * get_row_size ();
+    if (region < shortCount)
+      return ((const HBINT16 *)p)[region];
+    else
+      return (p + HBINT16::static_size * shortCount)[region - shortCount];
+  }
+
+  void set_item_delta (unsigned int item, unsigned int region, int16_t delta)
+  {
+    HBINT8 *p = (HBINT8 *)get_delta_bytes () + item * get_row_size ();
+    if (region < shortCount)
+      ((HBINT16 *)p)[region].set (delta);
+    else
+      (p + HBINT16::static_size * shortCount)[region - shortCount].set (delta);
+  }
+
   protected:
   HBUINT16		itemCount;
   HBUINT16		shortCount;
@@ -1798,6 +1874,31 @@
 		  dataSets.sanitize (c, this));
   }
 
+  bool serialize (hb_serialize_context_t *c,
+		  const VariationStore *src,
+  		  const hb_array_t <hb_bimap_t> &inner_remaps)
+  {
+    TRACE_SUBSET (this);
+    unsigned int size = min_size + HBUINT32::static_size * inner_remaps.length;
+    if (unlikely (!c->allocate_size<HBUINT32> (size))) return_trace (false);
+    format.set (1);
+    if (unlikely (!regions.serialize (c, this)
+		    .serialize (c, &(src+src->regions)))) return_trace (false);
+
+    /* TODO: The following code could be simplified when
+     * OffsetListOf::subset () can take a custom param to be passed to VarData::serialize ()
+     */
+    dataSets.len.set (inner_remaps.length);
+    for (unsigned int i = 0; i < inner_remaps.length; i++)
+    {
+      if (unlikely (!dataSets[i].serialize (c, this)
+		      .serialize (c, &(src+src->dataSets[i]), inner_remaps[i])))
+      	return_trace (false);
+    }
+    
+    return_trace (true);
+  }
+
   unsigned int get_region_index_count (unsigned int ivs) const
   { return (this+dataSets[ivs]).get_region_index_count (); }
 
@@ -1810,6 +1911,10 @@
                                       &scalars[0], num_scalars);
   }
 
+  const VarRegionList &get_regions () const { return this+regions; }
+
+  unsigned int get_sub_table_count () const { return dataSets.len; }
+
   protected:
   HBUINT16				format;
   LOffsetTo<VarRegionList>		regions;
diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh
new file mode 100644
index 0000000..354edf9
--- /dev/null
+++ b/src/hb-ot-var-gvar-table.hh
@@ -0,0 +1,706 @@
+/*
+ * 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[1];
+	   p.y = p.x * matrix[2] + 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 < MIN (0, peak) || v > 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 StructAfter<Tuple> (get_peak_tuple ()[has_peak ()? axis_count: 0]); }
+  const Tuple &get_end_tuple (unsigned int axis_count) const
+  { return StructAfter<Tuple> (get_peak_tuple ()[has_peak ()? (axis_count * 2): 0]); }
+
+  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.set (1);
+    out->version.minor.set (0);
+    out->axisCount.set (axisCount);
+    out->sharedTupleCount.set (sharedTupleCount);
+
+    unsigned int num_glyphs = c->plan->num_output_glyphs ();
+    out->glyphCount.set (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.set (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.set (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.set ((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.set (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].set (glyph_offset);
+      else
+      	((HBUINT16 *)subset_offsets)[gid].set (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].set (glyph_offset);
+    else
+      ((HBUINT16 *)subset_offsets)[num_glyphs].set (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
+  { return get_offset (glyph+1) - get_offset (glyph); }
+
+  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 <= MIN (prev_val, next_val))
+      	return (prev_val < next_val) ? prev_delta: next_delta;
+      else if (target_val >= 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;
+    }
+
+    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;
+
+      contour_point_vector_t	deltas;	/* flag is used to indicate referenced point */
+      deltas.resize (points.length);
+      for (unsigned int i = 0; i < deltas.length; i++)
+      	deltas[i].init ();
+
+      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 = shared_indices.length? shared_indices: private_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 < 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;
+	}
+      } while (iterator.move_to_next ());
+
+      /* infer deltas for unreferenced points */
+      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];
+
+      unsigned int start_point = 0;
+      for (unsigned int c = 0; c < end_points.length; c++)
+      {
+	unsigned int end_point = end_points[c];
+	for (unsigned int i = start_point; i < end_point; i++)
+	{
+	  if (deltas[i].flag) continue;
+	  /* search in both directions within the contour for a pair of referenced points */
+	  unsigned int prev;
+	  for (prev = i;;)
+	  {
+	    if (prev-- <= start_point) prev = end_point;
+	    if (prev == i || deltas[prev].flag) break;
+	  }
+	  if (prev == i) continue;	/* no (previous) referenced point was found */
+	  unsigned int next;
+	  for (next = i;;)
+	  {
+	    if (next++ >= end_point) next = start_point;
+	    if (next == i || deltas[next].flag) break;
+	  }
+	  assert (next != i);
+	  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);
+	}
+	start_point = end_point + 1;
+      }
+
+      /* apply accumulated / inferred deltas to points */
+      for (unsigned int i = 0; i < points.length; i++)
+      {
+	points[i].x += deltas[i].x;
+	points[i].y += deltas[i].y;
+      }
+
+      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/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh
index a8d9fe3..ef549c1 100644
--- a/src/hb-ot-var-hvar-table.hh
+++ b/src/hb-ot-var-hvar-table.hh
@@ -44,6 +44,38 @@
 				  get_width ()));
   }
 
+  template <typename T>
+  bool serialize (hb_serialize_context_t *c, const T &plan)
+  {
+    unsigned int width = plan.get_width ();
+    unsigned int inner_bit_count = plan.get_inner_bit_count ();
+    const hb_array_t<const unsigned int> output_map = plan.get_output_map ();
+
+    TRACE_SERIALIZE (this);
+    if (unlikely (output_map.length && ((((inner_bit_count-1)&~0xF)!=0) || (((width-1)&~0x3)!=0))))
+      return_trace (false);
+    if (unlikely (!c->extend_min (*this))) return_trace (false);
+
+    format.set (((width-1)<<4)|(inner_bit_count-1));
+    mapCount.set (output_map.length);
+    HBUINT8 *p = c->allocate_size<HBUINT8> (width * output_map.length);
+    if (unlikely (!p)) return_trace (false);
+    for (unsigned int i = 0; i < output_map.length; i++)
+    {
+      unsigned int v = output_map[i];
+      unsigned int outer = v >> 16;
+      unsigned int inner = v & 0xFFFF;
+      unsigned int u = (outer << inner_bit_count)|inner;
+      for (unsigned int w = width; w > 0;)
+      {
+	p[--w].set (u);
+	u >>= 8;
+      }
+      p += width;
+    }
+    return_trace (true);
+  }
+
   unsigned int map (unsigned int v) const /* Returns 16.16 outer.inner. */
   {
     /* If count is zero, pass value unchanged.  This takes
@@ -63,7 +95,7 @@
     }
 
     { /* Repack it. */
-      unsigned int n = get_inner_bitcount ();
+      unsigned int n = get_inner_bit_count ();
       unsigned int outer = u >> n;
       unsigned int inner = u & ((1 << n) - 1);
       u = (outer<<16) | inner;
@@ -72,10 +104,9 @@
     return u;
   }
 
-  protected:
-  unsigned int get_width () const          { return ((format >> 4) & 3) + 1; }
-
-  unsigned int get_inner_bitcount () const { return (format & 0xF) + 1; }
+  unsigned int get_map_count () const	    { return mapCount; }
+  unsigned int get_width () const           { return ((format >> 4) & 3) + 1; }
+  unsigned int get_inner_bit_count () const { return (format & 0xF) + 1; }
 
   protected:
   HBUINT16	format;		/* A packed field that describes the compressed
@@ -88,6 +119,179 @@
   DEFINE_SIZE_ARRAY (4, mapDataZ);
 };
 
+struct index_map_subset_plan_t
+{
+  enum index_map_index_t {
+    ADV_INDEX,
+    LSB_INDEX,	/* dual as TSB */
+    RSB_INDEX,	/* dual as BSB */
+    VORG_INDEX
+  };
+
+  void init (const DeltaSetIndexMap &index_map,
+	     unsigned int 	    im_index,
+	     hb_bimap_t		    &outer_remap,
+	     hb_vector_t<hb_bimap_t> &inner_remaps,
+	     const hb_subset_plan_t *plan)
+  {
+    map_count = 0;
+    outer_bit_count = 0;
+    inner_bit_count = 0;
+    max_inners.init ();
+    output_map.init ();
+
+    if (&index_map == &Null(DeltaSetIndexMap))
+    {
+      /* Advance width index map is required. If its offset is missing,
+       * treat it as an indentity map. */
+      if (im_index == ADV_INDEX)
+      {
+	outer_remap.add (0);
+	hb_codepoint_t old_gid;
+	for (hb_codepoint_t gid = 0; gid < plan->num_output_glyphs (); gid++)
+	  if (plan->old_gid_for_new_gid (gid, &old_gid))
+	    inner_remaps[0].add (old_gid);
+      }
+      return;
+    }
+
+    unsigned int	last_val = (unsigned int)-1;
+    hb_codepoint_t	last_gid = (hb_codepoint_t)-1;
+    hb_codepoint_t	gid = (hb_codepoint_t)index_map.get_map_count ();
+
+    outer_bit_count = (index_map.get_width () * 8) - index_map.get_inner_bit_count ();
+    max_inners.resize (inner_remaps.length);
+    for (unsigned i = 0; i < inner_remaps.length; i++) max_inners[i] = 0;
+
+    /* Search backwards for a map value different from the last map value */
+    for (; gid > 0; gid--)
+    {
+      hb_codepoint_t	old_gid;
+      if (!plan->old_gid_for_new_gid (gid - 1, &old_gid))
+      	continue;
+
+      unsigned int	v = index_map.map (old_gid);
+      if (last_gid == (hb_codepoint_t)-1)
+      {
+	last_val = v;
+	last_gid = gid;
+	continue;
+      }
+      if (v != last_val) break;
+  
+      last_gid = gid;
+    }
+
+    if (unlikely (last_gid == (hb_codepoint_t)-1)) return;
+    map_count = last_gid;
+    for (gid = 0; gid < map_count; gid++)
+    {
+      hb_codepoint_t	old_gid;
+      if (!plan->old_gid_for_new_gid (gid, &old_gid))
+      	continue;
+      unsigned int v = index_map.map (old_gid);
+      unsigned int outer = v >> 16;
+      unsigned int inner = v & 0xFFFF;
+      outer_remap.add (outer);
+      if (inner > max_inners[outer]) max_inners[outer] = inner;
+      inner_remaps[outer].add (inner);
+    }
+  }
+
+  void fini ()
+  {
+    max_inners.fini ();
+    output_map.fini ();
+  }
+
+  void remap (const hb_subset_plan_t *plan,
+	      const DeltaSetIndexMap *input_map,
+	      const hb_bimap_t &outer_remap,
+	      const hb_vector_t<hb_bimap_t> &inner_remaps)
+  {
+    /* Leave output_map empty for an identity map */
+    /* TODO: if retain_gids, convert identity to a customized map, or not subset varstore? */
+    if (input_map == &Null(DeltaSetIndexMap))
+      return;
+
+    for (unsigned int i = 0; i < max_inners.length; i++)
+    {
+      if (inner_remaps[i].get_count () == 0) continue;
+      unsigned int bit_count = (max_inners[i]==0)? 1: hb_bit_storage (inner_remaps[i][max_inners[i]]);
+      if (bit_count > inner_bit_count) inner_bit_count = bit_count;
+    }
+
+    output_map.resize (map_count);
+    for (hb_codepoint_t gid = 0; gid < output_map.length; gid++)
+    {
+      hb_codepoint_t	old_gid = 0;
+      (void)plan->old_gid_for_new_gid (gid, &old_gid);
+      unsigned int v = input_map->map (old_gid);
+      unsigned int outer = v >> 16;
+      output_map[gid] = (outer_remap[outer] << 16) | (inner_remaps[outer][v & 0xFFFF]);
+    }
+  }
+
+  unsigned int get_inner_bit_count () const { return inner_bit_count; }
+  unsigned int get_width () const { return ((outer_bit_count + inner_bit_count + 7) / 8); }
+  unsigned int get_map_count () const { return map_count; }
+
+  unsigned int get_size () const
+  { return (map_count? (DeltaSetIndexMap::min_size + get_width () * map_count): 0); }
+
+  bool is_identity () const { return get_output_map ().length == 0; }
+  hb_array_t<const unsigned int> get_output_map () const { return output_map.as_array (); }
+
+  protected:
+  unsigned int	map_count;
+  hb_vector_t<unsigned int>
+  		max_inners;
+  unsigned int	outer_bit_count;
+  unsigned int	inner_bit_count;
+  hb_vector_t<unsigned int>
+		output_map;
+};
+
+struct hvarvvar_subset_plan_t
+{
+  hvarvvar_subset_plan_t() : inner_remaps (), index_map_plans () {}
+  ~hvarvvar_subset_plan_t() { fini (); }
+
+  void init (const hb_array_t<const DeltaSetIndexMap *> &index_maps,
+	     const VariationStore &_var_store,
+	     const hb_subset_plan_t *plan)
+  {
+    index_map_plans.resize (index_maps.length);
+
+    var_store = &_var_store;
+    inner_remaps.resize (var_store->get_sub_table_count ());
+
+    for (unsigned int i = 0; i < inner_remaps.length; i++)
+      inner_remaps[i].init ();
+
+    for (unsigned int i = 0; i < index_maps.length; i++)
+      index_map_plans[i].init (*index_maps[i], i, outer_remap, inner_remaps, plan);
+
+    outer_remap.reorder ();
+    for (unsigned int i = 0; i < inner_remaps.length; i++)
+      if (inner_remaps[i].get_count () > 0) inner_remaps[i].reorder ();
+
+    for (unsigned int i = 0; i < index_maps.length; i++)
+      index_map_plans[i].remap (plan, index_maps[i], outer_remap, inner_remaps);
+  }
+
+  void fini ()
+  {
+    inner_remaps.fini_deep ();
+    index_map_plans.fini_deep ();
+  }
+
+  hb_bimap_t			outer_remap;
+  hb_vector_t<hb_bimap_t>	inner_remaps;
+  hb_vector_t<index_map_subset_plan_t>
+  				index_map_plans;
+  const VariationStore		*var_store;
+};
 
 /*
  * HVAR -- Horizontal Metrics Variations
@@ -114,6 +318,58 @@
 		  rsbMap.sanitize (c, this));
   }
 
+  void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
+  {
+    index_maps.push (&(this+advMap));
+    index_maps.push (&(this+lsbMap));
+    index_maps.push (&(this+rsbMap));
+  }
+
+  bool serialize_index_maps (hb_serialize_context_t *c,
+			     const hb_array_t<index_map_subset_plan_t> &im_plans)
+  {
+    TRACE_SUBSET (this);
+    if (im_plans[index_map_subset_plan_t::ADV_INDEX].is_identity ())
+      advMap.set (0);
+    else if (unlikely (!advMap.serialize (c, this).serialize (c, im_plans[index_map_subset_plan_t::ADV_INDEX])))
+      return_trace (false);
+    if (im_plans[index_map_subset_plan_t::LSB_INDEX].is_identity ())
+      lsbMap.set (0);
+    else if (unlikely (!lsbMap.serialize (c, this).serialize (c, im_plans[index_map_subset_plan_t::LSB_INDEX])))
+      return_trace (false);
+    if (im_plans[index_map_subset_plan_t::RSB_INDEX].is_identity ())
+      rsbMap.set (0);
+    else if (unlikely (!rsbMap.serialize (c, this).serialize (c, im_plans[index_map_subset_plan_t::RSB_INDEX])))
+      return_trace (false);
+
+    return_trace (true);
+  }
+
+  template <typename T>
+  bool _subset (hb_subset_context_t *c) const
+  {
+    TRACE_SUBSET (this);
+    hvarvvar_subset_plan_t	hvar_plan;
+    hb_vector_t<const DeltaSetIndexMap *>
+				index_maps;
+
+    ((T*)this)->listup_index_maps (index_maps);
+    hvar_plan.init (index_maps.as_array (), this+varStore, c->plan);
+
+    T *out = c->serializer->allocate_min<T> ();
+    if (unlikely (!out)) return_trace (false);
+
+    out->version.major.set (1);
+    out->version.minor.set (0);
+
+    if (!unlikely (out->varStore.serialize (c->serializer, out)
+		     .serialize (c->serializer, hvar_plan.var_store, hvar_plan.inner_remaps.as_array ())))
+      return_trace (false);
+
+    return_trace (out->T::serialize_index_maps (c->serializer,
+						hvar_plan.index_map_plans.as_array ()));
+  }
+
   float get_advance_var (hb_codepoint_t glyph,
 			 const int *coords, unsigned int coord_count) const
   {
@@ -121,7 +377,15 @@
     return (this+varStore).get_delta (varidx, coords, coord_count);
   }
 
-  bool has_sidebearing_deltas () const { return lsbMap && rsbMap; }
+  float get_side_bearing_var (hb_codepoint_t glyph,
+			      const int *coords, unsigned int coord_count) const
+  {
+    if (!has_side_bearing_deltas ()) return 0.f;
+    unsigned int varidx = (this+lsbMap).map (glyph);
+    return (this+varStore).get_delta (varidx, coords, coord_count);
+  }
+
+  bool has_side_bearing_deltas () const { return lsbMap && rsbMap; }
 
   protected:
   FixedVersion<>version;	/* Version of the metrics variation table
@@ -141,6 +405,7 @@
 
 struct HVAR : HVARVVAR {
   static constexpr hb_tag_t tableTag = HB_OT_TAG_HVAR;
+  bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<HVAR> (c); }
 };
 struct VVAR : HVARVVAR {
   static constexpr hb_tag_t tableTag = HB_OT_TAG_VVAR;
@@ -152,6 +417,28 @@
 		  vorgMap.sanitize (c, this));
   }
 
+  void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
+  {
+    HVARVVAR::listup_index_maps (index_maps);
+    index_maps.push (&(this+vorgMap));
+  }
+
+  bool serialize_index_maps (hb_serialize_context_t *c,
+			     const hb_array_t<index_map_subset_plan_t> &im_plans)
+  {
+    TRACE_SUBSET (this);
+    if (unlikely (!HVARVVAR::serialize_index_maps (c, im_plans)))
+      return_trace (false);
+    if (!im_plans[index_map_subset_plan_t::VORG_INDEX].get_map_count ())
+      vorgMap.set (0);
+    else if (unlikely (!vorgMap.serialize (c, this).serialize (c, im_plans[index_map_subset_plan_t::VORG_INDEX])))
+      return_trace (false);
+
+    return_trace (true);
+  }
+
+  bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<VVAR> (c); }
+
   protected:
   LOffsetTo<DeltaSetIndexMap>
 		vorgMap;	/* Offset to vertical-origin var-idx mapping. */
diff --git a/src/hb-subset-cff-common.cc b/src/hb-subset-cff-common.cc
index aa5d27b..a130845 100644
--- a/src/hb-subset-cff-common.cc
+++ b/src/hb-subset-cff-common.cc
@@ -50,7 +50,7 @@
 			    unsigned int &subset_fdselect_size /* OUT */,
 			    unsigned int &subset_fdselect_format /* OUT */,
 			    hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */,
-			    remap_t &fdmap /* OUT */)
+			    hb_bimap_t &fdmap /* OUT */)
 {
   subset_fd_count = 0;
   subset_fdselect_size = 0;
@@ -97,13 +97,6 @@
     }
     else
     {
-      /* create a fdmap */
-      if (!fdmap.reset (fdCount))
-      {
-	hb_set_destroy (set);
-	return false;
-      }
-
       hb_codepoint_t  fd = CFF_UNDEF_CODE;
       while (set->next (&fd))
 	fdmap.add (fd);
diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh
index 921b025..b35f6c3 100644
--- a/src/hb-subset-cff-common.hh
+++ b/src/hb-subset-cff-common.hh
@@ -541,19 +541,18 @@
   bool	  drop_hints;
 };
 
-struct subr_remap_t : remap_t
+struct subr_remap_t : hb_bimap_t
 {
   void create (hb_set_t *closure)
   {
     /* create a remapping of subroutine numbers from old to new.
      * no optimization based on usage counts. fonttools doesn't appear doing that either.
      */
-    reset (closure->get_max () + 1);
-    for (hb_codepoint_t old_num = 0; old_num < length; old_num++)
-    {
-      if (hb_set_has (closure, old_num))
-	add (old_num);
-    }
+    hb_codepoint_t max = closure->get_max ();
+    if (max != HB_MAP_VALUE_INVALID)
+      for (hb_codepoint_t old_num = 0; old_num <= max; old_num++)
+	if (closure->has (old_num))
+	  add (old_num);
 
     if (get_count () < 1240)
       bias = 107;
@@ -563,14 +562,6 @@
       bias = 32768;
   }
 
-  hb_codepoint_t operator[] (unsigned int old_num) const
-  {
-    if (old_num >= length)
-      return CFF_UNDEF_CODE;
-    else
-      return remap_t::operator[] (old_num);
-  }
-
   int biased_num (unsigned int old_num) const
   {
     hb_codepoint_t new_num = (*this)[old_num];
@@ -581,15 +572,15 @@
   int bias;
 };
 
-struct subr_remap_ts
+struct subr_remaps_t
 {
-  subr_remap_ts ()
+  subr_remaps_t ()
   {
     global_remap.init ();
     local_remaps.init ();
   }
 
-  ~subr_remap_ts () { fini (); }
+  ~subr_remaps_t () { fini (); }
 
   void init (unsigned int fdCount)
   {
@@ -687,8 +678,8 @@
       if (unlikely (!interp.interpret (param)))
 	return false;
 
-      /* finalize parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
-      SUBSETTER::finalize_parsed_str (interp.env, param, parsed_charstrings[i]);
+      /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
+      SUBSETTER::complete_parsed_str (interp.env, param, parsed_charstrings[i]);
     }
 
     if (plan->drop_hints)
@@ -1005,7 +996,7 @@
   parsed_cs_str_vec_t		parsed_global_subrs;
   hb_vector_t<parsed_cs_str_vec_t>  parsed_local_subrs;
 
-  subr_remap_ts			remaps;
+  subr_remaps_t			remaps;
 
   private:
   typedef typename SUBRS::count_type subr_count_type;
@@ -1021,7 +1012,7 @@
 			    unsigned int &subset_fdselect_size /* OUT */,
 			    unsigned int &subset_fdselect_format /* OUT */,
 			    hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */,
-			    CFF::remap_t &fdmap /* OUT */);
+			    hb_bimap_t &fdmap /* OUT */);
 
 HB_INTERNAL bool
 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc
index 601cbe9..20a01a5 100644
--- a/src/hb-subset-cff1.cc
+++ b/src/hb-subset-cff1.cc
@@ -34,12 +34,12 @@
 
 using namespace CFF;
 
-struct remap_sid_t : remap_t
+struct remap_sid_t : hb_bimap_t
 {
   unsigned int add (unsigned int sid)
   {
     if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
-      return offset_sid (remap_t::add (unoffset_sid (sid)));
+      return offset_sid (hb_bimap_t::add (unoffset_sid (sid)));
     else
       return sid;
   }
@@ -49,7 +49,7 @@
     if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
       return sid;
     else
-      return offset_sid (remap_t::operator [] (unoffset_sid (sid)));
+      return offset_sid (hb_bimap_t::operator [] (unoffset_sid (sid)));
   }
 
   static const unsigned int num_std_strings = 391;
@@ -326,7 +326,7 @@
 struct range_list_t : hb_vector_t<code_pair_t>
 {
   /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
-  bool finalize (unsigned int last_glyph)
+  bool complete (unsigned int last_glyph)
   {
     bool  two_byte = false;
     for (unsigned int i = (*this).length; i > 0; i--)
@@ -397,7 +397,7 @@
   cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc, const hb_subset_plan_t *plan)
     : subr_subsetter_t (acc, plan) {}
 
-  static void finalize_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
+  static void complete_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
   {
     /* insert width at the beginning of the charstring as necessary */
     if (env.has_width)
@@ -510,7 +510,7 @@
     }
     supp_codes.fini ();
 
-    subset_enc_code_ranges.finalize (glyph);
+    subset_enc_code_ranges.complete (glyph);
 
     assert (subset_enc_num_codes <= 0xFF);
     size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
@@ -555,7 +555,7 @@
       last_sid = sid;
     }
 
-    bool two_byte = subset_charset_ranges.finalize (glyph);
+    bool two_byte = subset_charset_ranges.complete (glyph);
 
     size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
     if (!two_byte)
@@ -577,9 +577,6 @@
 
   bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
   {
-    if (unlikely (!sidmap.reset (acc.stringIndex->count)))
-      return false;
-
     for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
     {
       unsigned int sid = acc.topDict.nameSIDs[i];
@@ -592,7 +589,7 @@
 
     if (acc.fdArray != &Null(CFF1FDArray))
       for (unsigned int i = 0; i < orig_fdcount; i++)
-	if (fdmap.includes (i))
+	if (fdmap.has (i))
 	  (void)sidmap.add (acc.fontDicts[i].fontName);
 
     return true;
@@ -735,7 +732,7 @@
       {
 	subset_localsubrs[fd].init ();
 	offsets.localSubrsInfos[fd].init ();
-	if (fdmap.includes (fd))
+	if (fdmap.has (fd))
 	{
 	  if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
 	    return false;
@@ -786,7 +783,7 @@
       cff1_font_dict_op_serializer_t fontSzr;
       unsigned int dictsSize = 0;
       for (unsigned int i = 0; i < acc.fontDicts.length; i++)
-	if (fdmap.includes (i))
+	if (fdmap.has (i))
 	  dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr);
 
       offsets.FDArrayInfo.offSize = calcOffSize (dictsSize);
@@ -809,7 +806,7 @@
     offsets.privateDictInfo.offset = final_size;
     for (unsigned int i = 0; i < orig_fdcount; i++)
     {
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
 	bool  has_localsubrs = offsets.localSubrsInfos[i].size > 0;
 	cff_private_dict_op_serializer_t privSzr (desubroutinize, plan->drop_hints);
@@ -853,7 +850,7 @@
 
   /* font dict index remap table from fullset FDArray to subset FDArray.
    * set to CFF_UNDEF_CODE if excluded from subset */
-  remap_t   fdmap;
+  hb_bimap_t   fdmap;
 
   str_buff_vec_t		subset_charstrings;
   str_buff_vec_t		subset_globalsubrs;
@@ -1030,7 +1027,7 @@
   assert (plan.offsets.privateDictInfo.offset == (unsigned) (c.head - c.start));
   for (unsigned int i = 0; i < acc.privateDicts.length; i++)
   {
-    if (plan.fdmap.includes (i))
+    if (plan.fdmap.has (i))
     {
       PrivateDict  *pd = c.start_embed<PrivateDict> ();
       if (unlikely (pd == nullptr)) return false;
diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc
index f033fbd..b93e5d7 100644
--- a/src/hb-subset-cff2.cc
+++ b/src/hb-subset-cff2.cc
@@ -228,7 +228,7 @@
   cff2_subr_subsetter_t (const OT::cff2::accelerator_subset_t &acc, const hb_subset_plan_t *plan)
     : subr_subsetter_t (acc, plan) {}
 
-  static void finalize_parsed_str (cff2_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
+  static void complete_parsed_str (cff2_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
   {
     /* vsindex is inserted at the beginning of the charstring as necessary */
     if (env.seen_vsindex ())
@@ -326,18 +326,16 @@
       {
 	subset_localsubrs[fd].init ();
 	offsets.localSubrsInfos[fd].init ();
-	if (fdmap.includes (fd))
-	{
-	  if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
-	    return false;
 
-	  unsigned int dataSize = subset_localsubrs[fd].total_size ();
-	  if (dataSize > 0)
-	  {
-	    offsets.localSubrsInfos[fd].offset = final_size;
-	    offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize);
-	    offsets.localSubrsInfos[fd].size = CFF2Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].length, dataSize);
-	  }
+	if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
+	  return false;
+
+	unsigned int dataSize = subset_localsubrs[fd].total_size ();
+	if (dataSize > 0)
+	{
+	  offsets.localSubrsInfos[fd].offset = final_size;
+	  offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize);
+	  offsets.localSubrsInfos[fd].size = CFF2Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].length, dataSize);
 	}
       }
     }
@@ -378,7 +376,7 @@
       cff_font_dict_op_serializer_t fontSzr;
       unsigned int dictsSize = 0;
       for (unsigned int i = 0; i < acc.fontDicts.length; i++)
-	if (fdmap.includes (i))
+	if (fdmap.has (i))
 	  dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr);
 
       offsets.FDArrayInfo.offSize = calcOffSize (dictsSize);
@@ -397,7 +395,7 @@
     offsets.privateDictsOffset = final_size;
     for (unsigned int i = 0; i < orig_fdcount; i++)
     {
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
 	bool  has_localsubrs = offsets.localSubrsInfos[i].size > 0;
 	cff_private_dict_op_serializer_t privSzr (desubroutinize, drop_hints);
@@ -427,7 +425,7 @@
   unsigned int    subset_fdselect_format;
   hb_vector_t<code_pair_t>   subset_fdselect_ranges;
 
-  remap_t   fdmap;
+  hb_bimap_t   fdmap;
 
   str_buff_vec_t	    subset_charstrings;
   str_buff_vec_t	    subset_globalsubrs;
@@ -537,7 +535,7 @@
   assert (plan.offsets.privateDictsOffset == (unsigned) (c.head - c.start));
   for (unsigned int i = 0; i < acc.privateDicts.length; i++)
   {
-    if (plan.fdmap.includes (i))
+    if (plan.fdmap.has (i))
     {
       PrivateDict  *pd = c.start_embed<PrivateDict> ();
       if (unlikely (pd == nullptr)) return false;
diff --git a/src/hb-subset.cc b/src/hb-subset.cc
index 135265f..cff7c56 100644
--- a/src/hb-subset.cc
+++ b/src/hb-subset.cc
@@ -45,6 +45,8 @@
 #include "hb-ot-vorg-table.hh"
 #include "hb-ot-layout-gsub-table.hh"
 #include "hb-ot-layout-gpos-table.hh"
+#include "hb-ot-var-gvar-table.hh"
+#include "hb-ot-var-hvar-table.hh"
 
 
 static unsigned int
@@ -198,6 +200,15 @@
     case HB_OT_TAG_GPOS:
       result = _subset2<const OT::GPOS> (plan);
       break;
+    case HB_OT_TAG_gvar:
+      result = _subset2<const OT::gvar> (plan);
+      break;
+    case HB_OT_TAG_HVAR:
+      result = _subset2<const OT::HVAR> (plan);
+      break;
+    case HB_OT_TAG_VVAR:
+      result = _subset2<const OT::VVAR> (plan);
+      break;
 
     default:
       hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
diff --git a/test/api/Makefile.am b/test/api/Makefile.am
index 67d66e1..eb03381 100644
--- a/test/api/Makefile.am
+++ b/test/api/Makefile.am
@@ -50,6 +50,9 @@
 	test-subset-vmtx \
 	test-subset-cff1 \
 	test-subset-cff2 \
+	test-subset-gvar \
+	test-subset-hvar \
+	test-subset-vvar \
 	test-unicode \
 	test-version \
 	$(NULL)
@@ -64,6 +67,9 @@
 test_subset_vmtx_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
 test_subset_cff1_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
 test_subset_cff2_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
+test_subset_gvar_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
+test_subset_hvar_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
+test_subset_vvar_LDADD = $(LDADD) $(top_builddir)/src/libharfbuzz-subset.la
 
 test_unicode_CPPFLAGS = \
 	$(AM_CPPFLAGS) \
@@ -82,6 +88,7 @@
 	test-ot-name \
 	test-ot-tag \
 	test-ot-extents-cff \
+	test-ot-metrics-tt-var \
 	$(NULL)
 
 
diff --git a/test/api/fonts/AdobeVFPrototype.abc.otf b/test/api/fonts/AdobeVFPrototype.abc.otf
index cc47708..022e6fa 100644
--- a/test/api/fonts/AdobeVFPrototype.abc.otf
+++ b/test/api/fonts/AdobeVFPrototype.abc.otf
Binary files differ
diff --git a/test/api/fonts/AdobeVFPrototype.ac.nohints.otf b/test/api/fonts/AdobeVFPrototype.ac.nohints.otf
index 935bdbf..63fd0a7 100644
--- a/test/api/fonts/AdobeVFPrototype.ac.nohints.otf
+++ b/test/api/fonts/AdobeVFPrototype.ac.nohints.otf
Binary files differ
diff --git a/test/api/fonts/AdobeVFPrototype.ac.nosubrs.nohints.otf b/test/api/fonts/AdobeVFPrototype.ac.nosubrs.nohints.otf
index 85f6cf6..0509ba8 100644
--- a/test/api/fonts/AdobeVFPrototype.ac.nosubrs.nohints.otf
+++ b/test/api/fonts/AdobeVFPrototype.ac.nosubrs.nohints.otf
Binary files differ
diff --git a/test/api/fonts/AdobeVFPrototype.ac.nosubrs.otf b/test/api/fonts/AdobeVFPrototype.ac.nosubrs.otf
index ad4d53b..8d26868 100644
--- a/test/api/fonts/AdobeVFPrototype.ac.nosubrs.otf
+++ b/test/api/fonts/AdobeVFPrototype.ac.nosubrs.otf
Binary files differ
diff --git a/test/api/fonts/AdobeVFPrototype.ac.otf b/test/api/fonts/AdobeVFPrototype.ac.otf
index beab7d5..231c2ac 100644
--- a/test/api/fonts/AdobeVFPrototype.ac.otf
+++ b/test/api/fonts/AdobeVFPrototype.ac.otf
Binary files differ
diff --git a/test/api/fonts/AdobeVFPrototype.ac.retaingids.otf b/test/api/fonts/AdobeVFPrototype.ac.retaingids.otf
index 8cb3005..e0f0546 100644
--- a/test/api/fonts/AdobeVFPrototype.ac.retaingids.otf
+++ b/test/api/fonts/AdobeVFPrototype.ac.retaingids.otf
Binary files differ
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.abc.ttf b/test/api/fonts/SourceSansVariable-Roman.abc.ttf
new file mode 100644
index 0000000..690d7d5
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman.abc.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSansVariable-Roman.ac.retaingids.ttf b/test/api/fonts/SourceSansVariable-Roman.ac.retaingids.ttf
new file mode 100644
index 0000000..a77d9da
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman.ac.retaingids.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSansVariable-Roman.ac.ttf b/test/api/fonts/SourceSansVariable-Roman.ac.ttf
new file mode 100644
index 0000000..2387ea7
--- /dev/null
+++ b/test/api/fonts/SourceSansVariable-Roman.ac.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/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/fonts/SourceSerifVariable-Roman-VVAR.ac.retaingids.ttf b/test/api/fonts/SourceSerifVariable-Roman-VVAR.ac.retaingids.ttf
new file mode 100644
index 0000000..60ff321
--- /dev/null
+++ b/test/api/fonts/SourceSerifVariable-Roman-VVAR.ac.retaingids.ttf
Binary files differ
diff --git a/test/api/fonts/SourceSerifVariable-Roman-VVAR.ac.ttf b/test/api/fonts/SourceSerifVariable-Roman-VVAR.ac.ttf
new file mode 100644
index 0000000..b6b9e97
--- /dev/null
+++ b/test/api/fonts/SourceSerifVariable-Roman-VVAR.ac.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..3259a79
--- /dev/null
+++ b/test/api/test-ot-metrics-tt-var.c
@@ -0,0 +1,179 @@
+/*
+ * 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, ==, 875);
+  g_assert_cmpint (extents.width, ==, 551);
+  g_assert_cmpint (extents.height, ==, -875);
+
+  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, ==, 668);
+  g_assert_cmpint (extents.width, ==, 593);
+  g_assert_cmpint (extents.height, ==, -680);
+
+  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);
+
+  return hb_test_run ();
+}
diff --git a/test/api/test-subset-gvar.c b/test/api/test-subset-gvar.c
new file mode 100644
index 0000000..991be8f
--- /dev/null
+++ b/test/api/test-subset-gvar.c
@@ -0,0 +1,103 @@
+/*
+ * 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-subset-test.h"
+
+/* Unit tests for gvar subsetting */
+
+static void
+test_subset_gvar_noop (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file("fonts/SourceSansVariable-Roman.abc.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'b');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','v','a','r'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+}
+
+static void
+test_subset_gvar (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.ac.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','v','a','r'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+static void
+test_subset_gvar_retaingids (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.ac.retaingids.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
+  hb_subset_input_set_retain_gids (input, true);
+  face_abc_subset = hb_subset_test_create_subset (face_abc, input);
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','v','a','r'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+int
+main (int argc, char **argv)
+{
+  hb_test_init (&argc, &argv);
+
+  hb_test_add (test_subset_gvar_noop);
+  hb_test_add (test_subset_gvar);
+  hb_test_add (test_subset_gvar_retaingids);
+
+  return hb_test_run ();
+}
diff --git a/test/api/test-subset-hvar.c b/test/api/test-subset-hvar.c
new file mode 100644
index 0000000..3326e6d
--- /dev/null
+++ b/test/api/test-subset-hvar.c
@@ -0,0 +1,168 @@
+/*
+ * 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-subset-test.h"
+
+/* Unit tests for HVAR subsetting */
+
+static void
+test_subset_map_HVAR_noop (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file("fonts/AdobeVFPrototype.abc.otf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'b');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+}
+
+static void
+test_subset_map_HVAR (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.otf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+static void
+test_subset_map_HVAR_retaingids (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.retaingids.otf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
+  hb_subset_input_set_retain_gids (input, true);
+  face_abc_subset = hb_subset_test_create_subset (face_abc, input);
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+static void
+test_subset_identity_HVAR_noop (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file("fonts/SourceSansVariable-Roman.abc.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'b');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+}
+
+static void
+test_subset_identity_HVAR (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.ac.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+static void
+test_subset_identity_HVAR_retaingids (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.ac.retaingids.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
+  hb_subset_input_set_retain_gids (input, true);
+  face_abc_subset = hb_subset_test_create_subset (face_abc, input);
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('H','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+
+int
+main (int argc, char **argv)
+{
+  hb_test_init (&argc, &argv);
+
+  hb_test_add (test_subset_map_HVAR_noop);
+  hb_test_add (test_subset_map_HVAR);
+  hb_test_add (test_subset_map_HVAR_retaingids);
+  hb_test_add (test_subset_identity_HVAR_noop);
+  hb_test_add (test_subset_identity_HVAR);
+  hb_test_add (test_subset_identity_HVAR_retaingids);
+
+  return hb_test_run ();
+}
diff --git a/test/api/test-subset-vvar.c b/test/api/test-subset-vvar.c
new file mode 100644
index 0000000..2b829a3
--- /dev/null
+++ b/test/api/test-subset-vvar.c
@@ -0,0 +1,103 @@
+/*
+ * 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-subset-test.h"
+
+/* Unit tests for VVAR subsetting */
+
+static void
+test_subset_VVAR_noop (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'b');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('V','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+}
+
+static void
+test_subset_VVAR (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.ac.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('V','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+static void
+test_subset_VVAR_retaingids (void)
+{
+  hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
+  hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.ac.retaingids.ttf");
+
+  hb_set_t *codepoints = hb_set_create ();
+  hb_face_t *face_abc_subset;
+  hb_set_add (codepoints, 'a');
+  hb_set_add (codepoints, 'c');
+  hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
+  hb_subset_input_set_retain_gids (input, true);
+  face_abc_subset = hb_subset_test_create_subset (face_abc, input);
+  hb_set_destroy (codepoints);
+
+  hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('V','V','A','R'));
+
+  hb_face_destroy (face_abc_subset);
+  hb_face_destroy (face_abc);
+  hb_face_destroy (face_ac);
+}
+
+int
+main (int argc, char **argv)
+{
+  hb_test_init (&argc, &argv);
+
+  hb_test_add (test_subset_VVAR_noop);
+  hb_test_add (test_subset_VVAR);
+  hb_test_add (test_subset_VVAR_retaingids);
+
+  return hb_test_run ();
+}
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.1FC,21,41,20,62,63.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.1FC,21,41,20,62,63.ttf
new file mode 100644
index 0000000..1800da8
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.1FC,21,41,20,62,63.ttf
Binary files differ
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.61,62,63.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.61,62,63.ttf
new file mode 100644
index 0000000..4b538a8
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.61,62,63.ttf
Binary files differ
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.D7,D8,D9,DA,DE.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.D7,D8,D9,DA,DE.ttf
new file mode 100644
index 0000000..d579c1f
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.default.D7,D8,D9,DA,DE.ttf
Binary files differ
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.1FC,21,41,20,62,63.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.1FC,21,41,20,62,63.ttf
new file mode 100644
index 0000000..1800da8
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.1FC,21,41,20,62,63.ttf
Binary files differ
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.61,62,63.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.61,62,63.ttf
new file mode 100644
index 0000000..4b538a8
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.61,62,63.ttf
Binary files differ
diff --git a/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.D7,D8,D9,DA,DE.ttf b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.D7,D8,D9,DA,DE.ttf
new file mode 100644
index 0000000..d579c1f
--- /dev/null
+++ b/test/subset/data/expected/full-font/SourceSerifVariable-Roman.drop-hints.D7,D8,D9,DA,DE.ttf
Binary files differ
diff --git a/test/subset/data/fonts/SourceSerifVariable-Roman.ttf b/test/subset/data/fonts/SourceSerifVariable-Roman.ttf
new file mode 100644
index 0000000..4a73845
--- /dev/null
+++ b/test/subset/data/fonts/SourceSerifVariable-Roman.ttf
Binary files differ
diff --git a/test/subset/data/tests/full-font.tests b/test/subset/data/tests/full-font.tests
index ff195ce..28ae084 100644
--- a/test/subset/data/tests/full-font.tests
+++ b/test/subset/data/tests/full-font.tests
@@ -1,5 +1,6 @@
 FONTS:
 Roboto-Regular.ttf
+SourceSerifVariable-Roman.ttf
 
 PROFILES:
 default.txt