This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFont.cpp
364 lines (333 loc) · 6.67 KB
/
Font.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
355
356
357
358
359
360
361
362
363
364
#define DEBUGME
/**
* Some of this code lifted from Arduino serial Print class and modified.
* See: https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Print.cpp
*/
#include "Evade2.h"
#include "charset.h"
static const PROGMEM BYTE *const charset[] = {
NULL, // space
font_emark,
#ifdef FULL_CHARSET
font_dquote,
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_pound, // #
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_dollar, // $
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_percent, // %
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_amp, // &
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_squote, // '
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_lparen, // (
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_rparen, // )
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_asterisk, // *
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_plus,
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_comma,
#else
NULL,
#endif
#ifdef FULL_CHARSET
font_minus,
#else
NULL,
#endif
font_period,
font_fslash,
font_0,
font_1,
font_2,
font_3,
font_4,
font_5,
font_6,
font_7,
font_8,
font_9,
font_colon,
#ifdef full_charset
font_semicolon,
#else
NULL,
#endif
#ifdef full_charset
font_lt, // <
#else
NULL,
#endif
#ifdef full_charset
font_eq, // =
#else
NULL,
#endif
#ifdef full_charset
font_gt, // >
#else
NULL,
#endif
#ifdef full_charset
font_qmark,
#else
NULL,
#endif
#ifdef full_charset
font_at, // @
#else
NULL,
#endif
font_a,
font_b,
font_c,
font_d,
font_e,
font_f,
font_g,
font_h,
font_i,
font_j,
font_k,
font_l,
font_m,
font_n,
font_o,
font_p,
font_q,
font_r,
font_s,
font_t,
font_u,
font_v,
font_w,
font_x,
font_y,
font_z,
#ifdef full_charset
font_lt, // [
#else
NULL,
#endif
#ifdef full_charset
font_bslash, // '\'
#else
NULL,
#endif
#ifdef full_charset
font_gt, // ]
#else
NULL,
#endif
#ifdef full_charset
font_caret, // ^
#else
NULL,
#endif
#ifdef full_charset
font_uscore, // _
#else
NULL,
#endif
NULL, // ``
};
FXP_SSCALE Font::scale = 0x100;
#define SCALE(val) (((val)*scale)>>8)
#ifdef ENABLE_ROTATING_TEXT
BYTE Font::print_string_rotatedx(BYTE x, BYTE y, FXP_ANGLE theta, const __FlashStringHelper *ifsh) {
//theta = float(theta) * 3.1415926 / 180;
float f_theta = RADIANS(theta / (UINT16_MAX/360));
float cost = cos(f_theta),
sint = sin(f_theta);
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
const BYTE size = 9;
BYTE xo = x;
while (char c = pgm_read_byte(p++)) {
PGM_P glyph = (PGM_P)pgm_read_word(&charset[toupper(c) - 32]);
if (glyph) {
BYTE lines = pgm_read_byte(glyph++);
for (BYTE i = 0; i < lines; i++) {
BYTE x0 = SCALE((BYTE)pgm_read_byte(glyph++)) + x,
y0 = SCALE((BYTE)pgm_read_byte(glyph++)) + y,
x1 = SCALE((BYTE)pgm_read_byte(glyph++)) + x,
y1 = SCALE((BYTE)pgm_read_byte(glyph++)) + y;
Graphics::drawLine(
x0,
((y0 - y) * sint + cost + y),
x1,
((y1 - y) * sint + cost + y));
}
x += SCALE(size);
}
else {
x += SCALE(6);
}
}
return x - xo;
}
#endif
BYTE Font::write(BYTE x, BYTE y, char c) {
PGM_P glyph;
const BYTE width = 9;
float fscale = float(scale >> 8) + float(scale & 0xff) / 256.0;
glyph = (PGM_P)pgm_read_word(&charset[toupper(c) - 32]);
if (glyph) {
BYTE lines = pgm_read_byte(glyph++);
for (BYTE i = 0; i < lines; i++) {
BYTE x0 = SCALE((BYTE)pgm_read_byte(glyph++)),
y0 = SCALE((BYTE)pgm_read_byte(glyph++)),
x1 = SCALE((BYTE)pgm_read_byte(glyph++)),
y1 = SCALE((BYTE)pgm_read_byte(glyph++));
Graphics::drawLine(x + x0, y + y0, x + x1, y + y1);
}
}
return SCALE(width);
}
BYTE Font::print_string(BYTE x, BYTE y, char *s) {
BYTE xx = x;
while (char c = *s++) {
BYTE width = Font::write(x, y, c);
x += width;
}
return x - xx; // width of string printed
}
BYTE Font::print_long(BYTE x, BYTE y, LONG n, BYTE base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
*str = '\0';
// prevent crash if called with base == 1
if (base < 2)
base = 10;
do {
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while (n);
return print_string(x, y, str);
}
#ifdef PRINTF_FLOAT
BYTE Font::print_float(BYTE x, BYTE y, double number, BYTE digits) {
BYTE xx = x;
if (isnan(number)) {
x += write(x, y, 'n');
x += write(x, y, 'a');
x += write(x, y, 'n');
return x;
}
if (isinf(number)) {
x += write(x, y, 'i');
x += write(x, y, 'n');
x += write(x, y, 'f');
return x;
}
if (number > 4294967040.0 || number < -4294967040.0) {
x += write(x, y, 'o');
x += write(x, y, 'v');
x += write(x, y, 'f');
return x;
}
// Handle negative numbers
if (number < 0.0) {
x += write(x, y, '-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
x += print_long(x, y, int_part, 10);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
x += write(x, y, '.');
}
// Extract digits from the remainder one at a time
while (digits-- > 0) {
remainder *= 10.0;
unsigned int toPrint = (unsigned int)(remainder);
x += print_long(x, y, toPrint, 10);
remainder -= toPrint;
}
return x - xx;
}
#endif
BYTE Font::_printf(BYTE x, BYTE y, const __FlashStringHelper *ifsh, ...) {
va_list ap;
BYTE xx = x;
char c;
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
va_start(ap, ifsh);
while (c = pgm_read_byte(p++)) {
if (c == '%') {
c = pgm_read_byte(p++);
switch (c) {
case '\0':
va_end(ap);
return x - xx;
#ifdef PRINTF_FLOAT
case '%':
x += Font::write(x, y, '%');
break;
case 'f':
x += print_float(x, y, va_arg(ap, double));
break;
#endif
case 'd':
x += print_long(x, y, (unsigned long)va_arg(ap, int));
break;
#ifdef PRINTF_FLOAT
case 'x':
x += print_long(x, y, (unsigned long)va_arg(ap, int) & 0xffff, 16);
break;
case 'l':
x += print_long(x, y, va_arg(ap, long));
break;
#endif
default:
x += Font::write(x, y, c);
break;
}
}
else {
x += Font::write(x, y, c);
}
}
va_end(ap);
return xx - x;
}