mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 16:01:07 +01:00
Merge pull request #5569 from harfbuzz/instancer-opt-more
Instancer opt more
This commit is contained in:
6 files changed
+83
-42
No files matched your search
@@ -142,6 +142,7 @@ struct hb_bit_page_t
|
||||
|
||||
bool operator [] (hb_codepoint_t g) const { return get (g); }
|
||||
bool operator () (hb_codepoint_t g) const { return get (g); }
|
||||
bool has (hb_codepoint_t g) const { return get (g); }
|
||||
|
||||
void add_range (hb_codepoint_t a, hb_codepoint_t b)
|
||||
{
|
||||
@@ -346,6 +347,38 @@ struct hb_bit_page_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Iterator implementation.
|
||||
*/
|
||||
struct iter_t : hb_iter_with_fallback_t<iter_t, hb_codepoint_t>
|
||||
{
|
||||
static constexpr bool is_sorted_iterator = true;
|
||||
iter_t (const hb_bit_page_t &s_ = Null (hb_bit_page_t), bool init = true) : s (&s_), v (INVALID)
|
||||
{
|
||||
if (init)
|
||||
v = s->get_min ();
|
||||
}
|
||||
|
||||
typedef hb_codepoint_t __item_t__;
|
||||
hb_codepoint_t __item__ () const { return v; }
|
||||
bool __more__ () const { return v != INVALID; }
|
||||
void __next__ () {
|
||||
s->next (&v); if (l) l--;
|
||||
}
|
||||
void __prev__ () { s->previous (&v); }
|
||||
unsigned __len__ () const { return l; }
|
||||
iter_t end () const { return iter_t (*s, false); }
|
||||
bool operator != (const iter_t& o) const
|
||||
{ return s != o.s || v != o.v; }
|
||||
|
||||
protected:
|
||||
const hb_bit_page_t *s;
|
||||
hb_codepoint_t v;
|
||||
unsigned l;
|
||||
};
|
||||
iter_t iter () const { return iter_t (*this); }
|
||||
operator iter_t () const { return iter (); }
|
||||
|
||||
static constexpr hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
|
||||
|
||||
typedef unsigned long long elt_t;
|
||||
|
||||
+24
-14
@@ -475,7 +475,8 @@ struct tuple_delta_t
|
||||
bool compile_tuple_var_header (const hb_map_t& axes_index_map,
|
||||
unsigned points_data_length,
|
||||
const hb_map_t& axes_old_index_tag_map,
|
||||
const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map)
|
||||
const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map,
|
||||
hb_alloc_pool_t *pool = nullptr)
|
||||
{
|
||||
/* compiled_deltas could be empty after iup delta optimization, we can skip
|
||||
* compiling this tuple and return true */
|
||||
@@ -484,7 +485,7 @@ struct tuple_delta_t
|
||||
unsigned cur_axis_count = axes_index_map.get_population ();
|
||||
/* allocate enough memory: 1 peak + 2 intermediate coords + fixed header size */
|
||||
unsigned alloc_len = 3 * cur_axis_count * (F2DOT14::static_size) + 4;
|
||||
if (unlikely (!compiled_tuple_header.resize_dirty (alloc_len))) return false;
|
||||
if (unlikely (!compiled_tuple_header.allocate_from_pool (pool, alloc_len, false))) return false;
|
||||
|
||||
unsigned flag = 0;
|
||||
/* skip the first 4 header bytes: variationDataSize+tupleIndex */
|
||||
@@ -522,7 +523,8 @@ struct tuple_delta_t
|
||||
o->tupleIndex = flag;
|
||||
|
||||
unsigned total_header_len = 4 + (peak_count + interim_count) * (F2DOT14::static_size);
|
||||
return compiled_tuple_header.resize (total_header_len);
|
||||
compiled_tuple_header.shrink (total_header_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned encode_peak_coords (hb_array_t<F2DOT14> peak_coords,
|
||||
@@ -545,14 +547,16 @@ struct tuple_delta_t
|
||||
return compiled_interm_coords.length;
|
||||
}
|
||||
|
||||
bool compile_deltas (hb_vector_t<int> &rounded_deltas_scratch)
|
||||
{ return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas, rounded_deltas_scratch); }
|
||||
bool compile_deltas (hb_vector_t<int> &rounded_deltas_scratch,
|
||||
hb_alloc_pool_t *pool = nullptr)
|
||||
{ return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas, rounded_deltas_scratch, pool); }
|
||||
|
||||
static bool compile_deltas (hb_array_t<const bool> point_indices,
|
||||
hb_array_t<const float> x_deltas,
|
||||
hb_array_t<const float> y_deltas,
|
||||
hb_vector_t<unsigned char> &compiled_deltas, /* OUT */
|
||||
hb_vector_t<int> &rounded_deltas /* scratch */)
|
||||
hb_vector_t<int> &rounded_deltas, /* scratch */
|
||||
hb_alloc_pool_t *pool = nullptr)
|
||||
{
|
||||
if (unlikely (!rounded_deltas.resize_dirty (point_indices.length)))
|
||||
return false;
|
||||
@@ -566,12 +570,16 @@ struct tuple_delta_t
|
||||
rounded_deltas.resize (j);
|
||||
|
||||
if (!rounded_deltas) return true;
|
||||
/* allocate enough memories 5 * num_deltas */
|
||||
unsigned alloc_len = 5 * rounded_deltas.length;
|
||||
/* Allocate enough memory: this is the correct bound:
|
||||
* Worst case scenario is that each delta has to be encoded in 4 bytes, and there
|
||||
* are runs of 64 items each. Any delta encoded in less than 4 bytes (2, 1, or 0)
|
||||
* is still smaller than the 4-byte encoding even with their control byte.
|
||||
* The initial 2 is to handle length==0, for both x and y deltas. */
|
||||
unsigned alloc_len = 2 + 4 * rounded_deltas.length + (rounded_deltas.length + 63) / 64;
|
||||
if (y_deltas)
|
||||
alloc_len *= 2;
|
||||
|
||||
if (unlikely (!compiled_deltas.resize_dirty (alloc_len))) return false;
|
||||
if (unlikely (!compiled_deltas.allocate_from_pool (pool, alloc_len, false))) return false;
|
||||
|
||||
unsigned encoded_len = compile_deltas (compiled_deltas, rounded_deltas);
|
||||
|
||||
@@ -592,13 +600,13 @@ struct tuple_delta_t
|
||||
if (j != rounded_deltas.length) return false;
|
||||
encoded_len += compile_deltas (compiled_deltas.as_array ().sub_array (encoded_len), rounded_deltas);
|
||||
}
|
||||
return compiled_deltas.resize (encoded_len);
|
||||
compiled_deltas.shrink (encoded_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
static unsigned compile_deltas (hb_array_t<unsigned char> encoded_bytes,
|
||||
hb_array_t<const int> deltas)
|
||||
{
|
||||
assert (encoded_bytes.length >= 5 * deltas.length);
|
||||
return TupleValues::compile_unsafe (deltas, encoded_bytes);
|
||||
}
|
||||
|
||||
@@ -1276,7 +1284,8 @@ struct TupleVariationData
|
||||
const hb_map_t& axes_old_index_tag_map,
|
||||
bool use_shared_points,
|
||||
bool is_gvar = false,
|
||||
const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map = nullptr)
|
||||
const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map = nullptr,
|
||||
hb_alloc_pool_t *pool = nullptr)
|
||||
{
|
||||
// return true for empty glyph
|
||||
if (!tuple_vars)
|
||||
@@ -1310,12 +1319,13 @@ struct TupleVariationData
|
||||
* this tuple */
|
||||
if (!points_data->length)
|
||||
continue;
|
||||
if (!tuple.compile_deltas (rounded_deltas_scratch))
|
||||
if (!tuple.compile_deltas (rounded_deltas_scratch, pool))
|
||||
return false;
|
||||
|
||||
unsigned points_data_length = (points_data != shared_points_bytes) ? points_data->length : 0;
|
||||
if (!tuple.compile_tuple_var_header (axes_index_map, points_data_length, axes_old_index_tag_map,
|
||||
shared_tuples_idx_map))
|
||||
shared_tuples_idx_map,
|
||||
pool))
|
||||
return false;
|
||||
compiled_byte_size += tuple.compiled_tuple_header.length + points_data_length + tuple.compiled_deltas.length;
|
||||
}
|
||||
|
||||
@@ -165,7 +165,8 @@ struct glyph_variations_t
|
||||
if (!vars.compile_bytes (axes_index_map, axes_old_index_tag_map,
|
||||
true, /* use shared points*/
|
||||
true,
|
||||
&shared_tuples_idx_map))
|
||||
&shared_tuples_idx_map,
|
||||
&pool))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
|
||||
#include "hb-subset-instancer-iup.hh"
|
||||
|
||||
#include "hb-bit-page.hh"
|
||||
|
||||
using hb_iup_set_t = hb_bit_page_t;
|
||||
|
||||
/* This file is a straight port of the following:
|
||||
*
|
||||
* https://github.com/fonttools/fonttools/blob/main/Lib/fontTools/varLib/iup.py
|
||||
@@ -37,7 +41,7 @@ constexpr static unsigned MAX_LOOKBACK = 8;
|
||||
static void _iup_contour_bound_forced_set (const hb_array_t<const contour_point_t> contour_points,
|
||||
const hb_array_t<const int> x_deltas,
|
||||
const hb_array_t<const int> y_deltas,
|
||||
hb_bit_set_t& forced_set, /* OUT */
|
||||
hb_iup_set_t& forced_set, /* OUT */
|
||||
double tolerance = 0.0)
|
||||
{
|
||||
unsigned len = contour_points.length;
|
||||
@@ -150,10 +154,10 @@ static bool rotate_array (const hb_array_t<const T>& org_array,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool rotate_set (const hb_bit_set_t& org_set,
|
||||
static bool rotate_set (const hb_iup_set_t& org_set,
|
||||
int k,
|
||||
unsigned n,
|
||||
hb_bit_set_t& out)
|
||||
hb_iup_set_t& out)
|
||||
{
|
||||
if (!n) return false;
|
||||
k %= n;
|
||||
@@ -162,14 +166,14 @@ static bool rotate_set (const hb_bit_set_t& org_set,
|
||||
|
||||
if (k == 0)
|
||||
{
|
||||
out.set (org_set);
|
||||
out = org_set;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto v : org_set)
|
||||
for (unsigned v : org_set)
|
||||
out.add ((v + k) % n);
|
||||
}
|
||||
return !out.in_error ();
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Given two reference coordinates (start and end of contour_points array),
|
||||
@@ -280,7 +284,7 @@ static bool _can_iup_in_between (const hb_array_t<const contour_point_t> contour
|
||||
static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_points,
|
||||
const hb_vector_t<int>& x_deltas,
|
||||
const hb_vector_t<int>& y_deltas,
|
||||
const hb_bit_set_t& forced_set,
|
||||
const hb_iup_set_t& forced_set,
|
||||
double tolerance,
|
||||
unsigned lookback,
|
||||
hb_vector_t<unsigned>& costs, /* OUT */
|
||||
@@ -347,6 +351,9 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
y_deltas.length != n)
|
||||
return false;
|
||||
|
||||
if (unlikely (n > hb_iup_set_t::PAGE_BITS))
|
||||
return true; // Refuse to work
|
||||
|
||||
bool all_within_tolerance = true;
|
||||
double tolerance_sq = tolerance * tolerance;
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
@@ -389,7 +396,7 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
}
|
||||
|
||||
/* else, solve the general problem using Dynamic Programming */
|
||||
hb_bit_set_t &forced_set = scratch.forced_set.reset ();
|
||||
hb_iup_set_t forced_set;
|
||||
_iup_contour_bound_forced_set (contour_points, x_deltas, y_deltas, forced_set, tolerance);
|
||||
|
||||
hb_vector_t<unsigned> &costs = scratch.costs.reset ();
|
||||
@@ -405,7 +412,7 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
hb_vector_t<int> &rot_y_deltas = scratch.rot_y_deltas.reset ();
|
||||
contour_point_vector_t &rot_points = scratch.rot_points;
|
||||
rot_points.reset ();
|
||||
hb_bit_set_t &rot_forced_set = scratch.rot_forced_set.reset ();
|
||||
hb_iup_set_t rot_forced_set;
|
||||
if (!rotate_array (contour_points, k, rot_points) ||
|
||||
!rotate_array (x_deltas, k, rot_x_deltas) ||
|
||||
!rotate_array (y_deltas, k, rot_y_deltas) ||
|
||||
@@ -418,7 +425,7 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
scratch.interp_x_deltas, scratch.interp_y_deltas))
|
||||
return false;
|
||||
|
||||
hb_bit_set_t &solution = scratch.solution.reset ();
|
||||
hb_iup_set_t solution;
|
||||
int index = n - 1;
|
||||
while (index != -1)
|
||||
{
|
||||
@@ -471,11 +478,10 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
|
||||
unsigned best_cost = n + 1;
|
||||
int len = costs.length;
|
||||
hb_bit_set_t &solution = scratch.solution.reset ();
|
||||
hb_bit_set_t &best_sol = scratch.best_sol.reset ();
|
||||
hb_iup_set_t best_sol;
|
||||
for (int start = n - 1; start < len; start++)
|
||||
{
|
||||
solution.reset ();
|
||||
hb_iup_set_t solution;
|
||||
int i = start;
|
||||
int lookback = start - (int) n;
|
||||
while (i > lookback)
|
||||
@@ -489,7 +495,7 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
|
||||
unsigned cost = costs.arrayZ[start] - cost_i;
|
||||
if (cost <= best_cost)
|
||||
{
|
||||
best_sol.set (solution);
|
||||
best_sol = solution;
|
||||
best_cost = cost;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,10 +38,6 @@ struct iup_scratch_t
|
||||
hb_vector_t<int> rot_x_deltas;
|
||||
hb_vector_t<int> rot_y_deltas;
|
||||
contour_point_vector_t rot_points;
|
||||
hb_bit_set_t forced_set;
|
||||
hb_bit_set_t rot_forced_set;
|
||||
hb_bit_set_t solution;
|
||||
hb_bit_set_t best_sol;
|
||||
};
|
||||
|
||||
/* given contour points and deltas, optimize a set of referenced points within error
|
||||
|
||||
+4
-9
@@ -107,9 +107,6 @@ struct hb_vector_t
|
||||
void
|
||||
set_storage (Type *array, unsigned n)
|
||||
{
|
||||
if (unlikely (in_error ()))
|
||||
return;
|
||||
|
||||
assert (allocated == 0);
|
||||
assert (length == 0);
|
||||
|
||||
@@ -548,21 +545,19 @@ struct hb_vector_t
|
||||
HB_ALWAYS_INLINE_VECTOR_ALLOCS
|
||||
bool allocate_from_pool (allocator_t *allocator, unsigned size, unsigned int initialize = true)
|
||||
{
|
||||
assert (!length && !allocated);
|
||||
if (allocator)
|
||||
{
|
||||
assert (!length && !allocated);
|
||||
arrayZ = (Type *) allocator->alloc (size * sizeof (Type));
|
||||
if (unlikely (!arrayZ))
|
||||
{
|
||||
set_error ();
|
||||
return false;
|
||||
}
|
||||
set_storage (arrayZ, size);
|
||||
if (initialize)
|
||||
{
|
||||
length = 0;
|
||||
grow_vector (size, hb_prioritize);
|
||||
}
|
||||
else
|
||||
length = size;
|
||||
return true;
|
||||
}
|
||||
return resize_full ((int) size, initialize, true);
|
||||
@@ -654,7 +649,7 @@ struct hb_vector_t
|
||||
|
||||
shrink_vector (size);
|
||||
|
||||
if (shrink_memory)
|
||||
if (is_owned () && shrink_memory)
|
||||
alloc_exact (size); /* To force shrinking memory if needed. */
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user