mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-29 09:34:05 +01:00
[VARC] Preserve precision when accumulating 32-bit deltas (#6314)
Packed VARC deltas can use the full signed 32-bit range. Converting them to float before scaling and accumulation loses small residuals when large contributions cancel, affecting conditions, transforms, and component axis values. Accumulate MultiItemVariationStore deltas in double and keep condition and axis values at that precision until comparison or rounding. Leave region scalars, drawing transforms, and ordinary variation stores unchanged. Add a generated regression font covering all three consumers. Tests: meson test -C build (281 passed, 1 skipped); C++11 syntax checks with default, HB_NO_VAR, and HB_OPTIMIZE_SIZE configurations. Assisted-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
1 parent
95ba917215
commit
c042c00839
7 files changed
+144
-22
No files matched your search
@@ -32,7 +32,7 @@ bool
|
||||
VarComponent::decompile_record (const VARC &varc,
|
||||
hb_ubytes_t total_record,
|
||||
hb_vector_t<unsigned> *axis_indices,
|
||||
hb_vector_t<float> *axis_values,
|
||||
hb_vector_t<double> *axis_values,
|
||||
record_t *decoded,
|
||||
int64_t *budget)
|
||||
{
|
||||
@@ -403,18 +403,18 @@ VarComponent::get_path_at (const hb_varc_context_t &c,
|
||||
return hb_ubytes_t ();
|
||||
coord_setter_t coord_setter (axisIndices ? component_coords : hb_array<int> ());
|
||||
for (unsigned i = 0; i < axisIndices.length; i++)
|
||||
coord_setter[axisIndices[i]] = hb_clamp_to<int> (roundf (axisValues[i]));
|
||||
coord_setter[axisIndices[i]] = hb_clamp_to<int> (round (axisValues[i]));
|
||||
if (axisIndices)
|
||||
component_coords = coord_setter.get_coords ();
|
||||
|
||||
// Apply transform variations if any
|
||||
if (transformVarIdx != VarIdx::NO_VARIATION && coords)
|
||||
{
|
||||
float transformValues[9];
|
||||
double transformValues[9];
|
||||
unsigned numTransformValues = 0;
|
||||
#define PROCESS_TRANSFORM_COMPONENT(shift, type, flag, name) \
|
||||
if (flags & (unsigned) flags_t::flag) \
|
||||
transformValues[numTransformValues++] = transform.name;
|
||||
transformValues[numTransformValues++] = (double) transform.name;
|
||||
VARC_PROCESS_TRANSFORM_COMPONENTS;
|
||||
#undef PROCESS_TRANSFORM_COMPONENT
|
||||
if (unlikely (!hb_budget_spend (c.budget, numTransformValues,
|
||||
@@ -424,7 +424,7 @@ VarComponent::get_path_at (const hb_varc_context_t &c,
|
||||
numTransformValues = 0;
|
||||
#define PROCESS_TRANSFORM_COMPONENT(shift, type, flag, name) \
|
||||
if (flags & (unsigned) flags_t::flag) \
|
||||
transform.name = transformValues[numTransformValues++];
|
||||
transform.name = (float) transformValues[numTransformValues++];
|
||||
VARC_PROCESS_TRANSFORM_COMPONENTS;
|
||||
#undef PROCESS_TRANSFORM_COMPONENT
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace OT {
|
||||
struct hb_varc_scratch_t
|
||||
{
|
||||
hb_vector_t<unsigned> axisIndices;
|
||||
hb_vector_t<float> axisValues;
|
||||
hb_vector_t<double> axisValues;
|
||||
hb_glyf_scratch_t glyf_scratch;
|
||||
};
|
||||
|
||||
@@ -101,7 +101,7 @@ struct VarComponent
|
||||
HB_INTERNAL static bool decompile_record (const VARC &varc,
|
||||
hb_ubytes_t record,
|
||||
hb_vector_t<unsigned> *axis_indices,
|
||||
hb_vector_t<float> *axis_values,
|
||||
hb_vector_t<double> *axis_values,
|
||||
record_t *decoded /* OUT */,
|
||||
int64_t *budget = nullptr);
|
||||
};
|
||||
|
||||
+6
-5
@@ -2008,11 +2008,11 @@ struct TupleValues
|
||||
}
|
||||
|
||||
private:
|
||||
template <bool scaled>
|
||||
void _add_to (hb_array_t<float> out, float scale = 1.0f)
|
||||
template <bool scaled, typename T>
|
||||
void _add_to (hb_array_t<T> out, T scale = T (1))
|
||||
{
|
||||
unsigned n = out.length;
|
||||
float *arrayZ = out.arrayZ;
|
||||
T *arrayZ = out.arrayZ;
|
||||
|
||||
for (unsigned i = 0; i < n;)
|
||||
{
|
||||
@@ -2080,7 +2080,8 @@ struct TupleValues
|
||||
}
|
||||
|
||||
public:
|
||||
void add_to (hb_array_t<float> out, float scale = 1.0f)
|
||||
template <typename T>
|
||||
void add_to (hb_array_t<T> out, float scale = 1.0f)
|
||||
{
|
||||
#ifndef HB_OPTIMIZE_SIZE
|
||||
// The following branch is supposed to speed things up by avoiding
|
||||
@@ -2090,7 +2091,7 @@ struct TupleValues
|
||||
_add_to<false> (out);
|
||||
else
|
||||
#endif
|
||||
_add_to<true> (out, scale);
|
||||
_add_to<true> (out, (T) scale);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
+10
-10
@@ -3404,7 +3404,7 @@ struct MultiVarData
|
||||
void get_delta (unsigned int inner,
|
||||
const int *coords, unsigned int coord_count,
|
||||
const SparseVarRegionList ®ions,
|
||||
hb_array_t<float> out,
|
||||
hb_array_t<double> out,
|
||||
hb_scalar_cache_t *cache = nullptr) const
|
||||
{
|
||||
auto &deltaSets = get_delta_sets ();
|
||||
@@ -3827,7 +3827,7 @@ struct MultiItemVariationStore
|
||||
private:
|
||||
void get_delta (unsigned int outer, unsigned int inner,
|
||||
const int *coords, unsigned int coord_count,
|
||||
hb_array_t<float> out,
|
||||
hb_array_t<double> out,
|
||||
hb_scalar_cache_t *cache = nullptr) const
|
||||
{
|
||||
#ifdef HB_NO_VAR
|
||||
@@ -3847,7 +3847,7 @@ struct MultiItemVariationStore
|
||||
public:
|
||||
void get_delta (unsigned int index,
|
||||
const int *coords, unsigned int coord_count,
|
||||
hb_array_t<float> out,
|
||||
hb_array_t<double> out,
|
||||
hb_scalar_cache_t *cache = nullptr) const
|
||||
{
|
||||
unsigned int outer = index >> 16;
|
||||
@@ -3856,7 +3856,7 @@ struct MultiItemVariationStore
|
||||
}
|
||||
void get_delta (unsigned int index,
|
||||
hb_array_t<const int> coords,
|
||||
hb_array_t<float> out,
|
||||
hb_array_t<double> out,
|
||||
hb_scalar_cache_t *cache = nullptr) const
|
||||
{
|
||||
return get_delta (index,
|
||||
@@ -4203,14 +4203,14 @@ struct MultiItemVarStoreInstancer
|
||||
|
||||
operator bool () const { return varStore && bool (coords); }
|
||||
|
||||
float operator[] (uint32_t varIdx) const
|
||||
double operator[] (uint32_t varIdx) const
|
||||
{
|
||||
float v = 0;
|
||||
double v = 0;
|
||||
(*this) (hb_array (&v, 1), varIdx);
|
||||
return v;
|
||||
}
|
||||
|
||||
void operator() (hb_array_t<float> out, uint32_t varIdx, unsigned short offset = 0) const
|
||||
void operator() (hb_array_t<double> out, uint32_t varIdx, unsigned short offset = 0) const
|
||||
{
|
||||
if (coords && varIdx != VarIdx::NO_VARIATION)
|
||||
{
|
||||
@@ -4221,7 +4221,7 @@ struct MultiItemVarStoreInstancer
|
||||
}
|
||||
else
|
||||
for (unsigned i = 0; i < out.length; i++)
|
||||
out.arrayZ[i] = 0.f;
|
||||
out.arrayZ[i] = 0.;
|
||||
}
|
||||
|
||||
const MultiItemVariationStore *varStore;
|
||||
@@ -4407,8 +4407,8 @@ struct ConditionValue
|
||||
bool evaluate (const int *coords, unsigned int coord_len,
|
||||
Instancer *instancer) const
|
||||
{
|
||||
float value = defaultValue;
|
||||
value += (*instancer)[varIdx];
|
||||
double value = defaultValue;
|
||||
value += (double) (*instancer)[varIdx];
|
||||
return value > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
"""Generate the VARC delta-precision regression font using current FontTools."""
|
||||
|
||||
import sys
|
||||
|
||||
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.ttLib.tables.TupleVariation import TupleVariation
|
||||
from fontTools.varLib.builder import (
|
||||
buildMultiVarData,
|
||||
buildMultiVarStore,
|
||||
buildSparseVarRegionList,
|
||||
)
|
||||
|
||||
|
||||
def build():
|
||||
order = [".notdef", "leaf", "conditional", "translated", "axes"]
|
||||
fb = FontBuilder(1000)
|
||||
fb.setupGlyphOrder(order)
|
||||
fb.setupCharacterMap({65: "conditional", 66: "translated", 67: "axes"})
|
||||
glyphs = {name: TTGlyphPen(None).glyph() for name in order}
|
||||
pen = TTGlyphPen(None)
|
||||
pen.moveTo((100, 0))
|
||||
pen.lineTo((200, 0))
|
||||
pen.lineTo((100, 100))
|
||||
pen.closePath()
|
||||
glyphs["leaf"] = pen.glyph()
|
||||
fb.setupGlyf(glyphs)
|
||||
fb.setupHorizontalMetrics(
|
||||
{name: (500, 100 if name == "leaf" else 0) for name in order}
|
||||
)
|
||||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
fb.setupNameTable({"familyName": "VARC delta precision", "styleName": "Regular"})
|
||||
fb.setupOS2()
|
||||
fb.setupPost()
|
||||
fb.setupFvar([("TEST", 0, 0, 1, "Test"), ("LEAF", 0, 0, 1, "Leaf")], [])
|
||||
fb.setupGvar(
|
||||
{
|
||||
"leaf": [
|
||||
TupleVariation(
|
||||
{"LEAF": (0, 1, 1)}, [(16384, 0)] * 3 + [(0, 0)] * 4
|
||||
)
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
varc = ot.VARC()
|
||||
varc.Version = 0x10000
|
||||
varc.Coverage = ot.Coverage()
|
||||
varc.Coverage.glyphs = order[2:]
|
||||
data = buildMultiVarData([0, 1], [])
|
||||
data.Item = [
|
||||
[16777217, -16777216], # condition: +1 at TEST=1
|
||||
[2147483647, 0, 0, -2147483583, 0, 0], # translation: +64
|
||||
[16777217, -16777216], # axis override: +1 in F2DOT14 units
|
||||
]
|
||||
varc.MultiVarStore = buildMultiVarStore(
|
||||
buildSparseVarRegionList([{"TEST": (0, 1, 1)}] * 2, ["TEST", "LEAF"]),
|
||||
[data],
|
||||
)
|
||||
condition = ot.ConditionTable()
|
||||
condition.Format, condition.DefaultValue, condition.VarIdx = 2, 0, 0
|
||||
varc.ConditionList = ot.ConditionList()
|
||||
varc.ConditionList.ConditionTable = [condition]
|
||||
varc.AxisIndicesList = ot.AxisIndicesList()
|
||||
varc.AxisIndicesList.Item = [(1,)]
|
||||
varc.VarCompositeGlyphs = ot.VarCompositeGlyphs()
|
||||
varc.VarCompositeGlyphs.VarCompositeGlyph = []
|
||||
for name in order[2:]:
|
||||
component = ot.VarComponent()
|
||||
component.glyphName = "leaf"
|
||||
if name == "axes":
|
||||
component.axisIndicesIndex = 0
|
||||
component.axisValues = (0.0,)
|
||||
component.axisValuesVarIndex = 2
|
||||
else:
|
||||
component.flags = int(
|
||||
ot.VarComponentFlags.HAVE_TRANSLATE_X
|
||||
| ot.VarComponentFlags.HAVE_SCALE_X
|
||||
| ot.VarComponentFlags.HAVE_SCALE_Y
|
||||
)
|
||||
component.transform.translateX = 400
|
||||
component.transform.scaleX = component.transform.scaleY = 2
|
||||
if name == "conditional":
|
||||
component.conditionIndex = 0
|
||||
else:
|
||||
component.transformVarIndex = 1
|
||||
glyph = ot.VarCompositeGlyph()
|
||||
glyph.components = [component]
|
||||
varc.VarCompositeGlyphs.VarCompositeGlyph.append(glyph)
|
||||
fb.font["VARC"] = newTable("VARC")
|
||||
fb.font["VARC"].table = varc
|
||||
return fb.font
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
font = build()
|
||||
for path in sys.argv[1:]:
|
||||
font.save(path)
|
||||
Binary file not shown.
@@ -360,6 +360,26 @@ test_hb_draw_varc_short_cff2 (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_hb_draw_varc_delta_precision (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/varc-delta-precision.ttf");
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_face_destroy (face);
|
||||
hb_variation_t variation = { HB_TAG ('T','E','S','T'), 1.f };
|
||||
hb_font_set_variations (font, &variation, 1);
|
||||
|
||||
const float first_moves[] = {600.f, 664.f, 101.f};
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (first_moves); i++)
|
||||
{
|
||||
draw_data_t draw_data = {0};
|
||||
hb_font_draw_glyph (font, i + 2, funcs, &draw_data);
|
||||
g_assert_cmpuint (draw_data.move_to_count, ==, 1);
|
||||
g_assert_cmpfloat (draw_data.first_move_x, ==, first_moves[i]);
|
||||
}
|
||||
hb_font_destroy (font);
|
||||
}
|
||||
|
||||
static void
|
||||
test_hb_draw_varc_budget (void)
|
||||
{
|
||||
@@ -418,6 +438,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_hb_draw_varc_conditional);
|
||||
hb_test_add (test_hb_draw_varc_static_gvar);
|
||||
hb_test_add (test_hb_draw_varc_short_cff2);
|
||||
hb_test_add (test_hb_draw_varc_delta_precision);
|
||||
hb_test_add (test_hb_draw_varc_budget);
|
||||
#endif
|
||||
unsigned result = hb_test_run ();
|
||||
|
||||
Reference in new issue
Block a user