[subset] VORG table to use _subset2 method and new iterator frameworks
authorQunxin Liu <qxliu@google.com>
Wed, 12 Jun 2019 18:02:48 +0000 (11:02 -0700)
committerGarret Rieger <grieger@google.com>
Mon, 24 Jun 2019 17:42:59 +0000 (10:42 -0700)
src/hb-ot-vorg-table.hh
src/hb-subset.cc

index b2059e8..19e08eb 100644 (file)
@@ -69,102 +69,50 @@ struct VORG
     return vertYOrigins[i].vertOriginY;
   }
 
-  bool _subset (const hb_subset_plan_t *plan HB_UNUSED,
-               const VORG *vorg_table,
-               const hb_vector_t<VertOriginMetric> &subset_metrics,
-               unsigned int dest_sz,
-               void *dest) const
+  template <typename Iterator,
+            hb_requires (hb_is_iterator (Iterator))>
+  void serialize (hb_serialize_context_t *c,
+                  Iterator it,
+                  FWORD defaultVertOriginY)
   {
-    hb_serialize_context_t c (dest, dest_sz);
-
-    VORG *subset_table = c.start_serialize<VORG> ();
-    if (unlikely (!c.extend_min (*subset_table)))
-      return false;
-
-    subset_table->version.major = 1;
-    subset_table->version.minor = 0;
-
-    subset_table->defaultVertOriginY = vorg_table->defaultVertOriginY;
-    subset_table->vertYOrigins.len = subset_metrics.length;
-
-    bool success = true;
-    if (subset_metrics.length > 0)
-    {
-      unsigned int  size = VertOriginMetric::static_size * subset_metrics.length;
-      VertOriginMetric  *metrics = c.allocate_size<VertOriginMetric> (size);
-      if (likely (metrics != nullptr))
-        memcpy (metrics, &subset_metrics[0], size);
-      else
-        success = false;
-    }
-    c.end_serialize ();
-
-    return success;
+
+    if (unlikely (!c->extend_min ((*this))))  return;
+
+    this->version.major = 1;
+    this->version.minor = 0;
+
+    this->defaultVertOriginY = defaultVertOriginY;
+    this->vertYOrigins.len = it.len ();
+
+    + it
+    | hb_apply ([c] (const VertOriginMetric& _) { c->copy (_);})
+    ;
   }
 
-  bool subset (hb_subset_plan_t *plan) const
+  bool subset (hb_subset_context_t *c) const
   {
-    hb_blob_t *vorg_blob = hb_sanitize_context_t().reference_table<VORG> (plan->source);
-    const VORG *vorg_table = vorg_blob->as<VORG> ();
-
-    /* count the number of glyphs to be included in the subset table */
-    hb_vector_t<VertOriginMetric> subset_metrics;
-    subset_metrics.init ();
-
-
-    hb_codepoint_t old_glyph = HB_SET_VALUE_INVALID;
-    unsigned int i = 0;
-    while (i < vertYOrigins.len
-           && plan->glyphset ()->next (&old_glyph))
-    {
-      while (old_glyph > vertYOrigins[i].glyph)
-      {
-        i++;
-        if (i >= vertYOrigins.len)
-          break;
-      }
-
-      if (old_glyph == vertYOrigins[i].glyph)
-      {
-        hb_codepoint_t new_glyph;
-        if (plan->new_gid_for_old_gid (old_glyph, &new_glyph))
-        {
-          VertOriginMetric *metrics = subset_metrics.push ();
-          metrics->glyph = new_glyph;
-          metrics->vertOriginY = vertYOrigins[i].vertOriginY;
-        }
-      }
-    }
-
-    /* alloc the new table */
-    unsigned int dest_sz = VORG::min_size + VertOriginMetric::static_size * subset_metrics.length;
-    void *dest = (void *) malloc (dest_sz);
-    if (unlikely (!dest))
-    {
-      subset_metrics.fini ();
-      hb_blob_destroy (vorg_blob);
-      return false;
-    }
+    TRACE_SUBSET (this);
+    VORG *vorg_prime = c->serializer->start_embed<VORG> ();
+    if (unlikely (!c->serializer->check_success (vorg_prime))) return_trace (false);
+
+    auto it =
+    + vertYOrigins.as_array ()
+    | hb_filter (c->plan->glyphset (), &VertOriginMetric::glyph)
+    | hb_map ([&] (const VertOriginMetric& _)
+              {
+                hb_codepoint_t new_glyph = HB_SET_VALUE_INVALID;
+                c->plan->new_gid_for_old_gid (_.glyph, &new_glyph);
+
+                VertOriginMetric metric;
+                metric.glyph = new_glyph;
+                metric.vertOriginY = _.vertOriginY;
+                return metric;
+              })
+    ;
 
     /* serialize the new table */
-    if (!_subset (plan, vorg_table, subset_metrics, dest_sz, dest))
-    {
-      subset_metrics.fini ();
-      free (dest);
-      hb_blob_destroy (vorg_blob);
-      return false;
-    }
-
-    hb_blob_t *result = hb_blob_create ((const char *)dest,
-                                        dest_sz,
-                                        HB_MEMORY_MODE_READONLY,
-                                        dest,
-                                        free);
-    bool success = plan->add_table (HB_OT_TAG_VORG, result);
-    hb_blob_destroy (result);
-    subset_metrics.fini ();
-    hb_blob_destroy (vorg_blob);
-    return success;
+    vorg_prime->serialize (c->serializer, it, defaultVertOriginY);
+    return_trace (true);
   }
 
   bool sanitize (hb_sanitize_context_t *c) const
index 3f4c2e4..b117870 100644 (file)
@@ -204,7 +204,7 @@ _subset_table (hb_subset_plan_t *plan,
       result = _subset<const OT::cff2> (plan);
       break;
     case HB_OT_TAG_VORG:
-      result = _subset<const OT::VORG> (plan);
+      result = _subset2<const OT::VORG> (plan);
       break;
 #endif