~mendelmaleh/dummy

ref: b862a107cb4810e7514eccf69deb3fc5f6f5dc4c dummy/cmd/dummy/main.go -rw-r--r-- 2.8 KiB
b862a107Mendel E Document new endpoints 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
	}
}