Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[write-fonts] Graphviz improvements #612

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion write-fonts/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,10 @@ impl Graph {

#[cfg(feature = "dot2")]
pub(crate) fn write_graph_viz(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
graphviz::GraphVizGraph::from_graph(self).write_to_file(path)
// if this is set then we prune the generated graph
const PRUNE_GRAPH_ENV_VAR: &str = "FONTC_PRUNE_GRAPH";
anthrotype marked this conversation as resolved.
Show resolved Hide resolved
let try_trim_graph = std::env::var_os(PRUNE_GRAPH_ENV_VAR).is_some();
graphviz::GraphVizGraph::from_graph(self, try_trim_graph).write_to_file(path)
}
}

Expand Down
63 changes: 38 additions & 25 deletions write-fonts/src/graph/graphviz.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
//! Support for generating graphviz files from our object graph

use super::{Graph, ObjectId, OffsetLen, Space};
use std::collections::{BTreeSet, HashSet};

use super::{Graph, ObjectId, OffsetLen};

pub struct GraphVizGraph<'a> {
graph: &'a Graph,
nodes: Vec<ObjectId>,
edges: Vec<GraphVizEdge>,
}

impl<'a> GraphVizGraph<'a> {
pub(crate) fn from_graph(graph: &'a Graph) -> Self {
pub(crate) fn from_graph(graph: &'a Graph, prune_non_overflows: bool) -> Self {
let mut edges = Vec::new();

// if we are pruning it means that we remove all nodes in spaces
// that do not include overflows.
let nodes: BTreeSet<_> = if !prune_non_overflows {
graph.objects.keys().copied().collect()
} else {
let overflows = graph.find_overflows();
let overflow_spaces = overflows
.iter()
.map(|overflow| graph.nodes.get(&overflow.child).unwrap().space)
.collect::<HashSet<_>>();
graph
.nodes
.iter()
.filter_map(|(id, node)| overflow_spaces.contains(&node.space).then_some(*id))
.collect()
};

for (parent_id, table) in &graph.objects {
if !nodes.contains(parent_id) {
continue;
}
let parent = &graph.nodes[parent_id];
for link in &table.offsets {
if !nodes.contains(&link.object) {
continue;
}
let child = &graph.nodes[&link.object];
let len = child.position - parent.position;
edges.push(GraphVizEdge {
Expand All @@ -23,7 +50,12 @@ impl<'a> GraphVizGraph<'a> {
});
}
}
GraphVizGraph { graph, edges }

GraphVizGraph {
graph,
edges,
nodes: nodes.into_iter().collect(),
}
}

/// Write out this graph as a graphviz file to the provided path.
Expand All @@ -46,13 +78,11 @@ pub struct GraphVizEdge {

impl<'a> dot2::GraphWalk<'a> for GraphVizGraph<'a> {
type Node = ObjectId;

type Edge = GraphVizEdge;

type Subgraph = Space;
type Subgraph = ();

fn nodes(&'a self) -> dot2::Nodes<'a, Self::Node> {
self.graph.order.as_slice().into()
self.nodes.as_slice().into()
}

fn edges(&'a self) -> dot2::Edges<'a, Self::Edge> {
Expand All @@ -66,29 +96,12 @@ impl<'a> dot2::GraphWalk<'a> for GraphVizGraph<'a> {
fn target(&'a self, edge: &Self::Edge) -> Self::Node {
edge.target
}

fn subgraphs(&'a self) -> dot2::Subgraphs<'a, Self::Subgraph> {
let mut spaces: Vec<_> = self.graph.nodes.values().map(|x| x.space).collect();
spaces.sort_unstable();
spaces.dedup();
spaces.into()
}

fn subgraph_nodes(&'a self, s: &Self::Subgraph) -> dot2::Nodes<'a, Self::Node> {
self.graph
.nodes
.iter()
.filter_map(|(id, node)| (node.space == *s).then_some(*id))
.collect()
}
}

impl<'a> dot2::Labeller<'a> for GraphVizGraph<'a> {
type Node = ObjectId;

type Edge = GraphVizEdge;

type Subgraph = Space;
type Subgraph = ();

fn graph_id(&'a self) -> dot2::Result<dot2::Id<'a>> {
dot2::Id::new("TablePacking")
Expand Down