mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 18:13:53 +01:00
[subset] Preserve precision in variable layout conditions
Partial instancing rounds the default and surviving deltas of format-2 layout conditions independently, moving their Boolean boundary and changing GSUB or GPOS lookup selection. Rescale variable conditions into private variation-store rows before integer encoding, leaving shared positioning values unchanged. Prefer exact integers; otherwise use the available signed 32-bit precision. Carry defaults outside int16 in a neutral variation region. Keep source values separate from output mappings so serialization retries start with the original condition, and make private-row order independent of source addresses. Propagate store-instancing failures to the serializer. Add legacy and additive feature-variation fixtures covering nested conditions, fractional coefficients, large defaults, shared positioning rows, and a second instancing pass. Tested: full Meson suite with and without experimental API (279 passed; 261 passed and 1 skipped respectively), C++11 reduced-feature syntax checks, and C++11 sanitizer builds with failing allocation checks. Assisted-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
1 parent
6ef0c69328
commit
49d6ba3fa4
19 files changed
+382
-20
No files matched your search
+14
-10
@@ -696,17 +696,20 @@ struct GDEF
|
||||
if (varStore)
|
||||
{
|
||||
item_variations_t item_vars;
|
||||
if (item_vars.instantiate (this+varStore, c->plan, true, true,
|
||||
c->plan->gdef_varstore_inner_maps.as_array ())) {
|
||||
subset_varstore = out->varStore.serialize_serialize (c->serializer,
|
||||
item_vars.has_long_word (),
|
||||
c->plan->axis_tags,
|
||||
item_vars.get_region_list (),
|
||||
item_vars.get_vardata_encodings ());
|
||||
varstore_index = c->serializer->last_added_child_index();
|
||||
}
|
||||
if (unlikely (!c->serializer->check_success (
|
||||
item_vars.instantiate (this+varStore, c->plan, true, true,
|
||||
c->plan->gdef_varstore_inner_maps.as_array (), true))))
|
||||
return_trace (false);
|
||||
subset_varstore = out->varStore.serialize_serialize (c->serializer,
|
||||
item_vars.has_long_word (),
|
||||
c->plan->axis_tags,
|
||||
item_vars.get_region_list (),
|
||||
item_vars.get_vardata_encodings ());
|
||||
varstore_index = c->serializer->last_added_child_index();
|
||||
remap_varidx_after_instantiation (item_vars.get_varidx_map (),
|
||||
c->plan->layout_variation_idx_delta_map);
|
||||
remap_varidx_after_instantiation (item_vars.get_varidx_map (),
|
||||
c->plan->layout_condition_idx_value_map);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -1116,8 +1119,9 @@ struct GDEF
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename Key>
|
||||
static void remap_varidx_after_instantiation (const hb_map_t& varidx_map,
|
||||
hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>>& layout_variation_idx_delta_map /* IN/OUT */)
|
||||
hb_hashmap_t<Key, hb_pair_t<unsigned, int>>& layout_variation_idx_delta_map /* IN/OUT */)
|
||||
{
|
||||
/* varidx_map is empty which means varstore is empty after instantiation,
|
||||
* no variations, map all varidx to HB_OT_LAYOUT_NO_VARIATIONS_INDEX.
|
||||
|
||||
@@ -218,6 +218,7 @@ struct hb_collect_variation_indices_context_t :
|
||||
hb_set_t *layout_variation_indices;
|
||||
const hb_set_t *glyph_set;
|
||||
const hb_map_t *gpos_lookups;
|
||||
hb_hashmap_t<const void*, hb_pair_t<unsigned, int>> *condition_values = nullptr;
|
||||
|
||||
hb_collect_variation_indices_context_t (hb_set_t *layout_variation_indices_,
|
||||
const hb_set_t *glyph_set_,
|
||||
@@ -4392,6 +4393,14 @@ struct ConditionValue
|
||||
if (varIdx == VarIdx::NO_VARIATION)
|
||||
return_trace (true);
|
||||
|
||||
hb_pair_t<unsigned, int> *condition_value;
|
||||
if (c->plan->layout_condition_idx_value_map.has (this, &condition_value))
|
||||
{
|
||||
out->defaultValue = condition_value->second;
|
||||
out->varIdx = condition_value->first;
|
||||
return_trace (true);
|
||||
}
|
||||
|
||||
hb_pair_t<unsigned, int> *new_varidx_delta;
|
||||
if (!c->plan->layout_variation_idx_delta_map.has (varIdx, &new_varidx_delta))
|
||||
return_trace (false);
|
||||
@@ -4705,7 +4714,8 @@ struct Condition
|
||||
}
|
||||
|
||||
bool collect_var_indices (hb_set_t *var_indices,
|
||||
unsigned depth = HB_MAX_NESTING_LEVEL) const;
|
||||
unsigned depth = HB_MAX_NESTING_LEVEL,
|
||||
hb_hashmap_t<const void*, hb_pair_t<unsigned, int>> *condition_values = nullptr) const;
|
||||
|
||||
bool serialize (hb_serialize_context_t *c,
|
||||
const Condition *src,
|
||||
@@ -4779,7 +4789,8 @@ struct ConditionList
|
||||
};
|
||||
|
||||
inline bool
|
||||
Condition::collect_var_indices (hb_set_t *var_indices, unsigned depth) const
|
||||
Condition::collect_var_indices (hb_set_t *var_indices, unsigned depth,
|
||||
hb_hashmap_t<const void*, hb_pair_t<unsigned, int>> *condition_values) const
|
||||
{
|
||||
if (unlikely (!depth)) return false;
|
||||
switch (u.format.v)
|
||||
@@ -4789,23 +4800,29 @@ Condition::collect_var_indices (hb_set_t *var_indices, unsigned depth) const
|
||||
return true;
|
||||
case 2:
|
||||
if (u.format2.varIdx != VarIdx::NO_VARIATION)
|
||||
var_indices->add (u.format2.varIdx);
|
||||
{
|
||||
var_indices->add (u.format2.varIdx);
|
||||
if (condition_values &&
|
||||
!condition_values->set (&u.format2,
|
||||
hb_pair (unsigned (u.format2.varIdx), int (u.format2.defaultValue))))
|
||||
return false;
|
||||
}
|
||||
return !var_indices->in_error ();
|
||||
case 3:
|
||||
for (const auto &offset : u.format3.conditions)
|
||||
if (unlikely (!(&u.format3+offset).collect_var_indices (var_indices,
|
||||
depth - 1)))
|
||||
depth - 1, condition_values)))
|
||||
return false;
|
||||
return true;
|
||||
case 4:
|
||||
for (const auto &offset : u.format4.conditions)
|
||||
if (unlikely (!(&u.format4+offset).collect_var_indices (var_indices,
|
||||
depth - 1)))
|
||||
depth - 1, condition_values)))
|
||||
return false;
|
||||
return true;
|
||||
case 5:
|
||||
return (&u.format5+u.format5.condition).collect_var_indices (var_indices,
|
||||
depth - 1);
|
||||
depth - 1, condition_values);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -5008,6 +5025,17 @@ Condition::subset_lookup_condition_impl (hb_subset_context_t *c,
|
||||
LOOKUP_CONDITION_SUBSET_TRUE :
|
||||
LOOKUP_CONDITION_SUBSET_FALSE);
|
||||
|
||||
hb_pair_t<unsigned, int> *condition_value;
|
||||
if (c->plan->layout_condition_idx_value_map.has (&src, &condition_value))
|
||||
{
|
||||
out->defaultValue = condition_value->second;
|
||||
out->varIdx = condition_value->first;
|
||||
if (out->varIdx == VarIdx::NO_VARIATION)
|
||||
return out->defaultValue > 0 ? LOOKUP_CONDITION_SUBSET_TRUE :
|
||||
LOOKUP_CONDITION_SUBSET_FALSE;
|
||||
return LOOKUP_CONDITION_SUBSET_KEEP;
|
||||
}
|
||||
|
||||
hb_pair_t<unsigned, int> *new_varidx_delta;
|
||||
if (!c->plan->layout_variation_idx_delta_map.has (src.varIdx,
|
||||
&new_varidx_delta))
|
||||
@@ -5235,7 +5263,9 @@ struct ConditionSet
|
||||
void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
|
||||
{
|
||||
for (const auto &offset : conditions)
|
||||
if (unlikely (!(this+offset).collect_var_indices (c->layout_variation_indices)))
|
||||
if (unlikely (!(this+offset).collect_var_indices (c->layout_variation_indices,
|
||||
HB_MAX_NESTING_LEVEL,
|
||||
c->condition_values)))
|
||||
c->layout_variation_indices->err ();
|
||||
}
|
||||
|
||||
@@ -5553,7 +5583,9 @@ struct LookupConditionRecord
|
||||
void collect_variation_indices (hb_collect_variation_indices_context_t *c,
|
||||
const void *base) const
|
||||
{
|
||||
if (unlikely (!(base+condition).collect_var_indices (c->layout_variation_indices)))
|
||||
if (unlikely (!(base+condition).collect_var_indices (c->layout_variation_indices,
|
||||
HB_MAX_NESTING_LEVEL,
|
||||
c->condition_values)))
|
||||
c->layout_variation_indices->err ();
|
||||
}
|
||||
|
||||
|
||||
+118
-2
@@ -1851,7 +1851,8 @@ struct item_variations_t
|
||||
const hb_subset_plan_t *plan,
|
||||
bool optimize=true,
|
||||
bool use_no_variation_idx=true,
|
||||
const hb_array_t <const hb_inc_bimap_t> inner_maps = hb_array_t<const hb_inc_bimap_t> ())
|
||||
const hb_array_t <const hb_inc_bimap_t> inner_maps = hb_array_t<const hb_inc_bimap_t> (),
|
||||
bool layout_conditions = false)
|
||||
{
|
||||
if (!create_from_item_varstore (varStore, plan->axes_old_index_tag_map, inner_maps))
|
||||
return false;
|
||||
@@ -1859,11 +1860,126 @@ struct item_variations_t
|
||||
if (plan->has_avar2 && plan->avar2_reachable_ranges.get_population ())
|
||||
for (tuple_variations_t& tuple_vars : vars)
|
||||
tuple_vars.cull_unreachable (plan->avar2_reachable_ranges);
|
||||
if (!instantiate_tuple_vars (plan->axes_location, plan->axes_triple_distances))
|
||||
if (!instantiate_tuple_vars (plan->axes_location, plan->axes_triple_distances,
|
||||
!layout_conditions))
|
||||
return false;
|
||||
if (layout_conditions &&
|
||||
(!instantiate_condition_values (plan, varStore) || !build_region_list ()))
|
||||
return false;
|
||||
return as_item_varstore (optimize, use_no_variation_idx);
|
||||
}
|
||||
|
||||
bool instantiate_condition_values (const hb_subset_plan_t *plan,
|
||||
const ItemVariationStore &varStore)
|
||||
{
|
||||
// GDEF serialization can be retried; always start from the source values.
|
||||
auto &condition_map = plan->layout_condition_idx_value_map;
|
||||
condition_map.clear ();
|
||||
if (!plan->layout_condition_values) return true;
|
||||
using condition_value_t = hb_pair_t<const void*, hb_pair_t<unsigned, int>>;
|
||||
hb_vector_t<condition_value_t> conditions;
|
||||
for (auto condition : plan->layout_condition_values.iter ())
|
||||
conditions.push (condition);
|
||||
if (unlikely (conditions.in_error ())) return false;
|
||||
// Row order must not depend on the addresses of the source conditions.
|
||||
conditions.qsort ([] (const condition_value_t &a, const condition_value_t &b) {
|
||||
if (a.second.first != b.second.first)
|
||||
return a.second.first < b.second.first ? -1 : 1;
|
||||
return a.second.second < b.second.second ? -1 :
|
||||
a.second.second > b.second.second ? 1 : 0;
|
||||
});
|
||||
hb_map_t outer_map;
|
||||
unsigned new_outer = 0;
|
||||
for (unsigned i = 0; i < plan->gdef_varstore_inner_maps.length; i++)
|
||||
if (plan->gdef_varstore_inner_maps[i].get_population ())
|
||||
if (!outer_map.set (i, new_outer++)) return false;
|
||||
for (auto condition : conditions)
|
||||
{
|
||||
unsigned old_idx = condition.second.first;
|
||||
float value = condition.second.second +
|
||||
varStore.get_delta (old_idx, plan->normalized_coords.as_array ());
|
||||
unsigned old_outer = old_idx >> 16;
|
||||
if (!outer_map.has (old_outer))
|
||||
continue;
|
||||
unsigned outer = outer_map.get (old_outer);
|
||||
unsigned inner = plan->gdef_varstore_inner_maps[old_outer].get (old_idx & 0xFFFF);
|
||||
unsigned idx = (outer << 16) | inner;
|
||||
if (outer >= vars.length || inner >= var_data_num_rows[outer])
|
||||
continue;
|
||||
|
||||
double maximum = fabs ((double) value);
|
||||
bool variable = false;
|
||||
for (const tuple_delta_t &tuple : vars[outer].tuple_vars)
|
||||
{
|
||||
if (unlikely (inner >= tuple.deltas_x.length)) return false;
|
||||
float delta = tuple.deltas_x[inner];
|
||||
variable |= delta != 0.f;
|
||||
maximum = hb_max (maximum, fabs ((double) delta));
|
||||
}
|
||||
if (!variable)
|
||||
{
|
||||
if (!condition_map.set (condition.first,
|
||||
hb_pair (unsigned (VarIdx::NO_VARIATION), int (value > 0))))
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
|
||||
double scale = 1.;
|
||||
while (maximum * scale > INT_MAX)
|
||||
scale *= .5;
|
||||
// Positive rescaling preserves the Boolean boundary. Prefer exact
|
||||
// integers, otherwise use the available 32-bit precision.
|
||||
while (maximum * scale * 2 <= INT_MAX)
|
||||
{
|
||||
bool integral = (double) value * scale == round ((double) value * scale);
|
||||
for (const tuple_delta_t &tuple : vars[outer].tuple_vars)
|
||||
integral &= (double) tuple.deltas_x[inner] * scale ==
|
||||
round ((double) tuple.deltas_x[inner] * scale);
|
||||
if (integral) break;
|
||||
scale *= 2;
|
||||
}
|
||||
int default_value = (int) round ((double) value * scale);
|
||||
if (scale != 1. || default_value < -32768 || default_value > 32767)
|
||||
{
|
||||
// A condition may share its row with a positioning value. Give it a
|
||||
// private copy before integer rounding discards fractional deltas.
|
||||
tuple_variations_t private_row;
|
||||
for (const tuple_delta_t &tuple : vars[outer].tuple_vars)
|
||||
{
|
||||
if (!tuple.deltas_x[inner]) continue;
|
||||
tuple_delta_t copy;
|
||||
copy.axis_tuples = tuple.axis_tuples;
|
||||
copy.indices.push (true);
|
||||
copy.deltas_x.push ((float) round ((double) tuple.deltas_x[inner] * scale));
|
||||
if (unlikely (copy.axis_tuples.in_error () || copy.indices.in_error () ||
|
||||
copy.deltas_x.in_error ())) return false;
|
||||
private_row.tuple_vars.push (std::move (copy));
|
||||
}
|
||||
if (unlikely (vars.length >= 0xFFFFu)) return false;
|
||||
if (unlikely (private_row.tuple_vars.in_error ())) return false;
|
||||
idx = vars.length << 16;
|
||||
vars.push (std::move (private_row));
|
||||
var_data_num_rows.push (1);
|
||||
if (unlikely (vars.in_error () || var_data_num_rows.in_error ())) return false;
|
||||
if (default_value < -32768 || default_value > 32767)
|
||||
{
|
||||
// A neutral region can carry the bias when the int16 default cannot.
|
||||
unsigned count = vars[idx >> 16].tuple_vars.length;
|
||||
add_tuple (idx >> 16, hb_hashmap_t<hb_tag_t, Triple> (),
|
||||
0, default_value, 1);
|
||||
default_value = 0;
|
||||
const auto &tuples = vars[idx >> 16].tuple_vars;
|
||||
if (unlikely (tuples.in_error () || tuples.length != count + 1 ||
|
||||
tuples.tail ().indices.in_error () ||
|
||||
tuples.tail ().deltas_x.in_error ())) return false;
|
||||
}
|
||||
}
|
||||
if (!condition_map.set (condition.first, hb_pair (idx, default_value)))
|
||||
return false;
|
||||
}
|
||||
return !condition_map.in_error ();
|
||||
}
|
||||
|
||||
/* keep below APIs public only for unit test: test-item-varstore */
|
||||
bool create_from_item_varstore (const ItemVariationStore& varStore,
|
||||
const hb_map_t& axes_old_index_tag_map,
|
||||
|
||||
@@ -490,6 +490,8 @@ collect_layout_variation_indices (hb_subset_plan_t* plan)
|
||||
OT::hb_collect_variation_indices_context_t c (&varidx_set,
|
||||
&plan->_glyphset_gsub,
|
||||
&plan->gpos_lookups);
|
||||
if (plan->normalized_coords && !plan->all_axes_pinned)
|
||||
c.condition_values = &plan->layout_condition_values;
|
||||
gdef->collect_variation_indices (&c);
|
||||
|
||||
if (hb_ot_layout_has_substitution (plan->source))
|
||||
@@ -508,6 +510,7 @@ collect_layout_variation_indices (hb_subset_plan_t* plan)
|
||||
|
||||
unsigned subtable_count = gdef->get_var_store ().get_sub_table_count ();
|
||||
generate_varstore_inner_maps (varidx_set, subtable_count, plan->gdef_varstore_inner_maps);
|
||||
plan->check_success (!plan->layout_condition_values.in_error ());
|
||||
|
||||
gdef.destroy ();
|
||||
gsub.destroy ();
|
||||
|
||||
@@ -127,6 +127,11 @@ HB_SUBSET_PLAN_MEMBER (hb_map_t, colrv1_new_deltaset_idx_varidx_map)
|
||||
//Old layout item variation index -> (New varidx, delta) mapping
|
||||
HB_SUBSET_PLAN_MEMBER (mutable hb_hashmap_t E(<unsigned, hb_pair_t E(<unsigned, int>)>), layout_variation_idx_delta_map)
|
||||
|
||||
//Layout condition -> (source variation index, default value)
|
||||
HB_SUBSET_PLAN_MEMBER (hb_hashmap_t E(<const void*, hb_pair_t E(<unsigned, int>)>), layout_condition_values)
|
||||
//Layout condition -> (private variation index, rescaled default value)
|
||||
HB_SUBSET_PLAN_MEMBER (mutable hb_hashmap_t E(<const void*, hb_pair_t E(<unsigned, int>)>), layout_condition_idx_value_map)
|
||||
|
||||
//gdef varstore retained varidx mapping
|
||||
HB_SUBSET_PLAN_MEMBER (hb_vector_t<hb_inc_bimap_t>, gdef_varstore_inner_maps)
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,141 @@
|
||||
"""Generate precision fixtures with FontTools supporting LookupVariationRecord."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||
from fontTools.ttLib import newTable
|
||||
from fontTools.ttLib.tables import otTables as ot
|
||||
from fontTools.varLib import builder, featureVars
|
||||
|
||||
|
||||
def value(default, index=ot.NO_VARIATION_INDEX):
|
||||
condition = ot.ConditionTable()
|
||||
condition.Format, condition.DefaultValue, condition.VarIdx = 2, default, index
|
||||
return condition
|
||||
|
||||
|
||||
def compound(format, children):
|
||||
condition = ot.ConditionTable()
|
||||
condition.Format, condition.ConditionTable = format, children
|
||||
if format != 5:
|
||||
condition.ConditionCount = len(children)
|
||||
return condition
|
||||
|
||||
|
||||
for tag, default, peak in (
|
||||
("GSUB", 0, 1),
|
||||
("GPOS", 0, 1),
|
||||
("GSUB", 32767, 1),
|
||||
("GPOS", 32767, 1),
|
||||
("GSUB", 0, 0.75),
|
||||
("GPOS", 0, 0.75),
|
||||
):
|
||||
for legacy in (True, False):
|
||||
fb = FontBuilder(1000)
|
||||
order = [".notdef"] + [
|
||||
name for char in "ABCDEF" for name in (char, char + ".alt")
|
||||
]
|
||||
fb.setupGlyphOrder(order)
|
||||
fb.setupCharacterMap({ord(char): char for char in "ABCDEF"})
|
||||
fb.setupGlyf({name: TTGlyphPen(None).glyph() for name in order})
|
||||
fb.setupHorizontalMetrics({name: (600, 0) for name in order})
|
||||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
fb.setupOS2()
|
||||
fb.setupNameTable(
|
||||
{"familyName": "Layout condition precision", "styleName": "Regular"}
|
||||
)
|
||||
fb.setupPost()
|
||||
fb.setupFvar([(axis, -1, 0, 1, axis) for axis in ("TEST", "DUMY")], [])
|
||||
font = fb.font
|
||||
addOpenTypeFeaturesFromString(
|
||||
font,
|
||||
"""
|
||||
languagesystem DFLT dflt;
|
||||
lookup DefaultSub { sub A by A.alt; } DefaultSub;
|
||||
lookup ConditionalSub { sub B by B.alt; } ConditionalSub;
|
||||
feature liga { lookup DefaultSub; } liga;
|
||||
feature zzzz { lookup ConditionalSub; } zzzz;
|
||||
lookup DefaultPos { pos [A A.alt] 10; } DefaultPos;
|
||||
lookup ConditionalPos { pos B 20; } ConditionalPos;
|
||||
feature kern { lookup DefaultPos; } kern;
|
||||
feature zzzz { lookup ConditionalPos; } zzzz;
|
||||
""",
|
||||
)
|
||||
gdef = font["GDEF"] = newTable("GDEF")
|
||||
gdef.table = ot.GDEF()
|
||||
gdef.table.Version = 0x10003
|
||||
for field in (
|
||||
"GlyphClassDef",
|
||||
"AttachList",
|
||||
"LigCaretList",
|
||||
"MarkAttachClassDef",
|
||||
"MarkGlyphSetsDef",
|
||||
):
|
||||
setattr(gdef.table, field, None)
|
||||
gdef.table.VarStore = builder.buildVarStore(
|
||||
builder.buildVarRegionList(
|
||||
[
|
||||
{"TEST": (0, peak, 1)},
|
||||
{"DUMY": (0, 1, 1)},
|
||||
{"TEST": (0, peak, 1), "DUMY": (0, 1, 1)},
|
||||
],
|
||||
["TEST", "DUMY"],
|
||||
),
|
||||
[
|
||||
builder.buildVarData(
|
||||
[0, 1, 2], [[1, -32768 if default else -1, 0], [1, 0, -1]]
|
||||
)
|
||||
],
|
||||
)
|
||||
position = font["GPOS"].table.LookupList.Lookup[0].SubTable[0]
|
||||
position.Value.XPlaDevice = builder.buildVarDevTable(0)
|
||||
position.ValueFormat |= 0x10
|
||||
condition = compound(
|
||||
3,
|
||||
[
|
||||
compound(4, [value(default, 0), value(0)]),
|
||||
compound(5, compound(5, value(0, 1))),
|
||||
],
|
||||
)
|
||||
table = font[tag].table
|
||||
feature = next(
|
||||
i
|
||||
for i, rec in enumerate(table.FeatureList.FeatureRecord)
|
||||
if rec.FeatureTag == ("liga" if tag == "GSUB" else "kern")
|
||||
)
|
||||
variations = table.FeatureVariations = ot.FeatureVariations()
|
||||
table.Version = 0x10001
|
||||
variations.Version = 0x10000 if legacy else 0x10001
|
||||
variations.FeatureVariationRecord = []
|
||||
variations.FeatureVariationCount = 0
|
||||
if legacy:
|
||||
variations.FeatureVariationRecord = [
|
||||
featureVars.buildFeatureVariationRecord(
|
||||
[condition],
|
||||
[featureVars.buildFeatureTableSubstitutionRecord(feature, [1])],
|
||||
)
|
||||
]
|
||||
variations.FeatureVariationCount = 1
|
||||
else:
|
||||
record = ot.LookupVariationRecord()
|
||||
record.FeatureIndex = feature
|
||||
lookups = record.FeatureLookupsTable = ot.FeatureLookupsTable()
|
||||
lookups.Version, lookups.Flags = 0x10000, 1
|
||||
conditional = ot.LookupConditionRecord()
|
||||
conditional.ConditionTable = condition
|
||||
conditional.LookupIndexList = ot.LookupIndexList()
|
||||
conditional.LookupIndexList.LookupIndex = [1]
|
||||
conditional.LookupIndexList.LookupIndexCount = 1
|
||||
lookups.LookupConditionRecord = [conditional]
|
||||
lookups.LookupConditionCount = 1
|
||||
variations.LookupVariationRecord = [record]
|
||||
variations.LookupVariationCount = 1
|
||||
font["head"].created = font["head"].modified = 3800000000
|
||||
font.recalcTimestamp = False
|
||||
suffix = "-large" if default else "-third" if peak != 1 else ""
|
||||
font.save(
|
||||
Path(__file__).parent
|
||||
/ f"feature-variation-precision-{tag}-{'legacy' if legacy else 'lookup'}{suffix}.ttf"
|
||||
)
|
||||
@@ -228,6 +228,8 @@ assert_condition_instance_matches (hb_face_t *face, hb_face_t *subset,
|
||||
{
|
||||
g_assert_cmpuint (subset_info[i].codepoint, ==, info[i].codepoint);
|
||||
g_assert_cmpint (subset_pos[i].x_advance, ==, pos[i].x_advance);
|
||||
/* Numeric positioning deltas can round by one unit during instancing. */
|
||||
g_assert_cmpint (abs (subset_pos[i].x_offset - pos[i].x_offset), <=, 1);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < 2; i++)
|
||||
@@ -295,6 +297,64 @@ test_subset_lookup_variation_fractional_value (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_condition_value_precision (void)
|
||||
{
|
||||
const char *filenames[] = {
|
||||
"fonts/feature-variation-precision-GSUB-legacy.ttf",
|
||||
"fonts/feature-variation-precision-GSUB-lookup.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-legacy.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-lookup.ttf",
|
||||
"fonts/feature-variation-precision-GSUB-legacy-large.ttf",
|
||||
"fonts/feature-variation-precision-GSUB-lookup-large.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-legacy-large.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-lookup-large.ttf",
|
||||
"fonts/feature-variation-precision-GSUB-legacy-third.ttf",
|
||||
"fonts/feature-variation-precision-GSUB-lookup-third.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-legacy-third.ttf",
|
||||
"fonts/feature-variation-precision-GPOS-lookup-third.ttf"
|
||||
};
|
||||
const hb_tag_t tags[] = {HB_TAG ('T','E','S','T'), HB_TAG ('D','U','M','Y')};
|
||||
const int locations[] = {-8192, 0, 2048, 4096, 5461, 5462,
|
||||
8192, 12288, 16383, 16384};
|
||||
for (unsigned f = 0; f < G_N_ELEMENTS (filenames); f++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file (filenames[f]);
|
||||
for (unsigned pinned = 0; pinned < 2; pinned++)
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (locations); i++)
|
||||
{
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS);
|
||||
hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'F');
|
||||
g_assert_true (hb_subset_input_pin_axis_location (
|
||||
input, face, tags[pinned], locations[i] / 16384.f));
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
g_assert_nonnull (subset);
|
||||
for (unsigned j = 0; j < G_N_ELEMENTS (locations); j++)
|
||||
{
|
||||
int coords[] = {0, 0};
|
||||
coords[pinned] = locations[i];
|
||||
coords[1 - pinned] = locations[j];
|
||||
assert_condition_instance_matches (face, subset, coords, &locations[j], 1);
|
||||
/* A private bias region must survive instancing the font again. */
|
||||
hb_subset_input_t *static_input = hb_subset_input_create_or_fail ();
|
||||
hb_subset_input_set_flags (static_input, HB_SUBSET_FLAGS_RETAIN_GIDS);
|
||||
hb_set_add_range (hb_subset_input_unicode_set (static_input), 'A', 'F');
|
||||
g_assert_true (hb_subset_input_pin_axis_location (
|
||||
static_input, subset, tags[1 - pinned], locations[j] / 16384.f));
|
||||
hb_face_t *static_subset = hb_subset_or_fail (subset, static_input);
|
||||
g_assert_nonnull (static_subset);
|
||||
assert_condition_instance_matches (face, static_subset, coords, NULL, 0);
|
||||
hb_face_destroy (static_subset);
|
||||
hb_subset_input_destroy (static_input);
|
||||
}
|
||||
hb_face_destroy (subset);
|
||||
hb_subset_input_destroy (input);
|
||||
}
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_feature_variation_conditions (void)
|
||||
{
|
||||
@@ -899,6 +959,7 @@ main (int argc, char **argv)
|
||||
#ifndef HB_NO_VAR
|
||||
hb_test_add (test_subset_lookup_variations);
|
||||
hb_test_add (test_subset_lookup_variation_fractional_value);
|
||||
hb_test_add (test_subset_condition_value_precision);
|
||||
hb_test_add (test_subset_feature_variation_conditions);
|
||||
hb_test_add (test_subset_feature_variation_universal);
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user