// Package caption provides a caption rendering interface that prioritises ease of use. package caption import ( "sync" "github.com/golang/freetype/truetype" "golang.org/x/image/font" "golang.org/x/image/font/gofont/goregular" ) var ( // package-wide default font defaultFont = goregular.TTF ) // A Context is used for efficient line rendering. // The zero value is ready to use. Do not copy a non-zero Context. type Context struct { defaultFont font.Face defaultFontErr error defaultFontOnce sync.Once defaultFontData []byte defaultFontOpts *truetype.Options } /* NewContext initialises Context with a custom default truetype font and returns its address. Defaults baked into Context will only take effect if both Line.Font and Line.Options are zero. */ func NewContext(v []byte, opts *truetype.Options) *Context { return &Context{defaultFontData: v, defaultFontOpts: opts} } // parseFont is called before using Context.defaultFont. func (ctx *Context) parseFont() error { ctx.defaultFontOnce.Do(func() { if len(ctx.defaultFontData) == 0 { ctx.defaultFontData = defaultFont } if f, err := truetype.Parse(ctx.defaultFontData); err != nil { ctx.defaultFontErr = err return } else { ctx.defaultFont = truetype.NewFace(f, ctx.defaultFontOpts) } }) return ctx.defaultFontErr }