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"
|
|
|
|
|
|
|
|
|
|
|
|
struct graph_t
|
|
|
|
{
|
2020-10-30 18:29:51 +01:00
|
|
|
// TODO(garretrieger): add an error tracking system similar to what serialize_context_t
|
|
|
|
// does.
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
struct vertex_t
|
|
|
|
{
|
2020-11-07 01:22:48 +01:00
|
|
|
vertex_t () :
|
|
|
|
distance (0),
|
|
|
|
incoming_edges (0),
|
|
|
|
start (0),
|
|
|
|
end (0),
|
|
|
|
priority(0) {}
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
void fini () { obj.fini (); }
|
|
|
|
|
|
|
|
hb_serialize_context_t::object_t obj;
|
|
|
|
int64_t distance;
|
|
|
|
unsigned incoming_edges;
|
|
|
|
unsigned start;
|
|
|
|
unsigned end;
|
2020-11-07 01:22:48 +01:00
|
|
|
unsigned priority;
|
|
|
|
|
|
|
|
bool is_shared () const
|
|
|
|
{
|
|
|
|
return incoming_edges > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_leaf () const
|
|
|
|
{
|
|
|
|
return !obj.links.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void raise_priority ()
|
|
|
|
{
|
|
|
|
priority++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
int64_t modified_distance = distance + distance_modifier ();
|
|
|
|
return (modified_distance << 24) | (0x00FFFFFF & order);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t distance_modifier () const
|
|
|
|
{
|
|
|
|
if (!priority) return 0;
|
|
|
|
int64_t table_size = obj.tail - obj.head;
|
|
|
|
return -(table_size - table_size / (1 << priority));
|
|
|
|
}
|
2020-11-07 00:37:05 +01:00
|
|
|
};
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
struct overflow_record_t
|
|
|
|
{
|
|
|
|
unsigned parent;
|
|
|
|
const hb_serialize_context_t::object_t::link_t* link;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct clone_buffer_t
|
|
|
|
{
|
|
|
|
clone_buffer_t () : head (nullptr), tail (nullptr) {}
|
|
|
|
|
|
|
|
void copy (const hb_serialize_context_t::object_t& object)
|
|
|
|
{
|
|
|
|
fini ();
|
|
|
|
unsigned size = object.tail - object.head;
|
|
|
|
head = (char*) malloc (size);
|
|
|
|
memcpy (head, object.head, size);
|
|
|
|
tail = head + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* head;
|
|
|
|
char* tail;
|
|
|
|
|
|
|
|
void fini ()
|
|
|
|
{
|
|
|
|
if (!head) return;
|
|
|
|
free (head);
|
|
|
|
head = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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)
|
2020-11-07 00:37:05 +01:00
|
|
|
: edge_count_invalid (true),
|
|
|
|
distance_invalid (true),
|
|
|
|
positions_invalid (true)
|
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 ();
|
|
|
|
v->obj = *objects[i];
|
2020-10-29 22:58:34 +01:00
|
|
|
if (!removed_nil) continue;
|
2020-11-07 00:37:05 +01:00
|
|
|
for (unsigned i = 0; i < v->obj.links.length; i++)
|
2020-10-29 22:58:34 +01:00
|
|
|
// Fix indices to account for removed nil object.
|
2020-11-07 00:37:05 +01:00
|
|
|
v->obj.links[i].objidx--;
|
2020-10-29 22:58:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~graph_t ()
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
vertices_.fini_deep ();
|
2020-11-06 01:39:23 +01:00
|
|
|
clone_buffers_.fini_deep ();
|
2020-10-29 22:58:34 +01:00
|
|
|
}
|
2020-10-29 01:49:09 +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.
|
|
|
|
*/
|
2020-11-07 00:37:05 +01:00
|
|
|
void serialize (hb_serialize_context_t* c) const
|
2020-10-29 01:49:09 +01:00
|
|
|
{
|
|
|
|
c->start_serialize<void> ();
|
2020-11-07 00:37:05 +01:00
|
|
|
for (unsigned i = 0; i < vertices_.length; i++) {
|
2020-10-29 01:49:09 +01:00
|
|
|
c->push ();
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
size_t size = vertices_[i].obj.tail - vertices_[i].obj.head;
|
2020-10-29 01:49:09 +01:00
|
|
|
char* start = c->allocate_size <char> (size);
|
|
|
|
if (!start) return;
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
memcpy (start, vertices_[i].obj.head, size);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& link : vertices_[i].obj.links)
|
2020-10-29 01:49:09 +01:00
|
|
|
serialize_link (link, start, c);
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
// All duplications are already encoded in the graph, so don't
|
|
|
|
// enable sharing during packing.
|
2020-10-29 01:49:09 +01:00
|
|
|
c->pop_pack (false);
|
|
|
|
}
|
|
|
|
c->end_serialize ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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;
|
2020-11-05 23:23:29 +01:00
|
|
|
hb_vector_t<unsigned> id_map;
|
2020-11-07 00:37:05 +01:00
|
|
|
id_map.resize (vertices_.length);
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
hb_vector_t<unsigned> removed_edges;
|
|
|
|
removed_edges.resize (vertices_.length);
|
|
|
|
update_incoming_edge_count ();
|
|
|
|
|
|
|
|
queue.push (root_idx ());
|
|
|
|
int new_id = vertices_.length - 1;
|
2020-10-29 01:49:09 +01:00
|
|
|
|
|
|
|
while (queue.length)
|
|
|
|
{
|
2020-10-29 22:58:34 +01:00
|
|
|
unsigned next_id = queue[0];
|
2020-10-29 01:49:09 +01:00
|
|
|
queue.remove(0);
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t& next = vertices_[next_id];
|
2020-10-29 01:49:09 +01:00
|
|
|
sorted_graph.push (next);
|
2020-11-05 23:23:29 +01:00
|
|
|
id_map[next_id] = new_id--;
|
2020-10-29 01:49:09 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& link : next.obj.links) {
|
2020-10-30 18:29:51 +01:00
|
|
|
// TODO(garretrieger): sort children from smallest to largest
|
2020-11-07 00:37:05 +01:00
|
|
|
removed_edges[link.objidx]++;
|
|
|
|
if (!(vertices_[link.objidx].incoming_edges - removed_edges[link.objidx]))
|
2020-10-29 01:49:09 +01:00
|
|
|
queue.push (link.objidx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:58:34 +01:00
|
|
|
if (new_id != -1)
|
|
|
|
{
|
|
|
|
// Graph is not fully connected, there are unsorted objects.
|
|
|
|
// TODO(garretrieger): handle this.
|
|
|
|
assert (false);
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
remap_obj_indices (id_map, &sorted_graph);
|
2020-10-29 22:58:34 +01:00
|
|
|
|
2020-10-29 01:49:09 +01:00
|
|
|
sorted_graph.as_array ().reverse ();
|
2020-11-10 01:52:36 +01:00
|
|
|
|
|
|
|
vertices_.fini_deep ();
|
2020-11-07 00:37:05 +01:00
|
|
|
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;
|
2020-11-05 23:23:29 +01:00
|
|
|
hb_vector_t<unsigned> id_map;
|
2020-11-07 00:37:05 +01:00
|
|
|
id_map.resize (vertices_.length);
|
|
|
|
|
|
|
|
hb_vector_t<unsigned> removed_edges;
|
|
|
|
removed_edges.resize (vertices_.length);
|
|
|
|
update_incoming_edge_count ();
|
2020-11-03 01:16:27 +01:00
|
|
|
|
|
|
|
// 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.
|
2020-11-07 01:22:48 +01:00
|
|
|
queue.insert (root_idx (), root ().modified_distance (0));
|
2020-11-07 00:37:05 +01:00
|
|
|
int new_id = root_idx ();
|
2020-11-05 20:22:16 +01:00
|
|
|
unsigned order = 1;
|
|
|
|
while (!queue.is_empty ())
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2020-11-05 20:22:16 +01:00
|
|
|
unsigned next_id = queue.extract_minimum().first;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t& next = vertices_[next_id];
|
2020-11-03 01:16:27 +01:00
|
|
|
sorted_graph.push (next);
|
2020-11-05 23:23:29 +01:00
|
|
|
id_map[next_id] = new_id--;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& link : next.obj.links) {
|
|
|
|
removed_edges[link.objidx]++;
|
|
|
|
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
|
|
|
|
// distance they'll be added to the topolical order in the order that they are
|
|
|
|
// referenced from the parent object.
|
2020-11-07 01:22:48 +01:00
|
|
|
queue.insert (link.objidx,
|
|
|
|
vertices_[link.objidx].modified_distance (order++));
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_id != -1)
|
|
|
|
{
|
|
|
|
// Graph is not fully connected, there are unsorted objects.
|
|
|
|
// TODO(garretrieger): handle this.
|
|
|
|
assert (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
remap_obj_indices (id_map, &sorted_graph);
|
|
|
|
|
|
|
|
sorted_graph.as_array ().reverse ();
|
2020-11-10 01:52:36 +01:00
|
|
|
|
|
|
|
vertices_.fini_deep ();
|
2020-11-07 00:37:05 +01:00
|
|
|
vertices_ = sorted_graph;
|
2020-11-03 01:16:27 +01:00
|
|
|
sorted_graph.fini_deep ();
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01: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.
|
|
|
|
*/
|
|
|
|
void duplicate (unsigned parent_idx, unsigned child_idx)
|
|
|
|
{
|
2020-11-07 01:22:48 +01:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " Duplicating %d => %d",
|
|
|
|
parent_idx, child_idx);
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
positions_invalid = true;
|
|
|
|
|
2020-11-10 01:52:36 +01:00
|
|
|
auto* clone = vertices_.push ();
|
2020-11-07 00:37:05 +01:00
|
|
|
auto& child = vertices_[child_idx];
|
2020-11-06 01:39:23 +01:00
|
|
|
clone_buffer_t* buffer = clone_buffers_.push ();
|
2020-11-07 00:37:05 +01:00
|
|
|
buffer->copy (child.obj);
|
|
|
|
|
|
|
|
clone->obj.head = buffer->head;
|
|
|
|
clone->obj.tail = buffer->tail;
|
|
|
|
clone->distance = child.distance;
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& l : child.obj.links)
|
|
|
|
clone->obj.links.push (l);
|
2020-11-06 01:39:23 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
auto& parent = vertices_[parent_idx];
|
|
|
|
unsigned clone_idx = vertices_.length - 2;
|
|
|
|
for (unsigned i = 0; i < parent.obj.links.length; i++)
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
auto& l = parent.obj.links[i];
|
|
|
|
if (l.objidx == child_idx)
|
|
|
|
{
|
|
|
|
l.objidx = clone_idx;
|
|
|
|
clone->incoming_edges++;
|
|
|
|
child.incoming_edges--;
|
|
|
|
}
|
2020-11-06 01:39:23 +01: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.
|
2020-11-07 00:37:05 +01:00
|
|
|
vertex_t root = vertices_[vertices_.length - 2];
|
|
|
|
vertices_[vertices_.length - 2] = *clone;
|
|
|
|
vertices_[vertices_.length - 1] = root;
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
/*
|
|
|
|
* Raises the sorting priority of all children.
|
|
|
|
*/
|
|
|
|
void raise_childrens_priority (unsigned parent_idx)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for (unsigned i = 0; i < parent.links.length; i++)
|
|
|
|
vertices_[parent.links[i].objidx].raise_priority ();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& link : vertices_[parent_idx].obj.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;
|
|
|
|
r.link = &link;
|
|
|
|
overflows->push (r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overflows) return false;
|
|
|
|
return overflows->length;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
update_incoming_edge_count ();
|
2020-11-06 01:39:23 +01:00
|
|
|
for (const auto& o : overflows)
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
const auto& child = vertices_[o.link->objidx];
|
2020-11-07 01:22:48 +01:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, " overflow from %d => %d (%d incoming , %d outgoing)",
|
2020-11-06 01:39:23 +01:00
|
|
|
o.parent,
|
|
|
|
o.link->objidx,
|
2020-11-07 00:37:05 +01:00
|
|
|
child.incoming_edges,
|
|
|
|
child.obj.links.length);
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
/*
|
|
|
|
* Creates a map from objid to # of incoming edges.
|
|
|
|
*/
|
|
|
|
void update_incoming_edge_count ()
|
|
|
|
{
|
|
|
|
if (!edge_count_invalid) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < vertices_.length; i++)
|
|
|
|
vertices_[i].incoming_edges = 0;
|
|
|
|
|
|
|
|
for (const vertex_t& v : vertices_)
|
|
|
|
{
|
|
|
|
for (auto& l : v.obj.links)
|
|
|
|
{
|
|
|
|
vertices_[l.objidx].incoming_edges++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
edge_count_invalid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
/*
|
|
|
|
* Finds the distance too each object in the graph
|
|
|
|
* 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;
|
2020-11-07 00:37:05 +01:00
|
|
|
queue.insert (vertices_.length - 1, 0);
|
2020-11-05 23:23:29 +01:00
|
|
|
|
2020-11-05 19:34:26 +01:00
|
|
|
hb_set_t visited;
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-05 19:34:26 +01:00
|
|
|
while (!queue.is_empty ())
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2020-11-05 19:34:26 +01:00
|
|
|
unsigned next_idx = queue.extract_minimum ().first;
|
|
|
|
if (visited.has (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;
|
2020-11-05 19:34:26 +01:00
|
|
|
visited.add (next_idx);
|
2020-11-03 01:16:27 +01:00
|
|
|
|
2020-11-07 00:37:05 +01:00
|
|
|
for (const auto& link : next.obj.links)
|
2020-11-03 01:16:27 +01:00
|
|
|
{
|
2020-11-05 19:34:26 +01:00
|
|
|
if (visited.has (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;
|
2020-11-03 01:16:27 +01:00
|
|
|
int64_t child_weight = child.tail - child.head +
|
|
|
|
(!link.is_wide ? (1 << 16) : ((int64_t) 1 << 32));
|
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;
|
2020-11-05 19:34:26 +01:00
|
|
|
queue.insert (link.objidx, child_distance);
|
|
|
|
}
|
2020-11-03 01:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 18:21:25 +01:00
|
|
|
// TODO(garretrieger): Handle this. If anything is left, part of the graph is disconnected.
|
2020-11-05 19:34:26 +01:00
|
|
|
assert (queue.is_empty ());
|
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
|
|
|
{
|
|
|
|
if (link.is_signed)
|
|
|
|
{
|
|
|
|
if (link.is_wide)
|
|
|
|
return offset >= -((int64_t) 1 << 31) && offset < ((int64_t) 1 << 31);
|
|
|
|
else
|
|
|
|
return offset >= -(1 << 15) && offset < (1 << 15);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (link.is_wide)
|
|
|
|
return offset >= 0 && offset < ((int64_t) 1 << 32);
|
|
|
|
else
|
|
|
|
return offset >= 0 && offset < (1 << 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:29:51 +01:00
|
|
|
/*
|
|
|
|
* Updates all objidx's in all links using the provided mapping.
|
|
|
|
*/
|
2020-11-05 23:23:29 +01:00
|
|
|
void remap_obj_indices (const hb_vector_t<unsigned>& id_map,
|
2020-11-07 00:37:05 +01:00
|
|
|
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++)
|
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
for (unsigned j = 0; j < (*sorted_graph)[i].obj.links.length; j++)
|
2020-10-30 18:29:51 +01:00
|
|
|
{
|
2020-11-07 00:37:05 +01:00
|
|
|
auto& link = (*sorted_graph)[i].obj.links[j];
|
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
|
|
|
{
|
|
|
|
if (link.is_wide)
|
|
|
|
{
|
|
|
|
if (link.is_signed)
|
|
|
|
{
|
|
|
|
serialize_link_of_type<OT::HBINT32> (link, head, c);
|
|
|
|
} else {
|
|
|
|
serialize_link_of_type<OT::HBUINT32> (link, head, c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (link.is_signed)
|
|
|
|
{
|
|
|
|
serialize_link_of_type<OT::HBINT16> (link, head, c);
|
|
|
|
} else {
|
|
|
|
serialize_link_of_type<OT::HBUINT16> (link, head, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2020-11-06 01:39:23 +01:00
|
|
|
hb_vector_t<clone_buffer_t> clone_buffers_;
|
2020-11-07 00:37:05 +01:00
|
|
|
bool edge_count_invalid;
|
|
|
|
bool distance_invalid;
|
|
|
|
bool positions_invalid;
|
2020-10-29 01:49:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-serialize the provided object graph into the serialization context
|
|
|
|
* using BFS (Breadth First Search) to produce the topological ordering.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
hb_resolve_overflows (const hb_vector_t<hb_serialize_context_t::object_t *>& packed,
|
|
|
|
hb_serialize_context_t* c) {
|
2020-11-07 01:22:48 +01:00
|
|
|
// Kahn sort is ~twice as fast as shortest distance sort and works for many fonts
|
|
|
|
// so try it first.
|
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-07 01:22:48 +01:00
|
|
|
if (!sorted_graph.will_overflow ()) return;
|
2020-11-06 01:39:23 +01:00
|
|
|
|
|
|
|
sorted_graph.sort_shortest_distance ();
|
|
|
|
|
|
|
|
unsigned round = 0;
|
|
|
|
hb_vector_t<graph_t::overflow_record_t> overflows;
|
|
|
|
// TODO(garretrieger): select a good limit for max rounds.
|
|
|
|
while (sorted_graph.will_overflow (&overflows) && round++ < 10) {
|
2020-11-07 01:22:48 +01:00
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "=== Over flow resolution round %d ===", round);
|
2020-11-06 01:39:23 +01:00
|
|
|
sorted_graph.print_overflows (overflows);
|
|
|
|
|
|
|
|
bool resolution_attempted = false;
|
2020-11-07 01:22:48 +01:00
|
|
|
hb_set_t priority_bumped_parents;
|
|
|
|
// Try resolving the furthest overflows first.
|
2020-11-06 01:39:23 +01:00
|
|
|
for (int i = overflows.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
const graph_t::overflow_record_t& r = overflows[i];
|
2020-11-07 01:22:48 +01:00
|
|
|
const auto& child = sorted_graph.vertices_[r.link->objidx];
|
|
|
|
if (child.is_shared ())
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
|
|
|
// The child object is shared, we may be able to eliminate the overflow
|
|
|
|
// by duplicating it.
|
|
|
|
sorted_graph.duplicate (r.parent, r.link->objidx);
|
|
|
|
resolution_attempted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
if (child.is_leaf () && !priority_bumped_parents.has (r.parent))
|
|
|
|
{
|
|
|
|
// TODO(garretrieger): initially limiting this to leaf's but likely
|
|
|
|
// can be used for non-leafs as well.
|
|
|
|
// TODO(garretrieger): add a maximum priority, don't try to raise past this.
|
|
|
|
sorted_graph.raise_childrens_priority (r.parent);
|
|
|
|
priority_bumped_parents.add (r.parent);
|
|
|
|
resolution_attempted = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-06 01:39:23 +01:00
|
|
|
// TODO(garretrieger): add additional offset resolution strategies
|
|
|
|
// - Promotion to extension lookups.
|
|
|
|
// - Table splitting.
|
|
|
|
}
|
|
|
|
|
2020-11-07 01:22:48 +01:00
|
|
|
if (resolution_attempted)
|
2020-11-06 01:39:23 +01:00
|
|
|
{
|
2020-11-07 01:22:48 +01:00
|
|
|
sorted_graph.sort_shortest_distance ();
|
|
|
|
continue;
|
2020-11-06 01:39:23 +01:00
|
|
|
}
|
2020-11-07 01:22:48 +01:00
|
|
|
|
|
|
|
DEBUG_MSG (SUBSET_REPACK, nullptr, "No resolution available :(");
|
|
|
|
break;
|
2020-10-29 01:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sorted_graph.serialize (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_REPACKER_HH */
|