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
|
//! Hikari Text Renderer
const framebuffer = @import("framebuffer.zig");
const font_mod = @import("font.zig");
pub const TextRenderer = struct {
fb: *framebuffer.Framebuffer,
font: *const font_mod.Font,
cursor_x: u32,
cursor_y: u32,
fg_color: framebuffer.Color,
bg_color: framebuffer.Color,
columns: u32,
rows: u32,
pub fn initialize(
fb: *framebuffer.Framebuffer,
font: *const font_mod.Font,
) TextRenderer {
return TextRenderer{
.fb = fb,
.font = font,
.cursor_x = 0,
.cursor_y = 0,
.fg_color = framebuffer.Color.white,
.bg_color = framebuffer.Color.black,
.columns = fb.width / font.width,
.rows = fb.height / font.height,
};
}
pub fn set_colors(self: *TextRenderer, fg: framebuffer.Color, bg: framebuffer.Color) void {
self.fg_color = fg;
self.bg_color = bg;
}
pub fn set_cursor(self: *TextRenderer, col: u32, row: u32) void {
self.cursor_x = col;
self.cursor_y = row;
}
pub fn draw_char(self: *TextRenderer, codepoint: u32) void {
const glyph = self.font.get_glyph(codepoint) orelse self.font.get_glyph('?') orelse return;
const screen_x = self.cursor_x * self.font.width;
const screen_y = self.cursor_y * self.font.height;
var y: u32 = 0;
while (y < self.font.height) : (y += 1) {
var x: u32 = 0;
while (x < self.font.width) : (x += 1) {
const pixel_on = self.font.get_glyph_pixel(glyph, x, y);
const color = if (pixel_on) self.fg_color else self.bg_color;
self.fb.put_pixel(screen_x + x, screen_y + y, color);
}
}
}
pub fn draw_char_at(self: *TextRenderer, codepoint: u32, col: u32, row: u32) void {
const old_x = self.cursor_x;
const old_y = self.cursor_y;
self.cursor_x = col;
self.cursor_y = row;
self.draw_char(codepoint);
self.cursor_x = old_x;
self.cursor_y = old_y;
}
pub fn put_char(self: *TextRenderer, c: u8) void {
switch (c) {
'\n' => {
self.cursor_x = 0;
self.cursor_y += 1;
if (self.cursor_y >= self.rows) {
self.scroll_up();
self.cursor_y = self.rows - 1;
}
},
'\r' => {
self.cursor_x = 0;
},
'\t' => {
const tab_stop = 8;
self.cursor_x = ((self.cursor_x / tab_stop) + 1) * tab_stop;
if (self.cursor_x >= self.columns) {
self.cursor_x = 0;
self.cursor_y += 1;
if (self.cursor_y >= self.rows) {
self.scroll_up();
self.cursor_y = self.rows - 1;
}
}
},
0x08 => {
if (self.cursor_x > 0) {
self.cursor_x -= 1;
}
},
else => {
self.draw_char(c);
self.cursor_x += 1;
if (self.cursor_x >= self.columns) {
self.cursor_x = 0;
self.cursor_y += 1;
if (self.cursor_y >= self.rows) {
self.scroll_up();
self.cursor_y = self.rows - 1;
}
}
},
}
}
pub fn print(self: *TextRenderer, str: []const u8) void {
for (str) |c| {
self.put_char(c);
}
}
pub fn print_line(self: *TextRenderer, str: []const u8) void {
self.print(str);
self.put_char('\n');
}
pub fn scroll_up(self: *TextRenderer) void {
const line_height = self.font.height;
const scroll_pixels = (self.rows - 1) * line_height;
self.fb.copy_rect(0, line_height, 0, 0, self.fb.width, scroll_pixels);
self.fb.fill_rect(
0,
scroll_pixels,
self.fb.width,
line_height,
self.bg_color,
);
}
pub fn clear_screen(self: *TextRenderer) void {
self.fb.clear(self.bg_color);
self.cursor_x = 0;
self.cursor_y = 0;
}
pub fn clear_line(self: *TextRenderer, row: u32) void {
if (row >= self.rows) {
return;
}
self.fb.fill_rect(
0,
row * self.font.height,
self.fb.width,
self.font.height,
self.bg_color,
);
}
pub fn print_u64(self: *TextRenderer, value: u64) void {
var buf: [20]u8 = undefined;
var i: usize = 0;
var v = value;
if (v == 0) {
self.put_char('0');
return;
}
while (v > 0) {
buf[i] = @truncate((v % 10) + '0');
v /= 10;
i += 1;
}
while (i > 0) {
i -= 1;
self.put_char(buf[i]);
}
}
pub fn print_hex(self: *TextRenderer, value: u64) void {
const hex_chars = "0123456789ABCDEF";
self.print("0x");
var started = false;
var shift: u6 = 60;
while (true) {
const nibble: u4 = @truncate((value >> shift) & 0xF);
if (nibble != 0 or started or shift == 0) {
self.put_char(hex_chars[nibble]);
started = true;
}
if (shift == 0) break;
shift -= 4;
}
}
};
|