2020-10-29 01:49:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 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 HB_REPACKER_HH
|
|
|
|
#define HB_REPACKER_HH
|
|
|
|
|
|
|
|
#include "hb-open-type.hh"
|
2020-10-29 22:58:34 +01:00
|
|
|
#include "hb-map.hh"
|
2020-11-05 19:34:26 +01:00
|
|
|
#include "hb-priority-queue.hh"
|
2020-10-29 01:49:09 +01:00
|
|
|
#include "hb-serialize.hh"
|
|
|
|
#include "hb-vector.hh"
|
|
|
|
|
2021-10-12 22:13:32 +02:00
|
|
|
/*
|
|
|
|
* For a detailed writeup on the overflow resolution algorithm see:
|
2021-10-13 01:11:25 +02:00
|
|
|
* docs/repacker.md
|
2021-10-12 22:13:32 +02:00
|
|
|
*/
|
2020-10-29 01:49:09 +01:00
|
|
|
|
|
|
|
struct graph_t
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
struct vertex_t
|
|
|
|
{
|
2020-11-07 01:22:48 +01:00
|
|
|
vertex_t () :
|
|
|
|
distance (0),
|
2021-09-09 02:31:39 +02:00
|
|
|
space (0),
|
2021-09-25 00:58:57 +02:00
|
|
|
parents (),
|
2020-11-07 01:22:48 +01:00
|
|
|
start (0),
|
|
|
|
end (0),
|
|
|
|
priority(0) {}
|
|
|
|
|
2021-10-05 23:03:02 +02:00
|
|
|
void fini () {
|
|
|
|
obj.fini ();
|
|
|
|
parents.fini ();
|
|
|
|
}
|
2020-11-07 00:37:05 +01:00
|
|
|
|
|
|
|
hb_serialize_context_t::object_t obj;
|
|
|
|
int64_t distance;
|
2021-09-09 01:08:48 +02:00
|
|
|
int64_t space;
|
2021-09-25 00:58:57 +02:00
|
|
|
hb_vector_t<unsigned> parents;
|
2020-11-07 00:37:05 +01:00
|
|
|
unsigned start;
|
|
|
|
unsigned end;
|
2020-11-07 01:22:48 +01:00
|
|
|
unsigned priority;
|
|
|
|
|
|
|
|
bool is_shared () const
|
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
return parents.length > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned incoming_edges () const
|
|
|
|
{
|
|
|
|
return parents.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_parent (unsigned parent_index)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < parents.length; i++)
|
|
|
|
{
|
|
|
|
if (parents[i] != parent_index) continue;
|
|
|
|
parents.remove (i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remap_parents (const hb_vector_t<unsigned>& id_map)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < parents.length; i++)
|
|
|
|
parents[i] = id_map[parents[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
void remap_parent (unsigned old_index, unsigned new_index)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < parents.length; i++)
|
|
|
|
{
|
|
|
|
if (parents[i] == old_index)
|
|
|
|
parents[i] = new_index;
|
|
|
|
}
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_leaf () const
|
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
return !obj.real_links.length && !obj.virtual_links.length;
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 00:44:06 +01:00
|
|
|
bool raise_priority ()
|
2020-11-07 01:22:48 +01:00
|
|
|
{
|
2021-12-10 00:44:06 +01:00
|
|
|
if (has_max_priority ()) return false;
|
2020-11-07 01:22:48 +01:00
|
|
|
priority++;
|
2021-12-10 00:44:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_max_priority () const {
|
|
|
|
return priority >= 3;
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t modified_distance (unsigned order) const
|
|
|
|
{
|
|
|
|
// TODO(garretrieger): once priority is high enough, should try
|
|
|
|
// setting distance = 0 which will force to sort immediately after
|
|
|
|
// it's parent where possible.
|
2020-11-10 01:52:36 +01:00
|
|
|
|
2021-04-05 21:03:17 +02:00
|
|
|
int64_t modified_distance =
|
2021-12-07 00:23:35 +01:00
|
|
|
hb_min (hb_max(distance + distance_modifier (), 0), 0x7FFFFFFFFFF);
|
2021-12-10 00:44:06 +01:00
|
|
|
if (has_max_priority ()) {
|
|
|
|
modified_distance = 0;
|
|
|
|
}
|
2021-12-07 00:23:35 +01:00
|
|
|
return (modified_distance << 18) | (0x003FFFF & order);
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t distance_modifier () const
|
|
|
|
{
|
|
|
|
if (!priority) return 0;
|
|
|
|
int64_t table_size = obj.tail - obj.head;
|
2021-12-10 00:44:06 +01:00
|
|
|
|
|
|
|
if (priority == 1)
|
|
|
|
return -table_size / 2;
|
|
|
|
|
|
|
|
return -table_size;
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
2020-11-07 00:37:05 +01:00
|
|
|
};
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
struct overflow_record_t
|
|
|
|
{
|
|
|
|
unsigned parent;
|
2021-09-17 22:46:07 +02:00
|
|
|
unsigned child;
|
2020-11-06 01:39:23 +01:00
|
|
|
};
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
/*
|
|
|
|
* A topological sorting of an object graph. Ordered
|
|
|
|
* in reverse serialization order (first object in the
|
2020-10-30 18:29:51 +01:00
|
|
|
* serialization is at the end of the list). This matches
|
2020-10-29 01:49:09 +01:00
|
|
|
* the 'packed' object stack used internally in the
|
|
|
|
* serializer
|
|
|
|
*/
|
|
|
|
graph_t (const hb_vector_t<hb_serialize_context_t::object_t *>& objects)
|
2021-09-25 00:58:57 +02:00
|
|
|
: parents_invalid (true),
|
2020-11-07 00:37:05 +01:00
|
|
|
distance_invalid (true),
|
2020-11-10 23:11:57 +01:00
|
|
|
positions_invalid (true),
|
|
|
|
successful (true)
|
2020-10-29 22:58:34 +01:00
|
|
|
{
|
2021-10-05 19:53:05 +02:00
|
|
|
num_roots_for_space_.push (1);
|
2020-10-29 22:58:34 +01:00
|
|
|
bool removed_nil = false;
|
|
|
|
for (unsigned i = 0; i < objects.length; i++)
|
|
|
|
{
|
2020-11-02 23:51:39 +01:00
|
|
|
// TODO(grieger): check all links point to valid objects.
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
// If this graph came from a serialization buffer object 0 is the
|
|
|
|
// nil object. We don't need it for our purposes here so drop it.
|
|
|
|
if (i == 0 && !objects[i])
|
|
|
|
{
|
|
|
|
removed_nil = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t* v = vertices_.push ();
|
2021-03-31 20:23:46 +02:00
|
|
|
if (check_success (!vertices_.in_error ()))
|
|
|
|
v->obj = *objects[i];
|
2020-10-29 22:58:34 +01:00
|
|
|
if (!removed_nil) continue;
|
2021-11-30 22:45:22 +01:00
|
|
|
// Fix indices to account for removed nil object.
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& l : v->obj.all_links_writer ()) {
|
2021-11-30 22:45:22 +01:00
|
|
|
l.objidx--;
|
|
|
|
}
|
2020-10-29 22:58:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~graph_t ()
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
vertices_.fini_deep ();
|
2020-10-29 22:58:34 +01:00
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
bool in_error () const
|
|
|
|
{
|
2021-10-05 19:53:05 +02:00
|
|
|
return !successful ||
|
|
|
|
vertices_.in_error () ||
|
|
|
|
num_roots_for_space_.in_error ();
|
2020-11-10 23:11:57 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
const vertex_t& root () const
|
|
|
|
{
|
|
|
|
return vertices_[root_idx ()];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned root_idx () const
|
|
|
|
{
|
|
|
|
// Object graphs are in reverse order, the first object is at the end
|
|
|
|
// of the vector. Since the graph is topologically sorted it's safe to
|
|
|
|
// assume the first object has no incoming edges.
|
|
|
|
return vertices_.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hb_serialize_context_t::object_t& object(unsigned i) const
|
|
|
|
{
|
|
|
|
return vertices_[i].obj;
|
|
|
|
}
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
/*
|
|
|
|
* serialize graph into the provided serialization buffer.
|
|
|
|
*/
|
2021-12-06 21:54:19 +01:00
|
|
|
hb_blob_t* serialize () const
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2021-12-06 21:54:19 +01:00
|
|
|
hb_vector_t<char> buffer;
|
|
|
|
size_t size = serialized_length ();
|
|
|
|
if (!buffer.alloc (size)) {
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Unable to allocate output buffer.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
hb_serialize_context_t c((void *) buffer, size);
|
|
|
|
|
|
|
|
c.start_serialize<void> ();
|
2020-11-07 00:37:05 +01:00
|
|
|
for (unsigned i = 0; i < vertices_.length; i++) {
|
2021-12-06 21:54:19 +01:00
|
|
|
c.push ();
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
size_t size = vertices_[i].obj.tail - vertices_[i].obj.head;
|
2021-12-06 21:54:19 +01:00
|
|
|
char* start = c.allocate_size <char> (size);
|
|
|
|
if (!start) {
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Buffer out of space.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
memcpy (start, vertices_[i].obj.head, size);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
// Only real links needs to be serialized.
|
|
|
|
for (const auto& link : vertices_[i].obj.real_links)
|
2021-12-06 21:54:19 +01:00
|
|
|
serialize_link (link, start, &c);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
// All duplications are already encoded in the graph, so don't
|
|
|
|
// enable sharing during packing.
|
2021-12-06 21:54:19 +01:00
|
|
|
c.pop_pack (false);
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
2021-12-06 21:54:19 +01:00
|
|
|
c.end_serialize ();
|
|
|
|
|
|
|
|
if (c.in_error ()) {
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Error during serialization. Err flag: %d",
|
|
|
|
c.errors);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.copy_blob ();
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-10-30 18:29:51 +01:00
|
|
|
* Generates a new topological sorting of graph using Kahn's
|
|
|
|
* algorithm: https://en.wikipedia.org/wiki/Topological_sorting#Algorithms
|
2020-10-29 01:49:09 +01:00
|
|
|
*/
|
2020-10-30 18:29:51 +01:00
|
|
|
void sort_kahn ()
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
positions_invalid = true;
|
|
|
|
|
|
|
|
if (vertices_.length <= 1) {
|
2020-10-29 22:58:34 +01:00
|
|
|
// Graph of 1 or less doesn't need sorting.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_vector_t<unsigned> queue;
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_vector_t<vertex_t> sorted_graph;
|
2021-10-06 20:12:32 +02:00
|
|
|
if (unlikely (!check_success (sorted_graph.resize (vertices_.length)))) return;
|
2020-11-05 23:23:29 +01:00
|
|
|
hb_vector_t<unsigned> id_map;
|
2021-04-20 02:18:05 +02:00
|
|
|
if (unlikely (!check_success (id_map.resize (vertices_.length)))) return;
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_vector_t<unsigned> removed_edges;
|
2021-04-20 02:18:05 +02:00
|
|
|
if (unlikely (!check_success (removed_edges.resize (vertices_.length)))) return;
|
2021-09-25 00:58:57 +02:00
|
|
|
update_parents ();
|
2020-11-07 00:37:05 +01:00
|
|
|
|
|
|
|
queue.push (root_idx ());
|
|
|
|
int new_id = vertices_.length - 1;
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
while (!queue.in_error () && queue.length)
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2020-10-29 22:58:34 +01:00
|
|
|
unsigned next_id = queue[0];
|
2020-11-10 23:11:57 +01:00
|
|
|
queue.remove (0);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t& next = vertices_[next_id];
|
2021-10-06 20:12:32 +02:00
|
|
|
sorted_graph[new_id] = next;
|
2020-11-05 23:23:29 +01:00
|
|
|
id_map[next_id] = new_id--;
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& link : next.obj.all_links ()) {
|
2020-11-07 00:37:05 +01:00
|
|
|
removed_edges[link.objidx]++;
|
2021-09-25 00:58:57 +02:00
|
|
|
if (!(vertices_[link.objidx].incoming_edges () - removed_edges[link.objidx]))
|
2020-10-29 01:49:09 +01:00
|
|
|
queue.push (link.objidx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
check_success (!queue.in_error ());
|
|
|
|
check_success (!sorted_graph.in_error ());
|
|
|
|
if (!check_success (new_id == -1))
|
2021-10-05 19:53:05 +02:00
|
|
|
print_orphaned_nodes ();
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
remap_all_obj_indices (id_map, &sorted_graph);
|
2020-11-10 01:52:36 +01:00
|
|
|
|
2021-11-02 00:59:17 +01:00
|
|
|
hb_swap (vertices_, sorted_graph);
|
2020-10-29 22:58:34 +01:00
|
|
|
sorted_graph.fini_deep ();
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:16:27 +01:00
|
|
|
/*
|
|
|
|
* Generates a new topological sorting of graph ordered by the shortest
|
|
|
|
* distance to each node.
|
|
|
|
*/
|
|
|
|
void sort_shortest_distance ()
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
positions_invalid = true;
|
|
|
|
|
|
|
|
if (vertices_.length <= 1) {
|
2020-11-03 01:16:27 +01:00
|
|
|
// Graph of 1 or less doesn't need sorting.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
update_distances ();
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-05 20:22:16 +01:00
|
|
|
hb_priority_queue_t queue;
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_vector_t<vertex_t> sorted_graph;
|
2021-10-06 20:12:32 +02:00
|
|
|
if (unlikely (!check_success (sorted_graph.resize (vertices_.length)))) return;
|
2020-11-05 23:23:29 +01:00
|
|
|
hb_vector_t<unsigned> id_map;
|
2021-04-20 02:18:05 +02:00
|
|
|
if (unlikely (!check_success (id_map.resize (vertices_.length)))) return;
|
2020-11-07 00:37:05 +01:00
|
|
|
|
|
|
|
hb_vector_t<unsigned> removed_edges;
|
2021-04-20 02:18:05 +02:00
|
|
|
if (unlikely (!check_success (removed_edges.resize (vertices_.length)))) return;
|
2021-09-25 00:58:57 +02:00
|
|
|
update_parents ();
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2021-03-18 19:13:47 +01:00
|
|
|
queue.insert (root ().modified_distance (0), root_idx ());
|
2020-11-07 00:37:05 +01:00
|
|
|
int new_id = root_idx ();
|
2020-11-05 20:22:16 +01:00
|
|
|
unsigned order = 1;
|
2020-11-10 23:11:57 +01:00
|
|
|
while (!queue.in_error () && !queue.is_empty ())
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2021-03-18 19:13:47 +01:00
|
|
|
unsigned next_id = queue.pop_minimum().second;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t& next = vertices_[next_id];
|
2021-10-06 20:12:32 +02:00
|
|
|
sorted_graph[new_id] = next;
|
2020-11-05 23:23:29 +01:00
|
|
|
id_map[next_id] = new_id--;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& link : next.obj.all_links ()) {
|
2020-11-07 00:37:05 +01:00
|
|
|
removed_edges[link.objidx]++;
|
2021-09-25 00:58:57 +02:00
|
|
|
if (!(vertices_[link.objidx].incoming_edges () - removed_edges[link.objidx]))
|
2020-11-05 20:22:16 +01:00
|
|
|
// Add the order that the links were encountered to the priority.
|
|
|
|
// This ensures that ties between priorities objects are broken in a consistent
|
|
|
|
// way. More specifically this is set up so that if a set of objects have the same
|
2020-11-11 22:50:18 +01:00
|
|
|
// distance they'll be added to the topological order in the order that they are
|
2020-11-05 20:22:16 +01:00
|
|
|
// referenced from the parent object.
|
2021-03-18 19:13:47 +01:00
|
|
|
queue.insert (vertices_[link.objidx].modified_distance (order++),
|
|
|
|
link.objidx);
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
check_success (!queue.in_error ());
|
|
|
|
check_success (!sorted_graph.in_error ());
|
|
|
|
if (!check_success (new_id == -1))
|
2021-10-05 19:53:05 +02:00
|
|
|
print_orphaned_nodes ();
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
remap_all_obj_indices (id_map, &sorted_graph);
|
2020-11-10 01:52:36 +01:00
|
|
|
|
2021-11-02 00:59:17 +01:00
|
|
|
hb_swap (vertices_, sorted_graph);
|
2020-11-03 01:16:27 +01:00
|
|
|
sorted_graph.fini_deep ();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
/*
|
2021-09-28 21:08:18 +02:00
|
|
|
* Assign unique space numbers to each connected subgraph of 32 bit offset(s).
|
2020-11-06 01:39:23 +01:00
|
|
|
*/
|
2021-09-28 21:08:18 +02:00
|
|
|
bool assign_32bit_spaces ()
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
2021-09-08 19:14:00 +02:00
|
|
|
unsigned root_index = root_idx ();
|
2021-09-28 21:08:18 +02:00
|
|
|
hb_set_t visited;
|
|
|
|
hb_set_t roots;
|
2021-09-09 00:07:02 +02:00
|
|
|
for (unsigned i = 0; i <= root_index; i++)
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
// Only real links can form 32 bit spaces
|
|
|
|
for (auto& l : vertices_[i].obj.real_links)
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
|
|
|
if (l.width == 4 && !l.is_signed)
|
2021-09-09 01:08:48 +02:00
|
|
|
{
|
2021-09-28 21:08:18 +02:00
|
|
|
roots.add (l.objidx);
|
2021-09-29 01:04:27 +02:00
|
|
|
find_subgraph (l.objidx, visited);
|
2021-09-09 01:08:48 +02:00
|
|
|
}
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 21:08:18 +02:00
|
|
|
|
2021-09-29 01:04:27 +02:00
|
|
|
// Mark everything not in the subgraphs of 32 bit roots as visited.
|
|
|
|
// This prevents 32 bit subgraphs from being connected via nodes not in the 32 bit subgraphs.
|
|
|
|
visited.invert ();
|
|
|
|
|
2021-09-28 21:08:18 +02:00
|
|
|
if (!roots) return false;
|
|
|
|
|
|
|
|
while (roots)
|
|
|
|
{
|
|
|
|
unsigned next = HB_SET_VALUE_INVALID;
|
|
|
|
if (!roots.next (&next)) break;
|
|
|
|
|
|
|
|
hb_set_t connected_roots;
|
|
|
|
find_connected_nodes (next, roots, visited, connected_roots);
|
2021-10-05 19:53:05 +02:00
|
|
|
isolate_subgraph (connected_roots);
|
2021-09-30 03:14:57 +02:00
|
|
|
|
|
|
|
unsigned next_space = this->next_space ();
|
2021-10-05 19:53:05 +02:00
|
|
|
num_roots_for_space_.push (0);
|
2021-09-28 21:08:18 +02:00
|
|
|
for (unsigned root : connected_roots)
|
|
|
|
{
|
2021-09-30 03:14:57 +02:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Subgraph %u gets space %u", root, next_space);
|
|
|
|
vertices_[root].space = next_space;
|
2021-10-05 19:53:05 +02:00
|
|
|
num_roots_for_space_[next_space] = num_roots_for_space_[next_space] + 1;
|
2021-09-28 21:08:18 +02:00
|
|
|
distance_invalid = true;
|
|
|
|
positions_invalid = true;
|
|
|
|
}
|
2021-10-05 19:53:05 +02:00
|
|
|
|
2021-09-28 22:36:06 +02:00
|
|
|
// TODO(grieger): special case for GSUB/GPOS use extension promotions to move 16 bit space
|
|
|
|
// into the 32 bit space as needed, instead of using isolation.
|
2021-09-28 21:08:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Isolates the subgraph of nodes reachable from root. Any links to nodes in the subgraph
|
|
|
|
* that originate from outside of the subgraph will be removed by duplicating the linked to
|
|
|
|
* object.
|
2021-10-05 19:53:05 +02:00
|
|
|
*
|
|
|
|
* Indices stored in roots will be updated if any of the roots are duplicated to new indices.
|
2021-09-08 01:32:13 +02:00
|
|
|
*/
|
2021-10-05 19:53:05 +02:00
|
|
|
bool isolate_subgraph (hb_set_t& roots)
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
update_parents ();
|
2021-09-08 01:32:13 +02:00
|
|
|
hb_hashmap_t<unsigned, unsigned> subgraph;
|
2021-09-10 23:55:24 +02:00
|
|
|
|
|
|
|
// incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these
|
|
|
|
// set the subgraph incoming edge count to match all of root_idx's incoming edges
|
2021-09-29 23:28:27 +02:00
|
|
|
hb_set_t parents;
|
2021-09-28 22:36:06 +02:00
|
|
|
for (unsigned root_idx : roots)
|
|
|
|
{
|
2021-09-29 23:28:27 +02:00
|
|
|
subgraph.set (root_idx, wide_parents (root_idx, parents));
|
2021-09-28 22:36:06 +02:00
|
|
|
find_subgraph (root_idx, subgraph);
|
|
|
|
}
|
2021-09-08 01:32:13 +02:00
|
|
|
|
2021-09-29 23:28:27 +02:00
|
|
|
unsigned original_root_idx = root_idx ();
|
2021-09-10 23:55:24 +02:00
|
|
|
hb_hashmap_t<unsigned, unsigned> index_map;
|
2021-09-08 19:14:00 +02:00
|
|
|
bool made_changes = false;
|
2021-09-08 01:32:13 +02:00
|
|
|
for (auto entry : subgraph.iter ())
|
|
|
|
{
|
|
|
|
const auto& node = vertices_[entry.first];
|
|
|
|
unsigned subgraph_incoming_edges = entry.second;
|
2020-11-07 01:22:48 +01:00
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
if (subgraph_incoming_edges < node.incoming_edges ())
|
2021-09-08 19:14:00 +02:00
|
|
|
{
|
2021-09-08 01:32:13 +02:00
|
|
|
// Only de-dup objects with incoming links from outside the subgraph.
|
2021-09-08 19:14:00 +02:00
|
|
|
made_changes = true;
|
2021-09-08 22:44:25 +02:00
|
|
|
duplicate_subgraph (entry.first, index_map);
|
2021-09-08 19:14:00 +02:00
|
|
|
}
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:14:00 +02:00
|
|
|
if (!made_changes)
|
|
|
|
return false;
|
|
|
|
|
2021-09-29 23:28:27 +02:00
|
|
|
if (original_root_idx != root_idx ()
|
|
|
|
&& parents.has (original_root_idx))
|
|
|
|
{
|
|
|
|
// If the root idx has changed since parents was determined, update root idx in parents
|
|
|
|
parents.add (root_idx ());
|
|
|
|
parents.del (original_root_idx);
|
|
|
|
}
|
|
|
|
|
2021-09-25 01:28:34 +02:00
|
|
|
auto new_subgraph =
|
|
|
|
+ subgraph.keys ()
|
|
|
|
| hb_map([&] (unsigned node_idx) {
|
|
|
|
if (index_map.has (node_idx)) return index_map[node_idx];
|
|
|
|
return node_idx;
|
|
|
|
})
|
|
|
|
;
|
2021-09-29 23:28:27 +02:00
|
|
|
|
2021-09-25 01:28:34 +02:00
|
|
|
remap_obj_indices (index_map, new_subgraph);
|
2021-10-05 21:46:59 +02:00
|
|
|
remap_obj_indices (index_map, parents.iter (), true);
|
2021-09-29 23:28:27 +02:00
|
|
|
|
2021-10-05 19:53:05 +02:00
|
|
|
// Update roots set with new indices as needed.
|
|
|
|
unsigned next = HB_SET_VALUE_INVALID;
|
|
|
|
while (roots.next (&next))
|
|
|
|
{
|
|
|
|
if (index_map.has (next))
|
|
|
|
{
|
|
|
|
roots.del (next);
|
|
|
|
roots.add (index_map[next]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:14:00 +02:00
|
|
|
return true;
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void find_subgraph (unsigned node_idx, hb_hashmap_t<unsigned, unsigned>& subgraph)
|
|
|
|
{
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& link : vertices_[node_idx].obj.all_links ())
|
2021-09-10 23:55:24 +02:00
|
|
|
{
|
|
|
|
if (subgraph.has (link.objidx))
|
|
|
|
{
|
|
|
|
subgraph.set (link.objidx, subgraph[link.objidx] + 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
subgraph.set (link.objidx, 1);
|
2021-09-08 01:32:13 +02:00
|
|
|
find_subgraph (link.objidx, subgraph);
|
2021-09-10 23:55:24 +02:00
|
|
|
}
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:04:27 +02:00
|
|
|
void find_subgraph (unsigned node_idx, hb_set_t& subgraph)
|
|
|
|
{
|
|
|
|
if (subgraph.has (node_idx)) return;
|
|
|
|
subgraph.add (node_idx);
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& link : vertices_[node_idx].obj.all_links ())
|
2021-09-29 01:04:27 +02:00
|
|
|
find_subgraph (link.objidx, subgraph);
|
|
|
|
}
|
|
|
|
|
2021-09-08 22:44:25 +02:00
|
|
|
/*
|
|
|
|
* duplicates all nodes in the subgraph reachable from node_idx. Does not re-assign
|
|
|
|
* links. index_map is updated with mappings from old id to new id. If a duplication has already
|
|
|
|
* been performed for a given index, then it will be skipped.
|
|
|
|
*/
|
|
|
|
void duplicate_subgraph (unsigned node_idx, hb_hashmap_t<unsigned, unsigned>& index_map)
|
|
|
|
{
|
|
|
|
if (index_map.has (node_idx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
index_map.set (node_idx, duplicate (node_idx));
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& l : object (node_idx).all_links ()) {
|
2021-09-08 22:44:25 +02:00
|
|
|
duplicate_subgraph (l.objidx, index_map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:32:13 +02:00
|
|
|
/*
|
|
|
|
* Creates a copy of node_idx and returns it's new index.
|
|
|
|
*/
|
|
|
|
unsigned duplicate (unsigned node_idx)
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
positions_invalid = true;
|
2021-09-09 00:07:02 +02:00
|
|
|
distance_invalid = true;
|
2020-11-07 00:37:05 +01:00
|
|
|
|
2020-11-10 01:52:36 +01:00
|
|
|
auto* clone = vertices_.push ();
|
2021-09-08 01:32:13 +02:00
|
|
|
auto& child = vertices_[node_idx];
|
2021-10-06 19:51:45 +02:00
|
|
|
if (vertices_.in_error ()) {
|
2021-09-08 01:32:13 +02:00
|
|
|
return -1;
|
2020-11-10 23:11:57 +01:00
|
|
|
}
|
2020-11-07 00:37:05 +01:00
|
|
|
|
2021-10-06 19:51:45 +02:00
|
|
|
clone->obj.head = child.obj.head;
|
|
|
|
clone->obj.tail = child.obj.tail;
|
2020-11-07 00:37:05 +01:00
|
|
|
clone->distance = child.distance;
|
2021-09-09 02:31:39 +02:00
|
|
|
clone->space = child.space;
|
2021-09-25 00:58:57 +02:00
|
|
|
clone->parents.reset ();
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
unsigned clone_idx = vertices_.length - 2;
|
2021-11-30 22:45:22 +01:00
|
|
|
for (const auto& l : child.obj.real_links)
|
2021-09-09 18:57:42 +02:00
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
clone->obj.real_links.push (l);
|
|
|
|
vertices_[l.objidx].parents.push (clone_idx);
|
|
|
|
}
|
|
|
|
for (const auto& l : child.obj.virtual_links)
|
|
|
|
{
|
|
|
|
clone->obj.virtual_links.push (l);
|
2021-09-25 00:58:57 +02:00
|
|
|
vertices_[l.objidx].parents.push (clone_idx);
|
2021-09-09 18:57:42 +02:00
|
|
|
}
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
check_success (!clone->obj.real_links.in_error ());
|
|
|
|
check_success (!clone->obj.virtual_links.in_error ());
|
2020-11-10 23:11:57 +01:00
|
|
|
|
2021-09-08 01:32:13 +02:00
|
|
|
// The last object is the root of the graph, so swap back the root to the end.
|
|
|
|
// The root's obj idx does change, however since it's root nothing else refers to it.
|
|
|
|
// all other obj idx's will be unaffected.
|
|
|
|
vertex_t root = vertices_[vertices_.length - 2];
|
2021-09-25 00:58:57 +02:00
|
|
|
vertices_[clone_idx] = *clone;
|
2021-09-08 01:32:13 +02:00
|
|
|
vertices_[vertices_.length - 1] = root;
|
|
|
|
|
2021-09-29 23:28:27 +02:00
|
|
|
// Since the root moved, update the parents arrays of all children on the root.
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& l : root.obj.all_links ())
|
2021-09-29 23:28:27 +02:00
|
|
|
vertices_[l.objidx].remap_parent (root_idx () - 1, root_idx ());
|
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
return clone_idx;
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates a copy of child and re-assigns the link from
|
|
|
|
* parent to the clone. The copy is a shallow copy, objects
|
|
|
|
* linked from child are not duplicated.
|
|
|
|
*/
|
2021-09-10 23:55:24 +02:00
|
|
|
bool duplicate (unsigned parent_idx, unsigned child_idx)
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
update_parents ();
|
2021-09-10 23:55:24 +02:00
|
|
|
|
|
|
|
unsigned links_to_child = 0;
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& l : vertices_[parent_idx].obj.all_links ())
|
2021-09-10 23:55:24 +02:00
|
|
|
{
|
|
|
|
if (l.objidx == child_idx) links_to_child++;
|
|
|
|
}
|
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
if (vertices_[child_idx].incoming_edges () <= links_to_child)
|
2021-09-10 23:55:24 +02:00
|
|
|
{
|
|
|
|
// Can't duplicate this node, doing so would orphan the original one as all remaining links
|
|
|
|
// to child are from parent.
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %d => %d",
|
|
|
|
parent_idx, child_idx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:32:13 +02:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " Duplicating %d => %d",
|
|
|
|
parent_idx, child_idx);
|
|
|
|
|
|
|
|
unsigned clone_idx = duplicate (child_idx);
|
2021-09-10 23:55:24 +02:00
|
|
|
if (clone_idx == (unsigned) -1) return false;
|
2021-09-08 01:32:13 +02:00
|
|
|
// duplicate shifts the root node idx, so if parent_idx was root update it.
|
|
|
|
if (parent_idx == clone_idx) parent_idx++;
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
auto& parent = vertices_[parent_idx];
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& l : parent.obj.all_links_writer ())
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
if (l.objidx != child_idx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
reassign_link (l, parent_idx, clone_idx);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
2021-09-10 23:55:24 +02:00
|
|
|
|
|
|
|
return true;
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
/*
|
|
|
|
* Raises the sorting priority of all children.
|
|
|
|
*/
|
2021-12-10 00:44:06 +01:00
|
|
|
bool raise_childrens_priority (unsigned parent_idx)
|
2020-11-07 01:22:48 +01:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " Raising priority of all children of %d",
|
|
|
|
parent_idx);
|
|
|
|
// This operation doesn't change ordering until a sort is run, so no need
|
|
|
|
// to invalidate positions. It does not change graph structure so no need
|
|
|
|
// to update distances or edge counts.
|
|
|
|
auto& parent = vertices_[parent_idx].obj;
|
2021-12-10 00:44:06 +01:00
|
|
|
bool made_change = false;
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& l : parent.all_links_writer ())
|
2021-12-10 00:44:06 +01:00
|
|
|
made_change |= vertices_[l.objidx].raise_priority ();
|
|
|
|
return made_change;
|
2020-11-07 01:22:48 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
/*
|
|
|
|
* Will any offsets overflow on graph when it's serialized?
|
|
|
|
*/
|
2020-11-07 01:22:48 +01:00
|
|
|
bool will_overflow (hb_vector_t<overflow_record_t>* overflows = nullptr)
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2020-11-06 01:39:23 +01:00
|
|
|
if (overflows) overflows->resize (0);
|
2020-11-07 00:37:05 +01:00
|
|
|
update_positions ();
|
2020-11-02 23:51:39 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (int parent_idx = vertices_.length - 1; parent_idx >= 0; parent_idx--)
|
2020-11-02 23:51:39 +01:00
|
|
|
{
|
2021-11-30 22:45:22 +01:00
|
|
|
// Don't need to check virtual links for overflow
|
|
|
|
for (const auto& link : vertices_[parent_idx].obj.real_links)
|
2020-11-02 23:51:39 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
int64_t offset = compute_offset (parent_idx, link);
|
2020-11-06 01:39:23 +01:00
|
|
|
if (is_valid_offset (offset, link))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!overflows) return true;
|
|
|
|
|
|
|
|
overflow_record_t r;
|
|
|
|
r.parent = parent_idx;
|
2021-09-17 22:46:07 +02:00
|
|
|
r.child = link.objidx;
|
2020-11-06 01:39:23 +01:00
|
|
|
overflows->push (r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overflows) return false;
|
|
|
|
return overflows->length;
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:53:05 +02:00
|
|
|
void print_orphaned_nodes ()
|
|
|
|
{
|
|
|
|
if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
|
|
|
|
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Graph is not fully connected.");
|
|
|
|
parents_invalid = true;
|
|
|
|
update_parents();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < root_idx (); i++)
|
|
|
|
{
|
|
|
|
const auto& v = vertices_[i];
|
|
|
|
if (!v.parents)
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Node %u is orphaned.", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
void print_overflows (const hb_vector_t<overflow_record_t>& overflows)
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
|
|
|
if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
|
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
update_parents ();
|
2021-12-10 00:44:06 +01:00
|
|
|
int limit = 10;
|
2020-11-06 01:39:23 +01:00
|
|
|
for (const auto& o : overflows)
|
|
|
|
{
|
2021-12-10 00:44:06 +01:00
|
|
|
if (!limit--) break;
|
2021-09-07 22:22:19 +02:00
|
|
|
const auto& parent = vertices_[o.parent];
|
2021-09-17 22:46:07 +02:00
|
|
|
const auto& child = vertices_[o.child];
|
2021-09-07 22:22:19 +02:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr,
|
2021-09-30 03:14:57 +02:00
|
|
|
" overflow from "
|
|
|
|
"%4d (%4d in, %4d out, space %2d) => "
|
|
|
|
"%4d (%4d in, %4d out, space %2d)",
|
2020-11-06 01:39:23 +01:00
|
|
|
o.parent,
|
2021-09-25 00:58:57 +02:00
|
|
|
parent.incoming_edges (),
|
2021-11-30 22:45:22 +01:00
|
|
|
parent.obj.real_links.length + parent.obj.virtual_links.length,
|
2021-09-30 03:14:57 +02:00
|
|
|
space_for (o.parent),
|
2021-09-17 22:46:07 +02:00
|
|
|
o.child,
|
2021-09-25 00:58:57 +02:00
|
|
|
child.incoming_edges (),
|
2021-11-30 22:45:22 +01:00
|
|
|
child.obj.real_links.length + child.obj.virtual_links.length,
|
2021-09-30 03:14:57 +02:00
|
|
|
space_for (o.child));
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
2021-12-10 00:44:06 +01:00
|
|
|
if (overflows.length > 10) {
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " ... plus %d more overflows.", overflows.length - 10);
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 19:53:05 +02:00
|
|
|
unsigned num_roots_for_space (unsigned space) const
|
2021-09-30 03:14:57 +02:00
|
|
|
{
|
2021-10-05 19:53:05 +02:00
|
|
|
return num_roots_for_space_[space];
|
2021-09-30 03:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned next_space () const
|
|
|
|
{
|
2021-10-05 19:53:05 +02:00
|
|
|
return num_roots_for_space_.length;
|
2021-09-30 03:14:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 00:23:35 +01:00
|
|
|
void move_to_new_space (const hb_set_t& indices)
|
2021-09-30 03:14:57 +02:00
|
|
|
{
|
2021-12-07 00:23:35 +01:00
|
|
|
num_roots_for_space_.push (0);
|
|
|
|
unsigned new_space = num_roots_for_space_.length - 1;
|
|
|
|
|
|
|
|
for (unsigned index : indices) {
|
|
|
|
auto& node = vertices_[index];
|
|
|
|
num_roots_for_space_[node.space] = num_roots_for_space_[node.space] - 1;
|
|
|
|
num_roots_for_space_[new_space] = num_roots_for_space_[new_space] + 1;
|
|
|
|
node.space = new_space;
|
|
|
|
}
|
2021-09-30 03:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned space_for (unsigned index, unsigned* root = nullptr) const
|
|
|
|
{
|
|
|
|
const auto& node = vertices_[index];
|
|
|
|
if (node.space)
|
|
|
|
{
|
|
|
|
if (root != nullptr)
|
|
|
|
*root = index;
|
|
|
|
return node.space;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node.parents)
|
|
|
|
{
|
|
|
|
if (root)
|
|
|
|
*root = index;
|
|
|
|
return 0;
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
2021-09-30 03:14:57 +02:00
|
|
|
|
|
|
|
return space_for (node.parents[0], root);
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
void err_other_error () { this->successful = false; }
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
private:
|
|
|
|
|
2021-12-06 21:54:19 +01:00
|
|
|
size_t serialized_length () const {
|
|
|
|
size_t total_size = 0;
|
|
|
|
for (unsigned i = 0; i < vertices_.length; i++) {
|
|
|
|
size_t size = vertices_[i].obj.tail - vertices_[i].obj.head;
|
|
|
|
total_size += size;
|
|
|
|
}
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2021-09-29 23:28:27 +02:00
|
|
|
/*
|
|
|
|
* Returns the numbers of incoming edges that are 32bits wide.
|
|
|
|
*/
|
|
|
|
unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
2021-10-05 21:46:59 +02:00
|
|
|
hb_set_t visited;
|
2021-09-29 23:28:27 +02:00
|
|
|
for (unsigned p : vertices_[node_idx].parents)
|
|
|
|
{
|
2021-10-05 21:46:59 +02:00
|
|
|
if (visited.has (p)) continue;
|
|
|
|
visited.add (p);
|
|
|
|
|
2021-11-30 22:45:22 +01:00
|
|
|
// Only real links can be wide
|
|
|
|
for (const auto& l : vertices_[p].obj.real_links)
|
2021-09-29 23:28:27 +02:00
|
|
|
{
|
|
|
|
if (l.objidx == node_idx && l.width == 4 && !l.is_signed)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
parents.add (p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
bool check_success (bool success)
|
|
|
|
{ return this->successful && (success || (err_other_error (), false)); }
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
/*
|
|
|
|
* Creates a map from objid to # of incoming edges.
|
|
|
|
*/
|
2021-09-25 00:58:57 +02:00
|
|
|
void update_parents ()
|
2020-11-07 00:37:05 +01:00
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
if (!parents_invalid) return;
|
2020-11-07 00:37:05 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < vertices_.length; i++)
|
2021-09-25 00:58:57 +02:00
|
|
|
vertices_[i].parents.reset ();
|
2020-11-07 00:37:05 +01:00
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
for (unsigned p = 0; p < vertices_.length; p++)
|
2020-11-07 00:37:05 +01:00
|
|
|
{
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& l : vertices_[p].obj.all_links ())
|
2020-11-07 00:37:05 +01:00
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
vertices_[l.objidx].parents.push (p);
|
2020-11-07 00:37:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
parents_invalid = false;
|
2020-11-07 00:37:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compute the serialized start and end positions for each vertex.
|
|
|
|
*/
|
|
|
|
void update_positions ()
|
|
|
|
{
|
|
|
|
if (!positions_invalid) return;
|
|
|
|
|
|
|
|
unsigned current_pos = 0;
|
|
|
|
for (int i = root_idx (); i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto& v = vertices_[i];
|
|
|
|
v.start = current_pos;
|
|
|
|
current_pos += v.obj.tail - v.obj.head;
|
|
|
|
v.end = current_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
positions_invalid = false;
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:16:27 +01:00
|
|
|
/*
|
2020-11-11 22:50:18 +01:00
|
|
|
* Finds the distance to each object in the graph
|
2020-11-03 01:16:27 +01:00
|
|
|
* from the initial node.
|
|
|
|
*/
|
2020-11-07 00:37:05 +01:00
|
|
|
void update_distances ()
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
if (!distance_invalid) return;
|
|
|
|
|
2020-11-03 01:16:27 +01:00
|
|
|
// Uses Dijkstra's algorithm to find all of the shortest distances.
|
|
|
|
// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
2020-11-05 19:34:26 +01:00
|
|
|
//
|
|
|
|
// Implementation Note:
|
|
|
|
// Since our priority queue doesn't support fast priority decreases
|
|
|
|
// we instead just add new entries into the queue when a priority changes.
|
|
|
|
// Redundant ones are filtered out later on by the visited set.
|
|
|
|
// According to https://www3.cs.stonybrook.edu/~rezaul/papers/TR-07-54.pdf
|
|
|
|
// for practical performance this is faster then using a more advanced queue
|
|
|
|
// (such as a fibonaacci queue) with a fast decrease priority.
|
2020-11-07 00:37:05 +01:00
|
|
|
for (unsigned i = 0; i < vertices_.length; i++)
|
2020-11-05 19:34:26 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
if (i == vertices_.length - 1)
|
|
|
|
vertices_[i].distance = 0;
|
2020-11-05 19:34:26 +01:00
|
|
|
else
|
2020-11-07 00:37:05 +01:00
|
|
|
vertices_[i].distance = hb_int_max (int64_t);
|
2020-11-05 19:34:26 +01:00
|
|
|
}
|
2020-11-05 18:21:25 +01:00
|
|
|
|
2020-11-05 23:23:29 +01:00
|
|
|
hb_priority_queue_t queue;
|
2021-03-18 19:13:47 +01:00
|
|
|
queue.insert (0, vertices_.length - 1);
|
2020-11-05 23:23:29 +01:00
|
|
|
|
2021-10-06 20:12:32 +02:00
|
|
|
hb_vector_t<bool> visited;
|
|
|
|
visited.resize (vertices_.length);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
while (!queue.in_error () && !queue.is_empty ())
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2021-03-18 19:13:47 +01:00
|
|
|
unsigned next_idx = queue.pop_minimum ().second;
|
2021-10-06 20:12:32 +02:00
|
|
|
if (visited[next_idx]) continue;
|
2020-11-07 00:37:05 +01:00
|
|
|
const auto& next = vertices_[next_idx];
|
|
|
|
int64_t next_distance = vertices_[next_idx].distance;
|
2021-10-06 20:12:32 +02:00
|
|
|
visited[next_idx] = true;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& link : next.obj.all_links ())
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2021-10-06 20:12:32 +02:00
|
|
|
if (visited[link.objidx]) continue;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
const auto& child = vertices_[link.objidx].obj;
|
2021-09-23 23:14:06 +02:00
|
|
|
unsigned link_width = link.width ? link.width : 4; // treat virtual offsets as 32 bits wide
|
2021-09-09 02:31:39 +02:00
|
|
|
int64_t child_weight = (child.tail - child.head) +
|
2021-09-23 23:14:06 +02:00
|
|
|
((int64_t) 1 << (link_width * 8)) * (vertices_[link.objidx].space + 1);
|
2020-11-05 18:21:25 +01:00
|
|
|
int64_t child_distance = next_distance + child_weight;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
if (child_distance < vertices_[link.objidx].distance)
|
2020-11-05 19:34:26 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
vertices_[link.objidx].distance = child_distance;
|
2021-03-18 19:13:47 +01:00
|
|
|
queue.insert (child_distance, link.objidx);
|
2020-11-05 19:34:26 +01:00
|
|
|
}
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 23:11:57 +01:00
|
|
|
|
|
|
|
check_success (!queue.in_error ());
|
|
|
|
if (!check_success (queue.is_empty ()))
|
|
|
|
{
|
2021-10-05 19:53:05 +02:00
|
|
|
print_orphaned_nodes ();
|
2020-11-10 23:11:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
distance_invalid = false;
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 23:51:39 +01:00
|
|
|
int64_t compute_offset (
|
|
|
|
unsigned parent_idx,
|
2020-11-07 00:37:05 +01:00
|
|
|
const hb_serialize_context_t::object_t::link_t& link) const
|
2020-11-02 23:51:39 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
const auto& parent = vertices_[parent_idx];
|
|
|
|
const auto& child = vertices_[link.objidx];
|
2020-11-02 23:51:39 +01:00
|
|
|
int64_t offset = 0;
|
|
|
|
switch ((hb_serialize_context_t::whence_t) link.whence) {
|
|
|
|
case hb_serialize_context_t::whence_t::Head:
|
2020-11-07 00:37:05 +01:00
|
|
|
offset = child.start - parent.start; break;
|
2020-11-02 23:51:39 +01:00
|
|
|
case hb_serialize_context_t::whence_t::Tail:
|
2020-11-07 00:37:05 +01:00
|
|
|
offset = child.start - parent.end; break;
|
2020-11-02 23:51:39 +01:00
|
|
|
case hb_serialize_context_t::whence_t::Absolute:
|
2020-11-07 00:37:05 +01:00
|
|
|
offset = child.start; break;
|
2020-11-02 23:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert (offset >= link.bias);
|
|
|
|
offset -= link.bias;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid_offset (int64_t offset,
|
2020-11-07 00:37:05 +01:00
|
|
|
const hb_serialize_context_t::object_t::link_t& link) const
|
2020-11-02 23:51:39 +01:00
|
|
|
{
|
2021-09-23 23:14:06 +02:00
|
|
|
if (unlikely (!link.width))
|
|
|
|
// Virtual links can't overflow.
|
|
|
|
return link.is_signed || offset >= 0;
|
|
|
|
|
2020-11-02 23:51:39 +01:00
|
|
|
if (link.is_signed)
|
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
if (link.width == 4)
|
2020-11-02 23:51:39 +01:00
|
|
|
return offset >= -((int64_t) 1 << 31) && offset < ((int64_t) 1 << 31);
|
|
|
|
else
|
|
|
|
return offset >= -(1 << 15) && offset < (1 << 15);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-17 18:59:45 +02:00
|
|
|
if (link.width == 4)
|
2020-11-02 23:51:39 +01:00
|
|
|
return offset >= 0 && offset < ((int64_t) 1 << 32);
|
2021-04-17 18:59:45 +02:00
|
|
|
else if (link.width == 3)
|
|
|
|
return offset >= 0 && offset < ((int32_t) 1 << 24);
|
2020-11-02 23:51:39 +01:00
|
|
|
else
|
|
|
|
return offset >= 0 && offset < (1 << 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 00:58:57 +02:00
|
|
|
/*
|
|
|
|
* Updates a link in the graph to point to a different object. Corrects the
|
|
|
|
* parents vector on the previous and new child nodes.
|
|
|
|
*/
|
|
|
|
void reassign_link (hb_serialize_context_t::object_t::link_t& link,
|
|
|
|
unsigned parent_idx,
|
|
|
|
unsigned new_idx)
|
|
|
|
{
|
|
|
|
unsigned old_idx = link.objidx;
|
|
|
|
link.objidx = new_idx;
|
|
|
|
vertices_[old_idx].remove_parent (parent_idx);
|
|
|
|
vertices_[new_idx].parents.push (parent_idx);
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:32:13 +02:00
|
|
|
/*
|
|
|
|
* Updates all objidx's in all links using the provided mapping. Corrects incoming edge counts.
|
|
|
|
*/
|
|
|
|
template<typename Iterator, hb_requires (hb_is_iterator (Iterator))>
|
|
|
|
void remap_obj_indices (const hb_hashmap_t<unsigned, unsigned>& id_map,
|
2021-10-05 21:46:59 +02:00
|
|
|
Iterator subgraph,
|
|
|
|
bool only_wide = false)
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
|
|
|
if (!id_map) return;
|
|
|
|
for (unsigned i : subgraph)
|
|
|
|
{
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& link : vertices_[i].obj.all_links_writer ())
|
2021-09-08 01:32:13 +02:00
|
|
|
{
|
|
|
|
if (!id_map.has (link.objidx)) continue;
|
2021-10-05 21:46:59 +02:00
|
|
|
if (only_wide && !(link.width == 4 && !link.is_signed)) continue;
|
2021-09-25 00:58:57 +02:00
|
|
|
|
|
|
|
reassign_link (link, i, id_map[link.objidx]);
|
2021-09-08 01:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
/*
|
|
|
|
* Updates all objidx's in all links using the provided mapping.
|
|
|
|
*/
|
2021-09-25 00:58:57 +02:00
|
|
|
void remap_all_obj_indices (const hb_vector_t<unsigned>& id_map,
|
|
|
|
hb_vector_t<vertex_t>* sorted_graph) const
|
2020-10-30 18:29:51 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < sorted_graph->length; i++)
|
|
|
|
{
|
2021-09-25 00:58:57 +02:00
|
|
|
(*sorted_graph)[i].remap_parents (id_map);
|
2021-12-01 00:25:40 +01:00
|
|
|
for (auto& link : (*sorted_graph)[i].obj.all_links_writer ())
|
2020-10-30 18:29:51 +01:00
|
|
|
{
|
2020-11-05 23:23:29 +01:00
|
|
|
link.objidx = id_map[link.objidx];
|
2020-10-30 18:29:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
template <typename O> void
|
|
|
|
serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link,
|
|
|
|
char* head,
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_serialize_context_t* c) const
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
|
|
|
OT::Offset<O>* offset = reinterpret_cast<OT::Offset<O>*> (head + link.position);
|
|
|
|
*offset = 0;
|
|
|
|
c->add_link (*offset,
|
2020-10-29 22:58:34 +01:00
|
|
|
// serializer has an extra nil object at the start of the
|
|
|
|
// object array. So all id's are +1 of what our id's are.
|
|
|
|
link.objidx + 1,
|
2020-10-29 01:49:09 +01:00
|
|
|
(hb_serialize_context_t::whence_t) link.whence,
|
|
|
|
link.bias);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize_link (const hb_serialize_context_t::object_t::link_t& link,
|
|
|
|
char* head,
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_serialize_context_t* c) const
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2021-05-12 23:46:54 +02:00
|
|
|
switch (link.width)
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
2021-09-23 23:14:06 +02:00
|
|
|
case 0:
|
|
|
|
// Virtual links aren't serialized.
|
|
|
|
return;
|
2021-05-12 23:46:54 +02:00
|
|
|
case 4:
|
2020-10-29 01:49:09 +01:00
|
|
|
if (link.is_signed)
|
|
|
|
{
|
|
|
|
serialize_link_of_type<OT::HBINT32> (link, head, c);
|
|
|
|
} else {
|
|
|
|
serialize_link_of_type<OT::HBUINT32> (link, head, c);
|
|
|
|
}
|
2021-05-12 23:46:54 +02:00
|
|
|
return;
|
|
|
|
case 2:
|
2020-10-29 01:49:09 +01:00
|
|
|
if (link.is_signed)
|
|
|
|
{
|
|
|
|
serialize_link_of_type<OT::HBINT16> (link, head, c);
|
|
|
|
} else {
|
|
|
|
serialize_link_of_type<OT::HBUINT16> (link, head, c);
|
|
|
|
}
|
2021-05-12 23:46:54 +02:00
|
|
|
return;
|
|
|
|
case 3:
|
2021-04-17 18:59:45 +02:00
|
|
|
serialize_link_of_type<OT::HBUINT24> (link, head, c);
|
2021-05-12 23:46:54 +02:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// Unexpected link width.
|
|
|
|
assert (0);
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 21:08:18 +02:00
|
|
|
/*
|
|
|
|
* Finds all nodes in targets that are reachable from start_idx, nodes in visited will be skipped.
|
|
|
|
* For this search the graph is treated as being undirected.
|
|
|
|
*
|
|
|
|
* Connected targets will be added to connected and removed from targets. All visited nodes
|
|
|
|
* will be added to visited.
|
|
|
|
*/
|
|
|
|
void find_connected_nodes (unsigned start_idx,
|
|
|
|
hb_set_t& targets,
|
|
|
|
hb_set_t& visited,
|
|
|
|
hb_set_t& connected)
|
|
|
|
{
|
|
|
|
if (visited.has (start_idx)) return;
|
|
|
|
visited.add (start_idx);
|
|
|
|
|
|
|
|
if (targets.has (start_idx))
|
|
|
|
{
|
|
|
|
targets.del (start_idx);
|
|
|
|
connected.add (start_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& v = vertices_[start_idx];
|
|
|
|
|
|
|
|
// Graph is treated as undirected so search children and parents of start_idx
|
2021-12-01 00:25:40 +01:00
|
|
|
for (const auto& l : v.obj.all_links ())
|
2021-09-28 21:08:18 +02:00
|
|
|
find_connected_nodes (l.objidx, targets, visited, connected);
|
|
|
|
|
|
|
|
for (unsigned p : v.parents)
|
|
|
|
find_connected_nodes (p, targets, visited, connected);
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
public:
|
2020-11-07 00:37:05 +01:00
|
|
|
// TODO(garretrieger): make private, will need to move most of offset overflow code into graph.
|
|
|
|
hb_vector_t<vertex_t> vertices_;
|
|
|
|
private:
|
2021-09-25 00:58:57 +02:00
|
|
|
bool parents_invalid;
|
2020-11-07 00:37:05 +01:00
|
|
|
bool distance_invalid;
|
|
|
|
bool positions_invalid;
|
2020-11-10 23:11:57 +01:00
|
|
|
bool successful;
|
2021-10-05 19:53:05 +02:00
|
|
|
hb_vector_t<unsigned> num_roots_for_space_;
|
2020-10-29 01:49:09 +01:00
|
|
|
};
|
|
|
|
|
2021-09-30 03:14:57 +02:00
|
|
|
static bool _try_isolating_subgraphs (const hb_vector_t<graph_t::overflow_record_t>& overflows,
|
|
|
|
graph_t& sorted_graph)
|
|
|
|
{
|
2021-12-07 00:23:35 +01:00
|
|
|
unsigned space = 0;
|
|
|
|
hb_set_t roots_to_isolate;
|
|
|
|
|
2021-09-30 03:14:57 +02:00
|
|
|
for (int i = overflows.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
const graph_t::overflow_record_t& r = overflows[i];
|
|
|
|
|
2021-12-07 00:23:35 +01:00
|
|
|
unsigned root;
|
|
|
|
unsigned overflow_space = sorted_graph.space_for (r.parent, &root);
|
|
|
|
if (!overflow_space) continue;
|
2021-12-07 01:00:15 +01:00
|
|
|
if (sorted_graph.num_roots_for_space (overflow_space) <= 1) continue;
|
2021-09-30 03:14:57 +02:00
|
|
|
|
2021-12-07 00:23:35 +01:00
|
|
|
if (!space) {
|
|
|
|
space = overflow_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space == overflow_space)
|
|
|
|
roots_to_isolate.add(root);
|
2021-09-30 03:14:57 +02:00
|
|
|
}
|
2021-12-07 00:23:35 +01:00
|
|
|
|
|
|
|
if (!roots_to_isolate) return false;
|
|
|
|
|
2021-12-07 01:00:15 +01:00
|
|
|
unsigned maximum_to_move = hb_max ((sorted_graph.num_roots_for_space (space) / 2u), 1u);
|
|
|
|
if (roots_to_isolate.get_population () > maximum_to_move) {
|
|
|
|
// Only move at most half of the roots in a space at a time.
|
|
|
|
unsigned extra = roots_to_isolate.get_population () - maximum_to_move;
|
|
|
|
while (extra--) {
|
|
|
|
unsigned root = HB_SET_VALUE_INVALID;
|
|
|
|
roots_to_isolate.previous (&root);
|
|
|
|
roots_to_isolate.del (root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:23:35 +01:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr,
|
|
|
|
"Overflow in space %d (%d roots). Moving %d roots to space %d.",
|
|
|
|
space,
|
|
|
|
sorted_graph.num_roots_for_space (space),
|
|
|
|
roots_to_isolate.get_population (),
|
|
|
|
sorted_graph.next_space ());
|
|
|
|
|
|
|
|
sorted_graph.isolate_subgraph (roots_to_isolate);
|
|
|
|
sorted_graph.move_to_new_space (roots_to_isolate);
|
|
|
|
|
|
|
|
return true;
|
2021-09-30 03:14:57 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 01:52:37 +02:00
|
|
|
static bool _process_overflows (const hb_vector_t<graph_t::overflow_record_t>& overflows,
|
|
|
|
hb_set_t& priority_bumped_parents,
|
|
|
|
graph_t& sorted_graph)
|
|
|
|
{
|
|
|
|
bool resolution_attempted = false;
|
|
|
|
|
|
|
|
// Try resolving the furthest overflows first.
|
|
|
|
for (int i = overflows.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
const graph_t::overflow_record_t& r = overflows[i];
|
|
|
|
const auto& child = sorted_graph.vertices_[r.child];
|
|
|
|
if (child.is_shared ())
|
|
|
|
{
|
|
|
|
// The child object is shared, we may be able to eliminate the overflow
|
|
|
|
// by duplicating it.
|
2021-09-10 23:55:24 +02:00
|
|
|
if (!sorted_graph.duplicate (r.parent, r.child)) continue;
|
2021-09-08 01:52:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.is_leaf () && !priority_bumped_parents.has (r.parent))
|
|
|
|
{
|
|
|
|
// This object is too far from it's parent, attempt to move it closer.
|
|
|
|
//
|
|
|
|
// TODO(garretrieger): initially limiting this to leaf's since they can be
|
|
|
|
// moved closer with fewer consequences. However, this can
|
|
|
|
// likely can be used for non-leafs as well.
|
|
|
|
// TODO(garretrieger): also try lowering priority of the parent. Make it
|
|
|
|
// get placed further up in the ordering, closer to it's children.
|
|
|
|
// this is probably preferable if the total size of the parent object
|
|
|
|
// is < then the total size of the children (and the parent can be moved).
|
|
|
|
// Since in that case moving the parent will cause a smaller increase in
|
|
|
|
// the length of other offsets.
|
2021-12-10 00:44:06 +01:00
|
|
|
if (sorted_graph.raise_childrens_priority (r.parent)) {
|
|
|
|
priority_bumped_parents.add (r.parent);
|
|
|
|
resolution_attempted = true;
|
|
|
|
}
|
2021-09-08 01:52:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(garretrieger): add additional offset resolution strategies
|
|
|
|
// - Promotion to extension lookups.
|
|
|
|
// - Table splitting.
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolution_attempted;
|
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
|
|
|
|
/*
|
2020-11-11 22:50:18 +01:00
|
|
|
* Attempts to modify the topological sorting of the provided object graph to
|
|
|
|
* eliminate offset overflows in the links between objects of the graph. If a
|
|
|
|
* non-overflowing ordering is found the updated graph is serialized it into the
|
|
|
|
* provided serialization context.
|
|
|
|
*
|
|
|
|
* If necessary the structure of the graph may be modified in ways that do not
|
|
|
|
* affect the functionality of the graph. For example shared objects may be
|
|
|
|
* duplicated.
|
2021-10-12 22:13:32 +02:00
|
|
|
*
|
|
|
|
* For a detailed writeup describing how the algorithm operates see:
|
2021-10-13 01:11:25 +02:00
|
|
|
* docs/repacker.md
|
2020-10-29 01:49:09 +01:00
|
|
|
*/
|
2021-12-06 21:54:19 +01:00
|
|
|
inline hb_blob_t*
|
2020-10-29 01:49:09 +01:00
|
|
|
hb_resolve_overflows (const hb_vector_t<hb_serialize_context_t::object_t *>& packed,
|
2021-09-08 19:14:00 +02:00
|
|
|
hb_tag_t table_tag,
|
2021-12-10 00:44:06 +01:00
|
|
|
unsigned max_rounds = 20) {
|
2020-11-07 01:22:48 +01:00
|
|
|
// Kahn sort is ~twice as fast as shortest distance sort and works for many fonts
|
2020-11-11 22:50:18 +01:00
|
|
|
// so try it first to save time.
|
2020-10-29 01:49:09 +01:00
|
|
|
graph_t sorted_graph (packed);
|
2020-10-30 18:29:51 +01:00
|
|
|
sorted_graph.sort_kahn ();
|
2020-11-11 22:50:18 +01:00
|
|
|
if (!sorted_graph.will_overflow ())
|
|
|
|
{
|
2021-12-06 21:54:19 +01:00
|
|
|
return sorted_graph.serialize ();
|
2020-11-11 22:50:18 +01:00
|
|
|
}
|
2020-11-06 01:39:23 +01:00
|
|
|
|
|
|
|
sorted_graph.sort_shortest_distance ();
|
|
|
|
|
2021-09-08 19:14:00 +02:00
|
|
|
if ((table_tag == HB_OT_TAG_GPOS
|
|
|
|
|| table_tag == HB_OT_TAG_GSUB)
|
|
|
|
&& sorted_graph.will_overflow ())
|
|
|
|
{
|
2021-09-29 01:04:27 +02:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Assigning spaces to 32 bit subgraphs.");
|
2021-09-28 21:08:18 +02:00
|
|
|
if (sorted_graph.assign_32bit_spaces ())
|
2021-09-08 19:14:00 +02:00
|
|
|
sorted_graph.sort_shortest_distance ();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
unsigned round = 0;
|
|
|
|
hb_vector_t<graph_t::overflow_record_t> overflows;
|
|
|
|
// TODO(garretrieger): select a good limit for max rounds.
|
2020-11-10 23:11:57 +01:00
|
|
|
while (!sorted_graph.in_error ()
|
|
|
|
&& sorted_graph.will_overflow (&overflows)
|
2021-09-09 19:25:43 +02:00
|
|
|
&& round++ < max_rounds) {
|
2021-09-08 19:14:00 +02:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "=== Overflow resolution round %d ===", round);
|
2020-11-06 01:39:23 +01:00
|
|
|
sorted_graph.print_overflows (overflows);
|
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
hb_set_t priority_bumped_parents;
|
2021-09-30 03:14:57 +02:00
|
|
|
|
|
|
|
if (!_try_isolating_subgraphs (overflows, sorted_graph))
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
2021-09-30 03:14:57 +02:00
|
|
|
if (!_process_overflows (overflows, priority_bumped_parents, sorted_graph))
|
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "No resolution available :(");
|
|
|
|
break;
|
|
|
|
}
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
2021-09-08 01:52:37 +02:00
|
|
|
sorted_graph.sort_shortest_distance ();
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:11:57 +01:00
|
|
|
if (sorted_graph.in_error ())
|
|
|
|
{
|
2021-12-06 21:54:19 +01:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Sorted graph in error state.");
|
|
|
|
return nullptr;
|
2020-11-10 23:11:57 +01:00
|
|
|
}
|
2021-09-07 22:22:19 +02:00
|
|
|
|
|
|
|
if (sorted_graph.will_overflow ())
|
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "Offset overflow resolution failed.");
|
2021-12-06 21:54:19 +01:00
|
|
|
return nullptr;
|
2021-09-07 22:22:19 +02:00
|
|
|
}
|
2021-12-06 21:54:19 +01:00
|
|
|
|
|
|
|
return sorted_graph.serialize ();
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HB_REPACKER_HH */
|