summaryrefslogtreecommitdiff
path: root/fonttobytes/main.go
blob: ed0752c0227954d0d0db22f17c18207915c45c8b (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
48
49
50
package main

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

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
	}

	f, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatalln("Failed to open file", flag.Arg(0), err)
	}
	fontbytes, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalln("Failed to read file", flag.Arg(0), err)
	}

	var compressed bytes.Buffer
	w := zlib.NewWriter(&compressed)
	w.Write(fontbytes)
	w.Close()

	fmt.Printf("[]byte{")
	first := true
	for _, b := range compressed.Bytes() {
		if first {
			fmt.Printf("%d", b)
			first = false
			continue
		}
		fmt.Printf(", %d", b)
	}
	fmt.Printf("}\n")
}