/
typesetting.go
59 lines (53 loc) · 1.23 KB
/
typesetting.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
package main
import (
"fmt"
"os"
"unicode/utf8"
)
func printMessage(message [][]int) {
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
if message[i][j] > 0 {
fmt.Print(" . ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
func render(msg string) [][]int {
matrix := make([][]int, numRows)
for i := range matrix {
matrix[i] = make([]int, numCols)
}
msgWidth := 0
for _, char := range msg {
msgWidth += font[char].width
}
numRunes := utf8.RuneCountInString(msg)
spaceBetweenChars := (numCols - msgWidth) / (numRunes - 1)
if spaceBetweenChars < 1 {
fmt.Printf("Message %q is too long. Try a shorter message.", msg)
os.Exit(1)
}
spaceBetweenChars = 1
margins := numCols - msgWidth - spaceBetweenChars*(numRunes-1)
xOffset := margins / 2
for _, char := range msg {
if charInfo, ok := font[char]; ok {
charWidth := charInfo.width
charMatrix := charInfo.matrix
for r := 0; r < numRows; r++ {
for c := 0; c < charWidth; c++ {
matrix[r][c+xOffset] = charMatrix[r][c]
}
}
xOffset += charWidth + spaceBetweenChars
} else {
fmt.Printf("Character %q is not recognized. Add it to characters.go and subimt a PR.\n", string(char))
os.Exit(1)
}
}
return matrix
}