go-graphviz icon indicating copy to clipboard operation
go-graphviz copied to clipboard

PNG renderer default font selection on macOS is worse than `dot`

Open spacedub opened this issue 2 years ago • 2 comments

When rendering with dot -Tpng, you get a result using Times,serif on my system.

When rendering with go-graphviz to SVG, you also get the same font.

But when rendering with go-graphviz to PNG, you end-up with a sans font that does looks quirky - specifically, it does overflow from the shape.

Am I missing something that would allow to get a consistent behavior here?

Though, I would expect by default that go-graphviz PNG renderer behaves the same as the SVG renderer, and the same as the default dot png output.

Might be related to #25

Attaching screenshots.

Thanks!

dot_render gograph_render

spacedub avatar Oct 14 '22 18:10 spacedub

Thought I would just add that when manually specifying the font to Times New Roman, you do get the expected results with go-graphviz renderer.

spacedub avatar Oct 14 '22 18:10 spacedub

Here is a workaround in case others want an escape hatch, in case the default stays the same. This relies on "github.com/flopp/go-findfont"

func useSystemFont(g *graphviz.Graphviz, name string) {
	fontPath, err := findfont.Find(name)
	if err != nil {
		panic(err)
	}

	fontData, err := os.ReadFile(fontPath)
	if err != nil {
		panic(err)
	}
	ft, err := truetype.Parse(fontData)
	if err != nil {
		panic(err)
	}
	g.SetFontFace(func(size float64) (font.Face, error) {
		opt := &truetype.Options{
			Size:              size,
		}
		return truetype.NewFace(ft, opt), nil
	})
}

func main(){
	g := graphviz.New()
	graph, err := g.Graph()
	if err != nil {
		log.Fatal().Err(err)
	}
	defer func() {
		if err := graph.Close(); err != nil {
			log.Fatal().Err(err)
		}
		_ = g.Close()
	}()

	useSystemFont(g, "Times New Roman.ttf")
}

spacedub avatar Oct 14 '22 19:10 spacedub