Skip to content

Commit 055421a

Browse files
authored
Merge pull request #268 from ReactiveBayes/dev-force-svg
Force svg+xml for GraphViz object as a workaround
2 parents bbb6781 + ddd1dd8 commit 055421a

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

ext/GraphPPLGraphVizExt.jl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,40 @@ function convert_strategy(strategy::Symbol)
459459
end
460460
end
461461

462+
"""
463+
GraphVizGraphWrapper
464+
465+
A wrapper type for GraphViz.Graph that restricts display capabilities to SVG and text formats only.
466+
467+
This wrapper is designed to prevent display issues that can occur with PNG format on some systems
468+
by limiting the available display formats to SVG and plain text.
469+
470+
# Fields
471+
- `graph::GraphViz.Graph`: The wrapped GraphViz graph object
472+
- `dot_string::String`: The DOT string representation of the graph
473+
"""
474+
struct GraphVizGraphWrapper
475+
graph::GraphViz.Graph
476+
dot_string::String
477+
end
478+
479+
# Override showable to only allow SVG and text display
480+
function Base.showable(mime::MIME, x::GraphVizGraphWrapper)
481+
if mime isa MIME"image/svg+xml" || mime isa MIME"text/plain"
482+
return showable(mime, x.graph)
483+
end
484+
return false
485+
end
486+
487+
# Delegate show methods to the wrapped graph
488+
function Base.show(io::IO, mime::MIME"image/svg+xml", x::GraphVizGraphWrapper)
489+
show(io, mime, x.graph)
490+
end
491+
492+
function Base.show(io::IO, mime::MIME"text/plain", x::GraphVizGraphWrapper)
493+
show(io, mime, x.dot_string)
494+
end
495+
462496
"""
463497
Converts a GraphPPL.Model to a DOT string for visualization with GraphViz.jl.
464498
@@ -474,7 +508,9 @@ Converts a GraphPPL.Model to a DOT string for visualization with GraphViz.jl.
474508
- `save_to::String=nothing`: Optional path to save SVG output
475509
476510
# Returns
477-
- `String`: DOT format string representing the graph
511+
- `GraphVizGraphWrapper`: A wrapper around the GraphViz.Graph object that restricts display capabilities to SVG and text formats only.
512+
Use `.graph` property to access the GraphViz.Graph object directly.
513+
Use `.dot_string` property to access the DOT string representation of the graph.
478514
479515
# Details
480516
Generates a DOT string visualization of a GraphPPL.Model with configurable layout and styling options.
@@ -514,15 +550,15 @@ function GraphViz.load(
514550
write(io_buffer, "}")
515551

516552
final_string = String(take!(io_buffer))
517-
final_dot = GraphViz.Graph(final_string)
553+
final_graph = GraphViz.Graph(final_string)
518554

519555
if !isnothing(save_to)
520556
open(save_to, "w") do io
521-
show(io, MIME"image/svg+xml"(), final_dot)
557+
show(io, MIME"image/svg+xml"(), final_graph)
522558
end
523559
end
524560

525-
return final_dot
561+
return GraphVizGraphWrapper(final_graph, final_string)
526562
end
527563

528564
end

0 commit comments

Comments
 (0)