-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathfont.go
112 lines (102 loc) · 2.38 KB
/
font.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package figure
import (
"bufio"
"bytes"
"io"
"path"
"strings"
)
const defaultFont = "standard"
var colors = map[string]string{
"reset": "\033[0m",
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"purple": "\033[35m",
"cyan": "\033[36m",
"gray": "\033[37m",
"white": "\033[97m",
}
type font struct {
name string
height int
baseline int
hardblank byte
reverse bool
letters [][]string
}
func newFont(name string) (font font) {
font.setName(name)
fontBytes, err := Asset(path.Join("fonts", font.name+".flf"))
if err != nil {
panic(err)
}
fontBytesReader := bytes.NewReader(fontBytes)
scanner := bufio.NewScanner(fontBytesReader)
font.setAttributes(scanner)
font.setLetters(scanner)
return font
}
func newFontFromReader(reader io.Reader) (font font) {
scanner := bufio.NewScanner(reader)
font.setAttributes(scanner)
font.setLetters(scanner)
return font
}
func (font *font) setName(name string) {
font.name = name
if len(name) < 1 {
font.name = defaultFont
}
}
func (font *font) setAttributes(scanner *bufio.Scanner) {
for scanner.Scan() {
text := scanner.Text()
if strings.HasPrefix(text, signature) {
font.height = getHeight(text)
font.baseline = getBaseline(text)
font.hardblank = getHardblank(text)
font.reverse = getReverse(text)
break
}
}
}
func (font *font) setLetters(scanner *bufio.Scanner) {
font.letters = append(font.letters, make([]string, font.height, font.height)) //TODO: set spaces from flf
for i := range font.letters[0] {
font.letters[0][i] = " "
} //TODO: set spaces from flf
letterIndex := 0
for scanner.Scan() {
text, cutLength, letterIndexInc := scanner.Text(), 1, 0
if lastCharLine(text, font.height) {
font.letters = append(font.letters, []string{})
letterIndexInc = 1
if font.height > 1 {
cutLength = 2
}
}
if letterIndex > 0 {
appendText := ""
if len(text) > 1 {
appendText = text[:len(text)-cutLength]
}
font.letters[letterIndex] = append(font.letters[letterIndex], appendText)
}
letterIndex += letterIndexInc
}
}
func (font *font) evenLetters() {
var longest int
for _, letter := range font.letters {
if len(letter) > 0 && len(letter[0]) > longest {
longest = len(letter[0])
}
}
for _, letter := range font.letters {
for i, row := range letter {
letter[i] = row + strings.Repeat(" ", longest-len(row))
}
}
}