[subset] Add source_blob as a hb_subset_context_t field (#2203)

So no more double sanitizing source table.
This commit is contained in:
Ebrahim Byagowi 2020-02-28 22:24:25 +03:30 committed by GitHub
parent e90213868b
commit e642aab116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 38 deletions

View File

@ -339,20 +339,16 @@ struct sbix
strikes.sanitize (c, this))); strikes.sanitize (c, this)));
} }
bool add_strike (hb_subset_context_t *c, bool
const void *dst_base, add_strike (hb_subset_context_t *c, const void *dst_base, unsigned i) const
unsigned int i, {
unsigned int sbix_len) const { if (strikes[i].is_null () || c->source_blob->length < (unsigned) strikes[i])
if (strikes[i].is_null () ||
sbix_len < (unsigned int) strikes[i])
return false; return false;
return (this+strikes[i]).subset (c, sbix_len - (unsigned int) strikes[i]); return (this+strikes[i]).subset (c, c->source_blob->length - (unsigned) strikes[i]);
} }
bool serialize_strike_offsets (hb_subset_context_t *c, bool serialize_strike_offsets (hb_subset_context_t *c, const void *dst_base) const
const void *dst_base,
unsigned int sbix_len) const
{ {
TRACE_SERIALIZE (this); TRACE_SERIALIZE (this);
@ -369,7 +365,7 @@ struct sbix
*o = 0; *o = 0;
auto snap = c->serializer->snapshot (); auto snap = c->serializer->snapshot ();
c->serializer->push (); c->serializer->push ();
bool ret = add_strike (c, dst_base, i, sbix_len); bool ret = add_strike (c, dst_base, i);
if (!ret) if (!ret)
{ {
c->serializer->pop_discard (); c->serializer->pop_discard ();
@ -396,11 +392,8 @@ struct sbix
if (unlikely (!sbix_prime)) return_trace (false); if (unlikely (!sbix_prime)) return_trace (false);
if (unlikely (!c->serializer->embed (this->version))) return_trace (false); if (unlikely (!c->serializer->embed (this->version))) return_trace (false);
if (unlikely (!c->serializer->embed (this->flags))) return_trace (false); if (unlikely (!c->serializer->embed (this->flags))) return_trace (false);
hb_blob_ptr_t<sbix> table = hb_sanitize_context_t ().reference_table<sbix> (c->plan->source);
const unsigned int sbix_len = table.get_blob ()->length;
table.destroy ();
return_trace (serialize_strike_offsets (c, sbix_prime, sbix_len)); return_trace (serialize_strike_offsets (c, sbix_prime));
} }
protected: protected:

View File

@ -409,25 +409,19 @@ struct gvar
unsigned int num_glyphs = c->plan->num_output_glyphs (); unsigned int num_glyphs = c->plan->num_output_glyphs ();
out->glyphCount = num_glyphs; out->glyphCount = num_glyphs;
hb_blob_ptr_t<gvar> table = hb_sanitize_context_t ().reference_table<gvar> (c->plan->source);
unsigned int subset_data_size = 0; unsigned int subset_data_size = 0;
for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
{ {
hb_codepoint_t old_gid; hb_codepoint_t old_gid;
if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue; if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue;
subset_data_size += get_glyph_var_data_bytes (table.get_blob (), old_gid).length; subset_data_size += get_glyph_var_data_bytes (c->source_blob, old_gid).length;
} }
bool long_offset = subset_data_size & ~0xFFFFu; bool long_offset = subset_data_size & ~0xFFFFu;
out->flags = long_offset ? 1 : 0; out->flags = long_offset ? 1 : 0;
HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1)); HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1));
if (!subset_offsets) if (!subset_offsets) return_trace (false);
{
table.destroy ();
return_trace (false);
}
/* shared tuples */ /* shared tuples */
if (!sharedTupleCount || !sharedTuples) if (!sharedTupleCount || !sharedTuples)
@ -436,29 +430,21 @@ struct gvar
{ {
unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount; unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size); F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
if (!tuples) if (!tuples) return_trace (false);
{
table.destroy ();
return_trace (false);
}
out->sharedTuples = (char *) tuples - (char *) out; out->sharedTuples = (char *) tuples - (char *) out;
memcpy (tuples, &(this+sharedTuples), shared_tuple_size); memcpy (tuples, &(this+sharedTuples), shared_tuple_size);
} }
char *subset_data = c->serializer->allocate_size<char> (subset_data_size); char *subset_data = c->serializer->allocate_size<char> (subset_data_size);
if (!subset_data) if (!subset_data) return_trace (false);
{ out->dataZ = subset_data - (char *) out;
table.destroy ();
return_trace (false);
}
out->dataZ = subset_data - (char *)out;
unsigned int glyph_offset = 0; unsigned int glyph_offset = 0;
for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
{ {
hb_codepoint_t old_gid; hb_codepoint_t old_gid;
hb_bytes_t var_data_bytes = c->plan->old_gid_for_new_gid (gid, &old_gid) hb_bytes_t var_data_bytes = c->plan->old_gid_for_new_gid (gid, &old_gid)
? get_glyph_var_data_bytes (table.get_blob (), old_gid) ? get_glyph_var_data_bytes (c->source_blob, old_gid)
: hb_bytes_t (); : hb_bytes_t ();
if (long_offset) if (long_offset)
@ -476,7 +462,6 @@ struct gvar
else else
((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2; ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2;
table.destroy ();
return_trace (true); return_trace (true);
} }

View File

@ -89,7 +89,7 @@ _subset (hb_subset_plan_t *plan)
retry: retry:
hb_serialize_context_t serializer ((void *) buf, buf_size); hb_serialize_context_t serializer ((void *) buf, buf_size);
serializer.start_serialize<TableType> (); serializer.start_serialize<TableType> ();
hb_subset_context_t c (plan, &serializer); hb_subset_context_t c (source_blob, plan, &serializer);
bool needed = table->subset (&c); bool needed = table->subset (&c);
if (serializer.ran_out_of_room) if (serializer.ran_out_of_room)
{ {

View File

@ -54,12 +54,15 @@ struct hb_subset_context_t :
dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
( _dispatch (obj, hb_prioritize, hb_forward<Ts> (ds)...) ) ( _dispatch (obj, hb_prioritize, hb_forward<Ts> (ds)...) )
hb_blob_t *source_blob;
hb_subset_plan_t *plan; hb_subset_plan_t *plan;
hb_serialize_context_t *serializer; hb_serialize_context_t *serializer;
unsigned int debug_depth; unsigned int debug_depth;
hb_subset_context_t (hb_subset_plan_t *plan_, hb_subset_context_t (hb_blob_t *source_blob_,
hb_subset_plan_t *plan_,
hb_serialize_context_t *serializer_) : hb_serialize_context_t *serializer_) :
source_blob (source_blob_),
plan (plan_), plan (plan_),
serializer (serializer_), serializer (serializer_),
debug_depth (0) {} debug_depth (0) {}