[iup] Use a bit-page instead of bit-set

Reject large contours instead.
This commit is contained in:
Behdad Esfahbod committed 2025-09-16 23:51:19 -04:00
1 parent d4cc4823fa
commit c03e1e0a7b
3 files changed
+53 -18

No files matched your search

+33
View File
@@ -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;
+20 -14
View File
@@ -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;
}
}
-4
View File
@@ -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