[subset/cff] Reuse a calculate index total data size

pull/4297/head
Behdad Esfahbod 2 years ago
parent ec0fbf8fa6
commit d4bbe3f486
  1. 24
      src/hb-ot-cff-common.hh
  2. 5
      src/hb-subset-cff1.cc
  3. 5
      src/hb-subset-cff2.cc

@ -65,11 +65,12 @@ struct CFFIndex
template <typename Iterable,
hb_requires (hb_is_iterable (Iterable))>
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<unsigned char> (size, false);
if (unlikely (!ret)) return_trace (false);
for (const auto &_ : +it)
@ -91,11 +92,12 @@ struct CFFIndex
template <typename Iterator,
hb_requires (hb_is_iterator (Iterator))>
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 <typename Iterable,
hb_requires (hb_is_iterable (Iterable))>
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;
}

@ -811,12 +811,13 @@ OT::cff1::accelerator_subset_t::serialize (hb_serialize_context_t *c,
{
c->push<CFF1CharStrings> ();
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<CFF1CharStrings> ();
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
{

@ -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<CFF2CharStrings> ();
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
{

Loading…
Cancel
Save