From d4bbe3f48663944385f25f608438e1eb678fc4b7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 23 Jun 2023 18:13:30 -0600 Subject: [PATCH] [subset/cff] Reuse a calculate index total data size --- src/hb-ot-cff-common.hh | 24 ++++++++++++++++-------- src/hb-subset-cff1.cc | 5 +++-- src/hb-subset-cff2.cc | 5 +++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index fb548b2e1..30ce7b826 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -65,11 +65,12 @@ struct CFFIndex template bool serialize (hb_serialize_context_t *c, - const Iterable &iterable) + const Iterable &iterable, + unsigned data_size = 0) { TRACE_SERIALIZE (this); auto it = hb_iter (iterable); - unsigned size = serialize_header(c, + it | hb_map (hb_iter) | hb_map (hb_len)); + unsigned size = serialize_header(c, + it | hb_map (hb_iter) | hb_map (hb_len), data_size); unsigned char *ret = c->allocate_size (size, false); if (unlikely (!ret)) return_trace (false); for (const auto &_ : +it) @@ -91,11 +92,12 @@ struct CFFIndex template unsigned serialize_header (hb_serialize_context_t *c, - Iterator it) + Iterator it, + unsigned data_size = 0) { TRACE_SERIALIZE (this); - unsigned total = + it | hb_reduce (hb_add, 0); + unsigned total = data_size ? data_size : + it | hb_reduce (hb_add, 0); unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8; /* serialize CFFIndex header */ @@ -174,16 +176,22 @@ struct CFFIndex template - static unsigned total_size (const Iterable &iterable) + static unsigned total_size (const Iterable &iterable, unsigned *data_size = nullptr) { auto it = + hb_iter (iterable) | hb_map (hb_iter) | hb_map (hb_len); - // The following should return min_size IMO. But that crashes a few - // tests. I have not investigated why. - if (!it) return 0; //min_size; + if (!it) + { + if (data_size) *data_size = 0; + // The following should return min_size IMO. But that crashes a few + // tests. I have not investigated why. + return 0; //min_size; + } unsigned total = + it | hb_reduce (hb_add, 0); unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8; + if (data_size) *data_size = total; + return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total; } diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index b6d1c883d..8b9f2308b 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -811,12 +811,13 @@ OT::cff1::accelerator_subset_t::serialize (hb_serialize_context_t *c, { c->push (); - unsigned total_size = CFF1CharStrings::total_size (plan.subset_charstrings); + unsigned data_size = 0; + unsigned total_size = CFF1CharStrings::total_size (plan.subset_charstrings, &data_size); if (unlikely (!c->start_zerocopy (total_size))) return false; auto *cs = c->start_embed (); - if (likely (cs->serialize (c, plan.subset_charstrings))) + if (likely (cs->serialize (c, plan.subset_charstrings, data_size))) plan.info.char_strings_link = c->pop_pack (false); else { diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 1dbf74314..bdae27c76 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -560,12 +560,13 @@ OT::cff2::accelerator_subset_t::serialize (hb_serialize_context_t *c, { c->push (); - unsigned total_size = CFF2CharStrings::total_size (plan.subset_charstrings); + unsigned data_size = 0; + unsigned total_size = CFF2CharStrings::total_size (plan.subset_charstrings, &data_size); if (unlikely (!c->start_zerocopy (total_size))) return false; auto *cs = c->start_embed (); - if (likely (cs->serialize (c, plan.subset_charstrings))) + if (likely (cs->serialize (c, plan.subset_charstrings, data_size))) plan.info.char_strings_link = c->pop_pack (false); else {