diff options
author | Nick White <git@njw.name> | 2020-02-27 17:45:16 +0000 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-02-27 17:45:16 +0000 |
commit | 3880414bbf2d6f2cd05e208abf919ae5ceabeddc (patch) | |
tree | dee30a151048de65a3e42cfdae7739c4502e148f /cmd/fonttobytes/main.go | |
parent | cda45588cfb796fdd2af27b1851685270df2c02b (diff) |
Reorganise all commands to be behind cmd/
Diffstat (limited to 'cmd/fonttobytes/main.go')
-rw-r--r-- | cmd/fonttobytes/main.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/cmd/fonttobytes/main.go b/cmd/fonttobytes/main.go new file mode 100644 index 0000000..8310e0f --- /dev/null +++ b/cmd/fonttobytes/main.go @@ -0,0 +1,49 @@ +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() + + // This could be done with %+v in printf, but using the decimal rather than + // hex output saves quite a few bytes, so we do that instead. + fmt.Printf("[]byte{") + for i, b := range compressed.Bytes() { + if i > 0 { + fmt.Printf(", ") + } + fmt.Printf("%d", b) + } + fmt.Printf("}\n") +} |