From 2b3562ac11ebb3b5f30e73accaeacaa1952edf09 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 3 Jul 2026 16:35:11 -0600 Subject: [PATCH] [subset] Privatize shared avar2 varIdx rows before offset compensation avar2's VarIdxMap may map several fvar axes to the same IVS delta row (Amstelvar does: GRAD+XOPQ, and YOPQ+YTLC+YTUC). The offset compensation code wrote a restricted or pinned axis's bias and tent deltas straight into its delta row, corrupting every other axis that reads the same row -- a free axis sharing a row with a restricted one could be off by up to full scale. Give each offset-receiving axis whose row is shared a private copy of the row (identical contents, preserving the rebased deltas) via the new item_variations_t::duplicate_row(), and repoint its varIdx before the offset loop. Sharers keep the clean row; the varstore optimization pass re-merges identical rows afterwards. Ports fonttools commit 0c0efb8ea from its avar2-instancing branch. Verified with a fontTools spec-pipeline differential oracle: the corruption (up to 8193 F2Dot14 units on a free axis sharing a row with a restricted one) is gone. A regression test follows in a later commit. Co-Authored-By: Claude Opus 4.8 --- src/hb-ot-var-avar-table.hh | 40 +++++++++++++++++++++++++++++++++++++ src/hb-ot-var-common.hh | 28 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/hb-ot-var-avar-table.hh b/src/hb-ot-var-avar-table.hh index a3d1a19fb..dbd5d61d8 100644 --- a/src/hb-ot-var-avar-table.hh +++ b/src/hb-ot-var-avar-table.hh @@ -614,6 +614,46 @@ struct avar for (unsigned i = 0; i < axisCount; i++) new_varidx_mapping[i] = varidx_map.map (i); + /* 5.5. Privatize shared varIdx delta rows before adding offset + * compensation. avar2's VarIdxMap may map several fvar axes to the SAME + * IVS delta row. Writing one axis's offset-compensation deltas into a + * shared row would corrupt every other axis that reads that row. So give + * each offset-receiving axis whose row is shared its own private copy of + * the row (identical contents, preserving the rebased deltas), then + * repoint its varIdx. Sharers keep the clean row; the varstore + * optimization pass re-merges identical rows afterwards. */ + hb_hashmap_t varidx_ref_count; + for (unsigned i = 0; i < axisCount; i++) + { + uint32_t varidx = new_varidx_mapping[i]; + if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) continue; + unsigned *count; + if (varidx_ref_count.has (varidx, &count)) + (*count)++; + else if (!varidx_ref_count.set (varidx, 1)) + return false; + } + for (unsigned i = 0; i < axisCount; i++) + { + hb_tag_t *axis_tag_ptr; + /* Only axes that will receive offset compensation (restricted or + * pinned) can contaminate a shared row. */ + if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag_ptr) || + !c->plan->user_axes_location.has (*axis_tag_ptr)) + continue; + uint32_t varidx = new_varidx_mapping[i]; + if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) + continue; /* gets a fresh, private VarData in the offset loop below */ + unsigned *count; + if (!varidx_ref_count.has (varidx, &count) || *count <= 1) + continue; /* sole owner: safe to write offsets in place */ + unsigned outer = varidx >> 16; + unsigned new_inner = item_vars.duplicate_row (outer, varidx & 0xFFFF); + if (unlikely (new_inner == (unsigned) -1)) return false; + new_varidx_mapping[i] = (outer << 16) | new_inner; + (*count)--; + } + /* 6. Add offset compensation tuples. * Track processed (outer,inner) pairs to avoid adding duplicate biases * when multiple axes share the same varIdx. */ diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 809671034..75b7565a0 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1832,6 +1832,34 @@ struct item_variations_t return false; } + /* Duplicate the delta row at position inner within VarData subtable outer, + * appending the copy as a new item. Used to give an axis a private copy of + * a delta row that avar2's VarIdxMap shares between several axes, before + * per-axis offset-compensation deltas are written into it. Returns the new + * inner index, or (unsigned) -1 on failure. */ + unsigned duplicate_row (unsigned outer, unsigned inner) + { + if (unlikely (outer >= vars.length || + inner >= var_data_num_rows[outer] || + var_data_num_rows[outer] >= 0xFFFFu)) + return (unsigned) -1; + for (tuple_delta_t& tuple : vars[outer].tuple_vars) + { + if (unlikely (inner >= tuple.deltas_x.length || + inner >= tuple.indices.length)) + return (unsigned) -1; + tuple.indices.push (tuple.indices.arrayZ[inner]); + tuple.deltas_x.push (tuple.deltas_x.arrayZ[inner]); + if (inner < tuple.deltas_y.length) + tuple.deltas_y.push (tuple.deltas_y.arrayZ[inner]); + if (unlikely (tuple.indices.in_error () || + tuple.deltas_x.in_error () || + tuple.deltas_y.in_error ())) + return (unsigned) -1; + } + return var_data_num_rows[outer]++; + } + /* Add a tuple with a single non-zero delta at position inner. * axis_tuples defines the region (empty map = constant/bias tuple). */ void add_tuple (unsigned outer,