Skip to content

Commit

Permalink
support font (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Oct 19, 2024
1 parent e4e4dd3 commit 2236280
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 41 deletions.
1 change: 1 addition & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ var (

// functions from gvc package.
var (
SetFontLoader = gvc.SetFontLoader
DefaultPlugins = gvc.DefaultPlugins
DeviceQuality = gvc.WithDeviceQuality
DeviceFeatures = gvc.WithDeviceFeatures
Expand Down
19 changes: 17 additions & 2 deletions cgraph/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,11 @@ func (g *Graph) SetInputScale(v float64) *Graph {
return g
}

// Label returns label attribute.
func (g *Graph) Label() (string, error) {
return g.GetStr(string(labelAttr))
}

// SetLabel
// Text label attached to objects.
// If a node's shape is record, then the label can have a special format which describes the record layout.
Expand All @@ -1080,10 +1085,15 @@ func (g *Graph) SetInputScale(v float64) *Graph {
// To get an HTML-like label, the label attribute value itself must be an HTML string.
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#a:label
func (g *Graph) SetLabel(v string) *Graph {
g.SafeSet(string(labelAttr), v, "")
g.SafeSet(string(labelAttr), v, "\\G")
return g
}

// Label returns label attribute.
func (n *Node) Label() (string, error) {
return n.GetStr(string(labelAttr))
}

// SetLabel
// Text label attached to objects.
// If a node's shape is record, then the label can have a special format which describes the record layout.
Expand All @@ -1097,6 +1107,11 @@ func (n *Node) SetLabel(v string) *Node {
return n
}

// Label returns label attribute.
func (e *Edge) Label() (string, error) {
return e.GetStr(string(labelAttr))
}

// SetLabel
// Text label attached to objects.
// If a node's shape is record, then the label can have a special format which describes the record layout.
Expand All @@ -1106,7 +1121,7 @@ func (n *Node) SetLabel(v string) *Node {
// To get an HTML-like label, the label attribute value itself must be an HTML string.
// https://graphviz.gitlab.io/_pages/doc/info/attrs.html#a:label
func (e *Edge) SetLabel(v string) *Edge {
e.SafeSet(string(labelAttr), v, "")
e.SafeSet(string(labelAttr), v, "\\E")
return e
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/dot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ require (
)

require (
github.com/flopp/go-findfont v0.1.0 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/tetratelabs/wazero v1.8.1 // indirect
golang.org/x/image v0.21.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
)
4 changes: 4 additions & 0 deletions cmd/dot/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/corona10/goimagehash v1.1.0 h1:teNMX/1e+Wn/AYSbLHX8mj+mF9r60R1kBeqE9MkoYwI=
github.com/corona10/goimagehash v1.1.0/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI=
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand All @@ -16,3 +18,5 @@ golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ go 1.22.0

require (
github.com/corona10/goimagehash v1.1.0
github.com/flopp/go-findfont v0.1.0
github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/tetratelabs/wazero v1.8.1
golang.org/x/image v0.21.0
)

require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
require (
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
golang.org/x/text v0.19.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/corona10/goimagehash v1.1.0 h1:teNMX/1e+Wn/AYSbLHX8mj+mF9r60R1kBeqE9MkoYwI=
github.com/corona10/goimagehash v1.1.0/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI=
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand All @@ -10,3 +12,5 @@ github.com/tetratelabs/wazero v1.8.1 h1:NrcgVbWfkWvVc4UtT4LRLDf91PsOzDzefMdwhLfA
github.com/tetratelabs/wazero v1.8.1/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
5 changes: 0 additions & 5 deletions graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/goccy/go-graphviz/cgraph"
"github.com/goccy/go-graphviz/gvc"
"golang.org/x/image/font"
)

type Graphviz struct {
Expand Down Expand Up @@ -75,10 +74,6 @@ func (g *Graphviz) SetLayout(layout Layout) *Graphviz {
return g
}

func (g *Graphviz) SetFontFace(callback func(size float64) (font.Face, error)) {
gvc.SetFontFace(callback)
}

func (g *Graphviz) Render(ctx context.Context, graph *Graph, format Format, w io.Writer) (e error) {
defer func() {
if err := g.ctx.FreeLayout(ctx, graph); err != nil {
Expand Down
37 changes: 37 additions & 0 deletions gvc/gvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (c *Context) Close() error {
}

func (c *Context) Layout(ctx context.Context, g *cgraph.Graph, engine string) error {
if err := c.setupNodeLabelIfEmpty(g); err != nil {
return err
}
res, err := c.gvc.Layout(ctx, toGraphWasm(g), engine)
if err != nil {
return err
Expand Down Expand Up @@ -111,6 +114,40 @@ func (c *Context) FreeClonedContext(ctx context.Context) error {
return c.gvc.FreeClonedContext(ctx)
}

func (c *Context) setupNodeLabelIfEmpty(g *cgraph.Graph) error {
n, err := g.FirstNode()
if err != nil {
return err
}
if err := c.setLabelIfEmpty(n); err != nil {
return err
}
for {
n, err = g.NextNode(n)
if err != nil {
return err
}
if n == nil {
break
}
if err := c.setLabelIfEmpty(n); err != nil {
return err
}
}
return nil
}

func (c *Context) setLabelIfEmpty(n *cgraph.Node) error {
label, err := n.Label()
if err != nil {
return err
}
if label == "" {
n.SetLabel("\\N")
}
return nil
}

func newPlugins(ctx context.Context, plugins ...Plugin) ([]*wasm.SymList, error) {
defaults, err := wasm.DefaultSymList(ctx)
if err != nil {
Expand Down
Loading

0 comments on commit 2236280

Please sign in to comment.