diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 7abeafa1d..9f8883a01 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -445,30 +445,82 @@ struct parsed_cs_str_t : parsed_values_t parsed (false), hint_dropped (false), has_prefix_ (false), - has_calls_ (false) + has_calls_ (false), + coalescing_ (false) { SUPER::init (); } + HB_ALWAYS_INLINE void add_op (op_code_t op, const byte_str_ref_t& str_ref) { - if (!is_parsed ()) - SUPER::add_op (op, str_ref); + if (is_parsed ()) return; + if (coalescing_) + { + /* Do not record individual tokens; only note their boundaries. + * Bytes are flushed as verbatim segments at call sites and at + * the end of the string. Only enabled when per-op granularity + * is not needed later for hint analysis. */ + penultimate_end_ = last_end_; + last_end_ = str_ref.get_offset (); + if (unlikely (op == OpCode_return || op == OpCode_endchar)) + flush_segment (str_ref, last_end_); + return; + } + SUPER::add_op (op, str_ref); } void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num) { - if (!is_parsed ()) + if (is_parsed ()) return; + has_calls_ = true; + + if (coalescing_) { - has_calls_ = true; - - /* Pop the subroutine number. */ - values.pop (); - + /* Flush bytes preceding the subroutine-number token, then skip + * over the number: it is re-encoded with the new bias. */ + flush_segment (str_ref, penultimate_end_); + opStart = last_end_; SUPER::add_op (op, str_ref, {subr_num}); + penultimate_end_ = last_end_ = str_ref.get_offset (); + return; } + + /* Pop the subroutine number. */ + values.pop (); + + SUPER::add_op (op, str_ref, {subr_num}); } + /* Flush pending bytes [opStart, end) as verbatim segment entries. */ + void flush_segment (const byte_str_ref_t& str_ref, unsigned end) + { + unsigned start = opStart; + if (end <= start) return; + while (start < end) + { + auto arr = str_ref.sub_array (start, hb_min (end - start, 255u)); + if (unlikely (!arr.length)) break; + parsed_cs_op_t *val = values.push (); + val->ptr = arr.arrayZ; + val->length = arr.length; + start += arr.length; + } + opStart = end; + } + + /* For coalescing mode: flush any pending bytes through the current + * position; used where the string ends without an explicit + * return/endchar op (CFF2). */ + void flush_coalesced (const byte_str_ref_t& str_ref) + { + if (coalescing_ && !is_parsed ()) + flush_segment (str_ref, str_ref.get_offset ()); + } + + void enable_coalescing () { coalescing_ = true; } + bool is_coalescing () const { return coalescing_; } + void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid) { has_prefix_ = true; @@ -532,6 +584,13 @@ struct parsed_cs_str_t : parsed_values_t bool vsindex_dropped : 1; bool has_prefix_ : 1; bool has_calls_ : 1; + /* Record verbatim byte segments instead of individual tokens, + * making a separate compact() pass unnecessary; incompatible with + * hint analysis. */ + bool coalescing_ : 1; + /* End offsets of the last two tokens seen (coalescing mode). */ + unsigned penultimate_end_ = 0; + unsigned last_end_ = 0; op_code_t prefix_op_; number_t prefix_num_; @@ -612,14 +671,19 @@ struct subr_subset_param_t parsed_cs_str_vec_t *parsed_local_subrs_, hb_set_t *global_closure_, hb_set_t *local_closure_, - bool drop_hints_) : + bool drop_hints_, + bool coalesce_ = false) : current_parsed_str (parsed_charstring_), parsed_charstring (parsed_charstring_), parsed_global_subrs (parsed_global_subrs_), parsed_local_subrs (parsed_local_subrs_), global_closure (global_closure_), local_closure (local_closure_), - drop_hints (drop_hints_) {} + drop_hints (drop_hints_), + coalesce (coalesce_) + { + if (coalesce) parsed_charstring->enable_coalescing (); + } parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context) { @@ -658,7 +722,10 @@ struct subr_subset_param_t else { if (!parsed_str->is_parsed ()) + { parsed_str->alloc (env.str_ref.total_size ()); + if (coalesce) parsed_str->enable_coalescing (); + } current_parsed_str = parsed_str; } } @@ -671,6 +738,7 @@ struct subr_subset_param_t hb_set_t *global_closure; hb_set_t *local_closure; bool drop_hints; + bool coalesce; }; struct subr_remap_t : hb_inc_bimap_t @@ -788,6 +856,12 @@ struct subr_subsetter_t return false; } + /* When hints are not analyzed (not dropping hints, not populating + * the accelerator), coalesce parsed tokens as they are added, + * instead of a separate compact() pass. */ + bool coalesce = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING) && + !plan->inprogress_accelerator; + /* phase 1 & 2 */ for (auto _ : plan->new_to_old_gid_list) { @@ -820,7 +894,8 @@ struct subr_subsetter_t &parsed_local_subrs_storage[fd], &closures.global_closure, &closures.local_closures[fd], - plan->flags & HB_SUBSET_FLAGS_NO_HINTING); + plan->flags & HB_SUBSET_FLAGS_NO_HINTING, + coalesce); if (unlikely (!interp.interpret (param))) return false; @@ -852,8 +927,11 @@ struct subr_subsetter_t * * The compacting both saves memory and makes further operations * faster. + * + * Not needed when tokens were coalesced during parsing. */ - parsed_charstrings[new_glyph].compact (); + if (!coalesce) + parsed_charstrings[new_glyph].compact (); } /* Since parsed strings were loaded from accelerator, we still need diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 9e6055d7d..6fc62124a 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -714,12 +714,14 @@ struct cff2_cs_opset_subr_subset_t : cff2_cs_opset_tflush_coalesced (env.str_ref); param.current_parsed_str->set_parsed (); env.return_from_subr (); param.set_current_str (env, false); break; case OpCode_endchar: + param.current_parsed_str->flush_coalesced (env.str_ref); param.current_parsed_str->set_parsed (); SUPER::process_op (op, env, param); break;