Add variation deltas to buffer structure

This commit is contained in:
Simon Cozens committed 2026-08-04 08:15:40 +01:00
1 parent c31bd6797a
commit 5ddecb74ba
3 files changed
+213 -1

No files matched your search

+123 -1
View File
@@ -170,6 +170,7 @@ hb_buffer_t::enlarge (unsigned int size)
unsigned int new_allocated = allocated;
hb_glyph_position_t *new_pos = nullptr;
hb_glyph_info_t *new_info = nullptr;
int *new_coords_deltas = nullptr;
bool separate_out = out_info != info;
if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
@@ -186,8 +187,15 @@ hb_buffer_t::enlarge (unsigned int size)
new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_bytes);
new_info = (hb_glyph_info_t *) hb_realloc (info, new_bytes);
if (num_coords_deltas)
{
unsigned new_coords_bytes;
if (likely (!hb_unsigned_mul_overflows (new_allocated, num_coords_deltas * sizeof (int), &new_coords_bytes)))
new_coords_deltas = (int *) hb_realloc (coords_deltas, new_coords_bytes);
}
done:
if (unlikely (!new_pos || !new_info))
if (unlikely (!new_pos || !new_info || (num_coords_deltas && !new_coords_deltas)))
successful = false;
if (likely (new_pos))
@@ -196,6 +204,20 @@ done:
if (likely (new_info))
info = new_info;
if (likely (new_coords_deltas || !num_coords_deltas))
{
/* Zero out newly allocated glyph slots in the deltas array. */
if (coords_deltas && new_coords_deltas && new_coords_deltas != coords_deltas &&
allocated > 0 && new_allocated > allocated)
{
unsigned int old_bytes = allocated * num_coords_deltas * sizeof (int);
unsigned int new_bytes = new_allocated * num_coords_deltas * sizeof (int);
hb_memset ((char *) new_coords_deltas + old_bytes, 0,
new_bytes - old_bytes);
}
coords_deltas = new_coords_deltas;
}
out_info = separate_out ? (hb_glyph_info_t *) pos : info;
if (likely (successful))
allocated = new_allocated;
@@ -235,6 +257,12 @@ hb_buffer_t::shift_forward (unsigned int count)
}
memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
if (coords_deltas)
{
memmove (coords_deltas + (idx + count) * num_coords_deltas,
coords_deltas + idx * num_coords_deltas,
(len - idx) * num_coords_deltas * sizeof (int));
}
if (idx + count > len)
{
/* Under memory failure we might expose this area. At least
@@ -293,6 +321,7 @@ hb_buffer_t::reset ()
invisible = 0;
not_found = 0;
not_found_variation_selector = HB_CODEPOINT_INVALID;
num_coords_deltas = 0;
clear ();
}
@@ -313,6 +342,13 @@ hb_buffer_t::clear ()
out_len = 0;
out_info = info;
/* Zero variation deltas but keep the axis count. */
if (coords_deltas)
{
unsigned int coords_bytes = num_coords_deltas * allocated * sizeof (int);
hb_memset (coords_deltas, 0, coords_bytes);
}
hb_memset (context, 0, sizeof context);
hb_memset (context_len, 0, sizeof context_len);
@@ -485,6 +521,12 @@ hb_buffer_t::move_to (unsigned int i)
if (unlikely (!make_room_for (count, count))) return false;
memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
if (coords_deltas)
{
memmove (coords_deltas + out_len * num_coords_deltas,
coords_deltas + idx * num_coords_deltas,
count * num_coords_deltas * sizeof (int));
}
idx += count;
out_len += count;
}
@@ -507,6 +549,12 @@ hb_buffer_t::move_to (unsigned int i)
idx -= count;
out_len -= count;
memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
if (coords_deltas)
{
memmove (coords_deltas + idx * num_coords_deltas,
coords_deltas + out_len * num_coords_deltas,
count * num_coords_deltas * sizeof (int));
}
}
return true;
@@ -868,6 +916,7 @@ hb_buffer_destroy (hb_buffer_t *buffer)
hb_free (buffer->info);
hb_free (buffer->pos);
hb_free (buffer->coords_deltas);
#ifndef HB_NO_BUFFER_MESSAGE
if (buffer->message_destroy)
buffer->message_destroy (buffer->message_data);
@@ -2059,6 +2108,11 @@ hb_buffer_append (hb_buffer_t *buffer,
hb_memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));
if (buffer->have_positions)
hb_memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));
if (buffer->coords_deltas && source->coords_deltas &&
buffer->num_coords_deltas == source->num_coords_deltas)
hb_memcpy (buffer->coords_deltas + orig_len * buffer->num_coords_deltas,
source->coords_deltas + start * source->num_coords_deltas,
(end - start) * source->num_coords_deltas * sizeof (int));
if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
{
@@ -2355,6 +2409,74 @@ hb_buffer_changed (hb_buffer_t *buffer)
buffer->changed ();
}
/**
* hb_buffer_set_variation_deltas_num_coords:
* @buffer: An #hb_buffer_t
* @num_coords: Number of variation axes for per-glyph deltas
*
* Sets the number of variation axes for per-glyph delta storage.
* Must be called before setting per-glyph variation deltas.
* Pass 0 to disable per-glyph deltas and free associated storage.
*
* Since: REPLACEME
**/
void
hb_buffer_set_variation_deltas_num_coords (hb_buffer_t *buffer,
unsigned int num_coords)
{
if (!buffer || !buffer->successful)
return;
if (unlikely (!buffer->setup_variation_deltas (num_coords)))
return;
}
/**
* hb_buffer_set_glyph_variation_delta:
* @buffer: An #hb_buffer_t
* @glyph_index: Index of the glyph in the buffer
* @axis_index: Index of the variation axis
* @delta: Normalized variation delta (2.14 fixed-point)
*
* Sets a per-glyph variation delta for a specific axis, in 2.14
* fixed-point format (matching hb_font_t's internal normalized
* coordinate representation).
*
* Since: REPLACEME
**/
void
hb_buffer_set_glyph_variation_delta (hb_buffer_t *buffer,
unsigned int glyph_index,
unsigned int axis_index,
int delta)
{
if (buffer)
buffer->set_variation_delta (glyph_index, axis_index, delta);
}
/**
* hb_buffer_get_glyph_variation_delta:
* @buffer: An #hb_buffer_t
* @glyph_index: Index of the glyph in the buffer
* @axis_index: Index of the variation axis
*
* Gets the per-glyph variation delta for a specific axis.
*
* Return value: The normalized variation delta (2.14 fixed-point),
* or 0 if not set.
*
* Since: REPLACEME
**/
int
hb_buffer_get_glyph_variation_delta (hb_buffer_t *buffer,
unsigned int glyph_index,
unsigned int axis_index)
{
if (!buffer)
return 0;
return buffer->get_variation_delta (glyph_index, axis_index);
}
bool
hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
{
+14
View File
@@ -873,6 +873,20 @@ hb_buffer_set_message_func (hb_buffer_t *buffer,
HB_EXTERN void
hb_buffer_changed (hb_buffer_t *buffer);
HB_EXTERN void
hb_buffer_set_variation_deltas_num_coords (hb_buffer_t *buffer, unsigned int num_coords);
HB_EXTERN void
hb_buffer_set_glyph_variation_delta (hb_buffer_t *buffer,
unsigned int glyph_index,
unsigned int axis_index,
int delta);
HB_EXTERN int
hb_buffer_get_glyph_variation_delta (hb_buffer_t *buffer,
unsigned int glyph_index,
unsigned int axis_index);
HB_END_DECLS
+76
View File
@@ -103,6 +103,13 @@ struct hb_buffer_t
hb_glyph_info_t *out_info;
hb_glyph_position_t *pos;
/* Per-glyph normalized variation deltas (2.14 fixed-point, per OpenType spec).
* Sized num_coords_deltas * len, one int per axis per glyph,
* laid out as deltas[glyph_idx * num_coords_deltas + axis].
* nullptr when num_coords_deltas == 0. */
unsigned int num_coords_deltas;
int *coords_deltas;
/* Text before / after the main buffer contents.
* Always in Unicode, and ordered outward.
* Index 0 is for "pre-context", 1 for "post-context". */
@@ -248,6 +255,19 @@ struct hb_buffer_t
hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
if (have_positions)
hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
if (coords_deltas && num_coords_deltas)
{
/* Reverse each glyph's delta block, which is num_coords_deltas ints each. */
int *base = coords_deltas + start * num_coords_deltas;
unsigned count = end - start;
for (unsigned i = 0; i < count / 2; i++)
{
int *a = base + i * num_coords_deltas;
int *b = base + (count - 1 - i) * num_coords_deltas;
for (unsigned j = 0; j < num_coords_deltas; j++)
hb_swap (a[j], b[j]);
}
}
}
void reverse () { reverse_range (0, len); }
@@ -711,6 +731,62 @@ struct hb_buffer_t
for (unsigned int i = 0; i < len; i++)
info[i].mask = (info[i].mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
}
HB_NODISCARD bool setup_variation_deltas (unsigned int num_coords)
{
if (num_coords_deltas == num_coords)
return true;
if (num_coords == 0)
{
num_coords_deltas = 0;
hb_free (coords_deltas);
coords_deltas = nullptr;
return true;
}
/* Ensure info/pos arrays are allocated first. */
if (unlikely (!ensure (len ? len : 1)))
return false;
/* Allocate or reallocate the deltas array. */
unsigned int coords_bytes = allocated * num_coords * sizeof (int);
int *new_deltas = (int *) hb_realloc (coords_deltas, coords_bytes);
if (unlikely (!new_deltas))
{
successful = false;
return false;
}
/* Zero out existing entries if we had fewer axes before. */
if (num_coords > num_coords_deltas && allocated > 0 && num_coords_deltas > 0)
{
/* We grew in axis count; zero the new axis columns for all rows. */
for (unsigned int i = 0; i < allocated; i++)
hb_memset (new_deltas + i * num_coords + num_coords_deltas, 0,
(num_coords - num_coords_deltas) * sizeof (int));
}
coords_deltas = new_deltas;
num_coords_deltas = num_coords;
return true;
}
HB_ALWAYS_INLINE
int get_variation_delta (unsigned int glyph_idx, unsigned int axis) const
{
if (unlikely (!coords_deltas || axis >= num_coords_deltas || glyph_idx >= len))
return 0;
return coords_deltas[glyph_idx * num_coords_deltas + axis];
}
HB_ALWAYS_INLINE
void set_variation_delta (unsigned int glyph_idx, unsigned int axis, int delta)
{
if (unlikely (!coords_deltas || axis >= num_coords_deltas || glyph_idx >= len))
return;
coords_deltas[glyph_idx * num_coords_deltas + axis] = delta;
}
};
DECLARE_NULL_INSTANCE (hb_buffer_t);