Add new hb_subset_input_to_command_line_or_fail().

This generates an equivalent hb-subset command line string for a given subset input.

Assisted by gemini.
This commit is contained in:
Garret Rieger committed 2026-07-14 16:26:49 -06:00
1 parent 946901f88d
commit 99380f0e44
4 files changed
+573

No files matched your search

+1
View File
@@ -21,6 +21,7 @@ if '--experimental-api' not in sys.argv:
experimental_symbols = \
"""hb_shape_justify
hb_subset_input_override_name_table
hb_subset_input_to_string_or_fail
hb_subset_cff_get_charstring_data
hb_subset_cff_get_charstrings_index
hb_subset_cff2_get_charstring_data
+372
View File
@@ -734,6 +734,378 @@ hb_subset_axis_range_to_string (hb_subset_input_t *input,
}
#endif
#ifdef HB_EXPERIMENTAL_API
static void
_append_arg (hb_vector_t<char> &sb, const char *arg)
{
if (sb.in_error ()) return;
if (sb.length)
sb.push (' ');
unsigned len = strlen (arg);
sb.extend (hb_bytes_t (arg, len));
}
static void
_format_number_set (const hb_set_t *set, bool is_hex, hb_vector_t<char> &out)
{
hb_codepoint_t first = HB_SET_VALUE_INVALID, last = HB_SET_VALUE_INVALID;
bool first_range = true;
while (hb_set_next_range (set, &first, &last))
{
if (!first_range)
out.push (',');
first_range = false;
char buf[64];
int len;
if (first == last)
{
if (is_hex)
len = snprintf (buf, sizeof (buf), "%X", first);
else
len = snprintf (buf, sizeof (buf), "%u", first);
}
else
{
if (is_hex)
len = snprintf (buf, sizeof (buf), "%X-%X", first, last);
else
len = snprintf (buf, sizeof (buf), "%u-%u", first, last);
}
if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return;
out.extend (hb_bytes_t (buf, len));
}
}
static void
_format_tag (hb_tag_t tag, hb_vector_t<char> &out)
{
char buf[4];
hb_tag_to_string (tag, buf);
unsigned len = 4;
while (len > 0 && buf[len - 1] == ' ')
len--;
if (len == 0)
{
static const char spaces[4] = {'_', '_', '_', '_'};
out.extend (hb_bytes_t (spaces, 4));
return;
}
for (unsigned i = 0; i < len; i++)
{
char c = buf[i];
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_'))
{
c = '_';
}
out.push (c);
}
}
static void
_format_tag_set (const hb_set_t *set, hb_vector_t<char> &out)
{
hb_codepoint_t tag = HB_SET_VALUE_INVALID;
bool first_tag = true;
while (hb_set_next (set, &tag))
{
if (!first_tag)
out.push (',');
first_tag = false;
_format_tag (tag, out);
}
}
static bool
_is_set_default (const hb_set_t *set, const hb_set_t *default_set)
{
return hb_set_is_inverted (set) == hb_set_is_inverted (default_set) &&
hb_set_is_equal (set, default_set);
}
template <typename Formatter>
static void
_format_set_arg (const hb_set_t *set,
const char *opt_name,
Formatter &&formatter,
hb_vector_t<char> &sb)
{
if (hb_set_is_inverted (set))
{
hb_set_t *excluded = hb_set_copy (set);
hb_set_invert (excluded);
hb_vector_t<char> opt;
opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
opt.extend (hb_bytes_t ("=*", 2));
opt.push ('\0');
if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
if (!hb_set_is_empty (excluded))
{
hb_vector_t<char> formatted;
formatter (excluded, formatted);
if (formatted.length)
{
hb_vector_t<char> opt2;
opt2.extend (hb_bytes_t (opt_name, strlen (opt_name)));
opt2.extend (hb_bytes_t ("-=", 2));
opt2.extend (formatted.as_array ());
opt2.push ('\0');
if (opt2.arrayZ) _append_arg (sb, opt2.arrayZ);
}
}
hb_set_destroy (excluded);
}
else if (hb_set_is_empty (set))
{
hb_vector_t<char> opt;
opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
opt.extend (hb_bytes_t ("-=*", 3));
opt.push ('\0');
if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
}
else
{
hb_vector_t<char> formatted;
formatter (set, formatted);
if (formatted.length)
{
hb_vector_t<char> opt;
opt.extend (hb_bytes_t (opt_name, strlen (opt_name)));
opt.extend (hb_bytes_t ("=", 1));
opt.extend (formatted.as_array ());
opt.push ('\0');
if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
}
}
}
static void
_format_number_set_arg (const hb_set_t *set,
const char *opt_name,
bool is_hex,
hb_vector_t<char> &sb)
{
_format_set_arg (set, opt_name, [is_hex] (const hb_set_t *s, hb_vector_t<char> &out) {
_format_number_set (s, is_hex, out);
}, sb);
}
static void
_format_tag_set_arg (const hb_set_t *set,
const char *opt_name,
hb_vector_t<char> &sb)
{
_format_set_arg (set, opt_name, _format_tag_set, sb);
}
static bool
_format_glyph_map_arg (const hb_map_t &glyph_map,
hb_vector_t<char> &sb)
{
if (glyph_map.is_empty ())
return true;
hb_set_t keys;
hb_map_keys (&glyph_map, &keys);
hb_vector_t<char> pairs;
hb_codepoint_t key = HB_SET_VALUE_INVALID;
bool first = true;
while (hb_set_next (&keys, &key))
{
if (!first)
pairs.push (',');
first = false;
hb_codepoint_t val = hb_map_get (&glyph_map, key);
char buf[64];
int len = snprintf (buf, sizeof (buf), "%u:%u", key, val);
if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return false;
pairs.extend (hb_bytes_t (buf, len));
}
if (pairs.length)
{
hb_vector_t<char> opt;
opt.extend (hb_bytes_t ("--gid-map=", 10));
opt.extend (pairs.as_array ());
opt.push ('\0');
if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
}
return true;
}
static bool
_format_axes_location_arg (const hb_hashmap_t<hb_tag_t, Triple> &axes_location,
hb_vector_t<char> &sb)
{
if (axes_location.is_empty ())
return true;
hb_vector_t<hb_tag_t> axis_tags;
for (auto tag : axes_location.keys ())
axis_tags.push (tag);
axis_tags.qsort ([] (const hb_tag_t &a, const hb_tag_t &b) { return a < b ? -1 : a > b ? 1 : 0; });
hb_vector_t<char> specs;
bool first = true;
for (hb_tag_t tag : axis_tags)
{
Triple triple = axes_location.get (tag);
if (!first)
specs.push (',');
first = false;
hb_vector_t<char> tag_buf;
_format_tag (tag, tag_buf);
char buf[128];
int len;
if (triple.minimum == triple.middle && triple.middle == triple.maximum)
{
len = snprintf (buf, sizeof (buf), "%.*s=%g", tag_buf.length, tag_buf.arrayZ, (double) triple.middle);
}
else if (std::isnan (triple.minimum) && std::isnan (triple.middle) && std::isnan (triple.maximum))
{
len = snprintf (buf, sizeof (buf), "%.*s=drop", tag_buf.length, tag_buf.arrayZ);
}
else
{
len = snprintf (buf, sizeof (buf), "%.*s=%g:%g:%g", tag_buf.length, tag_buf.arrayZ,
(double) triple.minimum, (double) triple.middle, (double) triple.maximum);
}
if (unlikely (len < 0 || (size_t) len >= sizeof (buf))) return false;
specs.extend (hb_bytes_t (buf, len));
}
if (specs.length)
{
hb_vector_t<char> opt;
opt.extend (hb_bytes_t ("--variations=", 13));
opt.extend (specs.as_array ());
opt.push ('\0');
if (opt.arrayZ) _append_arg (sb, opt.arrayZ);
}
return true;
}
/**
* hb_subset_input_to_string_or_fail:
* @input: a #hb_subset_input_t object.
*
* Produces a command line string representation of the given subset input
* suitable for use with the `util/hb-subset` command line tool.
*
* Return value: (transfer full): A new #hb_blob_t containing the command line
* string, or `NULL` if failed. Destroy with hb_blob_destroy().
*
* XSince: EXPERIMENTAL
**/
hb_blob_t *
hb_subset_input_to_string_or_fail (hb_subset_input_t *input)
{
if (unlikely (!input || input->in_error ()))
return nullptr;
hb_vector_t<char> sb;
struct flag_option_t {
hb_subset_flags_t flag;
const char *option;
};
static const flag_option_t flag_options[] = {
{ HB_SUBSET_FLAGS_NO_HINTING, "--no-hinting" },
{ HB_SUBSET_FLAGS_RETAIN_GIDS, "--retain-gids" },
{ HB_SUBSET_FLAGS_DESUBROUTINIZE, "--desubroutinize" },
{ HB_SUBSET_FLAGS_NAME_LEGACY, "--name-legacy" },
{ HB_SUBSET_FLAGS_SET_OVERLAPS_FLAG, "--set-overlaps-flag" },
{ HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED, "--passthrough-tables" },
{ HB_SUBSET_FLAGS_NOTDEF_OUTLINE, "--notdef-outline" },
{ HB_SUBSET_FLAGS_GLYPH_NAMES, "--glyph-names" },
{ HB_SUBSET_FLAGS_NO_PRUNE_UNICODE_RANGES, "--no-prune-unicode-ranges" },
{ HB_SUBSET_FLAGS_NO_LAYOUT_CLOSURE, "--no-layout-closure" },
{ HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS, "--optimize" },
{ HB_SUBSET_FLAGS_NO_BIDI_CLOSURE, "--no-bidi-closure" },
#ifdef HB_EXPERIMENTAL_API
{ HB_SUBSET_FLAGS_IFTB_REQUIREMENTS, "--iftb-requirements" },
{ HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS, "--retain-num-glyphs" },
#endif
{ HB_SUBSET_FLAGS_DOWNGRADE_CFF2, "--downgrade-cff2" },
};
#ifdef HB_EXPERIMENTAL_API
static_assert (sizeof (flag_options) / sizeof (flag_options[0]) == 15,
"Check all flags in hb_subset_flags_t are handled here.");
#else
static_assert (sizeof (flag_options) / sizeof (flag_options[0]) == 13,
"Check all flags in hb_subset_flags_t are handled here.");
#endif
for (const auto &fo : flag_options)
{
if (input->flags & fo.flag)
{
_append_arg (sb, fo.option);
}
}
hb_subset_input_t *default_input = hb_subset_input_create_or_fail ();
if (unlikely (!default_input))
return nullptr;
// Serialize all set flags if they are not equal to their default setting:
if (!_is_set_default (input->sets.unicodes, default_input->sets.unicodes))
_format_number_set_arg (input->sets.unicodes, "--unicodes", true, sb);
if (!_is_set_default (input->sets.glyphs, default_input->sets.glyphs))
_format_number_set_arg (input->sets.glyphs, "--gids", false, sb);
if (!_is_set_default (input->sets.name_ids, default_input->sets.name_ids))
_format_number_set_arg (input->sets.name_ids, "--name-IDs", false, sb);
if (!_is_set_default (input->sets.name_languages, default_input->sets.name_languages))
_format_number_set_arg (input->sets.name_languages, "--name-languages", false, sb);
if (!_is_set_default (input->sets.layout_features, default_input->sets.layout_features))
_format_tag_set_arg (input->sets.layout_features, "--layout-features", sb);
if (!_is_set_default (input->sets.layout_scripts, default_input->sets.layout_scripts))
_format_tag_set_arg (input->sets.layout_scripts, "--layout-scripts", sb);
if (!_is_set_default (input->sets.drop_tables, default_input->sets.drop_tables))
_format_tag_set_arg (input->sets.drop_tables, "--drop-tables", sb);
hb_subset_input_destroy (default_input);
static_assert (sizeof (input->sets) / sizeof (hb_set_t*) == 8,
"Check all sets in hb_subset_input_t::sets_t are handled here.");
if (unlikely (!_format_glyph_map_arg (input->glyph_map, sb)))
return nullptr;
if (unlikely (!_format_axes_location_arg (input->axes_location, sb)))
return nullptr;
if (unlikely (sb.in_error ()))
return nullptr;
char *buf = (char *) hb_malloc (sb.length + 1);
if (unlikely (!buf))
return nullptr;
if (sb.length)
hb_memcpy (buf, sb.arrayZ, sb.length);
buf[sb.length] = '\0';
return hb_blob_create_or_fail (buf, sb.length, HB_MEMORY_MODE_WRITABLE, buf, hb_free);
}
#endif
/**
* hb_subset_preprocess:
* @source: a #hb_face_t object.
+3
View File
@@ -228,6 +228,9 @@ hb_subset_axis_range_to_string (hb_subset_input_t *input,
unsigned size);
#ifdef HB_EXPERIMENTAL_API
HB_EXTERN hb_blob_t *
hb_subset_input_to_string_or_fail (hb_subset_input_t *input);
HB_EXTERN hb_bool_t
hb_subset_input_override_name_table (hb_subset_input_t *input,
hb_ot_name_id_t name_id,
+197
View File
@@ -403,6 +403,201 @@ test_subset_cff2_get_charstring_data_lifetime (void)
#endif
#ifdef HB_EXPERIMENTAL_API
static void
test_subset_input_to_string (void)
{
/* Test NULL input */
g_assert_null (hb_subset_input_to_string_or_fail (NULL));
/* Test default input -> empty string */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
g_assert_cmpint (len, ==, 0);
g_assert_cmpstr (data, ==, "");
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test unicodes ranges */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *unicodes = hb_subset_input_unicode_set (input);
hb_set_add (unicodes, 0x41);
hb_set_add (unicodes, 0x42);
hb_set_add (unicodes, 0x43);
hb_set_add (unicodes, 0x45);
hb_set_add (unicodes, 0x61);
hb_set_add (unicodes, 0x62);
hb_set_add (unicodes, 0x63);
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
g_assert_cmpstr (data, ==, "--unicodes=41-43,45,61-63");
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test gids ranges and flags */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *gids = hb_subset_input_glyph_set (input);
hb_set_add (gids, 1);
hb_set_add (gids, 2);
hb_set_add (gids, 3);
hb_set_add (gids, 5);
hb_set_add (gids, 10);
hb_set_add (gids, 11);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING | HB_SUBSET_FLAGS_RETAIN_GIDS);
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
g_assert_cmpstr (data, ==, "--no-hinting --retain-gids --gids=1-3,5,10-11");
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test keep everything */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_subset_input_keep_everything (input);
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--name-legacy --passthrough-tables --notdef-outline --glyph-names --no-prune-unicode-ranges --unicodes=* --gids=* --name-IDs=* --name-languages=* --layout-features=* --drop-tables-=*";
g_assert_cmpstr (data, ==, expected);
g_assert_cmpint (len, ==, strlen (expected));
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test inverted sets with exclusions */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *unicodes = hb_subset_input_set (input, HB_SUBSET_SETS_UNICODE);
hb_set_invert (unicodes);
hb_set_del (unicodes, 0x20);
hb_set_del (unicodes, 0x41);
hb_set_del (unicodes, 0x42);
hb_set_del (unicodes, 0x43);
hb_set_t *gids = hb_subset_input_set (input, HB_SUBSET_SETS_GLYPH_INDEX);
hb_set_invert (gids);
hb_set_del (gids, 5);
hb_set_del (gids, 6);
hb_set_del (gids, 7);
hb_set_t *features = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG);
hb_set_clear (features);
hb_set_invert (features);
hb_set_del (features, HB_TAG ('k', 'e', 'r', 'n'));
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--unicodes=* --unicodes-=20,41-43 --gids=* --gids-=5-7 --layout-features=* --layout-features-=kern";
g_assert_cmpstr (data, ==, expected);
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test empty non-default sets */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_clear (hb_subset_input_set (input, HB_SUBSET_SETS_NAME_ID));
hb_set_clear (hb_subset_input_set (input, HB_SUBSET_SETS_NAME_LANG_ID));
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--name-IDs-=* --name-languages-=*";
g_assert_cmpstr (data, ==, expected);
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test non-default non-inverted tag sets */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *features = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG);
hb_set_clear (features);
hb_set_add (features, HB_TAG ('l', 'i', 'g', 'a'));
hb_set_add (features, HB_TAG ('k', 'e', 'r', 'n'));
hb_set_t *scripts = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG);
hb_set_clear (scripts);
hb_set_add (scripts, HB_TAG ('l', 'a', 't', 'n'));
hb_set_t *drop_tables = hb_subset_input_set (input, HB_SUBSET_SETS_DROP_TABLE_TAG);
hb_set_clear (drop_tables);
hb_set_add (drop_tables, HB_TAG ('G', 'S', 'U', 'B'));
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--layout-features=kern,liga --layout-scripts=latn --drop-tables=GSUB";
g_assert_cmpstr (data, ==, expected);
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test tag sanitization with unsafe ASCII characters */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *features = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG);
hb_set_clear (features);
/* Tag with space, newline, and single-quote: 'a \n'' */
hb_set_add (features, HB_TAG ('a', ' ', '\n', '\''));
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--layout-features=a___";
g_assert_cmpstr (data, ==, expected);
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
/* Test trimming trailing whitespace from tags */
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_t *drop_tables = hb_subset_input_set (input, HB_SUBSET_SETS_DROP_TABLE_TAG);
hb_set_clear (drop_tables);
hb_set_add (drop_tables, HB_TAG ('c', 'v', 't', ' '));
hb_set_t *features = hb_subset_input_set (input, HB_SUBSET_SETS_LAYOUT_FEATURE_TAG);
hb_set_clear (features);
hb_set_add (features, HB_TAG (' ', ' ', ' ', ' '));
hb_blob_t *blob = hb_subset_input_to_string_or_fail (input);
g_assert_nonnull (blob);
unsigned len = 0;
const char *data = hb_blob_get_data (blob, &len);
const char *expected = "--layout-features=____ --drop-tables=cvt";
g_assert_cmpstr (data, ==, expected);
hb_blob_destroy (blob);
hb_subset_input_destroy (input);
}
}
#endif
int
main (int argc, char **argv)
{
@@ -417,6 +612,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_create_for_tables_face);
#ifdef HB_EXPERIMENTAL_API
hb_test_add (test_subset_input_to_string);
hb_test_add (test_subset_cff2_get_charstring_data);
hb_test_add (test_subset_cff2_get_all_charstrings_data);
hb_test_add (test_subset_cff2_get_charstring_data_no_cff);
@@ -426,3 +622,4 @@ main (int argc, char **argv)
return hb_test_run();
}