-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgText.cpp
More file actions
1778 lines (1542 loc) · 43.2 KB
/
gText.cpp
File metadata and controls
1778 lines (1542 loc) · 43.2 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
gText.cpp - Support for Text output on a graphical device
Copyright (c) 2009,2010 Bill Perry and Michael Margolis
vi:ts=4
This file is part of the Arduino GLCD library.
GLCD is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
GLCD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GLCD. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/pgmspace.h>
#include "include/gText.h"
#ifndef GLCD_NO_PRINTF
extern "C"
{
#include <stdio.h>
}
#endif
/*
* Experimental defines
*/
//#define GLCD_OLD_FONTDRAW // uncomment this define to get old font rendering (not recommended)
//extern glcd_Device GLCD; // this is the global GLCD instance, here upcast to the base glcd_Device class
// This constructor creates a text area using the entire display
// The device pointer is initialized using the global GLCD instance
// New constuctors can be added to take an exlicit glcd instance pointer
// if multiple glcd instances need to be supported
gText::gText()
{
// device = (glcd_Device*)&GLCD;
this->DefineArea(0,0,DISPLAY_WIDTH -1,DISPLAY_HEIGHT -1, DEFAULT_SCROLLDIR); // this should never fail
}
// This constructor creates a text area with the given coordinates
// full display area is used if any coordinate is invalid
gText::gText(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, textMode mode)
{
//device = (glcd_Device*)&GLCD;
if( ! this->DefineArea(x1,y1,x2,y2,mode))
this->DefineArea(0,0,DISPLAY_WIDTH -1,DISPLAY_HEIGHT -1,mode); // this should never fail
}
gText::gText(predefinedArea selection, textMode mode)
{
//device = (glcd_Device*)&GLCD;
if( ! this->DefineArea(selection,mode))
this->DefineArea(0,0,DISPLAY_WIDTH -1,DISPLAY_HEIGHT -1,mode); // this should never fail
}
gText::gText(uint8_t x1, uint8_t y1, uint8_t columns, uint8_t rows, Font_t font, textMode mode)
{
//device = (glcd_Device*)&GLCD;
if( ! this->DefineArea(x1,y1,columns,rows,font, mode))
{
this->DefineArea(0,0,DISPLAY_WIDTH -1,DISPLAY_HEIGHT -1,mode); // this should never fail
this->SelectFont(font);
}
}
/**
* Clear text area with the current font background color
* and home the cursor to upper left corner of the text area.
*
* @see DefineArea()
*/
void gText::ClearArea(void)
{
/*
* fill the area with font background color
*/
glcd_Device::SetPixels(this->tarea.x1, this->tarea.y1,
this->tarea.x2, this->tarea.y2,
this->FontColor == BLACK ? WHITE : BLACK);
/*
* put cursor at home position of text area to ensure we are always inside area.
*/
this->CursorToXY(0,0);
}
/**
* Define a Text area by columns and rows
*
* @param x X coordinate of upper left corner
* @param y Y coordinate of upper left corner
* @param columns number of text columns
* @param rows number of text rows
* @param font a font definition
* @param mode constants SCROLL_DOWN and SCROLL_UP control scroll direction
*
*
* Defines a text area sized to hold columns characters across and rows characters tall.
* It is properly sized for the specified font.
*
* The area within the newly defined text area is intentionally not cleared.
*
* While intended for fixed width fonts, sizing will work for variable
* width fonts.
*
* When variable width fonts are used, the column is based on assuming a width
* of the widest character.
*
* x,y is an absolute coordinate and is relateive to the 0,0 origin of the
* display.
*
* mode is an optional parameter and defaults to normal/up scrolling
*
* @note
* Upon defining the text area, the cursor position for the text area will be set to x,y
*
* @see ClearArea()
*/
uint8_t
gText::DefineArea(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, textMode mode)
{
uint8_t x2,y2;
this->SelectFont(font);
x2 = x + columns * (FontRead(this->Font+FONT_FIXED_WIDTH)+1) -1;
y2 = y + rows * (FontRead(this->Font+FONT_HEIGHT)+1) -1;
return this->DefineArea(x, y, x2, y2, mode);
}
/**
* Define a text area by absolute coordinates
*
* @param x1 X coordinate of upper left corner
* @param y1 Y coordinate of upper left corner
* @param x2 X coordinate of lower right corner
* @param y2 Y coordinate of lower right corner
* @param mode constants SCROLL_DOWN and SCROLL_UP control scroll direction
*
* Defines a text area based on absolute coordinates.
* The pixel coordinates for the text area are inclusive so x2,y2 is the lower right
* pixel of the text area.
*
* x1,y1 and x2,y2 are an absolute coordinates and are relateive to the 0,0 origin of the
* display.
*
* The area within the newly defined text area is intentionally not cleared.
*
* mode is an optional parameter and defaults to normal/up scrolling
*
* @returns true with the given area selected if all the coordinates are valid,
* otherwise returns returns false with the area set to the full display
*
* @note
* Upon creation of the text area, the cursor position for the text area will be set to x1, y1
*
* @see ClearArea()
*
*/
uint8_t
gText::DefineArea(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, textMode mode)
{
uint8_t ret = false;
if( (x1 >= x2)
|| (y1 >= y2)
|| (x1 >= DISPLAY_WIDTH)
|| (y1 >= DISPLAY_HEIGHT)
|| (x2 >= DISPLAY_WIDTH)
|| (y2 >= DISPLAY_WIDTH)
)
{
// failed sanity check so set defaults and return false
this->tarea.x1 = 0;
this->tarea.y1 = 0;
this->tarea.x2 = DISPLAY_WIDTH -1;
this->tarea.y2 = DISPLAY_HEIGHT -1;
this->tarea.mode = DEFAULT_SCROLLDIR;
}
else
{
this->tarea.x1 = x1;
this->tarea.y1 = y1;
this->tarea.x2 = x2;
this->tarea.y2 = y2;
this->tarea.mode = mode; // not yet sanity checked
ret = true;
}
/*
* set cursor position for the area
*/
this->x = x1;
this->y = y1;
#ifndef GLCD_NODEFER_SCROLL
/*
* Make sure to clear a deferred scroll operation when re defining areas.
*/
this->need_scroll = 0;
#endif
return ret;
}
/**
* Define a predefined generic text area
*
* @param selection a value from @ref predefinedArea
* @param mode constants SCROLL_DOWN and SCROLL_UP control scroll direction
*
* Defines a text area using a selection form a set of predefined areas.
*
* The area within the newly defined text area is intentionally not cleared.
*
* mode is an optional parameter and defaults to normal/up scrolling
*
* @return returns @em true if successful.
*
*
* @note
* Upon defining the text area, the cursor position for the text area will be set to
* the upper left coordinate of the given predefined area
*
* @see ClearArea()
* @see predefinedArea
*
*/
uint8_t
gText::DefineArea(predefinedArea selection, textMode mode)
{
uint8_t x1,y1,x2,y2;
TareaToken tok;
tok.token = selection;
x1 = tok.coord.x1;
y1 = tok.coord.y1;
x2 = tok.coord.x2;
y2 = tok.coord.y2;
return this->DefineArea(x1,y1,x2,y2, mode);
}
/*
* Scroll a pixel region up.
* Area scrolled is defined by x1,y1 through x2,y2 inclusive.
* x1,y1 is upper left corder, x2,y2 is lower right corner.
*
* color is the color to be used for the created space along the
* bottom.
*
* pixels is the *exact* pixels to scroll. 1 is 1 and 9 is 9 it is
* not 1 less or 1 more than what you want. It is *exact*.
*/
void gText::ScrollUp(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,
uint8_t pixels, uint8_t color)
{
uint8_t dy;
uint8_t dbyte;
uint8_t sy;
uint8_t sbyte;
uint8_t col;
/*
* Scrolling up more than area height?
*/
if(y1 + pixels > y2)
{
/*
* fill the region with "whitespace" because
* it is being totally scrolled out.
*/
glcd_Device::SetPixels(x1, y1, x2, y2, color);
return;
}
for(col = x1; col <= x2; col++)
{
dy = y1;
glcd_Device::GotoXY(col, dy & ~7);
dbyte = glcd_Device::ReadData();
/*
* preserve bits outside/above scroll region
*/
dbyte &= (_BV((dy & 7)) - 1);
sy = dy + pixels;
glcd_Device::GotoXY(col, sy & ~7);
sbyte = glcd_Device::ReadData();
while(sy <= y2)
{
if(sbyte & _BV(sy & 7))
{
dbyte |= _BV(dy & 7);
}
sy++;
if((sy & 7) == 0)
{
/*
* If we just crossed over, then we should be done.
*/
if(sy < DISPLAY_HEIGHT)
{
glcd_Device::GotoXY(col, sy & ~7);
sbyte = glcd_Device::ReadData();
}
}
if((dy & 7) == 7)
{
glcd_Device::GotoXY(col, dy & ~7); // Should be able to remove this
glcd_Device::WriteData(dbyte);
dbyte = 0;
}
dy++;
}
/*
* Handle the new area at the bottom of the region
*/
for(uint8_t p = pixels; p; p--)
{
if(color == BLACK)
{
dbyte |= _BV(dy & 7);
}
else
{
dbyte &= ~_BV(dy & 7);
}
if((dy & 7) == 7)
{
glcd_Device::GotoXY(col, dy & ~7); // should be able to remove this.
glcd_Device::WriteData(dbyte);
dbyte = 0;
}
dy++;
}
/*
* Flush out the final destination byte
*/
if(dy & 7)
{
dy--;
glcd_Device::GotoXY(col, dy & ~7);
sbyte = glcd_Device::ReadData();
/*
* Preserver bits outside/below region
*/
dy++;
sbyte &= ~(_BV((dy & 7)) - 1);
dbyte |= sbyte;
glcd_Device::WriteData(dbyte);
}
}
}
#ifndef GLCD_NO_SCROLLDOWN
void gText::ScrollDown(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2,
uint8_t pixels, uint8_t color)
{
uint8_t dy;
uint8_t dbyte;
uint8_t sy;
uint8_t sbyte;
uint8_t col;
/*
* Scrolling up more than area height?
*/
if(y1 + pixels > y2)
{
/*
* fill the region with "whitespace" because
* it is being totally scrolled out.
*/
glcd_Device::SetPixels(x1, y1, x2, y2, color);
return;
}
/*
* Process region from left to right
*/
for(col = x1; col <= x2; col++)
{
dy = y2;
glcd_Device::GotoXY(col, dy & ~7);
dbyte = glcd_Device::ReadData();
/*
* preserve bits outside/below scroll region
*/
dbyte &= ~(_BV(((dy & 7)+1)) - 1);
sy = dy - pixels;
glcd_Device::GotoXY(col, sy & ~7);
sbyte = glcd_Device::ReadData();
while(sy >= y1)
{
if(sbyte & _BV(sy & 7))
{
dbyte |= _BV(dy & 7);
}
if((dy & 7) == 0)
{
glcd_Device::GotoXY(col, dy & ~7); // Should be able to remove this
glcd_Device::WriteData(dbyte);
dbyte = 0;
}
dy--;
if(!sy)
break; /* if we bottomed out, we are done */
sy--;
if((sy & 7) == 7)
{
glcd_Device::GotoXY(col, sy & ~7);
sbyte = glcd_Device::ReadData();
}
}
/*
* Handle the new area at the top of the column
*/
for(uint8_t p = pixels; p; p--)
{
if(color == BLACK)
{
dbyte |= _BV(dy & 7);
}
else
{
dbyte &= ~_BV(dy & 7);
}
if((dy & 7) == 0)
{
glcd_Device::GotoXY(col, dy & ~7); // should be able to remove this.
glcd_Device::WriteData(dbyte);
dbyte = 0;
}
dy--;
}
dy++; /* point dy back to last destination row */
/*
* Flush out the final destination byte
*/
if(dy & 7)
{
glcd_Device::GotoXY(col, dy & ~7);
sbyte = glcd_Device::ReadData();
/*
* Preserve bits outside/above region
*/
sbyte &= (_BV((dy & 7)) - 1);
dbyte |= sbyte;
glcd_Device::WriteData(dbyte);
}
}
}
#endif //GLCD_NO_SCROLLDOWN
/*
* Handle all special processing characters
*/
void gText::SpecialChar(uint8_t c)
{
if(c == '\n')
{
uint8_t height = FontRead(this->Font+FONT_HEIGHT);
/*
* Erase all pixels remaining to edge of text area.on all wraps
* It looks better when using inverted (WHITE) text, on proportional fonts, and
* doing WHITE scroll fills.
*
*/
if(this->x < this->tarea.x2)
glcd_Device::SetPixels(this->x, this->y, this->tarea.x2, this->y+height, this->FontColor == BLACK ? WHITE : BLACK);
/*
* Check for scroll up vs scroll down (scrollup is normal)
*/
#ifndef GLCD_NO_SCROLLDOWN
if(this->tarea.mode == SCROLL_UP)
#endif
{
/*
* Normal/up scroll
*/
/*
* Note this comparison and the pixel calcuation below takes into
* consideration that fonts
* are atually 1 pixel taller when rendered.
* This extra pixel is along the bottom for a "gap" between the character below.
*/
if(this->y + 2*height >= this->tarea.y2)
{
#ifndef GLCD_NODEFER_SCROLL
if(!this->need_scroll)
{
this->need_scroll = 1;
return;
}
#endif
/*
* forumula for pixels to scroll is:
* (assumes "height" is one less than rendered height)
*
* pixels = height - ((this->tarea.y2 - this->y) - height) +1;
*
* The forumala below is unchanged
* But has been re-written/simplified in hopes of better code
*
*/
uint8_t pixels = 2*height + this->y - this->tarea.y2 +1;
/*
* Scroll everything to make room
* * NOTE: (FIXME, slight "bug")
* When less than the full character height of pixels is scrolled,
* There can be an issue with the newly created empty line.
* This is because only the # of pixels scrolled will be colored.
* What it means is that if the area starts off as white and the text
* color is also white, the newly created empty text line after a scroll
* operation will not be colored BLACK for the full height of the character.
* The only way to fix this would be alter the code use a "move pixels"
* rather than a scroll pixels, and then do a clear to end line immediately
* after the move and wrap.
*
* Currently this only shows up when
* there are are less than 2xheight pixels below the current Y coordinate to
* the bottom of the text area
* and the current background of the pixels below the current text line
* matches the text color
* and a wrap was just completed.
*
* After a full row of text is printed, the issue will resolve itself.
*
*
*/
this->ScrollUp(this->tarea.x1, this->tarea.y1,
this->tarea.x2, this->tarea.y2, pixels, this->FontColor == BLACK ? WHITE : BLACK);
this->x = this->tarea.x1;
this->y = this->tarea.y2 - height;
}
else
{
/*
* Room for simple wrap
*/
this->x = this->tarea.x1;
this->y = this->y+height+1;
}
}
#ifndef GLCD_NO_SCROLLDOWN
else
{
/*
* Reverse/Down scroll
*/
/*
* Check for Wrap vs scroll.
*
* Note this comparison and the pixel calcuation below takes into
* consideration that fonts
* are atually 1 pixel taller when rendered.
*
*/
if(this->y > this->tarea.y1 + height)
{
/*
* There is room so just do a simple wrap
*/
this->x = this->tarea.x1;
this->y = this->y - (height+1);
}
else
{
#ifndef GLCD_NODEFER_SCROLL
if(!this->need_scroll)
{
this->need_scroll = 1;
return;
}
#endif
/*
* Scroll down everything to make room for new line
* (assumes "height" is one less than rendered height)
*/
uint8_t pixels = height+1 - (this->tarea.y1 - this->y);
this->ScrollDown(this->tarea.x1, this->tarea.y1,
this->tarea.x2, this->tarea.y2, pixels, this->FontColor == BLACK ? WHITE : BLACK);
this->x = this->tarea.x1;
this->y = this->tarea.y1;
}
}
#endif
}
}
/**
* output a character
*
* @param c the character to output
*
* If the character will not fit on the current text line
* inside the text area,
* the text position is wrapped to the next line. This might be
* the next lower or the next higher line depending on the
* scroll direction.
*
* If there is not enough room to fit a full line of new text after
* wrapping, the entire text area will be scrolled to make room for a new
* line of text. The scroll direction will be up or down
* depending on the scroll direction for the text area.
*
* @see Puts()
* @see Puts_P()
* @see write()
*/
int gText::PutChar(uint8_t c)
{
if(this->Font == 0)
return 0; // no font selected
/*
* check for special character processing
*/
if(c < 0x20)
{
SpecialChar(c);
return 1;
}
uint8_t width = 0;
uint8_t height = FontRead(this->Font+FONT_HEIGHT);
uint8_t bytes = (height+7)/8; /* calculates height in rounded up bytes */
uint8_t firstChar = FontRead(this->Font+FONT_FIRST_CHAR);
uint8_t charCount = FontRead(this->Font+FONT_CHAR_COUNT);
uint16_t index = 0;
uint8_t thielefont;
if(c < firstChar || c >= (firstChar+charCount)) {
return 0; // invalid char
}
c-= firstChar;
if( isFixedWidthFont(this->Font) {
thielefont = 0;
width = FontRead(this->Font+FONT_FIXED_WIDTH);
index = c*bytes*width+FONT_WIDTH_TABLE;
}
else{
// variable width font, read width data, to get the index
thielefont = 1;
/*
* Because there is no table for the offset of where the data
* for each character glyph starts, run the table and add up all the
* widths of all the characters prior to the character we
* need to locate.
*/
for(uint8_t i=0; i<c; i++) {
index += FontRead(this->Font+FONT_WIDTH_TABLE+i);
}
/*
* Calculate the offset of where the font data
* for our character starts.
* The index value from above has to be adjusted because
* there is potentialy more than 1 byte per column in the glyph,
* when the characgter is taller than 8 bits.
* To account for this, index has to be multiplied
* by the height in bytes because there is one byte of font
* data for each vertical 8 pixels.
* The index is then adjusted to skip over the font width data
* and the font header information.
*/
index = index*bytes+charCount+FONT_WIDTH_TABLE;
/*
* Finally, fetch the width of our character
*/
width = FontRead(this->Font+FONT_WIDTH_TABLE+c);
}
#ifndef GLCD_NODEFER_SCROLL
/*
* check for a defered scroll
* If there is a deferred scroll,
* Fake a newline to complete it.
*/
if(this->need_scroll)
{
this->PutChar('\n'); // fake a newline to cause wrap/scroll
this->need_scroll = 0;
}
#endif
/*
* If the character won't fit in the text area,
* fake a newline to get the text area to wrap and
* scroll if necessary.
* NOTE/WARNING: the below calculation assumes a 1 pixel pad.
* This will need to be changed if/when configurable pixel padding is supported.
*/
if(this->x + width > this->tarea.x2)
{
this->PutChar('\n'); // fake a newline to cause wrap/scroll
#ifndef GLCD_NODEFER_SCROLL
/*
* We can't defer a scroll at this point since we need to ouput
* a character right now.
*/
if(this->need_scroll)
{
this->PutChar('\n'); // fake a newline to cause wrap/scroll
this->need_scroll = 0;
}
#endif
}
// last but not least, draw the character
#ifdef GLCD_OLD_FONTDRAW
/*================== OLD FONT DRAWING ============================*/
glcd_Device::GotoXY(this->x, this->y);
/*
* Draw each column of the glyph (character) horizontally
* 8 bits (1 page) at a time.
* i.e. if a font is taller than 8 bits, draw upper 8 bits first,
* Then drop down and draw next 8 bits and so on, until done.
* This code depends on WriteData() doing writes that span LCD
* memory pages, which has issues because the font data isn't
* always a multiple of 8 bits.
*/
for(uint8_t i=0; i<bytes; i++) /* each vertical byte */
{
uint16_t page = i*width; // page must be 16 bit to prevent overflow
for(uint8_t j=0; j<width; j++) /* each column */
{
uint8_t data = FontRead(this->Font+index+page+j);
/*
* This funkyness is because when the character glyph is not a
* multiple of 8 in height, the residual bits in the font data
* were aligned to the incorrect end of the byte with respect
* to the GLCD. I believe that this was an initial oversight (bug)
* in Thieles font creator program. It is easily fixed
* in the font program but then creates a potential backward
* compatiblity problem.
* --- bperrybap
*/
if(height > 8 && height < (i+1)*8) /* is it last byte of multibyte tall font? */
{
data >>= (i+1)*8-height;
}
if(this->FontColor == BLACK) {
glcd_Device::WriteData(data);
} else {
glcd_Device::WriteData(~data);
}
}
// 1px gap between chars
if(this->FontColor == BLACK) {
glcd_Device::WriteData(0x00);
} else {
glcd_Device::WriteData(0xFF);
}
glcd_Device::GotoXY(this->x, glcd_Device::Coord.y+8);
}
this->x = this->x+width+1;
/*================== END of OLD FONT DRAWING ============================*/
#else
/*================== NEW FONT DRAWING ===================================*/
/*
* Paint font data bits and write them to LCD memory 1 LCD page at a time.
* This is very different from simply reading 1 byte of font data
* and writing all 8 bits to LCD memory and expecting the write data routine
* to fragement the 8 bits across LCD 2 memory pages when necessary.
* That method (really doesn't work) and reads and writes the same LCD page
* more than once as well as not do sequential writes to memory.
*
* This method of rendering while much more complicated, somewhat scrambles the font
* data reads to ensure that all writes to LCD pages are always sequential and a given LCD
* memory page is never read or written more than once.
* And reads of LCD pages are only done at the top or bottom of the font data rendering
* when necessary.
* i.e it ensures the absolute minimum number of LCD page accesses
* as well as does the sequential writes as much as possible.
*
*/
uint8_t pixels = height +1; /* 1 for gap below character*/
uint8_t p;
uint8_t dy;
uint8_t tfp;
uint8_t dp;
uint8_t dbyte;
uint8_t fdata;
for(p = 0; p < pixels;)
{
dy = this->y + p;
/*
* Align to proper Column and page in LCD memory
*/
glcd_Device::GotoXY(this->x, (dy & ~7));
uint16_t page = p/8 * width; // page must be 16 bit to prevent overflow
for(uint8_t j=0; j<width; j++) /* each column of font data */
{
/*
* Fetch proper byte of font data.
* Note:
* This code "cheats" to add the horizontal space/pixel row
* below the font.
* It essentially creates a font pixel of 0 when the pixels are
* out of the defined pixel map.
*
* fake a fondata read read when we are on the very last
* bottom "pixel". This lets the loop logic continue to run
* with the extra fake pixel. If the loop is not the
* the last pixel the pixel will come from the actual
* font data, but that is ok as it is 0 padded.
*
*/
if(p >= height)
{
/*
* fake a font data read for padding below character.
*/
fdata = 0;
}
else
{
fdata = FontRead(this->Font+index+page+j);
/*
* Have to shift font data because Thiele shifted residual
* font bits the wrong direction for LCD memory.
*
* The real solution to this is to fix the variable width font format to
* not shift the residual bits the wrong direction!!!!
*/
if(thielefont && (height - (p&~7)) < 8)
{
fdata >>= 8 - (height & 7);
}
}
if(this->FontColor == WHITE)
fdata ^= 0xff; /* inverted data for "white" font color */
/*
* Check to see if a quick full byte write of font
* data can be done.
*/
if(!(dy & 7) && !(p & 7) && ((pixels -p) >= 8))
{
/*
* destination pixel is on a page boundary
* Font data is on byte boundary
* And there are 8 or more pixels left
* to paint so a full byte write can be done.
*/
glcd_Device::WriteData(fdata);
continue;
}
else
{
/*
* No, so must fetch byte from LCD memory.
*/
dbyte = glcd_Device::ReadData();
}
/*
* At this point there is either not a full page of data
* left to be painted or the font data spans multiple font
* data bytes. (or both) So, the font data bits will be painted
* into a byte and then written to the LCD memory.page.
*/
tfp = p; /* font pixel bit position */
dp = dy & 7; /* data byte pixel bit position */
/*
* paint bits until we hit bottom of page/byte
* or run out of pixels to paint.
*/
while((dp <= 7) && (tfp) < pixels)
{
if(fdata & _BV(tfp & 7))
{
dbyte |= _BV(dp);
}
else
{
dbyte &= ~_BV(dp);
}
/*
* Check for crossing font data bytes
*/
if((tfp & 7)== 7)
{
fdata = FontRead(this->Font+index+page+j+width);
/*
* Have to shift font data because Thiele shifted residual
* font bits the wrong direction for LCD memory.
*
*/
if((thielefont) && ((height - tfp) < 8))
{
fdata >>= (8 - (height & 7));
}