diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 251e9f7f4..42a5a76fe 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -166,6 +166,19 @@ struct hb_aat_apply_context_t : buffer_is_reversed = !buffer_is_reversed; } + /* Applying a subtable walks the whole buffer. The font controls the + * number of subtables, so charge each pass to the buffer's work budget. */ + HB_NODISCARD bool charge_buffer_pass () + { + buffer->max_ops -= buffer->len; + if (unlikely (buffer->max_ops < 0)) + { + buffer->successful = false; + return false; + } + return true; + } + void setup_buffer_glyph_set () { using_buffer_glyph_set = buffer->len >= 4 && buffer_glyph_set; @@ -369,16 +382,44 @@ struct LookupFormat2 template void collect_glyphs (set_t &glyphs) const { - unsigned count = segments.get_length (); - for (unsigned int i = 0; i < count; i++) - segments[i].collect_glyphs (glyphs); + collect_glyphs_filtered (glyphs, [] (const T &) { return true; }); } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const { + /* Segments are supposed to be sorted and disjoint, in which case adding + * them one by one touches each page of the set at most once. The + * sanitizer does not check that, and a malformed table can repeat a + * segment spanning the whole glyph range thousands of times, each + * costing a full pass over the set. So once a segment is out of order, + * add the rest as a single range; that is a superset of the glyphs, + * which is fine for a set that is only used as a pre-filter. */ unsigned count = segments.get_length (); - for (unsigned int i = 0; i < count; i++) - segments[i].collect_glyphs_filtered (glyphs, filter); + hb_codepoint_t next = 0; /* One past the last glyph added. */ + unsigned i; + for (i = 0; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first < next) break; /* Not sorted and disjoint. */ + if (segment.first == DELETED_GLYPH || segment.first > segment.last || + !filter (segment.value)) + continue; + glyphs.add_range (segment.first, segment.last); + next = segment.last + 1; + } + + hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; + for (; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first == DELETED_GLYPH || segment.first > segment.last || + !filter (segment.value)) + continue; + first = hb_min (first, (hb_codepoint_t) segment.first); + last = hb_max (last, (hb_codepoint_t) segment.last); + } + if (first <= last) + glyphs.add_range (first, last); } bool sanitize (hb_sanitize_context_t *c) const @@ -948,7 +989,8 @@ struct StateTable const HBUSHORT *states = (this+stateArrayTable).arrayZ; const Entry *entries = (this+entryTable).arrayZ; - unsigned int entry = states[state * n_classes + klass]; + /* State can be negative; see sanitize(). Keep the index signed. */ + unsigned int entry = states[state * (int) n_classes + (int) klass]; DEBUG_MSG (APPLY, nullptr, "e%u", entry); return entries[entry]; @@ -997,16 +1039,17 @@ struct StateTable if (min_state < state_neg) { /* Negative states. */ - if (unlikely (hb_unsigned_mul_overflows (min_state, num_classes))) + unsigned int num_neg_states = -min_state; + if (unlikely (hb_unsigned_mul_overflows (num_neg_states, num_classes))) return_trace (false); - if (unlikely (!c->check_range (&states[min_state * num_classes], - -min_state, + if (unlikely (!c->check_range (states - num_neg_states * num_classes, + num_neg_states, row_stride))) return_trace (false); if ((c->max_ops -= state_neg - min_state) <= 0) return_trace (false); { /* Sweep new states. */ - const HBUSHORT *stop = &states[min_state * num_classes]; + const HBUSHORT *stop = states - num_neg_states * num_classes; if (unlikely (stop > states)) return_trace (false); for (const HBUSHORT *p = states; stop < p; p--) diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index c53d6e353..7b22098f2 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -613,6 +613,10 @@ struct KerxSubTableFormat4 const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; if (!c->sanitizer.check_array (data, 2)) return; hb_barrier (); + /* Font functions may load the whole glyph outline to find a contour + * point, and a non-advancing transition can repeat that at will. + * Charge the two lookups to the buffer's work budget. */ + if (unlikely ((buffer->max_ops -= 2 * HB_BUDGET_256) <= 0)) return; unsigned int markControlPoint = *data++; unsigned int currControlPoint = *data++; hb_position_t markX = 0; @@ -1087,6 +1091,9 @@ struct KerxTable reverse = bool (st->u.header.coverage & st->u.header.Backwards) != HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction); + if (unlikely (!c->charge_buffer_pass ())) + break; + if (!c->buffer->message (c->font, "start subtable %u", c->lookup_index)) goto skip; @@ -1118,6 +1125,8 @@ struct KerxTable (void) c->buffer->message (c->font, "end subtable %u", c->lookup_index); + if (unlikely (!c->buffer->successful)) break; + skip: st = &StructAfter (*st); c->set_lookup_index (c->lookup_index + 1); @@ -1141,6 +1150,9 @@ struct KerxTable const SubTable *st = &thiz()->firstSubTable; unsigned int count = thiz()->tableCount; + /* The accelerator keeps two glyph sets per subtable. */ + if (unlikely (count > HB_AAT_MAX_SUBTABLES)) + return_trace (false); for (unsigned int i = 0; i < count; i++) { if (unlikely (!st->u.header.sanitize (c))) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index aa6081131..fdd2776a8 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -976,7 +976,7 @@ struct hb_accelerate_subtables_context_t : template return_t dispatch (const T &obj) { - hb_applicable_t *entry = &array[i++]; + hb_applicable_t *entry = &array[i]; entry->init (obj, num_glyphs); @@ -992,7 +992,7 @@ struct hb_accelerate_subtables_context_t : hb_applicable_t *array; unsigned num_glyphs; - unsigned i = 0; + unsigned i = 0; /* Index of the subtable being accelerated; set by Chain::accelerate(). */ }; struct hb_aat_layout_chain_accelerator_t @@ -1025,7 +1025,7 @@ struct hb_aat_layout_chain_accelerator_t thiz->count = count; hb_accelerate_subtables_context_t c_accelerate_subtables (thiz->subtables, num_glyphs); - chain.dispatch (&c_accelerate_subtables); + chain.accelerate (&c_accelerate_subtables); return thiz; } @@ -1140,6 +1140,9 @@ struct Chain hb_mask_t flags = defaultFlags; { unsigned int count = featureCount; + /* Charge the chain and its feature records; the map builder stops + * compiling further ranges once its budget is exhausted. */ + hb_budget_spend (map->budget, HB_BUDGET_1, 1 + count); for (unsigned i = 0; i < count; i++) { const Feature &feature = featureZ[i]; @@ -1164,7 +1167,7 @@ struct Chain #ifndef HB_NO_AAT else if (type == HB_AAT_LAYOUT_FEATURE_TYPE_LANGUAGE_TAG_TYPE && setting && /* TODO: Rudimentary language matching. */ - hb_language_matches (map->face->table.ltag->get_language (setting - 1), map->props.language)) + map->face->table.ltag->matches_language (setting - 1, map->props.language)) { flags &= feature.disableFlags; flags |= feature.enableFlags; @@ -1238,6 +1241,9 @@ struct Chain bool (coverage & ChainSubtable::Backwards) != HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction); + if (unlikely (!c->charge_buffer_pass ())) + break; + if (!c->buffer->message (c->font, "start chainsubtable %u", c->lookup_index)) goto skip; @@ -1260,6 +1266,22 @@ struct Chain size_t get_size () const { return length; } + /* Fills one accelerator entry per subtable, indexed by subtable position + * so that apply() can look them up by position. Subtables of unknown + * type are skipped by ChainSubtable::dispatch() and keep their zeroed + * entry, which is fine since apply() does not run them either. */ + void accelerate (hb_accelerate_subtables_context_t *c) const + { + const ChainSubtable *subtable = &StructAfter> (featureZ.as_array (featureCount)); + unsigned int count = subtableCount; + for (unsigned int i = 0; i < count; i++) + { + c->i = i; + subtable->dispatch (c); + subtable = &StructAfter> (*subtable); + } + } + template typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const { @@ -1385,6 +1407,7 @@ struct mortmorx if (unlikely (!accels[chain_index].cmpexch (nullptr, accel))) { + accel->destroy (); hb_free (accel); goto retry; } @@ -1409,9 +1432,22 @@ struct mortmorx return; for (unsigned int i = 0; i < count; i++) { - map->chain_flags[i].push (hb_aat_map_t::range_flags_t {chain->compile_flags (mapper), - mapper->range_first, - mapper->range_last}); + hb_mask_t flags = chain->compile_flags (mapper); + auto &ranges = map->chain_flags[i]; + /* Ranges are compiled in order and are contiguous. Extend the last + * one instead of starting a new one if this chain does not tell the + * two apart; the map would otherwise hold one entry per chain per + * range, a product the font and the caller's features do not bound. */ + if (ranges.length && ranges.tail ().flags == flags) + ranges.tail ().cluster_last = mapper->range_last; + else + { + /* Stored ranges cost memory; charge them more than visited ones. */ + hb_budget_spend (mapper->budget, HB_BUDGET_16); + ranges.push (hb_aat_map_t::range_flags_t {flags, + mapper->range_first, + mapper->range_last}); + } chain = &StructAfter> (*chain); } } @@ -1454,11 +1490,16 @@ struct mortmorx const Chain *chain = &firstChain; unsigned int count = chainCount; + unsigned int subtable_count = 0; for (unsigned int i = 0; i < count; i++) { if (!chain->sanitize (c, version)) return_trace (false); hb_barrier (); + /* The accelerator keeps a glyph set per subtable. */ + subtable_count += chain->get_subtable_count (); + if (unlikely (subtable_count > HB_AAT_MAX_SUBTABLES)) + return_trace (false); chain = &StructAfter> (*chain); } diff --git a/src/hb-aat-ltag-table.hh b/src/hb-aat-ltag-table.hh index c974025d4..6dc69b0e8 100644 --- a/src/hb-aat-ltag-table.hh +++ b/src/hb-aat-ltag-table.hh @@ -71,6 +71,32 @@ struct ltag range.length); } + /* Equivalent to hb_language_matches (get_language (i), specific), but + * without interning the font's tag: hb_language_from_string() appends + * every new string to a process-wide, linearly-searched, never-freed list, + * so a hostile font with many tags and many feature records referencing + * them could make that quadratic. */ + bool matches_language (unsigned int i, hb_language_t specific) const + { + const char *s = hb_language_to_string (specific); + if (!s) return false; + + const FTStringRange &range = tagRanges[i]; + const HBUINT8 *tag = (this+range.tag).arrayZ; + /* Canonicalize the same way hb_language_from_string() does: at most 63 + * bytes, lowercase, '_' to '-', stop at the first other non-alphanumeric. */ + unsigned len = hb_min ((unsigned) range.length, 63u); + unsigned l = 0; + for (; l < len; l++) + { + unsigned char c = TOLOWER (tag[l]); + if (c == '_') c = '-'; + if (!ISALNUM (c) && c != '-') break; + if (c != (unsigned char) s[l]) return false; + } + return l && (s[l] == '\0' || s[l] == '-'); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); diff --git a/src/hb-aat-map.cc b/src/hb-aat-map.cc index 0b85c901e..33e2b7cc1 100644 --- a/src/hb-aat-map.cc +++ b/src/hb-aat-map.cc @@ -156,6 +156,11 @@ hb_aat_map_builder_t::compile (hb_aat_map_t &m) hb_aat_layout_compile_map (this, &m); + /* Out of budget? Stop here; the adjustment below extends the ranges + * compiled so far to the end of the text. */ + if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, active_features.length))) + break; + last_index = event->index; } diff --git a/src/hb-aat-map.hh b/src/hb-aat-map.hh index cb22ffee4..33a94ca96 100644 --- a/src/hb-aat-map.hh +++ b/src/hb-aat-map.hh @@ -117,6 +117,7 @@ struct hb_aat_map_builder_t hb_sorted_vector_t current_features; unsigned range_first = HB_FEATURE_GLOBAL_START; unsigned range_last = HB_FEATURE_GLOBAL_END; + mutable int64_t budget = HB_BUDGET_AAT_MAP; }; diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 1e1cebd9f..49fe45c29 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -92,6 +92,13 @@ #define HB_MAX_LOOKUP_VISIT_COUNT 35000 #endif +/* Subtables of one kern, kerx, mort, or morx table; the table is rejected + * when it declares more. The table's accelerator keeps glyph sets for + * each, which can take far more memory than the subtable has bytes. */ +#ifndef HB_AAT_MAX_SUBTABLES +#define HB_AAT_MAX_SUBTABLES 1024 +#endif + #ifndef HB_MAX_GRAPH_EDGE_COUNT #define HB_MAX_GRAPH_EDGE_COUNT 16384 #endif @@ -261,6 +268,17 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_RASTER_MAX_DRAW_EDGES ((int64_t) 1 << 20) #endif +/* Compiling one AAT feature map: morx/mort chains and chain feature + * records visited for each range of the text with its own set of + * features, plus extra for each range a chain keeps rather than merges + * into the previous one, since those are what the map stores. The + * ranges come from the caller's features and the chains from the font, + * so neither input bounds their product. Once exhausted, the last + * range compiled extends to the end of the text. */ +#ifndef HB_BUDGET_AAT_MAP +#define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24) +#endif + #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 diff --git a/test/api/test-aat-layout.c b/test/api/test-aat-layout.c index e4e85001b..26e4755b6 100644 --- a/test/api/test-aat-layout.c +++ b/test/api/test-aat-layout.c @@ -116,6 +116,258 @@ test_aat_has (void) hb_face_destroy (trak); } + +static hb_bool_t +count_started_subtables (hb_buffer_t *buffer HB_UNUSED, + hb_font_t *font HB_UNUSED, + const char *message, + void *user_data) +{ + if (!strncmp (message, "start subtable ", 15) || + !strncmp (message, "start chainsubtable ", 20)) + (*(unsigned *) user_data)++; + return true; +} + +/* Shapes "aa" with a two-glyph font that has the table, which is + * freed; returns the number of its subtables that were applied. + * A table with more than HB_AAT_MAX_SUBTABLES subtables is rejected + * by sanitize, so none of them is. */ +static unsigned +count_applied_subtables (hb_tag_t tag, char *table, unsigned length) +{ + static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; + static const char cmap[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12, + 0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1}; + + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (table, length, HB_MEMORY_MODE_READONLY, table, free); + hb_face_builder_add_table (builder, tag, blob); + hb_blob_destroy (blob); + blob = hb_face_reference_blob (builder); + hb_face_destroy (builder); + hb_face_t *limit_face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + hb_font_t *font = hb_font_create (limit_face); + hb_face_destroy (limit_face); + + /* This exercises the OT/AAT shaper; other shapers (e.g. HarfRust) do not + * implement AAT, so pin it. */ + static const char *shaper_list[] = {"ot", NULL}; + unsigned started = 0; + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_buffer_set_message_func (buffer, count_started_subtables, &started, NULL); + hb_shape_full (font, buffer, NULL, 0, shaper_list); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + + return started; +} + +/* A kern table of format-3 subtables. */ +static char * +make_kern_table (unsigned num_subtables, unsigned *length) +{ + *length = 4 + 14 * num_subtables; + char *kern = (char *) calloc (*length, 1); + kern[2] = (char) (num_subtables >> 8); + kern[3] = (char) (num_subtables & 0xFF); + for (unsigned i = 0; i < num_subtables; i++) + { + char *subtable = kern + 4 + 14 * i; + subtable[3] = 14; /* length */ + subtable[4] = 3; /* format */ + subtable[5] = 1; /* coverage: horizontal */ + subtable[7] = 1; /* glyphCount */ + } + return kern; +} + +static void +test_aat_kern_subtable_limit (void) +{ + unsigned length; + char *kern; + + kern = make_kern_table (1024, &length); + g_assert_cmpuint (1024, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length)); + + kern = make_kern_table (1025, &length); + g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length)); +} + +/* A morx table of chains of five noncontextual subtables each. */ +static char * +make_morx_table (unsigned num_chains, unsigned *length) +{ + *length = 8 + (16 + 5 * 20) * num_chains; + char *morx = (char *) calloc (*length, 1); + morx[1] = 2; /* version */ + morx[6] = (char) (num_chains >> 8); + morx[7] = (char) (num_chains & 0xFF); + for (unsigned i = 0; i < num_chains; i++) + { + char *chain = morx + 8 + (16 + 5 * 20) * i; + chain[3] = 1; /* defaultFlags */ + chain[7] = 16 + 5 * 20; /* length */ + chain[15] = 5; /* subtableCount */ + for (unsigned j = 0; j < 5; j++) + { + char *subtable = chain + 16 + 20 * j; + subtable[3] = 20; /* length */ + subtable[4] = 0x20; /* coverage: all directions */ + subtable[7] = 4; /* type */ + subtable[11] = 1; /* subFeatureFlags */ + subtable[13] = 8; /* lookup format */ + subtable[15] = 1; /* firstGlyph */ + subtable[17] = 1; /* glyphCount */ + subtable[19] = 1; /* value */ + } + } + return morx; +} + +static void +test_aat_morx_subtable_limit (void) +{ + unsigned length; + char *morx; + + morx = make_morx_table (204, &length); /* 1020 subtables */ + g_assert_cmpuint (1020, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length)); + + morx = make_morx_table (205, &length); /* 1025 subtables */ + g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length)); +} + + +/* A font mapping 'a' and 'b' to glyphs 1 and 2. Its first morx chain turns + * glyph 1 into glyph 2 where common ligatures are on; they are off by default. + * That chain is followed by empty_chains chains that have nothing in them. */ +static hb_face_t * +create_morx_face (unsigned int empty_chains) +{ + static const unsigned char cmap[] = { + 0x00,0x00, 0x00,0x01, 0x00,0x03, 0x00,0x0A, 0x00,0x00,0x00,0x0C, + 0x00,0x0C, 0x00,0x00, 0x00,0x00,0x00,0x1C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, + 0x00,0x00,0x00,0x61, 0x00,0x00,0x00,0x62, 0x00,0x00,0x00,0x01, + }; + static const unsigned char maxp[] = {0x00,0x00,0x50,0x00, 0x00,0x03}; + static const unsigned char feat[] = { + 0x00,0x01,0x00,0x00, 0x00,0x01, 0x00,0x00, 0x00,0x00,0x00,0x00, + 0x00,0x01, 0x00,0x02, 0x00,0x00,0x00,0x18, 0x00,0x00, 0x01,0x00, + 0x00,0x02, 0x01,0x00, 0x00,0x03, 0x01,0x00, + }; + static const unsigned char chain[] = { + 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x3C, 0x00,0x00,0x00,0x02, 0x00,0x00,0x00,0x01, + 0x00,0x01, 0x00,0x02, 0x00,0x00,0x00,0x01, 0xFF,0xFF,0xFF,0xFF, + 0x00,0x01, 0x00,0x03, 0x00,0x00,0x00,0x00, 0xFF,0xFF,0xFF,0xFE, + 0x00,0x00,0x00,0x14, 0x20,0x00,0x00,0x04, 0x00,0x00,0x00,0x01, + 0x00,0x08, 0x00,0x01, 0x00,0x01, 0x00,0x02, + }; + unsigned int chain_count = 1 + empty_chains; + unsigned int morx_length = 8 + sizeof (chain) + 16 * empty_chains; + unsigned char *morx = (unsigned char *) calloc (1, morx_length); + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob; + + g_assert_nonnull (morx); + morx[1] = 2; + morx[4] = chain_count >> 24; + morx[5] = chain_count >> 16; + morx[6] = chain_count >> 8; + morx[7] = chain_count; + memcpy (morx + 8, chain, sizeof (chain)); + for (unsigned int i = 0; i < empty_chains; i++) + morx[8 + sizeof (chain) + 16 * i + 7] = 16; + + blob = hb_blob_create ((const char *) cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) feat, sizeof (feat), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('f','e','a','t'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) morx, morx_length, HB_MEMORY_MODE_READONLY, morx, free); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('m','o','r','x'), blob)); + hb_blob_destroy (blob); + + return builder; +} + +/* Shapes a run of 'a' that has ligatures on in every character whose index + * is not a multiple of period, each character having its own feature range. */ +static hb_buffer_t * +shape_ranged_features (hb_face_t *morx_face, unsigned int length, unsigned int period) +{ + hb_font_t *font = hb_font_create (morx_face); + hb_buffer_t *buffer = hb_buffer_create (); + hb_feature_t *features = (hb_feature_t *) calloc (length, sizeof (hb_feature_t)); + + g_assert_nonnull (features); + for (unsigned int i = 0; i < length; i++) + { + hb_buffer_add (buffer, 'a', i); + features[i].tag = HB_TAG ('l','i','g','a'); + features[i].value = i % period != 0; + features[i].start = i; + features[i].end = i + 1; + } + hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); + hb_buffer_guess_segment_properties (buffer); + /* AAT morx is the OT/AAT shaper only; other shapers (e.g. HarfRust) do not + * implement it, so pin it. */ + static const char *shaper_list[] = {"ot", NULL}; + hb_shape_full (font, buffer, features, length, shaper_list); + g_assert_cmpuint (length, ==, hb_buffer_get_length (buffer)); + + free (features); + hb_font_destroy (font); + return buffer; +} + +static void +test_aat_ranged_features (void) +{ + hb_face_t *morx_face = create_morx_face (1); + hb_buffer_t *buffer = shape_ranged_features (morx_face, 100, 3); + hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); + + for (unsigned int i = 0; i < 100; i++) + g_assert_cmpuint (i % 3 ? 2 : 1, ==, info[i].codepoint); + + hb_buffer_destroy (buffer); + hb_face_destroy (morx_face); +} + +static void +test_aat_ranged_features_many_chains (void) +{ + /* The number of chains times the number of ranges is what must not + * be paid for, in time or memory. The first ranges still work. */ + hb_face_t *morx_face = create_morx_face (20000); + hb_buffer_t *buffer = shape_ranged_features (morx_face, 5000, 2); + hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); + + for (unsigned int i = 0; i < 100; i++) + g_assert_cmpuint (i % 2 ? 2 : 1, ==, info[i].codepoint); + + hb_buffer_destroy (buffer); + hb_face_destroy (morx_face); +} + #endif int @@ -128,6 +380,10 @@ main (int argc, char **argv) hb_test_add (test_aat_get_feature_types); hb_test_add (test_aat_get_feature_selectors); hb_test_add (test_aat_has); + hb_test_add (test_aat_kern_subtable_limit); + hb_test_add (test_aat_morx_subtable_limit); + hb_test_add (test_aat_ranged_features); + hb_test_add (test_aat_ranged_features_many_chains); #endif face = hb_test_open_font_file ("fonts/aat-feat.ttf"); diff --git a/test/api/test-shape.c b/test/api/test-shape.c index 37571e18e..a7e706dba 100644 --- a/test/api/test-shape.c +++ b/test/api/test-shape.c @@ -242,6 +242,88 @@ test_shape_overlong_cluster_remerge (void) } + +#ifndef HB_NO_AAT +static hb_bool_t +count_contour_point_calls (hb_font_t *font HB_UNUSED, + void *font_data, + hb_codepoint_t glyph HB_UNUSED, + unsigned int point_index HB_UNUSED, + hb_position_t *x, + hb_position_t *y, + void *user_data HB_UNUSED) +{ + (*(unsigned *) font_data)++; + *x = *y = 0; + return TRUE; +} + +static void +test_shape_kerx_control_point_actions_are_charged (void) +{ + /* A two-glyph font whose kerx format-4 subtable loops on the letter 'a' + * in state 1 without advancing, performing a control-point action on + * every transition. Each action asks the font for two contour points, + * which may load a whole glyph outline, so shaping must charge them to + * the buffer's work budget. */ + static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; + static const char cmap[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12, + 0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1}; + static const unsigned char kerx[] = { + 0, 2, 0, 0, 0, 0, 0, 1, /* version 2, one subtable */ + 0, 0, 0, 82, 0, 0, 0, 4, 0, 0, 0, 0, /* length, format 4, tupleCount */ + 0, 0, 0, 5, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 48, /* nClasses, class, state, entry offsets */ + 0, 0, 0, 66, /* action type 0, control points at 66 */ + 0, 8, 0, 1, 0, 1, 0, 4, /* class lookup format 8: glyph 1 is class 4 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* state 0: class 4 takes entry 1 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, /* state 1: class 4 takes entry 2 */ + 0, 0, 0x00, 0, 0xFF, 0xFF, /* entry 0: to state 0 */ + 0, 1, 0xC0, 0, 0xFF, 0xFF, /* entry 1: to state 1, Mark, DontAdvance */ + 0, 1, 0x40, 0, 0, 0, /* entry 2: to state 1, DontAdvance, action 0 */ + 0, 0, 0, 0 /* action 0: mark point 0, current point 0 */ + }; + static const char *shapers[] = {"ot", NULL}; + + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) kerx, sizeof (kerx), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('k','e','r','x'), blob); + hb_blob_destroy (blob); + blob = hb_face_reference_blob (builder); + hb_face_destroy (builder); + hb_face_t *face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + + unsigned calls = 0; + hb_font_t *parent = hb_font_create (face); + hb_font_t *font = hb_font_create_sub_font (parent); + hb_font_funcs_t *funcs = hb_font_funcs_create (); + hb_font_funcs_set_glyph_contour_point_func (funcs, count_contour_point_calls, NULL, NULL); + hb_font_set_funcs (font, funcs, &calls, NULL); + hb_font_funcs_destroy (funcs); + + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape_full (font, buffer, NULL, 0, shapers); + + /* 131074 before the budget was charged. */ + g_assert_cmpuint (calls, >=, 2); + g_assert_cmpuint (calls, <=, 256); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + hb_font_destroy (parent); + hb_face_destroy (face); +} +#endif + static void test_shape_harfrust_budget (void) { @@ -315,6 +397,9 @@ main (int argc, char **argv) hb_test_add (test_shape_overlong_mark_cluster); hb_test_add (test_shape_overlong_cluster_remerge); hb_test_add (test_shape_harfrust_budget); +#ifndef HB_NO_AAT + hb_test_add (test_shape_kerx_control_point_actions_are_charged); +#endif /* TODO test fallback shaper */ /* TODO test shaper_full */ hb_test_add (test_shape_list); diff --git a/test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf b/test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf new file mode 100644 index 000000000..b14d64be7 Binary files /dev/null and b/test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf differ diff --git a/test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf b/test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf new file mode 100644 index 000000000..eb1503526 Binary files /dev/null and b/test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf differ diff --git a/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf b/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf new file mode 100644 index 000000000..c3f0d5311 Binary files /dev/null and b/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf differ diff --git a/test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf b/test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf new file mode 100644 index 000000000..a7535a861 Binary files /dev/null and b/test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf differ diff --git a/test/shape/data/in-house/meson.build b/test/shape/data/in-house/meson.build index f1417fe67..678faf58e 100644 --- a/test/shape/data/in-house/meson.build +++ b/test/shape/data/in-house/meson.build @@ -47,6 +47,7 @@ in_house_tests = [ 'indic-vowel-letter-spoofing.tests', 'item-context.tests', 'kbts.tests', + 'kern-format1.tests', 'kern-format2.tests', 'khmer-mark-order.tests', 'khmer-misc.tests', diff --git a/test/shape/data/in-house/tests/aat-morx.tests b/test/shape/data/in-house/tests/aat-morx.tests index fffc7469b..100c646e4 100644 --- a/test/shape/data/in-house/tests/aat-morx.tests +++ b/test/shape/data/in-house/tests/aat-morx.tests @@ -3,3 +3,15 @@ ../fonts/5f1d25b1f66fc4ef6d55b7c98f34b045b4adaa9d.ttf;;U+0041;[A=0+639|B=0+626|C=0+596|E=0+556] ../fonts/007e2c13e6baf28a7f1e8f981c2db0cb850b1f1f.ttf;--no-glyph-names;U+0061,U+0062,U+0063;[3=0+600|5=2+600] ../fonts/007e2c13e6baf28a7f1e8f981c2db0cb850b1f1f.ttf;--no-glyph-names;U+0061,U+0062,U+0063,U+0063;[3=0+600|5=2+600|4=3+600] +../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062;[2|2] +../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062,U+0063;[2|2|1] +../fonts/3503b8963831c2381809884633f483b7361baa97.ttf;--font-funcs=ot;U+0062,U+0062,U+0062,U+0062;[gid3=0+500|gid3=1+500|gid3=2+500|gid3=3+500] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=SR;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr-Latn-RS;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=srb;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=s;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=en;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh-hant;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=ZH_HANT;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh;U+0041;[gid1=0+600] diff --git a/test/shape/data/in-house/tests/kern-format1.tests b/test/shape/data/in-house/tests/kern-format1.tests new file mode 100644 index 000000000..d83525d7f --- /dev/null +++ b/test/shape/data/in-house/tests/kern-format1.tests @@ -0,0 +1 @@ +../fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf;;U+0061,U+0062;[gid1=0@-100,0+1014|gid2=1+1149]