-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_FTcharmap.c
4721 lines (3972 loc) · 166 KB
/
egi_FTcharmap.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
/*------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Note:
1. FreeType 2 is licensed under FTL/GPLv3 or GPLv2.
Based on freetype-2.5.5
2. If there are any selection marks, but not displayed in current charmap,
To press 'DEL' or 'BACKSPACE': it first deletes those selected chars,
then scrolls charmap to display/locate the typing cursor.
To press any other key: it just scrolls charmap to display/locate selection marks.
3. Sometimes it may needs more than one press/operation ( delete, backspace, shift etc.)
to make the cursor move, this is because there is/are nonprintable chars
with zero width.
--- Definition and glossary ---
0. cursor: A blinking cursor is an editing point, corresponding with chmap->pchoff pointing to some where of
chmap->txtbuff, to/from which you may add/delete chars.
--- NOTICE ---
1. A cursor usually can move to the left side of the last char of a dline, NOT the right side.
If a cursor can move to the right side of the last char of a dline, or say end of the dline, that
means it is a newline char ('\n'), OR it's the EOF.
2. Sometimes it may needs more than one press/operation ( delete, backspace, shift etc.)
to make the cursor move, this is because there is/are nonprintable chars with zero width.
3. If the cursor(pchoff) in NOT shown in current charmap, just press any key to scroll to locate it.
!!! WARNING !!!
If you press the deleting key, it will execute anyway, even the cursor(pchoff) is NOT in the current charmap!
though it will scroll to cursor position after deletion, you may NOT be aware of anything that have been deleted.
So always bring the cursor within your sight before editing!
1. char: A printable ASCII code OR a local character with UFT-8 encoding.
2. charmap: A EGI_FTCHAR_MAP struct that holds data of currently displayed chars,
of both their corresponding coordinates in displaying window and offset position in memory.
FAINT WARNING: Do NOT confuse with Freetype charmap concept!
3. dlines: displayed/charmapped line, A line starts/ends at displaying window left/right end side.
retline: A line starts/ends by a new line token '\n'.
4. scroll_up/down:
scroll up/down charmap for one dline
keep cursor position unchanged(relative to txtbuff)
Functions: FTcharmap_scroll_oneline_up(), FTcharmap_scroll_oneline_down()
5. shift_up/down/left/right:
shift typing cursor up/down/left/right.
charmap follow the cursor to scroll if it gets out of current charmap.
Functions: FTcharmap_shift_cursor_up(), FTcharmap_shift_cursor_down()
FTcharmap_shift_cursor_left(), FTcharmap_shift_cursor_right()
6. page_up/down:
scroll whole charmap up/down totally out of current displayed charmap,
keep cursor position unchanged(relative to txtbuff)
Functions: FTcharmap_page_up(), FTcharmap_page_down()
7. EOF: For txtbuff: A '\0' token to signal as end of the buffered string.
For saved file: A '\n' to comply with UNIX/POSIX file end standard.
8. Selection mark:
Two offsets of txtbuff, pchoff and pchoff2, which defines selected content/chars btween them.
if(pchoff==pchoff2), then no selection.
<--- PRE_Charmap Actions --->
PRE_1: Set chmap->txtdlncount ( txtdlinePos[] index,a link of txtbuff->charmap
as txtdlinePos[txtdlncount]=pref-txtbuff
and chmap->txtdlinePos[txtdlncount]==chmap->maplinePos[0] )
PRE_2: Set chmap->pref ( starting point for charmapping )
PRE_3: Set chmap->pchoff/pchoff2 ( chmap->pch/pch2: to be derived from pchoff/pchoff2 in charmapping! )
PRE_4: Set chmap->fix_cursor ( option: keep cursor position unchanged on screen )
PRE_5: Set chmap->follow_cursor ( option: auto. scroll to keep cursor always in current charmap )
PRE_6: Set chmap->request ( request charmapping exclusively and immediately )
<--- DO_Charmap: FTcharmap_uft8strings_writeFB() --->
charmap_1: Update chmap->chcount
Charmap_2: Update chmap->charX[], charY[],charPos[]
charmap_3: Update chmap->maplncount
charmap_4: Update chmap->maplinePos[]
charmap_5: Update chmap->txtdlcount ( NOTE: chmap->txtdlinePos[txtdlncount]==chmap->maplinePos[0] )
charmap_6: Update chmap->txtdlinePos[]
charmap_7: Check chmap->follow_cursor
charmap_8: Check chmap->fix_cursor
charmap_9: Draw selection mark
<--- POST_Charmap Actions: Also see FTcharmap_uft8strings_writeFB() --->
POST_1: Redraw cursor
POST_2: Check chmap->errbits
TODO:
1. A faster way to memmove...
2. A faster way to locate pch...
3. Delete/insert/.... editing functions will fail/abort if chmap->request!=0. ( loop trying/waiting ??)
4. TAB key code in FTcharmap_uft8strings_writeFB() may affect some editing functions, need to test/check.
As TAB key is NOT a fixed symwidth symbol any more! cstr_charlen_uft8() will fail in some case.
5. Type conflict: BANDMAP function param: pos as 'unsigned int', while FTcharmap startPos/endPos IS 'int'!
Enusure they are all >=0!
Journal:
2020-12-27.
1.Modify: int FTcharmap_get_txtdlIndex(EGI_FTCHAR_MAP *chmap, int pchoff)
to int FTcharmap_get_txtdlIndex(EGI_FTCHAR_MAP *chmap, unsigned int pchoff)
2.Modify: FTcharmap_uft8strings_writeFB()
When input fb_dev is NULL: For ASCII chars, call FTsymbol_unicode_writeFB()! instead of FT_Get_Advance()!
See. FTsymbol_uft8strings_pixlen() also.
2021-1-9.
1. Add chmap->charColorMap for go_backSpace()/insert_char()/delete_string()/insert_string.
2. Modify: FTcharmap_create() to add param 'charColorMap_ON' and 'fontcolor'.
2021-1-12.
1. Add chmap->hlmarkColorMap for go_backSpace()/insert_char()/delete_string()/insert_string.
2. Add FTcharmap_modify_charColor() and FTcharmap_modify_hlmarkColor().
2021-1-13.
1. Add param 'bool request' to FTcharmap_modify_charColor() and FTcharmap_modify_hlmarkColor().
2. Add chmap->precolor for insert_char() and insert_string().
2021-1-18.
1. Add FTcharmap_shrink_dlines().
2. Modify FTcharmap_create() and FTcharmap_uft8strings_writeFB() to embed/add chmap->face.
2021-1-28.
1. Modify FTcharmap_insert_string_nolock(): if(strsize<=0), DO NOT reset chmap->precolor=-1;
want to keep the value for next use. Example: XTERM use escape sequence code to set color
, and no string followed, ^[[32m[ 0.000000] ^[[0m^[[1mZone ranges:^[[0m^M
For FTcharmap_insert_char(), case of chsize<=0 is ruled out!
2. FTcharmap_uft8strings_writeFB(): Check FTsymbol_glyph_buffered(), and access egi_fontbuffer
to quickly extract advanceX.
2022-05-21:
1. FTcharmap_uft8strings_writeFB(): For Fullwidth/Halfwidth chars, get advance value by calling
FTsymbol_unicode_writeFB() instead of FT_Get_Advances().
2022-06-20:
1. EGI_FTCHAR_MAP: add member 'readOnly' and modify related functions:
FTcharmap_go_backspace(), FTcharmap_insert_string_nolock(), FTcharmap_insert_string(),
FTcharmap_insert_char(), FTcharmap_delete_string_nolock(), FTcharmap_delete_string(),
FTcharmap_copy_from_syspad(), FTcharmap_cut_to_syspad().
2022-06-22:
1. Add FTcharmap_goto_dline().
2022-06-25:
1. EGI_FTCHAR_MAP: Add member 'txtdlntotal'
2. FTcharmap_create(): Add param 'fw,fh'
3. FTcharmap_uft8strings_writeFB(): Remove param 'fw,fh'
4. FTcharmap_load_file():
4.1 Remove param 'txtsize', add param 'fullmap'.
4.2 Add segment 7-8, charmap to end to get txtdlntotal.
Midas Zhou
[email protected](Not in use since 2022_03_01)
-------------------------------------------------------------------*/
#include "egi_log.h"
#include "egi_debug.h"
#include "egi_FTcharmap.h"
#include "egi_cstring.h"
#include "egi_utils.h"
#include "egi_timer.h"
#include "egi_unihan.h"
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <errno.h>
static int FTcharmap_locate_charPos_nolock( EGI_FTCHAR_MAP *chmap, int x, int y); /* No mutex_lock */
static void FTcharmap_mark_selection(FBDEV *fb_dev, EGI_FTCHAR_MAP *chmap); /* No mutex_lock */
/*-------------------------------------------------
Drawing cursor as of EGI_FTCHAR_MAP.draw_cursor
@x,y: origin point.
@lndis: distance between two lines.
--------------------------------------------------*/
static void FTcharmap_draw_cursor(int x, int y, int lndis )
{
fbset_color(WEGI_COLOR_RED);
//draw_wline(&gv_fb_dev, x, y, x+1, y+lndis-1,2);
draw_filled_rect(&gv_fb_dev, x, y, x+1, y+lndis-1);
}
/*------------------------------------------------------------------
To create a char map with given params.
@txtsize: Size of txtbuff[], exclude '\0'.
! txtsize+1(EOF) bytes to be allocated.
@x0,y0: charmap left top point.
@height,width: Charmap window size,including margin area + mappixpl.
@offx,offy: offset, used to adjust margins.
@face: FT_Face
@mapsize: How many chars hold in the displaying map.
or Max. number of chars to be displayed once.
!!!NOTE, size-1 for char, and the last data is
always for new insterting point.
!size+1 units to be allocated.
@maplines: Max. displayed lines
@mappixpl: Max pixels per dline.
@maplndis: Distance between two dlines, in pixels.
@bkgcolor: Charmap background color.
@fontcolor: Default font color, it applys only charColorMap_ON is fale.
@charColorMap_ON: If true, Then initialize/create chmap->charColorMap
Else, chmap->fontcolor will used.
@hlmarkColorMap_ON: If true, Then initialize/create chmap->hlmarkColorMap
Return:
A pointer to char map OK
NULL Fails
---------------------------------------------------------------------------*/
EGI_FTCHAR_MAP* FTcharmap_create(size_t txtsize, int x0, int y0, int height, int width, int offx, int offy,
FT_Face face, int fw, int fh, size_t mapsize, size_t maplines, size_t mappixpl, int maplndis,
int bkgcolor, EGI_16BIT_COLOR fontcolor, bool charColorMap_ON, bool hlmarkColorMap_ON)
{
if( height<=0 || width <=0 ) {
printf("%s: Input height and/or width is invalid!\n",__func__);
return NULL;
}
EGI_FTCHAR_MAP *chmap=calloc(1, sizeof(EGI_FTCHAR_MAP));
if(chmap==NULL) {
printf("%s: Fail to calloc chmap!\n",__func__);
return NULL;
}
/* 1. Init charmap mutex */
if(pthread_mutex_init(&chmap->mutex,NULL) != 0)
{
printf("%s: fail to initiate charmap mutex.\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
/* 2. Allocate txtbuff */
chmap->txtbuff=calloc(1,sizeof(typeof(*chmap->txtbuff))*(txtsize+1) );
if( chmap->txtbuff == NULL) {
printf("%s: Fail to calloc chmap txtbuff!\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
chmap->txtsize=txtsize+1; /* +1 EOF */
chmap->pref=chmap->txtbuff; /* Init. pref pointing to txtbuff */
/* TEST */
printf(" --- TEST ---: sizeof(typeof(*chmap->txtdlinePos))=%d\n", sizeof(typeof(*chmap->txtdlinePos)) );
/* 3. To allocate txtdlinePos */
chmap->txtdlines=1024; /* Initial value, Min>2, see mem_grow codes in FTcharmap_uft8strings_writeFB().*/
chmap->txtdlinePos=calloc(1, sizeof(typeof(*chmap->txtdlinePos))*(chmap->txtdlines) );
if( chmap->txtdlinePos == NULL) {
printf("%s: Fail to calloc chmap txtdlinePos!\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
/* 4. To create charColorMap */
if( charColorMap_ON ) {
chmap->charColorMap=egi_colorBandMap_create(fontcolor, COLORMAP_BANDS_GROW_SIZE);
if(chmap->charColorMap==NULL) {
printf("%s: Fail to create colorBandMap for chars!\n", __func__);
FTcharmap_free(&chmap);
return NULL;
}
}
/* 5. To create hlmarkColorMap for highlight mark. If no bkgcolor, set mark color as BLACK! */
if( hlmarkColorMap_ON ) {
chmap->hlmarkColorMap=egi_colorBandMap_create(bkgcolor>=0?bkgcolor:WEGI_COLOR_BLACK, COLORMAP_BANDS_GROW_SIZE);
if(chmap->hlmarkColorMap==NULL) {
printf("%s: Fail to create colorBandMap for highlight mark!\n", __func__);
FTcharmap_free(&chmap);
return NULL;
}
}
/* 6. To allocate maplinePos */
chmap->maplinePos=calloc(1,sizeof(typeof(*chmap->maplinePos))*maplines );
if( chmap->maplinePos == NULL) {
printf("%s: Fail to calloc chmap maplinePos!\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
chmap->maplines=maplines;
chmap->mappixpl=mappixpl;
chmap->maplndis=maplndis;
/* 7. To allocate just once!???? to be big enough */
chmap->charX=calloc(1, sizeof(typeof(*chmap->charX))*(mapsize+1) );
chmap->charY=calloc(1, sizeof(typeof(*chmap->charY))*(mapsize+1) );
chmap->charPos=calloc(1, sizeof(typeof(*chmap->charPos))*(mapsize+1) );
if(chmap->charX==NULL || chmap->charY==NULL || chmap->charPos==NULL ) {
printf("%s: Fail to calloc chmap members!\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
/* 8. Set default color/alpha */
chmap->bkgcolor=bkgcolor;
chmap->precolor=-1; /* <0 to make it invalid. it will prevail fontcolor. */
chmap->fontcolor=fontcolor; /* charColorMap!=NULL will prevail. */
chmap->markcolor=WEGI_COLOR_YELLOW; /* Can be ajust aft. init */
chmap->markalpha=50;
/* 9. Assign other params */
chmap->mapx0=x0;
chmap->mapy0=y0;
chmap->width=width; /* Include margin area */
chmap->height=height;
chmap->offx=offx; /* offx/offy is to adjust margin. */
chmap->offy=offy;
chmap->mapsize=mapsize;
/* 10. Assign font typeface */
chmap->face=face;
chmap->fw=fw;
chmap->fh=fh;
/* 11. Set default draw_cursor function */
chmap->draw_cursor=FTcharmap_draw_cursor;
/* 12. Set blink timer */
if(gettimeofday(&chmap->tm_blink, NULL)!=0) {
printf("%s: Fail to set chmap->tm_blink!\n",__func__);
FTcharmap_free(&chmap);
return NULL;
}
return chmap;
}
/*------------------------------------------------------------
Set selection mark color/alpha
@chmap: An EGI_FTCHAR_MAP struct
@colro, alpha: color and alpha value for selection mark.
-------------------------------------------------------------*/
void FTcharmap_set_markcolor(EGI_FTCHAR_MAP *chmap, EGI_16BIT_COLOR color, EGI_8BIT_ALPHA alpha)
{
if(chmap==NULL)
return;
chmap->markcolor=color;
chmap->markalpha=alpha;
}
/*--------------------------------------------------------------
Grow/realloc chmap->txtbuff more_size units.
Return:
0 Ok
<0 Fails.
---------------------------------------------------------------*/
inline int FTcharmap_memGrow_txtbuff(EGI_FTCHAR_MAP *chmap, size_t more_size)
{
int off;
if( chmap==NULL )
return -1;
/* Save current offset of pref */
off=chmap->pref-chmap->txtbuff;
/* Grow and init. to 0 in egi_mem_grow() */
if( egi_mem_grow( (void **)&chmap->txtbuff,
sizeof(typeof(*chmap->txtbuff))*chmap->txtsize, sizeof(typeof(*chmap->txtbuff))*more_size ) !=0 )
return -2;
else {
chmap->txtsize += more_size; /* size in units */
/* --- Critical ---
* Since chmap->pref is a pointer to chmap->txtbuff, so as chmap->txtbuff changes, It MUST adjust accordingly!
*/
chmap->pref = chmap->txtbuff+off;
}
return 0;
}
/*--------------------------------------------------------
Grow/realloc charmap-txtdlinePos[] more_size units.
Return:
0 Ok
<0 Fails.
----------------------------------------------------------*/
inline int FTcharmap_memGrow_txtdlinePos(EGI_FTCHAR_MAP *chmap, size_t more_size)
{
if( chmap==NULL )
return -1;
/* Grow chmap->txtdlinePos[], initial reset to 0 in egi_mem_grow() */
if( egi_mem_grow( (void **)&chmap->txtdlinePos,
sizeof(typeof(*chmap->txtdlinePos))*chmap->txtdlines, sizeof(typeof(*chmap->txtdlinePos))*more_size ) !=0 )
return -2;
else
chmap->txtdlines += more_size; //TXTDLINES_GROW_SIZE; /* size in units */
return 0;
}
/*------------------------------------------------------------------
Grow/realloc charmap members: charX[], charY[], charPos[]
more_size units.
Return:
0 Ok
<0 Fails.
------------------------------------------------------------------*/
inline int FTcharmap_memGrow_mapsize(EGI_FTCHAR_MAP *chmap, size_t more_size)
{
if( chmap==NULL )
return -1;
/* Grow and init. to 0 in egi_mem_grow() */
if( egi_mem_grow( (void **)&chmap->charX,
sizeof(typeof(*chmap->charX))*chmap->mapsize, sizeof(typeof(*chmap->charX))*more_size ) !=0 )
return -2;
if( egi_mem_grow( (void **)&chmap->charY,
sizeof(typeof(*chmap->charY))*chmap->mapsize, sizeof(typeof(*chmap->charY))*more_size ) !=0 )
return -3;
if( egi_mem_grow( (void **)&chmap->charPos,
sizeof(typeof(*chmap->charPos))*chmap->mapsize, sizeof(typeof(*chmap->charPos))*more_size ) !=0 )
return -4;
/* Adjust mapsize */
chmap->mapsize += more_size; /* size in units */
return 0;
}
/*-------------------------------------------------------------------------------
Load content of a file into chmap->txtbuff, and original data in txtbuff will be
lost. if the file dose NOT exist, then the file will be created(for future save
operation).
chmap->txtbuff will be realloced to (fsize+txtsize+1) bytes before copying file
data to chmap->txtbuff.
If create a new file, then chmap->txtbuff will be realloced to (txtsize+1) bytes.
@fpath: file path
@chmap: An EGI_FTCHAR_MAP
@fullmap: True: charmap to the end.
False: charmap only the first page.
XXX@txtsize: If new file, the txtsize is chmap->txtbuff->txtsize=txtsize+1.
Else chmap->txtbuff->txtsize=fsize+txtsize+1
(txtsize is as appendable size...OK chmap->txtbuff will atuo_grow)
Return:
0 Succeed
<0 Fail
-------------------------------------------------------------------------------*/
int FTcharmap_load_file(const char *fpath, EGI_FTCHAR_MAP *chmap, bool fullmap)
{
int fd;
int fsize;
struct stat sb;
char * fp=NULL;
size_t txtsize=1024; /* As appendable size */
/* Check input */
if(chmap==NULL)
return -1;
/* 1. Mmap input file */
fd=open(fpath, O_CREAT|O_RDWR|O_CLOEXEC, S_IRWXU|S_IRWXG );
if(fd<0) {
printf("%s: Fail to open input file '%s'. ERR:%s\n", __func__, fpath, strerror(errno));
return -1;
}
/* 2. Obtain file stat */
if( fstat(fd,&sb)<0 ) {
printf("%s: Fail call fstat for file '%s'. ERR:%s\n", __func__, fpath, strerror(errno));
if( close(fd) !=0 )
printf("%s: Fail to close file '%s'. ERR:%s!\n",__func__, fpath, strerror(errno));
return -2;
}
fsize=sb.st_size;
/* 3. If not new file: mmap txt file */
if(fsize >0) {
fp=mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
if(fp==MAP_FAILED) {
printf("%s: Fail to mmap file '%s'. ERR:%s\n", __func__, fpath, strerror(errno));
if( close(fd) !=0 )
printf("%s: Fail to close file '%s'. ERR:%s!\n",__func__, fpath, strerror(errno));
return -3;
}
}
/* 4. Free and realloc chmap->txtbuff */
free(chmap->txtbuff); chmap->txtbuff=NULL;
chmap->txtbuff=calloc(1,sizeof(typeof(*chmap->txtbuff))*(fsize+txtsize+1) ); /* +1 EOF */
if( chmap->txtbuff == NULL) {
printf("%s: Fail to calloc chmap txtbuff!\n",__func__);
munmap(fp,fsize);
close(fd);
return -3;
}
chmap->txtsize=fsize+txtsize+1; /* txtsize is appendable size */
chmap->pref=chmap->txtbuff; /* Init. pref pointing to txtbuff */
/* 5. If not new file: Copy file content to txtbfuff */
if(fsize>0)
memcpy(chmap->txtbuff, fp, fsize);
/* 6. Set txtlen */
chmap->txtlen=strlen((char *)chmap->txtbuff);
EGI_PDEBUG(DBG_CHARMAP,"Copy from '%s' chmap->txtlen=%d bytes, totally %d characters.\n", fpath, chmap->txtlen,
cstr_strcount_uft8((const unsigned char *)chmap->txtbuff) );
/* 7. Init charmap PRE_1-6, and charmap first page. */
chmap->txtdlncount=0;
chmap->pref=chmap->txtbuff;
chmap->pchoff=chmap->pchoff2=0;
chmap->fix_cursor=false;
chmap->follow_cursor=false;
chmap->request=1;
/* charmap first page */
FTcharmap_uft8strings_writeFB(NULL, chmap, //chmap->fw, chmap->fh, /* FBdev, charmap, fw, fh */
-1, 255, /* transcolor, opaque */
NULL, NULL, NULL, NULL); /* int *cnt, int *lnleft, int* penx, int* peny */
/* 7A. Get charmap->txtlntotlal */
chmap->txtdlntotal=chmap->txtdlncount+chmap->maplncount;
egi_dpstd("----- chmap->txtdlntotal=%d \n", chmap->txtdlntotal);
if(fullmap) {
/* 8. Charmap to file end, to get txtlntotal */
while( chmap->pref[chmap->charPos[chmap->chcount-1]] != '\0' )
{
/* Charmap next page */
FTcharmap_page_down(chmap);
/* Update charmap data */
FTcharmap_uft8strings_writeFB(NULL, chmap, //chmap->fw, chmap->fh, /* FBdev, charmap, fw, fh */
-1, 255, /* transcolor, opaque */
NULL, NULL, NULL, NULL); /* int *cnt, int *lnleft, int* penx, int* peny */
}
/* 8A. Get charmap->txtlntotlal */
chmap->txtdlntotal=chmap->txtdlncount+chmap->maplncount;
egi_dpstd("----- chmap->txtdlntotal=%d \n", chmap->txtdlntotal);
/* 9. Go to head page */
FTcharmap_goto_firstDline(chmap);
/* Update charmap data */
FTcharmap_uft8strings_writeFB(NULL, chmap, //chmap->fw, chmap->fh, /* FBdev, charmap, fw, fh */
-1, 255, /* transcolor, opaque */
NULL, NULL, NULL, NULL); /* int *cnt, int *lnleft, int* penx, int* peny */
}
/* 10. munmap file */
if( fsize>0 && munmap(fp,fsize) !=0 )
printf("%s: Fail to munmap file '%s'. ERR:%s!\n",__func__, fpath, strerror(errno));
if( close(fd) !=0 )
printf("%s: Fail to close file '%s'. ERR:%s!\n",__func__, fpath, strerror(errno));
return 0;
}
/*-------------------------------------------------------------
Save chmap->txtbuff to a file.
@fpath: file path
@chmap: An EGI_FTCHAR_MAP
Return:
0 Succeed
<0 Fail
---------------------------------------------------------------*/
int FTcharmap_save_file(const char *fpath, EGI_FTCHAR_MAP *chmap)
{
FILE *fil;
int ret=0;
int nwrite=0;
/* Check input */
if(chmap==NULL || chmap->txtsize<1 )
return -1;
fil=fopen(fpath, "w");
if(fil==NULL) {
printf("%s: Fail to open file '%s' for write. ERR:%s\n", __func__, fpath, strerror(errno));
return -2;
}
/* Get mutex lock -----------> */
if(pthread_mutex_lock(&chmap->mutex) !=0){
printf("%s: Fail to lock charmap mutex!", __func__);
return -3;
}
/* Write txtbuff to fil */
/* Min LEN. In case it's a new empty file. */
if(chmap->txtlen==0) {
chmap->txtbuff[0]='\n';
chmap->txtlen=1;
}
#if 0 /* NOPE!!! This will increase chmap->txtlen, instead to put EOF after fwriting txtbuff !!! */
if(chmap->txtbuff[chmap->txtlen-1]!='\n') { /* txtbuff[txtlen] is ALWAYS '\0', as txtbuff EOF */
chmap->txtbuff[chmap->txtlen]='\n'; /* UNIX File EOF standard */
chmap->txtlen++;
}
#endif
nwrite=fwrite(chmap->txtbuff, 1, chmap->txtlen, fil);
if(nwrite < chmap->txtlen) {
if(ferror(fil))
printf("%s: Fail to write to '%s'.\n", __func__, fpath);
else
printf("%s: WARNING! fwrite %d bytes of total %d bytes.\n", __func__, nwrite, chmap->txtlen);
ret=-4;
}
EGI_PDEBUG(DBG_CHARMAP,"%d bytes written.\n", nwrite);
/* End_Of_File: UNIX/POSIX standard */
if(chmap->txtbuff[chmap->txtlen-1]!='\n') /* txtbuff[txtlen] is ALWAYS '\0', as txtbuff EOF */
fwrite("\n", 1, 1, fil); /* No error check here!!! */
/* Close fil */
if( fclose(fil) !=0 ) {
printf("%s: Fail to close file '%s'. ERR:%s\n", __func__, fpath, strerror(errno));
ret=-5;
}
/* <-------- Put mutex lock */
pthread_mutex_unlock(&chmap->mutex);
return ret;
}
/*----------------------------------------------
Free an EGI_FTCHAR_MAP.
-----------------------------------------------*/
void FTcharmap_free(EGI_FTCHAR_MAP **chmap)
{
if( chmap==NULL || *chmap==NULL)
return;
/* Hope there is no other user.. */
/* Get mutex lock -----------> */
if(pthread_mutex_lock(&((*chmap)->mutex)) !=0 )
printf("%s: Fail to lock charmap mutex!\n",__func__);
/* free(ptr): If ptr is NULL, no operation is performed */
free( (*chmap)->txtbuff );
free( (*chmap)->txtdlinePos );
free( (*chmap)->maplinePos );
free( (*chmap)->charX );
free( (*chmap)->charY );
free( (*chmap)->charPos );
/* Free colorband MAP */
egi_colorBandMap_free(&(*chmap)->charColorMap);
egi_colorBandMap_free(&(*chmap)->hlmarkColorMap);
/* ??? necesssary ??? */
/* <-------- Put mutex lock */
pthread_mutex_unlock(&((*chmap)->mutex));
/* Destroy thread mutex lock for page resource access */
if(pthread_mutex_destroy(&((*chmap)->mutex)) !=0 )
printf("%s: Fail to destroy charmap mutex!\n",__func__);
free( *chmap );
*chmap=NULL;
}
/*--------- NEGATIVE: use charmap->maplinePos[] instead ---------
Set chmap->pref to position of the begin char of the next
displayed line.
@chmap: pointer to an EGI_FTCHAR_MAP
Return:
0 OK, chmap->pref changed.
<0 Fails, chmap->pref unchanged.
1 There's only one disp line. chmpa->pref unchagned.
--------------------------------------------------------*/
int FTcharmap_set_pref_nextDispLine(EGI_FTCHAR_MAP *chmap)
{
int i;
if( chmap==NULL || chmap->txtbuff==NULL )
return -1;
/* If only 1 displine */
if( chmap->charY[0] == chmap->charY[chmap->chcount-1] )
return 1;
for(i=0; i < chmap->chcount; i++) {
if( chmap->charY[i] > chmap->charY[0] ) /* Find the first char NOT in the first displine. */
break;
}
/* Reset pref */
chmap->pref=chmap->pref+chmap->charPos[i];
return 0;
}
/*-----------------------------------------------------------------------------------------
Charmap a string of charaters with UFT-8 encoding.
It's a process of arranging characters with specified size and font to a certain window area.
It can stop when finish filling up the area with chars, or it can continue to lay out chars
followed in the txtbuff until to the end, so that each arranged line will be remembered by its
offset in the txtbuff, and each chars in the CURRENT charmap has its coordinates value.
If you edite contents of the txtbuff, it should be charmapped again to update its position/coordinates.
TODO: 1. Alphabetic words are treated letter by letter, and they may be separated at the end of
a line, so it looks not good.
2. Apply character functions in <ctype.h> to rule out chars, with consideration of locale setting?
3. !!! WARNING !!!
Any ASCII codes that may be preseted in the txtbuff MUST all be charmapped into charX/Y/Pos[].
MUST NOT skip any char,event it has zero width, OR it will cause charmap data error in further operation,
Example: when shift chmap->pchoff, it MAY point to a char that has NO charmap data at all!
example: TAB(9) and CR(13)!
see 'EXCLUDE WARNING' in the function.
4. To investigate:
If you try to insert a samll size char( like '.') in the beginning of a dline, and it happens
that there is just enough space left in previous dline, then after charmapping the char
will be displayed at the end of the last dline! ---- Any further problems ???
@fbdev: FB device
If NULL: Ignore to redraw bkgcolor and curosr!
@chmap: pointer to an EGI_FTCHAR_MAP
@face: A face object in FreeType2 library.
@fh,fw: Height and width of the wchar.
//@pstr: pointer to a string with UTF-8 encoding.
@xleft: Pixel space left in FB X direction (horizontal writing)
Xleft will be subtrated by slot->advance.x first anyway, If xleft<0 then, it aborts.
//@pixpl: pixels per line.
//@lines: number of lines available.
//@gap: space between two lines, in pixel. may be minus.
@x0,y0: Left top coordinate of the character bitmap,relative to FB coord system.
@transpcolor: >=0 transparent pixel will not be written to FB, so backcolor is shown there.
<0 no transparent pixel
Replaced by charColorMap and chmap->fontcolor
//@fontcolor: font color(symbol color for a symbol)
// >= 0, use given font color.
// <0 , use default color in img data.
use following COLOR:
#define SYM_NOSUB_COLOR -1 --- no substitute color defined for a symbol or font
#define SYM_NOTRANSP_COLOR -1 --- no transparent color defined for a symbol or font
@opaque: >=0 set aplha value (0-255) for all pixels, and alpha data in sympage
will be ignored
<0 Use symbol alpha data, or none.
0 100% back ground color/transparent
255 100% front color
@cnt: Total recognizable UFT-8 characters written.
@lnleft: Lines left unwritten.
@penx: The last pen position X
@peny: The last pen position Y.
Note: Whatever pstr is finished or not
penx,peny will be reset to starting position of next line!!!
return:
>=0 bytes write to FB
<0 fails
------------------------------------------------------------------------------------------------*/
int FTcharmap_uft8strings_writeFB( FBDEV *fb_dev, EGI_FTCHAR_MAP *chmap,
//int fw, int fh,
int transpcolor, int opaque,
int *cnt, int *lnleft, int* penx, int* peny )
{
int i,k;
int size;
int count; /* number of character written to FB*/
int px,py; /* bitmap insertion origin(BBOX left top), relative to FB */
const unsigned char *pstr=NULL; /* == chmap->pref */
const unsigned char *p=NULL; /* variable pointer, to tranverse each char in pstr[] */
int xleft; /* available pixels remainded in current dline */
int ln; /* dlines used */
unsigned int lines; /* Total dlines, =chmap->maplines */
unsigned int pixpl; /* pixels per dline, =chmap->mappixpl */
unsigned off=0;
int x0; /* charmap left top point, exclude margin */
int y0;
int pchx=0; /* if(chmap->fix_cursor), then save charXY[pch] */
int pchy=0;
int charlen;
int TabWidth;
struct timeval tm_now;
wchar_t wcstr[1]; /* BETTER AS: wchart_t wcode */
FT_Fixed advance; /* typedef signed long FT_Fixed; to store 16.16 fixed-point values */
FT_Error error;
int sdw; /* Self_defined width for some unicodes */
int fontcolor;
int hlmarkcolor;
FT_Face face;
int fw,fh;
/* 1. Check input charmap */
if(chmap==NULL || chmap->txtbuff==NULL) {
printf("%s: Input chmap or its data is invalid!\n", __func__);
return -2;
}
// if( pixpl==0 || lines==0 ) return -1; /* move to just after initiate vars */
/* 2. Get mutex lock -----------> */
if(pthread_mutex_lock(&chmap->mutex) !=0){
printf("%s: Fail to lock charmap mutex!", __func__);
return -3;
}
/* 3. Check chmap->request : NOT yet applied for all functions. */
#if 0
if(!chmap->request) {
/* <-------- Put mutex lock */
pthread_mutex_unlock(&chmap->mutex);
return 1;
}
#endif
/* 4. Check input font face */
if(chmap->face==NULL) {
printf("%s: Input FT_Face is NULL!\n",__func__);
/* <-------- Put mutex lock */
pthread_mutex_unlock(&chmap->mutex);
return -1;
}
/* Get typeface and font size */
face=chmap->face;
fw=chmap->fw; fh=chmap->fh;
/* 5. if(chmap->fix_cursor), save charXY[pch] to */
if(chmap->fix_cursor) {
pchx=chmap->charX[chmap->pch];
pchy=chmap->charY[chmap->pch];
}
/* 6. Init charmap canvas params */
x0=chmap->mapx0+chmap->offx;
y0=chmap->mapy0+chmap->offy;
lines=chmap->maplines;
pixpl=chmap->mappixpl;
//gap=chmap->maplngap;
/* 7. Get TabWidth */
TabWidth=FTsymbol_cooked_charWidth(9,fw);
START_CHARMAP: /* If follow_cursor, loopback here */
/* 8. Draw/clear blank paper/canvas, not only when follow_cursor */
if( chmap->bkgcolor >= 0 && fb_dev != NULL ) {
fbset_color2(fb_dev, chmap->bkgcolor);
/* Include margin area, offx/offy is to adjust margin. */
draw_filled_rect(fb_dev, chmap->mapx0, chmap->mapy0, chmap->mapx0+chmap->width -1, chmap->mapy0+chmap->height -1 );
#if 0 /* Draw grids */
fbset_color(WEGI_COLOR_GRAYB);
for(k=0; k<=chmap->maplines; k++)
draw_line(fb_dev, x0, y0+k*chmap->maplndis-1, x0+chmap->mappixpl-1, y0+k*chmap->maplndis-1);
#endif
}
/* 9. Init pstr and p */
pstr=chmap->pref; /* However, pref may be NULL */
p=pstr;
/* 10. Init tmp. vars */
px=x0;
py=y0;
xleft=pixpl;
count=0;
ln=0; /* Line index from 0 */
/* 11. Check input pixpl and lines, */
if(pixpl==0 || lines==0)
return -4; //goto CHARMAP_END;
/* 12. Must reset temp charmap vars and clear old data */
memset(chmap->maplinePos, 0, chmap->maplines*sizeof(typeof(*chmap->maplinePos)) );
memset(chmap->charPos, 0, chmap->mapsize*sizeof(typeof(*chmap->charPos)));
memset(chmap->charX, 0, chmap->mapsize*sizeof(typeof(*chmap->charX)) );
memset(chmap->charY, 0, chmap->mapsize*sizeof(typeof(*chmap->charY)) );
/* 13. Reset/Init. charmap data, first maplinePos */
chmap->chcount=0;
chmap->maplncount=0;
chmap->maplinePos[chmap->maplncount]=0; /* relative to chmap->pref */
chmap->maplncount++; /* MAPLNCOUNT_PRE_INCREMENT */
/* 14. Init.charmap global data */
/* NOTE: !!! chmap->txtdlncount=xxx, MUST preset before calling this function */
chmap->txtdlinePos[chmap->txtdlncount++]=chmap->pref-chmap->txtbuff;
/* 15. Charmap chars till lines are used up. */
while( *p ) {
/* W.1 Check txtdlncount and mem_grow txtdlinePos[] if necessary. */
if(chmap->txtdlncount >= chmap->txtdlines-2) { /* ==chmap->txtdlines-1-1, -1 for allowance. */
EGI_PDEBUG(DBG_CHARMAP,"txtdlncount=%d, txtdlines=%d, maplncount=%d, mem_grow chmap->txtdlinePos...\n",
chmap->txtdlncount, chmap->txtdlines,chmap->maplncount);
if( FTcharmap_memGrow_txtdlinePos(chmap, TXTDLINES_GROW_SIZE ) !=0 ) {
chmap->errbits |= CHMAPERR_TXTDLINES_LIMIT;
printf("%s: Fail to mem_grow chmap->txtdlinePos from %d to %d units more!\n",
__func__, chmap->txtdlines, TXTDLINES_GROW_SIZE);
/* <-------- Put mutex lock */
pthread_mutex_unlock(&chmap->mutex);
return -5;
}
}
/* W.2 check whether lines are used up */
if( lines < ln+1) { /* ln index from 0, lines size_t */
//printf("%s: ln=%d, Lines not enough! finish only %d chars.\n", __func__, ln, count);
goto CHARMAP_END;
}
/* W.3 convert one character to unicode, return size of utf-8 code */
size=char_uft8_to_unicode(p, wcstr);
/* W.4 Get fontcolor from colormap here! */
if(chmap->charColorMap!=NULL)
fontcolor=egi_colorBandMap_pickColor(chmap->charColorMap, p-chmap->txtbuff);
else
fontcolor=chmap->fontcolor;
/* W.5 Get highlight mark color from colormap here! */
if(chmap->hlmarkColorMap!=NULL) {
hlmarkcolor=egi_colorBandMap_pickColor(chmap->hlmarkColorMap, p-chmap->txtbuff);
//printf("hlmarkcolor=%u, RGB: 0x%06X, getY=%u \n",hlmarkcolor, COLOR_16TO24BITS(hlmarkcolor), egi_color_getY(hlmarkcolor));
/* If highligh color too dark, make fontcolor WHITE */
if( egi_color_getY(hlmarkcolor) < 100 )
fontcolor=WEGI_COLOR_WHITE;
}
#if 0 /* ----TEST: print ASCII code */
if(size==1) {
printf("%s: ASCII code: %d \n",__func__,*wcstr);
}
//if( !iswprint(*wcstr) ) ---Need to setlocale(), not supported yet!
// printf("Not printalbe wchar code: 0x%x \n",*wcstr);
#endif /*-----TEST END----*/
/* W.6 NOTE: Shift offset to next wchar, notice that following p is for the next char!!! */
if(size>0) {
p+=size;
count++;
}
else { /* If fails, try to step 1 byte forward to locate next recognizable unicode wchar */
printf("%s: WARNING! char_size < 0!\n",__func__);
p++;
continue;
}
/* W.7 CONTROL ASCII CODE:
* If return to next line
*/
if(*wcstr=='\n') { /* ASCII Newline 10 */
//printf(" ... ASCII code: Next Line ...\n ");
/* W.7.1 Check Size */
if ( chmap->chcount >= chmap->mapsize ) {
EGI_PDEBUG(DBG_CHARMAP,"mem_grow chmap->mapsize charXYPOS from %d to %d units more...\n",
chmap->mapsize, MAPSIZE_GROW_SIZE);
if( FTcharmap_memGrow_mapsize(chmap, MAPSIZE_GROW_SIZE) !=0 ) {
chmap->errbits |= CHMAPERR_MAPSIZE_LIMIT;
printf("%s: Fail to mem_grow chmap->mapsize, charXYPOS from %d to %d units more!\n",
__func__, chmap->mapsize, MAPSIZE_GROW_SIZE);
}
}
/* W.7.2 Fillin CHAR MAP before start a new line */
else { /* ( chmap->chcount < chmap->mapsize ) */
chmap->charX[chmap->chcount]=x0+pixpl-xleft; /* line end */
chmap->charY[chmap->chcount]=py;
chmap->charPos[chmap->chcount]=p-pstr-size; /* deduce size, as p now points to end of '\n' */
chmap->chcount++;
/* Get start postion of next displaying line. */
if(chmap->maplncount < chmap->maplines ) { /* LIMIT_MAPLNCOUNT */
/* set maplinePos[] */
chmap->maplinePos[chmap->maplncount++]=p-pstr; /* keep p+size */
/* set txtdlinePos[] */
if(chmap->txtdlncount < chmap->txtdlines-1 )