-
Notifications
You must be signed in to change notification settings - Fork 4
/
ascii-render-char.go
71 lines (60 loc) · 1.18 KB
/
ascii-render-char.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/qeedquan/go-media/stb/stbtt"
)
var flags struct {
Font string
Scale float64
}
func main() {
flags.Scale = 20
flag.StringVar(&flags.Font, "f", flags.Font, "use font file")
flag.Float64Var(&flags.Scale, "s", flags.Scale, "use scale")
flag.Usage = usage
flag.Parse()
if flags.Font == "" || flag.NArg() < 1 {
usage()
}
f, err := loadfont(flags.Font)
ck(err)
defer f.Free()
for _, arg := range flag.Args() {
for _, ch := range arg {
render(f, ch)
}
}
}
func render(f *stbtt.FontInfo, ch rune) {
b, w, h, _, _ := f.CodepointBitmap(0, f.ScaleForPixelHeight(flags.Scale), ch)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
const p = " .:ioVM@"
fmt.Printf("%c", p[b[y*w+x]>>5])
}
fmt.Println()
}
fmt.Println()
}
func loadfont(name string) (*stbtt.FontInfo, error) {
data, err := os.ReadFile(name)
if err != nil {
return nil, err
}
var f stbtt.FontInfo
err = f.Init(data, stbtt.GetFontOffsetForIndex(data, 0))
return &f, nil
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [options] chars ...")
flag.PrintDefaults()
os.Exit(2)
}