From 35212ba3ea3056f104722410647cf43c02f41ff3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 14 Sep 2025 17:46:13 -0600 Subject: [PATCH 1/2] [gvar] Change a storage type No functional change. --- src/hb-open-type.hh | 1 + src/hb-ot-var-common.hh | 20 ++++++++------------ src/hb-ot-var-gvar-table.hh | 26 +++++++++++++------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 358ba3e7b..85c5ac8c7 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -82,6 +82,7 @@ struct NumType NumType operator ++ (int) { NumType c (*this); ++*this; return c; } NumType operator -- (int) { NumType c (*this); --*this; return c; } + unsigned hash () const { return hb_array ((const char *) &v, sizeof (v)).hash (); } HB_INTERNAL static int cmp (const NumType *a, const NumType *b) { return b->cmp (*a); } HB_INTERNAL static int cmp (const void *a, const void *b) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 897dcc09a..1a093a582 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -242,7 +242,7 @@ struct tuple_delta_t hb_vector_t compiled_tuple_header; hb_vector_t compiled_deltas; - hb_vector_t compiled_peak_coords; + hb_vector_t compiled_peak_coords; hb_vector_t compiled_interm_coords; tuple_delta_t () = default; @@ -374,7 +374,7 @@ struct tuple_delta_t const hb_map_t& axes_old_index_tag_map) { unsigned cur_axis_count = axes_index_map.get_population (); - if (unlikely (!compiled_peak_coords.resize (cur_axis_count * F2DOT14::static_size))) + if (unlikely (!compiled_peak_coords.resize (cur_axis_count))) return false; if (unlikely (!compiled_interm_coords.resize (2 * cur_axis_count))) return false; @@ -391,20 +391,16 @@ struct tuple_delta_t hb_tag_t axis_tag = axes_old_index_tag_map.get (i); Triple *coords = nullptr; - F2DOT14 peak_coord; if (axis_tuples.has (axis_tag, &coords)) { float min_val = coords->minimum; float val = coords->middle; float max_val = coords->maximum; - peak_coord.set_float (val); - int16_t peak_val = peak_coord.to_int (); - compiled_peak_coords.arrayZ[2 * j] = static_cast (peak_val >> 8); - compiled_peak_coords.arrayZ[2 * j + 1] = static_cast (peak_val & 0xFF); + compiled_peak_coords.arrayZ[j].set_float (val); - start_coords[j].set_float (min_val); - end_coords[j].set_float (max_val); + start_coords.arrayZ[j].set_float (min_val); + end_coords.arrayZ[j].set_float (max_val); if (min_val != hb_min (val, 0.f) || max_val != hb_max (val, 0.f)) interms_needed = true; } @@ -424,7 +420,7 @@ 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*, unsigned>* shared_tuples_idx_map) + const hb_hashmap_t*, unsigned>* shared_tuples_idx_map) { /* compiled_deltas could be empty after iup delta optimization, we can skip * compiling this tuple and return true */ @@ -479,7 +475,7 @@ struct tuple_delta_t { hb_memcpy (&peak_coords[0], &compiled_peak_coords[0], compiled_peak_coords.length * sizeof (compiled_peak_coords[0])); flag |= TupleVariationHeader::TuppleIndex::EmbeddedPeakTuple; - return compiled_peak_coords.length / F2DOT14::static_size; + return compiled_peak_coords.length; } /* if no need to encode intermediate coords, then just return p */ @@ -1223,7 +1219,7 @@ struct TupleVariationData const hb_map_t& axes_old_index_tag_map, bool use_shared_points, bool is_gvar = false, - const hb_hashmap_t*, unsigned>* shared_tuples_idx_map = nullptr) + const hb_hashmap_t*, unsigned>* shared_tuples_idx_map = nullptr) { // return true for empty glyph if (!tuple_vars) diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index 63682216b..85e407025 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -66,12 +66,12 @@ struct glyph_variations_t hb_vector_t glyph_variations; - hb_vector_t compiled_shared_tuples; + hb_vector_t compiled_shared_tuples; private: unsigned shared_tuples_count = 0; /* shared coords-> index map after instantiation */ - hb_hashmap_t*, unsigned> shared_tuples_idx_map; + hb_hashmap_t*, unsigned> shared_tuples_idx_map; public: unsigned compiled_shared_tuples_count () const @@ -172,7 +172,7 @@ struct glyph_variations_t { /* key is pointer to compiled_peak_coords inside each tuple, hashing * function will always deref pointers first */ - hb_hashmap_t*, unsigned> coords_count_map; + hb_hashmap_t*, unsigned> coords_count_map; /* count the num of shared coords */ for (tuple_variations_t& vars: glyph_variations) @@ -193,7 +193,7 @@ struct glyph_variations_t return false; /* add only those coords that are used more than once into the vector and sort */ - hb_vector_t*> shared_coords; + hb_vector_t*> shared_coords; if (unlikely (!shared_coords.alloc (coords_count_map.get_population ()))) return false; @@ -207,7 +207,7 @@ struct glyph_variations_t if (!shared_coords) return true; /* sorting based on the coords frequency first (high to low), then compare * the coords bytes */ - hb_qsort (shared_coords.arrayZ, shared_coords.length, sizeof (hb_vector_t*), _cmp_coords, (void *) (&coords_count_map)); + hb_qsort (shared_coords.arrayZ, shared_coords.length, sizeof (hb_vector_t*), _cmp_coords, (void *) (&coords_count_map)); /* build shared_coords->idx map and shared tuples byte array */ @@ -220,7 +220,7 @@ struct glyph_variations_t { shared_tuples_idx_map.set (shared_coords[i], i); /* add a concat() in hb_vector_t? */ - for (char c : shared_coords[i]->iter ()) + for (auto c : shared_coords[i]->iter ()) compiled_shared_tuples.push (c); } @@ -229,13 +229,13 @@ struct glyph_variations_t static int _cmp_coords (const void *pa, const void *pb, void *arg) { - const hb_hashmap_t*, unsigned>* coords_count_map = - reinterpret_cast*, unsigned>*> (arg); + const hb_hashmap_t*, unsigned>* coords_count_map = + reinterpret_cast*, unsigned>*> (arg); - /* shared_coords is hb_vector_t*> so casting pa/pb + /* shared_coords is hb_vector_t*> so casting pa/pb * to be a pointer to a pointer */ - const hb_vector_t** a = reinterpret_cast**> (const_cast(pa)); - const hb_vector_t** b = reinterpret_cast**> (const_cast(pb)); + const hb_vector_t** a = reinterpret_cast**> (const_cast(pa)); + const hb_vector_t** b = reinterpret_cast**> (const_cast(pb)); bool has_a = coords_count_map->has (*a); bool has_b = coords_count_map->has (*b); @@ -402,9 +402,9 @@ struct gvar_GVAR out->sharedTuples = 0; else { - hb_array_t shared_tuples = glyph_vars.compiled_shared_tuples.as_array ().copy (c); + hb_array_t shared_tuples = glyph_vars.compiled_shared_tuples.as_array ().copy (c); if (!shared_tuples.arrayZ) return_trace (false); - out->sharedTuples = shared_tuples.arrayZ - (char *) out; + out->sharedTuples = (const char *) shared_tuples.arrayZ - (char *) out; } char *glyph_var_data = c->start_embed (); From 4d5901993535852f24f9204d80c8fcad2d81fbd3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 14 Sep 2025 18:04:51 -0600 Subject: [PATCH 2/2] [instancer/tuplevar] Allocate intermediates array on demand --- src/hb-ot-var-common.hh | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 1a093a582..b39be6593 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -376,11 +376,8 @@ struct tuple_delta_t unsigned cur_axis_count = axes_index_map.get_population (); if (unlikely (!compiled_peak_coords.resize (cur_axis_count))) return false; - if (unlikely (!compiled_interm_coords.resize (2 * cur_axis_count))) - return false; - auto start_coords = compiled_interm_coords.as_array ().sub_array (0, cur_axis_count); - auto end_coords = compiled_interm_coords.as_array (). sub_array (cur_axis_count); - bool interms_needed = false; + + hb_array_t start_coords, end_coords; unsigned orig_axis_count = axes_old_index_tag_map.get_population (); unsigned j = 0; @@ -399,18 +396,36 @@ struct tuple_delta_t compiled_peak_coords.arrayZ[j].set_float (val); - start_coords.arrayZ[j].set_float (min_val); - end_coords.arrayZ[j].set_float (max_val); if (min_val != hb_min (val, 0.f) || max_val != hb_max (val, 0.f)) - interms_needed = true; + { + if (!compiled_interm_coords) + { + if (unlikely (!compiled_interm_coords.resize (2 * cur_axis_count))) + return false; + start_coords = compiled_interm_coords.as_array ().sub_array (0, cur_axis_count); + end_coords = compiled_interm_coords.as_array ().sub_array (cur_axis_count); + + for (unsigned k = 0; k < j; k++) + { + signed peak = compiled_peak_coords.arrayZ[k].to_int (); + if (!peak) continue; + start_coords.arrayZ[k].set_int (hb_min (peak, 0)); + end_coords.arrayZ[k].set_int (hb_max (peak, 0)); + } + } + + } + + if (compiled_interm_coords) + { + start_coords.arrayZ[j].set_float (min_val); + end_coords.arrayZ[j].set_float (max_val); + } } j++; } - if (!interms_needed) - compiled_interm_coords.clear (); - return !compiled_peak_coords.in_error () && !compiled_interm_coords.in_error (); }