Skip to content

About

Go bindings for Graphviz: layout and render DOT graphs with the real Graphviz compiled to WebAssembly and run on wazero, no C toolchain and no system Graphviz

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

 
 

Latest commit

 

History

660 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

go-graphviz ci

Go bindings for Graphviz

About this fork

A fork of github.com/goccy/go-graphviz, maintained by forkcloser. The library is the same: Graphviz compiled to WebAssembly, run on wazero, no C toolchain and no system Graphviz. The upstream dot command is not part of this module; forkcloser ships its own, github.com/forkcloser/dot, as a static binary with every layout and format of the library. What this fork fixes and changes that upstream does not have is the table in UPSTREAM.md, beside the fork point and what was taken from upstream since.

Features

The embedded Graphviz is the version of the graphviz entry in pins.yaml; graphviz.Version() asks the module itself.

  • Pure Go Library
  • No need to install Graphviz library ( brew install graphviz or apt-get install graphviz )
    • The Graphviz library has been converted to WebAssembly (WASM) and embedded it, so it works consistently across all environments
  • Supports encoding/decoding for DOT language
  • Supports custom renderer for custom format
  • Supports setting graph properties in a type-safe manner

Supported Layout

circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi

The embedded Graphviz is built without a triangulation library (no HAVE_GTS) and without the IPSEP solver (no IPSEPCOLA), as the hand-written config.h has it. A dot built with both, as Homebrew's is, lays these out differently:

  • overlap=false removes overlaps by the Voronoi method, where such a dot uses prism, so the layout is another. This holds for neato, fdp and sfdp.
  • overlap=prism, with or without its number, is ignored with the warning Overlap value "prism" unsupported - ignored, and does what overlap=false does.
  • sfdp removes no overlaps unless told to: its default is prism in such a dot and nothing here, so its nodes can overlap. overlap=false, overlap=scale and overlap=voronoi remove them.
  • mode=ipsep is ignored by neato with the warning Illegal value ipsep for attribute "mode", and the layout is the default mode's.

sfdp also places its nodes elsewhere than a native dot does for the same graph, overlap removal aside: Graphviz's sfdp starts from positions drawn from the C library's rand(), and the module carries its own C library. The layout is the same from one run to the next.

Supported Format

dot svg png jpg

The above are the formats supported by default. You can also add custom formats.

Fonts

Labels are measured for the layout and drawn with the fonts installed on the machine, as Graphviz's own tools do, so text fits its node in every format, and the same graph can lay out slightly differently on two machines with different fonts, or against a native Graphviz on the same machine, whose pango plugin measures the same font through a different text engine (a node a point wider or narrower). A font name resolves the way Graphviz resolves it: a PostScript name such as Times-Roman or Helvetica-Bold, a family with style words (DejaVu Sans Bold, Arial:italic), or a list of them, looked up among the installed fonts by name, with the metric-compatible substitutes Graphviz knows (Arial for Helvetica, the Liberation and TeX Gyre faces). Where nothing installed answers, the embedded Go fonts stand in, in regular, bold, italic and monospace cuts. Those cover Latin, Greek and Cyrillic: a label in another script, CJK among them, draws as boxes on a machine with no font for it, unless a FontLoader (SetFontLoader) supplies one.

A font can also be named by the path of its file (fontname="/fonts/face.ttf"). The file is read from the file system SetFileSystem names, as a node image is, and from the machine when none is named.

Fonts are read from the platform's directories: on macOS ~/Library/Fonts, /Library/Fonts and /System/Library/Fonts; on Windows the system and the user's Fonts folders; on Linux and the BSDs ~/.fonts, then fonts under XDG_DATA_HOME (~/.local/share/fonts when it is unset) and under each XDG_DATA_DIRS entry (/usr/local/share/fonts and /usr/share/fonts when it is unset); on Android /system/fonts.

Installation

$ go get github.com/forkcloser/go-graphviz

Synopsis

1. Write DOT Graph in Go

package main

