git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

font/gofont/gen.go (2.7K)

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