forked from achimdoebler/UGUI
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ugui.c
2352 lines (2108 loc) · 60.8 KB
/
ugui.c
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
/* -------------------------------------------------------------------------------- */
/* -- µGUI - Generic GUI module (C)Achim Döbler, 2015 -- */
/* -------------------------------------------------------------------------------- */
// µGUI is a generic GUI module for embedded systems.
// This is a free software that is open for education, research and commercial
// developments under license policy of following terms.
//
// Copyright (C) 2015, Achim Döbler, all rights reserved.
// URL: http://www.embeddedlightning.com/
//
// * The µGUI module is a free software and there is NO WARRANTY.
// * No restriction on use. You can use, modify and redistribute it for
// personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
// * Redistributions of source code must retain the above copyright notice.
//
/* -------------------------------------------------------------------------------- */
#include "ugui.h"
/* Static functions */
static UG_RESULT _UG_WindowDrawTitle( UG_WINDOW* wnd );
static void _UG_WindowUpdate( UG_WINDOW* wnd );
static UG_RESULT _UG_WindowClear( UG_WINDOW* wnd );
static void _UG_FontSelect( UG_FONT *font);
static UG_S16 _UG_PutChar( UG_CHAR chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc);
static UG_S16 _UG_GetCharData(UG_CHAR encoding, const UG_U8 **p);
#ifdef UGUI_USE_UTF8
static UG_U16 _UG_DecodeUTF8(char **str);
#endif
static UG_U16 ptr_8to16(const UG_U8* p){
UG_U16 d = *p++;
return ((d<<8) | *p);
}
static const UG_COLOR pal_window[] = {
C_PAL_WINDOW
};
/* Pointer to the gui */
static UG_GUI* gui;
UG_S16 UG_Init( UG_GUI* g, UG_DEVICE *device )
{
UG_U8 i;
g->device = device;
#if defined(UGUI_USE_CONSOLE)
g->console.x_start = 4;
g->console.y_start = 4;
g->console.x_end = g->device->x_dim - g->console.x_start-1;
g->console.y_end = g->device->y_dim - g->console.x_start-1;
g->console.x_pos = g->console.x_end;
g->console.y_pos = g->console.y_end;
#endif
g->char_h_space = 1;
g->char_v_space = 1;
g->font=NULL;
g->currentFont.bytes_per_char = 0;
g->currentFont.char_height = 0;
g->currentFont.char_width = 0;
g->currentFont.number_of_chars = 0;
g->currentFont.number_of_offsets = 0;
g->currentFont.widths = NULL;
g->currentFont.offsets = NULL;
g->currentFont.data = NULL;
g->currentFont.font = NULL;
g->desktop_color = C_DESKTOP_COLOR;
g->fore_color = C_WHITE;
g->back_color = C_BLACK;
g->next_window = NULL;
g->active_window = NULL;
g->last_window = NULL;
/* Clear drivers */
for(i=0;i<NUMBER_OF_DRIVERS;i++)
{
g->driver[i].driver = NULL;
g->driver[i].state = 0;
}
gui = g;
return 1;
}
UG_S16 UG_SelectGUI( UG_GUI* g )
{
gui = g;
return 1;
}
UG_GUI* UG_GetGUI( void )
{
return gui;
}
/*
* Sets the GUI font
*/
void UG_FontSelect( UG_FONT* font )
{
gui->font = font;
}
void UG_FillScreen( UG_COLOR c )
{
UG_FillFrame(0,0,gui->device->x_dim-1,gui->device->y_dim-1,c);
}
void UG_FillFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_S16 n,m;
if ( x2 < x1 )
swap(x1,x2);
if ( y2 < y1 )
swap(y1,y2);
/* Is hardware acceleration available? */
if ( gui->driver[DRIVER_FILL_FRAME].state & DRIVER_ENABLED )
{
if( ((UG_RESULT(*)(UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c))gui->driver[DRIVER_FILL_FRAME].driver)(x1,y1,x2,y2,c) == UG_RESULT_OK ) return;
}
for( m=y1; m<=y2; m++ )
{
for( n=x1; n<=x2; n++ )
{
gui->device->pset(n,m,c);
}
}
}
void UG_FillRoundFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd;
if ( x2 < x1 )
swap(x1,x2);
if ( y2 < y1 )
swap(y1,y2);
if ( r<=0 ) return;
xd = 3 - (r << 1);
x = 0;
y = r;
UG_FillFrame(x1 + r, y1, x2 - r, y2, c);
while ( x <= y )
{
if( y > 0 )
{
UG_DrawLine(x2 + x - r, y1 - y + r, x2+ x - r, y + y2 - r, c);
UG_DrawLine(x1 - x + r, y1 - y + r, x1- x + r, y + y2 - r, c);
}
if( x > 0 )
{
UG_DrawLine(x1 - y + r, y1 - x + r, x1 - y + r, x + y2 - r, c);
UG_DrawLine(x2 + y - r, y1 - x + r, x2 + y - r, x + y2 - r, c);
}
if ( xd < 0 )
{
xd += (x << 2) + 6;
}
else
{
xd += ((x - y) << 2) + 10;
y--;
}
x++;
}
}
void UG_DrawMesh( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_U16 spacing, UG_COLOR c )
{
UG_U16 p;
if ( x2 < x1 )
swap(x1,x2);
if ( y2 < y1 )
swap(y1,y2);
for( p=y1; p<y2; p+=spacing )
{
UG_DrawLine(x1, p, x2, p, c);
}
UG_DrawLine(x1, y2, x2, y2, c);
for( p=x1; p<x2; p+=spacing )
{
UG_DrawLine(p, y1, p, y2, c);
}
UG_DrawLine(x2, y1, x2, y2, c);
}
void UG_DrawFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_DrawLine(x1,y1,x2,y1,c);
UG_DrawLine(x1,y2,x2,y2,c);
UG_DrawLine(x1,y1,x1,y2,c);
UG_DrawLine(x2,y1,x2,y2,c);
}
void UG_DrawRoundFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 r, UG_COLOR c )
{
if(r == 0)
{
UG_DrawFrame(x1, y1, x2, y2, c);
return;
}
if ( x2 < x1 )
swap(x1,x2);
if ( y2 < y1 )
swap(y1,y2);
if ( r > x2 ) return;
if ( r > y2 ) return;
UG_DrawLine(x1+r, y1, x2-r, y1, c);
UG_DrawLine(x1+r, y2, x2-r, y2, c);
UG_DrawLine(x1, y1+r, x1, y2-r, c);
UG_DrawLine(x2, y1+r, x2, y2-r, c);
UG_DrawArc(x1+r, y1+r, r, 0x0C, c);
UG_DrawArc(x2-r, y1+r, r, 0x03, c);
UG_DrawArc(x1+r, y2-r, r, 0x30, c);
UG_DrawArc(x2-r, y2-r, r, 0xC0, c);
}
void UG_DrawPixel( UG_S16 x0, UG_S16 y0, UG_COLOR c )
{
gui->device->pset(x0,y0,c);
}
void UG_DrawCircle( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd,yd,e;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 1 - (r << 1);
yd = 0;
e = 0;
x = r;
y = 0;
while ( x >= y )
{
gui->device->pset(x0 - x, y0 + y, c);
gui->device->pset(x0 - x, y0 - y, c);
gui->device->pset(x0 + x, y0 + y, c);
gui->device->pset(x0 + x, y0 - y, c);
gui->device->pset(x0 - y, y0 + x, c);
gui->device->pset(x0 - y, y0 - x, c);
gui->device->pset(x0 + y, y0 + x, c);
gui->device->pset(x0 + y, y0 - x, c);
y++;
e += yd;
yd += 2;
if ( ((e << 1) + xd) > 0 )
{
x--;
e += xd;
xd += 2;
}
}
}
void UG_FillCircle( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 3 - (r << 1);
x = 0;
y = r;
while ( x <= y )
{
if( y > 0 )
{
UG_DrawLine(x0 - x, y0 - y,x0 - x, y0 + y, c);
UG_DrawLine(x0 + x, y0 - y,x0 + x, y0 + y, c);
}
if( x > 0 )
{
UG_DrawLine(x0 - y, y0 - x,x0 - y, y0 + x, c);
UG_DrawLine(x0 + y, y0 - x,x0 + y, y0 + x, c);
}
if ( xd < 0 )
{
xd += (x << 2) + 6;
}
else
{
xd += ((x - y) << 2) + 10;
y--;
}
x++;
}
UG_DrawCircle(x0, y0, r,c);
}
void UG_DrawArc( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_U8 s, UG_COLOR c )
{
UG_S16 x,y,xd,yd,e;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 1 - (r << 1);
yd = 0;
e = 0;
x = r;
y = 0;
while ( x >= y )
{
// Q1
if ( s & 0x01 ) gui->device->pset(x0 + x, y0 - y, c);
if ( s & 0x02 ) gui->device->pset(x0 + y, y0 - x, c);
// Q2
if ( s & 0x04 ) gui->device->pset(x0 - y, y0 - x, c);
if ( s & 0x08 ) gui->device->pset(x0 - x, y0 - y, c);
// Q3
if ( s & 0x10 ) gui->device->pset(x0 - x, y0 + y, c);
if ( s & 0x20 ) gui->device->pset(x0 - y, y0 + x, c);
// Q4
if ( s & 0x40 ) gui->device->pset(x0 + y, y0 + x, c);
if ( s & 0x80 ) gui->device->pset(x0 + x, y0 + y, c);
y++;
e += yd;
yd += 2;
if ( ((e << 1) + xd) > 0 )
{
x--;
e += xd;
xd += 2;
}
}
}
void UG_DrawLine( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_S16 n, dx, dy, sgndx, sgndy, dxabs, dyabs, x, y, drawx, drawy;
/* Is hardware acceleration available? */
if ( gui->driver[DRIVER_DRAW_LINE].state & DRIVER_ENABLED )
{
if( ((UG_RESULT(*)(UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c))gui->driver[DRIVER_DRAW_LINE].driver)(x1,y1,x2,y2,c) == UG_RESULT_OK ) return;
}
dx = x2 - x1;
dy = y2 - y1;
dxabs = (dx>0)?dx:-dx;
dyabs = (dy>0)?dy:-dy;
sgndx = (dx>0)?1:-1;
sgndy = (dy>0)?1:-1;
x = dyabs >> 1;
y = dxabs >> 1;
drawx = x1;
drawy = y1;
gui->device->pset(drawx, drawy,c);
if( dxabs >= dyabs )
{
for( n=0; n<dxabs; n++ )
{
y += dyabs;
if( y >= dxabs )
{
y -= dxabs;
drawy += sgndy;
}
drawx += sgndx;
gui->device->pset(drawx, drawy,c);
}
}
else
{
for( n=0; n<dyabs; n++ )
{
x += dxabs;
if( x >= dyabs )
{
x -= dyabs;
drawx += sgndx;
}
drawy += sgndy;
gui->device->pset(drawx, drawy,c);
}
}
}
/* Draw a triangle */
void UG_DrawTriangle( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 x3, UG_S16 y3, UG_COLOR c ){
UG_DrawLine(x1, y1, x2, y2, c);
UG_DrawLine(x2, y2, x3, y3, c);
UG_DrawLine(x3, y3, x1, y1, c);
}
/* Fill a triangle */
void UG_FillTriangle( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 x3, UG_S16 y3, UG_COLOR c ){
UG_S16 a, b, y, last;
/* Sort coordinates by Y order (y3 >= y2 >= y1) */
if (y1 > y2) {
swap(y1, y2); swap(x1, x2);
}
if (y2 > y3) {
swap(y3, y2); swap(x3, x2);
}
if (y1 > y2) {
swap(y1, y2); swap(x1, x2);
}
/* Handle awkward all-on-same-line case as its own thing */
if (y1 == y3) {
a = b = x1;
if (x2 < a) {
a = x2;
} else if (x2 > b) {
b = x2;
}
if (x3 < a) {
a = x3;
} else if (x3 > b) {
b = x3;
}
UG_DrawLine(a, y1, b + 1, y1, c);
return;
}
UG_S16
dx01 = x2 - x1,
dy01 = y2 - y1,
dx02 = x3 - x1,
dy02 = y3 - y1,
dx12 = x3 - x2,
dy12 = y3 - y2,
sa = 0,
sb = 0;
/* For upper part of triangle, find scanline crossings for segments
* 0-1 and 0-2. If y2=y3 (flat-bottomed triangle), the scanline y2
* is included here (and second loop will be skipped, avoiding a /0
* error there), otherwise scanline y2 is skipped here and handled
* in the second loop...which also avoids a /0 error here if y1=y2
* (flat-topped triangle).
*/
if (y2 == y3) {
last = y2; /* Include y2 scanline */
} else {
last = y2 - 1; /* Skip it */
}
for (y = y1; y <= last; y++) {
a = x1 + sa / dy01;
b = x1 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
*/
if (a > b) {
swap(a, b);
}
UG_DrawLine(a, y, b + 1, y, c);
}
/* For lower part of triangle, find scanline crossings for segments
* 0-2 and 1-2. This loop is skipped if y2=y3.
*/
sa = dx12 * (y - y2);
sb = dx02 * (y - y1);
for (; y <= y3; y++) {
a = x2 + sa / dy12;
b = x1 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x2 + (x3 - x2) * (y - y2) / (y3 - y2);
b = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
*/
if (a > b) {
swap(a, b);
}
UG_DrawLine(a, y, b + 1, y, c);
}
}
void UG_PutString( UG_S16 x, UG_S16 y, char* str )
{
UG_S16 xp,yp,cw;
UG_CHAR chr;
xp=x;
yp=y;
_UG_FontSelect(gui->font);
while ( *str != 0 )
{
#ifdef UGUI_USE_UTF8
if(! gui->currentFont.is_old_font){ // Old font charset compatibility
chr = _UG_DecodeUTF8(&str);
}
else{
chr = *str++;
}
#else
chr = *str++;
#endif
if ( chr == '\n' )
{
xp = gui->device->x_dim;
continue;
}
cw = _UG_GetCharData(chr,NULL);
if(cw==-1) continue;
if ( xp + cw > gui->device->x_dim - 1 )
{
xp = x;
yp += gui->currentFont.char_height+gui->char_v_space;
}
_UG_PutChar(chr, xp, yp, gui->fore_color, gui->back_color);
xp += cw + gui->char_h_space;
}
if((gui->driver[DRIVER_FILL_AREA].state & DRIVER_ENABLED))
((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(-1,-1,-1,-1); // -1 to indicate finish
}
void UG_PutChar( UG_CHAR chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc )
{
_UG_FontSelect(gui->font);
_UG_PutChar(chr,x,y,fc,bc);
if((gui->driver[DRIVER_FILL_AREA].state & DRIVER_ENABLED))
((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(-1,-1,-1,-1); // -1 to indicate finish
}
#if defined(UGUI_USE_CONSOLE)
void UG_ConsolePutString( char* str )
{
UG_CHAR chr;
UG_S16 cw;
_UG_FontSelect(gui->font);
while ( *str != 0 )
{
#ifdef UGUI_USE_UTF8
if(! gui->currentFont.is_old_font){ // Old font charset compatibility
chr = _UG_DecodeUTF8(&str);
}
else{
chr = *str++;
}
#else
chr = *str++;
#endif
if ( chr == '\n' )
{
gui->console.x_pos = gui->device->x_dim;
str++;
continue;
}
cw = _UG_GetCharData(chr, NULL);
if(cw==-1){
continue;
}
gui->console.x_pos += cw+gui->char_h_space;
if ( gui->console.x_pos+cw > gui->console.x_end )
{
gui->console.x_pos = gui->console.x_start;
gui->console.y_pos += gui->currentFont.char_height+gui->char_v_space;
}
if ( gui->console.y_pos+ gui->currentFont.char_height > gui->console.y_end )
{
gui->console.x_pos = gui->console.x_start;
gui->console.y_pos = gui->console.y_start;
UG_FillFrame(gui->console.x_start,gui->console.y_start,gui->console.x_end,gui->console.y_end,gui->console.back_color);
}
_UG_PutChar(chr, gui->console.x_pos, gui->console.y_pos, gui->console.fore_color, gui->console.back_color);
}
if((gui->driver[DRIVER_FILL_AREA].state & DRIVER_ENABLED))
((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(-1,-1,-1,-1); // -1 to indicate finish
}
void UG_ConsoleSetArea( UG_S16 xs, UG_S16 ys, UG_S16 xe, UG_S16 ye )
{
gui->console.x_start = xs;
gui->console.y_start = ys;
gui->console.x_end = xe;
gui->console.y_end = ye;
}
void UG_ConsoleSetForecolor( UG_COLOR c )
{
gui->console.fore_color = c;
}
void UG_ConsoleSetBackcolor( UG_COLOR c )
{
gui->console.back_color = c;
}
#endif
void UG_SetForecolor( UG_COLOR c )
{
gui->fore_color = c;
}
void UG_SetBackcolor( UG_COLOR c )
{
gui->back_color = c;
}
UG_S16 UG_GetXDim( void )
{
return gui->device->x_dim;
}
UG_S16 UG_GetYDim( void )
{
return gui->device->y_dim;
}
void UG_FontSetHSpace( UG_U16 s )
{
gui->char_h_space = s;
}
void UG_FontSetVSpace( UG_U16 s )
{
gui->char_v_space = s;
}
void UG_FontSetTransparency( UG_U8 t )
{
gui->transparent_font=t;
}
UG_U8 UG_FontGetTransparency( void )
{
return gui->transparent_font;
}
/* -------------------------------------------------------------------------------- */
/* -- INTERNAL FUNCTIONS -- */
/* -------------------------------------------------------------------------------- */
/*
* Parses a pointer to a string, and converts it to Unicode.
* Automatically increasing the pointer address, so the calling function doesn't need to take care of that.
*
* Based on https://github.com/olikraus/u8g2/blob/master/csrc/u8x8_8x8.c
*
*/
#ifdef UGUI_USE_UTF8
UG_CHAR _UG_DecodeUTF8(char **str) {
char c=**str;
if ( c < 0x80 ) // Fast detection for simple ASCII
{
*str = *str+1;
return c;
}
UG_U8 bytes_left=0;
UG_CHAR encoding=0;
while(**str)
{
c=**str;
*str = *str+1;
if ( bytes_left == 0 )
{
if ( c < 0xe0 ) // 2 byte sequence
{
bytes_left = 1;
c &= 0x01f;
}
else if ( c < 0xf0 ) // 3 byte sequence
{
bytes_left = 2;
c &= 15;
}
else if ( c < 0xf8 ) // 4 byte sequence
{
bytes_left = 3;
c &= 7;
}
else if ( c < 0xfc ) // 5 byte sequence
{
bytes_left = 4;
c &= 3;
}
else // 6 byte sequence
{
bytes_left = 5;
c &= 1;
}
encoding = c;
}
else
{
encoding<<=6;
encoding |= (c & 0x3F);
if ( --bytes_left == 0 )
break;
}
}
return encoding;
}
#endif
/*
* Load char bitmap address into p, return the font width
*/
UG_S16 _UG_GetCharData(UG_CHAR encoding, const UG_U8 **p){
static UG_CHAR last_encoding;
static UG_S16 last_width;
static const UG_U8 * last_p;
static UG_FONT * last_font;
UG_U16 start=0;
UG_U16 skip=0;
UG_U16 t=0;
UG_U8 range=0;
UG_U8 found=0;
if( gui->currentFont.font==last_font && encoding==last_encoding){ // If called with the same arguments, return cached data
if(p){
*p=last_p; // Load char bitmap address
}
return last_width;
}
if( gui->currentFont.is_old_font){ // Compatibility with old fonts charset
switch ( encoding )
{
case 0xF6: encoding = 0x94; break; // ö
case 0xD6: encoding = 0x99; break; // Ö
case 0xFC: encoding = 0x81; break; // ü
case 0xDC: encoding = 0x9A; break; // Ü
case 0xE4: encoding = 0x84; break; // ä
case 0xC4: encoding = 0x8E; break; // Ä
case 0xB5: encoding = 0xE6; break; // µ
case 0xB0: encoding = 0xF8; break; // °
}
}
for(;t< gui->currentFont.number_of_offsets;t++) // Seek through the offsets
{
UG_U16 curr_offset = ptr_8to16( gui->currentFont.offsets+(t*2)); // Offsets are 16-bit, splitted in 2 byte values
if(curr_offset&0x8000) // If the offset has the MSB bit set, it means it's the a range start
{
start=curr_offset&0x7FFF; // Store range start
range=1; // Set flag
}
else if(range) // If range previously set, this is the range end
{
if(encoding>=start && encoding<=curr_offset) // If the encoding is between the range
{
skip += (encoding-start); // Calculate the skip value
found=1;
break;
}
else if(encoding<start) // If the encoding is lower than current range start, the char is not in the font
break;
skip += ((curr_offset-start)+1); // Encoding not found in the current range, increase skip size and clear range flasg
range=0;
}
else // Range not set, this is a single char offset
{
if(encoding==curr_offset) // If matching the current offset char
{
found=1;
break;
}
else if (encoding<curr_offset) // If the encoding is lower than current range, the char is not in the font
{
break;
}
skip++; // Else, increase skip and keep searching
}
}
if(found) // If char found
{
last_font = gui->currentFont.font; // Update cached data
last_encoding = encoding;
last_p = ( gui->currentFont.data+(skip* gui->currentFont.bytes_per_char));
if( gui->currentFont.widths){ // If width table available
last_width = *( gui->currentFont.widths+skip); // Use width from table
}
else{
last_width = gui->currentFont.char_width; // Else use width from char width
}
if(p){
*p=last_p; // Load char bitmap address
}
return(last_width); // Return char width
}
return -1; // -1 = char not found
}
/*
* Updates the current font data
*/
void _UG_FontSelect( UG_FONT *font){
if( gui->currentFont.font==font)
return;
gui->currentFont.font = font; // Save Font pointer
gui->currentFont.font_type = 0x7F & *font; // Byte 0: Font_type
gui->currentFont.is_old_font = (0x80 & *font++)&&1; // Byte 0: Bit 7 indicates old or new font type. 1=old font, 0=new font
gui->currentFont.char_width = *font++; // Byte 1: Char width
gui->currentFont.char_height = *font++; // Byte 2: Char height
gui->currentFont.number_of_chars = ptr_8to16(font); // Bytes 3+4: Number of chars
font+=2;
gui->currentFont.number_of_offsets = ptr_8to16(font); // Bytes 5+6: Number of offsets
font+=2;
gui->currentFont.bytes_per_char = ptr_8to16(font); // Bytes 7+8: Bytes per char
font+=2;
if(*font++){ // Byte 9: 1=Width table present, 0=not present
gui->currentFont.widths = font; // Save pointer to width table
font+= gui->currentFont.number_of_chars; // Increase number of chars
}
else{
gui->currentFont.widths = NULL; // No width table
}
gui->currentFont.offsets = font; // Save pointer to offset table
font += ( gui->currentFont.number_of_offsets*2); // Increase pointer by number of offsets*2 (2-byte values)
gui->currentFont.data = font; // Save pointer to bitmap data
}
UG_S16 _UG_PutChar( UG_CHAR chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc)
{
UG_U16 x0=0,y0=0,i,j,k,bn,fpixels=0,bpixels=0;
UG_S16 c;
UG_U8 b,trans=gui->transparent_font,driver=(gui->driver[DRIVER_FILL_AREA].state & DRIVER_ENABLED);
const UG_U8 * data; // Pointer to current char bitmap
UG_COLOR color;
void(*push_pixels)(UG_U16, UG_COLOR) = NULL;
UG_S16 actual_char_width = _UG_GetCharData(chr, &data);
if(actual_char_width==-1)
return -1; // Char not presnt in the font
bn = gui->currentFont.char_width;
if ( !bn ){
return 0;
}
bn >>= 3;
if ( gui->currentFont.char_width % 8 ) bn++;
/* Is hardware acceleration available? */
if (driver)
{
push_pixels = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x,y,x+actual_char_width-1,y+ gui->currentFont.char_height-1);
}
if ( gui->currentFont.font_type == FONT_TYPE_1BPP)
{
for( j=0;j< gui->currentFont.char_height;j++ )
{
c=0;
for( i=0;i<bn;i++ )
{
b = *data++;
for( k=0;(k<8) && c<actual_char_width; k++ )
{
if(b & 0x01 ) // Foreground pixel detected
{
if(driver)
{ // Accelerated output
if(bpixels && !trans) // Draw accumulated background pixels, only if transparent mode disabled
{
push_pixels(bpixels,bc); // Drawing accumulated pixels removes a lot of overhead, drawing speed is at least 3x faster
bpixels=0;
}
if(!fpixels && trans) // Store first foreground pixel position for transparent drawing
{
x0=x+c;
y0=y+j;
}
fpixels++; // Instead writing every pixel, count consecutive pixels, then send accumulated pixels in a single transaction
}
else
{ // Not accelerated output
gui->device->pset(x+c,y+j,fc);
}
}
else // Background pixel detected
{
if(driver)
{ // Accelerated output
if(fpixels) // Draw accumulated foreground pixels
{
if(!trans) // In transparent mode, we don't need to address pixel areas, so just keep pushing pixels
{
push_pixels(fpixels,fc);
fpixels=0;
}
else{ // In transparent mode, drawing needs to be broken in smaller parts, avoiding the background areas
while(fpixels)
{
UG_U16 width = (x+actual_char_width)-x0; // Detect available pixels in the current row from current x position
if(x0==x || fpixels<width) // If pixel draw count is lower than available pixels, or drawing at start of the row, drawn as-is
{
push_pixels = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x0,y0,x0+width-1,y0+(fpixels/actual_char_width));
push_pixels(fpixels,fc);
fpixels=0;
}
else // If pixel draw count is higher than available pixels, there's at least second line, drawn this row first
{
push_pixels = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x0,y0,x0+width-1,y0);
push_pixels(fpixels,fc);
fpixels -= width;
x0=x;
y0++;
}
}
}
}
bpixels++;
}
else if(!trans) // Not accelerated output
{
gui->device->pset(x+c,y+j,bc);
}
}
b >>= 1;
c++;
}
}
}
if(driver){ // After finishing, ensure there're no remaining pixels left, make another pass
if(bpixels && !trans)
{
push_pixels(bpixels,bc);
}
else if(fpixels)
{
if(!trans)
{
push_pixels(fpixels,fc);
}
else
{
while(fpixels)
{
UG_U16 width = (x+actual_char_width)-x0;
if(x0==x || fpixels<width)
{
push_pixels = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x0,y0,x0+width-1,y0+(fpixels/actual_char_width));
push_pixels(fpixels,fc);
fpixels=0;
}
else
{
push_pixels = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x0,y0,x0+width-1,y0);
push_pixels(fpixels,fc);
fpixels -= width;
x0=x;
y0++;
}
}
}
}
}
}
#if defined(UGUI_USE_COLOR_RGB888) || defined(UGUI_USE_COLOR_RGB565)
else if ( gui->currentFont.font_type == FONT_TYPE_8BPP)
{
for( j=0;j< gui->currentFont.char_height;j++ )