From 6a66992842dea6e3ad9c717a3fa047341ab50af6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:21:29 -0700 Subject: [PATCH 1/9] Add hb_segment_properties_overlay() New API: + hb_segment_properties_overlay() --- docs/harfbuzz-sections.txt | 1 + src/hb-buffer.cc | 39 ++++++++++++++++++++++++++++++++++++++ src/hb-buffer.h | 3 +++ 3 files changed, 43 insertions(+) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index a808da299..34a3918b4 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -103,6 +103,7 @@ hb_buffer_serialize_format_to_string hb_buffer_serialize_list_formats hb_segment_properties_equal hb_segment_properties_hash +hb_segment_properties_overlay hb_buffer_diff hb_buffer_set_message_func hb_buffer_t diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index be3161a54..abfb75f1c 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -86,7 +86,46 @@ hb_segment_properties_hash (const hb_segment_properties_t *p) (intptr_t) (p->language); } +/** + * hb_segment_properties_overlay: + * @p: #hb_segment_properties_t to fill in. + * @src: #hb_segment_properties_t to fill in from. + * + * Fills in missing fields of @p from @src in a considered manner. + * + * First, if @p does not have direction set, direction is copied from @src. + * + * Next, if @p and @src have the same direction (which can be unset), if @p + * does not have script set, script is copied from @src. + * + * Finally, if @p and @src have the same direction and script (which either + * can be unset), if @p does not have language set, language is copied from + * @src. + * + * Since: REPLACEME + **/ +HB_EXTERN void +hb_segment_properties_overlay (hb_segment_properties_t *p, + const hb_segment_properties_t *src) +{ + if (unlikely (!p || !src)) + return; + if (!p->direction) + p->direction = src->direction; + + if (p->direction != src->direction) + return; + + if (!p->script) + p->script = src->script; + + if (p->script != src->script) + return; + + if (!p->language) + p->language = src->language; +} /* Here is how the buffer works internally: * diff --git a/src/hb-buffer.h b/src/hb-buffer.h index a183cb9d4..deb76d411 100644 --- a/src/hb-buffer.h +++ b/src/hb-buffer.h @@ -170,6 +170,9 @@ hb_segment_properties_equal (const hb_segment_properties_t *a, HB_EXTERN unsigned int hb_segment_properties_hash (const hb_segment_properties_t *p); +HB_EXTERN void +hb_segment_properties_overlay (hb_segment_properties_t *p, + const hb_segment_properties_t *src); /** From 36e9f467a43d18f5b040e2bd47c87f271916a73f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:23:14 -0700 Subject: [PATCH 2/9] [buffer] Overlay segment-properties in hb_buffer_append() Part of https://github.com/harfbuzz/harfbuzz/issues/1555 --- src/hb-buffer.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index abfb75f1c..7f2f93fac 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -1788,6 +1788,8 @@ hb_buffer_append (hb_buffer_t *buffer, if (!buffer->have_positions && source->have_positions) buffer->clear_positions (); + hb_segment_properties_overlay (&buffer->props, &source->props); + memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0])); if (buffer->have_positions) memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0])); From 101d07efd9a11b1f215b913205d2560ee0df9135 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:24:07 -0700 Subject: [PATCH 3/9] [util] Move copy_buffer_properties() out of loop Now that hb_buffer_append() overlays properties, we can do this. Part of https://github.com/harfbuzz/harfbuzz/issues/1555 --- util/shape-options.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/shape-options.hh b/util/shape-options.hh index 8d3318420..a1491429e 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -184,6 +184,7 @@ struct shape_options_t /* Check that breaking up shaping at safe-to-break is indeed safe. */ hb_buffer_t *fragment = hb_buffer_create (); + copy_buffer_properties (fragment, buffer); hb_buffer_t *reconstruction = hb_buffer_create (); copy_buffer_properties (reconstruction, buffer); @@ -234,7 +235,6 @@ struct shape_options_t printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end); hb_buffer_clear_contents (fragment); - copy_buffer_properties (fragment, buffer); hb_buffer_flags_t flags = hb_buffer_get_flags (fragment); if (0 < text_start) From 34f5b855aa39cc5ac59fb7cd3e9e3a48507e1a08 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:25:06 -0700 Subject: [PATCH 4/9] [util] Simplify copy_buffer_properties() Now that hb_buffer_append() overlays segment_properties we can do this. Part of https://github.com/harfbuzz/harfbuzz/issues/1555 --- util/shape-options.hh | 3 --- 1 file changed, 3 deletions(-) diff --git a/util/shape-options.hh b/util/shape-options.hh index a1491429e..e6e709239 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -62,9 +62,6 @@ struct shape_options_t static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src) { - hb_segment_properties_t props; - hb_buffer_get_segment_properties (src, &props); - hb_buffer_set_segment_properties (dst, &props); hb_buffer_set_flags (dst, hb_buffer_get_flags (src)); hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src)); } From d0c3515ce423f689123f96111b105e7e4141a6b9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:26:41 -0700 Subject: [PATCH 5/9] [util] Copy unicode_funcs in copy_buffer_properties() Part of https://github.com/harfbuzz/harfbuzz/issues/1555 --- util/shape-options.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/util/shape-options.hh b/util/shape-options.hh index e6e709239..58a50d8d1 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -62,6 +62,7 @@ struct shape_options_t static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src) { + hb_buffer_set_unicode_funcs (dst, hb_buffer_get_unicode_funcs (src)); hb_buffer_set_flags (dst, hb_buffer_get_flags (src)); hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src)); } From f643b81ffc4d4dd6a100f83ff7bc618dfb7f5b69 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:45:31 -0700 Subject: [PATCH 6/9] [buffer] Clean up internal state bookkeeping hb_buffer_reset() was NOT resetting cluster_level. Ouch! Fix that. Part of https://github.com/harfbuzz/harfbuzz/issues/1555 --- src/hb-buffer.cc | 15 ++++++++------- src/hb-buffer.hh | 25 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 7f2f93fac..be049cbb3 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -261,6 +261,7 @@ hb_buffer_t::reset () hb_unicode_funcs_destroy (unicode); unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ()); flags = HB_BUFFER_FLAG_DEFAULT; + cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT; replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT; invisible = 0; not_found = 0; @@ -271,11 +272,10 @@ hb_buffer_t::reset () void hb_buffer_t::clear () { + content_type = HB_BUFFER_CONTENT_TYPE_INVALID; hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT; props = default_props; - scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; - content_type = HB_BUFFER_CONTENT_TYPE_INVALID; successful = true; have_output = false; have_positions = false; @@ -283,13 +283,15 @@ hb_buffer_t::clear () idx = 0; len = 0; out_len = 0; - out_info = info; - serial = 0; + out_info = info; memset (context, 0, sizeof context); memset (context_len, 0, sizeof context_len); + scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; + serial = 0; + deallocate_var_all (); } @@ -604,12 +606,11 @@ DEFINE_NULL_INSTANCE (hb_buffer_t) = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT, 0, /* invisible */ 0, /* not_found */ - HB_BUFFER_SCRATCH_FLAG_DEFAULT, - HB_BUFFER_MAX_LEN_DEFAULT, - HB_BUFFER_MAX_OPS_DEFAULT, + HB_BUFFER_CONTENT_TYPE_INVALID, HB_SEGMENT_PROPERTIES_DEFAULT, + false, /* successful */ false, /* have_output */ true /* have_positions */ diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 0f8140f1b..2b849c6a4 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -87,18 +87,21 @@ struct hb_buffer_t { hb_object_header_t header; - /* Information about how the text in the buffer should be treated */ + /* + * Information about how the text in the buffer should be treated. + */ + hb_unicode_funcs_t *unicode; /* Unicode functions */ hb_buffer_flags_t flags; /* BOT / EOT / etc. */ hb_buffer_cluster_level_t cluster_level; hb_codepoint_t replacement; /* U+FFFD or something else. */ hb_codepoint_t invisible; /* 0 or something else. */ hb_codepoint_t not_found; /* 0 or something else. */ - hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */ - unsigned int max_len; /* Maximum allowed len. */ - int max_ops; /* Maximum allowed operations. */ - /* Buffer contents */ + /* + * Buffer contents + */ + hb_buffer_content_type_t content_type; hb_segment_properties_t props; /* Script, language, direction */ @@ -115,8 +118,6 @@ struct hb_buffer_t hb_glyph_info_t *out_info; hb_glyph_position_t *pos; - unsigned int serial; - /* Text before / after the main buffer contents. * Always in Unicode, and ordered outward. * Index 0 is for "pre-context", 1 for "post-context". */ @@ -124,6 +125,16 @@ struct hb_buffer_t hb_codepoint_t context[2][CONTEXT_LENGTH]; unsigned int context_len[2]; + /* + * Not part of content. + */ + + hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */ + unsigned int serial; + + unsigned int max_len; /* Maximum allowed len. */ + int max_ops; /* Maximum allowed operations. */ + /* Debugging API */ #ifndef HB_NO_BUFFER_MESSAGE hb_buffer_message_func_t message_func; From 52f5711ed0354b0c5d396255886f03048997bbd9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:57:42 -0700 Subject: [PATCH 7/9] [buffer] Add hb_buffer_create_similar() Fixes https://github.com/harfbuzz/harfbuzz/issues/1555 --- docs/harfbuzz-sections.txt | 1 + src/hb-buffer.cc | 73 ++++++++++++++++++++++++++++---------- src/hb-buffer.h | 12 +++++-- src/hb-buffer.hh | 1 + util/shape-options.hh | 13 ++----- 5 files changed, 68 insertions(+), 32 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 34a3918b4..9dda7ccd8 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -45,6 +45,7 @@ hb_memory_mode_t HB_SEGMENT_PROPERTIES_DEFAULT HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT hb_buffer_create +hb_buffer_create_similar hb_buffer_reference hb_buffer_get_empty hb_buffer_destroy diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index be049cbb3..88e113760 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -104,7 +104,7 @@ hb_segment_properties_hash (const hb_segment_properties_t *p) * * Since: REPLACEME **/ -HB_EXTERN void +void hb_segment_properties_overlay (hb_segment_properties_t *p, const hb_segment_properties_t *src) { @@ -255,6 +255,18 @@ hb_buffer_t::get_scratch_buffer (unsigned int *size) /* HarfBuzz-Internal API */ +void +hb_buffer_t::similar (const hb_buffer_t &src) +{ + hb_unicode_funcs_destroy (unicode); + unicode = hb_unicode_funcs_reference (src.unicode); + flags = src.flags; + cluster_level = src.cluster_level; + replacement = src.invisible; + invisible = src.invisible; + not_found = src.not_found; +} + void hb_buffer_t::reset () { @@ -649,6 +661,47 @@ hb_buffer_create () return buffer; } +/** + * hb_buffer_create_similar: + * @src: An #hb_buffer_t + * + * Resets the buffer to its initial status, as if it was just newly created + * with hb_buffer_create(). + * + * Return value: (transfer full): + * A newly allocated #hb_buffer_t, similar to hb_buffer_create(). The only + * difference is that the buffer is configured similarly to @src. + * + * Since: REPLACEME + **/ +hb_buffer_t * +hb_buffer_create_similar (const hb_buffer_t *src) +{ + hb_buffer_t *buffer = hb_buffer_create (); + + buffer->similar (*src); + + return buffer; +} + +/** + * hb_buffer_reset: + * @buffer: An #hb_buffer_t + * + * Resets the buffer to its initial status, as if it was just newly created + * with hb_buffer_create(). + * + * Since: 0.9.2 + **/ +void +hb_buffer_reset (hb_buffer_t *buffer) +{ + if (unlikely (hb_object_is_immutable (buffer))) + return; + + buffer->reset (); +} + /** * hb_buffer_get_empty: * @@ -1196,24 +1249,6 @@ hb_buffer_get_not_found_glyph (hb_buffer_t *buffer) } -/** - * hb_buffer_reset: - * @buffer: An #hb_buffer_t - * - * Resets the buffer to its initial status, as if it was just newly created - * with hb_buffer_create(). - * - * Since: 0.9.2 - **/ -void -hb_buffer_reset (hb_buffer_t *buffer) -{ - if (unlikely (hb_object_is_immutable (buffer))) - return; - - buffer->reset (); -} - /** * hb_buffer_clear_contents: * @buffer: An #hb_buffer_t diff --git a/src/hb-buffer.h b/src/hb-buffer.h index deb76d411..2ba56d345 100644 --- a/src/hb-buffer.h +++ b/src/hb-buffer.h @@ -187,6 +187,13 @@ typedef struct hb_buffer_t hb_buffer_t; HB_EXTERN hb_buffer_t * hb_buffer_create (void); +HB_EXTERN hb_buffer_t * +hb_buffer_create_similar (const hb_buffer_t *src); + +HB_EXTERN void +hb_buffer_reset (hb_buffer_t *buffer); + + HB_EXTERN hb_buffer_t * hb_buffer_get_empty (void); @@ -394,8 +401,9 @@ HB_EXTERN hb_codepoint_t hb_buffer_get_not_found_glyph (hb_buffer_t *buffer); -HB_EXTERN void -hb_buffer_reset (hb_buffer_t *buffer); +/* + * Content API. + */ HB_EXTERN void hb_buffer_clear_contents (hb_buffer_t *buffer); diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 2b849c6a4..5a6417cc8 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -201,6 +201,7 @@ struct hb_buffer_t hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; } hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; } + HB_INTERNAL void similar (const hb_buffer_t &src); HB_INTERNAL void reset (); HB_INTERNAL void clear (); diff --git a/util/shape-options.hh b/util/shape-options.hh index 58a50d8d1..b7eb846dc 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -60,13 +60,6 @@ struct shape_options_t hb_buffer_guess_segment_properties (buffer); } - static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src) - { - hb_buffer_set_unicode_funcs (dst, hb_buffer_get_unicode_funcs (src)); - hb_buffer_set_flags (dst, hb_buffer_get_flags (src)); - hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src)); - } - void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len, const char *text_before, const char *text_after) { @@ -181,10 +174,8 @@ struct shape_options_t /* Check that breaking up shaping at safe-to-break is indeed safe. */ - hb_buffer_t *fragment = hb_buffer_create (); - copy_buffer_properties (fragment, buffer); - hb_buffer_t *reconstruction = hb_buffer_create (); - copy_buffer_properties (reconstruction, buffer); + hb_buffer_t *fragment = hb_buffer_create_similar (buffer); + hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer); unsigned int num_glyphs; hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs); From 43be5ba442548528c89ad31c0927cc68515b736e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Jan 2022 07:26:53 -0700 Subject: [PATCH 8/9] [buffer] Group shape-related members together --- src/hb-buffer.cc | 5 ++--- src/hb-buffer.hh | 14 ++++++-------- src/hb-ot-layout.hh | 5 ++--- src/hb-ot-shape.cc | 2 ++ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 88e113760..7401213e3 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -301,10 +301,9 @@ hb_buffer_t::clear () memset (context, 0, sizeof context); memset (context_len, 0, sizeof context_len); - scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; - serial = 0; - deallocate_var_all (); + serial = 0; + scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; } void diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 5a6417cc8..f2cbc7580 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -129,11 +129,14 @@ struct hb_buffer_t * Not part of content. */ +#ifndef HB_NDEBUG + uint8_t allocated_var_bits; +#endif + uint8_t serial; hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */ - unsigned int serial; - unsigned int max_len; /* Maximum allowed len. */ int max_ops; /* Maximum allowed operations. */ + /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */ /* Debugging API */ #ifndef HB_NO_BUFFER_MESSAGE @@ -145,11 +148,6 @@ struct hb_buffer_t static constexpr unsigned message_depth = 0u; #endif - /* Internal debugging. */ - /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */ -#ifndef HB_NDEBUG - uint8_t allocated_var_bits; -#endif /* Methods */ @@ -207,7 +205,7 @@ struct hb_buffer_t unsigned int backtrack_len () const { return have_output ? out_len : idx; } unsigned int lookahead_len () const { return len - idx; } - unsigned int next_serial () { return serial++; } + uint8_t next_serial () { return ++serial ? serial : ++serial; } HB_INTERNAL void add (hb_codepoint_t codepoint, unsigned int cluster); diff --git a/src/hb-ot-layout.hh b/src/hb-ot-layout.hh index 2c825e0c8..ede8f007d 100644 --- a/src/hb-ot-layout.hh +++ b/src/hb-ot-layout.hh @@ -482,10 +482,9 @@ _hb_glyph_info_get_lig_num_comps (const hb_glyph_info_t *info) } static inline uint8_t -_hb_allocate_lig_id (hb_buffer_t *buffer) { +_hb_allocate_lig_id (hb_buffer_t *buffer) +{ uint8_t lig_id = buffer->next_serial () & 0x07; - if (unlikely (!lig_id)) - lig_id = _hb_allocate_lig_id (buffer); /* in case of overflow */ return lig_id; } diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index 4dde3520d..dc66d2c8c 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -1146,6 +1146,7 @@ static void hb_ot_shape_internal (hb_ot_shape_context_t *c) { c->buffer->deallocate_var_all (); + c->buffer->serial = 0; c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR))) { @@ -1191,6 +1192,7 @@ hb_ot_shape_internal (hb_ot_shape_context_t *c) c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT; c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT; c->buffer->deallocate_var_all (); + c->buffer->serial = 0; } From d1e7df5c3f214b0f199fc2aff3e5645281fc5caa Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Jan 2022 07:32:04 -0700 Subject: [PATCH 9/9] [buffer] Add enter()/leave() pair around shape() --- src/hb-buffer.cc | 27 +++++++++++++++++++++++++++ src/hb-buffer.hh | 13 +++++++++++-- src/hb-ot-shape.cc | 19 ++----------------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 7401213e3..f5c709043 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -306,6 +306,33 @@ hb_buffer_t::clear () scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; } +void +hb_buffer_t::enter () +{ + deallocate_var_all (); + serial = 0; + scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; + if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_LEN_FACTOR))) + { + max_len = hb_max (len * HB_BUFFER_MAX_LEN_FACTOR, + (unsigned) HB_BUFFER_MAX_LEN_MIN); + } + if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_OPS_FACTOR))) + { + max_ops = hb_max (len * HB_BUFFER_MAX_OPS_FACTOR, + (unsigned) HB_BUFFER_MAX_OPS_MIN); + } +} +void +hb_buffer_t::leave () +{ + max_len = HB_BUFFER_MAX_LEN_DEFAULT; + max_ops = HB_BUFFER_MAX_OPS_DEFAULT; + deallocate_var_all (); + serial = 0; +} + + void hb_buffer_t::add (hb_codepoint_t codepoint, unsigned int cluster) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index f2cbc7580..7d3ef34a2 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -125,8 +125,9 @@ struct hb_buffer_t hb_codepoint_t context[2][CONTEXT_LENGTH]; unsigned int context_len[2]; + /* - * Not part of content. + * Managed by enter / leave */ #ifndef HB_NDEBUG @@ -138,7 +139,11 @@ struct hb_buffer_t int max_ops; /* Maximum allowed operations. */ /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */ - /* Debugging API */ + + /* + * Messaging callback + */ + #ifndef HB_NO_BUFFER_MESSAGE hb_buffer_message_func_t message_func; void *message_data; @@ -203,6 +208,10 @@ struct hb_buffer_t HB_INTERNAL void reset (); HB_INTERNAL void clear (); + /* Called around shape() */ + HB_INTERNAL void enter (); + HB_INTERNAL void leave (); + unsigned int backtrack_len () const { return have_output ? out_len : idx; } unsigned int lookahead_len () const { return len - idx; } uint8_t next_serial () { return ++serial ? serial : ++serial; } diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index dc66d2c8c..11fcc11f9 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -1145,19 +1145,7 @@ hb_propagate_flags (hb_buffer_t *buffer) static void hb_ot_shape_internal (hb_ot_shape_context_t *c) { - c->buffer->deallocate_var_all (); - c->buffer->serial = 0; - c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; - if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR))) - { - c->buffer->max_len = hb_max (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR, - (unsigned) HB_BUFFER_MAX_LEN_MIN); - } - if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR))) - { - c->buffer->max_ops = hb_max (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR, - (unsigned) HB_BUFFER_MAX_OPS_MIN); - } + c->buffer->enter (); /* Save the original direction, we use it later. */ c->target_direction = c->buffer->props.direction; @@ -1189,10 +1177,7 @@ hb_ot_shape_internal (hb_ot_shape_context_t *c) c->buffer->props.direction = c->target_direction; - c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT; - c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT; - c->buffer->deallocate_var_all (); - c->buffer->serial = 0; + c->buffer->leave (); }