[ot] Evaluate constant item variation regions at default coordinates

A zero-peak region ignores every axis and can contribute a nonzero
variation delta even when coordinates are absent or zero. Default
shortcuts skipped these deltas in HVAR/VVAR advances, vertical origins,
GPOS value records, anchors, and generic VariationDevice access.

Cache whether metrics and GDEF stores have regions active at the default
when their accelerators load. Preserve existing fast paths for ordinary
stores and evaluate constant-region stores at default coordinates.

Add a generated CFF2 font exercising constant and ordinary regions,
empty and short coordinate arrays, and returning to default after
non-default shaping. Skera and FontTools full instances give the same
metrics and positioning. All 254 configured Meson tests pass; core files
also compile with variation, vertical, and layout support disabled.

Assisted-by: Codex
This commit is contained in:
Behdad Esfahbod committed 2026-10-10 20:41:56 -06:00
1 parent d89fa94461
commit 5eb8323371
9 files changed
+153 -11

No files matched your search

+3
View File
@@ -985,6 +985,8 @@ struct GDEF
table = hb_blob_get_empty ();
}
has_default_variations = table->get_var_store ().has_default_regions ();
#ifndef HB_NO_GDEF_CACHE
/* Flatten each mark set whose glyphs span fewer than PAGE_BITS
* gids into a single bit-page biased at its first glyph, for
@@ -1086,6 +1088,7 @@ struct GDEF
}
hb_blob_ptr_t<GDEF> table;
bool has_default_variations;
#ifndef HB_NO_GDEF_CACHE
struct mark_glyph_set_bitmap_t
{
+3 -2
View File
@@ -37,12 +37,13 @@ struct AnchorFormat3
*x = font->em_fscale_x (xCoordinate);
*y = font->em_fscale_y (yCoordinate);
if ((font->x_ppem || font->has_nonzero_coords) && xDeviceTable.sanitize (&c->sanitizer, this))
bool use_variations = font->has_nonzero_coords || c->gdef_accel.has_default_variations;
if ((font->x_ppem || use_variations) && xDeviceTable.sanitize (&c->sanitizer, this))
{
hb_barrier ();
*x += (this+xDeviceTable).get_x_delta (font, c->var_store, c->var_store_cache);
}
if ((font->y_ppem || font->has_nonzero_coords) && yDeviceTable.sanitize (&c->sanitizer, this))
if ((font->y_ppem || use_variations) && yDeviceTable.sanitize (&c->sanitizer, this))
{
hb_barrier ();
*y += (this+yDeviceTable).get_y_delta (font, c->var_store, c->var_store_cache);
+3 -2
View File
@@ -120,8 +120,9 @@ struct ValueFormat : HBUINT16
if (!has_device ()) return ret;
bool use_x_device = font->x_ppem || font->has_nonzero_coords;
bool use_y_device = font->y_ppem || font->has_nonzero_coords;
bool use_variations = font->has_nonzero_coords || c->gdef_accel.has_default_variations;
bool use_x_device = font->x_ppem || use_variations;
bool use_y_device = font->y_ppem || use_variations;
if (!use_x_device && !use_y_device) return ret;
+5 -5
View File
@@ -406,7 +406,7 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
}
#ifndef HB_NO_VAR
if (!font->has_nonzero_coords)
if (!font->has_nonzero_coords && !hmtx.has_default_variations)
{
fallback:
#else
@@ -423,7 +423,7 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
}
#ifndef HB_NO_VAR
/* has_nonzero_coords. */
/* Non-default coordinates or constant variation regions. */
ot_font->check_serial (font);
hb_ot_font_advance_cache_t *advance_cache = ot_font->h.acquire_advance_cache ();
@@ -531,7 +531,7 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data,
}
#ifndef HB_NO_VAR
if (!font->has_nonzero_coords)
if (!font->has_nonzero_coords && !vmtx.has_default_variations)
{
fallback:
#else
@@ -548,7 +548,7 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data,
}
#ifndef HB_NO_VAR
/* has_nonzero_coords. */
/* Non-default coordinates or constant variation regions. */
ot_font->check_serial (font);
hb_ot_font_advance_cache_t *advance_cache = ot_font->v.acquire_advance_cache ();
@@ -668,7 +668,7 @@ hb_ot_get_glyph_v_origins (hb_font_t *font,
if (origin_cache && VORG.has_data ())
{
#ifndef HB_NO_VAR
if (!font->has_nonzero_coords)
if (!font->has_nonzero_coords && !ot_face->vmtx->has_default_variations)
#endif
{
for (unsigned i = 0; i < count; i++)
+4
View File
@@ -453,6 +453,7 @@ struct hmtx_accelerator_t : hb_dual_accelerator_t<hmtx::accelerator_t,
hb_dual_accelerator_t<hmtx::accelerator_t, HMTX::accelerator_t> (face)
{
var_table = hb_sanitize_context_t ().reference_table<HVAR> (face);
has_default_variations = var_table->get_var_store ().has_default_regions ();
}
~hmtx_accelerator_t ()
@@ -484,6 +485,7 @@ struct hmtx_accelerator_t : hb_dual_accelerator_t<hmtx::accelerator_t,
#endif
hb_blob_ptr_t<HVAR> var_table;
bool has_default_variations;
};
#ifndef HB_NO_VERTICAL
@@ -494,6 +496,7 @@ struct vmtx_accelerator_t : hb_dual_accelerator_t<vmtx::accelerator_t,
hb_dual_accelerator_t<vmtx::accelerator_t, VMTX::accelerator_t> (face)
{
var_table = hb_sanitize_context_t ().reference_table<VVAR> (face);
has_default_variations = var_table->get_var_store ().has_default_regions ();
}
~vmtx_accelerator_t ()
@@ -525,6 +528,7 @@ struct vmtx_accelerator_t : hb_dual_accelerator_t<vmtx::accelerator_t,
#endif
hb_blob_ptr_t<VVAR> var_table;
bool has_default_variations;
};
#else
struct vmtx_accelerator_t : vmtx::accelerator_t {
+13 -2
View File
@@ -3567,6 +3567,17 @@ struct ItemVariationStore
}
public:
bool has_default_regions () const
{
#ifdef HB_NO_VAR
return false;
#endif
const auto &region_list = this+regions;
for (unsigned i = 0; i < region_list.regionCount; i++)
if (region_list.evaluate (i, nullptr, 0)) return true;
return false;
}
bool has_delta_set (unsigned int index) const
{
#ifdef HB_NO_VAR
@@ -6184,12 +6195,12 @@ struct VariationDevice
hb_position_t get_x_delta (hb_font_t *font,
const ItemVariationStore &store,
hb_scalar_cache_t *store_cache = nullptr) const
{ return !font->has_nonzero_coords ? 0 : font->em_scalef_x (get_delta (font, store, store_cache)); }
{ return font->em_scalef_x (get_delta (font, store, store_cache)); }
hb_position_t get_y_delta (hb_font_t *font,
const ItemVariationStore &store,
hb_scalar_cache_t *store_cache = nullptr) const
{ return !font->has_nonzero_coords ? 0 : font->em_scalef_y (get_delta (font, store, store_cache)); }
{ return font->em_scalef_y (get_delta (font, store, store_cache)); }
VariationDevice* copy (hb_serialize_context_t *c,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map) const
Binary file not shown.
@@ -0,0 +1,78 @@
"""Generate constant HVAR/VVAR and GPOS regions at the default instance."""
from pathlib import Path
from copy import deepcopy
from fontTools.fontBuilder import FontBuilder
from fontTools.pens.t2CharStringPen import T2CharStringPen
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
from fontTools.ttLib import newTable
from fontTools.ttLib.tables import otTables as ot
from fontTools.varLib.builder import buildVarRegionList, buildVarData, buildVarStore
from fontTools.varLib.builder import buildVarDevTable, buildDeltaSetIndexMap
fb = FontBuilder(1000, isTTF=False)
names = [".notdef", "a", "acutecomb"]
fb.setupGlyphOrder(names)
fb.setupCharacterMap({0x61: "a", 0x301: "acutecomb"})
fb.setupHorizontalMetrics(
{name: (0 if name == "acutecomb" else 500, 0) for name in names}
)
fb.setupHorizontalHeader(ascent=800, descent=-200)
fb.setupVerticalMetrics({name: (1000, 0) for name in names})
fb.setupVerticalHeader(ascent=800, descent=-200)
fb.setupVerticalOrigins({name: 800 for name in names})
fb.setupNameTable({"familyName": "Default Item Variations", "styleName": "Regular"})
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200)
fb.setupPost()
fb.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], [])
pen = T2CharStringPen(None, None, CFF2=True)
fb.setupCFF2({name: pen.getCharString() for name in names})
regions = buildVarRegionList(
[{}, {"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}], ["wght", "wdth"]
)
for tag, delta in [("HVAR", 100), ("VVAR", 200)]:
table = newTable(tag)
table.table = getattr(ot, tag)()
table.table.Version = 0x00010000
table.table.VarStore = buildVarStore(
deepcopy(regions), [buildVarData([0, 1, 2], [[delta, 80, -40]] * len(names))]
)
if tag == "HVAR":
table.table.AdvWidthMap = table.table.LsbMap = table.table.RsbMap = None
else:
table.table.AdvHeightMap = table.table.TsbMap = table.table.BsbMap = None
table.table.VOrgMap = buildDeltaSetIndexMap([0] * len(names))
fb.font[tag] = table
addOpenTypeFeaturesFromString(
fb.font,
"feature kern { pos a <5 7 30 0>; } kern; "
"markClass acutecomb <anchor 0 0> @top; "
"feature mark { pos base a <anchor 300 700> mark @top; } mark;",
)
value = fb.font["GPOS"].table.LookupList.Lookup[0].SubTable[0].Value
value.XPlaDevice = buildVarDevTable(0)
value.YPlaDevice = buildVarDevTable(1)
value.XAdvDevice = buildVarDevTable(2)
fb.font["GPOS"].table.LookupList.Lookup[0].SubTable[0].ValueFormat |= 0x70
anchor = (
fb.font["GPOS"]
.table.LookupList.Lookup[1]
.SubTable[0]
.BaseArray.BaseRecord[0]
.BaseAnchor[0]
)
anchor.Format = 3
anchor.XDeviceTable = buildVarDevTable(0)
anchor.YDeviceTable = buildVarDevTable(1)
gdef = fb.font["GDEF"].table
gdef.Version = 0x00010003
gdef.GlyphClassDef = gdef.AttachList = gdef.LigCaretList = gdef.MarkAttachClassDef = (
None
)
gdef.MarkGlyphSetsDef = None
gdef.VarStore = buildVarStore(
regions, [buildVarData([0, 1, 2], [[11, 20, -10], [13, 20, -10], [40, 20, -10]])]
)
fb.font.recalcTimestamp = False
fb.font["head"].created = fb.font["head"].modified = 2082844800
fb.save(Path(__file__).parent / "default-item-variations.otf")
+44
View File
@@ -68,11 +68,55 @@ test_ot_metrics_get_var (void)
hb_face_destroy (face);
}
static void
test_ot_metrics_constant_regions (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/default-item-variations.otf");
hb_font_t *font = hb_font_create (face);
for (unsigned i = 0; i < 6; i++)
{
int coords[][2] = {{0, 0}, {0, 0}, {0, 0}, {8192, 0}, {0, 8192}, {0, 0}};
unsigned lengths[] = {0, 1, 2, 2, 2, 0};
hb_font_set_var_coords_normalized (font, coords[i], lengths[i]);
int delta = i == 3 ? 40 : i == 4 ? -20 : 0;
g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, 600 + delta);
g_assert_cmpint (hb_font_get_glyph_v_advance (font, 1), ==, -1200 - delta);
hb_position_t origin_x, origin_y;
g_assert_true (hb_font_get_glyph_v_origin (font, 1, &origin_x, &origin_y));
g_assert_cmpint (origin_x, ==, 300 + delta / 2);
g_assert_cmpint (origin_y, ==, 1000 + delta);
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, "a", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
hb_shape (font, buffer, NULL, 0);
unsigned count;
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count);
g_assert_cmpuint (count, ==, 1);
int positioning_delta = i == 3 ? 10 : i == 4 ? -5 : 0;
g_assert_cmpint (pos[0].x_advance, ==, 670 + delta + positioning_delta);
g_assert_cmpint (pos[0].x_offset, ==, 16 + positioning_delta);
g_assert_cmpint (pos[0].y_offset, ==, 20 + positioning_delta);
hb_buffer_reset (buffer);
hb_buffer_add_utf8 (buffer, "a\xCC\x81", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
hb_shape (font, buffer, NULL, 0);
pos = hb_buffer_get_glyph_positions (buffer, &count);
g_assert_cmpuint (count, ==, 2);
g_assert_cmpint (pos[1].x_advance, ==, 0);
g_assert_cmpint (pos[1].x_offset, ==, -343 - delta + positioning_delta);
g_assert_cmpint (pos[1].y_offset, ==, 733 + 2 * positioning_delta);
hb_buffer_destroy (buffer);
}
hb_font_destroy (font);
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_ot_metrics_get_no_var);
hb_test_add (test_ot_metrics_get_var);
hb_test_add (test_ot_metrics_constant_regions);
return hb_test_run ();
}