[repacker] restrict 32 bit subgraph connected component search to only nodes reachable via directed links.
diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh
index 6df7c42..5016625 100644
--- a/src/hb-repacker.hh
+++ b/src/hb-repacker.hh
@@ -374,16 +374,18 @@
       {
         if (l.width == 4 && !l.is_signed)
         {
-          visited.add (i);
           roots.add (l.objidx);
+          find_subgraph (l.objidx, visited);
         }
       }
     }
 
+    // Mark everything not in the subgraphs of 32 bit roots as visited.
+    // This prevents 32 bit subgraphs from being connected via nodes not in the 32 bit subgraphs.
+    visited.invert ();
+
     if (!roots) return false;
 
-    // TODO(grieger): add 16 bit only space to visited so it can't be used to connect 32 bit
-    //                subgraphs.
     unsigned space = 0;
     while (roots)
     {
@@ -473,6 +475,14 @@
     }
   }
 
+  void find_subgraph (unsigned node_idx, hb_set_t& subgraph)
+  {
+    if (subgraph.has (node_idx)) return;
+    subgraph.add (node_idx);
+    for (const auto& link : vertices_[node_idx].obj.links)
+      find_subgraph (link.objidx, subgraph);
+  }
+
   /*
    * duplicates all nodes in the subgraph reachable from node_idx. Does not re-assign
    * links. index_map is updated with mappings from old id to new id. If a duplication has already
@@ -1013,11 +1023,9 @@
        ||  table_tag == HB_OT_TAG_GSUB)
       && sorted_graph.will_overflow ())
   {
+    DEBUG_MSG (SUBSET_REPACK, nullptr, "Assigning spaces to 32 bit subgraphs.");
     if (sorted_graph.assign_32bit_spaces ())
-    {
-      DEBUG_MSG (SUBSET_REPACK, nullptr, "Assigning spaces to 32 bit subgraphs.");
       sorted_graph.sort_shortest_distance ();
-    }
   }
 
   unsigned round = 0;