Skip to content

Commit

Permalink
[buffer] Add hb_buffer_create_similar()
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad authored and Matthias Clasen committed Jan 7, 2022
1 parent edcd13e commit 032507a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 32 deletions.
1 change: 1 addition & 0 deletions docs/harfbuzz-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 54 additions & 19 deletions src/hb-buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 ()
{
Expand Down Expand Up @@ -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:
*
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/hb-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/hb-buffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Expand Down
13 changes: 2 additions & 11 deletions util/shape-options.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 032507a

Please sign in to comment.