summaryrefslogtreecommitdiff
path: root/thirdparty/harfbuzz/src/hb-buffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/harfbuzz/src/hb-buffer.cc')
-rw-r--r--thirdparty/harfbuzz/src/hb-buffer.cc196
1 files changed, 93 insertions, 103 deletions
diff --git a/thirdparty/harfbuzz/src/hb-buffer.cc b/thirdparty/harfbuzz/src/hb-buffer.cc
index 10063db050..c6591ca230 100644
--- a/thirdparty/harfbuzz/src/hb-buffer.cc
+++ b/thirdparty/harfbuzz/src/hb-buffer.cc
@@ -96,14 +96,15 @@ hb_segment_properties_hash (const hb_segment_properties_t *p)
* As an optimization, both info and out_info may point to the
* same piece of memory, which is owned by info. This remains the
* case as long as out_len doesn't exceed i at any time.
- * In that case, swap_buffers() is no-op and the glyph operations operate
- * mostly in-place.
+ * In that case, swap_buffers() is mostly no-op and the glyph operations
+ * operate mostly in-place.
*
* As soon as out_info gets longer than info, out_info is moved over
- * to an alternate buffer (which we reuse the pos buffer for!), and its
+ * to an alternate buffer (which we reuse the pos buffer for), and its
* current contents (out_len entries) are copied to the new place.
+ *
* This should all remain transparent to the user. swap_buffers() then
- * switches info and out_info.
+ * switches info over to out_info and does housekeeping.
*/
@@ -136,8 +137,8 @@ hb_buffer_t::enlarge (unsigned int size)
if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]))))
goto done;
- new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
- new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
+ new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_allocated * sizeof (pos[0]));
+ new_info = (hb_glyph_info_t *) hb_realloc (info, new_allocated * sizeof (info[0]));
done:
if (unlikely (!new_pos || !new_info))
@@ -218,9 +219,6 @@ hb_buffer_t::get_scratch_buffer (unsigned int *size)
void
hb_buffer_t::reset ()
{
- if (unlikely (hb_object_is_immutable (this)))
- return;
-
hb_unicode_funcs_destroy (unicode);
unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());
flags = HB_BUFFER_FLAG_DEFAULT;
@@ -233,9 +231,6 @@ hb_buffer_t::reset ()
void
hb_buffer_t::clear ()
{
- if (unlikely (hb_object_is_immutable (this)))
- return;
-
hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
props = default_props;
scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
@@ -288,27 +283,12 @@ hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
void
-hb_buffer_t::remove_output ()
-{
- if (unlikely (hb_object_is_immutable (this)))
- return;
-
- have_output = false;
- have_positions = false;
-
- out_len = 0;
- out_info = info;
-}
-
-void
hb_buffer_t::clear_output ()
{
- if (unlikely (hb_object_is_immutable (this)))
- return;
-
have_output = true;
have_positions = false;
+ idx = 0;
out_len = 0;
out_info = info;
}
@@ -316,9 +296,6 @@ hb_buffer_t::clear_output ()
void
hb_buffer_t::clear_positions ()
{
- if (unlikely (hb_object_is_immutable (this)))
- return;
-
have_output = false;
have_positions = true;
@@ -331,53 +308,26 @@ hb_buffer_t::clear_positions ()
void
hb_buffer_t::swap_buffers ()
{
- if (unlikely (!successful)) return;
-
assert (have_output);
- have_output = false;
+
+ assert (idx <= len);
+
+ if (unlikely (!successful || !next_glyphs (len - idx)))
+ goto reset;
if (out_info != info)
{
- hb_glyph_info_t *tmp_string;
- tmp_string = info;
+ pos = (hb_glyph_position_t *) info;
info = out_info;
- out_info = tmp_string;
- pos = (hb_glyph_position_t *) out_info;
}
-
- unsigned int tmp;
- tmp = len;
len = out_len;
- out_len = tmp;
+reset:
+ have_output = false;
+ out_len = 0;
idx = 0;
}
-
-void
-hb_buffer_t::replace_glyphs (unsigned int num_in,
- unsigned int num_out,
- const uint32_t *glyph_data)
-{
- if (unlikely (!make_room_for (num_in, num_out))) return;
-
- assert (idx + num_in <= len);
-
- merge_clusters (idx, idx + num_in);
-
- hb_glyph_info_t orig_info = info[idx];
- hb_glyph_info_t *pinfo = &out_info[out_len];
- for (unsigned int i = 0; i < num_out; i++)
- {
- *pinfo = orig_info;
- pinfo->codepoint = glyph_data[i];
- pinfo++;
- }
-
- idx += num_in;
- out_len += num_out;
-}
-
bool
hb_buffer_t::move_to (unsigned int i)
{
@@ -409,12 +359,11 @@ hb_buffer_t::move_to (unsigned int i)
/* This will blow in our face if memory allocation fails later
* in this same lookup...
*
- * We used to shift with extra 32 items, instead of the 0 below.
+ * We used to shift with extra 32 items.
* But that would leave empty slots in the buffer in case of allocation
- * failures. Setting to zero for now to avoid other problems (see
- * comments in shift_forward(). This can cause O(N^2) behavior more
- * severely than adding 32 empty slots can... */
- if (unlikely (idx < count && !shift_forward (count + 0))) return false;
+ * failures. See comments in shift_forward(). This can cause O(N^2)
+ * behavior more severely than adding 32 empty slots can... */
+ if (unlikely (idx < count && !shift_forward (count - idx))) return false;
assert (idx >= count);
@@ -666,7 +615,7 @@ DEFINE_NULL_INSTANCE (hb_buffer_t) =
HB_BUFFER_CONTENT_TYPE_INVALID,
HB_SEGMENT_PROPERTIES_DEFAULT,
false, /* successful */
- true, /* have_output */
+ false, /* have_output */
true /* have_positions */
/* Zero is good enough for everything else. */
@@ -753,14 +702,14 @@ hb_buffer_destroy (hb_buffer_t *buffer)
hb_unicode_funcs_destroy (buffer->unicode);
- free (buffer->info);
- free (buffer->pos);
+ hb_free (buffer->info);
+ hb_free (buffer->pos);
#ifndef HB_NO_BUFFER_MESSAGE
if (buffer->message_destroy)
buffer->message_destroy (buffer->message_data);
#endif
- free (buffer);
+ hb_free (buffer);
}
/**
@@ -768,7 +717,7 @@ hb_buffer_destroy (hb_buffer_t *buffer)
* @buffer: An #hb_buffer_t
* @key: The user-data key
* @data: A pointer to the user data
- * @destroy: (optional): A callback to call when @data is not needed anymore
+ * @destroy: (nullable): A callback to call when @data is not needed anymore
* @replace: Whether to replace an existing data with the same key
*
* Attaches a user-data key/data pair to the specified buffer.
@@ -795,7 +744,7 @@ hb_buffer_set_user_data (hb_buffer_t *buffer,
* Fetches the user data associated with the specified key,
* attached to the specified buffer.
*
- * Return value: (transfer-none): A pointer to the user data
+ * Return value: (transfer none): A pointer to the user data
*
* Since: 0.9.2
**/
@@ -1137,7 +1086,7 @@ hb_buffer_get_cluster_level (hb_buffer_t *buffer)
* Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
* when adding text to @buffer.
*
- * Default is %HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
+ * Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
*
* Since: 0.9.31
**/
@@ -1222,6 +1171,9 @@ hb_buffer_get_invisible_glyph (hb_buffer_t *buffer)
void
hb_buffer_reset (hb_buffer_t *buffer)
{
+ if (unlikely (hb_object_is_immutable (buffer)))
+ return;
+
buffer->reset ();
}
@@ -1237,6 +1189,9 @@ hb_buffer_reset (hb_buffer_t *buffer)
void
hb_buffer_clear_contents (hb_buffer_t *buffer)
{
+ if (unlikely (hb_object_is_immutable (buffer)))
+ return;
+
buffer->clear ();
}
@@ -1321,7 +1276,7 @@ hb_buffer_set_length (hb_buffer_t *buffer,
if (unlikely (hb_object_is_immutable (buffer)))
return length == 0;
- if (!buffer->ensure (length))
+ if (unlikely (!buffer->ensure (length)))
return false;
/* Wipe the new space */
@@ -1393,6 +1348,11 @@ hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
* Returns @buffer glyph position array. Returned pointer
* is valid as long as @buffer contents are not modified.
*
+ * If buffer did not have positions before, the positions will be
+ * initialized to zeros, unless this function is called from
+ * within a buffer message callback (see hb_buffer_set_message_func()),
+ * in which case %NULL is returned.
+ *
* Return value: (transfer none) (array length=length):
* The @buffer glyph position array.
* The value valid as long as buffer has not been modified.
@@ -1403,12 +1363,17 @@ hb_glyph_position_t *
hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
unsigned int *length)
{
- if (!buffer->have_positions)
- buffer->clear_positions ();
-
if (length)
*length = buffer->len;
+ if (!buffer->have_positions)
+ {
+ if (unlikely (buffer->message_depth))
+ return nullptr;
+
+ buffer->clear_positions ();
+ }
+
return (hb_glyph_position_t *) buffer->pos;
}
@@ -1501,20 +1466,20 @@ hb_buffer_reverse_clusters (hb_buffer_t *buffer)
*
* Sets unset buffer segment properties based on buffer Unicode
* contents. If buffer is not empty, it must have content type
- * %HB_BUFFER_CONTENT_TYPE_UNICODE.
+ * #HB_BUFFER_CONTENT_TYPE_UNICODE.
*
- * If buffer script is not set (ie. is %HB_SCRIPT_INVALID), it
+ * If buffer script is not set (ie. is #HB_SCRIPT_INVALID), it
* will be set to the Unicode script of the first character in
- * the buffer that has a script other than %HB_SCRIPT_COMMON,
- * %HB_SCRIPT_INHERITED, and %HB_SCRIPT_UNKNOWN.
+ * the buffer that has a script other than #HB_SCRIPT_COMMON,
+ * #HB_SCRIPT_INHERITED, and #HB_SCRIPT_UNKNOWN.
*
- * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID),
+ * Next, if buffer direction is not set (ie. is #HB_DIRECTION_INVALID),
* it will be set to the natural horizontal direction of the
* buffer script as returned by hb_script_get_horizontal_direction().
- * If hb_script_get_horizontal_direction() returns %HB_DIRECTION_INVALID,
- * then %HB_DIRECTION_LTR is used.
+ * If hb_script_get_horizontal_direction() returns #HB_DIRECTION_INVALID,
+ * then #HB_DIRECTION_LTR is used.
*
- * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID),
+ * Finally, if buffer language is not set (ie. is #HB_LANGUAGE_INVALID),
* it will be set to the process's default language as returned by
* hb_language_get_default(). This may change in the future by
* taking buffer script into consideration when choosing a language.
@@ -1551,7 +1516,10 @@ hb_buffer_add_utf (hb_buffer_t *buffer,
if (item_length == -1)
item_length = text_length - item_offset;
- buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
+ if (unlikely (item_length < 0 ||
+ item_length > INT_MAX / 8 ||
+ !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
+ return;
/* If buffer is empty and pre-context provided, install it.
* This check is written this way, to make sure people can
@@ -1768,11 +1736,6 @@ hb_buffer_append (hb_buffer_t *buffer,
if (start == end)
return;
- if (!buffer->len)
- buffer->content_type = source->content_type;
- if (!buffer->have_positions && source->have_positions)
- buffer->clear_positions ();
-
if (buffer->len + (end - start) < buffer->len) /* Overflows. */
{
buffer->successful = false;
@@ -1784,9 +1747,36 @@ hb_buffer_append (hb_buffer_t *buffer,
if (unlikely (!buffer->successful))
return;
+ if (!orig_len)
+ buffer->content_type = source->content_type;
+ if (!buffer->have_positions && source->have_positions)
+ buffer->clear_positions ();
+
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]));
+
+ if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
+ {
+ /* See similar logic in add_utf. */
+
+ /* pre-context */
+ if (!orig_len && start + source->context_len[0] > 0)
+ {
+ buffer->clear_context (0);
+ while (start > 0 && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
+ buffer->context[0][buffer->context_len[0]++] = source->info[--start].codepoint;
+ for (auto i = 0u; i < source->context_len[0] && buffer->context_len[0] < buffer->CONTEXT_LENGTH; i++)
+ buffer->context[0][buffer->context_len[0]++] = source->context[0][i];
+ }
+
+ /* post-context */
+ buffer->clear_context (1);
+ while (end < source->len && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
+ buffer->context[1][buffer->context_len[1]++] = source->info[end++].codepoint;
+ for (auto i = 0u; i < source->context_len[1] && buffer->context_len[1] < buffer->CONTEXT_LENGTH; i++)
+ buffer->context[1][buffer->context_len[1]++] = source->context[1][i];
+ }
}
@@ -1902,8 +1892,8 @@ hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_g
* @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepont_t) -1.
* @position_fuzz: allowed absolute difference in position values.
*
- * If dottedcircle_glyph is (hb_codepoint_t) -1 then %HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
- * and %HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most
+ * If dottedcircle_glyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
+ * and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most
* callers if just comparing two buffers is needed.
*
* Since: 1.5.0
@@ -1994,11 +1984,11 @@ hb_buffer_diff (hb_buffer_t *buffer,
/**
* hb_buffer_set_message_func:
* @buffer: An #hb_buffer_t
- * @func: (closure user_data) (destroy destroy) (scope notified):
- * @user_data:
- * @destroy:
- *
+ * @func: (closure user_data) (destroy destroy) (scope notified): Callback function
+ * @user_data: (nullable): Data to pass to @func
+ * @destroy: (nullable): The function to call when @user_data is not needed anymore
*
+ * Sets the implementation function for #hb_buffer_message_func_t.
*
* Since: 1.1.3
**/