From e41b688b50506844739794d715596f24c6ae9545 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Mon, 12 Jun 2023 13:25:19 -0700 Subject: [PATCH 01/10] [instancer] fix bug in tuple varstore decompiling when points count is 0, it means deltas will apply to all points --- src/hb-ot-var-common.hh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 18df261cf..1bc3184e4 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -591,7 +591,8 @@ struct TupleVariationData { fini (); return false; } const hb_vector_t &indices = has_private_points ? private_indices : shared_indices; - unsigned num_deltas = indices.length; + bool apply_to_all = (indices.length == 0); + unsigned num_deltas = apply_to_all ? point_count : indices.length; hb_vector_t deltas_x; @@ -618,7 +619,8 @@ struct TupleVariationData for (unsigned i = 0; i < num_deltas; i++) { - unsigned idx = indices[i]; + unsigned idx = apply_to_all ? i : indices[i]; + if (idx >= point_count) continue; var.indices[idx] = true; var.deltas_x[idx] = static_cast (deltas_x[i]); if (is_gvar) From 065e0af5925f260a5fcd40ec0a502ec8aa3ada85 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 10:58:24 -0700 Subject: [PATCH 02/10] [instancer] add compile_point_set() for tuple_variations_t Also add a byte_data_t struct to store compiled bytes --- src/hb-ot-var-common.hh | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 1bc3184e4..0b69370eb 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -416,6 +416,27 @@ struct TupleVariationHeader DEFINE_SIZE_MIN (4); }; +/* not using hb_bytes_t: avoid potential build issues with some compilers */ +struct byte_data_t +{ + const char *p = nullptr; //allocated by hb_calloc () or similar + unsigned length = 0; + + byte_data_t () = default; + byte_data_t (const byte_data_t&) = default; + byte_data_t (const char *p_, unsigned len_) : p (p_), length (len_) {} + ~byte_data_t () = default; + byte_data_t& operator= (const byte_data_t&) = default; + byte_data_t& operator= (byte_data_t&&) = default; + + void fini () { hb_free ((char *) p); p = nullptr; length = 0; } + + bool operator == (const byte_data_t& o) const + { return p == o.p && length == o.length; } + + explicit operator bool () const { return length; } +}; + struct tuple_delta_t { public: @@ -681,6 +702,88 @@ struct TupleVariationData tuple_vars.fini (); tuple_vars = std::move (new_vars); } + + byte_data_t compile_point_set (const hb_vector_t &point_indices) + { + unsigned num_points = 0; + for (bool i : point_indices) + if (i) num_points++; + + unsigned indices_length = point_indices.length; + /* If the points set consists of all points in the glyph, it's encoded with a + * single zero byte */ + if (num_points == indices_length) + { + char *p = (char *) hb_calloc (1, sizeof (char)); + if (unlikely (!p)) return byte_data_t (); + + return byte_data_t (p, 1); + } + + /* allocate enough memories: 2 bytes for count + 3 bytes for each point */ + unsigned num_bytes = 2 + 3 *num_points; + char *p = (char *) hb_calloc (num_bytes, sizeof (char)); + if (unlikely (!p)) return byte_data_t (); + + unsigned pos = 0; + /* binary data starts with the total number of reference points */ + if (num_points < 0x80) + p[pos++] = num_points; + else + { + p[pos++] = ((num_points >> 8) | 0x80); + p[pos++] = num_points & 0xFF; + } + + unsigned max_run_length = 0x7F; + unsigned i = 0; + unsigned last_value = 0; + unsigned num_encoded = 0; + while (i < indices_length && num_encoded < num_points) + { + unsigned run_length = 0; + unsigned header_pos = pos; + p[pos++] = 0; + + bool use_byte_encoding = false; + while (i < indices_length && num_encoded < num_points && + run_length <= max_run_length) + { + // find out next referenced point index + while (i < indices_length && !point_indices[i]) + i++; + + if (i >= indices_length) break; + + unsigned cur_value = i; + unsigned delta = cur_value - last_value; + + if (!use_byte_encoding) + use_byte_encoding = (delta >= 0 && delta <= 0xFF); + + if (use_byte_encoding && (delta <0 || delta > 0xFF)) + break; + + if (use_byte_encoding) + p[pos++] = delta; + else + { + p[pos++] = delta >> 8; + p[pos++] = delta & 0xFF; + } + i++; + last_value = cur_value; + run_length++; + num_encoded++; + } + + if (use_byte_encoding) + p[header_pos] = run_length - 1; + else + p[header_pos] = (run_length - 1) | 0x80; + } + return byte_data_t (p, pos); + } }; struct tuple_iterator_t From 19e5033b9c8d24ee6ce2df124576d5d724d80fc2 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:02:41 -0700 Subject: [PATCH 03/10] [instancer] add compile_all_point_sets() and find_shared_points () compiled bytes for points set are stored in a hashmap --- src/hb-ot-var-common.hh | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 0b69370eb..924125f14 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -583,6 +583,13 @@ struct TupleVariationData { hb_vector_t tuple_vars; + private: + /* referenced point set->compiled point data map */ + hb_hashmap_t*, byte_data_t> point_data_map; + /* referenced point set-> count map, used in finding shared points */ + hb_hashmap_t*, unsigned> point_set_count_map; + + public: void fini () { tuple_vars.fini (); } bool create_from_tuple_var_data (tuple_iterator_t iterator, unsigned tuple_var_count, @@ -784,6 +791,60 @@ struct TupleVariationData } return byte_data_t (p, pos); } + + /* compile all point set and store byte data in a point_set->byte_data_t hashmap, + * also update point_set->count map, which will be used in finding shared + * point set*/ + bool compile_all_point_sets () + { + for (const auto& tuple: tuple_vars) + { + const hb_vector_t* points_set = &(tuple.indices); + if (point_data_map.has (points_set)) + { + unsigned *count; + if (unlikely (!point_set_count_map.has (points_set, &count) || + !point_set_count_map.set (points_set, (*count) + 1))) + return false; + continue; + } + + byte_data_t compiled_data = compile_point_set (*points_set); + if (unlikely (compiled_data == byte_data_t ())) + return false; + + if (!point_data_map.set (points_set, compiled_data) || + !point_set_count_map.set (points_set, 1)) + return false; + } + return true; + } + + /* find shared points set which saves most bytes */ + byte_data_t find_shared_points () + { + unsigned max_saved_bytes = 0; + byte_data_t res{}; + + for (const auto& _ : point_data_map.iter ()) + { + const hb_vector_t* points_set = _.first; + unsigned data_length = _.second.length; + unsigned *count; + if (unlikely (!point_set_count_map.has (points_set, &count) || + *count <= 1)) + return byte_data_t (); + + unsigned saved_bytes = data_length * ((*count) -1); + if (saved_bytes > max_saved_bytes) + { + max_saved_bytes = saved_bytes; + res = _.second; + } + } + return res; + } + }; struct tuple_iterator_t From a00ad83a3bc0e86a16a791c0085403dd9a42b5a4 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:17:11 -0700 Subject: [PATCH 04/10] [instancer] add encode_delta_run() --- src/hb-ot-var-common.hh | 165 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 158 insertions(+), 7 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 924125f14..75eff9670 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -437,6 +437,13 @@ struct byte_data_t explicit operator bool () const { return length; } }; +enum packed_delta_flag_t +{ + DELTAS_ARE_ZERO = 0x80, + DELTAS_ARE_WORDS = 0x40, + DELTA_RUN_COUNT_MASK = 0x3F +}; + struct tuple_delta_t { public: @@ -549,6 +556,157 @@ struct tuple_delta_t return out; } + + char* encode_delta_run (char* p, char *end, unsigned& i, + const hb_vector_t& deltas) const + { + if (p >= end) return nullptr; + unsigned num_deltas = deltas.length; + while (i < num_deltas) + { + int val = deltas[i]; + if (val == 0) + p = encode_delta_run_as_zeroes (p, end, i, deltas); + else if (val >= -128 && val <= 127) + p = encode_delta_run_as_bytes (p, end, i, deltas); + else + p = encode_delta_run_as_words (p, end, i, deltas); + + if (!p || p > end) + return nullptr; + } + return p; + } + + char* encode_delta_run_as_zeroes (char* p, char *end, unsigned& i, + const hb_vector_t& deltas) const + { + if (p >= end) return nullptr; + unsigned num_deltas = deltas.length; + unsigned run_length = 0; + while (i < num_deltas && deltas[i] == 0) + { + i++; + run_length++; + } + + while (run_length >= 64) + { + if (p >= end) return nullptr; + *p++ = (DELTAS_ARE_ZERO | 63); + run_length -= 64; + } + + if (run_length) + { + if (p >= end) return nullptr; + *p++ = (DELTAS_ARE_ZERO | (run_length - 1)); + } + return p; + } + + char* encode_delta_run_as_bytes (char *p, char *end, unsigned &i, + const hb_vector_t& deltas) const + { + if (p >= end) return nullptr; + unsigned start = i; + unsigned num_deltas = deltas.length; + while (i < num_deltas) + { + int val = deltas[i]; + if (val > 127 || val < -128) + break; + + /* from fonttools: if there're 2 or more zeros in a sequence, + * it is better to start a new run to save bytes. */ + if (val == 0 && i + 1 < num_deltas && deltas[i+1] == 0) + break; + + i++; + } + unsigned run_length = i - start; + + HBINT8 *out = reinterpret_cast (p); + while (run_length >= 64) + { + if ((char *)out >= end) return nullptr; + *out++ = 63; + + for (unsigned j = 0; j < 64; j++) + { + if ((char *)out >= end) return nullptr; + *out++ = deltas[start + j]; + } + + start += 64; + run_length -= 64; + } + + if (run_length) + { + if ((char *)out >= end) return nullptr; + *out++ = run_length - 1; + while (start < i) + { + if ((char *)out >= end) return nullptr; + *out++ = deltas[start++]; + } + } + + return reinterpret_cast (out); + } + + char* encode_delta_run_as_words (char *p, char *end, unsigned &i, + const hb_vector_t& deltas) const + { + unsigned start = i; + unsigned num_deltas = deltas.length; + while (i < num_deltas) + { + int val = deltas[i]; + + /* start a new run for a single zero value*/ + if (val == 0) break; + + /* from fonttools: continue word-encoded run if there's only one + * single value in the range [-128, 127] because it is more compact. + * Only start a new run when there're 2 continuous such values. */ + if (val >= -128 && val <= 127 && + i + 1 < num_deltas && + deltas[i+1] >= -128 && deltas[i+1] <= 127) + break; + + i++; + } + + unsigned run_length = i - start; + HBINT16 *out = reinterpret_cast (p); + while (run_length >= 64) + { + if ((char *)out >= end) return nullptr; + *out++ = (DELTAS_ARE_WORDS | 63); + for (unsigned j = 0; j < 64; j++) + { + if ((char *)out >= end) return nullptr; + *out++ = deltas[start + j]; + } + + start += 64; + run_length -= 64; + } + + if (run_length) + { + if ((char *)out >= end) return nullptr; + *out++ = (DELTAS_ARE_WORDS | (run_length - 1)); + while (start < i) + { + if ((char *)out >= end) return nullptr; + *out++ = deltas[start++]; + } + } + return reinterpret_cast (out); + } }; struct TupleVariationData @@ -974,13 +1132,6 @@ struct TupleVariationData hb_vector_t &deltas /* IN/OUT */, const HBUINT8 *end) { - enum packed_delta_flag_t - { - DELTAS_ARE_ZERO = 0x80, - DELTAS_ARE_WORDS = 0x40, - DELTA_RUN_COUNT_MASK = 0x3F - }; - unsigned i = 0; unsigned count = deltas.length; while (i < count) From 6354b71f81bb85951d84d245eb68ec4e107aea55 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:24:05 -0700 Subject: [PATCH 05/10] [instancer] add compile_deltas() for tuple_delta_t --- src/hb-ot-var-common.hh | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 75eff9670..8d2b006b0 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -456,8 +456,23 @@ struct tuple_delta_t /* empty for cvar tuples */ hb_vector_t deltas_y; + /* compiled data: header and deltas + * compiled point data is saved in a hashmap within tuple_variations_t cause + * some point sets might be reused by different tuple variations */ + byte_data_t compiled_tuple_header; + byte_data_t compiled_deltas; + tuple_delta_t () = default; tuple_delta_t (const tuple_delta_t& o) = default; + ~tuple_delta_t () + { + compiled_deltas.fini (); + compiled_tuple_header.fini (); + deltas_y.fini (); + deltas_x.fini (); + indices.fini (); + axis_tuples.fini (); + } tuple_delta_t (tuple_delta_t&& o) : tuple_delta_t () { @@ -557,6 +572,61 @@ struct tuple_delta_t return out; } + bool compile_deltas () + { + hb_vector_t rounded_deltas; + if (unlikely (!rounded_deltas.alloc (indices.length))) + return false; + + for (unsigned i = 0; i < indices.length; i++) + { + if (!indices[i]) continue; + int rounded_delta = (int) roundf (deltas_x[i]); + rounded_deltas.push (rounded_delta); + } + + if (!rounded_deltas) return false; + /* allocate enough memories 3 * num_deltas */ + unsigned alloc_len = 3 * rounded_deltas.length; + if (deltas_y) + alloc_len *= 2; + + char *p = (char *) hb_malloc (alloc_len); + if (unlikely (!p)) return false; + + unsigned i = 0; + char *end = p + alloc_len; + char *o = encode_delta_run (p, end, i, rounded_deltas); + if (!o || o == p) + { hb_free (p); return false; } + + unsigned encoded_len = o - p; + + if (deltas_y) + { + /* reuse the rounded_deltas vector, check that deltas_y have the same num of deltas as deltas_x */ + unsigned j = 0; + for (unsigned idx = 0; idx < indices.length; idx++) + { + if (!indices[idx]) continue; + int rounded_delta = (int) roundf (deltas_y[idx]); + + if (j >= rounded_deltas.length) + { hb_free (p); return false; } + + rounded_deltas[j++] = rounded_delta; + } + + if (j != rounded_deltas.length) { hb_free (p); return false; } + + char *out = encode_delta_run (o, end, i, rounded_deltas); + if (!out || out == o) { hb_free (p); return false; } + encoded_len = out - p; + } + compiled_deltas = byte_data_t (p, encoded_len); + return true; + } + char* encode_delta_run (char* p, char *end, unsigned& i, const hb_vector_t& deltas) const { From 48c70ce5f667d6b24d7d4a72bc03095760dc3752 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:30:51 -0700 Subject: [PATCH 06/10] [instancer] add compile_tuple_var_header() --- src/hb-ot-var-common.hh | 116 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 8d2b006b0..6e7eb61c6 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -241,6 +241,7 @@ struct VarStoreInstancer /* https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader */ struct TupleVariationHeader { + friend struct tuple_delta_t; unsigned get_size (unsigned axis_count) const { return min_size + get_all_tuples (axis_count).get_size (); } @@ -384,6 +385,7 @@ struct TupleVariationHeader TupleIndexMask = 0x0FFFu }; + TuppleIndex& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; } DEFINE_SIZE_STATIC (2); }; @@ -572,6 +574,120 @@ struct tuple_delta_t return out; } + /* deltas should be compiled already before we compile tuple + * variation header cause we need to fill in the size of the + * serialized data for this tuple variation */ + //TODO(qxliu):add option to use sharedTuples in gvar + 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) + { + if (!compiled_deltas) return false; + + 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; + char *p = (char *) hb_calloc (alloc_len, sizeof (char)); + if (unlikely (!p)) return false; + + /* the first 4 bytes are fixed header bytes*/ + unsigned pos = 4; + unsigned flag = 0; + F2DOT14* end = reinterpret_cast (p + alloc_len); + /* encode peak coords */ + F2DOT14* peak_coords_end = encode_peak_coords(p+pos, end, flag, axes_index_map, axes_old_index_tag_map); + if (!peak_coords_end) + { hb_free (p); return false; } + + F2DOT14* interm_coords_end = encode_interm_coords (peak_coords_end, end, flag, axes_index_map, axes_old_index_tag_map); + if (!interm_coords_end) + { hb_free (p); return false; } + + //TODO(qxliu): add option to use shared_points in gvar + flag |= TupleVariationHeader::TuppleIndex::PrivatePointNumbers; + + unsigned serialized_data_size = points_data_length + compiled_deltas.length; + TupleVariationHeader *o = reinterpret_cast (p); + o->varDataSize = serialized_data_size; + o->tupleIndex = flag; + + unsigned total_header_len = reinterpret_cast (interm_coords_end) - p; + compiled_tuple_header = byte_data_t (p, total_header_len); + return true; + } + + F2DOT14* encode_peak_coords (char* p, F2DOT14* end, + unsigned& flag, + const hb_map_t& axes_index_map, + const hb_map_t& axes_old_index_tag_map) const + { + unsigned orig_axis_count = axes_old_index_tag_map.get_population (); + F2DOT14* out = reinterpret_cast (p); + for (unsigned i = 0; i < orig_axis_count; i++) + { + if (!axes_index_map.has (i)) /* axis pinned */ + continue; + if (out >= end) return nullptr; + hb_tag_t axis_tag = axes_old_index_tag_map.get (i); + Triple *coords; + if (!axis_tuples.has (axis_tag, &coords)) + (*out).set_int (0); + else + (*out).set_float (coords->middle); + out++; + } + flag |= TupleVariationHeader::TuppleIndex::EmbeddedPeakTuple; + return out; + } + + /* if no need to encode intermediate coords, then just return p */ + F2DOT14* encode_interm_coords (F2DOT14 *p, F2DOT14* end, + unsigned& flag, + const hb_map_t& axes_index_map, + const hb_map_t& axes_old_index_tag_map) const + { + unsigned orig_axis_count = axes_old_index_tag_map.get_population (); + unsigned cur_axis_count = axes_index_map.get_population (); + + F2DOT14* out = reinterpret_cast (p); + + F2DOT14* start_coords = out; + F2DOT14* end_coords = start_coords + cur_axis_count; + bool encode_needed = false; + for (unsigned i = 0; i < orig_axis_count; i++) + { + if (!axes_index_map.has (i)) /* axis pinned */ + continue; + hb_tag_t axis_tag = axes_old_index_tag_map.get (i); + Triple *coords; + float min_val = 0.f, val = 0.f, max_val = 0.f; + if (axis_tuples.has (axis_tag, &coords)) + { + min_val = coords->minimum; + val = coords->middle; + max_val = coords->maximum; + } + + if (start_coords >= end || end_coords >= end) + return nullptr; + + (*start_coords).set_float (min_val); + (*end_coords).set_float (max_val); + + start_coords++; + end_coords++; + if (min_val != hb_min (val, 0.f) || max_val != hb_max (val, 0.f)) + encode_needed = true; + } + + if (encode_needed) + { + flag |= TupleVariationHeader::TuppleIndex::IntermediateRegion; + return end_coords; + } + return p; + } + bool compile_deltas () { hb_vector_t rounded_deltas; From 5ef0199da331959e6b27da90d1032f78d3d775ef Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:39:20 -0700 Subject: [PATCH 07/10] [instancer] instantiate() and compile_bytes () for tuple_variations_t Also add testing code --- src/hb-ot-var-common.hh | 28 ++++++++++++++++++++++++++++ src/test-tuple-varstore.cc | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 6e7eb61c6..c4317f224 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1189,6 +1189,34 @@ struct TupleVariationData return res; } + void instantiate (const hb_hashmap_t& normalized_axes_location) + { + change_tuple_variations_axis_limits (&normalized_axes_location); + merge_tuple_variations (); + } + + bool compile_bytes (const hb_map_t& axes_index_map, + const hb_map_t& axes_old_index_tag_map) + { + // compile points set and store data in hashmap + if (!compile_all_point_sets ()) + return false; + // compile delta and tuple var header for each tuple variation + for (auto& tuple: tuple_vars) + { + const hb_vector_t* points_set = &(tuple.indices); + byte_data_t *points_data; + if (unlikely (!point_data_map.has (points_set, &points_data))) + return false; + + if (!tuple.compile_deltas ()) + return false; + + if (!tuple.compile_tuple_var_header (axes_index_map, points_data->length, axes_old_index_tag_map)) + return false; + } + return true; + } }; struct tuple_iterator_t diff --git a/src/test-tuple-varstore.cc b/src/test-tuple-varstore.cc index 85cccbfba..3b49928bc 100644 --- a/src/test-tuple-varstore.cc +++ b/src/test-tuple-varstore.cc @@ -110,6 +110,29 @@ test_decompile_cvar () } } + hb_map_t axes_index_map; + axes_index_map.set (0, 0); + bool res = tuple_variations.compile_bytes (axes_index_map, axis_idx_tag_map); + assert (res); + assert (tuple_variations.tuple_vars[0].compiled_tuple_header.length == 6); + const char tuple_var_header_1[] = "\x0\x51\xa0\x0\xc0\x0"; + for (unsigned i = 0; i < 6; i++) + assert(tuple_variations.tuple_vars[0].compiled_tuple_header.p[i] == tuple_var_header_1[i]); + + assert (tuple_variations.tuple_vars[1].compiled_tuple_header.length == 6); + const char tuple_var_header_2[] = "\x0\x54\xa0\x0\x40\x0"; + for (unsigned i = 0; i < 6; i++) + assert(tuple_variations.tuple_vars[1].compiled_tuple_header.p[i] == tuple_var_header_2[i]); + + assert (tuple_variations.tuple_vars[0].compiled_deltas.length == 37); + assert (tuple_variations.tuple_vars[1].compiled_deltas.length == 40); + const char compiled_deltas_1[] = "\x0d\xff\x00\xfe\x01\x00\xff\x00\xfe\x01\x00\xed\xed\xf3\xf3\x82\x00\xfe\x84\x06\xfe\x00\x01\xf1\xf1\xf6\xf6\x82\x04\x01\xf1\xf1\xf6\xf6\x82\x00\x01"; + for (unsigned i = 0; i < 37; i++) + assert (tuple_variations.tuple_vars[0].compiled_deltas.p[i] == compiled_deltas_1[i]); + + const char compiled_deltas_2[] = "\x0d\x01\x00\x04\xfe\x00\x01\x00\x04\xfe\x00\x44\x44\x30\x30\x82\x00\x04\x81\x09\x01\xff\x01\x05\xff\xfc\x33\x33\x25\x25\x82\x04\xff\x33\x33\x25\x25\x82\x00\xff"; + for (unsigned i = 0; i < 40; i++) + assert (tuple_variations.tuple_vars[1].compiled_deltas.p[i] == compiled_deltas_2[i]); } int From e39e02017e1537cd9f085b1f5999887a309c20c6 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 11:48:53 -0700 Subject: [PATCH 08/10] [instancer] add serialize() method for TupleVariationData --- src/hb-ot-var-common.hh | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index c4317f224..d5d4a8d71 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -437,6 +437,17 @@ struct byte_data_t { return p == o.p && length == o.length; } explicit operator bool () const { return length; } + + byte_data_t* copy (hb_serialize_context_t *c) const + { + if (unlikely (!length || !p)) return nullptr; + + char* ret = c->allocate_size (length); + if (unlikely (!ret)) return nullptr; + hb_memcpy (ret, p, length); + // cast returning pointer, required by serializer copy() + return reinterpret_cast (ret); + } }; enum packed_delta_flag_t @@ -935,6 +946,10 @@ struct TupleVariationData public: void fini () { tuple_vars.fini (); } + + unsigned get_var_count () const + { return tuple_vars.length; } + bool create_from_tuple_var_data (tuple_iterator_t iterator, unsigned tuple_var_count, unsigned point_count, @@ -1217,6 +1232,34 @@ struct TupleVariationData } return true; } + + bool serialize_var_headers (hb_serialize_context_t *c, unsigned& total_header_len) const + { + TRACE_SERIALIZE (this); + for (const auto& tuple: tuple_vars) + { + if (!c->copy (tuple.compiled_tuple_header)) + return_trace (false); + total_header_len += tuple.compiled_tuple_header.length; + } + return_trace (true); + } + + bool serialize_var_data (hb_serialize_context_t *c) const + { + TRACE_SERIALIZE (this); + for (const auto& tuple: tuple_vars) + { + const hb_vector_t* points_set = &(tuple.indices); + byte_data_t *point_data; + if (!point_data_map.has (points_set)) + return_trace (false); + + if (!c->copy (*point_data)) return_trace (false); + if (!c->copy (tuple.compiled_deltas)) return_trace (false); + } + return_trace (true); + } }; struct tuple_iterator_t @@ -1398,11 +1441,35 @@ struct TupleVariationData shared_tuples); } + bool serialize (hb_serialize_context_t *c, + bool is_gvar, + tuple_variations_t& tuple_variations) const + { + TRACE_SERIALIZE (this); + auto *out = c->start_embed (this); + if (unlikely (!c->extend_min (out))) return_trace (false); + + if (!c->check_assign (out->tupleVarCount, tuple_variations.get_var_count (), + HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false); + + unsigned total_header_len = 0; + + if (!tuple_variations.serialize_var_headers (c, total_header_len)) + return_trace (false); + + unsigned data_offset = min_size + total_header_len; + if (!is_gvar) data_offset += 4; + if (!c->check_assign (out->data, data_offset, HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false); + + return tuple_variations.serialize_var_data (c); + } + protected: struct TupleVarCount : HBUINT16 { bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); } unsigned int get_count () const { return (*this) & CountMask; } + TupleVarCount& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; } protected: enum Flags From f55619b8360505ff352c60e728dbf0c2e960e303 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 12:37:05 -0700 Subject: [PATCH 09/10] [instancer] add subset () for cvar --- src/hb-ot-var-cvar-table.hh | 40 ++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-var-cvar-table.hh b/src/hb-ot-var-cvar-table.hh index c85da7adc..5f7721d47 100644 --- a/src/hb-ot-var-cvar-table.hh +++ b/src/hb-ot-var-cvar-table.hh @@ -64,7 +64,7 @@ struct cvar if (!TupleVariationData::get_tuple_iterator (var_data_bytes, axis_count, this, shared_indices, &iterator)) return false; - + return tupleVariationData.decompile_tuple_variations (point_count, is_gvar, iterator, axes_old_index_tag_map, shared_indices, @@ -125,6 +125,44 @@ struct cvar return true; } + + bool serialize (hb_serialize_context_t *c, + TupleVariationData::tuple_variations_t& tuple_variations) const + { + TRACE_SERIALIZE (this); + if (unlikely (!c->embed (version))) return_trace (false); + + return_trace (tupleVariationData.serialize (c, false, tuple_variations)); + } + + bool subset (hb_subset_context_t *c) const + { + TRACE_SUBSET (this); + /* subset() for cvar is called by partial instancing only, we always pass + * through cvar table in other cases */ + if (unlikely (!c->plan->normalized_coords || + c->plan->pinned_at_default || + c->plan->all_axes_pinned)) + return_trace (false); + + OT::TupleVariationData::tuple_variations_t tuple_variations; + unsigned axis_count = c->plan->axes_old_index_tag_map.get_population (); + + const hb_tag_t cvt = HB_TAG('c','v','t',' '); + hb_blob_t *cvt_blob = hb_face_reference_table (c->plan->source, cvt); + unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size; + + if (!decompile_tuple_variations (axis_count, point_count, false, + &(c->plan->axes_old_index_tag_map), + tuple_variations)) + return_trace (false); + + tuple_variations.instantiate (c->plan->axes_location); + if (!tuple_variations.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map)) + return_trace (false); + + return_trace (serialize (c->serializer, tuple_variations)); + } static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan, const TupleVariationData *tuple_var_data, From b10cff9906de237cb7f56abd9e4ba99959966954 Mon Sep 17 00:00:00 2001 From: Qunxin Liu Date: Tue, 20 Jun 2023 13:36:10 -0700 Subject: [PATCH 10/10] [instancer] address review comments manage memory through vector when possible --- src/hb-ot-var-common.hh | 269 ++++++++++++++++++------------------ src/hb-ot-var-cvar-table.hh | 18 ++- src/test-tuple-varstore.cc | 8 +- 3 files changed, 150 insertions(+), 145 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index d5d4a8d71..520f41a8e 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -421,32 +421,25 @@ struct TupleVariationHeader /* not using hb_bytes_t: avoid potential build issues with some compilers */ struct byte_data_t { - const char *p = nullptr; //allocated by hb_calloc () or similar - unsigned length = 0; + hb_bytes_t bytes; byte_data_t () = default; - byte_data_t (const byte_data_t&) = default; - byte_data_t (const char *p_, unsigned len_) : p (p_), length (len_) {} - ~byte_data_t () = default; - byte_data_t& operator= (const byte_data_t&) = default; - byte_data_t& operator= (byte_data_t&&) = default; + byte_data_t (const char *p_, unsigned len_) : bytes (hb_bytes_t (p_, len_)) {} - void fini () { hb_free ((char *) p); p = nullptr; length = 0; } + void fini () { bytes.fini (); } bool operator == (const byte_data_t& o) const - { return p == o.p && length == o.length; } + { return bytes.arrayZ == o.bytes.arrayZ && bytes.length == o.bytes.length; } - explicit operator bool () const { return length; } + explicit operator bool () const { return bytes.length; } - byte_data_t* copy (hb_serialize_context_t *c) const + void copy (hb_serialize_context_t *c) const { - if (unlikely (!length || !p)) return nullptr; + if (unlikely (!bytes.length || !bytes.arrayZ)) return; - char* ret = c->allocate_size (length); - if (unlikely (!ret)) return nullptr; - hb_memcpy (ret, p, length); - // cast returning pointer, required by serializer copy() - return reinterpret_cast (ret); + char* ret = c->allocate_size (bytes.length); + if (unlikely (!ret)) return; + hb_memcpy (ret, bytes.arrayZ, bytes.length); } }; @@ -472,20 +465,11 @@ struct tuple_delta_t /* compiled data: header and deltas * compiled point data is saved in a hashmap within tuple_variations_t cause * some point sets might be reused by different tuple variations */ - byte_data_t compiled_tuple_header; - byte_data_t compiled_deltas; + hb_vector_t compiled_tuple_header; + hb_vector_t compiled_deltas; tuple_delta_t () = default; tuple_delta_t (const tuple_delta_t& o) = default; - ~tuple_delta_t () - { - compiled_deltas.fini (); - compiled_tuple_header.fini (); - deltas_y.fini (); - deltas_x.fini (); - indices.fini (); - axis_tuples.fini (); - } tuple_delta_t (tuple_delta_t&& o) : tuple_delta_t () { @@ -598,61 +582,60 @@ 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; - char *p = (char *) hb_calloc (alloc_len, sizeof (char)); - if (unlikely (!p)) return false; + if (unlikely (!compiled_tuple_header.resize (alloc_len))) return false; - /* the first 4 bytes are fixed header bytes*/ - unsigned pos = 4; unsigned flag = 0; - F2DOT14* end = reinterpret_cast (p + alloc_len); - /* encode peak coords */ - F2DOT14* peak_coords_end = encode_peak_coords(p+pos, end, flag, axes_index_map, axes_old_index_tag_map); - if (!peak_coords_end) - { hb_free (p); return false; } + /* skip the first 4 header bytes: variationDataSize+tupleIndex */ + F2DOT14* p = reinterpret_cast (compiled_tuple_header.begin () + 4); + F2DOT14* end = reinterpret_cast (compiled_tuple_header.end ()); + hb_array_t coords (p, end - p); - F2DOT14* interm_coords_end = encode_interm_coords (peak_coords_end, end, flag, axes_index_map, axes_old_index_tag_map); - if (!interm_coords_end) - { hb_free (p); return false; } + /* encode peak coords */ + unsigned peak_count = encode_peak_coords(coords, flag, axes_index_map, axes_old_index_tag_map); + if (!peak_count) return false; + + /* encode interim coords, it's optional so returned num could be 0 */ + unsigned interim_count = encode_interm_coords (coords.sub_array (peak_count), flag, axes_index_map, axes_old_index_tag_map); //TODO(qxliu): add option to use shared_points in gvar flag |= TupleVariationHeader::TuppleIndex::PrivatePointNumbers; unsigned serialized_data_size = points_data_length + compiled_deltas.length; - TupleVariationHeader *o = reinterpret_cast (p); + TupleVariationHeader *o = reinterpret_cast (compiled_tuple_header.begin ()); o->varDataSize = serialized_data_size; o->tupleIndex = flag; - unsigned total_header_len = reinterpret_cast (interm_coords_end) - p; - compiled_tuple_header = byte_data_t (p, total_header_len); - return true; + unsigned total_header_len = 4 + (peak_count + interim_count) * (F2DOT14::static_size); + return compiled_tuple_header.resize (total_header_len); } - F2DOT14* encode_peak_coords (char* p, F2DOT14* end, + unsigned encode_peak_coords (hb_array_t peak_coords, unsigned& flag, const hb_map_t& axes_index_map, const hb_map_t& axes_old_index_tag_map) const { unsigned orig_axis_count = axes_old_index_tag_map.get_population (); - F2DOT14* out = reinterpret_cast (p); + auto it = peak_coords.iter (); + unsigned count = 0; for (unsigned i = 0; i < orig_axis_count; i++) { if (!axes_index_map.has (i)) /* axis pinned */ continue; - if (out >= end) return nullptr; hb_tag_t axis_tag = axes_old_index_tag_map.get (i); Triple *coords; if (!axis_tuples.has (axis_tag, &coords)) - (*out).set_int (0); + (*it).set_int (0); else - (*out).set_float (coords->middle); - out++; + (*it).set_float (coords->middle); + it++; + count++; } flag |= TupleVariationHeader::TuppleIndex::EmbeddedPeakTuple; - return out; + return count; } /* if no need to encode intermediate coords, then just return p */ - F2DOT14* encode_interm_coords (F2DOT14 *p, F2DOT14* end, + unsigned encode_interm_coords (hb_array_t coords, unsigned& flag, const hb_map_t& axes_index_map, const hb_map_t& axes_old_index_tag_map) const @@ -660,11 +643,10 @@ struct tuple_delta_t unsigned orig_axis_count = axes_old_index_tag_map.get_population (); unsigned cur_axis_count = axes_index_map.get_population (); - F2DOT14* out = reinterpret_cast (p); - - F2DOT14* start_coords = out; - F2DOT14* end_coords = start_coords + cur_axis_count; + auto start_coords_iter = coords.sub_array (0, cur_axis_count).iter (); + auto end_coords_iter = coords.sub_array (cur_axis_count).iter (); bool encode_needed = false; + unsigned count = 0; for (unsigned i = 0; i < orig_axis_count; i++) { if (!axes_index_map.has (i)) /* axis pinned */ @@ -679,14 +661,12 @@ struct tuple_delta_t max_val = coords->maximum; } - if (start_coords >= end || end_coords >= end) - return nullptr; + (*start_coords_iter).set_float (min_val); + (*end_coords_iter).set_float (max_val); - (*start_coords).set_float (min_val); - (*end_coords).set_float (max_val); - - start_coords++; - end_coords++; + start_coords_iter++; + end_coords_iter++; + count += 2; if (min_val != hb_min (val, 0.f) || max_val != hb_max (val, 0.f)) encode_needed = true; } @@ -694,9 +674,9 @@ struct tuple_delta_t if (encode_needed) { flag |= TupleVariationHeader::TuppleIndex::IntermediateRegion; - return end_coords; + return count; } - return p; + return 0; } bool compile_deltas () @@ -718,16 +698,10 @@ struct tuple_delta_t if (deltas_y) alloc_len *= 2; - char *p = (char *) hb_malloc (alloc_len); - if (unlikely (!p)) return false; + if (unlikely (!compiled_deltas.resize (alloc_len))) return false; unsigned i = 0; - char *end = p + alloc_len; - char *o = encode_delta_run (p, end, i, rounded_deltas); - if (!o || o == p) - { hb_free (p); return false; } - - unsigned encoded_len = o - p; + unsigned encoded_len = encode_delta_run (i, compiled_deltas.as_array (), rounded_deltas); if (deltas_y) { @@ -738,49 +712,44 @@ struct tuple_delta_t if (!indices[idx]) continue; int rounded_delta = (int) roundf (deltas_y[idx]); - if (j >= rounded_deltas.length) - { hb_free (p); return false; } + if (j >= rounded_deltas.length) return false; rounded_deltas[j++] = rounded_delta; } - if (j != rounded_deltas.length) { hb_free (p); return false; } - - char *out = encode_delta_run (o, end, i, rounded_deltas); - if (!out || out == o) { hb_free (p); return false; } - encoded_len = out - p; + if (j != rounded_deltas.length) return false; + encoded_len += encode_delta_run (i, compiled_deltas.as_array ().sub_array (encoded_len), rounded_deltas); } - compiled_deltas = byte_data_t (p, encoded_len); - return true; + return compiled_deltas.resize (encoded_len); } - char* encode_delta_run (char* p, char *end, unsigned& i, - const hb_vector_t& deltas) const + unsigned encode_delta_run (unsigned& i, + hb_array_t encoded_bytes, + const hb_vector_t& deltas) const { - if (p >= end) return nullptr; unsigned num_deltas = deltas.length; + unsigned encoded_len = 0; while (i < num_deltas) { int val = deltas[i]; if (val == 0) - p = encode_delta_run_as_zeroes (p, end, i, deltas); + encoded_len += encode_delta_run_as_zeroes (i, encoded_bytes.sub_array (encoded_len), deltas); else if (val >= -128 && val <= 127) - p = encode_delta_run_as_bytes (p, end, i, deltas); + encoded_len += encode_delta_run_as_bytes (i, encoded_bytes.sub_array (encoded_len), deltas); else - p = encode_delta_run_as_words (p, end, i, deltas); - - if (!p || p > end) - return nullptr; + encoded_len += encode_delta_run_as_words (i, encoded_bytes.sub_array (encoded_len), deltas); } - return p; + return encoded_len; } - char* encode_delta_run_as_zeroes (char* p, char *end, unsigned& i, - const hb_vector_t& deltas) const + unsigned encode_delta_run_as_zeroes (unsigned& i, + hb_array_t encoded_bytes, + const hb_vector_t& deltas) const { - if (p >= end) return nullptr; unsigned num_deltas = deltas.length; unsigned run_length = 0; + auto it = encoded_bytes.iter (); + unsigned encoded_len = 0; while (i < num_deltas && deltas[i] == 0) { i++; @@ -789,23 +758,23 @@ struct tuple_delta_t while (run_length >= 64) { - if (p >= end) return nullptr; - *p++ = (DELTAS_ARE_ZERO | 63); + *it++ = (DELTAS_ARE_ZERO | 63); run_length -= 64; + encoded_len++; } if (run_length) { - if (p >= end) return nullptr; - *p++ = (DELTAS_ARE_ZERO | (run_length - 1)); + *it++ = (DELTAS_ARE_ZERO | (run_length - 1)); + encoded_len++; } - return p; + return encoded_len; } - char* encode_delta_run_as_bytes (char *p, char *end, unsigned &i, - const hb_vector_t& deltas) const + unsigned encode_delta_run_as_bytes (unsigned &i, + hb_array_t encoded_bytes, + const hb_vector_t& deltas) const { - if (p >= end) return nullptr; unsigned start = i; unsigned num_deltas = deltas.length; while (i < num_deltas) @@ -823,16 +792,18 @@ struct tuple_delta_t } unsigned run_length = i - start; - HBINT8 *out = reinterpret_cast (p); + unsigned encoded_len = 0; + auto it = encoded_bytes.iter (); + while (run_length >= 64) { - if ((char *)out >= end) return nullptr; - *out++ = 63; + *it++ = 63; + encoded_len++; for (unsigned j = 0; j < 64; j++) { - if ((char *)out >= end) return nullptr; - *out++ = deltas[start + j]; + *it++ = static_cast (deltas[start + j]); + encoded_len++; } start += 64; @@ -841,20 +812,22 @@ struct tuple_delta_t if (run_length) { - if ((char *)out >= end) return nullptr; - *out++ = run_length - 1; + *it++ = run_length - 1; + encoded_len++; + while (start < i) { - if ((char *)out >= end) return nullptr; - *out++ = deltas[start++]; + *it++ = static_cast (deltas[start++]); + encoded_len++; } } - return reinterpret_cast (out); + return encoded_len; } - char* encode_delta_run_as_words (char *p, char *end, unsigned &i, - const hb_vector_t& deltas) const + unsigned encode_delta_run_as_words (unsigned &i, + hb_array_t encoded_bytes, + const hb_vector_t& deltas) const { unsigned start = i; unsigned num_deltas = deltas.length; @@ -877,15 +850,20 @@ struct tuple_delta_t } unsigned run_length = i - start; - HBINT16 *out = reinterpret_cast (p); + auto it = encoded_bytes.iter (); + unsigned encoded_len = 0; while (run_length >= 64) { - if ((char *)out >= end) return nullptr; - *out++ = (DELTAS_ARE_WORDS | 63); + *it++ = (DELTAS_ARE_WORDS | 63); + encoded_len++; + for (unsigned j = 0; j < 64; j++) { - if ((char *)out >= end) return nullptr; - *out++ = deltas[start + j]; + int16_t delta_val = deltas[start + j]; + *it++ = static_cast (delta_val >> 8); + *it++ = static_cast (delta_val & 0xFF); + + encoded_len += 2; } start += 64; @@ -894,15 +872,17 @@ struct tuple_delta_t if (run_length) { - if ((char *)out >= end) return nullptr; - *out++ = (DELTAS_ARE_WORDS | (run_length - 1)); + *it++ = (DELTAS_ARE_WORDS | (run_length - 1)); while (start < i) { - if ((char *)out >= end) return nullptr; - *out++ = deltas[start++]; + int16_t delta_val = deltas[start++]; + *it++ = static_cast (delta_val >> 8); + *it++ = static_cast (delta_val & 0xFF); + + encoded_len += 2; } } - return reinterpret_cast (out); + return encoded_len; } }; @@ -945,7 +925,15 @@ struct TupleVariationData hb_hashmap_t*, unsigned> point_set_count_map; public: - void fini () { tuple_vars.fini (); } + ~tuple_variations_t () { fini (); } + void fini () + { + for (auto _ : point_data_map.values ()) + _.fini (); + + point_set_count_map.fini (); + tuple_vars.fini (); + } unsigned get_var_count () const { return tuple_vars.length; } @@ -1101,7 +1089,7 @@ struct TupleVariationData p[pos++] = num_points & 0xFF; } - unsigned max_run_length = 0x7F; + const unsigned max_run_length = 0x7F; unsigned i = 0; unsigned last_value = 0; unsigned num_encoded = 0; @@ -1112,6 +1100,7 @@ struct TupleVariationData p[pos++] = 0; bool use_byte_encoding = false; + bool new_run = true; while (i < indices_length && num_encoded < num_points && run_length <= max_run_length) { @@ -1124,10 +1113,13 @@ struct TupleVariationData unsigned cur_value = i; unsigned delta = cur_value - last_value; - if (!use_byte_encoding) - use_byte_encoding = (delta >= 0 && delta <= 0xFF); + if (new_run) + { + use_byte_encoding = (delta <= 0xFF); + new_run = false; + } - if (use_byte_encoding && (delta <0 || delta > 0xFF)) + if (use_byte_encoding && delta > 0xFF) break; if (use_byte_encoding) @@ -1188,7 +1180,7 @@ struct TupleVariationData for (const auto& _ : point_data_map.iter ()) { const hb_vector_t* points_set = _.first; - unsigned data_length = _.second.length; + unsigned data_length = _.second.bytes.length; unsigned *count; if (unlikely (!point_set_count_map.has (points_set, &count) || *count <= 1)) @@ -1227,7 +1219,7 @@ struct TupleVariationData if (!tuple.compile_deltas ()) return false; - if (!tuple.compile_tuple_var_header (axes_index_map, points_data->length, axes_old_index_tag_map)) + if (!tuple.compile_tuple_var_header (axes_index_map, points_data->bytes.length, axes_old_index_tag_map)) return false; } return true; @@ -1238,8 +1230,9 @@ struct TupleVariationData TRACE_SERIALIZE (this); for (const auto& tuple: tuple_vars) { - if (!c->copy (tuple.compiled_tuple_header)) - return_trace (false); + byte_data_t compiled_bytes {tuple.compiled_tuple_header.arrayZ, tuple.compiled_tuple_header.length}; + compiled_bytes.copy (c); + if (c->in_error ()) return_trace (false); total_header_len += tuple.compiled_tuple_header.length; } return_trace (true); @@ -1252,11 +1245,13 @@ struct TupleVariationData { const hb_vector_t* points_set = &(tuple.indices); byte_data_t *point_data; - if (!point_data_map.has (points_set)) + if (!point_data_map.has (points_set, &point_data)) return_trace (false); - if (!c->copy (*point_data)) return_trace (false); - if (!c->copy (tuple.compiled_deltas)) return_trace (false); + point_data->copy (c); + byte_data_t compiled_bytes {tuple.compiled_deltas.arrayZ, tuple.compiled_deltas.length}; + compiled_bytes.copy (c); + if (c->in_error ()) return_trace (false); } return_trace (true); } diff --git a/src/hb-ot-var-cvar-table.hh b/src/hb-ot-var-cvar-table.hh index 5f7721d47..de5433930 100644 --- a/src/hb-ot-var-cvar-table.hh +++ b/src/hb-ot-var-cvar-table.hh @@ -27,6 +27,7 @@ #define HB_OT_VAR_CVAR_TABLE_HH #include "hb-ot-var-common.hh" +#include "hb-ot-var-fvar-table.hh" namespace OT { @@ -138,12 +139,21 @@ struct cvar bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); + if (c->plan->all_axes_pinned) + return_trace (false); + /* subset() for cvar is called by partial instancing only, we always pass * through cvar table in other cases */ - if (unlikely (!c->plan->normalized_coords || - c->plan->pinned_at_default || - c->plan->all_axes_pinned)) - return_trace (false); + if (!c->plan->normalized_coords) + { + unsigned axis_count = c->plan->source->table.fvar->get_axis_count (); + unsigned total_size = min_size + tupleVariationData.get_size (axis_count); + char *out = c->serializer->allocate_size (total_size); + if (unlikely (!out)) return_trace (false); + + hb_memcpy (out, this, total_size); + return_trace (true); + } OT::TupleVariationData::tuple_variations_t tuple_variations; unsigned axis_count = c->plan->axes_old_index_tag_map.get_population (); diff --git a/src/test-tuple-varstore.cc b/src/test-tuple-varstore.cc index 3b49928bc..c1fb747e5 100644 --- a/src/test-tuple-varstore.cc +++ b/src/test-tuple-varstore.cc @@ -117,22 +117,22 @@ test_decompile_cvar () assert (tuple_variations.tuple_vars[0].compiled_tuple_header.length == 6); const char tuple_var_header_1[] = "\x0\x51\xa0\x0\xc0\x0"; for (unsigned i = 0; i < 6; i++) - assert(tuple_variations.tuple_vars[0].compiled_tuple_header.p[i] == tuple_var_header_1[i]); + assert(tuple_variations.tuple_vars[0].compiled_tuple_header.arrayZ[i] == tuple_var_header_1[i]); assert (tuple_variations.tuple_vars[1].compiled_tuple_header.length == 6); const char tuple_var_header_2[] = "\x0\x54\xa0\x0\x40\x0"; for (unsigned i = 0; i < 6; i++) - assert(tuple_variations.tuple_vars[1].compiled_tuple_header.p[i] == tuple_var_header_2[i]); + assert(tuple_variations.tuple_vars[1].compiled_tuple_header.arrayZ[i] == tuple_var_header_2[i]); assert (tuple_variations.tuple_vars[0].compiled_deltas.length == 37); assert (tuple_variations.tuple_vars[1].compiled_deltas.length == 40); const char compiled_deltas_1[] = "\x0d\xff\x00\xfe\x01\x00\xff\x00\xfe\x01\x00\xed\xed\xf3\xf3\x82\x00\xfe\x84\x06\xfe\x00\x01\xf1\xf1\xf6\xf6\x82\x04\x01\xf1\xf1\xf6\xf6\x82\x00\x01"; for (unsigned i = 0; i < 37; i++) - assert (tuple_variations.tuple_vars[0].compiled_deltas.p[i] == compiled_deltas_1[i]); + assert (tuple_variations.tuple_vars[0].compiled_deltas.arrayZ[i] == compiled_deltas_1[i]); const char compiled_deltas_2[] = "\x0d\x01\x00\x04\xfe\x00\x01\x00\x04\xfe\x00\x44\x44\x30\x30\x82\x00\x04\x81\x09\x01\xff\x01\x05\xff\xfc\x33\x33\x25\x25\x82\x04\xff\x33\x33\x25\x25\x82\x00\xff"; for (unsigned i = 0; i < 40; i++) - assert (tuple_variations.tuple_vars[1].compiled_deltas.p[i] == compiled_deltas_2[i]); + assert (tuple_variations.tuple_vars[1].compiled_deltas.arrayZ[i] == compiled_deltas_2[i]); } int