[subset/loca] Reduce a vector allocation

pull/4264/head
Behdad Esfahbod 1 year ago
parent cd249d2364
commit 6eae932566
  1. 46
      src/OT/glyf/glyf-helpers.hh
  2. 34
      src/OT/glyf/glyf.hh

@ -16,20 +16,30 @@ template<typename IteratorIn, typename IteratorOut,
hb_requires (hb_is_source_of (IteratorIn, unsigned int)),
hb_requires (hb_is_sink_of (IteratorOut, unsigned))>
static void
_write_loca (IteratorIn&& it, bool short_offsets, IteratorOut&& dest)
_write_loca (IteratorIn&& it,
const hb_sorted_vector_t<hb_pair_t<hb_codepoint_t, hb_codepoint_t>> new_to_old_gid_list,
bool short_offsets,
IteratorOut&& dest)
{
unsigned num_glyphs = hb_len (dest) - 1;
unsigned right_shift = short_offsets ? 1 : 0;
unsigned int offset = 0;
dest << 0;
+ it
| hb_map ([=, &offset] (unsigned int padded_size)
{
offset += padded_size;
DEBUG_MSG (SUBSET, nullptr, "loca entry offset %u", offset);
return offset >> right_shift;
})
| hb_sink (dest)
;
unsigned offset = 0;
*dest++ = 0;
for (hb_codepoint_t i = 0, j = 0; i < num_glyphs; i++)
{
if (i != new_to_old_gid_list[j].first)
{
DEBUG_MSG (SUBSET, nullptr, "loca entry empty offset %u", offset);
*dest++ = offset >> right_shift;
continue;
}
j++;
unsigned padded_size = *it++;
offset += padded_size;
DEBUG_MSG (SUBSET, nullptr, "loca entry offset %u padded-size %u", offset, padded_size);
*dest++ = offset >> right_shift;
}
}
static bool
@ -67,9 +77,13 @@ _add_head_and_set_loca_version (hb_subset_plan_t *plan, bool use_short_loca)
template<typename Iterator,
hb_requires (hb_is_source_of (Iterator, unsigned int))>
static bool
_add_loca_and_head (hb_subset_plan_t * plan, Iterator padded_offsets, bool use_short_loca)
_add_loca_and_head (hb_subset_plan_t * plan,
Iterator padded_offsets,
const hb_sorted_vector_t<hb_pair_t<hb_codepoint_t, hb_codepoint_t>> new_to_old_gid_list,
unsigned num_glyphs,
bool use_short_loca)
{
unsigned num_offsets = padded_offsets.len () + 1;
unsigned num_offsets = num_glyphs + 1;
unsigned entry_size = use_short_loca ? 2 : 4;
char *loca_prime_data = (char *) hb_malloc (entry_size * num_offsets);
@ -79,9 +93,9 @@ _add_loca_and_head (hb_subset_plan_t * plan, Iterator padded_offsets, bool use_s
entry_size, num_offsets, entry_size * num_offsets);
if (use_short_loca)
_write_loca (padded_offsets, true, hb_array ((HBUINT16 *) loca_prime_data, num_offsets));
_write_loca (padded_offsets, new_to_old_gid_list, true, hb_array ((HBUINT16 *) loca_prime_data, num_offsets));
else
_write_loca (padded_offsets, false, hb_array ((HBUINT32 *) loca_prime_data, num_offsets));
_write_loca (padded_offsets, new_to_old_gid_list, false, hb_array ((HBUINT32 *) loca_prime_data, num_offsets));
hb_blob_t *loca_blob = hb_blob_create (loca_prime_data,
entry_size * num_offsets,

@ -95,14 +95,6 @@ struct glyf
if (unlikely (!font)) return false;
}
hb_vector_t<unsigned> padded_offsets;
unsigned num_glyphs = c->plan->num_output_glyphs ();
if (unlikely (!padded_offsets.resize (num_glyphs, false, true)))
{
hb_font_destroy (font);
return false;
}
hb_vector_t<glyf_impl::SubsetGlyph> glyphs;
if (!_populate_subset_glyphs (c->plan, font, glyphs))
{
@ -113,16 +105,18 @@ struct glyf
if (font)
hb_font_destroy (font);
auto new_to_old_gid_list = c->plan->new_to_old_gid_list;
// Calculate glyph sizes for `loca` table
hb_vector_t<unsigned> padded_offsets;
if (unlikely (!padded_offsets.alloc (glyphs.length, true)))
return false;
unsigned max_offset = 0;
for (unsigned i = 0, j = 0; i < num_glyphs; i++)
for (auto &g : glyphs)
{
if (i == new_to_old_gid_list[j].first)
padded_offsets.arrayZ[i] = glyphs[j++].padded_size ();
else
padded_offsets.arrayZ[i] = 0;
max_offset += padded_offsets[i];
unsigned size = g.padded_size ();
padded_offsets.push (size);
max_offset += size;
}
bool use_short_loca = false;
@ -131,11 +125,9 @@ struct glyf
if (!use_short_loca)
{
for (unsigned i = 0, j = 0; i < num_glyphs; i++)
if (i == new_to_old_gid_list[j].first)
padded_offsets.arrayZ[i] = glyphs[j++].length ();
else
padded_offsets.arrayZ[i] = 0;
padded_offsets.resize (0);
for (auto &g : glyphs)
padded_offsets.push (g.length ());
}
bool result = glyf_prime->serialize (c->serializer, glyphs, use_short_loca, c->plan);
@ -148,6 +140,8 @@ struct glyf
return_trace (c->serializer->check_success (glyf_impl::_add_loca_and_head (c->plan,
padded_offsets.iter (),
c->plan->new_to_old_gid_list,
c->plan->num_output_glyphs (),
use_short_loca)));
}

Loading…
Cancel
Save