diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d5bba246..ce9e8814b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,7 @@ set (subset_project_sources ${PROJECT_SOURCE_DIR}/src/hb-subset-cff-common.hh ${PROJECT_SOURCE_DIR}/src/hb-subset-cff1.cc ${PROJECT_SOURCE_DIR}/src/hb-subset-cff2.cc + ${PROJECT_SOURCE_DIR}/src/hb-subset-cff2-round.hh ${PROJECT_SOURCE_DIR}/src/hb-subset-input.cc ${PROJECT_SOURCE_DIR}/src/hb-subset-input.hh ${PROJECT_SOURCE_DIR}/src/hb-subset-instancer-iup.hh diff --git a/src/hb-cff2-interp-cs.hh b/src/hb-cff2-interp-cs.hh index af810c362..2b705d49f 100644 --- a/src/hb-cff2-interp-cs.hh +++ b/src/hb-cff2-interp-cs.hh @@ -201,6 +201,9 @@ struct cff2_cs_interp_env_t : cs_interp_env_t bool have_coords () const { return num_coords; } + /* Subsetter instancing can defer rounding until absolute points are known. */ + bool round_blends = true; + protected: const int *coords; unsigned int num_coords; @@ -263,7 +266,10 @@ struct cff2_cs_opset_t : cs_opset_t, PAR unsigned n, unsigned i) { if (env.have_coords ()) - arg.set_int (round (arg.to_real () + env.blend_deltas (blends))); + { + double v = arg.to_real () + env.blend_deltas (blends); + arg.set_real (env.round_blends ? round (v) : v); + } else if (unlikely (arg.blending ())) { /* A blend result used as an operand of a later blend: the value is diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 7ae3e3ea1..ec5afd810 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -345,6 +345,7 @@ struct cs_command_t typedef hb_vector_t *cs_command_vec_t; struct cff2_instancing_plan_t; +struct cff2_cs_rounding_t; struct flatten_param_t { @@ -362,6 +363,7 @@ struct flatten_param_t /* CFF2 partial instancing: when set, blends are rewritten against the * instanced variation store instead of being copied or flattened. */ const cff2_instancing_plan_t *instancer = nullptr; + cff2_cs_rounding_t *rounding = nullptr; bool emitted_blend = false; }; diff --git a/src/hb-subset-cff2-round.hh b/src/hb-subset-cff2-round.hh new file mode 100644 index 000000000..a2f0d1997 --- /dev/null +++ b/src/hb-subset-cff2-round.hh @@ -0,0 +1,161 @@ +/* + * CFF2 instancing: round absolute outline points and stem edges before + * converting them back to relative operands (fonttools/fonttools#4338). + */ + +#ifndef HB_SUBSET_CFF2_ROUND_HH +#define HB_SUBSET_CFF2_ROUND_HH + +#include "hb-cff2-interp-cs.hh" + +namespace CFF { + +struct cff2_cs_rounding_t +{ + struct coordinate_t + { + double value = 0.; + hb_vector_t deltas; + + bool round (blend_arg_t &arg, unsigned k) + { + if (unlikely (!deltas.resize (k) || !arg.deltas.resize (k))) + return false; + + double previous = value; + value += arg.to_real (); + /* OpenType rounding, including negative half-integers. */ + arg.number_t::set_real (floor (value + .5) - floor (previous + .5)); + bool variable = false; + for (unsigned r = 0; r < k; r++) + { + previous = deltas.arrayZ[r]; + deltas.arrayZ[r] += arg.deltas.arrayZ[r].to_real (); + double d = floor (deltas.arrayZ[r] + .5) - floor (previous + .5); + arg.deltas.arrayZ[r].set_real (d); + variable |= d != 0.; + } + if (!variable) arg.deltas.clear (); + return true; + } + }; + + bool round (op_code_t op, hb_array_t args, unsigned k) + { + unsigned n = args.length; + switch (op) + { + case OpCode_rmoveto: + return n == 2 && alternating (args, k); + case OpCode_hmoveto: + case OpCode_vmoveto: + return n == 1 && point[op == OpCode_vmoveto].round (args[0], k); + case OpCode_rlineto: + return n && !(n % 2) && alternating (args, k); + case OpCode_rrcurveto: + return n && !(n % 6) && alternating (args, k); + case OpCode_rcurveline: + return n >= 8 && !((n - 2) % 6) && alternating (args, k); + case OpCode_rlinecurve: + return n >= 8 && !(n % 2) && alternating (args, k); + case OpCode_hlineto: + case OpCode_vlineto: + return n && alternating (args, k, op == OpCode_vlineto); + + case OpCode_hhcurveto: + case OpCode_vvcurveto: + { + if (unlikely (n < 4 || (n % 4) > 1)) return false; + unsigned axis = op == OpCode_vvcurveto; + unsigned i = 0; + if (n & 1) + if (!point[!axis].round (args[i++], k)) return false; + for (; i < n; i += 4) + { + if (!point[axis].round (args[i], k) || + !point[0].round (args[i + 1], k) || + !point[1].round (args[i + 2], k) || + !point[axis].round (args[i + 3], k)) return false; + } + return true; + } + + case OpCode_hvcurveto: + case OpCode_vhcurveto: + { + if (unlikely (n < 4 || (n % 4) > 1)) return false; + unsigned axis = op == OpCode_vhcurveto; + unsigned i = 0; + for (; i + 4 <= n; i += 4, axis = !axis) + { + if (!point[axis].round (args[i], k) || + !point[0].round (args[i + 1], k) || + !point[1].round (args[i + 2], k) || + !point[!axis].round (args[i + 3], k)) return false; + if (i + 5 == n) + return point[axis].round (args[i + 4], k); + } + return true; + } + + case OpCode_flex: + if (unlikely (n != 13 || !alternating (args.sub_array (0, 12), k))) + return false; + args[12].number_t::set_real (floor (args[12].to_real () + .5)); + for (auto &d : args[12].deltas) d.set_real (floor (d.to_real () + .5)); + return true; + + case OpCode_hflex: + case OpCode_hflex1: + { + if (unlikely (n != (op == OpCode_hflex ? 7u : 9u))) return false; + /* These operators implicitly return to the initial y coordinate. */ + coordinate_t y = point[1]; + if (unlikely (y.deltas.in_error ())) return false; + for (unsigned i = 0; i < n; i++) + { + unsigned axis = op == OpCode_hflex ? i == 2 : i == 1 || i == 3 || i == 7; + if (!point[axis].round (args[i], k)) return false; + } + point[1] = std::move (y); + return true; + } + + case OpCode_hstem: + case OpCode_hstemhm: + case OpCode_vstem: + case OpCode_vstemhm: + case OpCode_hintmask: + case OpCode_cntrmask: + { + /* Stem operands are successive edges, relative to zero per operator. + * A mask's operands declare implicit vertical stems. */ + coordinate_t stem; + if (unlikely (n % 2)) return false; + for (auto &arg : args) + if (!stem.round (arg, k)) return false; + return true; + } + + default: + return true; + } + } + + private: + bool alternating (hb_array_t args, unsigned k, unsigned axis = 0) + { + for (auto &arg : args) + { + if (!point[axis].round (arg, k)) return false; + axis = !axis; + } + return true; + } + + coordinate_t point[2]; +}; + +} /* namespace CFF */ + +#endif /* HB_SUBSET_CFF2_ROUND_HH */ diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 78b4abb45..3e3f7f913 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -35,6 +35,7 @@ #include "hb-subset-plan.hh" #include "hb-subset-cff-common.hh" #include "hb-cff2-interp-cs.hh" +#include "hb-subset-cff2-round.hh" #include "hb-subset-cff2-to-cff1.hh" using namespace CFF; @@ -321,6 +322,8 @@ struct cff2_usage_param_t cff2_instancing_plan_t *instancing = nullptr; bool drop_hints = false; + bool has_blend = false; + bool has_flex1 = false; unsigned ivs = 0; /* dict-side current vsindex */ }; @@ -328,6 +331,7 @@ struct cff2_cs_opset_usage_t : cff2_cs_opset_t &env, cff2_usage_param_t& param) { + param.has_flex1 |= op == OpCode_flex1; bool counts = true; switch (op) { @@ -354,7 +358,8 @@ struct cff2_cs_opset_usage_t : cff2_cs_opset_tnote_use (env.get_ivs ()); + param.has_blend = true; + if (param.instancing) param.instancing->note_use (env.get_ivs ()); break; } @@ -423,54 +428,52 @@ struct cff2_cs_opset_flatten_t : cff2_cs_opset_t &env, flatten_param_t& param) { - /* Optionally capture command for specialization (before flushing, to preserve args) */ - if (param.commands) + bool skip_command = false; + switch (op) { - bool skip_command = false; + case OpCode_return: + case OpCode_endchar: + skip_command = true; + break; - switch (op) + case OpCode_hstem: + case OpCode_hstemhm: + case OpCode_vstem: + case OpCode_vstemhm: + case OpCode_hintmask: + case OpCode_cntrmask: + skip_command = param.drop_hints; + break; + + default: + break; + } + + if (!skip_command && param.rounding && unlikely (!round_args (op, env, param))) + { + env.set_error (); + return; + } + + /* Capture resolved commands for CFF2-to-CFF1 specialization. */ + if (param.commands && !skip_command) + { + cs_command_t cmd (op); + for (unsigned int i = 0; i < env.argStack.get_count ();) { - case OpCode_return: - case OpCode_endchar: - skip_command = true; - break; - - case OpCode_hstem: - case OpCode_hstemhm: - case OpCode_vstem: - case OpCode_vstemhm: - case OpCode_hintmask: - case OpCode_cntrmask: - if (param.drop_hints) - skip_command = true; - break; - - default: - break; - } - - if (!skip_command) - { - cs_command_t cmd (op); - /* Capture resolved blend values */ - for (unsigned int i = 0; i < env.argStack.get_count ();) + const blend_arg_t &arg = env.argStack[i]; + if (arg.blending ()) { - const blend_arg_t &arg = env.argStack[i]; - if (arg.blending ()) - { - /* For blend args, capture only the resolved default value */ - cmd.args.push (arg); - /* Skip over the multiple blend values */ - i += arg.numValues; - } - else - { - cmd.args.push (arg); - i++; - } + cmd.args.push (arg); + i += arg.numValues; + } + else + { + cmd.args.push (arg); + i++; } - param.commands->push (cmd); } + param.commands->push (cmd); } switch (op) @@ -503,6 +506,12 @@ struct cff2_cs_opset_flatten_t : cff2_cs_opset_t &env, flatten_param_t& param) { + if (param.rounding) + { + encode_rounded_args (env, param); + SUPER::flush_args (env, param); + return; + } for (unsigned int i = 0; i < env.argStack.get_count ();) { const blend_arg_t &arg = env.argStack[i]; @@ -529,6 +538,65 @@ struct cff2_cs_opset_flatten_t : cff2_cs_opset_t &env, flatten_param_t& param) + { + unsigned k = 0; + if (param.instancer) + { + unsigned ivs = env.get_ivs (); + const auto *t = param.instancer->get_transform (ivs); + k = t && t->alive () ? t->matrix.length : 0; + for (unsigned i = 0; i < env.argStack.get_count (); i++) + { + auto &arg = env.argStack[i]; + if (!arg.blending ()) continue; + if (unlikely (!t || arg.deltas.length != t->gains.length)) return false; + double value = arg.to_real () + param.instancer->fold_deltas (ivs, arg.deltas.as_array ()); + hb_vector_t deltas = std::move (arg.deltas); + arg.set_real (value); + if (unlikely (!arg.deltas.resize (k))) return false; + for (unsigned r = 0; r < k; r++) + arg.deltas.arrayZ[r].set_real (t->transform_delta (r, deltas.as_array ())); + } + } + return param.rounding->round (op, hb_array (env.argStack.arrayZ, env.argStack.get_count ()), k); + } + + static void encode_rounded_args (cff2_cs_interp_env_t &env, flatten_param_t& param) + { + str_encoder_t encoder (param.flatStr); + unsigned count = env.argStack.get_count (); + for (unsigned i = 0; i < count;) + { + const auto &arg = env.argStack[i]; + if (!arg.blending ()) + { + encoder.encode_num_cs (arg); + i++; + continue; + } + unsigned k = arg.deltas.length; + unsigned n = 1; + while (i + n < count && env.argStack[i + n].deltas.length == k) n++; + /* Previously emitted operands remain on the stack during each blend. */ + unsigned chunk = hb_min (n, (512 - i) / (k + 1)); + if (unlikely (!chunk)) + { + env.set_error (); + return; + } + for (unsigned j = 0; j < chunk; j++) + encoder.encode_num_cs (env.argStack[i + j]); + for (unsigned j = 0; j < chunk; j++) + for (const auto &d : env.argStack[i + j].deltas) + encoder.encode_num_cs (d); + encoder.encode_int (chunk); + encoder.encode_op (OpCode_blendcs); + param.emitted_blend = true; + i += chunk; + } + } + static void flatten_blends (const blend_arg_t &arg, unsigned int i, cff2_cs_interp_env_t &env, flatten_param_t& param) { /* flatten the default values */ @@ -1131,10 +1199,13 @@ struct cff2_subset_plan if (desubroutinize) { - if (partial_instancing) + if (instancing_requested) { - /* Pass 1: collect which VarDatas the retained blends reference, so - * unused ones get pruned and the survivors renumbered. */ + /* Identify charstrings that can round absolute coordinates. Conservatively + * keep relative rounding for flex1, whose implicit direction can vary. + * Also collect the VarDatas referenced by partial instances. */ + hb_vector_t round_charstrings; + if (unlikely (!round_charstrings.resize (num_glyphs))) return false; hb_set_t used_fds; for (unsigned int i = 0; i < num_glyphs; i++) { @@ -1150,34 +1221,40 @@ struct cff2_subset_plan cff2_cs_interp_env_t env (str, acc, fd); cs_interpreter_t, cff2_cs_opset_usage_t, cff2_usage_param_t> interp (env); cff2_usage_param_t param; - param.instancing = &instancing; + param.instancing = partial_instancing ? &instancing : nullptr; param.drop_hints = drop_hints; if (unlikely (!interp.interpret (param))) return false; + round_charstrings.arrayZ[i] = param.has_blend && !param.has_flex1; } - for (unsigned fd : used_fds) + if (partial_instancing) { - cff2_usage_param_t param; - param.instancing = &instancing; - unsigned count = acc.privateDicts[fd].get_count (); - for (unsigned j = 0; j < count; j++) + for (unsigned fd : used_fds) { - const op_str_t &opstr = acc.privateDicts[fd][j]; - if (drop_hints && dict_opset_t::is_hint_op (opstr.op)) - continue; - cff2_priv_dict_interp_env_t env {hb_ubytes_t (opstr.ptr, opstr.length)}; - dict_interpreter_t interp (env); - if (unlikely (!interp.interpret (param))) - return false; + cff2_usage_param_t param; + param.instancing = &instancing; + unsigned count = acc.privateDicts[fd].get_count (); + for (unsigned j = 0; j < count; j++) + { + const op_str_t &opstr = acc.privateDicts[fd][j]; + if (drop_hints && dict_opset_t::is_hint_op (opstr.op)) + continue; + cff2_priv_dict_interp_env_t env {hb_ubytes_t (opstr.ptr, opstr.length)}; + dict_interpreter_t interp (env); + if (unlikely (!interp.interpret (param))) + return false; + } } + if (unlikely (!instancing.finalize ())) + return false; } - if (unlikely (!instancing.finalize ())) - return false; - /* Pass 2: like subr_flattener_t, but blends stay symbolic (no coords - * fed to the env) and get rewritten against the instanced store. */ + /* Keep full-instance blends unrounded until their absolute positions + * are known; partial-instance blends stay symbolic for rewriting. */ if (!subset_charstrings.resize_exact (num_glyphs)) return false; + if (capture_commands && !charstring_commands.resize_exact (num_glyphs)) + return false; for (unsigned int i = 0; i < num_glyphs; i++) { hb_codepoint_t glyph; @@ -1188,14 +1265,20 @@ struct cff2_subset_plan if (unlikely (fd >= acc.fdCount)) return false; - cff2_cs_interp_env_t env (str, acc, fd); + cff2_cs_interp_env_t env (str, acc, fd, + pinned ? plan->normalized_coords.arrayZ : nullptr, + pinned ? plan->normalized_coords.length : 0); + env.round_blends = !round_charstrings.arrayZ[i]; cs_interpreter_t, cff2_cs_opset_flatten_t, flatten_param_t> interp (env); flatten_param_t param = { subset_charstrings.arrayZ[i], (bool) (plan->flags & HB_SUBSET_FLAGS_NO_HINTING), - plan + plan, + capture_commands ? &charstring_commands.arrayZ[i] : nullptr }; - param.instancer = &instancing; + cff2_cs_rounding_t rounding; + if (round_charstrings.arrayZ[i]) param.rounding = &rounding; + param.instancer = partial_instancing ? &instancing : nullptr; if (unlikely (!interp.interpret (param))) return false; } diff --git a/src/meson.build b/src/meson.build index 95f64d78f..1ef9ba444 100644 --- a/src/meson.build +++ b/src/meson.build @@ -399,6 +399,7 @@ hb_subset_sources = files( 'hb-subset-cff-common.hh', 'hb-subset-cff1.cc', 'hb-subset-cff2.cc', + 'hb-subset-cff2-round.hh', 'hb-subset-cff2-to-cff1.hh', 'hb-subset-input.cc', 'hb-subset-input.hh', diff --git a/test/api/fonts/AdobeVFPrototype.abc.static.otf b/test/api/fonts/AdobeVFPrototype.abc.static.otf index 0c2e43db4..e26994011 100644 Binary files a/test/api/fonts/AdobeVFPrototype.abc.static.otf and b/test/api/fonts/AdobeVFPrototype.abc.static.otf differ diff --git a/test/api/fonts/cff2-rounding.otf b/test/api/fonts/cff2-rounding.otf new file mode 100644 index 000000000..c5eb11398 Binary files /dev/null and b/test/api/fonts/cff2-rounding.otf differ diff --git a/test/api/generate-cff2-rounding-font.py b/test/api/generate-cff2-rounding-font.py new file mode 100644 index 000000000..6716178c6 --- /dev/null +++ b/test/api/generate-cff2-rounding-font.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +"""Generate CFF2 instancing regressions for fonttools/fonttools#4338. + +Run from the repository root. Each ring contour closes explicitly in the +default and in every variation region, so relative rounding must not open it. +The other glyphs exercise compact path operators, flexes, and hint masks. +""" + +import math +from pathlib import Path +import subprocess +import sys + +from fontTools.fontBuilder import FontBuilder +from fontTools.misc.psCharStrings import T2CharString + + +def ring(radius, wobble): + commands = [] + previous = (0, 0) + for cx, cy, direction in ((400, 400, 1), (1100, 400, -1)): + points = [ + ( + round(cx + (radius + wobble * (i % 3)) * math.cos(i * math.pi / 8)), + round(cy + (radius - wobble * (i % 2)) * math.sin(i * math.pi / 8)), + ) + for i in range(16) + ][::direction] + commands.append(("rmoveto", [points[0][j] - previous[j] for j in (0, 1)])) + previous = points[0] + for start, end in zip(points, points[1:] + points[:1]): + controls = [ + tuple(start[j] + (end[j] - start[j]) // 3 for j in (0, 1)), + tuple(end[j] - (end[j] - start[j]) // 3 for j in (0, 1)), + end, + ] + args = [] + for point in controls: + args.extend(point[j] - previous[j] for j in (0, 1)) + previous = point + commands.append(("rrcurveto", args)) + return commands + + +def blended(default, deltas): + return list(default) + [d[i] for i in range(len(default)) for d in deltas] + [len(default), "blend"] + + +program = [] +masters = [ring(r, w) for r, w in ((300, 7), (337, 23), (305, 11), (343, 29), (321, 13))] +for i, (op, args) in enumerate(masters[0]): + # Independent wght, slnt, interaction, and intermediate-wght regions. + deltas = [ + [masters[1][i][1][j] - v for j, v in enumerate(args)], + [masters[2][i][1][j] - v for j, v in enumerate(args)], + [masters[3][i][1][j] - masters[1][i][1][j] - masters[2][i][1][j] + v for j, v in enumerate(args)], + [masters[4][i][1][j] - (masters[1][i][1][j] + v) / 2 for j, v in enumerate(args)], + ] + program.extend(blended(args, deltas) + [op]) + +programs = {".notdef": [], "ring": program} +operators = { + "hlineto": [23, 31, -19, 41, 37], + "vlineto": [23, 31, -19, 41, 37], + "rcurveline": [17, 23, 31, -19, 41, 37, 29, -11], + "rlinecurve": [17, 23, 31, -19, 41, 37, 29, -11], + "hhcurveto": [13, 17, 23, 31, -19, 41, 37, 29, -11], + "vvcurveto": [13, 17, 23, 31, -19, 41, 37, 29, -11], + "hvcurveto": [17, 23, 31, -19, 41, 37, 29, -11, 13], + "vhcurveto": [17, 23, 31, -19, 41, 37, 29, -11, 13], + "flex": [17, 23, 31, -19, 41, 37, 29, -11, 13, 47, 19, -23, 50], + "hflex": [17, 23, 31, -19, 41, 37, 29], + "hflex1": [17, 23, 31, -19, 41, 37, 29, -11, 13], + # Its direction changes over wght: exercise the conservative fallback. + "flex1": [10, 20] * 5 + [17], +} +for op in ("hhcurveto", "vvcurveto", "hvcurveto", "vhcurveto"): + operators[op + ".one"] = [17, 23, 31, -19] + operators[op + ".two"] = [17, 23, 31, -19, 41, 37, 29, -11] +for op, args in operators.items(): + deltas = [[(j % 7) - 3 for j in range(len(args))]] + [[0] * len(args)] * 3 + if op == "flex": + deltas[0][-1] = 0 + if op == "flex1": + deltas[0] = [30, -10] * 5 + [0] + p = blended([101, 103], [[1, -1], [0, 0], [0, 0], [0, 0]]) + ["rmoveto"] + # Repeat to expose cumulative errors, including flex's implicit y reset. + for _ in range(3): + p += blended(args, deltas) + [op.split(".")[0]] + p += [23, 29, "rlineto", 31, "hmoveto", 37, "vmoveto", 41, 43, "rlineto"] + programs[op] = p + +stem = blended([101, 23, 29, 31], [[1, 1, 1, 1], [0] * 4, [0] * 4, [0] * 4]) +programs["hinted"] = stem + ["hstemhm"] + stem + ["hintmask", b"\xf0"] + program +programs["fractional"] = [10.25, 20.75, "rmoveto", 100.5, 0, 0, 100.5, -100.5, 0, "rlineto"] + +order = list(programs) +fb = FontBuilder(1000, isTTF=False) +fb.setupGlyphOrder(order) +fb.setupCharacterMap({0x41 + i: name for i, name in enumerate(order[1:])}) +fb.setupHorizontalMetrics({name: (2000, 0) for name in order}) +fb.setupHorizontalHeader(ascent=1600, descent=-400) +fb.setupNameTable({"familyName": "CFF2 Rounding", "styleName": "Regular"}) +fb.setupOS2() +fb.setupFvar([("wght", 300, 300, 900, "Weight"), ("slnt", -11, 0, 0, "Slant")], []) +fb.setupCFF2( + {name: T2CharString(program=p) for name, p in programs.items()}, + regions=[ + {"wght": (0, 1, 1)}, + {"slnt": (-1, -1, 0)}, + {"wght": (0, 1, 1), "slnt": (-1, -1, 0)}, + {"wght": (0, 0.5, 1)}, + ], +) +fb.setupPost(keepGlyphNames=True) +fb.font["head"].created = fb.font["head"].modified = 2082844800 +fb.font.recalcTimestamp = False +fb.save(Path(__file__).parent / "fonts" / "cff2-rounding.otf") + +# The existing API test also compares this table's serialized bytes. +subprocess.run( + [ + sys.argv[1] if len(sys.argv) > 1 else "build/util/hb-subset", + str(Path(__file__).parent / "fonts" / "AdobeVFPrototype.abc.otf"), + "--unicodes=61,62,63", + "--variations=wght=250,CNTR=0", + "--output-file=" + str(Path(__file__).parent / "fonts" / "AdobeVFPrototype.abc.static.otf"), + ], + check=True, +) diff --git a/test/api/test-instance-cff2.c b/test/api/test-instance-cff2.c index d560b5c1b..d62c1dc00 100644 --- a/test/api/test-instance-cff2.c +++ b/test/api/test-instance-cff2.c @@ -158,6 +158,156 @@ test_instance_cff2_large_store (void) } +typedef struct { + char op; + unsigned count; + float points[6]; +} rounding_command_t; + +static void +rounding_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, + hb_draw_state_t *st HB_UNUSED, float x, float y, + void *user_data HB_UNUSED) +{ + rounding_command_t command = {'M', 2, {x, y}}; + g_array_append_val ((GArray *) data, command); +} + +static void +rounding_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, + hb_draw_state_t *st HB_UNUSED, float x, float y, + void *user_data HB_UNUSED) +{ + rounding_command_t command = {'L', 2, {x, y}}; + g_array_append_val ((GArray *) data, command); +} + +static void +rounding_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, + hb_draw_state_t *st HB_UNUSED, float x1, float y1, + float x2, float y2, float x, float y, + void *user_data HB_UNUSED) +{ + rounding_command_t command = {'C', 6, {x1, y1, x2, y2, x, y}}; + g_array_append_val ((GArray *) data, command); +} + +static void +rounding_close_path (hb_draw_funcs_t *dfuncs HB_UNUSED, void *data, + hb_draw_state_t *st HB_UNUSED, void *user_data HB_UNUSED) +{ + rounding_command_t command = {'Z', 0, {0}}; + g_array_append_val ((GArray *) data, command); +} + +static void +check_rounding (hb_face_t *face, hb_face_t *subset, float weight, float slant, + float tolerance) +{ + hb_variation_t location[] = {{HB_TAG ('w','g','h','t'), weight}, + {HB_TAG ('s','l','n','t'), slant}}; + hb_font_t *original = hb_font_create (face); + hb_font_t *instanced = hb_font_create (subset); + hb_font_set_variations (original, location, 2); + hb_font_set_variations (instanced, location, 2); + hb_draw_funcs_t *funcs = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (funcs, rounding_move_to, NULL, NULL); + hb_draw_funcs_set_line_to_func (funcs, rounding_line_to, NULL, NULL); + hb_draw_funcs_set_cubic_to_func (funcs, rounding_cubic_to, NULL, NULL); + hb_draw_funcs_set_close_path_func (funcs, rounding_close_path, NULL, NULL); + + for (hb_codepoint_t glyph = 1; glyph < hb_face_get_glyph_count (face); glyph++) + { + char name[64]; + g_assert_true (hb_font_get_glyph_name (original, glyph, name, sizeof (name))); + GArray *expected = g_array_new (FALSE, FALSE, sizeof (rounding_command_t)); + GArray *actual = g_array_new (FALSE, FALSE, sizeof (rounding_command_t)); + g_assert_true (hb_font_draw_glyph_or_fail (original, glyph, funcs, expected)); + g_assert_true (hb_font_draw_glyph_or_fail (instanced, glyph, funcs, actual)); + g_test_message ("glyph %u, wght %g, slnt %g", glyph, weight, slant); + if (!strcmp (name, "flex1")) + { + /* flex1 deliberately retains relative rounding for the entire glyph. + * At wght=570 its first dx/dy round to 24/15, after a 101/103 moveto. */ + if (weight == 570) + { + rounding_command_t *curve = &g_array_index (actual, rounding_command_t, 1); + g_assert_cmpint (curve->op, ==, 'C'); + g_assert_cmpfloat (curve->points[0], ==, 125); + g_assert_cmpfloat (curve->points[1], ==, 118); + } + g_array_free (actual, TRUE); + g_array_free (expected, TRUE); + continue; + } + g_assert_cmpuint (expected->len, ==, actual->len); + for (unsigned i = 0; i < expected->len; i++) + { + rounding_command_t *a = &g_array_index (actual, rounding_command_t, i); + rounding_command_t *e = &g_array_index (expected, rounding_command_t, i); + g_assert_cmpint (a->op, ==, e->op); + for (unsigned j = 0; j < e->count; j++) + g_assert_cmpfloat_with_epsilon (a->points[j], e->points[j], tolerance); + /* The rings' final cubic endpoint must equal their moveto, without + * relying on the implicit line supplied by close_path. */ + if ((!strcmp (name, "ring") || !strcmp (name, "hinted")) && a->op == 'Z') + { + rounding_command_t *end = &g_array_index (actual, rounding_command_t, i - 1); + unsigned start = i - 1; + while (g_array_index (actual, rounding_command_t, start).op != 'M') start--; + rounding_command_t *move = &g_array_index (actual, rounding_command_t, start); + g_assert_cmpint (end->op, ==, 'C'); + g_assert_cmpfloat_with_epsilon (end->points[4], move->points[0], .001); + g_assert_cmpfloat_with_epsilon (end->points[5], move->points[1], .001); + } + } + g_array_free (actual, TRUE); + g_array_free (expected, TRUE); + } + hb_draw_funcs_destroy (funcs); + hb_font_destroy (original); + hb_font_destroy (instanced); +} + +static void +test_instance_cff2_absolute_rounding (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/cff2-rounding.otf"); + for (unsigned mode = 0; mode < 4; mode++) + for (unsigned no_hinting = 0; no_hinting < 2; no_hinting++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, hb_face_get_glyph_count (face) - 1); + if (no_hinting) hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING); + if (mode == 0 || mode == 3) + { + g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w','g','h','t'), mode == 0 ? 570 : 300)); + g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('s','l','n','t'), mode == 0 ? -5.5 : 0)); + } + else + { + g_assert_true (hb_subset_input_set_axis_range (input, face, HB_TAG ('w','g','h','t'), 300, 900, 400)); + if (mode == 2) + g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('s','l','n','t'), -5.5)); + } + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + check_rounding (face, subset, mode == 0 ? 570 : mode == 3 ? 300 : 400, + mode == 1 || mode == 3 ? 0 : -5.5, .501); + if (mode == 1 || mode == 2) + { + check_rounding (face, subset, 300, mode == 1 ? 0 : -5.5, 1.51); + check_rounding (face, subset, 600, mode == 1 ? -11 : -5.5, 2.01); + check_rounding (face, subset, 900, mode == 1 ? -11 : -5.5, 2.01); + } + hb_face_destroy (subset); + hb_subset_input_destroy (input); + } + hb_face_destroy (face); +} + + + int main (int argc, char **argv) { @@ -167,6 +317,7 @@ main (int argc, char **argv) hb_test_add (test_instance_cff2_private_blend_overflow); hb_test_add (test_instance_cff2_empty_bounds); hb_test_add (test_instance_cff2_large_store); + hb_test_add (test_instance_cff2_absolute_rounding); return hb_test_run(); } diff --git a/test/subset/data/expected/downgrade_cff2/SourceSerif4Variable-Roman-HelloWorld.downgrade-cff2.48,65,6C,6C,6F,20,57,6F,72,6C,64.wght=650,opsz=48.otf b/test/subset/data/expected/downgrade_cff2/SourceSerif4Variable-Roman-HelloWorld.downgrade-cff2.48,65,6C,6C,6F,20,57,6F,72,6C,64.wght=650,opsz=48.otf index bd320852f..f623441a9 100644 Binary files a/test/subset/data/expected/downgrade_cff2/SourceSerif4Variable-Roman-HelloWorld.downgrade-cff2.48,65,6C,6C,6F,20,57,6F,72,6C,64.wght=650,opsz=48.otf and b/test/subset/data/expected/downgrade_cff2/SourceSerif4Variable-Roman-HelloWorld.downgrade-cff2.48,65,6C,6C,6F,20,57,6F,72,6C,64.wght=650,opsz=48.otf differ diff --git a/test/subset/data/expected/downgrade_cff2_iftb/Cantarell-VF-ABC.downgrade-cff2-iftb.41,42,43.wght=650.otf b/test/subset/data/expected/downgrade_cff2_iftb/Cantarell-VF-ABC.downgrade-cff2-iftb.41,42,43.wght=650.otf index e400ab0da..82f757fa2 100644 Binary files a/test/subset/data/expected/downgrade_cff2_iftb/Cantarell-VF-ABC.downgrade-cff2-iftb.41,42,43.wght=650.otf and b/test/subset/data/expected/downgrade_cff2_iftb/Cantarell-VF-ABC.downgrade-cff2-iftb.41,42,43.wght=650.otf differ diff --git a/test/subset/data/expected/instantiate_cff2/AdobeVFPrototype.default.all.wght=650,CNTR=50.otf b/test/subset/data/expected/instantiate_cff2/AdobeVFPrototype.default.all.wght=650,CNTR=50.otf index acf48c3b4..f48c1aa1b 100644 Binary files a/test/subset/data/expected/instantiate_cff2/AdobeVFPrototype.default.all.wght=650,CNTR=50.otf and b/test/subset/data/expected/instantiate_cff2/AdobeVFPrototype.default.all.wght=650,CNTR=50.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.CNTR=drop.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.CNTR=drop.otf index 13f87196c..37c37bdea 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.CNTR=drop.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.CNTR=drop.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-500-700,CNTR=25-75.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-500-700,CNTR=25-75.otf index 5c91a9ba1..fd5c9a4dc 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-500-700,CNTR=25-75.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-500-700,CNTR=25-75.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-600.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-600.otf index f1af6023e..53951d419 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-600.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=300-600.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=500,CNTR=0-100.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=500,CNTR=0-100.otf index d11a3cef6..b23019e01 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=500,CNTR=0-100.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.41,42,43.wght=500,CNTR=0-100.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.CNTR=drop.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.CNTR=drop.otf index 389f12758..b727a49fe 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.CNTR=drop.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.CNTR=drop.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-500-700,CNTR=25-75.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-500-700,CNTR=25-75.otf index 801b6ecb4..6cdffc9a2 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-500-700,CNTR=25-75.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-500-700,CNTR=25-75.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-600.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-600.otf index c7e9e2cbd..10646a61c 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-600.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=300-600.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=500,CNTR=0-100.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=500,CNTR=0-100.otf index a68d19590..52a9aea5a 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=500,CNTR=0-100.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype.default.all.wght=500,CNTR=0-100.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.CNTR=drop.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.CNTR=drop.otf index 4db7879bc..cab1320cc 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.CNTR=drop.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.CNTR=drop.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-500-700,CNTR=25-75.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-500-700,CNTR=25-75.otf index 86131a8a3..e3cdc8223 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-500-700,CNTR=25-75.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-500-700,CNTR=25-75.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-600.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-600.otf index 4e7f4d1b9..130a0ca69 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-600.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=300-600.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=500,CNTR=0-100.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=500,CNTR=0-100.otf index 6ff124e68..c2257b6c9 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=500,CNTR=0-100.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.41,42,43.wght=500,CNTR=0-100.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.CNTR=drop.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.CNTR=drop.otf index 4db7879bc..cab1320cc 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.CNTR=drop.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.CNTR=drop.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-500-700,CNTR=25-75.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-500-700,CNTR=25-75.otf index 86131a8a3..e3cdc8223 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-500-700,CNTR=25-75.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-500-700,CNTR=25-75.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-600.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-600.otf index 4e7f4d1b9..130a0ca69 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-600.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=300-600.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=500,CNTR=0-100.otf b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=500,CNTR=0-100.otf index 6ff124e68..c2257b6c9 100644 Binary files a/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=500,CNTR=0-100.otf and b/test/subset/data/expected/instantiate_cff2_partial/AdobeVFPrototype_vsindex.default.all.wght=500,CNTR=0-100.otf differ diff --git a/test/subset/data/expected/instantiate_cff2_tsb/NotoSansJP-VF.subset.default.5005,5006,5009,500D.wght=300.otf b/test/subset/data/expected/instantiate_cff2_tsb/NotoSansJP-VF.subset.default.5005,5006,5009,500D.wght=300.otf index ed63414d3..2e62a8a0d 100644 Binary files a/test/subset/data/expected/instantiate_cff2_tsb/NotoSansJP-VF.subset.default.5005,5006,5009,500D.wght=300.otf and b/test/subset/data/expected/instantiate_cff2_tsb/NotoSansJP-VF.subset.default.5005,5006,5009,500D.wght=300.otf differ