-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfontutils.cpp
355 lines (322 loc) · 9.2 KB
/
fontutils.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @file fontutils.cpp
* @brief help functions for font and char
* @author Yunhui Fu (yhfudev@gmail.com)
* @version 1.0
* @date 2016-08-19
* @copyright GPL/BSD
*/
#include "Marlin.h"
#include "fontutils.h"
uint8_t
read_byte_ram(uint8_t * str)
{
return *str;
}
uint8_t
read_byte_rom(uint8_t * str)
{
return pgm_read_byte(str);
}
#if DEBUG
#if defined(ARDUINO)
#include <Arduino.h>
#include <stdarg.h>
void
serial_printf_P (const char *format, ...)
{
static char buff[128];
va_list args;
va_start (args,format);
vsnprintf_P(buff,sizeof(buff),format,args);
va_end (args);
buff[sizeof(buff)/sizeof(buff[0])-1]='\0';
//Serial.print(buff);
SERIAL_ECHO (buff); SERIAL_EOL;
}
#endif
#endif
#define FALSE 0
#define TRUE 1
#ifdef __WIN32__ // or whatever
#define PRIiSZ "ld"
#define PRIuSZ "Iu"
#else
#define PRIiSZ "zd"
#define PRIuSZ "zu"
#endif
#define PRIiOFF "lld"
#define PRIuOFF "llu"
#define DBGMSG(a,b, ...) TRACE( #__VA_ARGS__ )
//typedef int (* pf_bsearch_cb_comp_t)(void *userdata, size_t idx, void * data_pin); /*"data_list[idx] - *data_pin"*/
/**
* @brief 折半方式查找记录
*
* @param userdata : 用户数据指针
* @param num_data : 数据个数
* @param cb_comp : 比较两个数据的回调函数
* @param data_pinpoint : 所要查找的 匹配数据指针
* @param ret_idx : 查找到的位置;如果没有找到,则返回如添加该记录时其所在的位置。
*
* @return 找到则返回0,否则返回<0
*
* 折半方式查找记录, psl->marr 中指向的数据已经以先小后大方式排好序
*/
/**
* @brief Using binary search to find the position by data_pin
*
* @param userdata : User's data
* @param num_data : the item number of the sorted data
* @param cb_comp : the callback function to compare the user's data and pin
* @param data_pin : The reference data to be found
* @param ret_idx : the position of the required data; If failed, then it is the failed position, which is the insert position if possible.
*
* @return 0 on found, <0 on failed(fail position is saved by ret_idx)
*
* Using binary search to find the position by data_pin. The user's data should be sorted.
*/
int
pf_bsearch_r (void *userdata, size_t num_data, pf_bsearch_cb_comp_t cb_comp, void *data_pinpoint, size_t *ret_idx)
{
int retcomp;
uint8_t flg_found;
size_t ileft;
size_t iright;
size_t i;
assert (NULL != ret_idx);
/* 查找合适的位置 */
if (num_data < 1) {
*ret_idx = 0;
DBGMSG (PFDBG_CATLOG_PF, PFDBG_LEVEL_ERROR, "num_data(%"PRIuSZ") < 1", num_data);
return -1;
}
/* 折半查找 */
/* 为了不出现负数,以免缩小索引的所表示的数据范围
* (负数表明减少一位二进制位的使用),
* 内部 ileft 和 iright使用从1开始的下标,
* 即1表示C语言中的0, 2表示语言中的1,以此类推。
* 对外还是使用以 0 为开始的下标
*/
i = 0;
ileft = 1;
iright = num_data;
flg_found = 0;
for (; ileft <= iright;) {
i = (ileft + iright) / 2 - 1;
/* cb_comp should return the *userdata[i] - *data_pinpoint */
retcomp = cb_comp (userdata, i, data_pinpoint);
if (retcomp > 0) {
iright = i;
} else if (retcomp < 0) {
ileft = i + 2;
} else {
/* found ! */
flg_found = 1;
break;
}
}
if (flg_found) {
*ret_idx = i;
return 0;
}
if (iright <= i) {
*ret_idx = i;
} else if (ileft >= i + 2) {
*ret_idx = i + 1;
}
//DBGMSG (PFDBG_CATLOG_PF, PFDBG_LEVEL_DEBUG, "not found! num_data=%"PRIuSZ"; ileft=%"PRIuSZ", iright=%"PRIuSZ", i=%"PRIuSZ"", num_data, ileft, iright, i);
return -1;
}
/**
* @brief 转换 UTF-8 编码的一个字符为本地的 Unicode 字符(wchar_t)
*
* @param pstart : 存储 UTF-8 字符的指针
* @param cb_read_byte : 读取字符的函数;用于8位MCU ROM
* @param pval : 需要返回的 Unicode 字符存放地址指针
*
* @return 成功返回下个 UTF-8 字符的位置
*
* 转换 UTF-8 编码的一个字符为本地的 Unicode 字符(wchar_t)
*/
uint8_t *
get_utf8_value_cb (uint8_t *pstart, read_byte_cb_t cb_read_byte, wchar_t *pval)
{
uint32_t val = 0;
uint8_t valcur = 0;
uint8_t *p = pstart;
assert (NULL != pstart);
assert (NULL != cb_read_byte);
valcur = cb_read_byte (p);
if (0 == (0x80 & valcur)) {
val = valcur;
p ++;
} else if (0xC0 == (0xE0 & valcur)) {
val = valcur & 0x1F;
val <<= 6;
p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
p ++;
} else if (0xE0 == (0xF0 & valcur)) {
val = valcur & 0x0F;
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
p ++;
} else if (0xF0 == (0xF8 & valcur)) {
val = valcur & 0x07;
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
p ++;
} else if (0xF8 == (0xFC & valcur)) {
val = valcur & 0x03;
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
p ++;
} else if (0xFC == (0xFE & valcur)) {
val = valcur & 0x01;
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
val <<= 6; p ++;
valcur = cb_read_byte (p);
val |= (valcur & 0x3F);
p ++;
} else if (0x80 == (0xC0 & valcur)) {
/* error? */
TRACE ("ERR 1");
for (; 0x80 == (0xC0 & valcur); ) { p ++; valcur = cb_read_byte (p); }
} else {
/* error */
TRACE ("ERR 2");
for (; ((0xFE & valcur) > 0xFC); ) { p ++; valcur = cb_read_byte (p); }
}
/*if (val == 0) {
p = NULL;*/
/*
} else if (pstart + maxlen < p) {
p = pstart;
if (pval) *pval = 0;
}
*/
if (pval) *pval = val;
return p;
}
// uint8_t * get_utf8_value_cb (uint8_t *pstart, read_byte_cb_t cb_read_byte, wchar_t *pval);
int
utf8_strlen_cb (const char *pstart, read_byte_cb_t cb_read_byte)
{
wchar_t ch;
uint8_t *pnext;
int cnt = 0;
for (pnext = (uint8_t *)pstart; ; ) {
pnext = get_utf8_value_cb (pnext, cb_read_byte, &ch);
if (pnext == NULL) {
break;
}
if (ch == 0) {
break;
}
cnt ++;
TRACE ("cnt=%d, ch=0x%X", cnt, (int)ch);
}
return cnt;
}
int
utf8_strlen (const char *pstart)
{
return utf8_strlen_cb (pstart, read_byte_ram);
}
int
utf8_strlen_p (const char *pstart)
{
return utf8_strlen_cb (pstart, read_byte_rom);
}
char *
utf8_strncpy_cb ( char * destination, const char * source, size_t num, int len_src, read_byte_cb_t cb_read_byte )
{
uint8_t valcur = 0;
uint8_t *p = (uint8_t *)source;
uint8_t *d = (uint8_t *)destination;
assert (NULL != destination);
assert (NULL != source);
assert (NULL != cb_read_byte);
uint8_t *pend = p + len_src;
int len;
int cur = 0;
for (; p < pend; ) {
valcur = cb_read_byte (p);
len = 0;
if (0 == (0x80 & valcur)) {
len = 1;
} else if (0xC0 == (0xE0 & valcur)) {
len = 2;
} else if (0xE0 == (0xF0 & valcur)) {
len = 3;
} else if (0xF0 == (0xF8 & valcur)) {
len = 4;
} else if (0xF8 == (0xFC & valcur)) {
len = 5;
} else if (0xFC == (0xFE & valcur)) {
len = 6;
} else if (0x80 == (0xC0 & valcur)) {
/* error? */
for (; 0x80 == (0xC0 & valcur) && (p < pend); ) { p ++; valcur = cb_read_byte (p); }
} else {
/* error */
for (; ((0xFE & valcur) > 0xFC) && (p < pend); ) { p ++; valcur = cb_read_byte (p); }
}
if (cur + len < num) {
int i;
for (i = 0; i < len; i ++) {
valcur = cb_read_byte (p);
*d = valcur;
d ++;
p ++;
}
} else {
break;
}
}
*d = 0;
return destination;
}
char *
utf8_strncpy (char * destination, const char * source, size_t num)
{
return utf8_strncpy_cb (destination, source, num, strlen(source), read_byte_ram);
}
char *
utf8_strncpy_p (char * destination, const char * source, size_t num)
{
return utf8_strncpy_cb (destination, source, num, strlen_P(source), read_byte_rom);
}