import (
  "bytes"
  "context"
  "fmt"
  "log"

  "github.com/forkcloser/go-graphviz"
)

func main() {
  ctx := context.Background()
  g, err := graphviz.New(ctx)
  if err != nil { panic(err )}

  graph, err := g.Graph()
  if err != nil { panic(err) }
  defer func() {
    if err := graph.Close(); err != nil { panic(err) }
    g.Close()
  }()
  n, err := graph.CreateNodeByName("n")
  if err != nil { panic(err) }

  m, err := graph.CreateNodeByName("m")
  if err != nil { panic(err) }

  e, err := graph.CreateEdgeByName("e", n, m)
  if err != nil { panic(err) }
  e.SetLabel("e")

  var buf bytes.Buffer
  if err := g.Render(ctx, graph, graphviz.GV, &buf); err != nil {
    log.Fatal(err)
  }
  fmt.Println(buf.String())
}

2. Parse DOT Graph

path := "/path/to/dot.gv"
b, err := os.ReadFile(path)
if err != nil { panic(err) }
graph, err := graphviz.ParseBytes(b)

3. Render Graph

ctx := context.Background()
g, err := graphviz.New(ctx)
if err != nil { panic(err) }

graph, err := g.Graph()
if err != nil { panic(err) }

// create your graph

// 1. write encoded PNG data to buffer
var buf bytes.Buffer
if err := g.Render(ctx, graph, graphviz.PNG, &buf); err != nil { panic(err) }

// 2. get as image.Image instance
image, err := g.RenderImage(ctx, graph)
if err != nil { panic(err) }

// 3. write to file directly
if err := g.RenderFilename(ctx, graph, graphviz.PNG, "/path/to/graph.png"); err != nil { panic(err) }

Development

Every task runs through just: just lint, just test, just fix, just wasm, just bindings; just --list shows the rest. Tools are pinned by aqua and the shared conventions come from limen (see AGENTS.md).

just test first fetches Graphviz's own dot of the release the module is built from (just oracle: a build by testbin, pinned and verified per platform in pins.yaml), and the tests compare the module's output with it. Under just test a missing or mismatched dot fails; a bare go test uses a dot on PATH of the same release, or skips those tests.

How it works

  1. Generates bindings between Go and C from Protocol Buffers file with just bindings.
  2. Builds graphviz.wasm with just wasm (internal/wasm/build/build.sh): Graphviz, expat and the C bridge compiled to wasm32-wasip1 by a pinned wasi-sdk and shrunk by a pinned binaryen, every input fetched by version and checked against the digests in pins.yaml. No container and no configure run: the build is reproducible, and CI rebuilds the committed blob on Linux and macOS and compares the bytes.
  3. Uses Graphviz functionality from the sub-packages cgraph and gvc via the internal/wasm package.
  4. graphviz package provides facade interface for all sub packages.

Graphviz runs as one WebAssembly module for the process. If a graph makes it trap (a stack it overflows, an access outside its memory), that call and every call after it return gvc.ErrTrapped (a getter, which has no error to return, its zero value), and the process has to be restarted to render again: a trap leaves Graphviz's state half-updated, so no call is let in to draw from it, and no fresh module takes its place, since every graph and context a caller holds lives in the one that trapped.

A render during which Graphviz reports an error and goes on, a record or an HTML label it cannot read for one, is written whole, and Render then returns that error, a gvc.ErrReported: what dot does when it writes its output and exits 1. RenderImage returns its image with it, and RenderFilename has written its file. Any other error of Graphviz's is a render that failed, with nothing written. What Graphviz only warns of goes to the writer SetWarningWriter names.

License

MIT, see LICENSE.

internal/wasm/graphviz.wasm is built from Graphviz and expat at the versions pins.yaml names: Graphviz is under the Eclipse Public License 2.0 (LICENSE-graphviz), expat under the MIT license (LICENSE-expat).

About

Go bindings for Graphviz: layout and render DOT graphs with the real Graphviz compiled to WebAssembly and run on wazero, no C toolchain and no system Graphviz

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages