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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/**
* @file u8g_fontutf8.cpp
* @brief font api for u8g lib
* @author Yunhui Fu (yhfudev@gmail.com)
* @version 1.0
* @date 2015-02-19
* @copyright GPL/BSD
*/
#include "../../inc/MarlinConfigPre.h"
#if HAS_MARLINUI_U8GLIB
#include <string.h>
#include "../fontutils.h"
#include "u8g_fontutf8.h"
typedef void font_t;
/**
* @brief the callback function to draw something
*
* @param userdata : User's data
* @param msg : the u8g's string
* @param fnt_current : the font
*
* @return 0 on success, 1 to force quit, <0 on error
*
* Get the screen pixel width of a ROM UTF-8 string
*/
typedef int (* fontgroup_cb_draw_t)(void *userdata, const font_t *fnt_current, const char *msg);
/* return v1 - v2 */
static int fontinfo_compare(uxg_fontinfo_t * v1, uxg_fontinfo_t * v2) {
if (v1->page < v2->page) return -1;
else if (v1->page > v2->page) return 1;
if (v1->end < v2->begin) return -1;
else if (v1->begin > v2->end) return 1;
return 0;
}
/*"data_list[idx] - *data_pin"*/
static int pf_bsearch_cb_comp_fntifo_pgm (void *userdata, size_t idx, void *data_pin) {
uxg_fontinfo_t *fntinfo = (uxg_fontinfo_t*)userdata;
uxg_fontinfo_t localval;
memcpy_P(&localval, fntinfo + idx, sizeof(localval));
return fontinfo_compare(&localval, (uxg_fontinfo_t*)data_pin);
}
typedef struct _font_group_t {
const uxg_fontinfo_t * m_fntifo;
int m_fntinfo_num;
} font_group_t;
static int fontgroup_init(font_group_t * root, const uxg_fontinfo_t * fntinfo, int number) {
root->m_fntifo = fntinfo;
root->m_fntinfo_num = number;
return 0;
}
static const font_t* fontgroup_find(font_group_t * root, wchar_t val) {
uxg_fontinfo_t vcmp = {(uint16_t)(val / 128), (uint8_t)(val % 128 + 128), (uint8_t)(val % 128 + 128), 0, 0};
size_t idx = 0;
if (val < 256) return nullptr;
if (pf_bsearch_r((void*)root->m_fntifo, root->m_fntinfo_num, pf_bsearch_cb_comp_fntifo_pgm, (void*)&vcmp, &idx) < 0)
return nullptr;
memcpy_P(&vcmp, root->m_fntifo + idx, sizeof(vcmp));
return vcmp.fntdata;
}
static void fontgroup_drawwchar(font_group_t *group, const font_t *fnt_default, wchar_t val, void * userdata, fontgroup_cb_draw_t cb_draw_ram) {
uint8_t buf[2] = {0, 0};
const font_t * fntpqm = (font_t*)fontgroup_find(group, val);
if (!fntpqm) {
// Unknown char, use default font
buf[0] = (uint8_t)(val & 0xFF);
fntpqm = fnt_default;
}
if (fnt_default != fntpqm) {
buf[0] = (uint8_t)(val & 0x7F);
buf[0] |= 0x80; // use upper page to avoid 0x00 error in C. you may want to generate the font data
}
cb_draw_ram (userdata, fntpqm, (char*) buf);
}
/**
* @brief try to process a utf8 string
*
* @param pu8g : U8G pointer
* @param fnt_default : the default font
* @param utf8_msg : the UTF-8 string
* @param cb_read_byte : how to read the utf8_msg, from RAM or ROM (call read_byte_ram or pgm_read_byte)
* @param userdata : User's data
* @param cb_draw_ram : the callback function of userdata to draw a !RAM! string (actually it is to draw a one byte string in RAM)
*
* @return N/A
*
* Get the screen pixel width of a ROM UTF-8 string
*/
static void fontgroup_drawstring(font_group_t *group, const font_t *fnt_default, const char *utf8_msg, read_byte_cb_t cb_read_byte, void * userdata, fontgroup_cb_draw_t cb_draw_ram) {
uint8_t *p = (uint8_t*)utf8_msg;
for (;;) {
wchar_t val = 0;
p = get_utf8_value_cb(p, cb_read_byte, &val);
if (!val) break;
fontgroup_drawwchar(group, fnt_default, val, userdata, cb_draw_ram);
}
}
static bool flag_fontgroup_was_inited = false;
static font_group_t g_fontgroup_root = { nullptr, 0 };
/**
* @brief check if font is loaded
*/
static inline bool uxg_Utf8FontIsInited() { return flag_fontgroup_was_inited; }
int uxg_SetUtf8Fonts (const uxg_fontinfo_t * fntinfo, int number) {
flag_fontgroup_was_inited = 1;
return fontgroup_init(&g_fontgroup_root, fntinfo, number);
}
struct _uxg_drawu8_data_t {
u8g_t *pu8g;
unsigned int x;
unsigned int y;
unsigned int adv;
unsigned int max_width; // the max pixel width of the string allowed
const void * fnt_prev;
};
static int fontgroup_cb_draw_u8g(void *userdata, const font_t *fnt_current, const char *msg) {
struct _uxg_drawu8_data_t * pdata = (_uxg_drawu8_data_t*)userdata;
if (pdata->fnt_prev != fnt_current) {
u8g_SetFont(pdata->pu8g, (const u8g_fntpgm_uint8_t*)fnt_current);
pdata->fnt_prev = fnt_current;
}
if ((pdata->max_width != PIXEL_LEN_NOLIMIT) && (pdata->adv + u8g_GetStrPixelWidth(pdata->pu8g, (char*)msg) > pdata->max_width))
return 1;
pdata->adv += u8g_DrawStr(pdata->pu8g, pdata->x + pdata->adv, pdata->y, (char*) msg);
return 0;
}
/**
* @brief Draw a wchar_t at the specified position
*
* @param pu8g : U8G pointer
* @param x : position x axis
* @param y : position y axis
* @param ch : the wchar_t
* @param max_width : the pixel width of the string allowed
*
* @return number of pixels advanced
*
* Draw a UTF-8 string at the specified position
*/
unsigned int uxg_DrawWchar(u8g_t *pu8g, unsigned int x, unsigned int y, wchar_t ch, pixel_len_t max_width) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
if (!uxg_Utf8FontIsInited()) {
u8g_DrawStrP(pu8g, x, y, (const u8g_pgm_uint8_t *)PSTR("Err: utf8 font not initialized."));
return 0;
}
data.pu8g = pu8g;
data.x = x;
data.y = y;
data.adv = 0;
data.max_width = max_width;
data.fnt_prev = nullptr;
fontgroup_drawwchar(group, fnt_default, ch, (void*)&data, fontgroup_cb_draw_u8g);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv;
}
/**
* @brief Draw a UTF-8 string at the specified position
*
* @param pu8g : U8G pointer
* @param x : position x axis
* @param y : position y axis
* @param utf8_msg : the UTF-8 string
* @param max_width : the pixel width of the string allowed
*
* @return number of pixels advanced
*
* Draw a UTF-8 string at the specified position
*/
unsigned int uxg_DrawUtf8Str(u8g_t *pu8g, unsigned int x, unsigned int y, const char *utf8_msg, pixel_len_t max_width) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
if (!uxg_Utf8FontIsInited()) {
u8g_DrawStrP(pu8g, x, y, (const u8g_pgm_uint8_t *)PSTR("Err: utf8 font not initialized."));
return 0;
}
data.pu8g = pu8g;
data.x = x;
data.y = y;
data.adv = 0;
data.max_width = max_width;
data.fnt_prev = nullptr;
fontgroup_drawstring(group, fnt_default, utf8_msg, read_byte_ram, (void*)&data, fontgroup_cb_draw_u8g);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv;
}
/**
* @brief Draw a ROM UTF-8 string at the specified position
*
* @param pu8g : U8G pointer
* @param x : position x axis
* @param y : position y axis
* @param utf8_msg : the UTF-8 string
* @param max_width : the pixel width of the string allowed
*
* @return number of pixels advanced
*
* Draw a ROM UTF-8 string at the specified position
*/
unsigned int uxg_DrawUtf8StrP(u8g_t *pu8g, unsigned int x, unsigned int y, PGM_P utf8_msg, pixel_len_t max_width) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
if (!uxg_Utf8FontIsInited()) {
u8g_DrawStrP(pu8g, x, y, (const u8g_pgm_uint8_t *)PSTR("Err: utf8 font not initialized."));
return 0;
}
data.pu8g = pu8g;
data.x = x;
data.y = y;
data.adv = 0;
data.max_width = max_width;
data.fnt_prev = nullptr;
fontgroup_drawstring(group, fnt_default, utf8_msg, read_byte_rom, (void*)&data, fontgroup_cb_draw_u8g);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv;
}
static int fontgroup_cb_draw_u8gstrlen(void *userdata, const font_t *fnt_current, const char *msg) {
struct _uxg_drawu8_data_t * pdata = (_uxg_drawu8_data_t*)userdata;
if (pdata->fnt_prev != fnt_current) {
u8g_SetFont(pdata->pu8g, (const u8g_fntpgm_uint8_t*)fnt_current);
pdata->fnt_prev = fnt_current;
}
pdata->adv += u8g_GetStrPixelWidth(pdata->pu8g, (char*)msg);
return 0;
}
/**
* @brief Get the screen pixel width of a UTF-8 string
*
* @param pu8g : U8G pointer
* @param utf8_msg : the UTF-8 string
*
* @return the pixel width
*
* Get the screen pixel width of a UTF-8 string
*/
int uxg_GetUtf8StrPixelWidth(u8g_t *pu8g, const char *utf8_msg) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
if (!uxg_Utf8FontIsInited()) return -1;
memset(&data, 0, sizeof(data));
data.pu8g = pu8g;
data.adv = 0;
fontgroup_drawstring(group, fnt_default, utf8_msg, read_byte_ram, (void*)&data, fontgroup_cb_draw_u8gstrlen);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv;
}
/**
* @brief Get the screen pixel width of a ROM UTF-8 string
*
* @param pu8g : U8G pointer
* @param utf8_msg : the UTF-8 string
*
* @return the pixel width
*
* Get the screen pixel width of a ROM UTF-8 string
*/
int uxg_GetUtf8StrPixelWidthP(u8g_t *pu8g, PGM_P utf8_msg) {
struct _uxg_drawu8_data_t data;
font_group_t *group = &g_fontgroup_root;
const font_t *fnt_default = uxg_GetFont(pu8g);
if (!uxg_Utf8FontIsInited()) return -1;
memset(&data, 0, sizeof(data));
data.pu8g = pu8g;
data.adv = 0;
fontgroup_drawstring(group, fnt_default, utf8_msg, read_byte_rom, (void*)&data, fontgroup_cb_draw_u8gstrlen);
u8g_SetFont(pu8g, (const u8g_fntpgm_uint8_t*)fnt_default);
return data.adv;
}
#endif // HAS_MARLINUI_U8GLIB
|