diff --git a/perf/hb-draw-compare b/perf/hb-draw-compare index 61c4b0a5d..726cb74a5 100755 --- a/perf/hb-draw-compare +++ b/perf/hb-draw-compare @@ -18,7 +18,7 @@ variations= face_loader=ot shaper=ot font_funcs1=ot -font_funcs2=coretext +font_funcs2=fontations clear1=false clear2=false unicodes= diff --git a/src/OT/Layout/GPOS/AnchorFormat3.hh b/src/OT/Layout/GPOS/AnchorFormat3.hh index b683a0a0c..e08c380a0 100644 --- a/src/OT/Layout/GPOS/AnchorFormat3.hh +++ b/src/OT/Layout/GPOS/AnchorFormat3.hh @@ -94,7 +94,8 @@ struct AnchorFormat3 /* in case that all axes are pinned or no variations after instantiation, * both var_idxes will be mapped to HB_OT_LAYOUT_NO_VARIATIONS_INDEX */ if (x_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX && - y_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) + y_varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX && + c->plan->normalized_coords) return_trace (c->serializer->check_assign (out->format, 1, HB_SERIALIZE_ERROR_INT_OVERFLOW)); if (!c->serializer->embed (xDeviceTable)) return_trace (false); diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 17ae38766..928c74a16 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -11,11 +11,11 @@ namespace GSUB_impl { template struct LigatureSet { - protected: + public: Array16OfOffset16To> ligature; /* Array LigatureSet tables * ordered by preference */ - public: + DEFINE_SIZE_ARRAY (2, ligature); bool sanitize (hb_sanitize_context_t *c) const diff --git a/src/graph/classdef-graph.hh b/src/graph/classdef-graph.hh index da6378820..d6c174e87 100644 --- a/src/graph/classdef-graph.hh +++ b/src/graph/classdef-graph.hh @@ -74,7 +74,7 @@ struct ClassDef : public OT::ClassDef class_def_link->width = SmallTypes::size; class_def_link->objidx = class_def_prime_id; class_def_link->position = link_position; - class_def_prime_vertex.add_parent (parent_id); + class_def_prime_vertex.add_parent (parent_id, false); return true; } diff --git a/src/graph/coverage-graph.hh b/src/graph/coverage-graph.hh index 61ca063e3..2314020bf 100644 --- a/src/graph/coverage-graph.hh +++ b/src/graph/coverage-graph.hh @@ -98,11 +98,33 @@ struct Coverage : public OT::Layout::Common::Coverage coverage_link->width = SmallTypes::size; coverage_link->objidx = coverage_prime_id; coverage_link->position = link_position; - coverage_prime_vertex.add_parent (parent_id); + coverage_prime_vertex.add_parent (parent_id, false); return (Coverage*) coverage_prime_vertex.obj.head; } + // Filter an existing coverage table to glyphs at indices [start, end) and replace it with the filtered version. + static bool filter_coverage (gsubgpos_graph_context_t& c, + unsigned existing_coverage, + unsigned start, unsigned end) { + unsigned coverage_size = c.graph.vertices_[existing_coverage].table_size (); + auto& coverage_v = c.graph.vertices_[existing_coverage]; + Coverage* coverage_table = (Coverage*) coverage_v.obj.head; + if (!coverage_table || !coverage_table->sanitize (coverage_v)) + return false; + + auto new_coverage = + + hb_zip (coverage_table->iter (), hb_range ()) + | hb_filter ([&] (hb_pair_t p) { + return p.second >= start && p.second < end; + }) + | hb_map_retains_sorting (hb_first) + ; + + return make_coverage (c, new_coverage, existing_coverage, coverage_size * 2 + 100); + } + + // Replace the coverage table at dest obj with one covering 'glyphs'. template static bool make_coverage (gsubgpos_graph_context_t& c, It glyphs, diff --git a/src/graph/graph.hh b/src/graph/graph.hh index b24507ece..332f27169 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -50,6 +50,7 @@ struct graph_t private: unsigned incoming_edges_ = 0; unsigned single_parent = (unsigned) -1; + bool has_incoming_virtual_edges_ = false; hb_hashmap_t parents; public: @@ -66,6 +67,11 @@ struct graph_t return parents.in_error (); } + bool has_incoming_virtual_edges () const + { + return has_incoming_virtual_edges_; + } + bool link_positions_valid (unsigned num_objects, bool removed_nil) { hb_set_t assigned_bytes; @@ -121,7 +127,9 @@ struct graph_t } } - bool equals (const vertex_t& other, + bool equals (unsigned this_index, + unsigned other_index, + const vertex_t& other, const graph_t& graph, const graph_t& other_graph, unsigned depth) const @@ -129,8 +137,10 @@ struct graph_t if (!(as_bytes () == other.as_bytes ())) { DEBUG_MSG (SUBSET_REPACK, nullptr, - "vertex [%lu] bytes != [%lu] bytes, depth = %u", + "vertex %u [%lu bytes] != %u [%lu bytes], depth = %u", + this_index, (unsigned long) table_size (), + other_index, (unsigned long) other.table_size (), depth); @@ -162,6 +172,7 @@ struct graph_t hb_swap (a.single_parent, b.single_parent); hb_swap (a.parents, b.parents); hb_swap (a.incoming_edges_, b.incoming_edges_); + hb_swap (a.has_incoming_virtual_edges_, b.has_incoming_virtual_edges_); hb_swap (a.start, b.start); hb_swap (a.end, b.end); hb_swap (a.priority, b.priority); @@ -207,13 +218,16 @@ struct graph_t void reset_parents () { incoming_edges_ = 0; + has_incoming_virtual_edges_ = false; single_parent = (unsigned) -1; parents.reset (); } - void add_parent (unsigned parent_index) + void add_parent (unsigned parent_index, bool is_virtual) { assert (parent_index != (unsigned) -1); + has_incoming_virtual_edges_ |= is_virtual; + if (incoming_edges_ == 0) { single_parent = parent_index; @@ -408,7 +422,7 @@ struct graph_t link_a.bias != link_b.bias) return false; - if (!graph.vertices_[link_a.objidx].equals ( + if (!graph.vertices_[link_a.objidx].equals (link_a.objidx, link_b.objidx, other_graph.vertices_[link_b.objidx], graph, other_graph, depth + 1)) return false; @@ -490,7 +504,7 @@ struct graph_t bool operator== (const graph_t& other) const { - return root ().equals (other.root (), *this, other, 0); + return root ().equals (root_idx(), other.root_idx(), other.root (), *this, other, 0); } void print () const { @@ -501,6 +515,9 @@ struct graph_t for (const auto &l : v.obj.real_links) { printf("%u, ", l.objidx); } + for (const auto &l : v.obj.virtual_links) { + printf("v%u, ", l.objidx); + } printf("]\n"); } } @@ -556,7 +573,7 @@ struct graph_t link->width = 2; link->objidx = child_id; link->position = (char*) offset - (char*) v.obj.head; - vertices_[child_id].add_parent (parent_id); + vertices_[child_id].add_parent (parent_id, false); } /* @@ -943,9 +960,11 @@ struct graph_t /* * Moves the child of old_parent_idx pointed to by old_offset to a new * vertex at the new_offset. + * + * Returns the id of the child node that was moved. */ template - void move_child (unsigned old_parent_idx, + unsigned move_child (unsigned old_parent_idx, const O* old_offset, unsigned new_parent_idx, const O* new_offset) @@ -965,10 +984,12 @@ struct graph_t new_link->position = (const char*) new_offset - (const char*) new_v.obj.head; auto& child = vertices_[child_id]; - child.add_parent (new_parent_idx); + child.add_parent (new_parent_idx, false); old_v.remove_real_link (child_id, old_offset); child.remove_parent (old_parent_idx); + + return child_id; } /* @@ -1015,12 +1036,12 @@ struct graph_t for (const auto& l : child.obj.real_links) { clone->obj.real_links.push (l); - vertices_[l.objidx].add_parent (clone_idx); + vertices_[l.objidx].add_parent (clone_idx, false); } for (const auto& l : child.obj.virtual_links) { clone->obj.virtual_links.push (l); - vertices_[l.objidx].add_parent (clone_idx); + vertices_[l.objidx].add_parent (clone_idx, true); } check_success (!clone->obj.real_links.in_error ()); @@ -1073,10 +1094,15 @@ struct graph_t const auto& child = vertices_[child_idx]; unsigned links_to_child = child.incoming_edges_from_parent(parent_idx); - if (child.incoming_edges () <= links_to_child) + if (child.incoming_edges () <= links_to_child || child.has_incoming_virtual_edges()) { // Can't duplicate this node, doing so would orphan the original one as all remaining links // to child are from parent. + // + // We don't allow duplication of nodes with incoming virtual edges because we don't track + // the number of virtual vs real incoming edges. As a result we can't tell if a node + // with virtual edges may end up orphaned by duplication (ie. where one copy is only pointed + // to by virtual edges). DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %u => %u", parent_idx, child_idx); return -1; @@ -1091,12 +1117,15 @@ struct graph_t if (parent_idx == clone_idx) parent_idx++; auto& parent = vertices_[parent_idx]; + unsigned count = 0; + unsigned num_real = parent.obj.real_links.length; for (auto& l : parent.obj.all_links_writer ()) { + count++; if (l.objidx != child_idx) continue; - reassign_link (l, parent_idx, clone_idx); + reassign_link (l, parent_idx, clone_idx, count > num_real); } return clone_idx; @@ -1129,10 +1158,15 @@ struct graph_t links_to_child += child.incoming_edges_from_parent(parent_idx); } - if (child.incoming_edges () <= links_to_child) + if (child.incoming_edges () <= links_to_child || child.has_incoming_virtual_edges()) { // Can't duplicate this node, doing so would orphan the original one as all remaining links // to child are from parent. + // + // We don't allow duplication of nodes with incoming virtual edges because we don't track + // the number of virtual vs real incoming edges. As a result we can't tell if a node + // with virtual edges may end up orphaned by duplication (ie. where one copy is only pointed + // to by virtual edges). DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %u, ..., %u => %u", first_parent, last_parent, child_idx); return -1; } @@ -1146,12 +1180,15 @@ struct graph_t // duplicate shifts the root node idx, so if parent_idx was root update it. if (parent_idx == clone_idx) parent_idx++; auto& parent = vertices_[parent_idx]; + unsigned count = 0; + unsigned num_real = parent.obj.real_links.length; for (auto& l : parent.obj.all_links_writer ()) { + count++; if (l.objidx != child_idx) continue; - reassign_link (l, parent_idx, clone_idx); + reassign_link (l, parent_idx, clone_idx, count > num_real); } } @@ -1279,6 +1316,7 @@ struct graph_t if (!DEBUG_ENABLED(SUBSET_REPACK)) return; DEBUG_MSG (SUBSET_REPACK, nullptr, "Graph is not fully connected."); + parents_invalid = true; update_parents(); @@ -1398,8 +1436,11 @@ struct graph_t for (unsigned p = 0; p < count; p++) { - for (auto& l : vertices_.arrayZ[p].obj.all_links ()) - vertices_[l.objidx].add_parent (p); + for (auto& l : vertices_.arrayZ[p].obj.real_links) + vertices_[l.objidx].add_parent (p, false); + + for (auto& l : vertices_.arrayZ[p].obj.virtual_links) + vertices_[l.objidx].add_parent (p, true); } for (unsigned i = 0; i < count; i++) @@ -1502,12 +1543,13 @@ struct graph_t */ void reassign_link (hb_serialize_context_t::object_t::link_t& link, unsigned parent_idx, - unsigned new_idx) + unsigned new_idx, + bool is_virtual) { unsigned old_idx = link.objidx; link.objidx = new_idx; vertices_[old_idx].remove_parent (parent_idx); - vertices_[new_idx].add_parent (parent_idx); + vertices_[new_idx].add_parent (parent_idx, is_virtual); } /* @@ -1521,13 +1563,16 @@ struct graph_t if (!id_map) return; for (unsigned i : subgraph) { + unsigned num_real = vertices_[i].obj.real_links.length; + unsigned count = 0; for (auto& link : vertices_[i].obj.all_links_writer ()) { + count++; const uint32_t *v; if (!id_map.has (link.objidx, &v)) continue; if (only_wide && !(link.width == 4 && !link.is_signed)) continue; - reassign_link (link, i, *v); + reassign_link (link, i, *v, count > num_real); } } } diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh index 0f6d5662e..77ba4ee18 100644 --- a/src/graph/gsubgpos-graph.hh +++ b/src/graph/gsubgpos-graph.hh @@ -27,9 +27,11 @@ #include "graph.hh" #include "../hb-ot-layout-gsubgpos.hh" #include "../OT/Layout/GSUB/ExtensionSubst.hh" +#include "../OT/Layout/GSUB/SubstLookupSubTable.hh" #include "gsubgpos-context.hh" #include "pairpos-graph.hh" #include "markbasepos-graph.hh" +#include "ligature-graph.hh" #ifndef GRAPH_GSUBGPOS_GRAPH_HH #define GRAPH_GSUBGPOS_GRAPH_HH @@ -120,12 +122,10 @@ struct Lookup : public OT::Lookup unsigned type = lookupType; bool is_ext = is_extension (c.table_tag); - if (c.table_tag != HB_OT_TAG_GPOS) + if (c.table_tag != HB_OT_TAG_GPOS && c.table_tag != HB_OT_TAG_GSUB) return true; - if (!is_ext && - type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair && - type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase) + if (!is_ext && !is_supported_gpos_type(type, c) && !is_supported_gsub_type(type, c)) return true; hb_vector_t>> all_new_subtables; @@ -144,21 +144,32 @@ struct Lookup : public OT::Lookup subtable_index = extension->get_subtable_index (c.graph, ext_subtable_index); type = extension->get_lookup_type (); - if (type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair - && type != OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase) + if (!is_supported_gpos_type(type, c) && !is_supported_gsub_type(type, c)) continue; } hb_vector_t new_sub_tables; - switch (type) - { - case 2: - new_sub_tables = split_subtable (c, parent_index, subtable_index); break; - case 4: - new_sub_tables = split_subtable (c, parent_index, subtable_index); break; - default: - break; + + if (c.table_tag == HB_OT_TAG_GPOS) { + switch (type) + { + case 2: + new_sub_tables = split_subtable (c, parent_index, subtable_index); break; + case 4: + new_sub_tables = split_subtable (c, parent_index, subtable_index); break; + default: + break; + } + } else if (c.table_tag == HB_OT_TAG_GSUB) { + switch (type) + { + case 4: + new_sub_tables = split_subtable (c, parent_index, subtable_index); break; + default: + break; + } } + if (new_sub_tables.in_error ()) return false; if (!new_sub_tables) continue; hb_pair_t>* entry = all_new_subtables.push (); @@ -191,14 +202,14 @@ struct Lookup : public OT::Lookup hb_vector_t>>& subtable_ids) { bool is_ext = is_extension (c.table_tag); - auto& v = c.graph.vertices_[this_index]; + auto* v = &c.graph.vertices_[this_index]; fix_existing_subtable_links (c, this_index, subtable_ids); unsigned new_subtable_count = 0; for (const auto& p : subtable_ids) new_subtable_count += p.second.length; - size_t new_size = v.table_size () + size_t new_size = v->table_size () + new_subtable_count * OT::Offset16::static_size; char* buffer = (char*) hb_calloc (1, new_size); if (!buffer) return false; @@ -207,10 +218,10 @@ struct Lookup : public OT::Lookup hb_free (buffer); return false; } - hb_memcpy (buffer, v.obj.head, v.table_size()); + hb_memcpy (buffer, v->obj.head, v->table_size()); - v.obj.head = buffer; - v.obj.tail = buffer + new_size; + v->obj.head = buffer; + v->obj.tail = buffer + new_size; Lookup* new_lookup = (Lookup*) buffer; @@ -226,21 +237,23 @@ struct Lookup : public OT::Lookup if (is_ext) { unsigned ext_id = create_extension_subtable (c, subtable_id, type); - c.graph.vertices_[subtable_id].add_parent (ext_id); + c.graph.vertices_[subtable_id].add_parent (ext_id, false); subtable_id = ext_id; + // the reference to v may have changed on adding a node, so reassign it. + v = &c.graph.vertices_[this_index]; } - auto* link = v.obj.real_links.push (); + auto* link = v->obj.real_links.push (); link->width = 2; link->objidx = subtable_id; link->position = (char*) &new_lookup->subTable[offset_index++] - (char*) new_lookup; - c.graph.vertices_[subtable_id].add_parent (this_index); + c.graph.vertices_[subtable_id].add_parent (this_index, false); } } // Repacker sort order depends on link order, which we've messed up so resort it. - v.obj.real_links.qsort (); + v->obj.real_links.qsort (); // The head location of the lookup has changed, invalidating the lookups map entry // in the context. Update the map. @@ -326,7 +339,7 @@ struct Lookup : public OT::Lookup // Make extension point at the subtable. auto& ext_vertex = c.graph.vertices_[ext_index]; - ext_vertex.add_parent (lookup_index); + ext_vertex.add_parent (lookup_index, false); if (!existing_ext_index) subtable_vertex.remap_parent (lookup_index, ext_index); @@ -334,6 +347,19 @@ struct Lookup : public OT::Lookup } private: + bool is_supported_gsub_type(unsigned type, gsubgpos_graph_context_t& c) const { + return (c.table_tag == HB_OT_TAG_GSUB) && ( + type == OT::Layout::GSUB_impl::SubstLookupSubTable::Type::Ligature + ); + } + + bool is_supported_gpos_type(unsigned type, gsubgpos_graph_context_t& c) const { + return (c.table_tag == HB_OT_TAG_GPOS) && ( + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::Pair || + type == OT::Layout::GPOS_impl::PosLookupSubTable::Type::MarkBase + ); + } + unsigned extension_type (hb_tag_t table_tag) const { switch (table_tag) diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh new file mode 100644 index 000000000..611ab686e --- /dev/null +++ b/src/graph/ligature-graph.hh @@ -0,0 +1,480 @@ +/* + * Copyright © 2025 Google, Inc. + * + * 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. + * + * Google Author(s): Garret Rieger + */ + +#ifndef GRAPH_LIGATURE_GRAPH_HH +#define GRAPH_LIGATURE_GRAPH_HH + +#include "graph.hh" +#include "../OT/Layout/GSUB/LigatureSubst.hh" +#include "../OT/Layout/GSUB/LigatureSubstFormat1.hh" +#include "../OT/Layout/GSUB/LigatureSet.hh" +#include "../OT/Layout/types.hh" +#include +#include + +namespace graph { + +struct LigatureSet : public OT::Layout::GSUB_impl::LigatureSet +{ + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + if (vertex_len < OT::Layout::GSUB_impl::LigatureSet::min_size) return false; + hb_barrier (); + + int64_t total_len = ligature.get_size() + OT::Layout::GSUB_impl::LigatureSet::min_size - ligature.len.get_size(); + if (vertex_len < total_len) { + return false; + } + return true; + } +}; + +struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1_2 +{ + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + unsigned min_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size; + if (vertex_len < min_size) return false; + hb_barrier (); + + return vertex_len >= + min_size + ligatureSet.get_size() - ligatureSet.len.get_size(); + } + + hb_vector_t split_subtables (gsubgpos_graph_context_t& c, + unsigned parent_index, + unsigned this_index) + { + auto split_points = compute_split_points(c, parent_index, this_index); + split_context_t split_context { + c, + this, + c.graph.duplicate_if_shared (parent_index, this_index), + total_number_ligas(c, this_index), + liga_counts(c, this_index), + }; + return actuate_subtable_split (split_context, split_points); + } + + private: + unsigned total_number_ligas(gsubgpos_graph_context_t& c, unsigned this_index) const { + unsigned total = 0; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return 0; + } + total += liga_set.table->ligature.len; + } + return total; + } + + hb_vector_t liga_counts(gsubgpos_graph_context_t& c, unsigned this_index) const { + hb_vector_t result; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + result.push(!liga_set.table ? 0 : liga_set.table->ligature.len); + } + return result; + } + + hb_vector_t compute_split_points(gsubgpos_graph_context_t& c, + unsigned parent_index, + unsigned this_index) const + { + // For ligature subst coverage is always packed last, and as a result is where an overflow + // will happen if there is one, so we can check the estimate length of the + // LigatureSubstFormat1 -> Coverage offset length which is the sum of all data in the + // retained sub graph except for the coverage table itself. + const unsigned base_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size; + unsigned accumulated = base_size; + + unsigned ligature_index = 0; + hb_vector_t split_points; + for (unsigned i = 0; i < ligatureSet.len; i++) + { + accumulated += OT::HBUINT16::static_size; // for ligature set offset + accumulated += OT::Layout::GSUB_impl::LigatureSet::min_size; // for ligature set table + + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return hb_vector_t {}; + } + + for (unsigned j = 0; j < liga_set.table->ligature.len; j++) + { + const unsigned liga_id = c.graph.index_for_offset (liga_set.index, &liga_set.table->ligature[j]); + const unsigned liga_size = c.graph.vertices_[liga_id].table_size (); + + accumulated += OT::HBUINT16::static_size; // for ligature offset + accumulated += liga_size; // for the ligature table + + if (accumulated >= (1 << 16)) + { + split_points.push(ligature_index); + // We're going to split such that the current ligature will be in the new sub table. + // That means we'll have one ligature subst (base_base), one ligature set, and one liga table + accumulated = base_size + // for liga subst subtable + (OT::HBUINT16::static_size * 2) + // for liga set and liga offset + OT::Layout::GSUB_impl::LigatureSet::min_size + // for liga set subtable + liga_size; // for liga sub table + } + + ligature_index++; + } + } + + return split_points; + } + + + struct split_context_t + { + gsubgpos_graph_context_t& c; + LigatureSubstFormat1* thiz; + unsigned this_index; + unsigned original_count_; + hb_vector_t liga_counts; + + unsigned original_count () + { + return original_count_; + } + + unsigned clone_range (unsigned start, unsigned end) + { + return thiz->clone_range (c, this_index, liga_counts, start, end); + } + + bool shrink (unsigned count) + { + return thiz->shrink (c, this_index, original_count(), liga_counts, count); + } + }; + + hb_pair_t new_liga_set(gsubgpos_graph_context_t& c, unsigned count) const { + unsigned prime_size = OT::Layout::GSUB_impl::LigatureSet::min_size + + count * SmallTypes::size; + + unsigned prime_id = c.create_node (prime_size); + if (prime_id == (unsigned) -1) return hb_pair(-1, nullptr); + + LigatureSet* prime = (LigatureSet*) c.graph.object (prime_id).head; + prime->ligature.len = count; + return hb_pair(prime_id, prime); + } + + void clear_virtual_links (gsubgpos_graph_context_t& c, unsigned node_index) const + { + auto& obj = c.graph.vertices_[node_index].obj; + for (const auto& l : obj.virtual_links) + { + auto& child = c.graph.vertices_[l.objidx]; + child.remove_parent(node_index); + } + obj.virtual_links.clear(); + } + + void add_virtual_link(gsubgpos_graph_context_t& c, unsigned from, unsigned to) const { + auto& from_obj = c.graph.vertices_[from].obj; + c.graph.vertices_[to].add_parent(from, true); + auto& link = *from_obj.virtual_links.push (); + link.objidx = to; + } + + hb_pair_t current_liga_set_bounds (gsubgpos_graph_context_t& c, + unsigned liga_set_index, + const hb_serialize_context_t::object_t& liga_set) const + { + // Finds the actual liga indices present in the liga set currently. Takes + // into account those that have been removed by processing. + unsigned min_index = (unsigned) -1; + unsigned max_index = 0; + for (const auto& l : liga_set.real_links) { + if (l.position < 2) continue; + + unsigned liga_index = (l.position - 2) / 2; + min_index = hb_min(min_index, liga_index); + max_index = hb_max(max_index, liga_index); + } + return hb_pair(min_index, max_index + 1); + } + + void compact_liga_set (gsubgpos_graph_context_t& c, LigatureSet* table, hb_serialize_context_t::object_t& obj) const + { + if (table->ligature.len <= obj.real_links.length) return; + + // compact the remaining linked liga offsets into a continous array and shrink the node as needed. + unsigned to_remove = table->ligature.len - obj.real_links.length; + unsigned new_position = SmallTypes::size; + obj.real_links.qsort(); // for this to work we need to process links in order of position. + for (auto& l : obj.real_links) + { + l.position = new_position; + new_position += SmallTypes::size; + } + + table->ligature.len = obj.real_links.length; + obj.tail -= to_remove * SmallTypes::size; + } + + unsigned clone_range (gsubgpos_graph_context_t& c, + unsigned this_index, + hb_vector_t liga_counts, + unsigned start, unsigned end) const + { + DEBUG_MSG (SUBSET_REPACK, nullptr, + " Cloning LigatureSubstFormat1 (%u) range [%u, %u).", this_index, start, end); + + // Create an oversized new liga subst, we'll adjust the size down later. We don't know + // the final size until we process it but we also need it to exist while we're processing + // so that nodes can be moved to it as needed. + unsigned prime_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2::min_size + + ligatureSet.get_size() - ligatureSet.len.get_size(); + + unsigned liga_subst_prime_id = c.create_node (prime_size); + if (liga_subst_prime_id == (unsigned) -1) return -1; + + LigatureSubstFormat1* liga_subst_prime = (LigatureSubstFormat1*) c.graph.object (liga_subst_prime_id).head; + liga_subst_prime->format = this->format; + liga_subst_prime->ligatureSet.len = this->ligatureSet.len; + + // Create a place holder coverage prime id since we need to add virtual links to it while + // generating liga and liga sets. Afterwards it will be updated to have the correct coverage. + unsigned coverage_id = c.graph.index_for_offset (this_index, &coverage); + unsigned coverage_prime_id = c.graph.duplicate(coverage_id); + auto& coverage_prime_vertex = c.graph.vertices_[coverage_prime_id]; + auto* coverage_prime_link = c.graph.vertices_[liga_subst_prime_id].obj.real_links.push (); + coverage_prime_link->width = SmallTypes::size; + coverage_prime_link->objidx = coverage_prime_id; + coverage_prime_link->position = 2; + coverage_prime_vertex.add_parent (liga_subst_prime_id, false); + + // Locate all liga sets with ligas between start and end. + // Clone or move them as needed. + unsigned count = 0; + unsigned liga_set_count = 0; + unsigned liga_set_start = -1; + unsigned liga_set_end = 0; // inclusive + for (unsigned i = 0; i < liga_counts.length; i++) + { + unsigned num_ligas = liga_counts[i]; + + unsigned current_start = count; + unsigned current_end = count + num_ligas; + + if (current_start >= end || start >= current_end) { + // No intersection, so just skip + count += num_ligas; + continue; + } + + auto liga_set_index = c.graph.index_for_offset(this_index, &ligatureSet[i]); + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return -1; + } + + // Bounds may need to be adjusted if some ligas have been previously removed. + hb_pair_t liga_bounds = current_liga_set_bounds(c, liga_set_index, liga_set.vertex->obj); + current_start = hb_max(count + liga_bounds.first, current_start); + current_end = hb_min(count + liga_bounds.second, current_end); + + unsigned liga_set_prime_id; + if (current_start >= start && current_end <= end) { + // This liga set is fully contined within [start, end) + // We can move the entire ligaset to the new liga subset object. + liga_set_end = i; + if (i < liga_set_start) liga_set_start = i; + liga_set_prime_id = c.graph.move_child<> (this_index, + &ligatureSet[i], + liga_subst_prime_id, + &liga_subst_prime->ligatureSet[liga_set_count++]); + compact_liga_set(c, liga_set.table, liga_set.vertex->obj); + } + else + { + // This liga set partially overlaps [start, end). We'll need to create + // a new liga set sub table and move the intersecting ligas to it. + unsigned liga_count = hb_min(end, current_end) - hb_max(start, current_start); + auto result = new_liga_set(c, liga_count); + liga_set_prime_id = result.first; + LigatureSet* prime = result.second; + if (liga_set_prime_id == (unsigned) -1) return -1; + + unsigned new_index = 0; + for (unsigned j = hb_max(start, current_start) - count; j < hb_min(end, current_end) - count; j++) { + c.graph.move_child<> (liga_set_index, + &liga_set.table->ligature[j], + liga_set_prime_id, + &prime->ligature[new_index++]); + } + + liga_set_end = i; + if (i < liga_set_start) liga_set_start = i; + c.graph.add_link(&liga_subst_prime->ligatureSet[liga_set_count++], liga_subst_prime_id, liga_set_prime_id); + } + + // The new liga and all children set needs to have a virtual link to the new coverage table: + auto& liga_set_prime = c.graph.vertices_[liga_set_prime_id].obj; + clear_virtual_links(c, liga_set_prime_id); + add_virtual_link(c, liga_set_prime_id, coverage_prime_id); + for (const auto& l : liga_set_prime.real_links) { + clear_virtual_links(c, l.objidx); + add_virtual_link(c, l.objidx, coverage_prime_id); + } + + count += num_ligas; + } + + c.graph.vertices_[liga_subst_prime_id].obj.tail -= (liga_subst_prime->ligatureSet.len - liga_set_count) * SmallTypes::size; + liga_subst_prime->ligatureSet.len = liga_set_count; + + if (!Coverage::filter_coverage (c, + coverage_prime_id, + liga_set_start, liga_set_end + 1)) + return -1; + + return liga_subst_prime_id; + } + + bool shrink (gsubgpos_graph_context_t& c, + unsigned this_index, + unsigned old_count, + hb_vector_t liga_counts, + unsigned count) + { + DEBUG_MSG (SUBSET_REPACK, nullptr, + " Shrinking LigatureSubstFormat1 (%u) to [0, %u).", + this_index, + count); + if (count >= old_count) + return true; + + hb_set_t retained_indices; + unsigned new_liga_set_count = 0; + for (unsigned i = 0; i < liga_counts.length; i++) + { + auto liga_set = c.graph.as_table(this_index, &ligatureSet[i]); + if (!liga_set.table) { + return false; + } + + // We need the virtual links to coverage removed from all descendants on this liga subst. + // If any are left when we try to mutate the coverage table later it will be unnessecarily + // duplicated. Code later on will re-add the virtual links as needed (via retained_indices). + clear_virtual_links(c, liga_set.index); + retained_indices.add(liga_set.index); + for (const auto& liga_offset : liga_set.table->ligature) { + unsigned liga_index = c.graph.index_for_offset(liga_set.index, &liga_offset); + if (liga_index != (unsigned) -1) { + clear_virtual_links(c, liga_index); + retained_indices.add(liga_index); + } + } + + unsigned num_ligas = liga_counts[i]; + if (num_ligas >= count) { + // drop the trailing liga's from this set and all subsequent liga sets + unsigned num_ligas_to_remove = num_ligas - count; + new_liga_set_count = i + 1; + c.graph.vertices_[liga_set.index].obj.tail -= num_ligas_to_remove * SmallTypes::size; + liga_set.table->ligature.len = count; + break; + } else { + count -= num_ligas; + } + } + + // Adjust liga set array + c.graph.vertices_[this_index].obj.tail -= (ligatureSet.len - new_liga_set_count) * SmallTypes::size; + ligatureSet.len = new_liga_set_count; + + // Coverage matches the number of liga sets so rebuild as needed + auto coverage = c.graph.as_mutable_table (this_index, &this->coverage); + if (!coverage) return false; + + for (unsigned i : retained_indices.iter()) + add_virtual_link(c, i, coverage.index); + + unsigned coverage_size = coverage.vertex->table_size (); + auto new_coverage = + + hb_zip (coverage.table->iter (), hb_range ()) + | hb_filter ([&] (hb_pair_t p) { + return p.second < new_liga_set_count; + }) + | hb_map_retains_sorting (hb_first) + ; + + return Coverage::make_coverage (c, new_coverage, coverage.index, coverage_size); + } +}; + +struct LigatureSubst : public OT::Layout::GSUB_impl::LigatureSubst +{ + + hb_vector_t split_subtables (gsubgpos_graph_context_t& c, + unsigned parent_index, + unsigned this_index) + { + switch (u.format) { + case 1: + return ((LigatureSubstFormat1*)(&u.format1))->split_subtables (c, parent_index, this_index); +#ifndef HB_NO_BEYOND_64K + case 2: HB_FALLTHROUGH; + // Don't split 24bit Ligature Subs +#endif + default: + return hb_vector_t (); + } + } + + bool sanitize (graph_t::vertex_t& vertex) const + { + int64_t vertex_len = vertex.obj.tail - vertex.obj.head; + if (vertex_len < u.format.get_size ()) return false; + hb_barrier (); + + switch (u.format) { + case 1: + return ((LigatureSubstFormat1*)(&u.format1))->sanitize (vertex); +#ifndef HB_NO_BEYOND_64K + case 2: HB_FALLTHROUGH; +#endif + default: + // We don't handle format 2 here. + return false; + } + } +}; + +} + +#endif // GRAPH_LIGATURE_GRAPH_HH diff --git a/src/graph/pairpos-graph.hh b/src/graph/pairpos-graph.hh index fd46861de..46da27406 100644 --- a/src/graph/pairpos-graph.hh +++ b/src/graph/pairpos-graph.hh @@ -423,7 +423,7 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4width = SmallTypes::size; class_def_link->objidx = class_def_2_id; class_def_link->position = 10; - graph.vertices_[class_def_2_id].add_parent (pair_pos_prime_id); + graph.vertices_[class_def_2_id].add_parent (pair_pos_prime_id, false); graph.duplicate (pair_pos_prime_id, class_def_2_id); return pair_pos_prime_id; diff --git a/src/graph/serialize.hh b/src/graph/serialize.hh index 06e4bf44d..fb8950e6f 100644 --- a/src/graph/serialize.hh +++ b/src/graph/serialize.hh @@ -172,8 +172,11 @@ void print_overflows (graph_t& graph, template inline void serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link, char* head, + unsigned size, hb_serialize_context_t* c) { + assert(link.position + link.width <= size); + OT::Offset* offset = reinterpret_cast*> (head + link.position); *offset = 0; c->add_link (*offset, @@ -187,6 +190,7 @@ serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link, inline void serialize_link (const hb_serialize_context_t::object_t::link_t& link, char* head, + unsigned size, hb_serialize_context_t* c) { switch (link.width) @@ -197,21 +201,21 @@ void serialize_link (const hb_serialize_context_t::object_t::link_t& link, case 4: if (link.is_signed) { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } else { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } return; case 2: if (link.is_signed) { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } else { - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); } return; case 3: - serialize_link_of_type (link, head, c); + serialize_link_of_type (link, head, size, c); return; default: // Unexpected link width. @@ -251,7 +255,7 @@ inline hb_blob_t* serialize (const graph_t& graph) // Only real links needs to be serialized. for (const auto& link : vertices[i].obj.real_links) - serialize_link (link, start, &c); + serialize_link (link, start, size, &c); // All duplications are already encoded in the graph, so don't // enable sharing during packing. diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh index cb4fdeead..11d47c9dc 100644 --- a/src/hb-repacker.hh +++ b/src/hb-repacker.hh @@ -266,7 +266,7 @@ bool _resolve_shared_overflow(const hb_vector_t& overf result = sorted_graph.duplicate(&parents, r.child); } - if (result == (unsigned) -1) return result; + if (result == (unsigned) -1) return false; if (parents.get_population() > 1) { // If the duplicated node has more than one parent pre-emptively raise it's priority to the maximum. @@ -283,7 +283,7 @@ bool _resolve_shared_overflow(const hb_vector_t& overf sorted_graph.vertices_[result].give_max_priority(); } - return result; + return true; } static inline @@ -302,8 +302,11 @@ bool _process_overflows (const hb_vector_t& overflows, { // The child object is shared, we may be able to eliminate the overflow // by duplicating it. - if (!_resolve_shared_overflow(overflows, i, sorted_graph)) continue; - return true; + if (_resolve_shared_overflow(overflows, i, sorted_graph)) + return true; + + // Sometimes we can't duplicate a node which looks shared because it's not actually shared + // (eg. all links from the same parent) in this case continue on to other resolution options. } if (child.is_leaf () && !priority_bumped_parents.has (r.parent)) diff --git a/src/rust/font.rs b/src/rust/font.rs index e2180a2e0..cee949aab 100644 --- a/src/rust/font.rs +++ b/src/rust/font.rs @@ -5,7 +5,7 @@ use std::ffi::c_void; use std::mem::transmute; use std::ptr::null_mut; use std::sync::atomic::{AtomicPtr, AtomicU32, Ordering}; -use std::sync::{Mutex, OnceLock}; +use std::sync::{Arc, Mutex}; use skrifa::charmap::Charmap; use skrifa::charmap::MapVariant::Variant; @@ -35,7 +35,6 @@ struct FontationsData<'a> { outline_glyphs: OutlineGlyphCollection<'a>, color_glyphs: ColorGlyphCollection<'a>, glyph_names: GlyphNames<'a>, - glyph_from_names: OnceLock>, size: Size, vert_metrics: Option>, vert_origin: Option>, @@ -89,7 +88,6 @@ impl FontationsData<'_> { outline_glyphs, color_glyphs, glyph_names, - glyph_from_names: OnceLock::new(), size: Size::new(upem as f32), vert_metrics, vert_origin, @@ -980,8 +978,14 @@ extern "C" fn _hb_fontations_glyph_name( } } +static mut GLYPH_FROM_NAMES_KEY: hb_user_data_key_t = hb_user_data_key_t { unused: 0 }; + +extern "C" fn _hb_glyph_from_names_destroy(data: *mut c_void) { + let _ = unsafe { Arc::from_raw(data as *const HashMap) }; +} + extern "C" fn _hb_fontations_glyph_from_name( - _font: *mut hb_font_t, + font: *mut hb_font_t, font_data: *mut ::std::os::raw::c_void, name: *const ::std::os::raw::c_char, len: ::std::os::raw::c_int, @@ -992,13 +996,35 @@ extern "C" fn _hb_fontations_glyph_from_name( let name = unsafe { std::slice::from_raw_parts(name as *const u8, len as usize) }; let name = std::str::from_utf8(name).unwrap_or_default(); + let mut glyph_from_names: *mut c_void; - let glyph_from_names = data.glyph_from_names.get_or_init(|| { - data.glyph_names - .iter() - .map(|(gid, name)| (name.to_string(), gid.to_u32())) - .collect() - }); + let face = unsafe { hb_font_get_face(font) }; + loop { + glyph_from_names = unsafe { hb_face_get_user_data(face, &raw mut GLYPH_FROM_NAMES_KEY) }; + if !glyph_from_names.is_null() { + break; + } + // Create a new HashMap to store the glyph names + let mut glyph_from_names = HashMap::new(); + for (glyph_id, glyph_name) in data.glyph_names.iter() { + glyph_from_names.insert(glyph_name.to_string(), glyph_id.to_u32()); + } + + let glyph_from_names = Arc::new(glyph_from_names); + unsafe { + if hb_face_set_user_data( + face, + &raw mut GLYPH_FROM_NAMES_KEY, + Arc::into_raw(glyph_from_names) as *mut c_void, + Some(_hb_glyph_from_names_destroy), + true as hb_bool_t, + ) != false as hb_bool_t + { + break; + } + } + } + let glyph_from_names = unsafe { &*(glyph_from_names as *const HashMap) }; if let Some(glyph_id) = glyph_from_names.get(name) { unsafe { *glyph = *glyph_id; diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 7f89fbfa3..51256b9e0 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -24,6 +24,7 @@ * Google Author(s): Garret Rieger */ +#include #include #include "hb-repacker.hh" @@ -147,6 +148,17 @@ static unsigned add_extension (unsigned child, static unsigned add_coverage (unsigned start, unsigned end, hb_serialize_context_t* c) { + if (end - start == 0) { + uint8_t coverage[] = { + 0, 1, // format + 0, 1, // count + + (uint8_t) ((start >> 8) & 0xFF), + (uint8_t) (start & 0xFF), // glyph[0] + }; + return add_object ((char*) coverage, 6, c); + } + if (end - start == 1) { uint8_t coverage[] = { @@ -242,6 +254,31 @@ static unsigned add_pair_pos_1 (unsigned* pair_sets, return c->pop_pack (false); } +static void add_liga_set_header (char liga_count, + hb_serialize_context_t* c) +{ + uint8_t liga_count_bytes[] = { + (uint8_t) (0xFF & (liga_count >> 8)), + (uint8_t) (0xFF & liga_count), + }; + + start_object ((const char*) liga_count_bytes, 2, c); +} + +static void add_liga_header (unsigned coverage, unsigned liga_set_count, hb_serialize_context_t* c) +{ + uint8_t format[] = {0, 1}; + start_object ((const char*) format, 2, c); + add_offset(coverage, c); + + uint8_t liga_set_count_bytes[] = { + (uint8_t) (0xFF & (liga_set_count >> 8)), + (uint8_t) (0xFF & liga_set_count), + }; + + extend((const char *) liga_set_count_bytes, 2, c); +} + static unsigned add_pair_pos_2 (unsigned starting_class, unsigned coverage, unsigned class_def_1, uint16_t class_def_1_count, @@ -521,6 +558,39 @@ populate_serializer_simple (hb_serialize_context_t* c) c->end_serialize(); } +static void +populate_serializer_virtual (hb_serialize_context_t* c, bool with_overflow) +{ + std::string large_string(50000, 'a'); + c->start_serialize (); + + unsigned obj_4, obj_5; + if (with_overflow) { + obj_5 = add_object("55555", 5, c); + obj_4 = add_object(large_string.c_str(), 50000, c); + } else { + obj_4 = add_object(large_string.c_str(), 50000, c); + obj_5 = add_object("55555", 5, c); + } + + start_object(large_string.c_str(), 20000, c); + add_offset(obj_5, c); + unsigned obj_3 = c->pop_pack(false); + + start_object("2", 2, c); + add_virtual_offset(obj_5, c); + unsigned obj_2 = c->pop_pack(false); + + // obj 1 + start_object("1", 1, c); + add_offset(obj_2, c); + add_offset(obj_3, c); + add_offset(obj_4, c); + c->pop_pack(false); + + c->end_serialize(); +} + static void populate_serializer_with_overflow (hb_serialize_context_t* c) { @@ -1568,6 +1638,163 @@ populate_serializer_with_large_mark_base_pos_1 (hb_serialize_context_t* c) c->end_serialize(); } +template +static void +populate_serializer_with_large_liga (hb_serialize_context_t* c, bool sequential_liga_sets) +{ + std::string large_string(100000, 'a'); + c->start_serialize (); + + unsigned liga_subst[liga_subst_count]; + + for (unsigned l = 0; l < liga_subst_count; l++) { + unsigned coverage_start = 0; + unsigned coverage_end = liga_set_count - 1; + if (sequential_liga_sets) { + coverage_start = l * liga_set_count; + coverage_end = (l + 1) * liga_set_count - 1; + } + unsigned coverage = add_coverage(coverage_start, coverage_end, c); + + unsigned liga[liga_set_count * liga_per_set_count]; + unsigned liga_set[liga_set_count]; + for (unsigned i = 0; i < liga_set_count; i++) { + for (unsigned j = 0; j < liga_per_set_count; j++) + { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i * liga_per_set_count + j] = c->pop_pack (false); + } + + add_liga_set_header(liga_per_set_count, c); + add_virtual_offset(coverage, c); + for (unsigned j = 0; j < liga_per_set_count; j++) + { + add_offset(liga[i * liga_per_set_count + j], c); + } + + liga_set[i] = c->pop_pack(false); + } + + add_liga_header(coverage, liga_set_count, c); + for (unsigned i = 0; i < liga_set_count; i++) + { + add_offset(liga_set[i], c); + } + + liga_subst[l] = c->pop_pack(false); + } + + for (unsigned l = 0; l < liga_subst_count; l++) { + liga_subst[l] = add_extension(liga_subst[l], 4, c); + } + + start_lookup (7, liga_subst_count, c); + for (unsigned l = 0; l < liga_subst_count; l++) { + add_offset(liga_subst[l], c); + } + + unsigned lookup = finish_lookup (c); + + unsigned lookup_list = add_lookup_list (&lookup, 1, c); + + add_gsubgpos_header (lookup_list, c); + + c->end_serialize(); +} + +static void +populate_serializer_with_large_liga_overlapping_clone_result (hb_serialize_context_t* c) +{ + std::string large_string(100000, 'a'); + c->start_serialize (); + + constexpr unsigned liga_size = 30000; + + unsigned liga[6]; + unsigned liga_subst[3]; + unsigned liga_set[2]; + + // LigaSubst 3 + unsigned coverage = add_coverage(1, 1, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(2, c); + add_virtual_offset(coverage, c); + for (unsigned i = 0; i < 2; i++) + add_offset(liga[i], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 1, c); + add_offset(liga_set[0], c); + liga_subst[2] = c->pop_pack(false); + + // LigaSubst 2 + coverage = add_coverage(0, 1, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(1, c); + add_virtual_offset(coverage, c); + add_offset(liga[1], c); + liga_set[1] = c->pop_pack(false); + + add_liga_set_header(1, c); + add_virtual_offset(coverage, c); + add_offset(liga[0], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 2, c); + add_offset(liga_set[0], c); + add_offset(liga_set[1], c); + liga_subst[1] = c->pop_pack(false); + + // LigaSubst 1 + coverage = add_coverage(0, 0, c); + for (int i = 1; i >= 0; i--) { + start_object (large_string.c_str(), liga_size, c); + add_virtual_offset(coverage, c); + liga[i] = c->pop_pack (false); + } + + add_liga_set_header(2, c); + add_virtual_offset(coverage, c); + for (unsigned i = 0; i < 2; i++) + add_offset(liga[i], c); + liga_set[0] = c->pop_pack(false); + + add_liga_header(coverage, 1, c); + add_offset(liga_set[0], c); + liga_subst[0] = c->pop_pack(false); + + for (int l = 2; l >= 0; l--) { + liga_subst[l] = add_extension(liga_subst[l], 4, c); + } + + start_lookup (7, 3, c); + for (unsigned l = 0; l < 3; l++) { + add_offset(liga_subst[l], c); + } + + unsigned lookup = finish_lookup (c); + + unsigned lookup_list = add_lookup_list (&lookup, 1, c); + + add_gsubgpos_header (lookup_list, c); + + c->end_serialize(); +} + static void test_sort_shortest () { size_t buffer_size = 100; @@ -2113,6 +2340,75 @@ static void test_resolve_with_basic_mark_base_pos_1_split () free (expected_buffer); } +static void test_resolve_with_basic_liga_split () +{ + size_t buffer_size = 200000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 1, 2, 40000>(&c, false); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga<2, 1, 1, 40000>(&e, false); + + run_resolve_overflow_test ("test_resolve_with_basic_liga_split", + c, + e, + 20, + true, + HB_TAG('G', 'S', 'U', 'B')); + free (buffer); + free (expected_buffer); +} + +static void test_resolve_with_liga_split_move () +{ + size_t buffer_size = 400000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 6, 2, 16000>(&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga<3, 2, 2, 16000>(&e, true); + + run_resolve_overflow_test ("test_resolve_with_liga_split_move", + c, + e, + 20, + true, + HB_TAG('G', 'S', 'U', 'B')); + free (buffer); + free (expected_buffer); +} + +static void test_resolve_with_liga_split_overlapping_clone () +{ + size_t buffer_size = 400000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_with_large_liga<1, 2, 3, 30000>(&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_with_large_liga_overlapping_clone_result(&e); + + run_resolve_overflow_test ("test_resolve_with_liga_split_overlapping_clone", + c, + e, + 20, + true, + HB_TAG('G', 'S', 'U', 'B')); + free (buffer); + free (expected_buffer); +} + static void test_resolve_overflows_via_splitting_spaces () { size_t buffer_size = 160000; @@ -2257,6 +2553,32 @@ test_repack_last () free (expected_buffer); } +static void +test_dont_duplicate_virtual () +{ + size_t buffer_size = 200000; + void* buffer = malloc (buffer_size); + assert (buffer); + hb_serialize_context_t c (buffer, buffer_size); + populate_serializer_virtual (&c, true); + + void* expected_buffer = malloc (buffer_size); + assert (expected_buffer); + hb_serialize_context_t e (expected_buffer, buffer_size); + populate_serializer_virtual (&e, false); + + run_resolve_overflow_test ("test_dont_duplicate_virtual", + c, + e, + 20, + false, + HB_TAG('a', 'b', 'c', 'd'), + true); + + free (buffer); + free (expected_buffer); +} + // TODO(garretrieger): update will_overflow tests to check the overflows array. // TODO(garretrieger): add tests for priority raising. @@ -2294,6 +2616,10 @@ main (int argc, char **argv) test_resolve_with_pair_pos_2_split_with_device_tables (); test_resolve_with_close_to_limit_pair_pos_2_split (); test_resolve_with_basic_mark_base_pos_1_split (); + test_resolve_with_basic_liga_split (); + test_resolve_with_liga_split_move (); + test_dont_duplicate_virtual (); + test_resolve_with_liga_split_overlapping_clone (); // TODO(grieger): have run overflow tests compare graph equality not final packed binary. // TODO(grieger): split test where multiple subtables in one lookup are split to test link ordering.