HBUINT16 varDataSize; /* The size in bytes of the serialized
* data for this tuple variation table. */
TuppleIndex tupleIndex; /* A packed field. The high 4 bits are flags (see below). The low 12 bits are an index into a shared tuple
records array. */ /* UnsizedArrayOf<F2DOT14> peakTuple - optional */ /* Peak tuple record for this tuple variation table — optional, * determined by flags in the tupleIndex value. *
* Note that this must always be included in the 'cvar' table. */ /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */ /* Intermediate start tuple record for this tuple variation table — optional,
determined by flags in the tupleIndex value. */ /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */ /* Intermediate end tuple record for this tuple variation table — optional,
* determined by flags in the tupleIndex value. */ public:
DEFINE_SIZE_MIN (4);
};
struct tuple_delta_t
{ static constexpr bool realloc_move = true; // Watch out when adding new members!
/* indices_length = point_count, indice[i] = 1 means point i is referenced */
hb_vector_t<bool> indices;
hb_vector_t<double> deltas_x; /* empty for cvar tuples */
hb_vector_t<double> 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 */
hb_vector_t<unsignedchar> compiled_tuple_header;
hb_vector_t<unsignedchar> compiled_deltas;
unsigned num = indices.length; if (deltas_y) for (unsigned i = 0; i < num; i++)
{ if (!indices.arrayZ[i]) continue;
deltas_x[i] *= scalar;
deltas_y[i] *= scalar;
} else for (unsigned i = 0; i < num; i++)
{ if (!indices.arrayZ[i]) continue;
deltas_x[i] *= scalar;
} return *this;
}
unsigned orig_axis_count = axes_old_index_tag_map.get_population (); for (unsigned i = 0; i < orig_axis_count; i++)
{ if (!axes_index_map.has (i)) continue;
/* push F2DOT14 value into char vector */
int16_t val = peak_coord.to_int ();
compiled_peak_coords.push (static_cast<char> (val >> 8));
compiled_peak_coords.push (static_cast<char> (val & 0xFF));
}
return !compiled_peak_coords.in_error ();
}
/* 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 */ 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<char>*, unsigned>* shared_tuples_idx_map)
{ /* compiled_deltas could be empty after iup delta optimization, we can skip
* compiling this tuple and return true */ if (!compiled_deltas) returntrue;
/* 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);
/* pointdata length = 0 implies "use shared points" */ if (points_data_length)
flag |= TupleVariationHeader::TuppleIndex::PrivatePointNumbers;
for (unsigned i = 0; i < point_indices.length; i++)
{ if (!point_indices[i]) continue; int rounded_delta = (int) roundf (x_deltas.arrayZ[i]);
rounded_deltas.push (rounded_delta);
}
if (y_deltas)
{ /* reuse the rounded_deltas vector, check that y_deltas have the same num of deltas as x_deltas */ unsigned j = 0; for (unsigned idx = 0; idx < point_indices.length; idx++)
{ if (!point_indices[idx]) continue; int rounded_delta = (int) roundf (y_deltas.arrayZ[idx]);
for (unsigned i = 0; i < point_count; i++)
{ if (indices.arrayZ[i])
ref_count++; if (orig_points.arrayZ[i].is_end_point)
end_points.push (i);
} /* all points are referenced, nothing to do */ if (ref_count == point_count) returntrue; if (unlikely (end_points.in_error ())) returnfalse;
hb_set_t inferred_idxes; unsigned start_point = 0; for (unsigned end_point : end_points)
{ /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */ unsigned unref_count = 0; for (unsigned i = start_point; i < end_point + 1; i++)
unref_count += indices.arrayZ[i];
unref_count = (end_point - start_point + 1) - unref_count;
unsigned j = start_point; if (unref_count == 0 || unref_count > end_point - start_point) goto no_more_gaps; for (;;)
{ /* Locate the next gap of unreferenced points between two referenced points prev and next. * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
*/ unsignedint prev, next, i; for (;;)
{
i = j;
j = next_index (i, start_point, end_point); if (indices.arrayZ[i] && !indices.arrayZ[j]) break;
}
prev = j = i; for (;;)
{
i = j;
j = next_index (i, start_point, end_point); if (!indices.arrayZ[i] && indices.arrayZ[j]) break;
}
next = j; /* Infer deltas for all unref points in the gap between prev and next */
i = prev; for (;;)
{
i = next_index (i, start_point, end_point); if (i == next) break;
deltas_x.arrayZ[i] = infer_delta ((double) orig_points.arrayZ[i].x,
(double) orig_points.arrayZ[prev].x,
(double) orig_points.arrayZ[next].x,
deltas_x.arrayZ[prev], deltas_x.arrayZ[next]);
deltas_y.arrayZ[i] = infer_delta ((double) orig_points.arrayZ[i].y,
(double) orig_points.arrayZ[prev].y,
(double) orig_points.arrayZ[next].y,
deltas_y.arrayZ[prev], deltas_y.arrayZ[next]);
inferred_idxes.add (i); if (--unref_count == 0) goto no_more_gaps;
}
}
no_more_gaps:
start_point = end_point + 1;
}
for (unsigned i = 0; i < point_count; i++)
{ /* if points are not referenced and deltas are not inferred, set to 0.
* reference all points for gvar */ if ( !indices[i])
{ if (!inferred_idxes.has (i))
{
deltas_x.arrayZ[i] = 0.0;
deltas_y.arrayZ[i] = 0.0;
}
indices[i] = true;
}
} returntrue;
}
staticbool compile_point_set (const hb_vector_t<bool> &point_indices,
hb_vector_t<unsignedchar>& compiled_points /* OUT */)
{ unsigned num_points = 0; for (bool i : point_indices) if (i) num_points++;
/* when iup optimization is enabled, num of referenced points could be 0 */ if (!num_points) returntrue;
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) return compiled_points.resize (1);
/* allocate enough memories: 2 bytes for count + 3 bytes for each point */ unsigned num_bytes = 2 + 3 *num_points; if (unlikely (!compiled_points.resize (num_bytes, false))) returnfalse;
unsigned pos = 0; /* binary data starts with the total number of reference points */ if (num_points < 0x80)
compiled_points.arrayZ[pos++] = num_points; else
{
compiled_points.arrayZ[pos++] = ((num_points >> 8) | 0x80);
compiled_points.arrayZ[pos++] = num_points & 0xFF;
}
bool use_byte_encoding = false; bool new_run = true; 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++;
double r = (target_val - prev_val) / (next_val - prev_val); return prev_delta + r * (next_delta - prev_delta);
}
staticunsignedint next_index (unsignedint i, unsignedint start, unsignedint end)
{ return (i >= end) ? start : (i + 1); }
};
struct TupleVariationData
{ bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this); // here check on min_size only, TupleVariationHeader and var data will be // checked while accessing through iterator.
return_trace (c->check_struct (this));
}
private: /* referenced point set->compiled point data map */
hb_hashmap_t<const hb_vector_t<bool>*, hb_vector_t<char>> point_data_map; /* referenced point set-> count map, used in finding shared points */
hb_hashmap_t<const hb_vector_t<bool>*, unsigned> point_set_count_map;
/* empty for non-gvar tuples. * shared_points_bytes is a pointer to some value in the point_data_map, * which will be freed during map destruction. Save it for serialization, so
* no need to do find_shared_points () again */
hb_vector_t<char> *shared_points_bytes = nullptr;
/* total compiled byte size as TupleVariationData format, initialized to 0 */ unsigned compiled_byte_size = 0; bool needs_padding = false;
/* for gvar iup delta optimization: whether this is a composite glyph */ bool is_composite = false;
explicitoperatorbool () const { returnbool (tuple_vars); } unsigned get_var_count () const
{ unsigned count = 0; /* when iup delta opt is enabled, compiled_deltas could be empty and we
* should skip this tuple */ for (auto& tuple: tuple_vars) if (tuple.compiled_deltas) count++;
if (shared_points_bytes && shared_points_bytes->length)
count |= TupleVarCount::SharedPointNumbers; return count;
}
for (unsigned r = 0; r < num_regions; r++)
{ /* In VarData, deltas are organized in rows, convert them into
* column(region) based tuples, resize deltas_x first */
tuple_delta_t tuple; if (!tuple.deltas_x.resize (item_count, false) ||
!tuple.indices.resize (item_count, false)) returnfalse;
for (unsigned i = 0; i < item_count; i++)
{
tuple.indices.arrayZ[i] = true;
tuple.deltas_x.arrayZ[i] = var_data.get_item_delta_fast (inner_map ? inner_map->backward (i) : i,
r, delta_bytes, row_size);
}
bool change_tuple_variations_axis_limits (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location, const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances)
{ /* sort axis_tag/axis_limits, make result deterministic */
hb_vector_t<hb_tag_t> axis_tags; if (!axis_tags.alloc (normalized_axes_location.get_population ())) returnfalse; for (auto t : normalized_axes_location.keys ())
axis_tags.push (t);
axis_tags.qsort (_cmp_axis_tag); for (auto axis_tag : axis_tags)
{
Triple *axis_limit; if (!normalized_axes_location.has (axis_tag, &axis_limit)) returnfalse;
TripleDistances axis_triple_distances{1.0, 1.0}; if (axes_triple_distances.has (axis_tag))
axis_triple_distances = axes_triple_distances.get (axis_tag);
hb_vector_t<tuple_delta_t> new_vars; for (const tuple_delta_t& var : tuple_vars)
{
hb_vector_t<tuple_delta_t> out = var.change_tuple_var_axis_limit (axis_tag, *axis_limit, axis_triple_distances); if (!out) continue;
unsigned new_len = new_vars.length + out.length;
if (unlikely (!new_vars.alloc (new_len, false))) returnfalse;
for (unsigned i = 0; i < out.length; i++)
new_vars.push (std::move (out[i]));
}
tuple_vars.fini ();
tuple_vars = std::move (new_vars);
} returntrue;
}
/* merge tuple variations with overlapping tents, if iup delta optimization
* is enabled, add default deltas to contour_points */ bool merge_tuple_variations (contour_point_vector_t* contour_points = nullptr)
{
hb_vector_t<tuple_delta_t> new_vars;
hb_hashmap_t<const hb_hashmap_t<hb_tag_t, Triple>*, unsigned> m; unsigned i = 0; for (const tuple_delta_t& var : tuple_vars)
{ /* if all axes are pinned, drop the tuple variation */ if (var.axis_tuples.is_empty ())
{ /* if iup_delta_optimize is enabled, add deltas to contour coords */ if (contour_points && !contour_points->add_deltas (var.deltas_x,
var.deltas_y,
var.indices)) returnfalse; continue;
}
/* compile all point set and store byte data in a point_set->hb_bytes_t hashmap, * also update point_set->count map, which will be used in finding shared
* point set*/ bool compile_all_point_sets ()
{ for (constauto& tuple: tuple_vars)
{ const hb_vector_t<bool>* 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))) returnfalse; continue;
}
hb_vector_t<unsignedchar> compiled_point_data; if (!tuple_delta_t::compile_point_set (*points_set, compiled_point_data)) returnfalse;
bool calc_inferred_deltas (const contour_point_vector_t& contour_points)
{ for (tuple_delta_t& var : tuple_vars) if (!var.calc_inferred_deltas (contour_points)) returnfalse;
returntrue;
}
bool iup_optimize (const contour_point_vector_t& contour_points)
{ for (tuple_delta_t& var : tuple_vars)
{ if (!var.optimize (contour_points, is_composite)) returnfalse;
} returntrue;
}
public: bool instantiate (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location, const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances,
contour_point_vector_t* contour_points = nullptr, bool optimize = false)
{ if (!tuple_vars) returntrue; if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances)) returnfalse; /* compute inferred deltas only for gvar */ if (contour_points) if (!calc_inferred_deltas (*contour_points)) returnfalse;
/* if iup delta opt is on, contour_points can't be null */ if (optimize && !contour_points) returnfalse;
if (!merge_tuple_variations (optimize ? contour_points : nullptr)) returnfalse;
if (optimize && !iup_optimize (*contour_points)) returnfalse; return !tuple_vars.in_error ();
}
// compile points set and store data in hashmap if (!compile_all_point_sets ()) returnfalse;
/* total compiled byte size as TupleVariationData format, initialized to its
* min_size: 4 */
compiled_byte_size += 4;
if (use_shared_points)
{
find_shared_points (); if (shared_points_bytes)
compiled_byte_size += shared_points_bytes->length;
} // compile delta and tuple var header for each tuple variation for (auto& tuple: tuple_vars)
{ const hb_vector_t<bool>* points_set = &(tuple.indices);
hb_vector_t<char> *points_data; if (unlikely (!point_data_map.has (points_set, &points_data))) returnfalse;
/* when iup optimization is enabled, num of referenced points could be 0 * and thus the compiled points bytes is empty, we should skip compiling
* this tuple */ if (!points_data->length) continue; if (!tuple.compile_deltas ()) returnfalse;
TupleVarCount tupleVarCount; /* A packed field. The high 4 bits are flags, and the * low 12 bits are the number of tuple variation tables * for this glyph. The number of tuple variation tables
* can be any number between 1 and 4095. */
Offset16To<HBUINT8>
data; /* Offset from the start of the base table
* to the serialized data. */ /* TupleVariationHeader tupleVariationHeaders[] *//* Array of tuple variation headers. */ public:
DEFINE_SIZE_MIN (4);
};
using tuple_variations_t = TupleVariationData::tuple_variations_t; struct item_variations_t
{ using region_t = const hb_hashmap_t<hb_tag_t, Triple>*; private: /* each subtable is decompiled into a tuple_variations_t, in which all tuples
* have the same num of deltas (rows) */
hb_vector_t<tuple_variations_t> vars;
/* num of retained rows for each subtable, there're 2 cases when var_data is empty: * 1. retained item_count is zero * 2. regions is empty and item_count is non-zero. * when converting to tuples, both will be dropped because the tuple is empty, * however, we need to retain 2. as all-zero rows to keep original varidx
* valid, so we need a way to remember the num of rows for each subtable */
hb_vector_t<unsigned> var_data_num_rows;
/* original region list, decompiled from item varstore, used when rebuilding
* region list after instantiation */
hb_vector_t<hb_hashmap_t<hb_tag_t, Triple>> orig_region_list;
/* region list: vector of Regions, maintain the original order for the regions * that existed before instantiate (), append the new regions at the end. * Regions are stored in each tuple already, save pointers only.
* When converting back to item varstore, unused regions will be pruned */
hb_vector_t<region_t> region_list;
/* region -> idx map after instantiation and pruning unused regions */
hb_hashmap_t<region_t, unsigned> region_map;
/* all delta rows after instantiation */
hb_vector_t<hb_vector_t<int>> delta_rows; /* final optimized vector of encoding objects used to assemble the varstore */
hb_vector_t<delta_row_encoding_t> encodings;
/* old varidxes -> new var_idxes map */
hb_map_t varidx_map;
if (!build_region_list ()) returnfalse; returntrue;
}
bool build_region_list ()
{ /* scan all tuples and collect all unique regions, prune unused regions */
hb_hashmap_t<region_t, unsigned> all_regions;
hb_hashmap_t<region_t, unsigned> used_regions;
/* use a vector when inserting new regions, make result deterministic */
hb_vector_t<region_t> all_unique_regions; for (const tuple_variations_t& sub_table : vars)
{ for (const tuple_delta_t& tuple : sub_table.tuple_vars)
{
region_t r = &(tuple.axis_tuples); if (!used_regions.has (r))
{ bool all_zeros = true; for (float d : tuple.deltas_x)
{ int delta = (int) roundf (d); if (delta != 0)
{
all_zeros = false; break;
}
} if (!all_zeros)
{ if (!used_regions.set (r, 1)) returnfalse;
}
} if (all_regions.has (r)) continue; if (!all_regions.set (r, 1)) returnfalse;
all_unique_regions.push (r);
}
}
/* regions are empty means no variation data, return true */ if (!all_regions || !all_unique_regions) returntrue;
if (!region_list.alloc (all_regions.get_population ())) returnfalse;
unsigned idx = 0; /* append the original regions that pre-existed */ for (constauto& r : orig_region_list)
{ if (!all_regions.has (&r) || !used_regions.has (&r)) continue;
/* append the new regions at the end */ for (constauto& r: all_unique_regions)
{ if (!all_regions.has (r) || !used_regions.has (r)) continue;
region_list.push (r); if (!region_map.set (r, idx)) returnfalse;
all_regions.del (r);
idx++;
} return (!region_list.in_error ()) && (!region_map.in_error ());
}
/* main algorithm ported from fonttools VarStore_optimize() method, optimize
* varstore by default */
struct combined_gain_idx_tuple_t
{ int gain; unsigned idx_1; unsigned idx_2;
bool as_item_varstore (bool optimize=true, bool use_no_variation_idx=true)
{ /* return true if no variation data */ if (!region_list) returntrue; unsigned num_cols = region_list.length; /* pre-alloc a 2D vector for all sub_table's VarData rows */ unsigned total_rows = 0; for (unsigned major = 0; major < var_data_num_rows.length; major++)
total_rows += var_data_num_rows[major];
if (!delta_rows.resize (total_rows)) returnfalse; /* init all rows to [0]*num_cols */ for (unsigned i = 0; i < total_rows; i++) if (!(delta_rows[i].resize (num_cols))) returnfalse;
/* delta_rows map, used for filtering out duplicate rows */
hb_hashmap_t<const hb_vector_t<int>*, unsigned> delta_rows_map; for (unsigned major = 0; major < vars.length; major++)
{ /* deltas are stored in tuples(column based), convert them back into items
* (row based) delta */ const tuple_variations_t& tuples = vars[major]; unsigned num_rows = var_data_num_rows[major]; for (const tuple_delta_t& tuple: tuples.tuple_vars)
{ if (tuple.deltas_x.length != num_rows) returnfalse;
/* skip unused regions */ unsigned *col_idx; if (!region_map.has (&(tuple.axis_tuples), &col_idx)) continue;
for (unsigned i = 0; i < num_rows; i++)
{ int rounded_delta = roundf (tuple.deltas_x[i]);
delta_rows[start_row + i][*col_idx] += rounded_delta; if ((!has_long) && (rounded_delta < -65536 || rounded_delta > 65535))
has_long = true;
}
}
if (!optimize)
{ /* assemble a delta_row_encoding_t for this subtable, skip optimization so
* chars is not initialized, we only need delta rows for serialization */
delta_row_encoding_t obj; for (unsigned r = start_row; r < start_row + num_rows; r++)
obj.add_row (&(delta_rows.arrayZ[r]));
/* return directly if no optimization, maintain original VariationIndex so
* varidx_map would be empty */ if (!optimize) return !encodings.in_error ();
/* sort encoding_objs */
encoding_objs.qsort ();
/* main algorithm: repeatedly pick 2 best encodings to combine, and combine
* them */
hb_priority_queue_t<combined_gain_idx_tuple_t> queue; unsigned num_todos = encoding_objs.length; for (unsigned i = 0; i < num_todos; i++)
{ for (unsigned j = i + 1; j < num_todos; j++)
{ int combining_gain = encoding_objs.arrayZ[i].gain_from_merging (encoding_objs.arrayZ[j]); if (combining_gain > 0)
queue.insert (combined_gain_idx_tuple_t (-combining_gain, i, j), 0);
}
}
hb_set_t removed_todo_idxes; while (queue)
{ auto t = queue.pop_minimum ().first; unsigned i = t.idx_1; unsigned j = t.idx_2;
if (removed_todo_idxes.has (i) || removed_todo_idxes.has (j)) continue;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.