diff --git a/perf/benchmark-subset.cc b/perf/benchmark-subset.cc index b86150a37..e1cae862a 100644 --- a/perf/benchmark-subset.cc +++ b/perf/benchmark-subset.cc @@ -55,21 +55,22 @@ struct test_input_t unsigned max_subset_size; const axis_location_t *instance_opts; unsigned num_instance_opts; + bool no_hinting; } default_tests[] = { - {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 1000, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4096, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1400, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf", 300, nullptr, 0}, - {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts)}, - {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts)}, - {SUBSET_FONT_BASE_PATH "Fraunces.ttf", 900, _fraunces_partial_instance_opts, ARRAY_LEN (_fraunces_partial_instance_opts)}, + {SUBSET_FONT_BASE_PATH "Roboto-Regular.ttf", 1000, nullptr, 0, true}, + {SUBSET_FONT_BASE_PATH "Amiri-Regular.ttf", 4096, nullptr, 0, true}, + {SUBSET_FONT_BASE_PATH "NotoNastaliqUrdu-Regular.ttf", 1400, nullptr, 0, true}, + {SUBSET_FONT_BASE_PATH "NotoSansDevanagari-Regular.ttf", 1000, nullptr, 0, true}, + {SUBSET_FONT_BASE_PATH "Mplus1p-Regular.ttf", 10000, nullptr, 0, false}, + {SUBSET_FONT_BASE_PATH "SourceHanSans-Regular_subset.otf", 10000, nullptr, 0, false}, + {SUBSET_FONT_BASE_PATH "SourceSansPro-Regular.otf", 2000, nullptr, 0, false}, + {SUBSET_FONT_BASE_PATH "AdobeVFPrototype.otf", 300, nullptr, 0, false}, + {SUBSET_FONT_BASE_PATH "MPLUS1-Variable.ttf", 6000, _mplus_instance_opts, ARRAY_LEN (_mplus_instance_opts), false}, + {SUBSET_FONT_BASE_PATH "RobotoFlex-Variable.ttf", 900, _roboto_flex_instance_opts, ARRAY_LEN (_roboto_flex_instance_opts), false}, + {SUBSET_FONT_BASE_PATH "Fraunces.ttf", 900, _fraunces_partial_instance_opts, ARRAY_LEN (_fraunces_partial_instance_opts), false}, #if 0 - {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000}, + {"perf/fonts/NotoSansCJKsc-VF.ttf", 100000, nullptr, 0, false}, #endif }; @@ -156,8 +157,13 @@ static void BM_subset (benchmark::State &state, hb_subset_input_t* input = hb_subset_input_create_or_fail (); assert (input); + unsigned flags = 0; + if (test_input.no_hinting) + flags |= HB_SUBSET_FLAGS_NO_HINTING; if (retain_gids) - hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS); + flags |= HB_SUBSET_FLAGS_RETAIN_GIDS; + if (flags) + hb_subset_input_set_flags (input, flags); switch (operation) { @@ -219,6 +225,8 @@ static void test_subset (operation_t op, strcat (name, "/"); const char *p = strrchr (test_input.font_path, '/'); strcat (name, p ? p + 1 : test_input.font_path); + if (test_input.no_hinting) + strcat (name, "/nohinting"); if (retain_gids) strcat (name, "/retaingids"); diff --git a/src/OT/Layout/Common/CoverageFormat1.hh b/src/OT/Layout/Common/CoverageFormat1.hh index ee9425ec2..56f38de49 100644 --- a/src/OT/Layout/Common/CoverageFormat1.hh +++ b/src/OT/Layout/Common/CoverageFormat1.hh @@ -97,6 +97,14 @@ struct CoverageFormat1_3 hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))> void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const { + if (glyphArray.len > glyphs.get_population () * hb_bit_storage ((unsigned) glyphArray.len)) + { + for (auto g : glyphs) + if (get_coverage (g) != NOT_COVERED) + intersect_glyphs << g; + return; + } + unsigned count = glyphArray.len; for (unsigned i = 0; i < count; i++) if (glyphs.has (glyphArray[i])) diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index b22c4d782..db4053766 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -157,6 +157,14 @@ struct CoverageFormat2_4 hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))> void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const { + if (rangeRecord.len > glyphs.get_population () * hb_bit_storage ((unsigned) rangeRecord.len)) + { + for (auto g : glyphs) + if (get_coverage (g) != NOT_COVERED) + intersect_glyphs << g; + return; + } + /* Break out of loop for overlapping, broken, tables, * to avoid fuzzer timouts. */ hb_codepoint_t last = 0; diff --git a/src/OT/Layout/GDEF/GDEF.hh b/src/OT/Layout/GDEF/GDEF.hh index fe38fe326..591aa7879 100644 --- a/src/OT/Layout/GDEF/GDEF.hh +++ b/src/OT/Layout/GDEF/GDEF.hh @@ -453,6 +453,10 @@ struct MarkGlyphSetsFormat1 bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; } + unsigned get_count () const { return coverage.len; } + const Coverage &get_coverage (unsigned int set_index) const + { return this+coverage[set_index]; } + void collect_used_mark_sets (const hb_set_t& glyph_set, hb_set_t& used_mark_sets /* OUT */) const { @@ -467,18 +471,6 @@ struct MarkGlyphSetsFormat1 } } - template - void collect_coverage (hb_vector_t &sets) const - { - for (const auto &offset : coverage) - { - if (unlikely (!sets.resize (sets.length + 1))) - return; - const auto &cov = this+offset; - cov.collect_coverage (&sets.tail ()); - } - } - bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); @@ -539,12 +531,19 @@ struct MarkGlyphSets } } - template - void collect_coverage (hb_vector_t &sets) const + unsigned get_count () const { switch (u.format.v) { - case 1: hb_barrier (); u.format1.collect_coverage (sets); return; - default:return; + case 1: hb_barrier (); return u.format1.get_count (); + default:return 0; + } + } + + const Coverage &get_coverage (unsigned int set_index) const + { + switch (u.format.v) { + case 1: hb_barrier (); return u.format1.get_coverage (set_index); + default:return Null (Coverage); } } @@ -989,36 +988,38 @@ struct GDEF #ifndef HB_NO_GDEF_CACHE /* Flatten each mark set whose glyphs span fewer than PAGE_BITS - * gids into a single bit-page biased at its first glyph, for + * gids into a bit-vector biased at its first glyph, for * O(1) exact answers. Wider sets, or on allocation failure, - * fall back to the table. */ + * fall back to the table. + * + * Fill each entry straight from its Coverage table, so the work + * is proportional to the coverage records the sanitizer budgeted + * for, not to the glyphs they span: materializing an hb_set_t per + * set and walking it glyph by glyph let a 256kb font with 65535 + * all-glyph sets cost ~1GB and billions of iterations here. */ { - hb_vector_t sets; - table->get_mark_glyph_sets ().collect_coverage (sets); - if (!sets.in_error () && - mark_glyph_set_bitmaps.resize (sets.length)) - for (unsigned i = 0; i < sets.length; i++) + const auto &mark_sets = table->get_mark_glyph_sets (); + unsigned count = mark_sets.get_count (); + if (mark_glyph_set_bitmaps.resize (count)) + for (unsigned i = 0; i < count; i++) { auto &bitmap = mark_glyph_set_bitmaps.arrayZ[i]; - bitmap.bias = HB_SET_VALUE_INVALID; - const auto &set = sets.arrayZ[i]; - if (unlikely (set.in_error ())) continue; - hb_codepoint_t min_gid = HB_SET_VALUE_INVALID; - hb_codepoint_t max_gid = HB_SET_VALUE_INVALID; - hb_set_next (&set, &min_gid); - hb_set_previous (&set, &max_gid); - if (max_gid == HB_SET_VALUE_INVALID) + const Coverage &cov = mark_sets.get_coverage (i); + hb_min_max_t extents; + cov.collect_coverage (&extents); + if (extents.get_min () == HB_SET_VALUE_INVALID) continue; - if (max_gid - min_gid >= hb_bit_page_t::PAGE_BITS) + if (extents.get_max () - extents.get_min () < hb_bit_page_t::PAGE_BITS) { - /* Too wide for one page; a digest pre-filter instead. */ - for (hb_codepoint_t gid : set) - bitmap.u.digest.add (gid); - continue; + bitmap.glyphs = hb_bit_vector_t<> (extents.get_min (), extents.get_max ()); + if (likely (!bitmap.glyphs.in_error ())) + { + cov.collect_coverage (&bitmap.glyphs); + continue; + } } - bitmap.bias = min_gid; - for (hb_codepoint_t gid : set) - bitmap.u.page.add (gid - min_gid); + /* Too wide, or allocation failed; a digest pre-filter instead. */ + cov.collect_coverage (&bitmap.digest); } } #endif @@ -1053,12 +1054,8 @@ struct GDEF #ifndef HB_NO_GDEF_CACHE // We can access arrayZ directly because of sanitize_lookup_props() guarantee. const auto &bitmap = mark_glyph_set_bitmaps.arrayZ[set_index]; - if (likely (bitmap.bias != HB_SET_VALUE_INVALID)) - { - hb_codepoint_t biased = glyph_id - bitmap.bias; - return biased < hb_bit_page_t::PAGE_BITS && - bitmap.u.page.get (biased); - } + if (likely (!bitmap.glyphs.in_error ())) + return bitmap.glyphs.has (glyph_id); return mark_set_covers_slow (set_index, glyph_id); #else return table->mark_set_covers (set_index, glyph_id); @@ -1070,7 +1067,7 @@ struct GDEF bool mark_set_covers_slow (unsigned int set_index, hb_codepoint_t glyph_id) const { const auto &bitmap = mark_glyph_set_bitmaps.arrayZ[set_index]; - if (!bitmap.u.digest.may_have (glyph_id)) + if (!bitmap.digest.may_have (glyph_id)) return false; return table->mark_set_covers (set_index, glyph_id); } @@ -1095,15 +1092,8 @@ struct GDEF #ifndef HB_NO_GDEF_CACHE struct mark_glyph_set_bitmap_t { - /* Gid of the first bit (not page-aligned) for an exact one-page - * set, or HB_SET_VALUE_INVALID; then digest is a pre-filter for - * the table. The all-zeros state is valid for both members. */ - hb_codepoint_t bias = HB_SET_VALUE_INVALID; - union u_t { - u_t () : page () {} - hb_bit_page_t page; - hb_set_digest_t digest; - } u; + hb_bit_vector_t<> glyphs {1, 0}; + hb_set_digest_t digest; }; hb_vector_t mark_glyph_set_bitmaps; diff --git a/src/OT/Layout/GPOS/PairPosFormat1.hh b/src/OT/Layout/GPOS/PairPosFormat1.hh index 47e80b6bf..26446874f 100644 --- a/src/OT/Layout/GPOS/PairPosFormat1.hh +++ b/src/OT/Layout/GPOS/PairPosFormat1.hh @@ -56,9 +56,17 @@ struct PairPosFormat1_3 if (pairSet.len > glyphs->get_population () * hb_bit_storage ((unsigned) pairSet.len)) { + unsigned next = 0; for (hb_codepoint_t g : glyphs->iter ()) { unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED) continue; + /* Broken table. Skip. Important to avoid DoS. + * Coverage indices must increase with glyph id, as the + * Coverage iterator used below enforces; otherwise one + * PairSet could be scanned once per glyph. */ + if (unlikely (i < next)) continue; + next = i + 1; if ((this+pairSet[i]).intersects (glyphs, valueFormat)) return true; } diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index 46ceb984f..15c2ab83b 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -34,7 +34,9 @@ void collect_pair_variation_indices (hb_collect_variation_indices_context_t *c, else { unsigned klass1 = class_def1.get (cp); - class1_set.add (klass1); + /* Classes outside the matrix have no values; apply() ignores them too. */ + if (klass1 < class1_count) + class1_set.add (klass1); } } @@ -42,7 +44,8 @@ void collect_pair_variation_indices (hb_collect_variation_indices_context_t *c, for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs)) { unsigned klass2 = class_def2.get (cp); - class2_set.add (klass2); + if (klass2 < class2_count) + class2_set.add (klass2); } if (class1_set.is_empty () diff --git a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh index 166a9587d..f220797a2 100644 --- a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh +++ b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh @@ -36,9 +36,25 @@ struct AlternateSubstFormat1_2 void depend (hb_depend_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + // Filter by parent_active_glyphs like closure does - + hb_zip (this+coverage, alternateSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + if (alternateSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= alternateSet.len) + continue; + (this+alternateSet.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, alternateSet) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const AlternateSet& as = this+_.second; @@ -49,9 +65,26 @@ struct AlternateSubstFormat1_2 void closure (hb_closure_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (alternateSet.len > glyph_set.get_population () * 4) + { + if (unlikely (!c->spend_budget (HB_BUDGET_1, glyph_set.get_population ()))) return; + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= alternateSet.len) + continue; + (this+alternateSet.arrayZ[i]).closure (c); + } + + return; + } + if (unlikely (!c->spend_budget (HB_BUDGET_1, alternateSet.len))) return; - + hb_zip (this+coverage, alternateSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + + hb_zip (cov, alternateSet) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const AlternateSet &_) { _.closure (c); }) diff --git a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh index 3b40a195f..f48e258aa 100644 --- a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh +++ b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh @@ -45,8 +45,24 @@ struct LigatureSubstFormat1_2 void depend (hb_depend_context_t *c) const { - + hb_zip (this+coverage, ligatureSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (ligatureSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= ligatureSet.len) + continue; + (this+ligatureSet.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, ligatureSet) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const LigatureSet& ls = this+_.second; @@ -57,9 +73,26 @@ struct LigatureSubstFormat1_2 void closure (hb_closure_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (ligatureSet.len > glyph_set.get_population () * 4) + { + if (unlikely (!c->spend_budget (HB_BUDGET_1, glyph_set.get_population ()))) return; + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= ligatureSet.len) + continue; + (this+ligatureSet.arrayZ[i]).closure (c); + } + + return; + } + if (unlikely (!c->spend_budget (HB_BUDGET_1, ligatureSet.len))) return; - + hb_zip (this+coverage, ligatureSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + + hb_zip (cov, ligatureSet) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const LigatureSet &_) { _.closure (c); }) diff --git a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh index 79da5baf8..92b49534f 100644 --- a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh @@ -36,9 +36,26 @@ struct MultipleSubstFormat1_2 void closure (hb_closure_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (sequence.len > glyph_set.get_population () * 4) + { + if (unlikely (!c->spend_budget (HB_BUDGET_1, glyph_set.get_population ()))) return; + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= sequence.len) + continue; + (this+sequence.arrayZ[i]).closure (c); + } + + return; + } + if (unlikely (!c->spend_budget (HB_BUDGET_1, sequence.len))) return; - + hb_zip (this+coverage, sequence) - | hb_filter (c->parent_active_glyphs (), hb_first) + + hb_zip (cov, sequence) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const Sequence &_) { _.closure (c); }) @@ -47,9 +64,25 @@ struct MultipleSubstFormat1_2 void depend (hb_depend_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + // Filter by parent_active_glyphs like closure does - + hb_zip (this+coverage, sequence) - | hb_filter (c->parent_active_glyphs (), hb_first) + if (sequence.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= sequence.len) + continue; + (this+sequence.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, sequence) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const Sequence& s = this+_.second; diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh index 20159dac3..21afb3005 100644 --- a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -132,10 +132,26 @@ struct ReverseChainSingleSubstFormat1_2 hb_codepoint_t saved_context = c->depend_data->current_context_set_index; c->depend_data->current_context_set_index = context_set_idx; - + hb_zip (this+coverage, substitute) - | hb_filter (c->parent_active_glyphs (), hb_first) - | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); }) - ; + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (substitute.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= substitute.len) + continue; + c->depend_data->add_gsub_lookup (g, c->lookup_index, substitute.arrayZ[i]); + } + } + else + { + + hb_zip (cov, substitute) + | hb_filter (glyph_set, hb_first) + | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); }) + ; + } c->depend_data->current_context_set_index = saved_context; } @@ -150,9 +166,26 @@ struct ReverseChainSingleSubstFormat1_2 return; if (!intersects (c->glyphs)) return; + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (substitute.len > glyph_set.get_population () * 4) + { + if (unlikely (!c->spend_budget (HB_BUDGET_1, glyph_set.get_population ()))) return; + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= substitute.len) + continue; + c->output->add (substitute.arrayZ[i]); + } + + return; + } + if (unlikely (!c->spend_budget (HB_BUDGET_1, substitute.len))) return; - + hb_zip (this+coverage, substitute) - | hb_filter (c->parent_active_glyphs (), hb_first) + + hb_zip (cov, substitute) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_sink (c->output) ; diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 0be13c1bb..0e720dd92 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -1886,8 +1886,8 @@ public: queue.insert (0, root_idx ()); TRY (graph_result_t::from (queue, ALLOCATION_FAILURE)); - hb_vector_t visited; - if (unlikely (!visited.resize (vertices_.length))) + hb_bit_vector_t<> visited (0, count - 1); + if (unlikely (visited.in_error ())) return Err(ALLOCATION_FAILURE); while (!queue.in_error () && !queue.is_empty ()) @@ -1896,7 +1896,7 @@ public: if (visited[next_idx]) continue; const auto& next = vertices_[next_idx]; int64_t next_distance = next.distance; - visited[next_idx] = true; + visited.add (next_idx); for (const auto& link : next.obj().all_links ()) { diff --git a/src/hb-bit-vector.hh b/src/hb-bit-vector.hh index 58084e59a..2aaef821c 100644 --- a/src/hb-bit-vector.hh +++ b/src/hb-bit-vector.hh @@ -27,17 +27,26 @@ #include "hb.hh" +#include "hb-array.hh" #include "hb-atomic.hh" struct hb_min_max_t { void add (hb_codepoint_t v) { min_v = hb_min (min_v, v); max_v = hb_max (max_v, v); } - void add_range (hb_codepoint_t a, hb_codepoint_t b) + bool add_range (hb_codepoint_t a, hb_codepoint_t b) { - min_v = hb_min (min_v, a); - max_v = hb_max (max_v, b); + if (a <= b) + { + min_v = hb_min (min_v, a); + max_v = hb_max (max_v, b); + } + return true; } + template + bool add_sorted_array (const hb_sorted_array_t& arr) + { for (const auto &v : arr) add (v); return true; } + template void union_ (const set_t &set) { @@ -86,17 +95,17 @@ struct hb_bit_vector_t hb_bit_vector_t (unsigned min_v, unsigned max_v) : min_v (min_v), max_v (max_v) { - if (unlikely (min_v >= max_v)) + if (unlikely (min_v > max_v || max_v - min_v == UINT_MAX)) { - min_v = max_v = count = 0; + this->min_v = this->max_v = count = 0; return; } - unsigned num = (max_v - min_v + sizeof (int_t) * 8) / (sizeof (int_t) * 8); + unsigned num = (max_v - min_v) / ELT_BITS + 1; elts = (elt_t *) hb_calloc (num, sizeof (int_t)); if (unlikely (!elts)) { - min_v = max_v = count = 0; + this->min_v = this->max_v = count = 0; return; } @@ -107,6 +116,9 @@ struct hb_bit_vector_t hb_free (elts); } + /* Invalid ranges and allocation failure leave the vector in error. */ + bool in_error () const { return !elts; } + void add (hb_codepoint_t g) { elt (g) |= mask (g); } void del (hb_codepoint_t g) { elt (g) &= ~mask (g); } void set (hb_codepoint_t g, bool value) { if (value) add (g); else del (g); } @@ -117,10 +129,11 @@ struct hb_bit_vector_t bool operator [] (hb_codepoint_t g) const { return get (g); } bool operator () (hb_codepoint_t g) const { return get (g); } - void add_range (hb_codepoint_t a, hb_codepoint_t b) + bool add_range (hb_codepoint_t a, hb_codepoint_t b) { - if (unlikely (!count || a > b || a < min_v || b > max_v)) - return; + if (unlikely (a > b)) return true; + if (unlikely (!count || a < min_v || b > max_v)) + return false; elt_t *la = &elt (a); elt_t *lb = &elt (b); @@ -135,7 +148,12 @@ struct hb_bit_vector_t *lb |= ((mask (b) << 1) - 1llu); } + return true; } + + template + bool add_sorted_array (const hb_sorted_array_t& arr) + { for (const auto &g : arr) add (g); return !in_error (); } void del_range (hb_codepoint_t a, hb_codepoint_t b) { if (unlikely (!count || a > b || a < min_v || b > max_v)) @@ -185,7 +203,7 @@ struct hb_bit_vector_t return elts[g / ELT_BITS]; } - static constexpr int_t mask (hb_codepoint_t g) { return elt_t (1) << (g & ELT_MASK); } + int_t mask (hb_codepoint_t g) const { return int_t (1) << ((g - min_v) & ELT_MASK); } hb_codepoint_t min_v = 0, max_v = 0, count = 0; elt_t *elts = nullptr; diff --git a/src/hb-cff-specializer.hh b/src/hb-cff-specializer.hh index 12bf3dc56..fdcf3bd2b 100644 --- a/src/hb-cff-specializer.hh +++ b/src/hb-cff-specializer.hh @@ -139,6 +139,37 @@ generalize_commands (hb_vector_t &commands) commands.push (result[i]); } +/* Merge each command into the one before it wherever can_combine allows, + * from the last command to the first. Finished commands are kept packed + * at the end of the vector and moved to the front once at the end, such + * that dropping a merged command does not shift everything behind it. */ +template +static void +combine_commands (hb_vector_t &commands, Pred can_combine) +{ + unsigned count = commands.length; + unsigned done = count; /* Finished commands are in [done, count). */ + for (unsigned i = count; i-- > 0;) + { + auto &prev = commands.arrayZ[i]; + while (done < count && can_combine (prev, commands.arrayZ[done])) + { + /* Merge into first command, keep its operator */ + for (const auto &arg : commands.arrayZ[done].args) + prev.args.push (arg); + done++; + } + done--; + if (done != i) + commands.arrayZ[done] = std::move (prev); + } + + if (!done) return; + for (unsigned i = done; i < count; i++) + commands.arrayZ[i - done] = std::move (commands.arrayZ[i]); + commands.shrink (count - done); +} + /* Specialize CharString commands to optimize bytecode size * * Follows fontTools approach: @@ -190,55 +221,29 @@ specialize_commands (hb_vector_t &commands, /* Pass 2: Combine adjacent hlineto/vlineto operators * hlineto can take multiple args alternating with vlineto * This saves operator bytes */ - for (int i = (int)commands.length - 1; i > 0; i--) + combine_commands (commands, [maxstack] (const cs_command_t &prev, const cs_command_t &cmd) { - auto &cmd = commands[i]; - auto &prev = commands[i-1]; - /* Different starting directions can combine only after an odd number * of alternating arguments in the preceding command. */ - if (((prev.op == OpCode_hlineto && cmd.op == OpCode_vlineto) || - (prev.op == OpCode_vlineto && cmd.op == OpCode_hlineto)) && - (prev.args.length & 1)) - { - /* Check stack depth */ - unsigned combined_args = prev.args.length + cmd.args.length; - if (combined_args < maxstack) - { - /* Merge into first command, keep its operator */ - for (unsigned j = 0; j < cmd.args.length; j++) - prev.args.push (cmd.args[j]); - commands.remove_ordered (i); - i++; /* Adjust for removed element */ - } - } - } + return ((prev.op == OpCode_hlineto && cmd.op == OpCode_vlineto) || + (prev.op == OpCode_vlineto && cmd.op == OpCode_hlineto)) && + (prev.args.length & 1) && + /* Check stack depth */ + prev.args.length + cmd.args.length < maxstack; + }); /* Pass 3: Combine adjacent identical operators */ - for (int i = (int)commands.length - 1; i > 0; i--) + combine_commands (commands, [maxstack] (const cs_command_t &prev, const cs_command_t &cmd) { - auto &cmd = commands[i]; - auto &prev = commands[i-1]; - /* Same-direction h/v commands can combine only after an even number * of alternating arguments. Other repeatable operators concatenate. */ - if (prev.op == cmd.op && - (cmd.op == OpCode_rlineto || cmd.op == OpCode_rrcurveto || - ((cmd.op == OpCode_hlineto || cmd.op == OpCode_vlineto) && - !(prev.args.length & 1)))) - { - /* Check stack depth */ - unsigned combined_args = prev.args.length + cmd.args.length; - if (combined_args < maxstack) - { - /* Merge args */ - for (unsigned j = 0; j < cmd.args.length; j++) - prev.args.push (cmd.args[j]); - commands.remove_ordered (i); - i++; /* Adjust for removed element */ - } - } - } + return prev.op == cmd.op && + (cmd.op == OpCode_rlineto || cmd.op == OpCode_rrcurveto || + ((cmd.op == OpCode_hlineto || cmd.op == OpCode_vlineto) && + !(prev.args.length & 1))) && + /* Check stack depth */ + prev.args.length + cmd.args.length < maxstack; + }); } /* Encode commands back to binary CharString */ diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index 575ac289c..b734cc795 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -189,6 +189,15 @@ _hb_ft_get_solid_color (hb_ft_paint_context_t *c, return; } + /* A callback may have painted another glyph, selecting another palette on + * the face while it was unlocked. Re-select ours unconditionally: + * FT_Palette_Select is a no-op in FreeType when our palette is already + * current, and returns the palette pointer either way. */ + FT_Color *palette = nullptr; + (void) FT_Palette_Select (c->ft_font->ft_face, c->palette_index, &palette); + c->palette = hb_array ((const FT_Color *) palette, + palette ? c->palette.length : 0); + FT_Color ft_color = c->palette ? c->palette[color_index.palette_index] : FT_Color {}; *color = HB_COLOR (ft_color.blue, ft_color.green, @@ -551,6 +560,7 @@ hb_ft_paint_glyph_colr (hb_font_t *font, if (!palette) { // https://github.com/harfbuzz/harfbuzz/issues/5116 + palette_index = 0; (void) FT_Palette_Select(ft_face, 0, &palette); } @@ -563,13 +573,6 @@ hb_ft_paint_glyph_colr (hb_font_t *font, FT_COLOR_NO_ROOT_TRANSFORM, &paint)) { - /* Callbacks may select another palette on the same face. */ - hb_vector_t palette_copy; - palette_copy.extend (palette_array); - if (unlikely (palette_copy.in_error ())) - return false; - palette_array = palette_copy.as_array (); - hb_ft_paint_context_t c (ft_font, font, paint_funcs, paint_data, palette_array, palette_index, foreground); diff --git a/src/hb-limits.hh b/src/hb-limits.hh index befcf9e02..5bcb33dd3 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -107,6 +107,14 @@ #define HB_VAR_COMPOSITE_MAX_AXES 4096 #endif +/* Region axes copied into tuples when one ItemVariationStore is converted + * for instancing, summed over its VarData subtables. Every region a + * VarData references gets its own copy of that region's axes, and the + * font pays a few bytes per reference however many axes the region has. */ +#ifndef HB_VARSTORE_MAX_TUPLE_AXES +#define HB_VARSTORE_MAX_TUPLE_AXES (1u << 20) +#endif + #ifndef HB_GLYF_MAX_POINTS #define HB_GLYF_MAX_POINTS 200000 #endif diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index eab02852c..d802c9c2c 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -346,19 +346,26 @@ struct CheckSum : HBUINT32 { CheckSum& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; } - /* This is reference implementation from the spec. */ + /* This is reference implementation from the spec, extended to handle a + * trailing partial word. */ static uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length) { uint32_t Sum = 0L; - assert (0 == (Length & 3)); const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size; while (Table < EndPtr) Sum += *Table++; + + uint32_t remainder = Length & (HBUINT32::static_size - 1); + if (remainder) + { + const uint8_t *p = (const uint8_t *) EndPtr; + for (uint32_t i = 0; i < remainder; i++) + Sum += (uint32_t) p[i] << (24 - 8 * i); + } return Sum; } - /* Note: data should be 4byte aligned and have 4byte padding at the end. */ void set_for_data (const void *data, unsigned int length) { *this = CalcTableChecksum ((const HBUINT32 *) data, length); } diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index f4399dac0..d6aa3d02c 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -1241,6 +1241,8 @@ struct cff1 topDict.fini (); fontDicts.fini (); privateDicts.fini (); + hb_free (std_code_to_glyph_table.get_relaxed ()); + std_code_to_glyph_table.set_relaxed (nullptr); hb_blob_destroy (blob); blob = nullptr; } @@ -1259,12 +1261,56 @@ struct cff1 return 0; if (charset != &Null (Charset)) - return charset->get_glyph (sid, num_glyphs); + { + /* Charset::get_glyph() is a linear scan of the charset, and seac + * charstrings look their components up on every load. Build a + * code-to-glyph table for the 256 StandardEncoding codes in one + * pass over the charset on first use instead. */ + const uint16_t *table = get_std_code_to_glyph_table (); + if (unlikely (!table)) + return charset->get_glyph (sid, num_glyphs); + return table[code]; + } else if ((topDict.CharsetOffset == ISOAdobeCharset) && (code <= 228 /*zcaron*/)) return sid; return 0; } + private: + const uint16_t *get_std_code_to_glyph_table () const + { + retry: + uint16_t *table = std_code_to_glyph_table.get_acquire (); + if (likely (table)) + return table; + + table = (uint16_t *) hb_calloc (256, sizeof (uint16_t)); + if (unlikely (!table)) + return nullptr; + + /* Record the first glyph carrying each StandardEncoding SID, + * matching Charset::get_glyph(). */ + code_pair_t glyph_to_sid_cache {0, HB_CODEPOINT_INVALID}; + for (hb_codepoint_t glyph = 1; glyph < num_glyphs; glyph++) + { + hb_codepoint_t sid = charset->get_sid (glyph, num_glyphs, &glyph_to_sid_cache); + if (!sid) continue; + hb_codepoint_t code = lookup_standard_encoding_for_code (sid); + if (code && code < 256 && + lookup_standard_encoding_for_sid (code) == sid && + !table[code]) + table[code] = glyph; + } + + if (unlikely (!std_code_to_glyph_table.cmpexch (nullptr, table))) + { + hb_free (table); + goto retry; + } + return table; + } + public: + bool is_predef_encoding () const { return topDict.EncodingOffset <= ExpertEncoding; } hb_codepoint_t glyph_to_code (hb_codepoint_t glyph, @@ -1383,6 +1429,9 @@ struct cff1 unsigned int num_glyphs = 0; unsigned int num_charset_entries = 0; + + private: + mutable hb_atomic_t std_code_to_glyph_table {nullptr}; }; struct accelerator_t : accelerator_templ_t diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index c8120dbd6..02473f971 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1670,6 +1670,15 @@ struct ClassDefFormat1_3 return true; } + template + bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const + { + unsigned int count = classValue.len; + for (unsigned int i = 0; i < count; i++) + if (klasses.has (classValue[i])) glyphs->add (startGlyph + i); + return true; + } + bool intersects (const hb_set_t *glyphs) const { hb_codepoint_t start = startGlyph; @@ -1954,6 +1963,18 @@ struct ClassDefFormat2_4 return true; } + template + bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const + { + for (auto &range : rangeRecord) + { + if (klasses.has (range.value)) + if (unlikely (!range.collect_coverage (glyphs))) + return false; + } + return true; + } + bool intersects (const hb_set_t *glyphs) const { if (rangeRecord.len > glyphs->get_population () * hb_bit_storage ((unsigned) rangeRecord.len)) @@ -2310,6 +2331,21 @@ struct ClassDef } } + /* Like collect_class(), for all of klasses in one walk of the table. */ + template + bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const + { + switch (u.format.v) { + case 1: hb_barrier (); return u.format1.collect_classes (glyphs, klasses); + case 2: hb_barrier (); return u.format2.collect_classes (glyphs, klasses); +#ifndef HB_NO_BEYOND_64K + case 3: hb_barrier (); return u.format3.collect_classes (glyphs, klasses); + case 4: hb_barrier (); return u.format4.collect_classes (glyphs, klasses); +#endif + default:return false; + } + } + bool intersects (const hb_set_t *glyphs) const { switch (u.format.v) { @@ -5525,6 +5561,18 @@ struct FeatureTableSubstitution return nullptr; } + /* Calls visit (feature_index, substitute) until it returns false. */ + template + bool visit_substitutes (const hb_set_t *feature_indexes, + Visitor &visit) const + { + for (const FeatureTableSubstitutionRecord &record : substitutions) + if (feature_indexes->has (record.featureIndex) && + !visit (record.featureIndex, this+record.feature)) + return false; + return true; + } + void collect_lookups (const hb_set_t *feature_indexes, hb_set_t *lookup_indexes /* OUT */) const { @@ -6048,6 +6096,16 @@ struct FeatureVariations return (this+record.substitutions).find_substitute (feature_index); } + template + bool visit_substitutes (const hb_set_t *feature_indexes, + Visitor &visit) const + { + for (const FeatureVariationRecord &record : varRecords) + if (!(this+record.substitutions).visit_substitutes (feature_indexes, visit)) + return false; + return true; + } + bool collect_lookup_conditions (const int *coords, unsigned int coord_len, ItemVarStoreInstancer *instancer, diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index 0030a3c88..3eb8c5a1d 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -131,53 +131,49 @@ GSUB_accelerator_t::depend (hb_depend_data_builder_t *builder, hb_face_t *face) if (!builder->init_lookup_features (num_lookups)) return; + /* Query all tags at once, and then visit each feature, substitute and + * lookup variation once. Repeating any of that per tag or per feature + * multiplies work that sanitize() bounds only for a single pass. */ hb_vector_t feature_query_v; - feature_query_v.resize (2); - feature_query_v[1] = 0; - - hb_set_t seen_features; - hb_set_t feature_indexes, lookup_indexes; - + if (!builder->check_success (feature_query_v.alloc (num_features + 1))) + return; for (auto ft : feature_tags) + if (ft) /* Zero terminates the query. */ + feature_query_v.push (ft); + feature_query_v.push (0); + + hb_set_t feature_indexes, lookup_indexes; + hb_ot_layout_collect_features (face, HB_OT_TAG_GSUB, nullptr, nullptr, + feature_query_v.arrayZ, &feature_indexes); + + auto add_feature_lookups = [&] (unsigned feature_index, const Feature &feature) { - if (seen_features.has (ft)) continue; - seen_features.add (ft); - feature_query_v[0] = ft; - feature_indexes.reset (); - hb_ot_layout_collect_features (face, HB_OT_TAG_GSUB, nullptr, nullptr, - feature_query_v.arrayZ, &feature_indexes); lookup_indexes.reset (); - for (auto feature_index : feature_indexes) - this->table->get_feature (feature_index).add_lookup_indexes_to (&lookup_indexes); - + feature.add_lookup_indexes_to (&lookup_indexes); for (auto lookup_index : lookup_indexes) - if (unlikely (!builder->add_lookup_feature (lookup_index, ft))) - return; + if (unlikely (!builder->add_lookup_feature (lookup_index, feature_tags[feature_index]))) + return false; + return true; + }; - auto &fv = this->table->get_feature_variations (); - auto fi_count = fv.record_count (); - for (unsigned i = 0; i < fi_count; i++) - { - lookup_indexes.reset (); - for (auto feature_index : feature_indexes) - { - auto feature_ptr = fv.find_substitute (i, feature_index); - if (feature_ptr != nullptr) - feature_ptr->add_lookup_indexes_to (&lookup_indexes); - } - for (auto lookup_index : lookup_indexes) - if (unlikely (!builder->add_lookup_feature (lookup_index, ft))) - return; - } + for (auto feature_index : feature_indexes) + if (unlikely (!add_feature_lookups (feature_index, this->table->get_feature (feature_index)))) + return; + + auto &fv = this->table->get_feature_variations (); + if (unlikely (!fv.visit_substitutes (&feature_indexes, add_feature_lookups))) + return; #ifndef HB_NO_VAR + for (const auto &record : fv.get_lookup_variation_records ()) + { lookup_indexes.reset (); - fv.collect_lookup_variation_lookups (&feature_indexes, &lookup_indexes); + record.collect_lookups (&fv, &feature_indexes, &lookup_indexes); for (auto lookup_index : lookup_indexes) - if (unlikely (!builder->add_lookup_feature (lookup_index, ft))) + if (unlikely (!builder->add_lookup_feature (lookup_index, feature_tags[record.get_feature_index ()]))) return; -#endif } +#endif if (unlikely (!builder->finish_lookup_features ())) return; diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 5e030c581..32420de0d 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1683,19 +1683,11 @@ static inline bool intersects_glyph (const hb_set_t *glyphs, unsigned value, con { return glyphs->has (value); } -static inline bool intersects_class (const hb_set_t *glyphs, unsigned value, const void *data, void *cache) +static inline bool intersects_class (const hb_set_t *glyphs HB_UNUSED, unsigned value, const void *data HB_UNUSED, void *cache) { - const ClassDef &class_def = *reinterpret_cast(data); - hb_map_t *map = (hb_map_t *) cache; - - hb_codepoint_t *cached_v; - if (map->has (value, &cached_v)) - return *cached_v; - - bool v = class_def.intersects_class (glyphs, value); - map->set (value, v); - - return v; + /* ClassDef::intersects_class() walks the whole ClassDef, so callers collect + * ClassDef::intersected_classes() of glyphs into the cache upfront, once. */ + return reinterpret_cast (cache)->has (value); } static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, const void *data, void *cache HB_UNUSED) { @@ -1705,10 +1697,9 @@ static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, } -static inline void intersected_glyph (const hb_set_t *glyphs HB_UNUSED, const void *data, unsigned value, hb_set_t *intersected_glyphs, HB_UNUSED void *cache) +static inline void intersected_glyph (const hb_set_t *glyphs HB_UNUSED, const void *data HB_UNUSED, unsigned value, hb_set_t *intersected_glyphs, HB_UNUSED void *cache) { - unsigned g = reinterpret_cast(data)[value]; - intersected_glyphs->add (g); + intersected_glyphs->add (value); } using intersected_class_cache_t = hb_hashmap_t; @@ -1764,6 +1755,12 @@ static inline void collect_class (hb_set_t *glyphs, unsigned value, const void * const ClassDef &class_def = *reinterpret_cast(data); class_def.collect_class (glyphs, value); } +static inline void collect_classes (hb_set_t *glyphs, const hb_set_t &classes, const ClassDef &class_def) +{ + if (unlikely (classes.in_error ())) + glyphs->err (); + class_def.collect_classes (glyphs, classes); +} static inline void collect_coverage (hb_set_t *glyphs, unsigned value, const void *data) { Offset32To coverage; @@ -2439,17 +2436,10 @@ static inline void context_closure_recurse_lookups (hb_closure_context_t *c, } } else - { - const void *input_data = input.data; - unsigned input_value = seqIndex - 1; - if (context_format != ContextFormat::SimpleContext) - { - input_data = data; - input_value = input[seqIndex - 1]; - } - - intersected_glyphs_func (c->glyphs, input_data, input_value, &pos_glyphs, cache); - } + /* input is a width-erased glyph_data_t view, so input[seqIndex - 1] + * resolves the value at the right width for every format (glyph id + * for glyph-based rules, class/coverage index otherwise). */ + intersected_glyphs_func (c->glyphs, data, input[seqIndex - 1], &pos_glyphs, cache); } covered_seq_indicies.add (seqIndex); @@ -2707,17 +2697,10 @@ static inline void context_depend_recurse_lookups (hb_depend_context_t *c, } } else - { - const void *input_data = input.data; - unsigned input_value = seqIndex - 1; - if (context_format != ContextFormat::SimpleContext) - { - input_data = data; - input_value = input[seqIndex - 1]; - } - - intersected_glyphs_func (c->glyphs, input_data, input_value, &pos_glyphs, cache); - } + /* input is a width-erased glyph_data_t view, so input[seqIndex - 1] + * resolves the value at the right width for every format (glyph id + * for glyph-based rules, class/coverage index otherwise). */ + intersected_glyphs_func (c->glyphs, data, input[seqIndex - 1], &pos_glyphs, cache); } covered_seq_indices.add (seqIndex); @@ -3197,7 +3180,7 @@ struct Rule } void collect_second_glyphs (hb_set_digest_t *digest, - const ClassDef *class_def = nullptr) const + hb_set_t *classes = nullptr) const { if (inputCount <= 1) { @@ -3206,12 +3189,12 @@ struct Rule } unsigned second = inputZ.arrayZ[0]; - if (!class_def) + if (!classes) digest->add (second); else if (!second) *digest = hb_set_digest_t::full (); else - class_def->collect_class (digest, second); + classes->add (second); } bool would_apply (hb_would_apply_context_t *c, @@ -3368,11 +3351,11 @@ struct RuleSetOf } void collect_second_glyphs (hb_set_digest_t *digest, - const ClassDef *class_def = nullptr) const + hb_set_t *classes = nullptr) const { + hb_iter (rule) | hb_map (hb_add (this)) - | hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, class_def); }) + | hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, classes); }) ; } @@ -3644,6 +3627,22 @@ struct ContextFormat1_4 nullptr }; + if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4) + { + /* Recursion can reallocate the stack; fetch the set afresh each time. */ + hb_codepoint_t g = HB_SET_VALUE_INVALID; + while (c->previous_parent_active_glyphs ().next (&g)) + { + unsigned i = get_coverage ().get_coverage (g); + if (i == NOT_COVERED || i >= ruleSet.len) + continue; + (this+ruleSet.arrayZ[i]).closure (c, g, lookup_context); + } + + c->pop_cur_done_glyphs (); + return; + } + + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) | hb_filter ([&] (hb_codepoint_t _) { return c->previous_parent_active_glyphs ().has (_); @@ -3778,7 +3777,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (glyphs, &cache); struct ContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -3798,8 +3798,7 @@ struct ContextFormat2_5 | hb_map (hb_add (this)) | hb_enumerate | hb_map ([&] (const hb_pair_t p) - { return class_def.intersects_class (glyphs, p.first) && - coverage_glyph_classes.has (p.first) && + { return coverage_glyph_classes.has (p.first) && p.second.intersects (glyphs, lookup_context); }) | hb_any ; @@ -3819,7 +3818,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); intersected_class_cache_t intersected_cache; ContextDependLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -3829,10 +3829,11 @@ struct ContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const RuleSet& rule_set = this+_.second; @@ -3848,9 +3849,10 @@ struct ContextFormat2_5 if (!(this+coverage).intersects (c->glyphs)) return; - /* Filtering the rule sets walks the ClassDef once per set. */ + /* Charge the rule-set walk and both ClassDef scans. */ const ClassDef &class_def = this+classDef; - if (unlikely (!c->spend_budget (ruleSet.len, 1 + class_def.get_record_count ()))) + if (unlikely (!c->spend_budget (HB_BUDGET_1, ruleSet.len) || + !c->spend_budget (HB_BUDGET_2, 1 + class_def.get_record_count ()))) return; hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs (); @@ -3858,7 +3860,8 @@ struct ContextFormat2_5 get_coverage ().intersect_set (c->previous_parent_active_glyphs (), *cur_active_glyphs); - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); intersected_class_cache_t intersected_cache; struct ContextClosureLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -3868,10 +3871,11 @@ struct ContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const RuleSet& rule_set = this+_.second; @@ -3889,7 +3893,8 @@ struct ContextFormat2_5 const ClassDef &class_def = this+classDef; - hb_map_t cache; + hb_set_t cache; + class_def.intersected_classes (c->glyphs, &cache); struct ContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -3900,8 +3905,7 @@ struct ContextFormat2_5 + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_enumerate - | hb_filter ([&] (const hb_pair_t p) - { return class_def.intersects_class (c->glyphs, p.first); }) + | hb_filter (cache, hb_first) | hb_map (hb_second) | hb_apply ([&] (const RuleSet & _) { _.closure_lookups (c, lookup_context); }); @@ -3913,25 +3917,40 @@ struct ContextFormat2_5 { (this+coverage).collect_coverage (c->input); - const ClassDef &class_def = this+classDef; + /* Rules repeat classes, and can be shared. Collect the class values + * first, such that the ClassDef is walked once instead of per value. */ + hb_set_t input_classes; + hb_set_t *input = c->input; + c->input = &input_classes; + struct ContextCollectGlyphsLookupContext lookup_context = { - {collect_class}, - &class_def + {collect_glyph}, + nullptr }; + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); }) ; + + c->input = input; + + collect_classes (c->input, input_classes, this+classDef); } void collect_second_glyphs (hb_set_digest_t *digest) const { - const ClassDef &class_def = this+classDef; + /* Ditto. */ + hb_set_t classes; + hb_iter (ruleSet) | hb_map (hb_add (this)) - | hb_apply ([=, &class_def] (const RuleSet &_) { _.collect_second_glyphs (digest, &class_def); }) + | hb_apply ([=, &classes] (const RuleSet &_) { _.collect_second_glyphs (digest, &classes); }) ; + + if (unlikely (classes.in_error ())) + *digest = hb_set_digest_t::full (); + else + (this+classDef).collect_classes (digest, classes); } bool would_apply (hb_would_apply_context_t *c) const @@ -4739,23 +4758,23 @@ struct ChainRule } void collect_second_glyphs (hb_set_digest_t *digest, - const ClassDef *input_class_def = nullptr, - const ClassDef *lookahead_class_def = nullptr) const + hb_set_t *input_classes = nullptr, + hb_set_t *lookahead_classes = nullptr) const { const auto &input = StructAfter (backtrack); const auto &lookahead = StructAfter (input); unsigned second; - const ClassDef *class_def; + hb_set_t *classes; if (input.lenP1 > 1) { second = input.arrayZ[0]; - class_def = input_class_def; + classes = input_classes; } else if (lookahead.len) { second = lookahead.arrayZ[0]; - class_def = lookahead_class_def; + classes = lookahead_classes; } else { @@ -4763,12 +4782,12 @@ struct ChainRule return; } - if (!class_def) + if (!classes) digest->add (second); else if (!second) *digest = hb_set_digest_t::full (); else - class_def->collect_class (digest, second); + classes->add (second); } bool would_apply (hb_would_apply_context_t *c, @@ -4980,13 +4999,13 @@ struct ChainRuleSetOf } void collect_second_glyphs (hb_set_digest_t *digest, - const ClassDef *input_class_def = nullptr, - const ClassDef *lookahead_class_def = nullptr) const + hb_set_t *input_classes = nullptr, + hb_set_t *lookahead_classes = nullptr) const { + hb_iter (rule) | hb_map (hb_add (this)) | hb_apply ([=] (const ChainRuleType &_) - { _.collect_second_glyphs (digest, input_class_def, lookahead_class_def); }) + { _.collect_second_glyphs (digest, input_classes, lookahead_classes); }) ; } @@ -5282,6 +5301,22 @@ struct ChainContextFormat1_4 {nullptr, nullptr, nullptr} }; + if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4) + { + /* Recursion can reallocate the stack; fetch the set afresh each time. */ + hb_codepoint_t g = HB_SET_VALUE_INVALID; + while (c->previous_parent_active_glyphs ().next (&g)) + { + unsigned i = get_coverage ().get_coverage (g); + if (i == NOT_COVERED || i >= ruleSet.len) + continue; + (this+ruleSet.arrayZ[i]).closure (c, g, lookup_context); + } + + c->pop_cur_done_glyphs (); + return; + } + + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) | hb_filter ([&] (hb_codepoint_t _) { return c->previous_parent_active_glyphs ().has (_); @@ -5416,7 +5451,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (glyphs, &caches[0]); + input_class_def.intersected_classes (glyphs, &caches[1]); + lookahead_class_def.intersected_classes (glyphs, &caches[2]); struct ChainContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -5437,8 +5475,7 @@ struct ChainContextFormat2_5 | hb_map (hb_add (this)) | hb_enumerate | hb_map ([&] (const hb_pair_t p) - { return input_class_def.intersects_class (glyphs, p.first) && - coverage_glyph_classes.has (p.first) && + { return coverage_glyph_classes.has (p.first) && p.second.intersects (glyphs, lookup_context); }) | hb_any ; @@ -5460,7 +5497,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); intersected_class_cache_t intersected_cache; ChainContextDependLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -5472,10 +5512,11 @@ struct ChainContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const ChainRuleSet& chainrule_set = this+_.second; @@ -5491,11 +5532,14 @@ struct ChainContextFormat2_5 if (!(this+coverage).intersects (c->glyphs)) return; - /* Filtering the rule sets walks the input ClassDef once per set. */ + /* Charge the rule-set walk and the ClassDef scans for both glyph sets. */ const ClassDef &backtrack_class_def = this+backtrackClassDef; const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - if (unlikely (!c->spend_budget (ruleSet.len, 1 + input_class_def.get_record_count ()))) + if (unlikely (!c->spend_budget (HB_BUDGET_1, ruleSet.len) || + !c->spend_budget (HB_BUDGET_1, 1 + backtrack_class_def.get_record_count ()) || + !c->spend_budget (HB_BUDGET_2, 1 + input_class_def.get_record_count ()) || + !c->spend_budget (HB_BUDGET_1, 1 + lookahead_class_def.get_record_count ()))) return; hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs (); @@ -5503,7 +5547,10 @@ struct ChainContextFormat2_5 get_coverage ().intersect_set (c->previous_parent_active_glyphs (), *cur_active_glyphs); - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); intersected_class_cache_t intersected_cache; struct ChainContextClosureLookupContext lookup_context = { {intersects_class, intersected_class_glyphs}, @@ -5515,10 +5562,11 @@ struct ChainContextFormat2_5 &intersected_cache }; + hb_set_t active_classes; + input_class_def.intersected_classes (&c->parent_active_glyphs (), &active_classes); + + hb_enumerate (ruleSet) - | hb_filter ([&] (unsigned _) - { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, - hb_first) + | hb_filter (active_classes, hb_first) | hb_apply ([&] (const hb_pair_t&> _) { const ChainRuleSet& chainrule_set = this+_.second; @@ -5538,7 +5586,10 @@ struct ChainContextFormat2_5 const ClassDef &input_class_def = this+inputClassDef; const ClassDef &lookahead_class_def = this+lookaheadClassDef; - hb_map_t caches[3] = {}; + hb_set_t caches[3] = {}; + backtrack_class_def.intersected_classes (c->glyphs, &caches[0]); + input_class_def.intersected_classes (c->glyphs, &caches[1]); + lookahead_class_def.intersected_classes (c->glyphs, &caches[2]); struct ChainContextClosureLookupContext lookup_context = { {intersects_class, nullptr}, ContextFormat::ClassBasedContext, @@ -5551,8 +5602,7 @@ struct ChainContextFormat2_5 + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_enumerate - | hb_filter([&] (unsigned klass) - { return input_class_def.intersects_class (c->glyphs, klass); }, hb_first) + | hb_filter (caches[1], hb_first) | hb_map (hb_second) | hb_apply ([&] (const ChainRuleSet &_) { _.closure_lookups (c, lookup_context); }) @@ -5565,32 +5615,52 @@ struct ChainContextFormat2_5 { (this+coverage).collect_coverage (c->input); - const ClassDef &backtrack_class_def = this+backtrackClassDef; - const ClassDef &input_class_def = this+inputClassDef; - const ClassDef &lookahead_class_def = this+lookaheadClassDef; + /* Rules repeat classes, and can be shared. Collect the class values + * first, such that each ClassDef is walked once instead of per value. */ + hb_set_t backtrack_classes, input_classes, lookahead_classes; + hb_set_t *before = c->before; + hb_set_t *input = c->input; + hb_set_t *after = c->after; + c->before = &backtrack_classes; + c->input = &input_classes; + c->after = &lookahead_classes; struct ChainContextCollectGlyphsLookupContext lookup_context = { - {collect_class}, - {&backtrack_class_def, - &input_class_def, - &lookahead_class_def} + {collect_glyph}, + {nullptr, nullptr, nullptr} }; + hb_iter (ruleSet) | hb_map (hb_add (this)) | hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); }) ; + + c->before = before; + c->input = input; + c->after = after; + + collect_classes (c->before, backtrack_classes, this+backtrackClassDef); + collect_classes (c->input, input_classes, this+inputClassDef); + collect_classes (c->after, lookahead_classes, this+lookaheadClassDef); } void collect_second_glyphs (hb_set_digest_t *digest) const { - const ClassDef &input_class_def = this+inputClassDef; - const ClassDef &lookahead_class_def = this+lookaheadClassDef; + /* Ditto. */ + hb_set_t input_classes, lookahead_classes; + hb_iter (ruleSet) | hb_map (hb_add (this)) - | hb_apply ([digest, &input_class_def, &lookahead_class_def] (const ChainRuleSet &_) - { _.collect_second_glyphs (digest, &input_class_def, &lookahead_class_def); }) + | hb_apply ([digest, &input_classes, &lookahead_classes] (const ChainRuleSet &_) + { _.collect_second_glyphs (digest, &input_classes, &lookahead_classes); }) ; + + if (unlikely (input_classes.in_error () || lookahead_classes.in_error ())) + { + *digest = hb_set_digest_t::full (); + return; + } + (this+inputClassDef).collect_classes (digest, input_classes); + (this+lookaheadClassDef).collect_classes (digest, lookahead_classes); } bool would_apply (hb_would_apply_context_t *c) const @@ -6464,7 +6534,10 @@ struct GSUBGPOS subset_lookup_list2_offset (), this, c); else - serialize_subset_offset (c->subset_context, out->lookupList2, + /* LookupList2 entries are 32-bit; a 16-bit source list stays + * behind the 16-bit lookupList, which readers fall back to when + * lookupList2 is null. */ + serialize_subset_offset (c->subset_context, out->lookupList, subset_lookup_list_offset (), this, c); @@ -6485,6 +6558,12 @@ struct GSUBGPOS else #endif { + /* Written with the v1.0/v1.1 layout, whose header has none of the + * 32-bit v1.2 offsets; a v1.2 source must be recorded as v1.1 so + * the header matches what is actually serialized. */ + if (version.to_int () >= 0x00010002u) + out->version.minor = 1; + out->subset_lookup_list_offset () .serialize_subset (c->subset_context, subset_lookup_list_offset (), @@ -6507,7 +6586,7 @@ struct GSUBGPOS if (version.to_int () >= 0x00010001u) { auto snapshot = c->subset_context->serializer->snapshot (); - if (version.to_int () < 0x00010002u && + if (out->version.to_int () < 0x00010002u && !c->subset_context->serializer->extend_min (&out->featureVars)) return_trace (false); @@ -6517,7 +6596,7 @@ struct GSUBGPOS if (!ret) { out->featureVars = 0; - if (version.to_int () < 0x00010002u) + if (out->version.to_int () < 0x00010002u) { c->subset_context->serializer->revert (snapshot); out->version.major = 1; diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 2eefdf77d..156c6c293 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -1519,6 +1519,23 @@ hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes); } +/* Like hb_ot_layout_feature_with_variations_get_lookups(), but resolves the + * feature only once and returns all of its lookup indices. Resolving a feature + * under a FeatureVariations record is a linear search of the record's + * FeatureTableSubstitution, so callers must not page through the lookups. */ +hb_array_t +_hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, + hb_tag_t table_tag, + unsigned int feature_index, + unsigned int variations_index) +{ + const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag); + + const OT::Feature &f = g.get_feature_variation (feature_index, variations_index); + + return f.lookupIndex.as_array (); +} + /* * OT::GSUB diff --git a/src/hb-ot-layout.hh b/src/hb-ot-layout.hh index 38ad88b3e..b72b629ba 100644 --- a/src/hb-ot-layout.hh +++ b/src/hb-ot-layout.hh @@ -76,6 +76,12 @@ hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face, bool zero_context, hb_buffer_t *buffer); +HB_INTERNAL hb_array_t +_hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face, + hb_tag_t table_tag, + unsigned int feature_index, + unsigned int variations_index); + /* * GDEF diff --git a/src/hb-ot-map.cc b/src/hb-ot-map.cc index a1f5c9e5a..c0f518cab 100644 --- a/src/hb-ot-map.cc +++ b/src/hb-ot-map.cc @@ -136,8 +136,6 @@ hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m, bool per_syllable, hb_tag_t feature_tag) { - unsigned int lookup_indices[32]; - unsigned int offset, len; unsigned int table_lookup_count; table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]); @@ -166,32 +164,26 @@ hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m, return; } - offset = 0; - do { - len = ARRAY_LENGTH (lookup_indices); - hb_ot_layout_feature_with_variations_get_lookups (face, - table_tags[table_index], - feature_index, - key.variations_index[table_index], - offset, &len, - lookup_indices); - - for (unsigned int i = 0; i < len; i++) - { - if (lookup_indices[i] >= table_lookup_count) - continue; - hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); - lookup->mask = mask; - lookup->index = lookup_indices[i]; - lookup->auto_zwnj = auto_zwnj; - lookup->auto_zwj = auto_zwj; - lookup->random = random; - lookup->per_syllable = per_syllable; - lookup->feature_tag = feature_tag; - } - - offset += len; - } while (len == ARRAY_LENGTH (lookup_indices)); + /* Resolve the feature once: under a FeatureVariations record that is a + * linear search of the substitution records, so don't repeat it per batch + * of lookup indices. */ + auto lookup_indices = _hb_ot_layout_feature_with_variations_get_lookups (face, + table_tags[table_index], + feature_index, + key.variations_index[table_index]); + for (unsigned int lookup_index : lookup_indices) + { + if (lookup_index >= table_lookup_count) + continue; + hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); + lookup->mask = mask; + lookup->index = lookup_index; + lookup->auto_zwnj = auto_zwnj; + lookup->auto_zwj = auto_zwj; + lookup->random = random; + lookup->per_syllable = per_syllable; + lookup->feature_tag = feature_tag; + } } diff --git a/src/hb-ot-shaper-indic.cc b/src/hb-ot-shaper-indic.cc index b935ced6e..60328ea00 100644 --- a/src/hb-ot-shaper-indic.cc +++ b/src/hb-ot-shaper-indic.cc @@ -418,7 +418,10 @@ setup_syllables_indic (const hb_ot_shape_plan_t *plan HB_UNUSED, HB_BUFFER_ALLOCATE_VAR (buffer, syllable); find_syllables_indic (buffer); foreach_syllable (buffer, start, end) + { + hb_syllabic_record_cut (buffer, start, end); buffer->unsafe_to_break (start, end); + } return false; } @@ -988,7 +991,12 @@ initial_reordering_indic (const hb_ot_shape_plan_t *plan, ret = true; foreach_syllable (buffer, start, end) + { + /* locl, ccmp, or inserted dotted-circles may have pushed a piece over + * the limit again; final reordering needs to see the same pieces. */ + hb_syllabic_record_cut (buffer, start, end); initial_reordering_syllable_indic (plan, font->face, buffer, start, end); + } (void) buffer->message (font, "end reordering indic initial"); diff --git a/src/hb-ot-shaper-khmer.cc b/src/hb-ot-shaper-khmer.cc index eb3b585e8..3e5b93eb0 100644 --- a/src/hb-ot-shaper-khmer.cc +++ b/src/hb-ot-shaper-khmer.cc @@ -194,7 +194,10 @@ setup_syllables_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED, HB_BUFFER_ALLOCATE_VAR (buffer, syllable); find_syllables_khmer (buffer); foreach_syllable (buffer, start, end) + { + hb_syllabic_record_cut (buffer, start, end); buffer->unsafe_to_break (start, end); + } return false; } diff --git a/src/hb-ot-shaper-myanmar.cc b/src/hb-ot-shaper-myanmar.cc index 1b2a085a8..c4a6316cd 100644 --- a/src/hb-ot-shaper-myanmar.cc +++ b/src/hb-ot-shaper-myanmar.cc @@ -158,7 +158,10 @@ setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED, HB_BUFFER_ALLOCATE_VAR (buffer, syllable); find_syllables_myanmar (buffer); foreach_syllable (buffer, start, end) + { + hb_syllabic_record_cut (buffer, start, end); buffer->unsafe_to_break (start, end); + } return false; } diff --git a/src/hb-ot-shaper-syllabic.cc b/src/hb-ot-shaper-syllabic.cc index 97f62035c..e7631df95 100644 --- a/src/hb-ot-shaper-syllabic.cc +++ b/src/hb-ot-shaper-syllabic.cc @@ -99,6 +99,55 @@ hb_syllabic_insert_dotted_circles (hb_font_t *font, return true; } +/* foreach_syllable() cuts syllables longer than HB_MAX_SYLLABLE_LENGTH, + * but the cut is computed from the current glyph positions and is not + * recorded, so walks before and after substitutions would see different + * pieces. Record the cuts in syllable(), which substituted glyphs inherit: + * cut the whole syllable starting at start into the same pieces + * foreach_syllable() would, and give every piece but the last a serial + * different from its neighbours. The rest of the walk then sees pieces + * that are not cut further. The pieces are not syllables on their own and + * text must not be broken between them, so also keep them in one cluster. */ +void +hb_syllabic_record_cut_slow (hb_buffer_t *buffer, + unsigned int start, + unsigned int end) +{ + hb_glyph_info_t *info = buffer->info; + unsigned int count = buffer->len; + unsigned int syllable = info[start].syllable(); + if (end == count || info[end].syllable() != syllable) + return; /* Not cut. */ + + unsigned int syllable_end = end; + while (syllable_end < count && info[syllable_end].syllable() == syllable) + syllable_end++; + + buffer->merge_clusters (start, syllable_end); + + /* Alternate between two serials different from both the one before + * start and the one the last piece keeps. */ + unsigned int prev_serial = start ? info[start - 1].syllable() >> 4 : 0; + unsigned int last_serial = syllable >> 4; + unsigned int serials[2]; + unsigned int serial = 1; + for (unsigned int j = 0; j < 2; j++) + { + while (serial == prev_serial || serial == last_serial) + serial++; + serials[j] = serial++; + } + + unsigned int j = 0; + for (; syllable_end - start > HB_MAX_SYLLABLE_LENGTH; start = end, j ^= 1) + { + end = start + HB_MAX_SYLLABLE_LENGTH; + unsigned int v = (serials[j] << 4) | (syllable & 0x0F); + for (unsigned int i = start; i < end; i++) + info[i].syllable() = v; + } +} + HB_INTERNAL bool hb_syllabic_clear_var (const hb_ot_shape_plan_t *plan, hb_font_t *font, diff --git a/src/hb-ot-shaper-syllabic.hh b/src/hb-ot-shaper-syllabic.hh index f240ad1c2..bc2dd3215 100644 --- a/src/hb-ot-shaper-syllabic.hh +++ b/src/hb-ot-shaper-syllabic.hh @@ -38,6 +38,24 @@ hb_syllabic_insert_dotted_circles (hb_font_t *font, int repha_category = -1, int dottedcircle_position = -1); +HB_INTERNAL void +hb_syllabic_record_cut_slow (hb_buffer_t *buffer, + unsigned int start, + unsigned int end); + +/* Call for each piece [start, end) that foreach_syllable() yields. + * foreach_syllable() cuts syllables longer than HB_MAX_SYLLABLE_LENGTH; + * this records such cuts so later walks see the same pieces. Only does + * work for pieces of HB_MAX_SYLLABLE_LENGTH glyphs. */ +static inline void +hb_syllabic_record_cut (hb_buffer_t *buffer, + unsigned int start, + unsigned int end) +{ + if (unlikely (end - start >= HB_MAX_SYLLABLE_LENGTH)) + hb_syllabic_record_cut_slow (buffer, start, end); +} + HB_INTERNAL bool hb_syllabic_clear_var (const hb_ot_shape_plan_t *plan, hb_font_t *font, diff --git a/src/hb-ot-shaper-use.cc b/src/hb-ot-shaper-use.cc index c35765af9..84153c7d7 100644 --- a/src/hb-ot-shaper-use.cc +++ b/src/hb-ot-shaper-use.cc @@ -301,7 +301,10 @@ setup_syllables_use (const hb_ot_shape_plan_t *plan, HB_BUFFER_ALLOCATE_VAR (buffer, syllable); find_syllables_use (buffer); foreach_syllable (buffer, start, end) + { + hb_syllabic_record_cut (buffer, start, end); buffer->unsafe_to_break (start, end); + } setup_rphf_mask (plan, buffer); setup_topographical_masks (plan, buffer); return false; diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 92d582841..a97430d48 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1088,6 +1088,7 @@ struct TupleVariationData const hb_vector_t>& regions, const hb_map_t& axes_old_index_tag_map, unsigned& item_count, + unsigned& num_tuple_axes, const hb_inc_bimap_t* inner_map = nullptr) { /* NULL offset, to keep original varidx valid, just return */ @@ -1102,8 +1103,31 @@ struct TupleVariationData unsigned row_size = var_data.get_row_size (); const HBUINT8 *delta_bytes = var_data.get_delta_bytes (); + /* region index -> index of its tuple in tuple_vars */ + hb_map_t region_tuple_map; for (unsigned r = 0; r < num_regions; r++) { + unsigned region_index = var_data.get_region_index (r); + if (region_index >= regions.length) return false; + + /* A region the VarData references again adds its deltas to the + * tuple it already has, as merge_tuple_variations () would do, + * instead of getting another copy of the region's axes. */ + hb_codepoint_t *tuple_index; + if (region_tuple_map.has (region_index, &tuple_index)) + { + tuple_delta_t& tuple = tuple_vars.arrayZ[*tuple_index]; + for (unsigned i = 0; i < item_count; i++) + tuple.deltas_x.arrayZ[i] += var_data.get_item_delta_fast (inner_map ? inner_map->backward (i) : i, + r, delta_bytes, row_size); + continue; + } + + /* Each tuple copies its region's axes; charge them before copying. */ + const hb_hashmap_t& region = regions.arrayZ[region_index]; + num_tuple_axes += region.get_population (); + if (unlikely (num_tuple_axes > HB_VARSTORE_MAX_TUPLE_AXES)) return false; + /* In VarData, deltas are organized in rows, convert them into * column(region) based tuples, resize deltas_x first */ tuple_delta_t tuple; @@ -1118,10 +1142,9 @@ struct TupleVariationData r, delta_bytes, row_size); } - unsigned region_index = var_data.get_region_index (r); - if (region_index >= regions.length) return false; - tuple.axis_tuples = regions.arrayZ[region_index]; + tuple.axis_tuples = region; + if (unlikely (!region_tuple_map.set (region_index, tuple_vars.length))) return false; tuple_vars.push (std::move (tuple)); } return !tuple_vars.in_error (); @@ -2005,6 +2028,7 @@ struct item_variations_t if (!vars.alloc (num_var_data) || !var_data_num_rows.alloc (num_var_data)) return false; + unsigned num_tuple_axes = 0; for (unsigned i = 0; i < num_var_data; i++) { if (inner_maps && !inner_maps.arrayZ[i].get_population ()) @@ -2015,6 +2039,7 @@ struct item_variations_t orig_region_list, axes_old_index_tag_map, item_count, + num_tuple_axes, inner_maps ? &(inner_maps.arrayZ[i]) : nullptr)) return false; diff --git a/src/hb-subset-hinting.hh b/src/hb-subset-hinting.hh new file mode 100644 index 000000000..bd66d068c --- /dev/null +++ b/src/hb-subset-hinting.hh @@ -0,0 +1,456 @@ +/* + * Copyright © 2026 Ebrahim Byagowi + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + */ + +#ifndef HB_SUBSET_HINTING_HH +#define HB_SUBSET_HINTING_HH + +#include "hb.hh" +#include "hb-open-type.hh" +#include "hb-ot-face.hh" +#include "hb-ot-glyf-table.hh" +#include "hb-ot-name-table.hh" +#include "hb-ot-os2-table.hh" +#include "hb-utf.hh" + +#ifndef HB_NO_NAME +/* Some legacy fonts have hinting programs that are required for correct + * rendering; dropping them changes glyph advances and outlines. FreeType + * likewise refuses to load these fonts without hinting. The family names are + * from src/truetype/ttobjs.c in FreeType. */ +/* Longest family name in the table, including NUL. */ +static constexpr unsigned HINT_DEPENDENT_FAMILY_NAME_SIZE = 19; + +static const char hint_dependent_font_families[][HINT_DEPENDENT_FAMILY_NAME_SIZE] = { + "cpop", "DFGirl-W6-WIN-BF", "DFGothic-EB", "DFGyoSho-Lt", + "DFHei", "DFHSGothic-W5", "DFHSMincho-W3", "DFHSMincho-W7", + "DFKaiSho-SB", "DFKaiShu", "DFKai-SB", "DFMing", + "DLC", "HuaTianKaiTi?", "HuaTianSongTi?", "Ming(for ISO10646)", + "MingLiU", "MingMedium", "PMingLiU", "MingLi43", +}; + +/* Choose the name record FreeType would use for @name_id: + * Windows English, then Macintosh English/Roman, then Unicode. */ +static inline const OT::NameRecord * +_find_hint_dependent_name_record (const OT::name &name, + hb_ot_name_id_t name_id, + bool *utf16) +{ + const OT::NameRecord *windows = nullptr; + const OT::NameRecord *apple_english = nullptr; + const OT::NameRecord *apple_roman = nullptr; + const OT::NameRecord *unicode = nullptr; + bool windows_is_english = false; + + for (unsigned i = 0; i < (unsigned) name.count; i++) + { + const OT::NameRecord &record = name.nameRecordZ[i]; + if (record.nameID != name_id || !record.length) continue; + + switch ((unsigned) record.platformID) + { + case 0: + case 2: unicode = &record; break; + + case 1: + if (record.languageID == 0) apple_english = &record; + if (record.encodingID == 0) apple_roman = &record; + break; + + case 3: { + bool english = (record.languageID & 0x3ff) == 0x009; + if ((!windows || english) && + (record.encodingID == 0 || record.encodingID == 1 || + record.encodingID == 10)) + { + windows = &record; + windows_is_english = english; + } + break; + } + } + } + + const OT::NameRecord *apple = apple_english ? apple_english : apple_roman; + if (windows && (!apple || windows_is_english)) + { + *utf16 = true; + return windows; + } + if (apple) + { + *utf16 = false; + return apple; + } + if (unicode) + { + *utf16 = true; + return unicode; + } + + return nullptr; +} + +static inline bool +_name_matches_hint_dependent_family (hb_face_t *face, + hb_ot_name_id_t name_id, + bool *found) +{ + *found = false; + hb_blob_ptr_t name_table = + hb_sanitize_context_t ().reference_table (face); + HB_SCOPE_GUARD (name_table.destroy ()); + + unsigned table_length = name_table.get_length (); + if (unlikely (!table_length)) return false; + + const OT::name &table = *name_table; + bool utf16 = false; + const OT::NameRecord *record = + _find_hint_dependent_name_record (table, name_id, &utf16); + if (unlikely (!record)) return false; + + *found = true; + + unsigned string_offset = table.stringOffset; + if (unlikely (string_offset >= table_length)) return false; + + unsigned pool_length = table_length - string_offset; + unsigned record_offset = record->offset; + if (unlikely (record_offset >= pool_length)) return false; + + unsigned record_length = + hb_min ((unsigned) record->length, pool_length - record_offset); + const char *string_data = + (const char *) &table + string_offset + record_offset; + unsigned max_chars = utf16 ? record_length / 2 : record_length; + + char stack_name[128]; + hb_vector_t name; + if (max_chars + 1 <= sizeof (stack_name)) + { + name.set_storage (stack_name); + name.shrink (max_chars + 1); + } + else if (unlikely (!name.resize (max_chars + 1))) + return false; + + char *name_string = &name[0]; + char *out = name_string; + if (utf16) + { + for (unsigned i = 0; i + 1 < record_length; i += 2) + { + uint32_t cp = + ((uint8_t) string_data[i] << 8) | (uint8_t) string_data[i + 1]; + if (!cp) break; + *out++ = (cp >= 32 && cp <= 127) ? (char) cp : '?'; + } + } + else + { + for (unsigned i = 0; i < record_length; i++) + { + uint8_t cp = string_data[i]; + if (!cp) break; + *out++ = (cp >= 32 && cp <= 127) ? (char) cp : '?'; + } + } + *out = '\0'; + + /* PDF subset tags are exactly six uppercase ASCII letters followed by + * `+`; FreeType checks the same with ft_isupper(), so don't accept + * lowercase or digits here. */ + const char *family = name_string; + bool tagged = + (uint8_t *) out - (uint8_t *) name_string >= 8 && family[6] == '+'; + if (tagged) + for (unsigned i = 0; i < 6; i++) + if (!ISALPHA ((unsigned char) family[i]) || + TOUPPER ((unsigned char) family[i]) != (unsigned char) family[i]) + { + tagged = false; + break; + } + if (tagged) family += 7; + + return + + hb_iter (hint_dependent_font_families) + | hb_map ([&] (const char (&candidate)[HINT_DEPENDENT_FAMILY_NAME_SIZE]) + { return strstr (family, candidate) != nullptr; }) + | hb_any + ; +} +#endif + +struct hint_dependent_table_id_t +{ + bool operator== (const hint_dependent_table_id_t &o) const + { return checksum == o.checksum && length == o.length; } + + uint32_t checksum; + uint32_t length; +}; + +struct hint_dependent_font_id_t +{ + bool operator== (const hint_dependent_font_id_t &o) const + { return cvt == o.cvt && fpgm == o.fpgm && prep == o.prep; } + + hint_dependent_table_id_t cvt; + hint_dependent_table_id_t fpgm; + hint_dependent_table_id_t prep; +}; + +/* cvt/fpgm/prep identities for fonts that FreeType identifies even without a + * usable name table, from src/truetype/ttobjs.c in FreeType. */ +static const hint_dependent_font_id_t hint_dependent_font_ids[] = { + /* MingLiU 1995 */ + {{0x05BCF058, 0x000002E4}, + {0x28233BF1, 0x000087C4}, + {0xA344A1EA, 0x000001E1}}, + /* MingLiU 1996- */ + {{0x05BCF058, 0x000002E4}, + {0x28233BF1, 0x000087C4}, + {0xA344A1EB, 0x000001E1}}, + /* DFGothic-EB */ + {{0x12C3EBB2, 0x00000350}, + {0xB680EE64, 0x000087A7}, + {0xCE939563, 0x00000758}}, + /* DFGyoSho-Lt */ + {{0x11E5EAD4, 0x00000350}, + {0xCE5956E9, 0x0000BC85}, + {0x8272F416, 0x00000045}}, + /* DFHei-Md-HK-BF */ + {{0x1257EB46, 0x00000350}, + {0xF699D160, 0x0000715F}, + {0xD222F568, 0x000003BC}}, + /* DFHSGothic-W5 */ + {{0x1262EB4E, 0x00000350}, + {0xE86A5D64, 0x00007940}, + {0x7850F729, 0x000005FF}}, + /* DFHSMincho-W3 */ + {{0x122DEB0A, 0x00000350}, + {0x3D16328A, 0x0000859B}, + {0xA93FC33B, 0x000002CB}}, + /* DFHSMincho-W7 */ + {{0x125FEB26, 0x00000350}, + {0xA5ACC982, 0x00007EE1}, + {0x90999196, 0x0000041F}}, + /* DFKaiShu */ + {{0x11E5EAD4, 0x00000350}, + {0x5A30CA3B, 0x00009063}, + {0x13A42602, 0x0000007E}}, + /* DFKaiShu, variant */ + {{0x11E5EAD4, 0x00000350}, + {0xA6E78C01, 0x00008998}, + {0x13A42602, 0x0000007E}}, + /* DFKaiShu-Md-HK-BF */ + {{0x11E5EAD4, 0x00000360}, + {0x9DB282B2, 0x0000C06E}, + {0x53E6D7CA, 0x00000082}}, + /* DFMing-Bd-HK-BF */ + {{0x1243EB18, 0x00000350}, + {0xBA0A8C30, 0x000074AD}, + {0xF3D83409, 0x0000037B}}, + /* DLCLiShu */ + {{0x07DCF546, 0x00000308}, + {0x40FE7C90, 0x00008E2A}, + {0x608174B5, 0x0000007A}}, + /* DLCHayBold */ + {{0xEB891238, 0x00000308}, + {0xD2E4DCD4, 0x0000676F}, + {0x8EA5F293, 0x000003B8}}, + /* HuaTianKaiTi */ + {{0xFFFBFFFC, 0x00000008}, + {0x9C9E48B8, 0x0000BEA2}, + {0x70020112, 0x00000008}}, + /* HuaTianSongTi */ + {{0xFFFBFFFC, 0x00000008}, + {0x0A5A0483, 0x00017C39}, + {0x70020112, 0x00000008}}, + /* NEC fadpop7.ttf */ + {{0x00000000, 0x00000000}, + {0x40C92555, 0x000000E5}, + {0xA39B58E3, 0x0000117C}}, + /* NEC fadrei5.ttf */ + {{0x00000000, 0x00000000}, + {0x33C41652, 0x000000E5}, + {0x26D6C52A, 0x00000F6A}}, + /* NEC fangot7.ttf */ + {{0x00000000, 0x00000000}, + {0x6DB1651D, 0x0000019D}, + {0x6C6E4B03, 0x00002492}}, + /* NEC fangyo5.ttf */ + {{0x00000000, 0x00000000}, + {0x40C92555, 0x000000E5}, + {0xDE51FAD0, 0x0000117C}}, + /* NEC fankyo5.ttf */ + {{0x00000000, 0x00000000}, + {0x85E47664, 0x000000E5}, + {0xA6C62831, 0x00001CAA}}, + /* NEC fanrgo5.ttf */ + {{0x00000000, 0x00000000}, + {0x2D891CFD, 0x0000019D}, + {0xA0604633, 0x00001DE8}}, + /* NEC fangot5.ttc */ + {{0x00000000, 0x00000000}, + {0x40AA774C, 0x000001CB}, + {0x9B5CAA96, 0x00001F9A}}, + /* NEC fanmin3.ttc */ + {{0x00000000, 0x00000000}, + {0x0D3DE9CB, 0x00000141}, + {0xD4127766, 0x00002280}}, + /* NEC FA-Gothic, 1996 */ + {{0x00000000, 0x00000000}, + {0x4A692698, 0x000001F0}, + {0x340D4346, 0x00001FCA}}, + /* NEC FA-Minchou, 1996 */ + {{0x00000000, 0x00000000}, + {0xCD34C604, 0x00000166}, + {0x6CF31046, 0x000022B0}}, + /* NEC FA-RoundGothicB, 1996 */ + {{0x00000000, 0x00000000}, + {0x5DA75315, 0x0000019D}, + {0x40745A5F, 0x000022E0}}, + /* NEC FA-RoundGothicM, 1996 */ + {{0x00000000, 0x00000000}, + {0xF055FC48, 0x000001C2}, + {0x3900DED3, 0x00001E18}}, + /* MINGLI.TTF, 1992 */ + {{0x00170003, 0x00000060}, + {0xDBB4306E, 0x000058AA}, + {0xD643482A, 0x00000035}}, + /* DFHei-Bd-WIN-HK-BF */ + {{0x1269EB58, 0x00000350}, + {0x5CD5957A, 0x00006A4E}, + {0xF758323A, 0x00000380}}, + /* DFMing-Md-WIN-HK-BF */ + {{0x122FEB0B, 0x00000350}, + {0x7F10919A, 0x000070A9}, + {0x7CD7E7B7, 0x0000025C}}, +}; + +static inline hint_dependent_table_id_t +_table_id (hb_face_t *face, hb_tag_t tag) +{ + hint_dependent_table_id_t id = {0, 0}; + + hb_blob_t *blob = hb_face_reference_table (face, tag); + unsigned length = hb_blob_get_length (blob); + if (length) + { + unsigned data_length; + const char *data = hb_blob_get_data (blob, &data_length); + id.checksum = OT::CheckSum::CalcTableChecksum ( + (const OT::HBUINT32 *) data, data_length); + id.length = data_length; + } + hb_blob_destroy (blob); + + return id; +} + +static inline bool +_matches_hint_dependent_ids (hb_face_t *face) +{ + hint_dependent_font_id_t id; + id.cvt = _table_id (face, HB_TAG ('c', 'v', 't', ' ')); + id.fpgm = _table_id (face, HB_TAG ('f', 'p', 'g', 'm')); + id.prep = _table_id (face, HB_TAG ('p', 'r', 'e', 'p')); + + return + + hb_iter (hint_dependent_font_ids) + | hb_map ([&] (const hint_dependent_font_id_t &candidate) { return id == candidate; }) + | hb_any + ; +} + +static hb_user_data_key_t _hb_subset_hint_dependent_user_data_key = {0}; + +static inline bool +_hb_subset_face_requires_hinting_uncached (hb_face_t *face) +{ + if (!face->table.glyf->has_data ()) return false; + +#ifndef HB_NO_NAME + /* Match FreeType's choice: with the OS/2 WWS bit set the typographic + * family is preferred; otherwise the WWS family wins over it. */ + hb_ot_name_id_t name_ids[3]; + unsigned name_count; + if (face->table.OS2->fsSelection & OT::OS2::WWS) + { + name_ids[0] = HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY; + name_ids[1] = HB_OT_NAME_ID_FONT_FAMILY; + name_count = 2; + } + else + { + name_ids[0] = HB_OT_NAME_ID_WWS_FAMILY; + name_ids[1] = HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY; + name_ids[2] = HB_OT_NAME_ID_FONT_FAMILY; + name_count = 3; + } + for (unsigned i = 0; i < name_count; i++) + { + bool found = false; + if (_name_matches_hint_dependent_family (face, name_ids[i], &found)) + return true; + /* Do not fall through to a lower-priority family name if the + * preferred one exists but does not match. */ + if (found) break; + } +#endif + + return _matches_hint_dependent_ids (face); +} + +static inline bool +_hb_subset_face_requires_hinting (hb_face_t *face) +{ + enum class hint_dependent_cache_t : uintptr_t + { + UNCACHED, + CACHED_FALSE, + CACHED_TRUE, + }; + /* The user-data pointer is the enum value; UNCACHED is zero. */ + hint_dependent_cache_t cached = static_cast ( + (uintptr_t) hb_face_get_user_data (face, + &_hb_subset_hint_dependent_user_data_key)); + if (cached != hint_dependent_cache_t::UNCACHED) + return cached == hint_dependent_cache_t::CACHED_TRUE; + + bool result = _hb_subset_face_requires_hinting_uncached (face); + + hint_dependent_cache_t cache = result ? hint_dependent_cache_t::CACHED_TRUE + : hint_dependent_cache_t::CACHED_FALSE; + hb_face_set_user_data (face, &_hb_subset_hint_dependent_user_data_key, + (void *) static_cast (cache), + nullptr, false); + + return result; +} + +#endif /* HB_SUBSET_HINTING_HH */ diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index bc0238ae5..8f661cb18 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -31,6 +31,7 @@ #include "hb-set.hh" #include "hb-subset.h" #include "hb-unicode.h" +#include "hb-subset-hinting.hh" #include "hb-ot-cmap-table.hh" #include "hb-ot-glyf-table.hh" @@ -761,6 +762,13 @@ hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face, successful = true; flags = input->flags; + if ((flags & HB_SUBSET_FLAGS_NO_HINTING) && _hb_subset_face_requires_hinting (face)) + { + DEBUG_MSG (SUBSET, nullptr, + "Keeping hinting because the font requires it."); + flags &= ~HB_SUBSET_FLAGS_NO_HINTING; + } + unicode_to_new_gid_list.init (); name_ids = *input->sets.name_ids; diff --git a/src/hb-subset.h b/src/hb-subset.h index ec3171873..27e94eb67 100644 --- a/src/hb-subset.h +++ b/src/hb-subset.h @@ -59,7 +59,8 @@ typedef struct hb_subset_plan_t hb_subset_plan_t; * hb_subset_flags_t: * @HB_SUBSET_FLAGS_DEFAULT: all flags at their default value of false. * @HB_SUBSET_FLAGS_NO_HINTING: If set hinting instructions will be dropped in - * the produced subset. Otherwise hinting instructions will be retained. + * the produced subset. Otherwise hinting instructions will be retained. This + * flag is ignored for fonts whose hinting is required for correct rendering. * @HB_SUBSET_FLAGS_RETAIN_GIDS: If set glyph indices will not be modified in * the produced subset. If glyphs are dropped their indices will be retained * as an empty glyph. This is implied when the subset retains a table that diff --git a/src/meson.build b/src/meson.build index 93f5d4765..0f05e005d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -401,6 +401,7 @@ hb_subset_sources = files( 'hb-subset-cff2.cc', 'hb-subset-cff2-round.hh', 'hb-subset-cff2-to-cff1.hh', + 'hb-subset-hinting.hh', 'hb-subset-input.cc', 'hb-subset-input.hh', 'hb-subset-instancer-iup.hh', @@ -995,8 +996,11 @@ if get_option('tests').enabled() 'test-algs': ['test-algs.cc', 'hb-static.cc'], 'test-array': ['test-array.cc'], 'test-bimap': ['test-bimap.cc', 'hb-static.cc'], + 'test-bit-vector': ['test-bit-vector.cc', 'hb-static.cc'], + 'test-gdef-cache': ['test-gdef-cache.cc', 'hb-static.cc'], 'test-cache': ['test-cache.cc'], 'test-cff': ['test-cff.cc', 'hb-static.cc'], + 'test-checksum': ['test-checksum.cc', 'hb-static.cc'], 'test-colr': ['test-colr.cc', 'hb-static.cc'], 'test-classdef-graph': ['graph/test-classdef-graph.cc', 'hb-static.cc', 'graph/gsubgpos-context.cc'], 'test-result': ['test-result.cc', 'hb-static.cc'], diff --git a/src/test-bit-vector.cc b/src/test-bit-vector.cc new file mode 100644 index 000000000..fd0aa918b --- /dev/null +++ b/src/test-bit-vector.cc @@ -0,0 +1,119 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#define hb_calloc bit_vector_test_calloc + +#include "hb-bit-vector.hh" + +static bool fail_allocation; +static size_t last_allocation; + +extern "C" void * +bit_vector_test_calloc (size_t count, size_t size) +{ + last_allocation = count * size; + return fail_allocation ? nullptr : calloc (count, size); +} + +int +main () +{ + /* Check every bit offset, including ranges that wrap within a word + * or cross a word boundary relative to an unaligned minimum. */ + for (unsigned bias = 0; bias < 64; bias++) + for (unsigned first = 0; first < 130; first++) + for (unsigned last = first; last < 130; last++) + { + hb_bit_vector_t<> bits (bias, bias + 129); + hb_always_assert (!bits.in_error ()); + hb_always_assert (bits.add_range (bias + first, bias + last)); + for (unsigned i = 0; i < 130; i++) + hb_always_assert (bits.has (bias + i) == (first <= i && i <= last)); + bits.del_range (bias + first, bias + last); + for (unsigned i = 0; i < 130; i++) + hb_always_assert (!bits.has (bias + i)); + bits.add_range (bias, bias + 129); + bits.del_range (bias + first, bias + last); + for (unsigned i = 0; i < 130; i++) + hb_always_assert (bits.has (bias + i) == (i < first || i > last)); + } + + for (unsigned value : {0u, 37u, UINT_MAX}) + { + hb_bit_vector_t<> bits (value, value); + hb_always_assert (!bits.in_error ()); + hb_always_assert (bits.count == 1 && last_allocation == 8); + hb_always_assert (!bits.has (value)); + bits.add (value); + hb_always_assert (bits.has (value)); + bits.del (value); + hb_always_assert (!bits.has (value)); + } + + for (unsigned size : {1u, 64u, 65u, 512u}) + { + hb_bit_vector_t<> bits (37, 37 + size - 1); + hb_always_assert (last_allocation == (size + 63) / 64 * 8); + bits.add_range (37, 37 + size - 1); + hb_always_assert (!bits.has (36) && !bits.has (37 + size)); + hb_always_assert (!bits.has (UINT_MAX)); + bits.add (36); + bits.del (37 + size); + hb_always_assert (bits.has (37) && bits.has (37 + size - 1)); + hb_bit_vector_t<> moved (std::move (bits)); + hb_always_assert (bits.in_error () && moved.has (37)); + bits = std::move (moved); + hb_always_assert (bits.has (37) && moved.in_error ()); + } + + /* Empty/reversed and unrepresentable inclusive ranges must not allocate. */ + last_allocation = 0; + hb_bit_vector_t<> empty (1, 0); + hb_bit_vector_t<> overflow (0, UINT_MAX); + hb_always_assert (empty.in_error () && overflow.in_error ()); + hb_always_assert (!last_allocation); + + fail_allocation = true; + hb_bit_vector_t<> failed (37, 102); + hb_always_assert (failed.in_error () && !failed.count); + hb_always_assert (!failed.has (37)); + failed.add (37); + hb_always_assert (!failed.has (37)); + hb_always_assert (!failed.add_range (37, 102)); + fail_allocation = false; + + const unsigned values[] = {37, 63, 64, 65, 100}; + auto array = hb_sorted_array (values); + hb_min_max_t extents; + hb_always_assert (extents.add_range (100, 37)); + hb_always_assert (extents.get_min () == HB_CODEPOINT_INVALID); + hb_always_assert (extents.add_sorted_array (array)); + hb_always_assert (extents.get_min () == 37 && extents.get_max () == 100); + hb_bit_vector_t<> bits (extents.get_min (), extents.get_max ()); + hb_always_assert (bits.add_sorted_array (array)); + for (unsigned i = 0; i < 110; i++) + hb_always_assert (bits.has (i) == (i == 37 || i == 63 || i == 64 || i == 65 || i == 100)); +} diff --git a/src/test-cff.cc b/src/test-cff.cc index 95089e3ec..2153a0c48 100644 --- a/src/test-cff.cc +++ b/src/test-cff.cc @@ -103,6 +103,39 @@ test_specialized_fractional_geometry () } } +static void +test_specialized_many_commands () +{ + /* Combining lines must not get slower with the number of commands that + * follow them, and must leave those commands alone. */ + const unsigned n = 100000; + hb_vector_t commands; + for (unsigned i = 0; i < n; i++) + { + CFF::cs_command_t cmd (OpCode_rlineto); + CFF::number_t dx, dy; + dx.set_int ((i & 1) ? 0 : 1); + dy.set_int ((i & 1) ? 1 : 0); + cmd.args.push (dx); + cmd.args.push (dy); + commands.push (std::move (cmd)); + } + for (unsigned i = 0; i < n; i++) + commands.push (CFF::cs_command_t (OpCode_hhcurveto)); + CFF::specialize_commands (commands); + unsigned num_lines = (n + 46) / 47; + hb_always_assert (commands.length == num_lines + n); + unsigned num_args = 0; + for (unsigned i = 0; i < num_lines; i++) + { + hb_always_assert (commands[i].op == ((num_args & 1) ? OpCode_vlineto : OpCode_hlineto)); + num_args += commands[i].args.length; + } + hb_always_assert (num_args == n); + for (unsigned i = num_lines; i < commands.length; i++) + hb_always_assert (commands[i].op == OpCode_hhcurveto && !commands[i].args.length); +} + static void test_charset1_splits_long_ranges () { @@ -319,6 +352,7 @@ main (int argc, char **argv) test_index_count_limits (); test_specialized_line_geometry (); test_specialized_fractional_geometry (); + test_specialized_many_commands (); test_charset1_splits_long_ranges (); test_fd_select_sentinel (); test_fd_select_sentinel (); diff --git a/src/test-checksum.cc b/src/test-checksum.cc new file mode 100644 index 000000000..6b70c75f7 --- /dev/null +++ b/src/test-checksum.cc @@ -0,0 +1,167 @@ +/* + * Copyright © 2026 Ebrahim Byagowi + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "hb.hh" +#include "hb-open-type.hh" + +static void +test_calc_table_checksum () +{ + static const uint8_t data[] = {'a', 'b', 'c', 'd', 'e'}; + const OT::HBUINT32 *words = (const OT::HBUINT32 *) data; + + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 0) == 0u); + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 1) == 0x61000000u); + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 2) == 0x61620000u); + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 3) == 0x61626300u); + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 4) == 0x61626364u); + hb_always_assert (OT::CheckSum::CalcTableChecksum (words, 5) == 0xC6626364u); + + /* The data pointer is deliberately unaligned. */ + hb_always_assert (OT::CheckSum::CalcTableChecksum ( + (const OT::HBUINT32 *) (data + 1), 4) == 0x62636465u); + hb_always_assert (OT::CheckSum::CalcTableChecksum ( + (const OT::HBUINT32 *) (data + 1), 3) == 0x62636400u); + + OT::CheckSum checksum; + for (unsigned length = 0; length <= 5; length++) + { + checksum.set_for_data (data, length); + hb_always_assert ((uint32_t) checksum == + OT::CheckSum::CalcTableChecksum (words, length)); + } + + checksum.set_for_data (data + 1, 4); + hb_always_assert ((uint32_t) checksum == 0x62636465u); +} + +/* The following vectors were generated once with Python's int.from_bytes(). + * They are embedded here rather than regenerated by a checked-in script. */ +static void +test_python_generated_checksums () +{ + static const uint8_t data[] = { + 0x94, 0x26, 0x60, 0xCD, 0xAD, 0x32, 0xF7, 0x18, 0xEF, 0xE1, + 0xC3, 0x9B, 0xC1, 0xE9, 0x65, 0xA4, 0x27, 0xE8, 0x28, 0x4E, + 0x5E, 0x87, 0x80, 0xF2, 0x20, 0x43, 0x86, 0xCC, 0x7D, 0x99, + 0x37, 0x2F, 0xA0, 0xC5, 0xCB, 0x47, 0x22, 0x44, 0x9C, 0x36, + }; + static const struct + { + unsigned offset; + unsigned length; + uint32_t checksum; + } cases[] = { + {0, 0, 0x00000000u}, + {0, 1, 0x94000000u}, + {0, 2, 0x94260000u}, + {0, 3, 0x94266000u}, + {0, 4, 0x942660CDu}, + {0, 5, 0x412660CDu}, + {0, 6, 0x415860CDu}, + {0, 7, 0x415957CDu}, + {0, 8, 0x415957E5u}, + {0, 9, 0x305957E5u}, + {0, 11, 0x313B1AE5u}, + {0, 13, 0xF23B1B80u}, + {0, 16, 0xF3248124u}, + {0, 19, 0x1B0CA924u}, + {0, 23, 0x79942972u}, + {0, 27, 0x99D7B064u}, + {0, 31, 0x1770E830u}, + {0, 32, 0x1770E85Fu}, + {1, 0, 0x00000000u}, + {1, 1, 0x26000000u}, + {1, 2, 0x26600000u}, + {1, 3, 0x2660CD00u}, + {1, 4, 0x2660CDADu}, + {1, 5, 0x5860CDADu}, + {1, 6, 0x5957CDADu}, + {1, 7, 0x5957E5ADu}, + {1, 8, 0x5957E69Cu}, + {1, 9, 0x3A57E69Cu}, + {1, 11, 0x3B1B819Cu}, + {1, 13, 0x241B825Du}, + {1, 16, 0x24812684u}, + {1, 19, 0x0CA97484u}, + {1, 23, 0x942A66E2u}, + {1, 27, 0xD7B13302u}, + {1, 31, 0x70E8627Fu}, + {1, 32, 0x70E8631Fu}, + {2, 0, 0x00000000u}, + {2, 1, 0x60000000u}, + {2, 2, 0x60CD0000u}, + {2, 3, 0x60CDAD00u}, + {2, 4, 0x60CDAD32u}, + {2, 5, 0x57CDAD32u}, + {2, 6, 0x57E5AD32u}, + {2, 7, 0x57E69C32u}, + {2, 8, 0x57E69D13u}, + {2, 9, 0x1AE69D13u}, + {2, 11, 0x1B825E13u}, + {2, 13, 0x80825EFCu}, + {2, 16, 0x812686E4u}, + {2, 19, 0xA974E4E4u}, + {2, 23, 0x2A67056Bu}, + {2, 27, 0xB13382AEu}, + {2, 31, 0xE8632347u}, + {2, 32, 0xE863240Cu}, + {3, 0, 0x00000000u}, + {3, 1, 0xCD000000u}, + {3, 2, 0xCDAD0000u}, + {3, 3, 0xCDAD3200u}, + {3, 4, 0xCDAD32F7u}, + {3, 5, 0xE5AD32F7u}, + {3, 6, 0xE69C32F7u}, + {3, 7, 0xE69D13F7u}, + {3, 8, 0xE69D14BAu}, + {3, 9, 0x819D14BAu}, + {3, 11, 0x825EFDBAu}, + {3, 13, 0x265EFE1Fu}, + {3, 16, 0x2686E647u}, + {3, 19, 0x74E56D47u}, + {3, 23, 0x6705B0C7u}, + {3, 27, 0x33834A4Du}, + {3, 31, 0x63240F84u}, + {3, 32, 0x6324104Fu}, + }; + + for (const auto &c : cases) + { + const uint8_t *p = data + c.offset; + hb_always_assert (OT::CheckSum::CalcTableChecksum ((const OT::HBUINT32 *) p, + c.length) == c.checksum); + OT::CheckSum checksum; + checksum.set_for_data (p, c.length); + hb_always_assert ((uint32_t) checksum == c.checksum); + } +} + +int +main () +{ + test_calc_table_checksum (); + test_python_generated_checksums (); + return 0; +} diff --git a/src/test-gdef-cache.cc b/src/test-gdef-cache.cc new file mode 100644 index 000000000..d3b884cf0 --- /dev/null +++ b/src/test-gdef-cache.cc @@ -0,0 +1,67 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb-ot-layout-gdef-table.hh" + +int +main () +{ +#if !defined(HB_NO_GDEF_CACHE) && !defined(HB_NO_OT_LAYOUT) + /* Empty, singleton, sparse, unaligned ranges, all glyphs, and spans + * immediately below and above the exact-cache limit. */ + static const unsigned char data[] = { + 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, + 0, 1, 0, 7, + 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 42, 0, 0, 0, 56, + 0, 0, 0, 72, 0, 0, 0, 82, 0, 0, 0, 92, + 0, 1, 0, 0, + 0, 1, 0, 1, 0, 37, + 0, 1, 0, 5, 0, 37, 0, 63, 0, 64, 0, 65, 0, 100, + 0, 2, 0, 2, 0, 37, 0, 102, 0, 0, 0, 150, 0, 159, 0, 66, + 0, 2, 0, 1, 0, 0, 255, 255, 0, 0, + 0, 2, 0, 1, 0, 37, 2, 36, 0, 0, + 0, 2, 0, 1, 0, 37, 2, 37, 0, 0, + }; + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create ((const char *) data, sizeof (data), + HB_MEMORY_MODE_READONLY, nullptr, nullptr); + hb_always_assert (hb_face_builder_add_table (face, HB_OT_TAG_GDEF, blob)); + hb_blob_destroy (blob); + hb_ot_layout_has_glyph_classes (face); + const auto *accel = face->table.GDEF.get_relaxed (); + hb_always_assert (accel && accel->mark_glyph_set_bitmaps.length == 7); + for (unsigned set = 0; set < 7; set++) + { + bool exact = set == 1 || set == 2 || set == 3 || set == 5; + hb_always_assert (accel->mark_glyph_set_bitmaps[set].glyphs.in_error () == !exact); + for (unsigned glyph = 0; glyph <= 65536; glyph++) + hb_always_assert (accel->mark_set_covers (set, glyph) == + accel->table->mark_set_covers (set, glyph)); + hb_always_assert (!accel->mark_set_covers (set, UINT_MAX)); + } + hb_face_destroy (face); +#endif +} diff --git a/src/test-item-varstore.cc b/src/test-item-varstore.cc index 7e0ca2eed..80497ef4a 100644 --- a/src/test-item-varstore.cc +++ b/src/test-item-varstore.cc @@ -457,6 +457,53 @@ test_condition_budget () hb_always_assert (budget < 0); } +static void +test_repeated_region_index () +{ + /* One axis, two regions, and a VarData whose region indices are {0, 1, 0}: + * the repeated region's two delta columns must be summed into one tuple. */ + const char store_data[] = + "\x00\x01\x00\x00\x00\x0c\x00\x01\x00\x00\x00\x1c" // ItemVariationStore + "\x00\x01\x00\x02" // VarRegionList: 1 axis, 2 regions + "\x00\x00\x40\x00\x40\x00" // region 0: (0, 1, 1) + "\xc0\x00\xc0\x00\x00\x00" // region 1: (-1, -1, 0) + "\x00\x02\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00" // VarData: 2 items, regions {0, 1, 0} + "\x01\x02\x03" // item 0 + "\x0a\x14\x1e"; // item 1 + const auto &store = *reinterpret_cast (store_data); + hb_map_t axis_map; + axis_map.set (0, HB_TAG ('T','E','S','T')); + + hb_vector_t> regions; + hb_always_assert (store.get_region_list ().get_var_regions (axis_map, regions)); + OT::tuple_variations_t tuples; + unsigned item_count = 0, num_tuple_axes = 0; + hb_always_assert (tuples.create_from_item_var_data (store.get_sub_table (0), regions, axis_map, + item_count, num_tuple_axes)); + hb_always_assert (item_count == 2 && num_tuple_axes == 2); + hb_always_assert (tuples.tuple_vars.length == 2); + hb_always_assert (tuples.tuple_vars[0].deltas_x[0] == 4.f && tuples.tuple_vars[0].deltas_x[1] == 40.f); + hb_always_assert (tuples.tuple_vars[1].deltas_x[0] == 2.f && tuples.tuple_vars[1].deltas_x[1] == 20.f); + + /* The region axes copied for one store are capped. */ + OT::tuple_variations_t capped; + num_tuple_axes = HB_VARSTORE_MAX_TUPLE_AXES - 1; + hb_always_assert (!capped.create_from_item_var_data (store.get_sub_table (0), regions, axis_map, + item_count, num_tuple_axes)); + + OT::item_variations_t item_vars; + hb_always_assert (item_vars.create_from_item_varstore (store, axis_map)); + hb_hashmap_t locations; + hb_hashmap_t distances; + hb_always_assert (item_vars.instantiate_tuple_vars (locations, distances)); + hb_always_assert (item_vars.as_item_varstore (false)); + hb_always_assert (item_vars.get_region_list ().length == 2); + const auto &items = item_vars.get_vardata_encodings ()[0].items; + hb_always_assert (items.length == 2); + hb_always_assert ((*items[0])[0] == 4 && (*items[0])[1] == 2); + hb_always_assert ((*items[1])[0] == 40 && (*items[1])[1] == 20); +} + static void test_failed_encoding () { @@ -530,4 +577,5 @@ main (int argc, char **argv) test_condition_delta_precision (); test_multi_instancer_budget (); test_condition_budget (); + test_repeated_region_index (); } diff --git a/test/api/test-be-subset-tables.c b/test/api/test-be-subset-tables.c index 6bd1fa898..f520da777 100644 --- a/test/api/test-be-subset-tables.c +++ b/test/api/test-be-subset-tables.c @@ -182,6 +182,116 @@ test_vorg_versions (void) #endif +static hb_face_t * +create_gsub_v1_2_face (void) +{ + /* A version 1.2 GSUB that keeps its lists behind the 16-bit offsets + * and has a FeatureVariations table. */ + static const char GSUB_data[] = { + 0, 1, 0, 2, /* version 1.2 */ + 0, 26, /* scriptList */ + 0, 46, /* featureList */ + 0, 62, /* lookupList */ + 0, 0, 0, 108, /* featureVars */ + 0, 0, 0, 0, /* scriptList2 */ + 0, 0, 0, 0, /* featureList2 */ + 0, 0, 0, 0, /* lookupList2 */ + /* ScriptList */ + 0, 1, 'D', 'F', 'L', 'T', 0, 8, /* one script at 8 */ + 0, 4, 0, 0, /* defaultLangSys at 4, no LangSys records */ + 0, 0, 0xFF, 0xFF, 0, 1, 0, 0, /* no required feature, feature 0 */ + /* FeatureList */ + 0, 1, 'c', 'c', 'm', 'p', 0, 8, /* one feature at 8 */ + 0, 0, 0, 2, 0, 0, 0, 1, /* lookups 0 and 1 */ + /* LookupList */ + 0, 2, 0, 6, 0, 26, /* two 16-bit lookup offsets */ + 0, 1, 0, 0, 0, 1, 0, 8, /* SingleSubst, one subtable at 8 */ + 0, 1, 0, 6, 0, 1, /* format 1, coverage at 6, delta +1 */ + 0, 1, 0, 1, 0, 1, /* coverage: glyph 1 */ + 0, 1, 0, 0, 0, 1, 0, 8, /* SingleSubst, one subtable at 8 */ + 0, 1, 0, 6, 0, 2, /* format 1, coverage at 6, delta +2 */ + 0, 1, 0, 1, 0, 2, /* coverage: glyph 2 */ + /* FeatureVariations */ + 0, 1, 0, 0, /* version 1.0 */ + 0, 0, 0, 1, /* one record */ + 0, 0, 0, 16, 0, 0, 0, 30, /* conditionSet at 16, substitutions at 30 */ + 0, 1, 0, 0, 0, 6, /* one condition at 6 */ + 0, 1, 0, 0, 0x20, 0, 0x40, 0, /* axis 0 in [0.5, 1.0] */ + 0, 1, 0, 0, 0, 1, /* one substitution record */ + 0, 0, 0, 0, 0, 12, /* feature 0, alternate Feature at 12 */ + 0, 0, 0, 1, 0, 0, /* lookup 0 only */ + }; + static const char FVAR_data[] = { + 0, 1, 0, 0, /* version 1.0 */ + 0, 16, 0, 2, 0, 1, 0, 20, /* one axis at 16, axisSize 20 */ + 0, 0, 0, 8, /* no instances */ + 'w', 'g', 'h', 't', + 0, 100, 0, 0, /* minValue 100 */ + 1, 0x90, 0, 0, /* defaultValue 400 */ + 3, 0x84, 0, 0, /* maxValue 900 */ + 0, 0, 1, 0, /* flags, axisNameID */ + }; + static const char MAXP_data[] = { + 0, 0, 0x50, 0, /* version 0.5 */ + 0, 5, /* numGlyphs */ + }; + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "GSUB", GSUB_data); + HB_FACE_ADD_TABLE (face, "fvar", FVAR_data); + HB_FACE_ADD_TABLE (face, "maxp", MAXP_data); + return face; +} + +static void +check_gsub_v1_2_lookup (hb_face_t *face, unsigned lookup_index, + hb_codepoint_t glyph, hb_codepoint_t expected) +{ + unsigned count = 1; + hb_codepoint_t alternate = HB_CODEPOINT_INVALID; + g_assert_cmpuint (hb_ot_layout_lookup_get_glyph_alternates (face, lookup_index, glyph, + 0, &count, &alternate), + ==, 1); + g_assert_cmpuint (alternate, ==, expected); +} + +static void +check_gsub_v1_2 (hb_face_t *face, hb_codepoint_t last_glyph, hb_bool_t has_feature_variations) +{ + int coords[] = {0x4000}; /* wght 900 */ + unsigned index; + g_assert_cmpuint (hb_ot_layout_table_get_lookup_count (face, HB_OT_TAG_GSUB), ==, 2); + check_gsub_v1_2_lookup (face, 0, 1, 2); + check_gsub_v1_2_lookup (face, 1, 2, last_glyph); + g_assert_cmpint (hb_ot_layout_table_find_feature_variations (face, HB_OT_TAG_GSUB, + coords, 1, &index), + ==, has_feature_variations); +} + +static void +test_gsub_v1_2_subset (void) +{ + hb_face_t *face = create_gsub_v1_2_face (); + check_gsub_v1_2 (face, 4, TRUE); + + /* Glyph 1 closes over 2 and 4; 4 is renumbered to 3. */ + hb_set_t *glyphs = hb_set_create (); + hb_set_add (glyphs, 1); + hb_face_t *subset = hb_subset_test_create_subset ( + face, hb_subset_test_create_input_from_glyphs (glyphs)); + check_gsub_v1_2 (subset, 3, TRUE); + hb_face_destroy (subset); + + /* Pinning the only axis drops the FeatureVariations. */ + hb_subset_input_t *input = hb_subset_test_create_input_from_glyphs (glyphs); + g_assert_true (hb_subset_input_pin_axis_to_default (input, face, HB_TAG ('w','g','h','t'))); + subset = hb_subset_test_create_subset (face, input); + check_gsub_v1_2 (subset, 3, FALSE); + hb_face_destroy (subset); + + hb_set_destroy (glyphs); + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -191,6 +301,7 @@ main (int argc, char **argv) hb_test_add (test_colr_paint_glyph2); hb_test_add (test_vorg_versions); #endif + hb_test_add (test_gsub_v1_2_subset); return hb_test_run (); } diff --git a/test/api/test-ot-collect-glyphs.c b/test/api/test-ot-collect-glyphs.c index 41f61807b..8b307906e 100644 --- a/test/api/test-ot-collect-glyphs.c +++ b/test/api/test-ot-collect-glyphs.c @@ -481,6 +481,107 @@ test_ot_layout_lookup_collect_glyphs_qahiri (void) hb_set_destroy (output); } +static uint8_t * +put16 (uint8_t *p, unsigned v) +{ + *p++ = (uint8_t) (v >> 8); + *p++ = (uint8_t) v; + return p; +} + +static void +test_ot_layout_lookup_collect_glyphs_shared_class_rules (void) +{ + /* A ContextSubstFormat2 whose RuleSet offsets all point at one RuleSet, + * whose Rule offsets all point at one Rule with 64 class values, over a + * 65535-entry ClassDef. The ClassDef must be walked once per subtable, + * not once per class value of every Rule visited; the latter takes + * about a minute. */ + enum { RULESETS = 16, RULES = 1000, CLASSES = 64, GLYPHS = 65535 }; + unsigned rule_len = 4 + 2 * CLASSES; + unsigned ruleset_len = 2 + 2 * RULES + rule_len; + unsigned coverage_len = 6; + unsigned classdef_len = 6 + 2 * GLYPHS; + unsigned ruleset_offset = 8 + 2 * RULESETS; + unsigned coverage_offset = ruleset_offset + ruleset_len; + unsigned classdef_offset = coverage_offset + coverage_len; + unsigned gsub_len = 10 + 2 + 2 + 4 + 8 + classdef_offset + classdef_len; + uint8_t *gsub = g_malloc0 (gsub_len); + uint8_t *p = gsub; + + /* GSUB header, empty ScriptList and FeatureList, LookupList with one lookup. */ + p = put16 (p, 1); p = put16 (p, 0); + p = put16 (p, 10); p = put16 (p, 12); p = put16 (p, 14); + p = put16 (p, 0); + p = put16 (p, 0); + p = put16 (p, 1); p = put16 (p, 4); + /* Lookup: type 5 (Context), one subtable right after. */ + p = put16 (p, 5); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8); + /* ContextSubstFormat2 */ + p = put16 (p, 2); p = put16 (p, coverage_offset); p = put16 (p, classdef_offset); + p = put16 (p, RULESETS); + for (unsigned i = 0; i < RULESETS; i++) + p = put16 (p, ruleset_offset); + /* RuleSet */ + p = put16 (p, RULES); + for (unsigned i = 0; i < RULES; i++) + p = put16 (p, 2 + 2 * RULES); + /* Rule: classes 1 and 2, alternating. */ + p = put16 (p, CLASSES + 1); p = put16 (p, 0); + for (unsigned i = 0; i < CLASSES; i++) + p = put16 (p, 1 + (i & 1)); + /* Coverage: glyph 1. */ + p = put16 (p, 1); p = put16 (p, 1); p = put16 (p, 1); + /* ClassDefFormat1: glyph i has class i % 3. */ + p = put16 (p, 1); p = put16 (p, 0); p = put16 (p, GLYPHS); + for (unsigned i = 0; i < GLYPHS; i++) + p = put16 (p, i % 3); + g_assert_cmpuint (p - gsub, ==, gsub_len); + + hb_blob_t *blob = hb_blob_create_or_fail ((const char *) gsub, gsub_len, + HB_MEMORY_MODE_WRITABLE, gsub, g_free); + hb_face_t *builder = hb_face_builder_create (); + hb_face_builder_add_table (builder, HB_OT_TAG_GSUB, 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); + + hb_set_t *before = hb_set_create(); + hb_set_t *input = hb_set_create(); + hb_set_t *after = hb_set_create(); + hb_set_t *output = hb_set_create(); + hb_codepoint_t g = HB_SET_VALUE_INVALID; + + g_assert_cmpuint (1, ==, hb_ot_layout_table_get_lookup_count (face, HB_OT_TAG_GSUB)); + + BEGIN(HB_OT_TAG_GSUB, 0); + g_assert_cmpuint (0, ==, hb_set_get_population (before)); + + /* Glyph 1 from the coverage, and every glyph of class 1 or 2. */ + g_assert_cmpuint (2 * (GLYPHS / 3), ==, hb_set_get_population (input)); + g = HB_SET_VALUE_INVALID; + g_assert_true (hb_set_next (input, &g)); + g_assert_cmpuint (1, ==, g); + g_assert_true (hb_set_next (input, &g)); + g_assert_cmpuint (2, ==, g); + g_assert_true (hb_set_next (input, &g)); + g_assert_cmpuint (4, ==, g); + g_assert_false (hb_set_has (input, 0)); + g_assert_true (hb_set_has (input, GLYPHS - 1)); + + g_assert_cmpuint (0, ==, hb_set_get_population (after)); + g_assert_cmpuint (0, ==, hb_set_get_population (output)); + END(); + + hb_face_destroy (face); + hb_set_destroy (before); + hb_set_destroy (input); + hb_set_destroy (after); + hb_set_destroy (output); +} + int main (int argc, char **argv) { @@ -488,5 +589,6 @@ main (int argc, char **argv) hb_test_add (test_ot_layout_lookup_collect_glyphs_source_sans); hb_test_add (test_ot_layout_lookup_collect_glyphs_noto_nastaliq); hb_test_add (test_ot_layout_lookup_collect_glyphs_qahiri); + hb_test_add (test_ot_layout_lookup_collect_glyphs_shared_class_rules); return hb_test_run (); } diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index e16c15935..449e8a4c4 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -684,6 +684,117 @@ test_depend_varc (void) hb_face_destroy (face); } +/* A GSUB face whose features have no lookups of their own. Feature 0 is 'aaaa', + * the others are 'bbbb', and the default LangSys lists all but the last. Every + * FeatureVariations record uses the same FeatureTableSubstitution, which gives + * lookup 0 (glyph 1 -> 2) to feature 0 and lookup 1 (glyph 3 -> 4) to the last + * feature; its other records are for a feature that does not exist. */ +static hb_face_t * +create_feature_variations_face (unsigned num_features, + unsigned num_records, + unsigned num_substitutions) +{ + unsigned lookup_list_offset = 14; + unsigned script_list_offset = lookup_list_offset + 6 + 2 * 20; + unsigned feature_list_offset = script_list_offset + 18 + (num_features - 1) * 2; + unsigned variations_offset = feature_list_offset + 2 + num_features * 6 + 4; + unsigned substitutions_size = 6 + num_substitutions * 6; + unsigned gsub_size = variations_offset + 8 + num_records * 8 + substitutions_size + 2 * 6; + unsigned char *gsub = (unsigned char *) g_malloc0 (gsub_size); + unsigned char *p = gsub; + unsigned char maxp[6] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x05}; + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob; + +#define PUT16(v) (*p++ = (unsigned char) ((v) >> 8), *p++ = (unsigned char) (v)) +#define PUT32(v) (PUT16 ((v) >> 16), PUT16 (v)) + PUT16 (1); PUT16 (1); /* version */ + PUT16 (script_list_offset); + PUT16 (feature_list_offset); + PUT16 (lookup_list_offset); + PUT32 (variations_offset); + + PUT16 (2); PUT16 (6); PUT16 (6 + 20); /* lookupCount, lookupOffsets */ + for (unsigned i = 0; i < 2; i++) + { + PUT16 (1); PUT16 (0); PUT16 (1); PUT16 (8); /* type, flag, subTableCount, offset */ + PUT16 (1); PUT16 (6); PUT16 (1); /* SingleSubst format, coverage, deltaGlyphID */ + PUT16 (1); PUT16 (1); PUT16 (1 + 2 * i); /* Coverage format, glyphCount, glyph */ + } + + PUT16 (1); PUT32 (HB_TAG ('D','F','L','T')); PUT16 (8); /* scriptCount, record */ + PUT16 (4); PUT16 (0); /* defaultLangSys, langSysCount */ + PUT16 (0); PUT16 (0xFFFF); PUT16 (num_features - 1); /* lookupOrder, required, count */ + for (unsigned i = 0; i < num_features - 1; i++) + PUT16 (i); + + PUT16 (num_features); + for (unsigned i = 0; i < num_features; i++) + { + PUT32 (i ? HB_TAG ('b','b','b','b') : HB_TAG ('a','a','a','a')); + PUT16 (2 + num_features * 6); + } + PUT16 (0); PUT16 (0); /* featureParams, lookupIndexCount */ + + PUT16 (1); PUT16 (0); /* version */ + PUT32 (num_records); + for (unsigned i = 0; i < num_records; i++) + { + PUT32 (0); /* conditionSet */ + PUT32 (8 + num_records * 8); /* featureTableSubstitution */ + } + + PUT16 (1); PUT16 (0); /* version */ + PUT16 (num_substitutions); + PUT16 (0); PUT32 (substitutions_size); + PUT16 (num_features - 1); PUT32 (substitutions_size + 6); + for (unsigned i = 2; i < num_substitutions; i++) + { + PUT16 (0xFFFF); PUT32 (0); + } + for (unsigned i = 0; i < 2; i++) + { + PUT16 (0); PUT16 (1); PUT16 (i); /* featureParams, lookupIndexCount, lookup */ + } + g_assert_true (p == gsub + gsub_size); +#undef PUT32 +#undef PUT16 + + blob = hb_blob_create ((const char *) gsub, gsub_size, + HB_MEMORY_MODE_READONLY, gsub, g_free); + g_assert_true (hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob)); + hb_blob_destroy (blob); + + blob = hb_blob_create ((const char *) maxp, sizeof (maxp), + HB_MEMORY_MODE_DUPLICATE, NULL, NULL); + g_assert_true (hb_face_builder_add_table (face, HB_TAG ('m','a','x','p'), blob)); + hb_blob_destroy (blob); + + return face; +} + +/* Lookups of substitute features get the tag of the feature they replace, in + * one pass over the FeatureVariations table. */ +static void +test_depend_feature_variations (void) +{ + /* 8,000 x 40,000 x 90 steps if every record is searched for every feature. */ + hb_face_t *face = create_feature_variations_face (8000, 40000, 90); + hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face); + hb_tag_t tag; + g_assert_nonnull (depend); + + tag = HB_TAG ('a','a','a','a'); + g_assert_true (find_dependency (depend, 1, 2, HB_OT_TAG_GSUB, &tag, NULL)); + tag = HB_TAG ('b','b','b','b'); + g_assert_false (find_dependency (depend, 1, 2, HB_OT_TAG_GSUB, &tag, NULL)); + /* No script reaches the last feature. */ + g_assert_false (find_dependency (depend, 3, 4, HB_OT_TAG_GSUB, NULL, NULL)); + + hb_subset_depend_destroy (depend); + hb_face_destroy (face); +} + #ifndef HB_NO_BEYOND_64K /* ContextSubst format 6 (beyond-64k) uses 24-bit Coverage offsets. * Lookup 0 matches glyphs 1 2 and calls lookup 1 (SingleSubst 2 -> 3) @@ -813,6 +924,7 @@ main (int argc, char **argv) hb_test_add (test_depend_math); hb_test_add (test_depend_varc); hb_test_add (test_depend_gsub_formats); + hb_test_add (test_depend_feature_variations); #ifndef HB_NO_BEYOND_64K hb_test_add (test_depend_context_format6); hb_test_add (test_depend_gsub_context_set_glyphs); diff --git a/test/api/test-ot-layout.c b/test/api/test-ot-layout.c index b6bec8895..f3f727383 100644 --- a/test/api/test-ot-layout.c +++ b/test/api/test-ot-layout.c @@ -105,6 +105,61 @@ test_ot_layout_gdef_1_4_offset2 (void) hb_face_destroy (face); } + +static void +test_ot_layout_lookups_substitute_closure_context_format4 (void) +{ + /* Lookup 0 is a SequenceContextFormat4 (24-bit glyph ids) with the + * single rule "1 2", applying lookup 1 at position 1. Lookup 1 is a + * SingleSubst 2 -> 3. Closing over {1, 2} must add glyph 3. */ + const char gsub[] = { + 0x00, 0x01, 0x00, 0x00, /* version 1.0 */ + 0x00, 0x0A, /* scriptListOffset */ + 0x00, 0x0C, /* featureListOffset */ + 0x00, 0x0E, /* lookupListOffset */ + 0x00, 0x00, /* ScriptList: no scripts */ + 0x00, 0x00, /* FeatureList: no features */ + 0x00, 0x02, 0x00, 0x06, 0x00, 0x2F, /* LookupList: two lookups */ + 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, /* Lookup 0: type 5, one subtable */ + 0x00, 0x04, /* SequenceContextFormat4 */ + 0x00, 0x00, 0x00, 0x0C, /* coverageOffset */ + 0x00, 0x00, 0x01, /* ruleSetCount */ + 0x00, 0x00, 0x12, /* ruleSetOffsets[0] */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, /* Coverage format 1, glyph 1 */ + 0x00, 0x01, 0x00, 0x04, /* RuleSet: one rule */ + 0x00, 0x02, 0x00, 0x01, /* Rule: inputCount 2, lookupCount 1 */ + 0x00, 0x00, 0x02, /* input[1] = glyph 2 */ + 0x00, 0x01, 0x00, 0x01, /* LookupRecord: sequenceIndex 1, lookup 1 */ + 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, /* Lookup 1: type 1, one subtable */ + 0x00, 0x01, 0x00, 0x06, 0x00, 0x01, /* SingleSubstFormat1, delta +1 */ + 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, /* Coverage format 1, glyph 2 */ + }; + const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x04}; /* version 0.5, 4 glyphs */ + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (gsub, sizeof (gsub), + HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_set_t *lookups = hb_set_create (); + hb_set_t *glyphs = hb_set_create (); + + g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GSUB, blob)); + hb_blob_destroy (blob); + blob = hb_blob_create (maxp, sizeof (maxp), + HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (face, HB_TAG ('m','a','x','p'), blob)); + hb_blob_destroy (blob); + + hb_set_add (lookups, 0); + hb_set_add (glyphs, 1); + hb_set_add (glyphs, 2); + hb_ot_layout_lookups_substitute_closure (face, lookups, glyphs); + + g_assert_true (hb_set_has (glyphs, 3)); + g_assert_cmpuint (hb_set_get_population (glyphs), ==, 3); + + hb_set_destroy (glyphs); + hb_set_destroy (lookups); + hb_face_destroy (face); +} #endif static void @@ -440,6 +495,90 @@ test_ot_layout_collect_lookup_variations (void) } #endif +static char * +write_be16 (char *p, unsigned v) +{ + *p++ = (char) (v >> 8); + *p++ = (char) v; + return p; +} + +static void +test_ot_layout_lookups_substitute_closure_large_coverage_one (unsigned lookup_type) +{ + /* Lookup 0 is a ContextSubst covering NUM_GLYPHS glyphs; every rule recurses + * into lookup 1, a MultipleSubst or AlternateSubst covering the same glyphs + * and mapping each to OUT1 and OUT2. Closure visits lookup 1 once per glyph + * with a single active glyph, so the cost of a visit must not scale with the + * size of lookup 1's coverage. */ + enum { NUM_GLYPHS = 16000, OUT1 = NUM_GLYPHS + 1, OUT2 = NUM_GLYPHS + 2 }; + const unsigned context_len = 6 + 2 * NUM_GLYPHS + 4 + 8 + 10; + const unsigned subst_len = 6 + 2 * NUM_GLYPHS + 6 + 10; + const unsigned gsub_len = 10 + 6 + 16 + context_len + subst_len; + char *gsub = g_malloc0 (gsub_len); + char *p = gsub; + unsigned i; + + p = write_be16 (p, 1); p = write_be16 (p, 0); /* version 1.0 */ + p = write_be16 (p, 0); p = write_be16 (p, 0); /* no ScriptList, no FeatureList */ + p = write_be16 (p, 10); /* LookupList */ + + p = write_be16 (p, 2); /* lookupCount */ + p = write_be16 (p, 6); p = write_be16 (p, 14); /* Lookup offsets */ + p = write_be16 (p, 5); p = write_be16 (p, 0); /* ContextSubst, no flags */ + p = write_be16 (p, 1); p = write_be16 (p, 16); /* one subtable */ + p = write_be16 (p, lookup_type); p = write_be16 (p, 0); + p = write_be16 (p, 1); p = write_be16 (p, 8 + context_len); + + /* ContextSubstFormat1: all rule sets share one rule applying lookup 1 at 0. */ + p = write_be16 (p, 1); p = write_be16 (p, context_len - 10); + p = write_be16 (p, NUM_GLYPHS); + for (i = 0; i < NUM_GLYPHS; i++) + p = write_be16 (p, 6 + 2 * NUM_GLYPHS); + p = write_be16 (p, 1); p = write_be16 (p, 4); /* RuleSet */ + p = write_be16 (p, 1); p = write_be16 (p, 1); /* Rule: glyphCount, lookupCount */ + p = write_be16 (p, 0); p = write_be16 (p, 1); /* SequenceLookupRecord */ + p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */ + p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0); + + /* MultipleSubstFormat1 / AlternateSubstFormat1: all glyphs share one + * Sequence / AlternateSet. */ + p = write_be16 (p, 1); p = write_be16 (p, subst_len - 10); + p = write_be16 (p, NUM_GLYPHS); + for (i = 0; i < NUM_GLYPHS; i++) + p = write_be16 (p, 6 + 2 * NUM_GLYPHS); + p = write_be16 (p, 2); p = write_be16 (p, OUT1); p = write_be16 (p, OUT2); + p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */ + p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0); + + g_assert_cmpuint (p - gsub, ==, gsub_len); + + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (gsub, gsub_len, HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GSUB, blob)); + hb_blob_destroy (blob); + hb_face_set_glyph_count (face, OUT2 + 1); + + hb_set_t *glyphs = hb_set_create (); + hb_set_add_range (glyphs, 1, NUM_GLYPHS); + hb_ot_layout_lookups_substitute_closure (face, NULL, glyphs); + + g_assert_cmpuint (hb_set_get_population (glyphs), ==, NUM_GLYPHS + 2); + g_assert_true (hb_set_has (glyphs, OUT1)); + g_assert_true (hb_set_has (glyphs, OUT2)); + + hb_set_destroy (glyphs); + hb_face_destroy (face); + g_free (gsub); +} + +static void +test_ot_layout_lookups_substitute_closure_large_coverage (void) +{ + test_ot_layout_lookups_substitute_closure_large_coverage_one (2); /* MultipleSubst */ + test_ot_layout_lookups_substitute_closure_large_coverage_one (3); /* AlternateSubst */ +} + int main (int argc, char **argv) { @@ -449,6 +588,7 @@ main (int argc, char **argv) #endif #ifndef HB_NO_BEYOND_64K hb_test_add (test_ot_layout_gdef_1_4_offset2); + hb_test_add (test_ot_layout_lookups_substitute_closure_context_format4); #endif hb_test_add (test_ot_layout_table_get_script_tags); hb_test_add (test_ot_layout_table_find_script); @@ -459,5 +599,6 @@ main (int argc, char **argv) #ifndef HB_NO_VAR hb_test_add (test_ot_layout_collect_lookup_variations); #endif + hb_test_add (test_ot_layout_lookups_substitute_closure_large_coverage); return hb_test_run (); } diff --git a/test/api/test-subset.c b/test/api/test-subset.c index 9527685bf..04af95b8b 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -916,6 +916,272 @@ test_subset_custom_glyph_mapping (void) hb_face_destroy (source); } +static hb_face_t * +_face_copy_tables (hb_face_t *source) +{ + hb_face_t *face = hb_face_builder_create (); + + hb_tag_t tags[32]; + unsigned offset = 0; + while (true) + { + unsigned count = G_N_ELEMENTS (tags); + hb_face_get_table_tags (source, offset, &count, tags); + if (!count) break; + + for (unsigned i = 0; i < count; i++) + { + hb_blob_t *blob = hb_face_reference_table (source, tags[i]); + hb_face_builder_add_table (face, tags[i], blob); + hb_blob_destroy (blob); + } + offset += count; + } + + return face; +} + +static hb_face_t * +_face_with_table (hb_face_t *source, hb_tag_t tag, const void *data, unsigned length) +{ + hb_face_t *face = _face_copy_tables (source); + hb_blob_t *blob = hb_blob_create_or_fail ((const char *) data, length, + HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (face, tag, blob); + hb_blob_destroy (blob); + + return face; +} + +static hb_face_t * +_face_with_name_table_and_wws (hb_face_t *source, + const void *name_data, unsigned name_length, + bool wws) +{ + hb_face_t *face = _face_copy_tables (source); + + hb_blob_t *name_blob = hb_blob_create_or_fail ((const char *) name_data, + name_length, + HB_MEMORY_MODE_READONLY, + NULL, NULL); + g_assert_nonnull (name_blob); + hb_face_builder_add_table (face, HB_TAG ('n','a','m','e'), name_blob); + hb_blob_destroy (name_blob); + + hb_blob_t *os2 = hb_face_reference_table (source, HB_TAG ('O','S','/','2')); + hb_blob_t *os2_copy = hb_blob_copy_writable_or_fail (os2); + hb_blob_destroy (os2); + g_assert_nonnull (os2_copy); + + unsigned length; + char *data = (char *) hb_blob_get_data_writable (os2_copy, &length); + g_assert_cmpuint (length, >=, 64); + /* fsSelection is at offset 62 in OS/2 version 0 and later. */ + uint16_t fs_selection = ((uint8_t) data[62] << 8) | (uint8_t) data[63]; + if (wws) + fs_selection |= 1u << 8; + else + fs_selection &= ~(1u << 8); + data[62] = fs_selection >> 8; + data[63] = fs_selection; + + hb_face_builder_add_table (face, HB_TAG ('O','S','/','2'), os2_copy); + hb_blob_destroy (os2_copy); + + return face; +} + +static hb_blob_t * +_blob_with_checksum (uint32_t checksum, unsigned length) +{ + char *data = (char *) g_malloc0 (length ? length : 1); + if (length >= 4) + { + data[0] = checksum >> 24; + data[1] = checksum >> 16; + data[2] = checksum >> 8; + data[3] = checksum; + } + return hb_blob_create_or_fail (data, length, HB_MEMORY_MODE_WRITABLE, + data, g_free); +} + +static void +test_subset_no_hinting_ignored_for_hint_dependent_font (void) +{ +#ifndef HB_NO_NAME + /* Minimal name table with Font Family = "MingLiU". */ + static const unsigned char name_table[] = { + 0x00, 0x00, 0x00, 0x01, 0x00, 0x12, + 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, 0x00, 0x01, 0x00, 0x0E, 0x00, 0x00, + 0x00, 0x4D, 0x00, 0x69, 0x00, 0x6E, 0x00, 0x67, + 0x00, 0x4C, 0x00, 0x69, 0x00, 0x55, + }; + /* "HuaTianKaiTi" followed by U+4F53. FreeType reads the non-ASCII + * character as `?`, matching the list entry "HuaTianKaiTi?". */ + static const unsigned char name_table_question[] = { + 0x00, 0x00, 0x00, 0x01, 0x00, 0x12, + 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, 0x00, 0x01, 0x00, 0x1A, 0x00, 0x00, + 0x00, 0x48, 0x00, 0x75, 0x00, 0x61, 0x00, 0x54, + 0x00, 0x69, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x4B, + 0x00, 0x61, 0x00, 0x69, 0x00, 0x54, 0x00, 0x69, + 0x4F, 0x53, + }; + static const hb_tag_t hint_tags[] = { + HB_TAG ('c','v','t',' '), + HB_TAG ('f','p','g','m'), + HB_TAG ('p','r','e','p'), + }; + + hb_face_t *source = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); + hb_face_t *face = _face_with_table (source, HB_TAG ('n','a','m','e'), + name_table, sizeof (name_table)); + hb_face_t *face_question = _face_with_table (source, HB_TAG ('n','a','m','e'), + name_table_question, + sizeof (name_table_question)); + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 'a'); + + for (unsigned i = 0; i < 3; i++) + { + bool hint_dependent = i != 0; + hb_face_t *target = i == 0 ? source : i == 1 ? face : face_question; + + hb_subset_input_t *input = hb_subset_test_create_input (codepoints); + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING); + hb_face_t *subset = hb_subset_or_fail (target, input); + g_assert_nonnull (subset); + + for (unsigned j = 0; j < G_N_ELEMENTS (hint_tags); j++) + { + hb_blob_t *blob = hb_face_reference_table (subset, hint_tags[j]); + g_assert_true (!!hb_blob_get_length (blob) == hint_dependent); + hb_blob_destroy (blob); + } + + hb_face_destroy (subset); + hb_subset_input_destroy (input); + } + + hb_set_destroy (codepoints); + hb_face_destroy (face_question); + hb_face_destroy (face); + hb_face_destroy (source); +#endif +} + +static void +test_subset_no_hinting_hint_dependent_wws_family (void) +{ +#ifndef HB_NO_NAME + /* Font Family = "NotTricky", Typographic Family = "MingLiU", and + * WWS Family = "NotTricky". With the OS/2 WWS bit set FreeType + * picks the typographic family and keeps hints; with it clear it + * picks the WWS family and must not fall through to the typographic + * one. */ + static const unsigned char name_table[] = { + 0x00, 0x00, 0x00, 0x03, 0x00, 0x2A, + 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, + 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x12, + 0x00, 0x03, 0x00, 0x01, 0x04, 0x09, 0x00, 0x15, 0x00, 0x12, 0x00, 0x20, + 0x00, 0x4E, 0x00, 0x6F, 0x00, 0x74, 0x00, 0x54, 0x00, 0x72, 0x00, 0x69, + 0x00, 0x63, 0x00, 0x6B, 0x00, 0x79, 0x00, 0x4D, 0x00, 0x69, 0x00, 0x6E, + 0x00, 0x67, 0x00, 0x4C, 0x00, 0x69, 0x00, 0x55, 0x00, 0x4E, 0x00, 0x6F, + 0x00, 0x74, 0x00, 0x54, 0x00, 0x72, 0x00, 0x69, 0x00, 0x63, 0x00, 0x6B, + 0x00, 0x79, + }; + static const hb_tag_t hint_tags[] = { + HB_TAG ('c','v','t',' '), + HB_TAG ('f','p','g','m'), + HB_TAG ('p','r','e','p'), + }; + + hb_face_t *source = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); + hb_face_t *wws = _face_with_name_table_and_wws (source, name_table, + sizeof (name_table), true); + hb_face_t *no_wws = _face_with_name_table_and_wws (source, name_table, + sizeof (name_table), false); + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 'a'); + + for (unsigned i = 0; i < 2; i++) + { + bool hint_dependent = i == 0; + hb_face_t *target = i == 0 ? wws : no_wws; + + hb_subset_input_t *input = hb_subset_test_create_input (codepoints); + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING); + hb_face_t *subset = hb_subset_or_fail (target, input); + g_assert_nonnull (subset); + + for (unsigned j = 0; j < G_N_ELEMENTS (hint_tags); j++) + { + hb_blob_t *blob = hb_face_reference_table (subset, hint_tags[j]); + g_assert_true (!!hb_blob_get_length (blob) == hint_dependent); + hb_blob_destroy (blob); + } + + hb_face_destroy (subset); + hb_subset_input_destroy (input); + } + + hb_set_destroy (codepoints); + hb_face_destroy (no_wws); + hb_face_destroy (wws); + hb_face_destroy (source); +#endif +} + +static void +test_subset_no_hinting_ignored_for_hint_dependent_ids (void) +{ + static const struct { + hb_tag_t tag; + uint32_t checksum; + unsigned length; + } ids[] = { + {HB_TAG ('c','v','t',' '), 0x05BCF058, 0x000002E4}, + {HB_TAG ('f','p','g','m'), 0x28233BF1, 0x000087C4}, + {HB_TAG ('p','r','e','p'), 0xA344A1EA, 0x000001E1}, + }; + static const hb_tag_t hint_tags[] = { + HB_TAG ('c','v','t',' '), + HB_TAG ('f','p','g','m'), + HB_TAG ('p','r','e','p'), + }; + + hb_face_t *source = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); + hb_face_t *face = _face_copy_tables (source); + for (unsigned i = 0; i < G_N_ELEMENTS (ids); i++) + { + hb_blob_t *blob = _blob_with_checksum (ids[i].checksum, ids[i].length); + hb_face_builder_add_table (face, ids[i].tag, blob); + hb_blob_destroy (blob); + } + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 'a'); + hb_subset_input_t *input = hb_subset_test_create_input (codepoints); + hb_set_destroy (codepoints); + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING); + + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + for (unsigned i = 0; i < G_N_ELEMENTS (hint_tags); i++) + { + hb_blob_t *blob = hb_face_reference_table (subset, hint_tags[i]); + g_assert_cmpuint (hb_blob_get_length (blob), >, 0); + hb_blob_destroy (blob); + } + + hb_face_destroy (subset); + hb_subset_input_destroy (input); + hb_face_destroy (face); + hb_face_destroy (source); +} + static hb_blob_t* _copy_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) { @@ -1554,6 +1820,9 @@ main (int argc, char **argv) hb_test_add (test_subset_non_glyph_layout_table_does_not_retain_gids); #endif hb_test_add (test_subset_custom_glyph_mapping); + hb_test_add (test_subset_no_hinting_ignored_for_hint_dependent_font); + hb_test_add (test_subset_no_hinting_hint_dependent_wws_family); + hb_test_add (test_subset_no_hinting_ignored_for_hint_dependent_ids); hb_test_add (test_subset_sanitized_table_cache); #if !defined(HB_NO_OT_LAYOUT) && !defined(HB_NO_OT_LAYOUT_BLOCKLIST) hb_test_add (test_subset_gdef_blocklist); diff --git a/test/fuzzing/fonts/gdef-mark-glyph-sets-aliased-all-glyphs.ttf b/test/fuzzing/fonts/gdef-mark-glyph-sets-aliased-all-glyphs.ttf new file mode 100644 index 000000000..61d7f4be7 Binary files /dev/null and b/test/fuzzing/fonts/gdef-mark-glyph-sets-aliased-all-glyphs.ttf differ diff --git a/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf b/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf new file mode 100644 index 000000000..6e48fd930 Binary files /dev/null and b/test/fuzzing/fonts/gpos-context2-classdef-rescan-timeout.ttf differ diff --git a/test/fuzzing/fonts/gpos-pairpos1-coverage-repeated-index-timeout.ttf b/test/fuzzing/fonts/gpos-pairpos1-coverage-repeated-index-timeout.ttf new file mode 100644 index 000000000..f48901da8 Binary files /dev/null and b/test/fuzzing/fonts/gpos-pairpos1-coverage-repeated-index-timeout.ttf differ diff --git a/test/fuzzing/fonts/gpos-pairpos2-class-outside-matrix-timeout.ttf b/test/fuzzing/fonts/gpos-pairpos2-class-outside-matrix-timeout.ttf new file mode 100644 index 000000000..a2d1b68cb Binary files /dev/null and b/test/fuzzing/fonts/gpos-pairpos2-class-outside-matrix-timeout.ttf differ diff --git a/test/shape/data/in-house/tests/indic-syllable.tests b/test/shape/data/in-house/tests/indic-syllable.tests index 84526d9cd..62c03d9a1 100644 --- a/test/shape/data/in-house/tests/indic-syllable.tests +++ b/test/shape/data/in-house/tests/indic-syllable.tests @@ -13,3 +13,4 @@ ../fonts/e2b17207c4b7ad78d843e1b0c4d00b09398a1137.ttf;;U+0BAA,U+0BAA,U+0BCD;[pa-tamil=0+778|pa-tamil.001=1+778|pulli-tamil=1@-385,0+0] ../fonts/41071178fbce4956d151f50967af458dbf555f7b.ttf;;U+0926,U+093F,U+0938,U+0902,U+092C,U+0930;[isigndeva=0+266|dadeva=0+541|sadeva=2+709|anusvaradeva=2@0,-1+0|badeva=4+537|radeva=5+436] ../fonts/65d1b9099cfb3191931d8d6112d7a03d979d579f.ttf;;U+00B2,U+0B95;[uni00B2=0+500|uni0B95=1+500] +../fonts/8116e5d8fedfbec74e45dc350d2416d810bed8c4.ttf;--no-positions;U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+094D,U+091F,U+093F;[uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni091F094D091F=0|uni094D=0|uni093F.630=0|uni091F094D091F=0]