-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
211 lines (180 loc) · 4.99 KB
/
print.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi
import (
_ "embed"
"errors"
"fmt"
"io/fs"
"github.com/elgopher/pi/font"
"github.com/elgopher/pi/internal/sfmt"
)
// Print prints text on the screen using system font. It takes into consideration
// clipping region and camera position.
//
// Only unicode characters with code < 256 are supported. Unsupported chars
// are printed as question mark. The entire table of available chars can be
// found here: https://github.com/elgopher/pi/blob/master/internal/system-font.png
//
// Print returns the right-most x position that occurred while printing.
func Print(text string, x, y int, color byte) (rightMostX int) {
return systemFont.Print(text, x, y, color)
}
// SystemFont returns Font instance for system font. System font cannot be changed.
func SystemFont() Font {
return systemFont
}
// CustomFont returns Font instance which can be used for printing text on the screen.
// CustomFont is loaded from custom-font.png resource file.
func CustomFont() Font {
return customFont
}
// SetCustomFontWidth sets width (in pixels) for all characters below 128.
//
// By default, width is 4. For Width > 8 only 8 pixels are drawn.
func SetCustomFontWidth(w int) {
if w < 0 {
w = 0
}
if w > 8 {
w = 8
}
customFont.Width = w
}
// SetCustomFontSpecialWidth sets width (in pixels) of all special characters (code>=128).
//
// By default, width is 8. For SpecialWidth > 8 only 8 pixels are drawn.
func SetCustomFontSpecialWidth(w int) {
if w < 0 {
w = 0
}
if w > 8 {
w = 8
}
customFont.SpecialWidth = w
}
// SetCustomFontHeight sets height of line (in pixels).
//
// By default, height is 6.
func SetCustomFontHeight(height int) {
if height < 0 {
height = 0
}
customFont.Height = height
}
// Font contains all information about loaded font and provides method to Print the text.
type Font struct {
// Data contains all 256 characters sorted by their ascii-like number.
// Each character is 8 subsequent bytes, starting from the top.
// Left-most pixel in a line is bit 0. Right-most pixel in a line is bit 7.
//
// The size of slice is always 8 * 256 = 2048.
//
// Can be freely read and updated. Changes will be visible immediately.
Data []byte
// Width in pixels for all characters below 128. For Width > 8 only 8 pixels are drawn.
Width int
// SpecialWidth is a width of all special characters (code>=128)
// For SpecialWidth > 8 only 8 pixels are drawn.
SpecialWidth int
// Height of line
Height int
}
// Print prints text on the screen at given coordinates. It takes into account
// clipping region and camera position.
//
// Only unicode characters with code < 256 are supported. Unsupported chars
// are printed as question mark. The entire table of available chars can be
// found here: https://github.com/elgopher/pi/blob/master/internal/system-font.png
//
// Print returns the right-most x position that occurred while printing.
func (f Font) Print(text string, x, y int, color byte) int {
startX := x
for _, r := range text {
if r != '\n' {
width := f.printRune(r, x, y, color)
x += width
} else {
x = startX
y += f.Height
}
}
return x
}
func (f Font) printRune(r rune, sx, sy int, color byte) int {
if r > 255 {
r = '?'
}
index := int(r) * 8
clip := screen.clip
for y := 0; y < 8; y++ {
if clip.Y > sy+y-Camera.Y {
continue
}
if clip.Y+clip.H <= sy+y-Camera.Y {
continue
}
screenWidth := screen.Width()
offset := screenWidth*y + sx + sy*screenWidth - Camera.Y*screenWidth - Camera.X
line := f.Data[index+y]
for bit := 0; bit < 8; bit++ {
if clip.X > sx+bit-Camera.X {
continue
}
if clip.X+clip.W <= sx+bit-Camera.X {
continue
}
if line&(1<<bit) == 1<<bit {
screen.pix[offset+bit] = color
}
}
}
if r < 128 {
return f.Width
} else {
return f.SpecialWidth
}
}
// String returns Font as string for debugging purposes.
func (f Font) String() string {
return fmt.Sprintf("{width: %d, specialWidth: %d, height: %d, data: %s}",
f.Width, f.SpecialWidth, f.Height, sfmt.FormatBigSlice(f.Data, 512))
}
const fontDataSize = 8 * 256
var (
systemFont = Font{
Width: 4,
SpecialWidth: 8,
Height: 6,
}
//go:embed internal/system-font.png
systemFontPNG []byte
defaultCustomFont = Font{Width: 4, SpecialWidth: 8, Height: 6}
customFont = Font{
Data: make([]byte, fontDataSize),
Width: defaultCustomFont.Width,
SpecialWidth: defaultCustomFont.SpecialWidth,
Height: defaultCustomFont.Height,
}
)
func init() {
var err error
systemFont.Data, err = font.Load(systemFontPNG)
if err != nil {
panic(err)
}
}
func loadCustomFont(resources fs.ReadFileFS) error {
fileContents, err := resources.ReadFile("custom-font.png")
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return fmt.Errorf("error loading custom-font.png file: %w", err)
}
customFont.Data, err = font.Load(fileContents)
if err != nil {
return fmt.Errorf("invalid custom-font.png: %w", err)
}
return nil
}