diff --git a/src/hb-subset-plan-layout.cc b/src/hb-subset-plan-layout.cc index 06f0c055e..7c6fd4f8a 100644 --- a/src/hb-subset-plan-layout.cc +++ b/src/hb-subset-plan-layout.cc @@ -564,8 +564,7 @@ collect_layout_variation_indices (hb_subset_plan_t* plan) plan->all_axes_pinned, plan->layout_variation_idx_delta_map); - unsigned subtable_count = gdef->get_var_store ().get_sub_table_count (); - generate_varstore_inner_maps (varidx_set, subtable_count, plan->gdef_varstore_inner_maps); + generate_varstore_inner_maps (gdef->get_var_store (), varidx_set, plan->gdef_varstore_inner_maps); plan->check_success (!plan->layout_condition_values.in_error ()); } #endif diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 446069178..72216e2c0 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -36,11 +36,13 @@ #ifndef HB_NO_VAR + template void - generate_varstore_inner_maps (const hb_set_t& varidx_set, - unsigned subtable_count, + generate_varstore_inner_maps (const ItemVarStore &var_store, + const hb_set_t& varidx_set, hb_vector_t &inner_maps /* OUT */) { + unsigned subtable_count = var_store.get_sub_table_count (); if (varidx_set.is_empty () || subtable_count == 0) return; if (unlikely (!inner_maps.resize (subtable_count))) return; @@ -49,12 +51,20 @@ uint16_t major = idx >> 16; uint16_t minor = idx & 0xFFFF; - if (major >= subtable_count) + /* Runtime treats out-of-range delta-set indices as zero; don't + * retain rows the store doesn't have. */ + if (!var_store.has_delta_set (idx)) continue; inner_maps[major].add (minor); } } + template + void + generate_varstore_inner_maps (const OT::ItemVariationStore &var_store, + const hb_set_t& varidx_set, + hb_vector_t &inner_maps /* OUT */); + /* Evaluate a variation-region tent at v, mirroring VarRegionAxis::evaluate * (invalid regions evaluate as constant 1). */ static double @@ -392,7 +402,7 @@ auto *store_cache = var_store.create_cache (); unsigned new_major = 0, new_minor = 0; - unsigned last_major = (variation_indices.get_min ()) >> 16; + unsigned last_major = (unsigned) -1; for (unsigned idx : variation_indices) { int delta = 0; @@ -408,7 +418,15 @@ uint16_t major = idx >> 16; if (major >= subtable_count) break; - if (major != last_major) + /* Keep in sync with generate_varstore_inner_maps (): rows the store + * doesn't have are not retained, so such an index keeps its runtime + * meaning (no delta) instead of consuming a new row. */ + if (!var_store.has_delta_set (idx)) + { + variation_idx_delta_map.set (idx, hb_pair_t (HB_OT_LAYOUT_NO_VARIATIONS_INDEX, delta)); + continue; + } + if (last_major != (unsigned) -1 && major != last_major) { new_minor = 0; ++new_major; @@ -444,15 +462,13 @@ hb_set_t varidx_set; base->collect_variation_indices (plan, varidx_set); const OT::ItemVariationStore &var_store = base->get_var_store (); - unsigned subtable_count = var_store.get_sub_table_count (); - remap_variation_indices (var_store, varidx_set, plan->normalized_coords, !plan->pinned_at_default, plan->all_axes_pinned, plan->base_variation_idx_map); - generate_varstore_inner_maps (varidx_set, subtable_count, plan->base_varstore_inner_maps); + generate_varstore_inner_maps (var_store, varidx_set, plan->base_varstore_inner_maps); } #endif diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index efe1f1383..0a3aed9fb 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -147,8 +147,7 @@ static void _colr_closure (hb_subset_plan_t* plan, { const OT::ItemVariationStore &var_store = colr.get_var_store (); // generated inner_maps is used by ItemVariationStore serialize(), which is subset only - unsigned subtable_count = var_store.get_sub_table_count (); - generate_varstore_inner_maps (variation_indices, subtable_count, plan->colrv1_varstore_inner_maps); + generate_varstore_inner_maps (var_store, variation_indices, plan->colrv1_varstore_inner_maps); if (!plan->all_axes_pinned) remap_variation_indices (var_store, diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index bb7c95275..3c7356caf 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -382,9 +382,10 @@ remap_colrv1_delta_set_index_indices (const DeltaSetIndexMap &index_map, hb_map_t &new_deltaset_idx_varidx_map /* OUT */); +template HB_INTERNAL void -generate_varstore_inner_maps (const hb_set_t& varidx_set, - unsigned subtable_count, +generate_varstore_inner_maps (const ItemVarStore &var_store, + const hb_set_t& varidx_set, hb_vector_t &inner_maps /* OUT */); HB_INTERNAL bool diff --git a/test/api/fonts/colr-phantom-rows.ttf b/test/api/fonts/colr-phantom-rows.ttf new file mode 100644 index 000000000..4c2795e90 Binary files /dev/null and b/test/api/fonts/colr-phantom-rows.ttf differ diff --git a/test/api/fonts/generate-colr-phantom-rows-font.py b/test/api/fonts/generate-colr-phantom-rows-font.py new file mode 100644 index 000000000..0a625e1b1 --- /dev/null +++ b/test/api/fonts/generate-colr-phantom-rows-font.py @@ -0,0 +1,44 @@ +"""Generate a COLRv1 font whose colour stops name variation rows that the +ItemVariationStore does not have.""" + +from pathlib import Path +from fontTools.fontBuilder import FontBuilder +from fontTools.colorLib.builder import buildCOLR, buildCPAL +from fontTools.pens.ttGlyphPen import TTGlyphPen +from fontTools.varLib.builder import buildVarRegionList, buildVarData, buildVarStore + +fb = FontBuilder(1000, isTTF=True) +names = [".notdef", "linear"] +fb.setupGlyphOrder(names) +fb.setupCharacterMap({0x61: "linear"}) +fb.setupGlyf({glyph: TTGlyphPen(None).glyph() for glyph in names}) +fb.setupHorizontalMetrics({glyph: (1000, 0) for glyph in names}) +fb.setupHorizontalHeader(ascent=800, descent=-200) +fb.setupNameTable({"familyName": "COLR Phantom Rows", "styleName": "Regular"}) +fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200) +fb.setupPost() +fb.setupFvar([("wght", 0, 0, 1, "Weight")], []) +# The store has two rows (0 and 1). Stop 0 uses them; the other stops name +# rows 2..5, which runtime evaluates as zero deltas. +paint = dict( + Format=5, + ColorLine=dict( + Extend="pad", + ColorStop=[ + dict(StopOffset=0, PaletteIndex=0, Alpha=1, VarIndexBase=0), + dict(StopOffset=0.5, PaletteIndex=1, Alpha=1, VarIndexBase=2), + dict(StopOffset=1, PaletteIndex=0, Alpha=1, VarIndexBase=4), + ], + ), + x0=0, y0=0, x1=100, y1=0, x2=0, y2=100, VarIndexBase=0xFFFFFFFF, +) +regions = buildVarRegionList([{"wght": (0, 1, 1)}], ["wght"]) +store = buildVarStore(regions, [buildVarData([0], [[1024], [-2048]], optimize=False)]) +fb.font["COLR"] = buildCOLR( + {"linear": paint}, version=1, glyphMap=fb.font.getReverseGlyphMap(), + varStore=store, clipBoxes={"linear": (0, 0, 100, 100)}, +) +fb.font["CPAL"] = buildCPAL([[(0, 0, 0, 1), (1, 1, 1, 1)]]) +fb.font.recalcTimestamp = False +fb.font["head"].created = fb.font["head"].modified = 2082844800 +fb.save(Path(__file__).parent / "colr-phantom-rows.ttf") diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index 4e38d6e9d..c80fc6db8 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -876,6 +876,74 @@ test_subset_colr_shared_layers (void) hb_face_destroy (face); } +static void +check_colr_phantom_rows (hb_face_t *face, bool variable) +{ + hb_blob_t *blob = hb_face_reference_table (face, HB_TAG ('C','O','L','R')); + unsigned length; + const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length); + g_assert_cmpuint (length, >=, 34); + unsigned list = read_uint_be (data + 14, 4); + g_assert_cmpuint (list + 10, <=, length); + g_assert_cmpuint (read_uint_be (data + list, 4), ==, 1); + unsigned paint = list + read_uint_be (data + list + 6, 4); + g_assert_cmpuint (paint + 16, <=, length); + g_assert_cmpuint (data[paint], ==, variable ? 5 : 4); + unsigned line = paint + read_uint_be (data + paint + 1, 3); + unsigned stride = variable ? 10 : 6; + g_assert_cmpuint (line + 3 + 3 * stride, <=, length); + g_assert_cmpuint (read_uint_be (data + line + 1, 2), ==, 3); + for (unsigned stop = 0; stop < 3; stop++) + { + unsigned at = line + 3 + stop * stride; + /* Only the first stop names rows the store has; the others keep + * their runtime meaning of no variation. */ + unsigned offset = stop * 8192, alpha = 16384; + if (!variable && !stop) + { + offset = 1024; + alpha = 14336; + } + g_assert_cmpuint (read_uint_be (data + at, 2), ==, offset); + g_assert_cmpuint (read_uint_be (data + at + 2, 2), ==, stop % 2); + g_assert_cmpuint (read_uint_be (data + at + 4, 2), ==, alpha); + if (variable) + g_assert_cmpuint (read_uint_be (data + at + 6, 4), ==, stop ? 0xFFFFFFFFu : 0); + } + unsigned store = read_uint_be (data + 30, 4); + if (!variable) + g_assert_cmpuint (store, ==, 0); + else + { + g_assert_cmpuint (store + 12, <=, length); + g_assert_cmpuint (read_uint_be (data + store + 6, 2), ==, 1); + unsigned var_data = store + read_uint_be (data + store + 8, 4); + g_assert_cmpuint (var_data + 6, <=, length); + /* No rows are invented for the stops the source store doesn't cover. */ + g_assert_cmpuint (read_uint_be (data + var_data, 2), ==, 2); + } + hb_blob_destroy (blob); +} + +static void +test_subset_colr_phantom_rows (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/colr-phantom-rows.ttf"); + for (unsigned mode = 0; mode < 3; mode++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, 1); + if (mode == 1) + g_assert_true (hb_subset_input_set_axis_range (input, face, HB_TAG ('w','g','h','t'), 0, 0.5f, 0)); + if (mode == 2) + g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w','g','h','t'), 1)); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + check_colr_phantom_rows (subset, mode != 2); + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -896,6 +964,7 @@ main (int argc, char **argv) hb_test_add (test_subset_colr_var_overflow); hb_test_add (test_subset_colr_layers_out_of_range); hb_test_add (test_subset_colr_shared_layers); + hb_test_add (test_subset_colr_phantom_rows); return hb_test_run(); }