summaryrefslogtreecommitdiff
path: root/cmd/fonttobytes/main.go
blob: 011baa1c5d42b720ed5905b7bfcfbe1d1135898a (plain)
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
package main

import (
	"bytes"
	"compress/zlib"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
)

func main() {
	flag.Usage = func() {
		fmt.Fprintln(flag.CommandLine.Output(), "Usage: fonttobytes font.ttf")
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() != 1 {
		flag.Usage()
		return
	}

	font, err := ioutil.ReadFile(flag.Arg(0))
	if err != nil {
		log.Fatalln(err)
	}

	// compress with zlib
	var buf bytes.Buffer
	w := zlib.NewWriter(&buf)
	w.Write(font)
	w.Close()

	// this could be done more simply with %+v, but that takes up
	// significantly more space due to printing each byte in hex
	// rather than dec format.

	fmt.Printf("[]byte{")
	for i, b := range buf.Bytes() {
		if i > 0 {
			fmt.Printf(", ")
		}
		fmt.Printf("%d", b)
	}
	fmt.Printf("}\n")
}