main.go raw

   1  // Copyright 2016 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that
   2  // can be found in the LICENSE file.
   3  
   4  // this will run this generator if go generate is called on this directory
   5  //go:generate go run main.go logmain.go
   6  
   7  package main
   8  
   9  // This program generates the subdirectories of Go packages that contain []byte versions of the TrueType font files
  10  // under ./ttfs.
  11  //
  12  // Currently, "go run gen.go" needs to be run manually. This isn't done by the usual "go generate" mechanism as there
  13  // isn't any other Go code in this directory (excluding sub-directories) to attach a "go:generate" line to.
  14  //
  15  // In any case, code generation should only need to happen when the underlying TTF files change, which isn't expected to
  16  // happen frequently.
  17  
  18  import (
  19  	"bytes"
  20  	"fmt"
  21  	"go/format"
  22  	"io/ioutil"
  23  	"os"
  24  	"path/filepath"
  25  	"strings"
  26  )
  27  
  28  const suffix = ".ttf"
  29  
  30  func main() {
  31  	ttfs, e := os.Open("ttfs")
  32  	if e != nil {
  33  		F.Ln(e)
  34  	}
  35  	defer func() {
  36  		if e = ttfs.Close(); E.Chk(e) {
  37  		}
  38  	}()
  39  	
  40  	infos, e := ttfs.Readdir(-1)
  41  	if e != nil {
  42  		F.Ln(e)
  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 e := os.Mkdir(pkgName, 0777); e != nil && !os.IsExist(e) {
  57  		F.Ln(e)
  58  	}
  59  	src, e := ioutil.ReadFile(filepath.Join("ttfs", ttfName))
  60  	if e != nil {
  61  		F.Ln(e)
  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 github.com/p9c/gel/fonts; DO NOT EDIT\n\n")
  71  	_, _ = fmt.Fprintf(b, "package %s\n\n", pkgName)
  72  	_, _ = fmt.Fprintf(b, "// TTF is the data for the %q TrueType font.\n", fontName)
  73  	_, _ = fmt.Fprintf(b, "var TTF = []byte{")
  74  	for i, x := range src {
  75  		if i&15 == 0 {
  76  			b.WriteByte('\n')
  77  		}
  78  		_, _ = fmt.Fprintf(b, "%#02x,", x)
  79  	}
  80  	_, _ = fmt.Fprintf(b, "\n}\n")
  81  	
  82  	dst, e := format.Source(b.Bytes())
  83  	if e != nil {
  84  		F.Ln(e)
  85  	}
  86  	if e := ioutil.WriteFile(filepath.Join(pkgName, "data.go"), dst, 0666); E.Chk(e) {
  87  		F.Ln(e)
  88  	}
  89  }
  90  
  91  // fontName maps "Go-Regular.ttf" to "Go Regular".
  92  func fontName(ttfName string) string {
  93  	s := ttfName[:len(ttfName)-len(suffix)]
  94  	s = strings.Replace(s, "-", " ", -1)
  95  	return s
  96  }
  97  
  98  // pkgName maps "Go-Regular.ttf" to "goregular".
  99  func pkgName(ttfName string) string {
 100  	s := ttfName[:len(ttfName)-len(suffix)]
 101  	s = strings.Replace(s, "-", "", -1)
 102  	s = strings.ToLower(s)
 103  	return s
 104  }
 105