From 0089443756cdcef0182e55cf8480b96a64d31cc7 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Fri, 9 Feb 2018 16:22:09 -0800 Subject: [PATCH] Keep a second set of glyph ids in subset plan which is sorted by glyph id and always has gid 0 --- src/hb-subset-glyf.cc | 2 +- src/hb-subset-plan.cc | 34 +++++++++++++++++++++++++--------- src/hb-subset-plan.hh | 1 + 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/hb-subset-glyf.cc b/src/hb-subset-glyf.cc index c6e2b5db..a186cdf7 100644 --- a/src/hb-subset-glyf.cc +++ b/src/hb-subset-glyf.cc @@ -157,7 +157,7 @@ hb_subset_glyf_and_loca (hb_subset_plan_t *plan, OT::glyf::accelerator_t glyf; glyf.init(face); - bool result = _hb_subset_glyf_and_loca (glyf, glyf_data, plan->gids_to_retain, glyf_prime, loca_prime); + bool result = _hb_subset_glyf_and_loca (glyf, glyf_data, plan->gids_to_retain_sorted, glyf_prime, loca_prime); glyf.fini(); *use_short_loca = false; diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 4cb4b7c9..fb8913e8 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -30,7 +30,7 @@ #include "hb-ot-cmap-table.hh" int -hb_codepoint_t_cmp (const void *l, const void *r) { +_hb_codepoint_t_cmp (const void *l, const void *r) { return *((hb_codepoint_t *) l) - *((hb_codepoint_t *) r); } @@ -40,8 +40,8 @@ hb_subset_plan_new_gid_for_old_id (hb_subset_plan_t *plan, hb_codepoint_t *new_gid) { // the index in old_gids is the new gid; only up to codepoints.len are valid - for (unsigned int i = 0; i < plan->codepoints.len; i++) { - if (plan->gids_to_retain[i] == old_gid) { + for (unsigned int i = 0; i < plan->gids_to_retain_sorted.len; i++) { + if (plan->gids_to_retain_sorted[i] == old_gid) { *new_gid = i; return true; } @@ -59,13 +59,14 @@ _populate_codepoints (hb_set_t *input_codepoints, hb_codepoint_t *wr = plan_codepoints.push(); *wr = cp; } - plan_codepoints.qsort (hb_codepoint_t_cmp); + plan_codepoints.qsort (_hb_codepoint_t_cmp); } void _populate_gids_to_retain (hb_face_t *face, hb_auto_array_t& codepoints, - hb_auto_array_t& old_gids) + hb_auto_array_t& old_gids, + hb_auto_array_t& old_gids_sorted) { OT::cmap::accelerator_t cmap; cmap.init (face); @@ -73,12 +74,16 @@ _populate_gids_to_retain (hb_face_t *face, hb_auto_array_t bad_indices; old_gids.alloc (codepoints.len); + bool has_zero = false; for (unsigned int i = 0; i < codepoints.len; i++) { hb_codepoint_t gid; if (!cmap.get_nominal_glyph (codepoints[i], &gid)) { gid = -1; *(bad_indices.push ()) = i; } + if (gid == 0) { + has_zero = true; + } *(old_gids.push ()) = gid; } @@ -90,13 +95,20 @@ _populate_gids_to_retain (hb_face_t *face, old_gids.remove (i); } + // Populate a second glyph id array that is sorted by glyph id + // and is gauranteed to contain 0. + old_gids_sorted.alloc (old_gids.len + (has_zero ? 0 : 1)); + for (unsigned int i = 0; i < old_gids.len; i++) { + *(old_gids_sorted.push ()) = old_gids[i]; + } + if (!has_zero) + *(old_gids_sorted.push ()) = 0; + old_gids_sorted.qsort (_hb_codepoint_t_cmp); + for (unsigned int i = 0; i < codepoints.len; i++) { DEBUG_MSG(SUBSET, nullptr, " U+%04X, old_gid %d, new_gid %d", codepoints[i], old_gids[i], i); } - // TODO always keep .notdef - - // TODO(Q1) expand with glyphs that make up complex glyphs // TODO expand with glyphs reached by G* // @@ -120,7 +132,10 @@ hb_subset_plan_create (hb_face_t *face, { hb_subset_plan_t *plan = hb_object_create (); _populate_codepoints (input->codepoints, plan->codepoints); - _populate_gids_to_retain (face, plan->codepoints, plan->gids_to_retain); + _populate_gids_to_retain (face, + plan->codepoints, + plan->gids_to_retain, + plan->gids_to_retain_sorted); return plan; } @@ -143,5 +158,6 @@ hb_subset_plan_destroy (hb_subset_plan_t *plan) plan->codepoints.finish (); plan->gids_to_retain.finish (); + plan->gids_to_retain_sorted.finish (); free (plan); } diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index c7e9108c..e1c3bd3e 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -40,6 +40,7 @@ struct hb_subset_plan_t { // codepoints is sorted and aligned with gids_to_retain. hb_auto_array_t codepoints; hb_auto_array_t gids_to_retain; + hb_auto_array_t gids_to_retain_sorted; }; typedef struct hb_subset_plan_t hb_subset_plan_t; -- GitLab