<commit_before>package render

import (
	"image"
	"io/ioutil"
	"path/filepath"
	"strings"

	"github.com/golang/freetype/truetype"
	"golang.org/x/image/font"

	"bitbucket.org/oakmoundstudio/oak/dlog"
)

var (
	fontdir string

	defaultHinting  font.Hinting
	defaultSize     float64
	defaultDPI      float64
	defaultColor    image.Image
	defaultFontFile string

	DefFontGenerator = FontGenerator{}

	loadedFonts = make(map[string]*truetype.Font)
)

type FontGenerator struct {
	File    string
	Color   string
	Size    float64
	Hinting string
	DPI     float64
}

func DefFont() *Font {
	return DefFontGenerator.Generate()
}

func (fg *FontGenerator) Generate() *Font {

	// Replace zero values with defaults
	if fg.File == "" {
		fg.File = defaultFontFile
	}
	if fg.Size == 0 {
		fg.Size = defaultSize
	}
	if fg.DPI == 0 {
		fg.DPI = defaultDPI
	}

	return &Font{
		FontGenerator: *fg,
		Drawer: font.Drawer{
			// Color and hinting zero values are replaced
			// by their respective parse functions in the
			// zero case.
			Src: parseFontColor(fg.Color),
			Face: truetype.NewFace(LoadFont(fg.File), &truetype.Options{
				Size:    fg.Size,
				DPI:     fg.DPI,
				Hinting: parseFontHinting(fg.Hinting),
			}),
		},
	}

}

func (fg *FontGenerator) Copy() *FontGenerator {
	newFg := new(FontGenerator)
	*newFg = *fg
	return newFg
}

type Font struct {
	FontGenerator
	font.Drawer
}

func (f *Font) Refresh() {
	f = f.Generate()
}

func (f *Font) Copy() *Font {
	return f.Generate()
}

func (f *Font) Reset() {
	// Generate will return all defaults with no args
	f.FontGenerator = FontGenerator{}
	f = f.Generate()
}

func SetFontDefaults(wd, assetPath, fontPath, hinting, color, file string, size, dpi float64) {
	fontdir = filepath.Join(
		wd,
		assetPath,
		fontPath)
	defaultHinting = parseFontHinting(hinting)
	defaultSize = size
	defaultDPI = dpi
	defaultColor = parseFontColor(color)
	defaultFontFile = file
}

func parseFontHinting(hintType string) (faceHinting font.Hinting) {
	hintType = strings.ToLower(hintType)
	switch hintType {
	case "none":
		faceHinting = font.HintingNone
	case "vertical":
		faceHinting = font.HintingVertical
	case "full":
		faceHinting = font.HintingFull
	default:
		dlog.Error("Unable to parse font hinting, ", hintType)
		faceHinting = font.HintingNone
	}
	return faceHinting
}

func parseFontColor(s string) image.Image {
	s = strings.ToLower(s)
	switch s {
	case "white":
		return image.White
	case "black":
		return image.Black
	default:
		return defaultColor
	}
}

func LoadFont(fontFile string) *truetype.Font {<commit_msg>Added green text to font colors (We should probably change how font colors work)<commit_after>package render

import (
	"image"
	"image/color"
	"io/ioutil"
	"path/filepath"
	"strings"

	"github.com/golang/freetype/truetype"
	"golang.org/x/image/font"

	"bitbucket.org/oakmoundstudio/oak/dlog"
)

var (
	fontdir string

	defaultHinting  font.Hinting
	defaultSize     float64
	defaultDPI      float64
	defaultColor    image.Image
	defaultFontFile string

	DefFontGenerator = FontGenerator{}

	loadedFonts = make(map[string]*truetype.Font)
)

type FontGenerator struct {
	File    string
	Color   string
	Size    float64
	Hinting string
	DPI     float64
}

func DefFont() *Font {
	return DefFontGenerator.Generate()
}

func (fg *FontGenerator) Generate() *Font {

	// Replace zero values with defaults
	if fg.File == "" {
		fg.File = defaultFontFile
	}
	if fg.Size == 0 {
		fg.Size = defaultSize
	}
	if fg.DPI == 0 {
		fg.DPI = defaultDPI
	}

	return &Font{
		FontGenerator: *fg,
		Drawer: font.Drawer{
			// Color and hinting zero values are replaced
			// by their respective parse functions in the
			// zero case.
			Src: parseFontColor(fg.Color),
			Face: truetype.NewFace(LoadFont(fg.File), &truetype.Options{
				Size:    fg.Size,
				DPI:     fg.DPI,
				Hinting: parseFontHinting(fg.Hinting),
			}),
		},
	}

}

func (fg *FontGenerator) Copy() *FontGenerator {
	newFg := new(FontGenerator)
	*newFg = *fg
	return newFg
}

type Font struct {
	FontGenerator
	font.Drawer
}

func (f *Font) Refresh() {
	f = f.Generate()
}

func (f *Font) Copy() *Font {
	return f.Generate()
}

func (f *Font) Reset() {
	// Generate will return all defaults with no args
	f.FontGenerator = FontGenerator{}
	f = f.Generate()
}

func SetFontDefaults(wd, assetPath, fontPath, hinting, color, file string, size, dpi float64) {
	fontdir = filepath.Join(
		wd,
		assetPath,
		fontPath)
	defaultHinting = parseFontHinting(hinting)
	defaultSize = size
	defaultDPI = dpi
	defaultColor = parseFontColor(color)
	defaultFontFile = file
}

func parseFontHinting(hintType string) (faceHinting font.Hinting) {
	hintType = strings.ToLower(hintType)
	switch hintType {
	case "none":
		faceHinting = font.HintingNone
	case "vertical":
		faceHinting = font.HintingVertical
	case "full":
		faceHinting = font.HintingFull
	default:
		dlog.Error("Unable to parse font hinting, ", hintType)
		faceHinting = font.HintingNone
	}
	return faceHinting
}

func parseFontColor(s string) image.Image {
	s = strings.ToLower(s)
	switch s {
	case "white":
		return image.White
	case "black":
		return image.Black
	case "green":
		return image.NewUniform(color.RGBA{0, 255, 0, 255})
	default:
		return defaultColor
	}
}

func LoadFont(fontFile string) *truetype.Font {
{'fraction_non_alphanumeric': 0.11039575837909486, 'fraction_numerical': 0.006059458435902291, 'mean_word_length': 3.7800904977375565, 'pattern_counts': {'":': 11, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3460925', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1894, 'n_words': 477}