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  // todo: lol!
  16  //
  17  // In any case, code generation should only need to happen when the underlying TTF files change, which isn't expected to
  18  // happen frequently.
  19  
  20  import (
  21  	"bytes"
  22  	"fmt"
  23  	"go/format"
  24  	"io/ioutil"
  25  	"os"
  26  	"path/filepath"
  27  	"strings"
  28  )
  29  
  30  const suffix = ".ttf"
  31  
  32  func main() {
  33  	ttfs, e := os.Open("ttfs")
  34  	if e != nil {
  35  		F.Ln(e)
  36  	}
  37  	defer func() {
  38  		if e = ttfs.Close(); E.Chk(e) {
  39  		}
  40  	}()
  41  	
  42  	infos, e := ttfs.Readdir(-1)
  43  	if e != nil {
  44  		F.Ln(e)
  45  	}
  46  	for _, info := range infos {
  47  		ttfName := info.Name()
  48  		if !strings.HasSuffix(ttfName, suffix) {
  49  			continue
  50  		}
  51  		do(ttfName)
  52  	}
  53  }
  54  
  55  func do(ttfName string) {
  56  	fontName := fontName(ttfName)
  57  	pkgName := pkgName(ttfName)
  58  	if e := os.Mkdir(pkgName, 0777); e != nil && !os.IsExist(e) {
  59  		F.Ln(e)
  60  	}
  61  	src, e := ioutil.ReadFile(filepath.Join("ttfs", ttfName))
  62  	if e != nil {
  63  		F.Ln(e)
  64  	}
  65  	
  66  	// desc := "a proportional-width, sans-serif"
  67  	// if strings.Contains(ttfName, "Mono") {
  68  	// 	desc = "a fixed-width, slab-serif"
  69  	// }
  70  	
  71  	b := new(bytes.Buffer)
  72  	_, _ = fmt.Fprintf(b, "// generated by go run github.com/p9c/p9/pkg/gel/fonts; DO NOT EDIT\n\n")
  73  	_, _ = fmt.Fprintf(b, "package %s\n\n", pkgName)
  74  	_, _ = fmt.Fprintf(b, "// TTF is the data for the %q TrueType font.\n", fontName)
  75  	_, _ = fmt.Fprintf(b, "var TTF = []byte{")
  76  	for i, x := range src {
  77  		if i&15 == 0 {
  78  			b.WriteByte('\n')
  79  		}
  80  		_, _ = fmt.Fprintf(b, "%#02x,", x)
  81  	}
  82  	_, _ = fmt.Fprintf(b, "\n}\n")
  83  	
  84  	dst, e := format.Source(b.Bytes())
  85  	if e != nil {
  86  		F.Ln(e)
  87  	}
  88  	if e := ioutil.WriteFile(filepath.Join(pkgName, "data.go"), dst, 0666); E.Chk(e) {
  89  		F.Ln(e)
  90  	}
  91  }
  92  
  93  // fontName maps "Go-Regular.ttf" to "Go Regular".
  94  func fontName(ttfName string) string {
  95  	s := ttfName[:len(ttfName)-len(suffix)]
  96  	s = strings.Replace(s, "-", " ", -1)
  97  	return s
  98  }
  99  
 100  // pkgName maps "Go-Regular.ttf" to "goregular".
 101  func pkgName(ttfName string) string {
 102  	s := ttfName[:len(ttfName)-len(suffix)]
 103  	s = strings.Replace(s, "-", "", -1)
 104  	s = strings.ToLower(s)
 105  	return s
 106  }
 107