mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 14:31:10 +01:00
[subset] Preserve overflowing static COLRv1 scales
Assisted-by: Codex
This commit is contained in:
1 parent
d89fa94461
commit
3ffb7fe26c
4 files changed
+237
No files matched your search
@@ -1163,6 +1163,70 @@ struct PaintTranslate
|
||||
DEFINE_SIZE_STATIC (4 + 2 * FWORD::static_size);
|
||||
};
|
||||
|
||||
template <typename Source>
|
||||
static bool serialize_scale_transform (hb_subset_context_t *c,
|
||||
float sx, float sy, float dx, float dy,
|
||||
const Source &source)
|
||||
{
|
||||
auto *out = c->serializer->allocate_size<PaintTransform<NoVariable>> (7);
|
||||
if (unlikely (!out)) return false;
|
||||
out->format = 12;
|
||||
|
||||
c->serializer->push ();
|
||||
auto *transform = c->serializer->allocate_size<Affine2x3> (Affine2x3::static_size);
|
||||
if (unlikely (!transform))
|
||||
{
|
||||
c->serializer->pop_discard ();
|
||||
return false;
|
||||
}
|
||||
transform->xx.set_float (sx);
|
||||
transform->yx.set_float (0);
|
||||
transform->xy.set_float (0);
|
||||
transform->yy.set_float (sy);
|
||||
transform->dx.set_float (dx);
|
||||
transform->dy.set_float (dy);
|
||||
c->serializer->add_link (out->transform, c->serializer->pop_pack ());
|
||||
|
||||
c->serializer->push ();
|
||||
bool ret = source ();
|
||||
if (ret)
|
||||
c->serializer->add_link (out->src, c->serializer->pop_pack ());
|
||||
else
|
||||
c->serializer->pop_discard ();
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Base>
|
||||
static bool subset_scale_as_transform (hb_subset_context_t *c,
|
||||
const Base *base,
|
||||
const Offset24To<Paint> &src,
|
||||
const ItemVarStoreInstancer &instancer,
|
||||
float sx, float sy,
|
||||
float cx = 0, float cy = 0)
|
||||
{
|
||||
if (src.is_null ()) return false;
|
||||
auto source = [&] { return c->dispatch (base+src, instancer); };
|
||||
float dx = cx * (1 - sx), dy = cy * (1 - sy);
|
||||
if (dx >= -32768 && dx < 32768 && dy >= -32768 && dy < 32768)
|
||||
return serialize_scale_transform (c, sx, sy, dx, dy, source);
|
||||
|
||||
// Preserve the center as separate translations when the combined matrix
|
||||
// exceeds Fixed. Splitting also makes the inverse of -32768 representable.
|
||||
auto inner = [&] {
|
||||
return serialize_scale_transform (c, 1, 1, -cx / 2, -cy / 2, source);
|
||||
};
|
||||
auto inverse = [&] {
|
||||
return serialize_scale_transform (c, 1, 1, -cx / 2, -cy / 2, inner);
|
||||
};
|
||||
auto scale = [&] {
|
||||
return serialize_scale_transform (c, sx, sy, 0, 0, inverse);
|
||||
};
|
||||
auto outer = [&] {
|
||||
return serialize_scale_transform (c, 1, 1, cx / 2, cy / 2, scale);
|
||||
};
|
||||
return serialize_scale_transform (c, 1, 1, cx / 2, cy / 2, outer);
|
||||
}
|
||||
|
||||
struct PaintScale
|
||||
{
|
||||
HB_INTERNAL void dependv1 (hb_colrv1_depend_context_t* c) const;
|
||||
@@ -1173,6 +1237,14 @@ struct PaintScale
|
||||
uint32_t varIdxBase) const
|
||||
{
|
||||
TRACE_SUBSET (this);
|
||||
if (c->plan->all_axes_pinned && instancer &&
|
||||
!c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION)
|
||||
{
|
||||
float sx = roundf (scaleX.to_float (instancer (varIdxBase, 0)) * 16384) / 16384;
|
||||
float sy = roundf (scaleY.to_float (instancer (varIdxBase, 1)) * 16384) / 16384;
|
||||
if (sx < -2 || sx >= 2 || sy < -2 || sy >= 2)
|
||||
return_trace (subset_scale_as_transform (c, this, src, instancer, sx, sy));
|
||||
}
|
||||
auto *out = c->serializer->embed (this);
|
||||
if (unlikely (!out)) return_trace (false);
|
||||
|
||||
@@ -1223,6 +1295,17 @@ struct PaintScaleAroundCenter
|
||||
uint32_t varIdxBase) const
|
||||
{
|
||||
TRACE_SUBSET (this);
|
||||
if (c->plan->all_axes_pinned && instancer &&
|
||||
!c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION)
|
||||
{
|
||||
float sx = roundf (scaleX.to_float (instancer (varIdxBase, 0)) * 16384) / 16384;
|
||||
float sy = roundf (scaleY.to_float (instancer (varIdxBase, 1)) * 16384) / 16384;
|
||||
if (sx < -2 || sx >= 2 || sy < -2 || sy >= 2)
|
||||
return_trace (subset_scale_as_transform (
|
||||
c, this, src, instancer, sx, sy,
|
||||
centerX + roundf (instancer (varIdxBase, 2)),
|
||||
centerY + roundf (instancer (varIdxBase, 3))));
|
||||
}
|
||||
auto *out = c->serializer->embed (this);
|
||||
if (unlikely (!out)) return_trace (false);
|
||||
|
||||
@@ -1279,6 +1362,13 @@ struct PaintScaleUniform
|
||||
uint32_t varIdxBase) const
|
||||
{
|
||||
TRACE_SUBSET (this);
|
||||
if (c->plan->all_axes_pinned && instancer &&
|
||||
!c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION)
|
||||
{
|
||||
float s = roundf (scale.to_float (instancer (varIdxBase, 0)) * 16384) / 16384;
|
||||
if (s < -2 || s >= 2)
|
||||
return_trace (subset_scale_as_transform (c, this, src, instancer, s, s));
|
||||
}
|
||||
auto *out = c->serializer->embed (this);
|
||||
if (unlikely (!out)) return_trace (false);
|
||||
|
||||
@@ -1324,6 +1414,16 @@ struct PaintScaleUniformAroundCenter
|
||||
uint32_t varIdxBase) const
|
||||
{
|
||||
TRACE_SUBSET (this);
|
||||
if (c->plan->all_axes_pinned && instancer &&
|
||||
!c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION)
|
||||
{
|
||||
float s = roundf (scale.to_float (instancer (varIdxBase, 0)) * 16384) / 16384;
|
||||
if (s < -2 || s >= 2)
|
||||
return_trace (subset_scale_as_transform (
|
||||
c, this, src, instancer, s, s,
|
||||
centerX + roundf (instancer (varIdxBase, 1)),
|
||||
centerY + roundf (instancer (varIdxBase, 2))));
|
||||
}
|
||||
auto *out = c->serializer->embed (this);
|
||||
if (unlikely (!out)) return_trace (false);
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,56 @@
|
||||
"""Generate COLRv1 scale variations that exceed F2Dot14 after instancing."""
|
||||
|
||||
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"] + [f"scale{i}" for i in range(16)]
|
||||
fb.setupGlyphOrder(names)
|
||||
fb.setupCharacterMap({0x61 + i: name for i, name in enumerate(names[1:])})
|
||||
glyphs = {}
|
||||
for name in names:
|
||||
pen = TTGlyphPen(None)
|
||||
glyphs[name] = pen.glyph()
|
||||
fb.setupGlyf(glyphs)
|
||||
fb.setupHorizontalMetrics({name: (1000, 0) for name in names})
|
||||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
fb.setupNameTable({"familyName": "COLR Wide Scale", "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")], [])
|
||||
rows = []
|
||||
paints = {}
|
||||
for i, name in enumerate(names[1:]):
|
||||
format = (17, 19, 21, 23)[i % 4]
|
||||
sign = 1 if i % 8 < 4 else -1
|
||||
paint = dict(
|
||||
Format=format,
|
||||
Paint=dict(Format=2, PaletteIndex=0, Alpha=1),
|
||||
VarIndexBase=len(rows),
|
||||
)
|
||||
if format in (17, 19):
|
||||
paint.update(scaleX=sign * 1.5, scaleY=-sign * 1.5)
|
||||
deltas = [sign * 16384, -sign * 16384, 3, -5]
|
||||
else:
|
||||
paint.update(scale=sign * 1.5)
|
||||
deltas = [sign * 16384, 3, -5, 0]
|
||||
if format in (19, 23):
|
||||
paint.update(centerX=37 if i < 8 else 30000, centerY=-29 if i < 8 else -30000)
|
||||
rows.extend([delta] for delta in deltas)
|
||||
paints[name] = paint
|
||||
regions = buildVarRegionList([{"wght": (0, 1, 1)}], ["wght", "wdth"])
|
||||
store = buildVarStore(regions, [buildVarData([0], rows)])
|
||||
fb.font["COLR"] = buildCOLR(
|
||||
paints,
|
||||
version=1,
|
||||
glyphMap=fb.font.getReverseGlyphMap(),
|
||||
varStore=store,
|
||||
clipBoxes={name: (0, 0, 1000, 1000) for name in names[1:]},
|
||||
)
|
||||
fb.font["CPAL"] = buildCPAL([[(1, 0, 0, 1)]])
|
||||
fb.font.recalcTimestamp = False
|
||||
fb.font["head"].created = fb.font["head"].modified = 2082844800
|
||||
fb.save(Path(__file__).parent / "colr-wide-scale.ttf")
|
||||
@@ -106,6 +106,86 @@ test_subset_colr_keep_no_colr_glyph (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
read_uint_be (const unsigned char *p, unsigned count)
|
||||
{
|
||||
unsigned value = 0;
|
||||
for (unsigned i = 0; i < count; i++)
|
||||
value = (value << 8) | p[i];
|
||||
return value;
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_colr_wide_scales (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/colr-wide-scale.ttf");
|
||||
for (unsigned composed = 0; composed < 2; composed++)
|
||||
{
|
||||
hb_face_t *source = hb_face_reference (face);
|
||||
if (composed)
|
||||
{
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 16);
|
||||
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','d','t','h'), 0));
|
||||
hb_face_t *partial = hb_subset_test_create_subset (source, input);
|
||||
hb_face_destroy (source);
|
||||
source = partial;
|
||||
}
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 16);
|
||||
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','g','h','t'), 1));
|
||||
if (!composed)
|
||||
g_assert_true (hb_subset_input_pin_axis_location (input, source, HB_TAG ('w','d','t','h'), 0));
|
||||
hb_face_t *subset = hb_subset_test_create_subset (source, input);
|
||||
hb_blob_t *blob = hb_face_reference_table (subset, 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 + 4 + 16 * 6, <=, length);
|
||||
g_assert_cmpuint (read_uint_be (data + list, 4), ==, 16);
|
||||
for (unsigned i = 0; i < 16; i++)
|
||||
{
|
||||
unsigned paint = list + read_uint_be (data + list + 4 + i * 6 + 2, 4);
|
||||
g_assert_cmpuint (paint + 7, <=, length);
|
||||
g_assert_cmpuint (data[paint], ==, 12); /* PaintTransform */
|
||||
float sx = i % 8 < 4 ? 2.5f : -2.5f;
|
||||
float sy = i % 4 < 2 ? -sx : sx;
|
||||
float cx = i % 2 ? (i < 8 ? 40 : 30003) : 0;
|
||||
float cy = i % 2 ? (i < 8 ? -34 : -30005) : 0;
|
||||
float xx = 1, yy = 1, dx = 0, dy = 0;
|
||||
unsigned depth = 0;
|
||||
while (data[paint] == 12)
|
||||
{
|
||||
g_assert_cmpuint (paint + 7, <=, length);
|
||||
unsigned transform = paint + read_uint_be (data + paint + 4, 3);
|
||||
g_assert_cmpuint (transform + 24, <=, length);
|
||||
float values[6];
|
||||
for (unsigned j = 0; j < 6; j++)
|
||||
values[j] = (int32_t) read_uint_be (data + transform + j * 4, 4) / 65536.f;
|
||||
g_assert_cmpfloat (values[1], ==, 0);
|
||||
g_assert_cmpfloat (values[2], ==, 0);
|
||||
dx += xx * values[4];
|
||||
dy += yy * values[5];
|
||||
xx *= values[0];
|
||||
yy *= values[3];
|
||||
paint += read_uint_be (data + paint + 1, 3);
|
||||
g_assert_cmpuint (paint, <, length);
|
||||
g_assert_cmpuint (++depth, <=, 5);
|
||||
}
|
||||
g_assert_cmpuint (data[paint], ==, 2); /* PaintSolid */
|
||||
g_assert_cmpfloat (xx, ==, sx);
|
||||
g_assert_cmpfloat (yy, ==, sy);
|
||||
g_assert_cmpfloat (dx, ==, cx * (1 - sx));
|
||||
g_assert_cmpfloat (dy, ==, cy * (1 - sy));
|
||||
}
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_destroy (subset);
|
||||
hb_face_destroy (source);
|
||||
}
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -115,6 +195,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_colr_keep_one_colr_glyph);
|
||||
hb_test_add (test_subset_colr_keep_mixed_glyph);
|
||||
hb_test_add (test_subset_colr_keep_no_colr_glyph);
|
||||
hb_test_add (test_subset_colr_wide_scales);
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
Reference in new issue
Block a user