From 52f5711ed0354b0c5d396255886f03048997bbd9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 3 Jan 2022 11:57:42 -0700 Subject: [PATCH] [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);