├── README.md ├── ttfs ├── HanyiJianhei.ttf ├── LantingXihei.ttf └── LantingJianhei.ttf └── gen.go /README.md: -------------------------------------------------------------------------------- 1 | # gofont 2 | 3 | generates ttfs fonts to go code -------------------------------------------------------------------------------- /ttfs/HanyiJianhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-baa/gofont/master/ttfs/HanyiJianhei.ttf -------------------------------------------------------------------------------- /ttfs/LantingXihei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-baa/gofont/master/ttfs/LantingXihei.ttf -------------------------------------------------------------------------------- /ttfs/LantingJianhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-baa/gofont/master/ttfs/LantingJianhei.ttf -------------------------------------------------------------------------------- /gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // +build ignore 6 | 7 | package main 8 | 9 | // This program generates the subdirectories of Go packages that contain []byte 10 | // versions of the TrueType font files under ./ttfs. 11 | // 12 | // Currently, "go run gen.go" needs to be run manually. This isn't done by the 13 | // usual "go generate" mechanism as there isn't any other Go code in this 14 | // directory (excluding sub-directories) to attach a "go:generate" line to. 15 | // 16 | // In any case, code generation should only need to happen when the underlying 17 | // TTF files change, which isn't expected to happen frequently. 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "go/format" 23 | "io/ioutil" 24 | "log" 25 | "os" 26 | "path/filepath" 27 | "strings" 28 | ) 29 | 30 | const suffix = ".ttf" 31 | 32 | func main() { 33 | ttfs, err := os.Open("ttfs") 34 | if err != nil { 35 | log.Fatal(err) 36 | } 37 | defer ttfs.Close() 38 | 39 | infos, err := ttfs.Readdir(-1) 40 | if err != nil { 41 | log.Fatal(err) 42 | } 43 | for _, info := range infos { 44 | ttfName := info.Name() 45 | if !strings.HasSuffix(ttfName, suffix) { 46 | continue 47 | } 48 | do(ttfName) 49 | } 50 | } 51 | 52 | func do(ttfName string) { 53 | fontName := fontName(ttfName) 54 | pkgName := pkgName(ttfName) 55 | if err := os.Mkdir(pkgName, 0777); err != nil && !os.IsExist(err) { 56 | log.Fatal(err) 57 | } 58 | src, err := ioutil.ReadFile(filepath.Join("ttfs", ttfName)) 59 | if err != nil { 60 | log.Fatal(err) 61 | } 62 | 63 | desc := "a proportional-width, sans-serif" 64 | if strings.Contains(ttfName, "Mono") { 65 | desc = "a fixed-width, slab-serif" 66 | } 67 | 68 | b := new(bytes.Buffer) 69 | fmt.Fprintf(b, "// generated by go run gen.go; DO NOT EDIT\n\n") 70 | fmt.Fprintf(b, "// Package %s provides the %q TrueType font\n", pkgName, fontName) 71 | fmt.Fprintf(b, "// from the Go font family. It is %s font.\n", desc) 72 | fmt.Fprintf(b, "//\n") 73 | fmt.Fprintf(b, "// See https://blog.golang.org/go-fonts for details.\n") 74 | fmt.Fprintf(b, "package %s\n\n", pkgName) 75 | fmt.Fprintf(b, "// TTF is the data for the %q TrueType font.\n", fontName) 76 | fmt.Fprintf(b, "var TTF = []byte{") 77 | for i, x := range src { 78 | if i&15 == 0 { 79 | b.WriteByte('\n') 80 | } 81 | fmt.Fprintf(b, "%#02x,", x) 82 | } 83 | fmt.Fprintf(b, "\n}\n") 84 | 85 | dst, err := format.Source(b.Bytes()) 86 | if err != nil { 87 | log.Fatal(err) 88 | } 89 | if err := ioutil.WriteFile(filepath.Join(pkgName, "data.go"), dst, 0666); err != nil { 90 | log.Fatal(err) 91 | } 92 | } 93 | 94 | // fontName maps "Go-Regular.ttf" to "Go Regular". 95 | func fontName(ttfName string) string { 96 | s := ttfName[:len(ttfName)-len(suffix)] 97 | s = strings.Replace(s, "-", " ", -1) 98 | return s 99 | } 100 | 101 | // pkgName maps "Go-Regular.ttf" to "goregular". 102 | func pkgName(ttfName string) string { 103 | s := ttfName[:len(ttfName)-len(suffix)] 104 | s = strings.Replace(s, "-", "", -1) 105 | s = strings.ToLower(s) 106 | return s 107 | } --------------------------------------------------------------------------------