forked from thegeek82000/openGLCD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgText.cpp
2794 lines (2477 loc) · 71.4 KB
/
gText.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
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 - openGLCD library support - Text output routines
Copyright (c) 2011-2014 Bill Perry
Copyright (c) 2009,2010 Bill Perry and Michael Margolis
vi:ts=4
------------------------------------------------------------------------
This file is part of the openGLCD library project
openGLCD is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License
as published by the Free Software Foundation.
openGLCD 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with openGLCD. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------
*/
#include "include/gText.h"
#include "include/glcd_errno.h"
#ifndef GLCDCFG_NO_PRINTF
#ifdef __AVR__
extern "C"
{
#include <stdio.h>
}
#else
#include <stdio.h>
#include <stdarg.h> // ARM tools don't include this in stdio.h WTF!
#endif
#endif
static FontCallback FontRead; // font callback shared across all instances
#ifdef GLCDCFG_UTF8
/**
* UTF8 character decoding
*
* @param c next character in UTF8 stream to process
*
* UTF8 character decode processing
* For now this is a crude hack that assumes no threading or reentrancy
* and that the UTF character codes are only 0-255.
* To go beyond 255 is a BIG deal since the font header currently only supports up to
* 256 characters. (it is only an 8 bit field)
* Also by only supporting 0-255 codes, when UTF8 processing is enabled, raw code processing
* can also be supported for characters above 0x7f with the exception that codes
* 0xc2 and 0xc3 will be lost.
*
* This code can process each byte seperately, or as a single 16bit UTF8 code.
* This allows using UTF8 wide character codes directly which is what is used
* when using a single UTF8 literal character.
*
* @returns < 0 if processing still on going, otherwise the final decoded character code.
*/
int
gText::UTF8decode(wchar_t c)
{
static uint8_t ucode = 0; // decoded character code
if((uint16_t)c > (uint8_t)0xff) // support sending UTF8 code as one 16 bit value
{
UTF8decode((uint8_t)(c >> 8));
return(UTF8decode((uint8_t)(c & 0xff)));
}
if((c == 0xc2) || (c == 0xc3)) // is it a UTF 80-7ff code point start?
{
ucode = (c & 0x1f) << 6;
return(-1); // still decoding
}
else
{
if(ucode)
c = (c & 0x3f) | ucode; // create final code point
}
ucode = 0; // done decoding
return(c);
}
#endif
/**
* Constructor creates a default gText text area object with no font
*
* Constructor creates a text area using the entire display but does not
* assign any font to it.
* SelectFont() must be used to select a font for the area before any text can
* be output to the display.
*
* @b Examples:
* @code
* // text area using full display
* gText textarea;
* @endcode
*
* @see \ref gText::DefineArea(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, gTextMode mode) "DefineArea()"
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
*/
gText::gText()
{
this->DefineArea(0,0,DISPLAY_WIDTH -1,DISPLAY_HEIGHT -1, DEFAULT_gTEXTMODE); // this should never fail
}
/**
* Constructor creates a gText text area object by 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 a value from @ref gTextMode
*
* Creates a gText area object and then calls
* \ref DefineArea(predefinedArea selection, Font_t font, gTextMode mode) "DefineArea()"
* with the same parameters to initalize it.
*
* @b Examples:
* @code
* // upper left quardrant
* textarea.DefineArea(GLCD.Left, GLCD.Top, GLCD.CenterX, GLCD.CenterY);
*
* // Right half, reverse scroll
* textarea.DefineArea(GLCD.CenterX, GLCD.Top, GLCD.Right, GLCD.Bottom, gTextMode_SCROLLDOWN);
* @endcode
* @note
*
* @see \ref gText::DefineArea(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, gTextMode mode) "DefineArea()"
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
*/
gText::gText(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, gTextMode mode)
{
this->DefineArea(x1,y1,x2,y2,mode);
}
/**
* Constructor creates a gText text area object by predefined area without font
*
* @param selection a value from @ref predefinedArea
* @param mode a value from @ref gTextMode
*
* Creates a gText area object and then calls
* \ref DefineArea(predefinedArea selection, gTextMode mode) "DefineArea()"
* with the same parameters to initalize it.
* Constructor does not assign any font to it.
* SelectFont() must be used to select a font for the area before any text can
* be output to the display.
*
* @b Examples:
* @code
* // right side of display
* textarea.DefineArea(textAreaRIGHT);
*
* // top half of display
* textarea.DefineArea(textAreaTOP);
*
* // upper left quadrant reverse scroll
* textarea.DefineArea(textAreaTOPLEFT, gTextMode_SCROLLDOWN);
* @endcode
*
* @see \ref DefineArea(predefinedArea selection, gTextMode mode) "DefineArea()"
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
*/
gText::gText(predefinedArea selection, gTextMode mode)
{
this->DefineArea(selection,mode);
}
/**
* Constructor creates a gText text area object by predefined area and font
*
* @param selection a value from @ref predefinedArea
* @param font a font definition
* @param mode a value from @ref gTextMode
*
* Creates a gText area object and then calls
* \ref DefineArea(predefinedArea selection, Font_t font, gTextMode mode) "DefineArea()"
* with the same parameters to initalize it.
*
* @b Examples:
* @code
* // right side of display
* textarea.DefineArea(textAreaRIGHT, System5x7);
*
* // top half of display
* textarea.DefineArea(textAreaTOP, System5x7);
*
* // upper left quadrant reverse scroll
* textarea.DefineArea(textAreaTOPLEFT, System5x7, gTextMode_SCROLLDOWN);
* @endcode
*
* @see \ref DefineArea(predefinedArea selection, Font_t font, gTextMode mode) "DefineArea()"
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
*/
gText::gText(predefinedArea selection, Font_t font, gTextMode mode)
{
this->DefineArea(selection,font, mode);
}
/**
* Constructor creates a gText text area object 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 a value from @ref gTextMode
*
*
* Creates a gText area object and then calls
* \ref DefineArea(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, gTextMode mode) "DefineArea()"
* with the same parameters to initalize it.
*
* @b Examples:
* @code
* // top of right side of display, 10 columns by 2 lines
* gText textarea(GLCD.CenterX, GLCD.Top, 10, 2, System5x7);
*
* // top of right side of display, 10 columns by 2 lines, reverse scroll
* gText textarea(GLCD.CenterX, GLCD.Top, 10, 2, System5x7, gTextMode_SCROLLDOWN);
* @endcode
*
* @see \ref DefineArea(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, gTextMode mode) "DefineArea()"
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
*/
gText::gText(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, gTextMode mode)
{
this->DefineArea(x,y,columns,rows,font, mode);
}
/**
* 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)
{
uint8_t color;
/*
* If no font then no font color has been set yet, so assume the background
* is to be cleared using the default font color which is all pixels off.
*/
if((this->Font == 0) || this->FontColor == PIXEL_ON )
color = PIXEL_OFF;
else
color = PIXEL_ON;
/*
* fill the area with font background color
*/
glcd_Device::SetPixels(this->tarea.x1, this->tarea.y1,
this->tarea.x2, this->tarea.y2, color);
/*
* 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 a value from @ref gTextMode
*
* Define 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
*
* @returns 0 with the desired area defined if all the coordinates are valid,
* otherwise returns non zero error code with the area set to the full display
*
* @b Examples:
* @code
* // top of right side of display, 10 columns by 2 lines
* textarea.DefineArea(GLCD.CenterX, GLCD.Top, 10, 2, System5x7);
*
* // top of right side of display, 10 columns by 2 lines, reverse scrolling
* textarea.DefineArea(GLCD.CenterX, GLCD.Top, 10, 2, System5x7, gTextMode_SCROLLDOWN);
* @endcode
* @note
* Upon defining the text area, the cursor position for the text area will be
* set to x,y
*
* @see ClearArea()
* @see SetAreaMode() ClearAreaMode()
* @see gTextMode
* @see \ref gText(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, gTextMode mode) "gText()"
*/
uint8_t
gText::DefineArea(uint8_t x, uint8_t y, uint8_t columns, uint8_t rows, Font_t font, gTextMode mode)
{
uint8_t x2,y2;
uint8_t width, height;
this->SelectFont(font);
width = FontRead(font+FONT_FIXED_WIDTH);
height = FontRead(font+FONT_HEIGHT);
if(!isNoPadFixedFont(font)) // all fonts except NoPadFixed fonts get pad pixels
{
width++;
height++;
}
x2 = x + columns * width -1;
y2 = y + rows * height -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 a value from @ref gTextMode
*
* Define 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 0 with the desired area defined if all the coordinates are valid,
* otherwise returns non zero error code with the area set to the full display
*
* @b Examples:
* @code
* // upper left quardrant
* textarea.DefineArea(GLCD.Left, GLCD.Top, GLCD.CenterX, GLCD.CenterY);
*
* // Right half, reverse scroll
* textarea.DefineArea(GLCD.CenterX, GLCD.Top, GLCD.Right, GLCD.Bottom, gTextMode_SCROLLDOWN);
* @endcode
* @note
* Upon creation of the text area, the cursor position for the text area will be set to x1, y1
*
* @see ClearArea()
* @see SetAreaMode()
* @see ClearAreaMode()
* @see gTextMode
* @see \ref gText::DefineArea(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, gTextMode mode) "gText()"
*
*/
uint8_t
gText::DefineArea(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, gTextMode mode)
{
uint8_t ret = GLCD_ENOERR; // assume call will work
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_gTEXTMODE;
ret = GLCD_EINVAL;
}
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
}
/*
* set cursor position for the area
*/
this->x = x1;
this->y = y1;
#ifndef GLCDCFG_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 a value from @ref gTextMode
*
* Define a text area using a selection form a set of predefined areas
* without setting or altering the area's font.
*
* The area within the newly defined text area is intentionally not cleared.
*
* mode is an optional parameter and defaults to normal/up scrolling
*
* @returns @em 0 with the desired area defined if all the coordinates are valid,
* otherwise returns non zero error code with the area set to the full display
*
* @b Examples:
* @code
* // right side of display
* textarea.DefineArea(textAreaRIGHT);
*
* // top half of display
* textarea.DefineArea(textAreaTOP);
*
* // upper left quadrant reverse scroll
* textarea.DefineArea(textAreaTOPLEFT, gTextMode_SCROLLDOWN);
* @endcode
*
* @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 SetAreaMode()
* @see ClearAreaMode()
* @see predefinedArea
* @see gTextMode
* @see \ref gText::gText(predefinedArea selection, Font_t font, gTextMode mode) "gText()"
*
*/
uint8_t
gText::DefineArea(predefinedArea selection, gTextMode 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);
}
/**
* Define a predefined generic text area and font
*
* @param selection a value from @ref predefinedArea
* @param font a font definition
* @param mode a value from @ref gTextMode
*
* Define a text area using a selection from 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
*
* @returns @em 0 with the desired area defined if all the coordinates are valid,
* otherwise returns non zero error code with the area set to the full display
*
* @b Examples:
* @code
* // right side of display
* textarea.DefineArea(textAreaRIGHT, System5x7);
*
* // top half of display
* textarea.DefineArea(textAreaTOP, System5x7);
*
* // upper left quadrant reverse scroll
* textarea.DefineArea(textAreaTOPLEFT, System5x7, gTextMode_SCROLLDOWN);
* @endcode
*
* @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 SetAreaMode()
* @see ClearAreaMode()
* @see predefinedArea
* @see gTextMode
*
*/
uint8_t
gText::DefineArea(predefinedArea selection, Font_t font, gTextMode mode)
{
this->SelectFont(font);
return this->DefineArea(selection, mode);
}
/**
* Get Text Area property
*
* @param property a value from @ref gTextProp_t
* Get Text Area property
*
* @returns the value of the specified text area property.
*
* @b Examples:
* @code
* height = GLCD.GetAreaProp(gTextProp_FontHeight); // font rendered height
* cols = GLCD.GetAreaProp(gTextProp_cols); // text columns
* rows = GLCD.GetAreaProp(gTextProp_rows); // text rows
* cols = textarea.GetAreaProp(gTextProp_cols); // text columns
* rows = textarea.GetAreaProp(gTextProp_rows); // text rows
* x1 = textarea.GetAreaProp(gTextProp_x1); // upper left x coordinate
* y1 = textarea.GetAreaProp(gTextProp_y1); // upper left y coordinate
* x2 = textarea.GetAreaProp(gTextProp_x2); // lower right x coordinate
* y2 = textarea.GetAreaProp(gTextProp_y2); // lower right y coordinate
* width = textarea.GetAreaProp(gTextProp_FontWidth);// font rendered fixed width
* minC = textarea.GetAreaProp(gTextProp_minC); // minimum font character code
* maxC = textarea.GetAreaProp(gTextProp_maxC); // maximum font character code
* @endcode
*
* @note
* The value returned for gTextProp_cols is based on the fixed width
* maximum size of the font.
* If a variable font is used, the true number of
* actual columns possible will vary depending on the characters rendered.
*
* gTextProp_FontWidth and gTextProp_FontHeight return rendered sizes
* in pixels which include any inter character padding pixels.
*
*
* @see DefineArea()
* @see CharHeight()
* @see CharWidth()
*
*/
int
gText::GetAreaProp(gTextProp_t property)
{
int rval;
switch(property)
{
case gTextProp_x1:
rval = this->tarea.x1;
break;
case gTextProp_y1:
rval = this->tarea.y1;
break;
case gTextProp_x2:
rval = this->tarea.x2;
break;
case gTextProp_y2:
rval = this->tarea.y2;
break;
case gTextProp_cols:
if(this->Font)
{
rval = (this->tarea.x2 - this->tarea.x1+1) / GetAreaProp(gTextProp_FontWidth);
}
else
{
rval = 0;
}
break;
case gTextProp_rows:
if(this->Font)
rval = (this->tarea.y2 - this->tarea.y1+1) / GetAreaProp(gTextProp_FontHeight);
else
rval = 0;
break;
case gTextProp_FontWidth: // always returns font's rendered max fixed width
if(this->Font)
{
rval = FontRead(this->Font+FONT_FIXED_WIDTH);
if(isFixedWidthPaddedFont(this->Font)) // there is 1 pixel pad on right
rval++;
}
else
{
rval = 0;
}
break;
case gTextProp_FontHeight:
rval = this->CharHeight(0);
break;
case gTextProp_minC:
return(FontRead(this->Font+FONT_FIRST_CHAR));
break;
case gTextProp_maxC:
return(FontRead(this->Font+FONT_FIRST_CHAR) + FontRead(this->Font+FONT_CHAR_COUNT) -1);
break;
default:
rval = 0;
}
return(rval);
}
/*
* 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 GLCDCFG_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 //GLCDCFG_NO_SCROLLDOWN
/*
* Handle all special processing characters
* returns non zero if character was a special character that was handled.
* returns zero if character was not a special character.
*/
uint8_t gText::SpecialChar(uint8_t c)
{
if(c == '\n')
{
uint8_t height = FontRead(this->Font+FONT_HEIGHT);
if(isNoPadFixedFont(this->Font))
height--; // reduce by one since code below assumes padding
#ifdef CONSIDER_FOR_LATER
/*
* Special check for 1 line text areas to avoid complex scrolling
* On single line text areas, clear the area and home the cursor.
*/
if(this->tarea.y2 == this->tarea.y1 + height)
{
this->ClearArea();
}
#endif
/*
* 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 GLCDCFG_NO_SCROLLDOWN
if(!(this->tarea.mode & SCROLL_DOWN))
#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 GLCDCFG_NODEFER_SCROLL
if(!this->need_scroll)
{
this->need_scroll = 1;
return(1);
}
#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.