diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index 015ef3a66..8263cf2e1 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -101,6 +101,15 @@ struct hb_bit_set_invertible_t } unsigned int get_population () const { return inverted ? INVALID - s.get_population () : s.get_population (); } + bool get_singleton (hb_codepoint_t *codepoint) const + { + if (likely (!inverted)) + return s.get_singleton (codepoint); + if (s.get_population () != INVALID - 1) + return false; + *codepoint = get_min (); + return *codepoint != INVALID; + } void add (hb_codepoint_t g) { unlikely (inverted) ? s.del (g) : s.add (g); } diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index f57e81bb7..25313e40a 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -942,6 +942,39 @@ struct hb_bit_set_t population = pop; return pop; } + bool get_singleton (hb_codepoint_t *codepoint) const + { + if (has_population ()) + { + if (population != 1) return false; + *codepoint = get_min (); + return true; + } + + hb_codepoint_t singleton = INVALID; + for (const auto &map : page_map) + { + const auto &page = pages.arrayZ[map.index]; + for (unsigned i = 0; i < page_t::len (); i++) + { + page_t::elt_t bits = page.v[i]; + if (!bits) continue; + if (singleton != INVALID || (bits & (bits - 1))) + return false; + singleton = map.major * page_t::PAGE_BITS + + i * page_t::ELT_BITS + hb_ctz (bits); + } + } + + if (singleton == INVALID) + { + population = 0; + return false; + } + population = 1; + *codepoint = singleton; + return true; + } hb_codepoint_t get_min () const { unsigned count = pages.length; diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index a27631ef3..ecb9c9730 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -389,6 +389,8 @@ struct hb_depend_context_t : GLYPH_SET_SINGLE }; + static constexpr hb_codepoint_t SINGLE_GLYPH_ACTIVE_FLAG = 0x80000000u; + struct glyph_set_cache_key_t { glyph_set_cache_key_t () = default; @@ -504,23 +506,43 @@ struct hb_depend_context_t : lookups_seen.del (lookup_idx); } + struct active_glyphs_frame_t + { + hb_set_t glyphs; + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + bool use_root = false; + }; + const hb_set_t& parent_active_glyphs () { if (!active_glyphs_stack_depth) return *glyphs; - return active_glyphs_stack[active_glyphs_stack_depth - 1]; + const active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1]; + return frame.use_root ? *glyphs : frame.glyphs; } - hb_set_t* push_cur_active_glyphs () + hb_set_t* push_cur_active_glyphs (hb_codepoint_t active_idx = HB_CODEPOINT_INVALID) { if (active_glyphs_stack_depth == active_glyphs_stack.length && unlikely (!active_glyphs_stack.push_or_fail ())) return nullptr; - hb_set_t *set = &active_glyphs_stack[active_glyphs_stack_depth++]; - set->clear (); - return set; + active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth++]; + frame.glyphs.clear (); + frame.active_idx = active_idx; + frame.use_root = false; + return &frame.glyphs; + } + + bool push_root_active_glyphs () + { + if (active_glyphs_stack_depth == active_glyphs_stack.length && + unlikely (!active_glyphs_stack.push_or_fail ())) + return false; + + active_glyphs_stack[active_glyphs_stack_depth++].use_root = true; + return true; } bool pop_cur_done_glyphs () @@ -537,9 +559,23 @@ struct hb_depend_context_t : /* The active glyphs and in-progress lookup set are part of the state. * Omitting the latter would make memoization incorrect for cyclic lookup * graphs whose available recursive paths depend on their ancestry. */ - hb_codepoint_t active_idx; - if (!find_or_create_recurse_set (parent_active_glyphs (), &active_idx)) - return false; + bool uses_root = !active_glyphs_stack_depth || + active_glyphs_stack[active_glyphs_stack_depth - 1].use_root; + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + if (!uses_root) + { + const active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1]; + active_idx = frame.active_idx; + if (active_idx == HB_CODEPOINT_INVALID) + { + hb_codepoint_t glyph; + if (frame.glyphs.get_singleton (&glyph) && + likely (glyph < SINGLE_GLYPH_ACTIVE_FLAG)) + active_idx = glyph | SINGLE_GLYPH_ACTIVE_FLAG; + else if (!find_or_create_recurse_set (frame.glyphs, &active_idx)) + return false; + } + } *key = recurse_key_t (lookup_idx, active_idx, @@ -581,6 +617,11 @@ struct hb_depend_context_t : } *index = recurse_sets.length; + if (unlikely (*index & SINGLE_GLYPH_ACTIVE_FLAG)) + { + hb_set_destroy (copy); + return depend_data->fail (); + } if (unlikely (!recurse_sets.push_or_fail (hb::unique_ptr {copy}))) return depend_data->fail (); if (unlikely (!recurse_set_to_index.set (recurse_sets[*index].get (), *index))) @@ -652,7 +693,7 @@ struct hb_depend_context_t : hb_depend_data_builder_t *depend_data; hb_face_t *face; hb_set_t *glyphs; - hb_vector_t active_glyphs_stack; + hb_vector_t active_glyphs_stack; unsigned active_glyphs_stack_depth = 0; recurse_func_t recurse_func; hb_codepoint_t lookup_index = HB_CODEPOINT_INVALID; @@ -2400,18 +2441,30 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, } covered_seq_indices.add (seqIndex); - hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (); - if (unlikely (!cur_active_glyphs)) + if (has_pos_glyphs) + { + hb_codepoint_t active_idx = HB_CODEPOINT_INVALID; + if (context_format == ContextFormat::SimpleContext) + { + hb_codepoint_t glyph = seqIndex ? pos_glyphs.get_min () : value; + if (likely (glyph < hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG)) + active_idx = glyph | hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG; + } + hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (active_idx); + if (unlikely (!cur_active_glyphs)) + { + c->depend_data->current_context_set_index = saved_context_set_index; + c->depend_data->current_edge_flags = saved_edge_flags; + return; + } + *cur_active_glyphs = std::move (pos_glyphs); + } + else if (unlikely (!c->push_root_active_glyphs ())) { c->depend_data->current_context_set_index = saved_context_set_index; c->depend_data->current_edge_flags = saved_edge_flags; return; } - if (has_pos_glyphs) { - *cur_active_glyphs = std::move (pos_glyphs); - } else { - *cur_active_glyphs = *c->glyphs; - } unsigned endIndex = inputCount; if (context_format == ContextFormat::CoverageBasedContext) diff --git a/src/hb-set.hh b/src/hb-set.hh index 3c8784ede..f9fcc6222 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -156,6 +156,7 @@ struct hb_sparseset_t { return s.next_many (codepoint, out, size); } unsigned int get_population () const { return s.get_population (); } + bool get_singleton (hb_codepoint_t *codepoint) const { return s.get_singleton (codepoint); } hb_codepoint_t get_min () const { return s.get_min (); } hb_codepoint_t get_max () const { return s.get_max (); } diff --git a/src/test-set.cc b/src/test-set.cc index b6ebf5036..bd835f867 100644 --- a/src/test-set.cc +++ b/src/test-set.cc @@ -158,6 +158,26 @@ main (int argc, char **argv) hb_always_assert (b.intersects (c)); } + /* Test singleton detection. */ + { + hb_set_t s; + hb_codepoint_t singleton; + hb_always_assert (!s.get_singleton (&singleton)); + + s.add (63); + hb_always_assert (s.get_singleton (&singleton)); + hb_always_assert (singleton == 63); + + s.add (1024); + hb_always_assert (!s.get_singleton (&singleton)); + s.del (63); + hb_always_assert (s.get_singleton (&singleton)); + hb_always_assert (singleton == 1024); + + s.invert (); + hb_always_assert (!s.get_singleton (&singleton)); + } + /* Adding HB_SET_VALUE_INVALID */ { hb_set_t s;