goccy / go-graphviz

Go bindings for Graphviz
MIT License
733 stars 70 forks source link

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

Open spacedub opened 2 years ago

spacedub commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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")
}
goccy commented 1 month ago

@spacedub There is still room for improvement in font selection, but I have adjusted the font size, so I believe this issue is resolved. Could you please confirm?