~mendelmaleh/dummy

4d97d48ec88f19b76f6e6598fd9c3d1301c80b30 — Mendel E 11 days ago f69361c
Add cli app
1 files changed, 137 insertions(+), 0 deletions(-)

A cmd/dummy/main.go
A cmd/dummy/main.go => cmd/dummy/main.go +137 -0
@@ 0,0 1,137 @@
package main

import (
	"flag"
	"image/png"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"runtime"
	"runtime/pprof"

	"git.sr.ht/~mendelmaleh/dummy"
	"git.sr.ht/~mendelmaleh/freetype"
	"git.sr.ht/~mendelmaleh/freetype/truetype"
	"github.com/pelletier/go-toml"
)

type Config struct {
	Dummy struct {
		Fonts, Font string
	}
}

type Options struct {
	dummy.Options
}

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

func main() {
	cpu, err := os.Create("cpu.prof")
	if err != nil {
		log.Fatal("could not create CPU profile: ", err)

	}
	defer cpu.Close() // error handling omitted for example
	if err := pprof.StartCPUProfile(cpu); err != nil {
		log.Fatal("could not start CPU profile: ", err)

	}
	defer pprof.StopCPUProfile()

	doc, err := ioutil.ReadFile("config.toml")
	if err != nil {
		log.Fatal(err)
	}

	config := Config{}
	err = toml.Unmarshal(doc, &config)
	if err != nil {
		log.Fatal(err)
	}

	fontfiles, err := ioutil.ReadDir(config.Dummy.Fonts)
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range fontfiles {
		if filepath.Ext(file.Name()) != ".ttf" {
			continue
		}

		filebytes, err := ioutil.ReadFile(config.Dummy.Fonts + file.Name())
		if err != nil {
			log.Println(err) // don't exit if one font doesn't work
			continue
		}

		font, err := freetype.ParseFont(filebytes)
		if err != nil {
			log.Println(err)
			continue
		}

		// fmt.Println(font.Name(truetype.NameIDFontFamily))
		fonts[font.Name(truetype.NameIDFontFamily)] = font
	}

	if len(fonts) < 1 {
		log.Fatal("No fonts loaded")
	}

	// set default font after it's loaded
	font, ok := fonts[config.Dummy.Font]
	if !ok {
		log.Fatal("Default font not found")
	}

	dopt := dummy.DefaultOptions
	dopt.Font = font

	opt := Options{dopt.Copy()}
	opt.FromFlags()

	im := dummy.Image(opt.Options)
	png.Encode(os.Stdout, im)

	mem, err := os.Create("mem.prof")
	if err != nil {
		log.Fatal("could not create memory profile: ", err)

	}
	defer mem.Close() // error handling omitted for example
	runtime.GC()      // get up-to-date statistics
	if err := pprof.WriteHeapProfile(mem); err != nil {
		log.Fatal("could not write memory profile: ", err)

	}
}

func (opt *Options) FromFlags() {
	// image
	flag.IntVar(&opt.Width, "width", opt.Width, "set the width of the image")
	flag.IntVar(&opt.Height, "height", -1, "set the height of the image")
	flag.StringVar(&opt.Mask, "mask", opt.Mask, "set the mask for the image")
	// TODO: colors

	// text
	flag.StringVar(&opt.Text, "text", opt.Text, "set the text for the image")
	flag.Float64Var(&opt.Size, "size", opt.Size, "set the font size for the text")
	flag.Float64Var(&opt.DPI, "dpi", opt.DPI, "set the dpi size for the font")

	var font string
	flag.StringVar(&font, "font", font, "set the font for the text")

	flag.Parse()

	if f, ok := fonts[font]; ok {
		opt.Font = f
	}

	if opt.Height == -1 {
		opt.Height = opt.Width
	}
}