Skip to content

Use Go fonts by default #127

Open
Open

Description

@gerald1248

This is very much an enhancement: would it be an option to switch to Go fonts by default?

Visually, I think it's sensible to adopt a shared look and feel for Go libraries. The font's quirky but it immediately says 'Golang'.

More importantly, though, the library could shed its dependency on the filesystem. Clearly go install works fine if the system is set up just so, but I much prefer distributing single-file binaries with everything packed in. (So I just double-click that? Uh, terminal's over here but I guess so. Boom no fonts.)

The font naming that's in place currently is clearly valuable (though I prefer to load the fonts via go-bindata's byte arrays) but for a general-purpose library it's much simpler to default to:

import (
  "golang.org/x/image/font/gofont/gobold"
  "golang.org/x/image/font/gofont/gobolditalic"
  "golang.org/x/image/font/gofont/goitalic"
  "golang.org/x/image/font/gofont/goregular"
)
//...
regular, err := truetype.Parse(goregular.TTF)

(It's what I'm using in my custom FontCache but it would simplify the library quite a bit.)

Activity

  1. llgcode commented on Dec 15, 2016

    @llgcode
    Owner

    Hi @gerald1248
    An article on how to use it in draw2d can be interesting.

  2. drahoslove commented on Dec 18, 2016

    @drahoslove
    Contributor

    Thanks, @gerald1248, for this idea.
    I'm currently using go fonts in draw2d like this:
    (Note: I use only Name attribute of FontData for simplification.)

    package gui
    
    import (
    	"fmt"
    	"github.com/llgcode/draw2d"
    	"github.com/golang/freetype/truetype"
    	"golang.org/x/image/font/gofont/goregular"
    	"golang.org/x/image/font/gofont/gobold"
    	"golang.org/x/image/font/gofont/goitalic"
    	"golang.org/x/image/font/gofont/gomono"
    )
    
    type MyFontCache map[string]*truetype.Font
    
    func (fc MyFontCache) Store(fd draw2d.FontData, font *truetype.Font) {
    	fc[fd.Name] = font
    }
    
    func (fc MyFontCache) Load(fd draw2d.FontData) (*truetype.Font, error) {
    	font, stored := fc[fd.Name]
    	if !stored {
    		return nil, fmt.Errorf("Font %s is not stored in font cache.", fd.Name)
    	}
    	return font, nil
    }
    
    func init () {
    	fontCache := MyFontCache{}
    
    	TTFs := map[string]([]byte){
    		"goregular": goregular.TTF,
    		"gobold": gobold.TTF,
    		"goitalic": goitalic.TTF,
    		"gomono": gomono.TTF,
    	}
    
    	for fontName, TTF := range TTFs {
    		font, err := truetype.Parse(TTF)
    		if err != nil {
    			panic(err)
    		}
    		fontCache.Store(draw2d.FontData{Name: fontName}, font)
    	}
    
    	draw2d.SetFontCache(fontCache)
    }

    Maybe this font (goregular) could be part of draw2d as default font. They (all go fonts) could be automatically preloaded in similar way in defaultFontCache.

  3. gerald1248 commented on Dec 21, 2016

    @gerald1248
    ContributorAuthor

    @Drahoslav7: This is pretty much exactly what I'm using in my custom font cache (GitHub).

    The question is how do deal with the existing library layout and expectation that fonts are fetched from the filesystem. For example, I've thrown out the resource folder but then I have to set the font directory to . to avoid errors being thrown.

    The existing font namer etc. is all working and useful but ideally it would be an optional extra. As far as I can tell, mapping bytearrays to the font descriptors the way you've done looks to me like the most Go-like way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions