-
Notifications
You must be signed in to change notification settings - Fork 28
/
font.c
251 lines (197 loc) · 6.13 KB
/
font.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "font.h"
#include "util.h"
#include "sys_time.h"
#include "stats.h"
#include "application.h"
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
enum
{
font_magic_1 = 0x4afb4afb,
font_magic_2 = 0x4afb4afc,
font_version_0 = 0,
font_version_1 = 1,
font_codepoint_last_entry = 0xffffffff,
fonts = 16,
};
typedef struct
{
uint32_t magic2;
uint32_t version;
uint32_t width;
uint32_t height;
uint32_t font_bitmap[]; // start with a unicode utf8 codepoint, with bit 31 set to one, then one entry (4 bytes) per line.
} font_t;
typedef struct
{
uint32_t magic1;
uint32_t version;
uint32_t font[16]; // this is not a real pointer but an offset from the start of the font area
} font_root_t;
assert_size(font_root_t, 72);
_Static_assert(offsetof(font_root_t, magic1) == 0, "offsetof(font_root_t, magic1) != 0)");
_Static_assert(offsetof(font_root_t, version) == 4, "offsetof(font_root_t, version) != 4)");
_Static_assert(offsetof(font_root_t, font[0]) == 8, "offsetof(font_root_t, font[0]) != 8)");
_Static_assert(offsetof(font_root_t, font[1]) == 12, "offsetof(font_root_t, font[1]) != 12)");
_Static_assert(offsetof(font_root_t, font[2]) == 16, "offsetof(font_root_t, font[2]) != 16)");
_Static_assert(offsetof(font_root_t, font[3]) == 20, "offsetof(font_root_t, font[3]) != 20)");
_Static_assert(offsetof(font_root_t, font[4]) == 24, "offsetof(font_root_t, font[4]) != 24)");
_Static_assert(offsetof(font_root_t, font[5]) == 28, "offsetof(font_root_t, font[5]) != 28)");
_Static_assert(offsetof(font_root_t, font[6]) == 32, "offsetof(font_root_t, font[6]) != 32)");
static bool init_done = false;
static const font_t *font[fonts];
unsigned int font_logging;
unsigned int font_other;
unsigned int font_selected;
void font_init(void)
{
const font_root_t *font_root = flash_cache_pointer(FONT_FLASH_OFFSET_0);
if((font_root->magic1 != font_magic_1) ||
(font_root->version != font_version_1))
{
log("font root invalid\n");
return;
}
for(unsigned int ix = 0; ix < fonts; ix++)
{
font[ix] = flash_cache_pointer(FONT_FLASH_OFFSET_0 + font_root->font[ix]);
if((font[ix]->magic2 != font_magic_2) ||
(font[ix]->version != font_version_1))
font[ix] = (font_t *)0;
}
if(!config_get_uint("font.id.logging", &font_logging, -1, -1))
font_logging = 0;
if(!config_get_uint("font.id.other", &font_other, -1, -1))
font_other = 0;
init_done = true;
}
bool font_select(bool logging)
{
unsigned int font_new;
font_new = logging ? font_logging : font_other;
if(font_new > fonts)
return(false);
if(!font[font_new])
return(false);
font_selected = font_new;
return(true);
}
bool font_get_info(font_info_t *info)
{
if((font_selected >= fonts) || (!font[font_selected]))
return(false);
info->width = font[font_selected]->width;
info->height = font[font_selected]->height;
return(true);
}
bool font_render(unsigned int code, font_cell_t cell)
{
unsigned int sync_brake;
unsigned int font_code;
unsigned int width, height;
unsigned int x, y;
const uint32_t *current;
uint64_t start, spent;
start = time_get_us();
if((font_selected >= fonts) || (!font[font_selected]))
return(false);
width = font[font_selected]->width;
height = font[font_selected]->height;
if((width > font_cell_width) || (height > font_cell_height))
{
log("font: invalid width/height\n");
return(false);
}
current = &font[font_selected]->font_bitmap[0];
for(;;)
{
for(sync_brake = 0; sync_brake < 64; sync_brake++, current++)
if(*current & (1U << 31))
break;
else
log("skip %04x %08x\n", (unsigned int)((current - (const uint32_t *)flash_cache_pointer(FONT_FLASH_OFFSET_0)) * 4), *current);
if(sync_brake >= 64)
{
log("font_render: sync brake\n");
return(false);
}
font_code = *current & ~(1U << 31);
if(font_code == font_codepoint_last_entry)
{
log("font_render: code not found (1)\n");
return(true);
}
if(font_code > code)
{
log("font_render: code not found (2) %u %u\n", font_code, code);
return(true);
}
if(font_code == code)
break;
current += height + 1;
}
current++;
for(y = 0; y < height; y++)
for(x = 0; x < width; x++)
cell[y][x] = font_code == code ? (current[y] & (1 << x) ? ~0 : 0) : 0;
spent = time_get_us() - start;
if(spent > stat_font_render_time)
stat_font_render_time = spent;
return(true);
}
roflash const char help_description_display_font_select[] = "select display font <0-2>\n";
app_action_t application_function_display_font_select(app_params_t *parameters)
{
unsigned int font_logging_local, font_other_local, ix;
if((parse_uint(1, parameters->src, &font_logging_local, 0, ' ') == parse_ok) && parse_uint(2, parameters->src, &font_other_local, 0, ' ') == parse_ok)
{
if((font_logging_local >= fonts) || (font_other_local >= fonts))
{
string_format(parameters->dst, "> font must be 0 - %u\n", fonts - 1U);
return(app_action_error);
}
if(!config_open_write())
{
string_append(parameters->dst, "> cannot set config (open)\n");
return(app_action_error);
}
if((font_logging_local == 0) && (font_other_local == 0))
config_delete("font.id", true, -1, -1);
else
if(!config_set_int("font.id.logging", font_logging_local, -1, -1) ||
!config_set_int("font.id.other", font_other_local, -1, -1))
{
config_abort_write();
string_append(parameters->dst, "> cannot set config\n");
return(app_action_error);
}
if(!config_close_write())
{
string_append(parameters->dst, "> cannot set config (close)\n");
return(app_action_error);
}
font_logging = font_logging_local;
font_other = font_other_local;
}
if(!config_get_uint("font.id.logging", &font_logging_local, -1, -1))
font_logging_local = 0;
if(!config_get_uint("font.id.other", &font_other_local, -1, -1))
font_other_local = 0;
for(ix = 0; ix < fonts; ix++)
{
string_format(parameters->dst, "> font %2u: ", ix);
if(!font[ix])
string_append(parameters->dst, "unavailable");
else
{
string_format(parameters->dst, "%2u x %2u %-7s %-5s",
font[ix]->width, font[ix]->height,
ix == font_logging_local ? "logging" : "",
ix == font_other_local ? "other" : "");
}
string_format(parameters->dst, "\n");
}
return(app_action_normal);
}