-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cpp
More file actions
441 lines (389 loc) · 20.5 KB
/
Copy pathText.cpp
File metadata and controls
441 lines (389 loc) · 20.5 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/******************************************************************************************************************************************************
* COPYRIGHT
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* \verbatim
* Copyright (c) Andreas Burnickl All rights reserved.
*
* \endverbatim
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* FILE DESCRIPTION
* -------------------------------------------------------------------------------------------------------------------------------------------------*/
/** \file Text.cpp
* \brief
*
* \details
*
*
******************************************************************************************************************************************************/
#define _TEXT_SOURCE_
/******************************************************************************************************************************************************
* I N C L U D E S
******************************************************************************************************************************************************/
#include "Text.h"
/******************************************************************************************************************************************************
* L O C A L C O N S T A N T M A C R O S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* L O C A L F U N C T I O N M A C R O S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* L O C A L D A T A T Y P E S A N D S T R U C T U R E S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* P U B L I C F U N C T I O N S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
setChar()
******************************************************************************************************************************************************/
StdReturnType Text::setChar(byte Column, byte Row, char Char, FontType Font)
{
byte fontIndex;
if(convertCharToFontIndex(Char, fontIndex) == E_NOT_OK) return E_NOT_OK;
#if(TEXT_SUPPORT_FONT_5X8 == STD_ON)
if(Font == FONT_5X8) {
FontSprite5x8::FontCharType FontChar;
if(Font5x8.getChar(fontIndex, FontChar) == E_NOT_OK) { return E_NOT_OK; }
return setCharFontVertical(Column, Row, FontChar, FONT_SPRITE_5X8_HEIGHT);
}
#endif
#if(TEXT_SUPPORT_FONT_7X9 == STD_ON)
if(Font == FONT_7X9) {
FontCourierNew7x9::FontCharType FontChar;
if(Font7x9.getChar(fontIndex, FontChar) == E_NOT_OK) { return E_NOT_OK; }
return setCharFontHorizontal(Column, Row, FontChar, FONT_COURIER_NEW_7X9_HEIGHT);
}
#endif
#if(TEXT_SUPPORT_FONT_7X10 == STD_ON)
if(Font == FONT_7X10) {
FontCourierNew7x10::FontCharType FontChar;
if(Font7x10.getChar(fontIndex, FontChar) == E_NOT_OK) { return E_NOT_OK; }
return setCharFontHorizontal(Column, Row, FontChar, FONT_COURIER_NEW_7X10_HEIGHT);
}
#endif
#if(TEXT_SUPPORT_FONT_9X10 == STD_ON)
if(Font == FONT_9X10) {
FontLucidaSans9x10::FontCharType FontChar;
if(Font9x10.getChar(fontIndex, FontChar) == E_NOT_OK) { return E_NOT_OK; }
return setCharFontVertical(Column, Row, FontChar, FONT_LUCIDA_SANS_9X10_HEIGHT);
}
#endif
#if(TEXT_SUPPORT_FONT_10X10 == STD_ON)
if(Font == FONT_10X10) {
FontTahoma10x10::FontCharType FontChar;
if(Font10x10.getChar(fontIndex, FontChar) == E_NOT_OK) { return E_NOT_OK; }
return setCharFontHorizontal(Column, Row, FontChar, FONT_TAHOMA_10X10_HEIGHT);
}
#endif
return E_NOT_OK;
} /* setChar */
/******************************************************************************************************************************************************
setCharWithShift()
******************************************************************************************************************************************************/
void Text::setCharWithShift(char Char, FontType Font)
{
State = STATE_CHAR_SHIFT;
Shift.State = SHIFT_STATE_BUSY;
Shift.Font = Font;
Shift.Char = Char;
Shift.CharWidth = getFontCharWidth(Font, Char);
Shift.Counter = Shift.CharWidth;
}
/******************************************************************************************************************************************************
setTextWithShift()
******************************************************************************************************************************************************/
void Text::setTextWithShift(const char* Text, FontType Font)
{
State = STATE_TEXT_SHIFT;
Shift.State = SHIFT_STATE_IDLE;
Shift.Font = Font;
Shift.Text = Text;
}
/******************************************************************************************************************************************************
task()
******************************************************************************************************************************************************/
void Text::task(bool Show)
{
if(State == STATE_TEXT_SHIFT) {
stringShiftTask();
if(Show) { Display::getInstance().show(); }
}
if(State == STATE_CHAR_SHIFT) {
charShiftTask();
if(Show) { Display::getInstance().show(); }
}
} /* task */
/******************************************************************************************************************************************************
stop()
******************************************************************************************************************************************************/
void Text::stop()
{
State = STATE_IDLE;
Shift.State = SHIFT_STATE_IDLE;
} /* stop */
/******************************************************************************************************************************************************
getFontHeight()
******************************************************************************************************************************************************/
byte Text::getFontHeight(FontType Font) const
{
#if(TEXT_SUPPORT_FONT_5X8 == STD_ON)
if(Font == FONT_5X8) return Font5x8.getHeight();
#endif
#if(TEXT_SUPPORT_FONT_7X9 == STD_ON)
if(Font == FONT_7X9) return Font7x9.getHeight();
#endif
#if(TEXT_SUPPORT_FONT_7X10 == STD_ON)
if(Font == FONT_7X10) return Font7x10.getHeight();
#endif
#if(TEXT_SUPPORT_FONT_9X10 == STD_ON)
if(Font == FONT_9X10) return Font9x10.getHeight();
#endif
#if(TEXT_SUPPORT_FONT_10X10 == STD_ON)
if(Font == FONT_10X10) return Font10x10.getHeight();
#endif
return 0;
} /* getFontHeight */
/******************************************************************************************************************************************************
getFontWidth()
******************************************************************************************************************************************************/
byte Text::getFontWidth(FontType Font) const
{
#if(TEXT_SUPPORT_FONT_5X8 == STD_ON)
if(Font == FONT_5X8) return Font5x8.getWidth();
#endif
#if(TEXT_SUPPORT_FONT_7X9 == STD_ON)
if(Font == FONT_7X9) return Font7x9.getWidth();
#endif
#if(TEXT_SUPPORT_FONT_7X10 == STD_ON)
if(Font == FONT_7X10) return Font7x10.getWidth();
#endif
#if(TEXT_SUPPORT_FONT_9X10 == STD_ON)
if(Font == FONT_9X10) return Font9x10.getWidth();
#endif
#if(TEXT_SUPPORT_FONT_10X10 == STD_ON)
if(Font == FONT_10X10) return Font10x10.getWidth();
#endif
return 0;
} /* getFontWidth */
/******************************************************************************************************************************************************
getFontCharWidth()
******************************************************************************************************************************************************/
byte Text::getFontCharWidth(FontType Font, char Char) const
{
byte index = convertCharToFontIndex(Char);
#if(TEXT_SUPPORT_FONT_5X8 == STD_ON)
if(Font == FONT_5X8) return Font5x8.getCharWidth(index);
#endif
#if(TEXT_SUPPORT_FONT_7X9 == STD_ON)
if(Font == FONT_7X9) return Font7x9.getCharWidth(index);
#endif
#if(TEXT_SUPPORT_FONT_7X10 == STD_ON)
if(Font == FONT_7X10) return Font7x10.getCharWidth(index);
#endif
#if(TEXT_SUPPORT_FONT_9X10 == STD_ON)
if(Font == FONT_9X10) return Font9x10.getCharWidth(index);
#endif
#if(TEXT_SUPPORT_FONT_10X10 == STD_ON)
if(Font == FONT_10X10) return Font10x10.getCharWidth(index);
#endif
return 0;
} /* getFontCharWidth */
/******************************************************************************************************************************************************
getFontOrientation()
******************************************************************************************************************************************************/
Orientation Text::getFontOrientation(FontType Font) const
{
#if(TEXT_SUPPORT_FONT_5X8 == STD_ON)
if(Font == FONT_5X8) return Font5x8.getOrientation();
#endif
#if(TEXT_SUPPORT_FONT_7X9 == STD_ON)
if(Font == FONT_7X9) return Font7x9.getOrientation();
#endif
#if(TEXT_SUPPORT_FONT_7X10 == STD_ON)
if(Font == FONT_7X10) return Font7x10.getOrientation();
#endif
#if(TEXT_SUPPORT_FONT_9X10 == STD_ON)
if(Font == FONT_9X10) return Font9x10.getOrientation();
#endif
#if(TEXT_SUPPORT_FONT_10X10 == STD_ON)
if(Font == FONT_10X10) return Font10x10.getOrientation();
#endif
return Orientation::ORIENTATION_NONE;
} /* getFontOrientation */
/******************************************************************************************************************************************************
* P R I V A T E F U N C T I O N S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
stringShiftTask()
******************************************************************************************************************************************************/
void Text::stringShiftTask()
{
if(Shift.State == SHIFT_STATE_IDLE) {
/* If end of string is not reached, load next char */
if(*Shift.Text != STD_NULL_CHARACTER) {
Shift.Char = *Shift.Text;
Shift.State = SHIFT_STATE_BUSY;
Shift.CharWidth = getFontCharWidth(Shift.Font, Shift.Char);
Shift.Counter = Shift.CharWidth;
charShiftTask();
Shift.Text++;
} else { /* otherwise task has finished */
if(State == STATE_TEXT_SHIFT) State = STATE_IDLE;
wcTransformation.shiftLeft();
}
} else {
/* go on shifting char */
charShiftTask();
}
} /* stringShiftTask */
/******************************************************************************************************************************************************
charShiftTask()
******************************************************************************************************************************************************/
void Text::charShiftTask()
{
if(Shift.State == SHIFT_STATE_BUSY) {
wcTransformation.shiftLeft();
if(Shift.Counter == 0u) {
Shift.State = SHIFT_STATE_IDLE;
if(State == STATE_CHAR_SHIFT) State = STATE_IDLE;
} else {
Shift.Counter--;
byte Column = DISPLAY_NUMBER_OF_COLUMNS - Shift.CharWidth + Shift.Counter;
byte Row = getRowCenter(Shift.Font);
/* A character setChar refuses is shifted through as a space, which is the blank
glyph and therefore clears the cells the previous step wrote. Leaving it out
instead would smear those pixels across the display for as long as the text
runs. The width was already taken from the same substitute. */
if(setChar(Column, Row, Shift.Char, Shift.Font) == E_NOT_OK) {
setChar(Column, Row, ' ', Shift.Font);
}
}
}
} /* charShiftTask */
/******************************************************************************************************************************************************
setCharFontHorizontal()
******************************************************************************************************************************************************/
template <typename RowType, byte RowsSize>
StdReturnType Text::setCharFontHorizontal(byte Column, byte Row, const FontCharHorizontal<RowType, RowsSize>& FontChar, byte FontHeight)
{
for(byte fontRow = 0u; fontRow < FontHeight; fontRow++)
{
RowType CharRow;
byte RowAbs = Row + fontRow;
if(FontChar.getRow(fontRow, CharRow) == E_NOT_OK) { return E_NOT_OK; }
if(setCharRow(CharRow, Column, RowAbs, FontChar.getWidth()) == E_NOT_OK) { return E_NOT_OK; }
}
return E_OK;
} /* setCharFontHorizontal */
/******************************************************************************************************************************************************
setCharRow()
******************************************************************************************************************************************************/
template <typename RowType>
StdReturnType Text::setCharRow(RowType CharRow, byte Column, byte RowAbs, byte FontWidth)
{
StdReturnType ReturnValue{E_OK};
for(byte fontColumn = 0u; fontColumn < FontWidth; fontColumn++)
{
byte ColumnAbs = Column + fontColumn;
if(ColumnAbs < DISPLAY_NUMBER_OF_COLUMNS && RowAbs < DISPLAY_NUMBER_OF_ROWS) {
if(Display::getInstance().writePixel(ColumnAbs, RowAbs, bitRead(CharRow, fontColumn)) == E_NOT_OK) { ReturnValue = E_NOT_OK; }
} else {
break;
}
}
return ReturnValue;
} /* setCharRow */
/******************************************************************************************************************************************************
setCharFontVertical()
******************************************************************************************************************************************************/
template <typename ColumnType, byte ColumnsSize>
StdReturnType Text::setCharFontVertical(byte Column, byte Row, const FontCharVertical<ColumnType, ColumnsSize>& FontChar, byte FontHeight)
{
for(byte fontColumn = 0u; fontColumn < FontChar.getWidth(); fontColumn++)
{
ColumnType CharColumn;
byte ColumnAbs = Column + fontColumn;
if(FontChar.getColumn(fontColumn, CharColumn) == E_NOT_OK) { return E_NOT_OK; }
if(setCharColumn(CharColumn, ColumnAbs, Row, FontHeight) == E_NOT_OK) { return E_NOT_OK; }
}
return E_OK;
} /* setCharFontVertical */
/******************************************************************************************************************************************************
setCharColumn()
******************************************************************************************************************************************************/
template <typename ColumnType>
StdReturnType Text::setCharColumn(ColumnType CharColumn, byte ColumnAbs, byte Row, byte FontHeight)
{
StdReturnType returnValue{E_OK};
for(byte fontRow = 0u; fontRow < FontHeight; fontRow++)
{
byte RowAbs = Row + fontRow;
if(ColumnAbs < DISPLAY_NUMBER_OF_COLUMNS && RowAbs < DISPLAY_NUMBER_OF_ROWS) {
if(Display::getInstance().writePixel(ColumnAbs, RowAbs, bitRead(CharColumn, fontRow)) == E_NOT_OK) { returnValue = E_NOT_OK; }
} else {
break;
}
}
return returnValue;
} /* setCharColumn */
/******************************************************************************************************************************************************
convertCharToFontIndex()
******************************************************************************************************************************************************/
/*! \brief converts char to sprite
* \details this function converts the ASCII char to sprite index
*
* \param[in] Char char to convert to sprite
* \param[out] Index appropriate sprite
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
StdReturnType Text::convertCharToFontIndex(char Char, byte& Index) const
{
StdReturnType returnValue = E_NOT_OK;
/* for umlauts we need a special treatment */
if('\xC4' == Char) { Index = 96u; returnValue = E_OK; }
else if('\xD6' == Char) { Index = 97u; returnValue = E_OK; }
else if('\xDC' == Char) { Index = 98u; returnValue = E_OK; }
else if('\xE4' == Char) { Index = 99u; returnValue = E_OK; }
else if('\xF6' == Char) { Index = 100u; returnValue = E_OK; }
else if('\xFC' == Char) { Index = 101u; returnValue = E_OK; }
/* The degree sign sits with them for the same reason: Latin-1 0xB0 is nowhere near the
ASCII run, so it is appended past its end rather than shifting every index after it. */
else if('\xB0' == Char) { Index = 102u; returnValue = E_OK; }
/* For all others only add the offset - within both bounds, and compared as a byte.
Whether char is signed is the platform's decision: where it is not, and AVR is one
of those, every Latin-1 byte above the six umlauts passes a lower bound on its own
and indexes past the end of a font table that has 102 entries. */
else {
const byte Value = static_cast<byte>(Char);
if((Value >= TEXT_ASCII_CHAR_MIN) && (Value <= TEXT_ASCII_CHAR_MAX)) {
Index = static_cast<byte>(Value + TEXT_ASCII_TABLE_OFFSET); returnValue = E_OK;
}
}
return returnValue;
} /* convertCharToFontIndex */
/******************************************************************************************************************************************************
convertCharToFontIndex()
******************************************************************************************************************************************************/
/*! \brief converts char to sprite
* \details this function converts the ASCII char to sprite index
*
* \param[in] Char char to convert to sprite
* \param[out] SpriteIndex appropriate sprite
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
byte Text::convertCharToFontIndex(char Char) const
{
byte Index;
/* The mapping is written once, above. It used to be written here as well, which made
adding a glyph a change in two branches that had to agree - see fonts.md. */
if(convertCharToFontIndex(Char, Index) == E_NOT_OK) {
/* The space, which is the first glyph: a character that cannot be drawn leaves a gap
rather than whatever byte sits past the end of the table. */
return SpaceFontIndex;
}
return Index;
} /* convertCharToFontIndex */
/******************************************************************************************************************************************************
* E N D O F F I L E
******************************************************************************************************************************************************/