[buffer] Add hb_buffer_create_similar()

Fixes https://github.com/harfbuzz/harfbuzz/issues/1555
This commit is contained in:
Behdad Esfahbod 2022-01-03 11:57:42 -07:00
parent f643b81ffc
commit 52f5711ed0
5 changed files with 68 additions and 32 deletions

View File

@ -45,6 +45,7 @@ hb_memory_mode_t
HB_SEGMENT_PROPERTIES_DEFAULT HB_SEGMENT_PROPERTIES_DEFAULT
HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT
hb_buffer_create hb_buffer_create
hb_buffer_create_similar
hb_buffer_reference hb_buffer_reference
hb_buffer_get_empty hb_buffer_get_empty
hb_buffer_destroy hb_buffer_destroy

View File

@ -104,7 +104,7 @@ hb_segment_properties_hash (const hb_segment_properties_t *p)
* *
* Since: REPLACEME * Since: REPLACEME
**/ **/
HB_EXTERN void void
hb_segment_properties_overlay (hb_segment_properties_t *p, hb_segment_properties_overlay (hb_segment_properties_t *p,
const hb_segment_properties_t *src) const hb_segment_properties_t *src)
{ {
@ -255,6 +255,18 @@ hb_buffer_t::get_scratch_buffer (unsigned int *size)
/* HarfBuzz-Internal API */ /* 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 void
hb_buffer_t::reset () hb_buffer_t::reset ()
{ {
@ -649,6 +661,47 @@ hb_buffer_create ()
return buffer; 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: * 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: * hb_buffer_clear_contents:
* @buffer: An #hb_buffer_t * @buffer: An #hb_buffer_t

View File

@ -187,6 +187,13 @@ typedef struct hb_buffer_t hb_buffer_t;
HB_EXTERN hb_buffer_t * HB_EXTERN hb_buffer_t *
hb_buffer_create (void); 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_EXTERN hb_buffer_t *
hb_buffer_get_empty (void); 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_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_EXTERN void
hb_buffer_clear_contents (hb_buffer_t *buffer); hb_buffer_clear_contents (hb_buffer_t *buffer);

View File

@ -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 () { 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_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 reset ();
HB_INTERNAL void clear (); HB_INTERNAL void clear ();

View File

@ -60,13 +60,6 @@ struct shape_options_t
hb_buffer_guess_segment_properties (buffer); 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, void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
const char *text_before, const char *text_after) 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. */ /* Check that breaking up shaping at safe-to-break is indeed safe. */
hb_buffer_t *fragment = hb_buffer_create (); hb_buffer_t *fragment = hb_buffer_create_similar (buffer);
copy_buffer_properties (fragment, buffer); hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);
hb_buffer_t *reconstruction = hb_buffer_create ();
copy_buffer_properties (reconstruction, buffer);
unsigned int num_glyphs; unsigned int num_glyphs;
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs); hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);