-
Notifications
You must be signed in to change notification settings - Fork 9
/
FontOperations.ino
333 lines (232 loc) · 7.21 KB
/
FontOperations.ino
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
/*
methods for drawing GLCD fonts are defined here
Fonts are created using GLCDFontCreator2
*/
struct {
uint16_t _txtClr;
int16_t _cursorX, _cursorY;
const uint8_t *_font;
uint8_t _fontW, _fontH;
uint8_t _firstCharInFont, _numCharsInFont, _nDataLines;
uint8_t _charSpacing, _lineSpacing;
}
fontData;
// ------------------------
void setTextColor(uint16_t txtClr) {
// ------------------------
fontData._txtClr = txtClr;
}
// ------------------------
void setCursor(int16_t cursorX, int16_t cursorY) {
// ------------------------
fontData._cursorX = cursorX;
fontData._cursorY = cursorY;
}
// ------------------------
void setCursorX(int16_t cursorX) {
// ------------------------
fontData._cursorX = cursorX;
}
// ------------------------
void setCursorY(int16_t cursorY) {
// ------------------------
fontData._cursorY = cursorY;
}
// ------------------------
void setFont(const uint8_t *font) {
// ------------------------
fontData._font = font;
// read width, height into variables
fontData._fontW = pgm_read_byte_near(fontData._font + 2);
fontData._fontH = pgm_read_byte_near(fontData._font + 3);
fontData._firstCharInFont = pgm_read_byte_near(fontData._font + 4);
fontData._numCharsInFont = pgm_read_byte_near(fontData._font + 5);
fontData._nDataLines = fontData._fontH / 8;
if((fontData._nDataLines * 8) < fontData._fontH)
fontData._nDataLines++;
fontData._charSpacing = fontData._nDataLines;
fontData._lineSpacing = fontData._nDataLines - 1;
}
// ------------------------
uint8_t drawChar(char ch) {
// ------------------------
// is character available
uint8_t charIndex = ch - fontData._firstCharInFont;
if( (charIndex >= 0) && (charIndex < fontData._numCharsInFont) ) {
// get this character size
uint8_t charSize = pgm_read_byte_near(fontData._font + charIndex + 6);
if(charSize == 0)
return 0;
uint8_t fontDataIndex = 6 + fontData._numCharsInFont;
int charDataIndex = fontDataIndex;
for(uint8_t i = 0; i < charIndex; i++)
charDataIndex += pgm_read_byte_near(fontData._font + 6 + i) * fontData._nDataLines;
// plot font bytes
for(uint8_t fLine = 0; fLine < fontData._nDataLines; fLine++) {
uint8_t pixelsRemaining = fontData._fontH - (fLine * 8);
// number of pixels to draw in this line
uint8_t nPixels2Draw = (pixelsRemaining < 8) ? pixelsRemaining : 8;
for(uint8_t x = 0; x < charSize; x++) {
uint8_t linPixels = pgm_read_byte_near(fontData._font + x + charDataIndex);
for(uint8_t j = 8 - nPixels2Draw, idx = 0; j < 8; j++, idx++)
if(bitRead(linPixels, j) == 1)
display.drawPixel(fontData._cursorX + x, fontData._cursorY + idx + (fLine * 8), fontData._txtClr);
}
charDataIndex += charSize;
}
// return drawn character size
return charSize;
}
else
return 0;
}
// ------------------------
uint8_t getWidth(char ch) {
// ------------------------
uint8_t charIndex = ch - fontData._firstCharInFont;
if( (charIndex >= 0) && (charIndex < fontData._numCharsInFont) ) {
// get this character size
return pgm_read_byte_near(fontData._font + charIndex + 6);
}
else
return 0;
}
// ------------------------
uint8_t getHeight(char ch) {
// ------------------------
// is character available
uint8_t charIndex = ch - fontData._firstCharInFont;
if( (charIndex >= 0) && (charIndex < fontData._numCharsInFont) ) {
// get this character size
uint8_t charSize = pgm_read_byte_near(fontData._font + charIndex + 6);
if(charSize == 0)
return 0;
uint8_t fontDataIndex = 6 + fontData._numCharsInFont;
int charDataIndex = fontDataIndex;
for(uint8_t i = 0; i < charIndex; i++)
charDataIndex += pgm_read_byte_near(fontData._font + 6 + i) * fontData._nDataLines;
uint8_t orPilexArr[fontData._nDataLines - 1];
for(int ln = 0; ln < fontData._nDataLines; ln++) {
uint8_t allPx = 0x0;
for(int x = 0; x < charSize; x++) {
uint8_t dataByte = pgm_read_byte_near(fontData._font + x + charDataIndex + (ln * charSize));
allPx = allPx | dataByte;
}
if(ln == (fontData._nDataLines - 1) )
// does last ored pixel require lsb shift
if((fontData._fontH % 8) != 0)
allPx = allPx >> (8 - (fontData._fontH % 8));
orPilexArr[ln] = allPx;
}
uint8_t lsbPos = findLSB(orPilexArr);
uint8_t msbPos = findMSB(orPilexArr);
return (msbPos - lsbPos + 1);
}
else
return 0;
}
// ------------------------
uint8_t getOffsetTop(char ch) {
// ------------------------
// is character available
uint8_t charIndex = ch - fontData._firstCharInFont;
if( (charIndex >= 0) && (charIndex < fontData._numCharsInFont) ) {
// get this character size
uint8_t charSize = pgm_read_byte_near(fontData._font + charIndex + 6);
if(charSize == 0)
return 0;
uint8_t fontDataIndex = 6 + fontData._numCharsInFont;
int charDataIndex = fontDataIndex;
for(uint8_t i = 0; i < charIndex; i++)
charDataIndex += pgm_read_byte_near(fontData._font + 6 + i) * fontData._nDataLines;
uint8_t orPilexArr[fontData._nDataLines - 1];
for(int ln = 0; ln < fontData._nDataLines; ln++) {
uint8_t allPx = 0x0;
for(int x = 0; x < charSize; x++) {
uint8_t dataByte = pgm_read_byte_near(fontData._font + x + charDataIndex + (ln * charSize));
allPx = allPx | dataByte;
}
if(ln == (fontData._nDataLines - 1) )
// does last ored pixel require lsb shift
if((fontData._fontH % 8) != 0)
allPx = allPx >> (8 - (fontData._fontH % 8));
orPilexArr[ln] = allPx;
}
return findLSB(orPilexArr);
}
else
return 0;
}
// ------------------------
uint8_t findLSB(uint8_t *orPilexArr) {
// ------------------------
uint8_t lsbPos = 0, counter = 0;
// find lsb here
for(int ln = 0; ln < fontData._nDataLines; ln++) {
for(int i = 0; i < 8; i++) {
if(bitRead(orPilexArr[ln], i) == 1)
return counter;
counter++;
}
}
return counter;
}
// ------------------------
uint8_t findMSB(uint8_t *orPilexArr) {
// ------------------------
uint8_t lmsbPos = 0, counter = (fontData._nDataLines * 8) - 1;
for(int ln = fontData._nDataLines - 1; ln > -1; ln--) {
for(int i = 7; i > -1; i--) {
if(bitRead(orPilexArr[ln], i) == 1)
return counter;
counter--;
}
}
return counter;
}
// ------------------------
int getStringWidth(const char *str) {
// ------------------------
int w = 0;
char ch;
for(;;) {
ch = *str;
if(ch == 0)
break;
uint8_t chWidth = getWidth(ch);
// add space for displayable chars only
if(chWidth > 0)
w += chWidth + fontData._charSpacing;
str++;
}
// reduce last inter-characters space
if(w > fontData._charSpacing)
w -= fontData._charSpacing;
return w;
}
// ------------------------
void drawString(const char *str) {
// ------------------------
char ch;
for(;;) {
ch = *str;
if(ch == 0)
break;
uint8_t chWidth = drawChar(ch);
// add space for displayable chars only
if(chWidth > 0)
fontData._cursorX += chWidth + fontData._charSpacing;
str++;
}
}
// ------------------------
void drawString(int16_t cursorX, int16_t cursorY, const char *str) {
// ------------------------
setCursor(cursorX, cursorY);
drawString(str);
}
// ------------------------
uint8_t getFontHeight() {
// ------------------------
return fontData._fontH + fontData._lineSpacing;
}