2 * Copyright © 2020 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Garret Rieger
27 #ifndef HB_REPACKER_HH
28 #define HB_REPACKER_HH
30 #include "hb-open-type.hh"
32 #include "hb-priority-queue.hh"
33 #include "hb-serialize.hh"
34 #include "hb-vector.hh"
37 * For a detailed writeup on the overflow resolution algorithm see:
45 hb_serialize_context_t::object_t obj;
46 int64_t distance = 0 ;
48 hb_vector_t<unsigned> parents;
51 unsigned priority = 0;
53 bool is_shared () const
55 return parents.length > 1;
58 unsigned incoming_edges () const
60 return parents.length;
63 void remove_parent (unsigned parent_index)
65 for (unsigned i = 0; i < parents.length; i++)
67 if (parents[i] != parent_index) continue;
73 void remap_parents (const hb_vector_t<unsigned>& id_map)
75 for (unsigned i = 0; i < parents.length; i++)
76 parents[i] = id_map[parents[i]];
79 void remap_parent (unsigned old_index, unsigned new_index)
81 for (unsigned i = 0; i < parents.length; i++)
83 if (parents[i] == old_index)
84 parents[i] = new_index;
90 return !obj.real_links.length && !obj.virtual_links.length;
93 bool raise_priority ()
95 if (has_max_priority ()) return false;
100 bool has_max_priority () const {
101 return priority >= 3;
104 int64_t modified_distance (unsigned order) const
106 // TODO(garretrieger): once priority is high enough, should try
107 // setting distance = 0 which will force to sort immediately after
108 // it's parent where possible.
110 int64_t modified_distance =
111 hb_min (hb_max(distance + distance_modifier (), 0), 0x7FFFFFFFFFF);
112 if (has_max_priority ()) {
113 modified_distance = 0;
115 return (modified_distance << 18) | (0x003FFFF & order);
118 int64_t distance_modifier () const
120 if (!priority) return 0;
121 int64_t table_size = obj.tail - obj.head;
124 return -table_size / 2;
130 struct overflow_record_t
137 * A topological sorting of an object graph. Ordered
138 * in reverse serialization order (first object in the
139 * serialization is at the end of the list). This matches
140 * the 'packed' object stack used internally in the
143 graph_t (const hb_vector_t<hb_serialize_context_t::object_t *>& objects)
144 : parents_invalid (true),
145 distance_invalid (true),
146 positions_invalid (true),
149 num_roots_for_space_.push (1);
150 bool removed_nil = false;
151 for (unsigned i = 0; i < objects.length; i++)
153 // TODO(grieger): check all links point to valid objects.
155 // If this graph came from a serialization buffer object 0 is the
156 // nil object. We don't need it for our purposes here so drop it.
157 if (i == 0 && !objects[i])
163 vertex_t* v = vertices_.push ();
164 if (check_success (!vertices_.in_error ()))
165 v->obj = *objects[i];
166 if (!removed_nil) continue;
167 // Fix indices to account for removed nil object.
168 for (auto& l : v->obj.all_links_writer ()) {
179 bool in_error () const
181 return !successful ||
182 vertices_.in_error () ||
183 num_roots_for_space_.in_error ();
186 const vertex_t& root () const
188 return vertices_[root_idx ()];
191 unsigned root_idx () const
193 // Object graphs are in reverse order, the first object is at the end
194 // of the vector. Since the graph is topologically sorted it's safe to
195 // assume the first object has no incoming edges.
196 return vertices_.length - 1;
199 const hb_serialize_context_t::object_t& object(unsigned i) const
201 return vertices_[i].obj;
205 * serialize graph into the provided serialization buffer.
207 hb_blob_t* serialize () const
209 hb_vector_t<char> buffer;
210 size_t size = serialized_length ();
211 if (!buffer.alloc (size)) {
212 DEBUG_MSG (SUBSET_REPACK, nullptr, "Unable to allocate output buffer.");
215 hb_serialize_context_t c((void *) buffer, size);
217 c.start_serialize<void> ();
218 for (unsigned i = 0; i < vertices_.length; i++) {
221 size_t size = vertices_[i].obj.tail - vertices_[i].obj.head;
222 char* start = c.allocate_size <char> (size);
224 DEBUG_MSG (SUBSET_REPACK, nullptr, "Buffer out of space.");
228 memcpy (start, vertices_[i].obj.head, size);
230 // Only real links needs to be serialized.
231 for (const auto& link : vertices_[i].obj.real_links)
232 serialize_link (link, start, &c);
234 // All duplications are already encoded in the graph, so don't
235 // enable sharing during packing.
241 DEBUG_MSG (SUBSET_REPACK, nullptr, "Error during serialization. Err flag: %d",
246 return c.copy_blob ();
250 * Generates a new topological sorting of graph using Kahn's
251 * algorithm: https://en.wikipedia.org/wiki/Topological_sorting#Algorithms
255 positions_invalid = true;
257 if (vertices_.length <= 1) {
258 // Graph of 1 or less doesn't need sorting.
262 hb_vector_t<unsigned> queue;
263 hb_vector_t<vertex_t> sorted_graph;
264 if (unlikely (!check_success (sorted_graph.resize (vertices_.length)))) return;
265 hb_vector_t<unsigned> id_map;
266 if (unlikely (!check_success (id_map.resize (vertices_.length)))) return;
268 hb_vector_t<unsigned> removed_edges;
269 if (unlikely (!check_success (removed_edges.resize (vertices_.length)))) return;
272 queue.push (root_idx ());
273 int new_id = vertices_.length - 1;
275 while (!queue.in_error () && queue.length)
277 unsigned next_id = queue[0];
280 vertex_t& next = vertices_[next_id];
281 sorted_graph[new_id] = next;
282 id_map[next_id] = new_id--;
284 for (const auto& link : next.obj.all_links ()) {
285 removed_edges[link.objidx]++;
286 if (!(vertices_[link.objidx].incoming_edges () - removed_edges[link.objidx]))
287 queue.push (link.objidx);
291 check_success (!queue.in_error ());
292 check_success (!sorted_graph.in_error ());
293 if (!check_success (new_id == -1))
294 print_orphaned_nodes ();
296 remap_all_obj_indices (id_map, &sorted_graph);
298 hb_swap (vertices_, sorted_graph);
299 sorted_graph.fini ();
303 * Generates a new topological sorting of graph ordered by the shortest
304 * distance to each node.
306 void sort_shortest_distance ()
308 positions_invalid = true;
310 if (vertices_.length <= 1) {
311 // Graph of 1 or less doesn't need sorting.
317 hb_priority_queue_t queue;
318 hb_vector_t<vertex_t> sorted_graph;
319 if (unlikely (!check_success (sorted_graph.resize (vertices_.length)))) return;
320 hb_vector_t<unsigned> id_map;
321 if (unlikely (!check_success (id_map.resize (vertices_.length)))) return;
323 hb_vector_t<unsigned> removed_edges;
324 if (unlikely (!check_success (removed_edges.resize (vertices_.length)))) return;
327 queue.insert (root ().modified_distance (0), root_idx ());
328 int new_id = root_idx ();
330 while (!queue.in_error () && !queue.is_empty ())
332 unsigned next_id = queue.pop_minimum().second;
334 vertex_t& next = vertices_[next_id];
335 sorted_graph[new_id] = next;
336 id_map[next_id] = new_id--;
338 for (const auto& link : next.obj.all_links ()) {
339 removed_edges[link.objidx]++;
340 if (!(vertices_[link.objidx].incoming_edges () - removed_edges[link.objidx]))
341 // Add the order that the links were encountered to the priority.
342 // This ensures that ties between priorities objects are broken in a consistent
343 // way. More specifically this is set up so that if a set of objects have the same
344 // distance they'll be added to the topological order in the order that they are
345 // referenced from the parent object.
346 queue.insert (vertices_[link.objidx].modified_distance (order++),
351 check_success (!queue.in_error ());
352 check_success (!sorted_graph.in_error ());
353 if (!check_success (new_id == -1))
354 print_orphaned_nodes ();
356 remap_all_obj_indices (id_map, &sorted_graph);
358 hb_swap (vertices_, sorted_graph);
359 sorted_graph.fini ();
363 * Assign unique space numbers to each connected subgraph of 32 bit offset(s).
365 bool assign_32bit_spaces ()
367 unsigned root_index = root_idx ();
370 for (unsigned i = 0; i <= root_index; i++)
372 // Only real links can form 32 bit spaces
373 for (auto& l : vertices_[i].obj.real_links)
375 if (l.width == 4 && !l.is_signed)
377 roots.add (l.objidx);
378 find_subgraph (l.objidx, visited);
383 // Mark everything not in the subgraphs of 32 bit roots as visited.
384 // This prevents 32 bit subgraphs from being connected via nodes not in the 32 bit subgraphs.
387 if (!roots) return false;
391 unsigned next = HB_SET_VALUE_INVALID;
392 if (unlikely (!check_success (!roots.in_error ()))) break;
393 if (!roots.next (&next)) break;
395 hb_set_t connected_roots;
396 find_connected_nodes (next, roots, visited, connected_roots);
397 if (unlikely (!check_success (!connected_roots.in_error ()))) break;
399 isolate_subgraph (connected_roots);
400 if (unlikely (!check_success (!connected_roots.in_error ()))) break;
402 unsigned next_space = this->next_space ();
403 num_roots_for_space_.push (0);
404 for (unsigned root : connected_roots)
406 DEBUG_MSG (SUBSET_REPACK, nullptr, "Subgraph %u gets space %u", root, next_space);
407 vertices_[root].space = next_space;
408 num_roots_for_space_[next_space] = num_roots_for_space_[next_space] + 1;
409 distance_invalid = true;
410 positions_invalid = true;
413 // TODO(grieger): special case for GSUB/GPOS use extension promotions to move 16 bit space
414 // into the 32 bit space as needed, instead of using isolation.
423 * Isolates the subgraph of nodes reachable from root. Any links to nodes in the subgraph
424 * that originate from outside of the subgraph will be removed by duplicating the linked to
427 * Indices stored in roots will be updated if any of the roots are duplicated to new indices.
429 bool isolate_subgraph (hb_set_t& roots)
432 hb_hashmap_t<unsigned, unsigned> subgraph;
434 // incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these
435 // set the subgraph incoming edge count to match all of root_idx's incoming edges
437 for (unsigned root_idx : roots)
439 subgraph.set (root_idx, wide_parents (root_idx, parents));
440 find_subgraph (root_idx, subgraph);
443 unsigned original_root_idx = root_idx ();
444 hb_hashmap_t<unsigned, unsigned> index_map;
445 bool made_changes = false;
446 for (auto entry : subgraph.iter ())
448 const auto& node = vertices_[entry.first];
449 unsigned subgraph_incoming_edges = entry.second;
451 if (subgraph_incoming_edges < node.incoming_edges ())
453 // Only de-dup objects with incoming links from outside the subgraph.
455 duplicate_subgraph (entry.first, index_map);
462 if (original_root_idx != root_idx ()
463 && parents.has (original_root_idx))
465 // If the root idx has changed since parents was determined, update root idx in parents
466 parents.add (root_idx ());
467 parents.del (original_root_idx);
472 | hb_map([&] (unsigned node_idx) {
473 if (index_map.has (node_idx)) return index_map[node_idx];
478 remap_obj_indices (index_map, new_subgraph);
479 remap_obj_indices (index_map, parents.iter (), true);
481 // Update roots set with new indices as needed.
482 unsigned next = HB_SET_VALUE_INVALID;
483 while (roots.next (&next))
485 if (index_map.has (next))
488 roots.add (index_map[next]);
495 void find_subgraph (unsigned node_idx, hb_hashmap_t<unsigned, unsigned>& subgraph)
497 for (const auto& link : vertices_[node_idx].obj.all_links ())
499 if (subgraph.has (link.objidx))
501 subgraph.set (link.objidx, subgraph[link.objidx] + 1);
504 subgraph.set (link.objidx, 1);
505 find_subgraph (link.objidx, subgraph);
509 void find_subgraph (unsigned node_idx, hb_set_t& subgraph)
511 if (subgraph.has (node_idx)) return;
512 subgraph.add (node_idx);
513 for (const auto& link : vertices_[node_idx].obj.all_links ())
514 find_subgraph (link.objidx, subgraph);
518 * duplicates all nodes in the subgraph reachable from node_idx. Does not re-assign
519 * links. index_map is updated with mappings from old id to new id. If a duplication has already
520 * been performed for a given index, then it will be skipped.
522 void duplicate_subgraph (unsigned node_idx, hb_hashmap_t<unsigned, unsigned>& index_map)
524 if (index_map.has (node_idx))
527 index_map.set (node_idx, duplicate (node_idx));
528 for (const auto& l : object (node_idx).all_links ()) {
529 duplicate_subgraph (l.objidx, index_map);
534 * Creates a copy of node_idx and returns it's new index.
536 unsigned duplicate (unsigned node_idx)
538 positions_invalid = true;
539 distance_invalid = true;
541 auto* clone = vertices_.push ();
542 auto& child = vertices_[node_idx];
543 if (vertices_.in_error ()) {
547 clone->obj.head = child.obj.head;
548 clone->obj.tail = child.obj.tail;
549 clone->distance = child.distance;
550 clone->space = child.space;
551 clone->parents.reset ();
553 unsigned clone_idx = vertices_.length - 2;
554 for (const auto& l : child.obj.real_links)
556 clone->obj.real_links.push (l);
557 vertices_[l.objidx].parents.push (clone_idx);
559 for (const auto& l : child.obj.virtual_links)
561 clone->obj.virtual_links.push (l);
562 vertices_[l.objidx].parents.push (clone_idx);
565 check_success (!clone->obj.real_links.in_error ());
566 check_success (!clone->obj.virtual_links.in_error ());
568 // The last object is the root of the graph, so swap back the root to the end.
569 // The root's obj idx does change, however since it's root nothing else refers to it.
570 // all other obj idx's will be unaffected.
571 vertex_t root = vertices_[vertices_.length - 2];
572 vertices_[clone_idx] = *clone;
573 vertices_[vertices_.length - 1] = root;
575 // Since the root moved, update the parents arrays of all children on the root.
576 for (const auto& l : root.obj.all_links ())
577 vertices_[l.objidx].remap_parent (root_idx () - 1, root_idx ());
583 * Creates a copy of child and re-assigns the link from
584 * parent to the clone. The copy is a shallow copy, objects
585 * linked from child are not duplicated.
587 bool duplicate (unsigned parent_idx, unsigned child_idx)
591 unsigned links_to_child = 0;
592 for (const auto& l : vertices_[parent_idx].obj.all_links ())
594 if (l.objidx == child_idx) links_to_child++;
597 if (vertices_[child_idx].incoming_edges () <= links_to_child)
599 // Can't duplicate this node, doing so would orphan the original one as all remaining links
600 // to child are from parent.
601 DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %d => %d",
602 parent_idx, child_idx);
606 DEBUG_MSG (SUBSET_REPACK, nullptr, " Duplicating %d => %d",
607 parent_idx, child_idx);
609 unsigned clone_idx = duplicate (child_idx);
610 if (clone_idx == (unsigned) -1) return false;
611 // duplicate shifts the root node idx, so if parent_idx was root update it.
612 if (parent_idx == clone_idx) parent_idx++;
614 auto& parent = vertices_[parent_idx];
615 for (auto& l : parent.obj.all_links_writer ())
617 if (l.objidx != child_idx)
620 reassign_link (l, parent_idx, clone_idx);
627 * Raises the sorting priority of all children.
629 bool raise_childrens_priority (unsigned parent_idx)
631 DEBUG_MSG (SUBSET_REPACK, nullptr, " Raising priority of all children of %d",
633 // This operation doesn't change ordering until a sort is run, so no need
634 // to invalidate positions. It does not change graph structure so no need
635 // to update distances or edge counts.
636 auto& parent = vertices_[parent_idx].obj;
637 bool made_change = false;
638 for (auto& l : parent.all_links_writer ())
639 made_change |= vertices_[l.objidx].raise_priority ();
644 * Will any offsets overflow on graph when it's serialized?
646 bool will_overflow (hb_vector_t<overflow_record_t>* overflows = nullptr)
648 if (overflows) overflows->resize (0);
651 for (int parent_idx = vertices_.length - 1; parent_idx >= 0; parent_idx--)
653 // Don't need to check virtual links for overflow
654 for (const auto& link : vertices_[parent_idx].obj.real_links)
656 int64_t offset = compute_offset (parent_idx, link);
657 if (is_valid_offset (offset, link))
660 if (!overflows) return true;
663 r.parent = parent_idx;
664 r.child = link.objidx;
669 if (!overflows) return false;
670 return overflows->length;
673 void print_orphaned_nodes ()
675 if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
677 DEBUG_MSG (SUBSET_REPACK, nullptr, "Graph is not fully connected.");
678 parents_invalid = true;
681 for (unsigned i = 0; i < root_idx (); i++)
683 const auto& v = vertices_[i];
685 DEBUG_MSG (SUBSET_REPACK, nullptr, "Node %u is orphaned.", i);
689 void print_overflows (const hb_vector_t<overflow_record_t>& overflows)
691 if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
695 for (const auto& o : overflows)
698 const auto& parent = vertices_[o.parent];
699 const auto& child = vertices_[o.child];
700 DEBUG_MSG (SUBSET_REPACK, nullptr,
702 "%4d (%4d in, %4d out, space %2d) => "
703 "%4d (%4d in, %4d out, space %2d)",
705 parent.incoming_edges (),
706 parent.obj.real_links.length + parent.obj.virtual_links.length,
707 space_for (o.parent),
709 child.incoming_edges (),
710 child.obj.real_links.length + child.obj.virtual_links.length,
711 space_for (o.child));
713 if (overflows.length > 10) {
714 DEBUG_MSG (SUBSET_REPACK, nullptr, " ... plus %d more overflows.", overflows.length - 10);
718 unsigned num_roots_for_space (unsigned space) const
720 return num_roots_for_space_[space];
723 unsigned next_space () const
725 return num_roots_for_space_.length;
728 void move_to_new_space (const hb_set_t& indices)
730 num_roots_for_space_.push (0);
731 unsigned new_space = num_roots_for_space_.length - 1;
733 for (unsigned index : indices) {
734 auto& node = vertices_[index];
735 num_roots_for_space_[node.space] = num_roots_for_space_[node.space] - 1;
736 num_roots_for_space_[new_space] = num_roots_for_space_[new_space] + 1;
737 node.space = new_space;
738 distance_invalid = true;
739 positions_invalid = true;
743 unsigned space_for (unsigned index, unsigned* root = nullptr) const
745 const auto& node = vertices_[index];
760 return space_for (node.parents[0], root);
763 void err_other_error () { this->successful = false; }
767 size_t serialized_length () const {
768 size_t total_size = 0;
769 for (unsigned i = 0; i < vertices_.length; i++) {
770 size_t size = vertices_[i].obj.tail - vertices_[i].obj.head;
777 * Returns the numbers of incoming edges that are 32bits wide.
779 unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const
783 for (unsigned p : vertices_[node_idx].parents)
785 if (visited.has (p)) continue;
788 // Only real links can be wide
789 for (const auto& l : vertices_[p].obj.real_links)
791 if (l.objidx == node_idx && l.width == 4 && !l.is_signed)
801 bool check_success (bool success)
802 { return this->successful && (success || (err_other_error (), false)); }
805 * Creates a map from objid to # of incoming edges.
807 void update_parents ()
809 if (!parents_invalid) return;
811 for (unsigned i = 0; i < vertices_.length; i++)
812 vertices_[i].parents.reset ();
814 for (unsigned p = 0; p < vertices_.length; p++)
816 for (auto& l : vertices_[p].obj.all_links ())
818 vertices_[l.objidx].parents.push (p);
822 parents_invalid = false;
826 * compute the serialized start and end positions for each vertex.
828 void update_positions ()
830 if (!positions_invalid) return;
832 unsigned current_pos = 0;
833 for (int i = root_idx (); i >= 0; i--)
835 auto& v = vertices_[i];
836 v.start = current_pos;
837 current_pos += v.obj.tail - v.obj.head;
841 positions_invalid = false;
845 * Finds the distance to each object in the graph
846 * from the initial node.
848 void update_distances ()
850 if (!distance_invalid) return;
852 // Uses Dijkstra's algorithm to find all of the shortest distances.
853 // https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
855 // Implementation Note:
856 // Since our priority queue doesn't support fast priority decreases
857 // we instead just add new entries into the queue when a priority changes.
858 // Redundant ones are filtered out later on by the visited set.
859 // According to https://www3.cs.stonybrook.edu/~rezaul/papers/TR-07-54.pdf
860 // for practical performance this is faster then using a more advanced queue
861 // (such as a fibonacci queue) with a fast decrease priority.
862 for (unsigned i = 0; i < vertices_.length; i++)
864 if (i == vertices_.length - 1)
865 vertices_[i].distance = 0;
867 vertices_[i].distance = hb_int_max (int64_t);
870 hb_priority_queue_t queue;
871 queue.insert (0, vertices_.length - 1);
873 hb_vector_t<bool> visited;
874 visited.resize (vertices_.length);
876 while (!queue.in_error () && !queue.is_empty ())
878 unsigned next_idx = queue.pop_minimum ().second;
879 if (visited[next_idx]) continue;
880 const auto& next = vertices_[next_idx];
881 int64_t next_distance = vertices_[next_idx].distance;
882 visited[next_idx] = true;
884 for (const auto& link : next.obj.all_links ())
886 if (visited[link.objidx]) continue;
888 const auto& child = vertices_[link.objidx].obj;
889 unsigned link_width = link.width ? link.width : 4; // treat virtual offsets as 32 bits wide
890 int64_t child_weight = (child.tail - child.head) +
891 ((int64_t) 1 << (link_width * 8)) * (vertices_[link.objidx].space + 1);
892 int64_t child_distance = next_distance + child_weight;
894 if (child_distance < vertices_[link.objidx].distance)
896 vertices_[link.objidx].distance = child_distance;
897 queue.insert (child_distance, link.objidx);
902 check_success (!queue.in_error ());
903 if (!check_success (queue.is_empty ()))
905 print_orphaned_nodes ();
909 distance_invalid = false;
912 int64_t compute_offset (
914 const hb_serialize_context_t::object_t::link_t& link) const
916 const auto& parent = vertices_[parent_idx];
917 const auto& child = vertices_[link.objidx];
919 switch ((hb_serialize_context_t::whence_t) link.whence) {
920 case hb_serialize_context_t::whence_t::Head:
921 offset = child.start - parent.start; break;
922 case hb_serialize_context_t::whence_t::Tail:
923 offset = child.start - parent.end; break;
924 case hb_serialize_context_t::whence_t::Absolute:
925 offset = child.start; break;
928 assert (offset >= link.bias);
933 bool is_valid_offset (int64_t offset,
934 const hb_serialize_context_t::object_t::link_t& link) const
936 if (unlikely (!link.width))
937 // Virtual links can't overflow.
938 return link.is_signed || offset >= 0;
943 return offset >= -((int64_t) 1 << 31) && offset < ((int64_t) 1 << 31);
945 return offset >= -(1 << 15) && offset < (1 << 15);
950 return offset >= 0 && offset < ((int64_t) 1 << 32);
951 else if (link.width == 3)
952 return offset >= 0 && offset < ((int32_t) 1 << 24);
954 return offset >= 0 && offset < (1 << 16);
959 * Updates a link in the graph to point to a different object. Corrects the
960 * parents vector on the previous and new child nodes.
962 void reassign_link (hb_serialize_context_t::object_t::link_t& link,
966 unsigned old_idx = link.objidx;
967 link.objidx = new_idx;
968 vertices_[old_idx].remove_parent (parent_idx);
969 vertices_[new_idx].parents.push (parent_idx);
973 * Updates all objidx's in all links using the provided mapping. Corrects incoming edge counts.
975 template<typename Iterator, hb_requires (hb_is_iterator (Iterator))>
976 void remap_obj_indices (const hb_hashmap_t<unsigned, unsigned>& id_map,
978 bool only_wide = false)
981 for (unsigned i : subgraph)
983 for (auto& link : vertices_[i].obj.all_links_writer ())
985 if (!id_map.has (link.objidx)) continue;
986 if (only_wide && !(link.width == 4 && !link.is_signed)) continue;
988 reassign_link (link, i, id_map[link.objidx]);
994 * Updates all objidx's in all links using the provided mapping.
996 void remap_all_obj_indices (const hb_vector_t<unsigned>& id_map,
997 hb_vector_t<vertex_t>* sorted_graph) const
999 for (unsigned i = 0; i < sorted_graph->length; i++)
1001 (*sorted_graph)[i].remap_parents (id_map);
1002 for (auto& link : (*sorted_graph)[i].obj.all_links_writer ())
1004 link.objidx = id_map[link.objidx];
1009 template <typename O> void
1010 serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link,
1012 hb_serialize_context_t* c) const
1014 OT::Offset<O>* offset = reinterpret_cast<OT::Offset<O>*> (head + link.position);
1016 c->add_link (*offset,
1017 // serializer has an extra nil object at the start of the
1018 // object array. So all id's are +1 of what our id's are.
1020 (hb_serialize_context_t::whence_t) link.whence,
1024 void serialize_link (const hb_serialize_context_t::object_t::link_t& link,
1026 hb_serialize_context_t* c) const
1031 // Virtual links aren't serialized.
1036 serialize_link_of_type<OT::HBINT32> (link, head, c);
1038 serialize_link_of_type<OT::HBUINT32> (link, head, c);
1044 serialize_link_of_type<OT::HBINT16> (link, head, c);
1046 serialize_link_of_type<OT::HBUINT16> (link, head, c);
1050 serialize_link_of_type<OT::HBUINT24> (link, head, c);
1053 // Unexpected link width.
1059 * Finds all nodes in targets that are reachable from start_idx, nodes in visited will be skipped.
1060 * For this search the graph is treated as being undirected.
1062 * Connected targets will be added to connected and removed from targets. All visited nodes
1063 * will be added to visited.
1065 void find_connected_nodes (unsigned start_idx,
1068 hb_set_t& connected)
1070 if (unlikely (!check_success (!visited.in_error ()))) return;
1071 if (visited.has (start_idx)) return;
1072 visited.add (start_idx);
1074 if (targets.has (start_idx))
1076 targets.del (start_idx);
1077 connected.add (start_idx);
1080 const auto& v = vertices_[start_idx];
1082 // Graph is treated as undirected so search children and parents of start_idx
1083 for (const auto& l : v.obj.all_links ())
1084 find_connected_nodes (l.objidx, targets, visited, connected);
1086 for (unsigned p : v.parents)
1087 find_connected_nodes (p, targets, visited, connected);
1091 // TODO(garretrieger): make private, will need to move most of offset overflow code into graph.
1092 hb_vector_t<vertex_t> vertices_;
1094 bool parents_invalid;
1095 bool distance_invalid;
1096 bool positions_invalid;
1098 hb_vector_t<unsigned> num_roots_for_space_;
1101 static bool _try_isolating_subgraphs (const hb_vector_t<graph_t::overflow_record_t>& overflows,
1102 graph_t& sorted_graph)
1105 hb_set_t roots_to_isolate;
1107 for (int i = overflows.length - 1; i >= 0; i--)
1109 const graph_t::overflow_record_t& r = overflows[i];
1112 unsigned overflow_space = sorted_graph.space_for (r.parent, &root);
1113 if (!overflow_space) continue;
1114 if (sorted_graph.num_roots_for_space (overflow_space) <= 1) continue;
1117 space = overflow_space;
1120 if (space == overflow_space)
1121 roots_to_isolate.add(root);
1124 if (!roots_to_isolate) return false;
1126 unsigned maximum_to_move = hb_max ((sorted_graph.num_roots_for_space (space) / 2u), 1u);
1127 if (roots_to_isolate.get_population () > maximum_to_move) {
1128 // Only move at most half of the roots in a space at a time.
1129 unsigned extra = roots_to_isolate.get_population () - maximum_to_move;
1131 unsigned root = HB_SET_VALUE_INVALID;
1132 roots_to_isolate.previous (&root);
1133 roots_to_isolate.del (root);
1137 DEBUG_MSG (SUBSET_REPACK, nullptr,
1138 "Overflow in space %d (%d roots). Moving %d roots to space %d.",
1140 sorted_graph.num_roots_for_space (space),
1141 roots_to_isolate.get_population (),
1142 sorted_graph.next_space ());
1144 sorted_graph.isolate_subgraph (roots_to_isolate);
1145 sorted_graph.move_to_new_space (roots_to_isolate);
1150 static bool _process_overflows (const hb_vector_t<graph_t::overflow_record_t>& overflows,
1151 hb_set_t& priority_bumped_parents,
1152 graph_t& sorted_graph)
1154 bool resolution_attempted = false;
1156 // Try resolving the furthest overflows first.
1157 for (int i = overflows.length - 1; i >= 0; i--)
1159 const graph_t::overflow_record_t& r = overflows[i];
1160 const auto& child = sorted_graph.vertices_[r.child];
1161 if (child.is_shared ())
1163 // The child object is shared, we may be able to eliminate the overflow
1164 // by duplicating it.
1165 if (!sorted_graph.duplicate (r.parent, r.child)) continue;
1169 if (child.is_leaf () && !priority_bumped_parents.has (r.parent))
1171 // This object is too far from it's parent, attempt to move it closer.
1173 // TODO(garretrieger): initially limiting this to leaf's since they can be
1174 // moved closer with fewer consequences. However, this can
1175 // likely can be used for non-leafs as well.
1176 // TODO(garretrieger): also try lowering priority of the parent. Make it
1177 // get placed further up in the ordering, closer to it's children.
1178 // this is probably preferable if the total size of the parent object
1179 // is < then the total size of the children (and the parent can be moved).
1180 // Since in that case moving the parent will cause a smaller increase in
1181 // the length of other offsets.
1182 if (sorted_graph.raise_childrens_priority (r.parent)) {
1183 priority_bumped_parents.add (r.parent);
1184 resolution_attempted = true;
1189 // TODO(garretrieger): add additional offset resolution strategies
1190 // - Promotion to extension lookups.
1191 // - Table splitting.
1194 return resolution_attempted;
1198 * Attempts to modify the topological sorting of the provided object graph to
1199 * eliminate offset overflows in the links between objects of the graph. If a
1200 * non-overflowing ordering is found the updated graph is serialized it into the
1201 * provided serialization context.
1203 * If necessary the structure of the graph may be modified in ways that do not
1204 * affect the functionality of the graph. For example shared objects may be
1207 * For a detailed writeup describing how the algorithm operates see:
1211 hb_resolve_overflows (const hb_vector_t<hb_serialize_context_t::object_t *>& packed,
1213 unsigned max_rounds = 20) {
1214 // Kahn sort is ~twice as fast as shortest distance sort and works for many fonts
1215 // so try it first to save time.
1216 graph_t sorted_graph (packed);
1217 sorted_graph.sort_kahn ();
1218 if (!sorted_graph.will_overflow ())
1220 return sorted_graph.serialize ();
1223 sorted_graph.sort_shortest_distance ();
1225 if ((table_tag == HB_OT_TAG_GPOS
1226 || table_tag == HB_OT_TAG_GSUB)
1227 && sorted_graph.will_overflow ())
1229 DEBUG_MSG (SUBSET_REPACK, nullptr, "Assigning spaces to 32 bit subgraphs.");
1230 if (sorted_graph.assign_32bit_spaces ())
1231 sorted_graph.sort_shortest_distance ();
1235 hb_vector_t<graph_t::overflow_record_t> overflows;
1236 // TODO(garretrieger): select a good limit for max rounds.
1237 while (!sorted_graph.in_error ()
1238 && sorted_graph.will_overflow (&overflows)
1239 && round++ < max_rounds) {
1240 DEBUG_MSG (SUBSET_REPACK, nullptr, "=== Overflow resolution round %d ===", round);
1241 sorted_graph.print_overflows (overflows);
1243 hb_set_t priority_bumped_parents;
1245 if (!_try_isolating_subgraphs (overflows, sorted_graph))
1247 if (!_process_overflows (overflows, priority_bumped_parents, sorted_graph))
1249 DEBUG_MSG (SUBSET_REPACK, nullptr, "No resolution available :(");
1254 sorted_graph.sort_shortest_distance ();
1257 if (sorted_graph.in_error ())
1259 DEBUG_MSG (SUBSET_REPACK, nullptr, "Sorted graph in error state.");
1263 if (sorted_graph.will_overflow ())
1265 DEBUG_MSG (SUBSET_REPACK, nullptr, "Offset overflow resolution failed.");
1269 return sorted_graph.serialize ();
1272 #endif /* HB_REPACKER_HH */