-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathegi_FTsymbol.c
More file actions
3168 lines (2643 loc) · 100 KB
/
egi_FTsymbol.c
File metadata and controls
3168 lines (2643 loc) · 100 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
/*------------------------------------------------------------------
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. Unicode emoji characters and sequences: https://www.unicode.org/emoji/charts/emoji-list.html
TODO:
1.Font size class, ratio of fw/fh to be fixed!
NOW: font width(fw) is regarded as font size.
2. FTsymbol_unicode_writeFB(): Check if font data is corrupted! it
MAY return error advanceX/bitmap.row.width ??? Set a limit
to avoid it return a too big/samll value!
3. Remote_font v.s. Local_font
Local: A set of English font
Remote: Various types of fonts
Journal:
2021-01-27:
1. Add FTsymbol_create_fontBuffer() and FTsymbol_free_fontBuffer().
2021-01-28:
1.FTsymbol_unicode_writeFB(), add EGI_FONT_BUFFER to speed up.
2021-05-25:
1. Add: FTsymbol_uft8strings_writeIMG().
2021-05-26:
1. Add member 'lib_mutex' for EGI_FONTS.
2. Apply lib_mutex for following functions:
FTsymbol_load_library()
FTsymbol_release_library()
FTsymbol_unicode_writeFB()
2021-07-16:
1. Add module private member 'enable_SplitWord' and functions of
FTsymbol_enable_SplitWord()
FTsymbol_disable_SplitWord()
FTsymbol_status_SplitWord()
2021-07-17:
1. Add FTsymbol_eword_pixlen()
2. FTsymbol_uft8strings_writeFB(): SplitWord contorl.
2021-07-20:
1. FTsymbol_eword_pixlen(): Count in any SPACEs/TABs.
2021-09-14:
1. Add FTsymbol_uft8strings_writeIMG2(), with rotation angle.
2021-11-26:
1. FTsymbol_uft8strings_writeFB(). If *p gets to the end of the string
with just xleft==0, then NO need for increment of ln/xleft/py.
2022-05-26:
1. Add egi_sysemojis.
2. FTsymbol_load_sysemojis()
3. Modify functions for Emojis font:
FTsymbol_load_library(), FTsymbol_load_sysfonts(), FTsymbol_load_appfonts()
2022-05-27:
1. FTsymbol_unicode_writeFB(): Add case FT_HAS_COLOR.
2022-06-16:
1. FTsymbol_unicode_writeFB(): TEST___ NO mutexlock for EGI_FONT_BUFFER.
2022-06-18:
1. Add FTsymbol_save_fontBuffer()
2. Add FTsymbol_load_fontBuffer()
2022-06-19:
1. EGI_FONT_BUFFER: Add member 'family_name' and 'style_name'.
modify FTsymbol_save_fontBuffer(), FTsymbol_load_fontBuffer()
Midas Zhou
midaszhou@yahoo.com(Not in use since 2022_03_01)
-------------------------------------------------------------------*/
#include "egi_log.h"
#include "egi_FTsymbol.h"
#include "egi_symbol.h"
#include "egi_cstring.h"
#include "egi_utils.h"
#include "egi_debug.h"
#include "sys_list.h" /* container_of() */
#include <arpa/inet.h>
#include <ctype.h>
#include <freetype2/ft2build.h>
#include <freetype2/ftglyph.h>
#include <freetype2/ftadvanc.h>
#include FT_FREETYPE_H
/* <<<<<<<<<<<<<<<<<< FreeType Fonts >>>>>>>>>>>>>>>>>>>>>>*/
EGI_SYMPAGE sympg_ascii={0}; /* default LiberationMono-Regular */
EGI_FONTS egi_sysfonts = {.ftname="sysfonts",};
EGI_FONTS egi_sysemojis = {.ftname="sysemojis",};
EGI_FONTS egi_appfonts = {.ftname="appfonts",};
EGI_FONT_BUFFER *egi_fontbuffer;
/*--------------------------------
Module Prative Parameters
--- Caution! ---
NOT thread safe!
---------------------------------*/
static float factor_TabWidth=3.0; /* TabWidth/Font_Width */
static float factor_SpaceWidth=1.0; /* SpaceWidth/FontWidth */
static bool enable_SplitWord=true; /* Enable a word splited into two lines
* NOW affect FTsymbol_uft8strings_writeFB() ONLY!
*/
/*--------------------------------------
Set TAB and SPACE width facotrs.
---------------------------------------*/
void FTsymbol_set_TabWidth( float factor)
{
factor_TabWidth=factor;
}
void FTsymbol_set_SpaceWidth( float factor)
{
factor_SpaceWidth=factor;
}
/*---------------------------------
Set enable_SplitWord.
----------------------------------*/
void FTsymbol_enable_SplitWord(void)
{
enable_SplitWord=true;
}
void FTsymbol_disable_SplitWord(void)
{
enable_SplitWord=false;
}
bool FTsymbol_status_SplitWord(void)
{
return enable_SplitWord;
}
/*--------------------------------------
Load FreeType2 EGI_FONT egi_sysfonts
for main process, as small as possible.
return:
0 OK
<0 Fails
---------------------------------------*/
int FTsymbol_load_sysfonts(void)
{
int ret=0;
char fpath_regular[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_light[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_bold[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_special[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_emojis[EGI_PATH_MAX+EGI_NAME_MAX];
/* Read egi.conf and get fonts paths */
egi_get_config_value("SYS_FONTS","regular",fpath_regular);
egi_get_config_value("SYS_FONTS","light",fpath_light);
egi_get_config_value("SYS_FONTS","bold",fpath_bold);
egi_get_config_value("SYS_FONTS","special",fpath_special);
egi_get_config_value("SYS_FONTS","emojis",fpath_emojis);
egi_sysfonts.fpath_regular=fpath_regular;
egi_sysfonts.fpath_light=fpath_light;
egi_sysfonts.fpath_bold=fpath_bold;
egi_sysfonts.fpath_special=fpath_special;
egi_sysfonts.fpath_emojis=fpath_emojis;
/* Load FT library with default fonts */
ret=FTsymbol_load_library( &egi_sysfonts );
if(ret) {
//return -1;
// Go on....
EGI_PLOG(LOGLV_ERROR,"%s: Some sysfonts missed or NOT configured!\n", __func__ );
}
return 0;
}
#if 1 ////////////////////////////////////////
/*--------------------------------------
Load FreeType2 EGI_FONT egi_sysemojis
for main process, as small as possible.
return:
0 OK
<0 Fails
---------------------------------------*/
int FTsymbol_load_sysemojis(void)
{
int ret=0;
char fpath_regular[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_light[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_bold[EGI_PATH_MAX+EGI_NAME_MAX];
char fpath_special[EGI_PATH_MAX+EGI_NAME_MAX];
/* Read egi.conf and get fonts paths */
egi_get_config_value("SYS_EMOJIS","regular",fpath_regular);
egi_get_config_value("SYS_EMOJIS","light",fpath_light);
egi_get_config_value("SYS_EMOJIS","bold",fpath_bold);
egi_get_config_value("SYS_EMOJIS","special",fpath_special);
egi_sysemojis.fpath_regular=fpath_regular;
egi_sysemojis.fpath_light=fpath_light;
egi_sysemojis.fpath_bold=fpath_bold;
egi_sysemojis.fpath_special=fpath_special;
/* Load FT library with default fonts */
ret=FTsymbol_load_library( &egi_sysemojis );
if(ret) {
//return -1;
// Go on....
EGI_PLOG(LOGLV_ERROR,"%s: Some sysEmojis missed or NOT configured!\n", __func__ );
}
return 0;
}
#endif ////////////////////////////////////////////
/*--------------------------------------
Load FreeType2 fonts for all APPs.
return:
0 OK
<0 Fails
---------------------------------------*/
int FTsymbol_load_appfonts(void)
{
int ret=0;
char fpath_regular[EGI_PATH_MAX+EGI_NAME_MAX]={0};
char fpath_light[EGI_PATH_MAX+EGI_NAME_MAX]={0};
char fpath_bold[EGI_PATH_MAX+EGI_NAME_MAX]={0};
char fpath_special[EGI_PATH_MAX+EGI_NAME_MAX]={0};
char fpath_emojis[EGI_PATH_MAX+EGI_NAME_MAX];
/* Read egi.conf and get fonts paths */
egi_get_config_value("APP_FONTS","regular",fpath_regular);
egi_get_config_value("APP_FONTS","light",fpath_light);
egi_get_config_value("APP_FONTS","bold",fpath_bold);
egi_get_config_value("APP_FONTS","special",fpath_special);
egi_get_config_value("APP_FONTS","emojis",fpath_emojis);
egi_appfonts.fpath_regular=fpath_regular;
egi_appfonts.fpath_light=fpath_light;
egi_appfonts.fpath_bold=fpath_bold;
egi_appfonts.fpath_special=fpath_special;
egi_appfonts.fpath_emojis=fpath_emojis;
/* Load FT library with default fonts */
ret=FTsymbol_load_library( &egi_appfonts );
if(ret) {
EGI_PLOG(LOGLV_WARN,"%s: Some appfonts missed or not configured!", __func__ );
//return -1;
}
return 0;
}
void FTsymbol_release_allfonts(void)
{
/* release FT fonts libraries */
FTsymbol_release_library(&egi_sysfonts);
FTsymbol_release_library(&egi_appfonts);
}
/*-----------------------------------------
Load all FT type symbol pages.
1. sympg_ascii
return:
0 OK
<0 Fails
-----------------------------------------*/
int FTsymbol_load_allpages(void)
{
char fpath_ascii[EGI_PATH_MAX+EGI_NAME_MAX]={0};
/* read egi.conf and get fonts paths */
if ( egi_get_config_value("FTSYMBOL_PAGE","ascii",fpath_ascii) == 0 &&
/* load ASCII font, bitmap size 18x18 in pixels, each line abt.18x2 pixels in height */
FTsymbol_load_asciis_from_fontfile( &sympg_ascii, fpath_ascii, 18, 18) ==0 )
{
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to load FTsymbol page sympg_ascii!", __func__ );
return 0;
}
else {
EGI_PLOG( LOGLV_CRITICAL,"%s: Fail to load FTsymbol page sympg_ascii!", __func__ );
return -1;
}
}
/* --------------------------------------
Release all TF type symbol pages
----------------------------------------*/
void FTsymbol_release_allpages(void)
{
/* release FreeType2 symbol page */
symbol_release_page(&sympg_ascii);
if(sympg_ascii.symwidth!=NULL) /* !!!Not static */
free(sympg_ascii.symwidth);
}
/*---------------------------------------------------------------
Initialize FT library and load faces.
Note:
1. You shall avoid to load all fonts in main process,
it will slow down fork() process significantly.
2. When a new face is created (FT_New_Face or FT_Open_Face), the
the library looks for a Unicode charmap within the list and
automatically activates it. Or use FT_Select_Charmap to manually
change encoding type.
@symlib: A pointer to EGI_FONTS.
return:
0 OK
!0 Fails, or not all faces are loaded successfully.
----------------------------------------------------------------*/
int FTsymbol_load_library( EGI_FONTS *symlib )
{
FT_Error error;
int ret=0;
/* check input data */
// if( symlib==NULL || symlib->fpath_regular==NULL
// || symlib->fpath_light==NULL
// || symlib->fpath_bold==NULL )
if( symlib == NULL )
{
egi_dpstd("Fail to check integrity of the input FTsymbol_library!\n");
return -1;
}
/* 0. Init lib_mutex mutex */
if(pthread_mutex_init(&symlib->lib_mutex, NULL) != 0)
{
egi_dpstd("Fail to init lib_mutex.\n");
return -1;
}
/* 1. initialize FT library */
error = FT_Init_FreeType( &symlib->library );
if(error) {
EGI_PLOG(LOGLV_ERROR, "%s: An error occured during FreeType library [%s] initialization.",
__func__, symlib->ftname);
return error;
}
/* 2. create face object: Regular */
error = FT_New_Face( symlib->library, symlib->fpath_regular, 0, &symlib->regular );
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: [%s] font file '%s' opens, but its font format is unsupported!",
__func__, symlib->ftname,symlib->fpath_regular);
ret=1;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read [%s] REGULAR font file '%s'.",
__func__, symlib->ftname, symlib->fpath_regular);
ret=2;
}
else {
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read [%s] REGULAR font file '%s', type face has %s kerning value.",
__func__, symlib->ftname, symlib->fpath_regular, FT_HAS_KERNING(symlib->regular) ? " ":"NO" );
}
/* 3. Create face object: Light */
error = FT_New_Face( symlib->library, symlib->fpath_light, 0, &symlib->light );
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: [%s] font file '%s' opens, but its font format is unsupported!",
__func__, symlib->ftname, symlib->fpath_light);
ret=3;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read [%s] LIGHT font file '%s'.",
__func__, symlib->ftname, symlib->fpath_light);
ret=4;
}
else {
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read [%s] LIGHT font file '%s', type face has %s kerning value.",
__func__, symlib->ftname, symlib->fpath_light, FT_HAS_KERNING(symlib->light) ? " ":"NO" );
}
/* 4. Create face object: Bold */
error = FT_New_Face( symlib->library, symlib->fpath_bold, 0, &symlib->bold );
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: [%s] font file '%s' opens, but its font format is unsupported!",
__func__, symlib->ftname, symlib->fpath_bold);
ret=5;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read [%s] BOLD font file '%s'.",
__func__, symlib->ftname,symlib->fpath_bold);
ret=6;
}
else {
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read [%s] BOLD font file '%s', type face has %s kerning value.",
__func__, symlib->ftname,symlib->fpath_bold, FT_HAS_KERNING(symlib->bold) ? " ":"NO");
}
/* 5. Create face object: Special */
error = FT_New_Face( symlib->library, symlib->fpath_special, 0, &symlib->special );
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: [%s] font file '%s' opens, but its font format is unsupported!",
__func__, symlib->ftname, symlib->fpath_special);
ret=7;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read [%s] SPECIAL font file '%s'.\n",
__func__, symlib->ftname, symlib->fpath_special);
ret=8;
}
else {
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read [%s] SPECIAL font file '%s', type face has %s kerning value.",
__func__, symlib->ftname, symlib->fpath_special, FT_HAS_KERNING(symlib->special) ? " ":"NO");
}
/* 6. Create face object: Emojis */
error = FT_New_Face( symlib->library, symlib->fpath_emojis, 0, &symlib->emojis );
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: [%s] font file '%s' opens, but its font format is unsupported!",
__func__, symlib->ftname, symlib->fpath_emojis);
ret=9;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read [%s] EMOJI font file '%s'.\n",
__func__, symlib->ftname, symlib->fpath_emojis);
ret=10;
}
else {
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read [%s] EMOJI font file '%s', type face has %s kerning value.",
__func__, symlib->ftname, symlib->fpath_emojis, FT_HAS_KERNING(symlib->emojis) ? " ":"NO");
}
return ret;
//FT_FAIL:
FT_Done_Face ( symlib->regular );
FT_Done_Face ( symlib->light );
FT_Done_Face ( symlib->bold );
FT_Done_Face ( symlib->special );
FT_Done_Face ( symlib->emojis );
FT_Done_FreeType( symlib->library );
return error;
}
/*------------------------------------------------------------------
Create a new FT_Face in the FT_library of given EGI_FONTS.
@symlib: An EGI_FONTS with a FT_Library to hold the FT Face.
@ftpath: Font file path for the new FT Face.
Return:
FT_Face OK
NULL Fails
--------------------------------------------------------------------*/
FT_Face FTsymbol_create_newFace( EGI_FONTS *symlib, const char *ftpath)
{
FT_Error error;
FT_Face face;
/* Initialize library if NULL */
if(symlib->library==NULL) {
printf("%s: Input EGI_FONTS has an empty FT_library! try to initialize it...\n",__func__);
error = FT_Init_FreeType( &symlib->library );
if(error) {
EGI_PLOG(LOGLV_ERROR, "%s: An error occured during FreeType library [%s] initialization.",
__func__, symlib->ftname);
return NULL;
}
}
/* Create a new face according to the input file path */
error = FT_New_Face( symlib->library, ftpath, 0, &face);
if(error==FT_Err_Unknown_File_Format) {
EGI_PLOG(LOGLV_WARN,"%s: font file '%s' opens, but its font format is unsupported!",
__func__, ftpath);
return NULL;
}
else if ( error ) {
EGI_PLOG(LOGLV_WARN,"%s: Fail to open or read font file '%s'.", __func__, ftpath);
return NULL;
}
EGI_PLOG(LOGLV_CRITICAL,"%s: Succeed to open and read font file '%s'.", __func__, ftpath);
return face;
}
/*--------------------------------------------------
Rlease FT library.
---------------------------------------------------*/
void FTsymbol_release_library( EGI_FONTS *symlib )
{
if(symlib==NULL || symlib->library==NULL)
return;
/* 1. Destroy lib_mutex */
/* Hope there is no other user */
if(pthread_mutex_lock(&symlib->lib_mutex) !=0 )
EGI_PLOG(LOGLV_TEST,"%s:Fail to lib_mutex!\n",__func__);
if( pthread_mutex_unlock(&symlib->lib_mutex) != 0)
EGI_PLOG(LOGLV_TEST,"%s:Fail to unlock lib_mutex!\n",__func__);
/* Destroy thread mutex lock for page resource access */
if(pthread_mutex_destroy(&symlib->lib_mutex) !=0 )
EGI_PLOG(LOGLV_TEST,"%s:Fail to destroy lib_mutex!.\n",__func__);
/* 2. Free FT faces and library */
FT_Done_Face ( symlib->regular );
FT_Done_Face ( symlib->light );
FT_Done_Face ( symlib->bold );
FT_Done_Face ( symlib->special );
FT_Done_FreeType( symlib->library );
}
/*--------------------------------------------------------------------------------------------------------
(Note: FreeType 2 is licensed under FTL/GPLv3 or GPLv2 ).
1. This program is ONLY for horizontal text layout !!!
2. To get the MAX boudary box heigh: symheight, which is capable of containing each character in a font set.
(symheight as for EGI_SYMPAGE)
1.1 base_uppmost + base_lowmost +1 ---> symheight (same height for all chars)
base_uppmost = MAX( slot->bitmap_top ) ( pen.y set to 0 as for baseline )
base_lowmost = MAX( slot->bitmap.rows - slot->bitmap_top ) ( pen.y set to 0 as for baseline )
!!! Note: Though W*H in pixels as in FT_Set_Pixel_Sizes(face,W,H) is capable to hold each
char images, but it is NOT aligned with the common baseline !!!
1.2 MAX(slot->advance.x/64, slot->bitmap.width) ---> symwidth ( for each charachter )
Each ascii symbol has different value of its FT bitmap.width.
3. 0-31 ASCII control chars, 32-126 ASCII printable symbols.
4. For ASCII charaters, then height defined in FT_Set_Pixel_Sizes() or FT_Set_Char_Size() is NOT the true
hight of a character bitmap, it is a norminal value including upper and/or low bearing gaps, depending
on differenct font glyphs.
5. Following parameters all affect the final position of a character.
5.1 slot->advance.x ,.y: Defines the current cursor/pen shift vector after loading/rendering a character.
!!! This will affect the position of the next character. !!!
5.2 HBearX, HBearY: position adjust for a character, relative to the current cursor position(local
origin).
5.3 slot->bitmap_left, slot->bitmap_top: Defines the left top point coordinate of a character bitmap,
relative to the global drawing system origin.
LiberationSans-Regular.ttf
Midas Zhou
midaszhou@yahoo.com(Not in use since 2022_03_01)
----------------------------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------------------------
Load a ASCII symbol_page struct from a font file by calling FreeType2 libs.
Note:
0. This is for pure western ASCII alphabets font set.
1. Load ASCII characters from 0 - 127, bitmap data of all nonprintable ASCII symbols( 0-31,127)
will not be loaded to symfont_page, and their symwidths are set to 0 accordingly.
2. Take default face_index=0 when call FT_New_Face(), you can modify it if
the font file contains more than one glphy face.
3. Inclination angle for each single character is set to be 0. If you want an oblique effect (for
single character ), just assign a certain value to 'deg'.
4. !!! symfont_page->symwidth MUST be freed by calling free() separately,for symwidth is statically
allocated in most case, and symbol_release_page() will NOT free it.
TODO: Apply character functions in <ctype.h> to rule out chars, with consideration of locale setting?
@symfont_page: pointer to a font symbol_page.
@font_path: font file path.
@Wp, Hp: in pixels, as for FT_Set_Pixel_Sizes(face, Wp, Hp )
!!!! NOTE: Hp is nominal font height and NOT symheight of symbole_page,
the later one shall be set to be the same for all symbols in the page.
Return:
0 ok
<0 fails
---------------------------------------------------------------------------------------------*/
int FTsymbol_load_asciis_from_fontfile( EGI_SYMPAGE *symfont_page, const char *font_path,
int Wp, int Hp )
{
FT_Error error;
FT_Library library;
FT_Face face; /* NOTE: typedef struct FT_FaceRec_* FT_Face */
FT_GlyphSlot slot;
FT_Matrix matrix; /* transformation matrix */
FT_Vector origin;
int bbox_W; // bbox_H; /* Width and Height of the boundary box */
int base_uppmost; /* in pixels, from baseline to the uppermost scanline */
int base_lowmost; /* in pixels, from baseline to the lowermost scanline */
int symheight; /* in pixels, =base_uppmost+base_lowmost+1, symbol height for EGI_SYMPAGE */
int symwidth_sum; /* sum of all charachter widths, as all symheight are the same,
* so symwidth_total*symheight is required mem space */
int sympix_total; /* total pixel number of all symbols in a sympage */
int hi,wi;
int pos_symdata, pos_bitmap;
FT_CharMap* pcharmaps=NULL;
char tag_charmap[6]={0};
uint32_t tag_num;
int deg; /* angle in degree */
double angle; /* angle in double */
int i,n; //j,k,m,n;
int ret=0;
/* A1. initialize FT library */
error = FT_Init_FreeType( &library );
if(error) {
printf("%s: An error occured during FreeType library initialization.\n",__func__);
return -1;
}
/* A2. create face object, face_index=0 */
error = FT_New_Face( library, font_path, 0, &face );
if(error==FT_Err_Unknown_File_Format) {
printf("%s: Font file opens, but its font format is unsupported!\n",__func__);
FT_Done_FreeType( library );
return -2;
}
else if ( error ) {
printf("%s: Fail to open or read font file '%s'.\n",__func__, font_path);
FT_Done_FreeType( library );
return -3;
}
/* A3. get pointer to the glyph slot */
slot = face->glyph;
/* A4. print font face[0] parameters */
#if 1 ///////////////////////// PRINT FONT INFO /////////////////////////////
printf(" FreeTypes load font file '%s' :: face_index[0] \n", font_path);
printf(" num_faces: %d\n", (int)face->num_faces);
printf(" face_index: %d\n", (int)face->face_index);
printf(" family name: %s\n", face->family_name);
printf(" style name: %s\n", face->style_name);
printf(" num_glyphs: %d\n", (int)face->num_glyphs);
printf(" face_flags: 0x%08X\n",(int)face->face_flags);
printf(" units_per_EM: %d\n", face->units_per_EM);
printf(" num_fixed_sizes: %d\n", face->num_fixed_sizes);
if(face->num_fixed_sizes !=0 ) {
for(i=0; i< face->num_fixed_sizes; i++) {
printf("[%d]: H%d x W%d\n",i, face->available_sizes[i].height,
face->available_sizes[i].width);
}
}
/* print available charmaps */
printf(" num_charmaps: %d\n", face->num_charmaps);
for(i=0; i< face->num_charmaps; i++) { /* print all charmap tags */
pcharmaps=face->charmaps;
tag_num=htonl((uint32_t)(*pcharmaps)->encoding );
memcpy( tag_charmap, &tag_num, 4); /* 4 bytes TAG */
printf(" [%d] %s\n", i,tag_charmap ); /* 'unic' as for Unicode */
pcharmaps++;
}
tag_num=htonl((uint32_t)( face->charmap->encoding));
memcpy( tag_charmap, &tag_num, 4); /* 4 bytes TAG */
printf(" charmap in use: %s\n", tag_charmap ); /* 'unic' as for Unicode */
/* vertical distance between two consective lines */
printf(" height(V dist, in font units): %d\n", face->height);
#endif //////////////////////////// END PRINT ///////////////////////////////////
/* A5. set character size in pixels */
error = FT_Set_Pixel_Sizes(face, Wp, Hp); /* width,height */
/* OR set character size in 26.6 fractional points, and resolution in dpi */
//error = FT_Set_Char_Size( face, 32*32, 0, 100, 0 );
/* A6. set char inclination angle and matrix for transformation */
deg=0.0;
angle = ( deg / 360.0 ) * 3.14159 * 2;
printf(" Font inclination angle: %d, %f \n", deg, angle);
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
/* --------------------------- NOTE -----------------------------------------------------
* 1. Define a boundary box with bbox_W and bbox_H to contain each character.
* 2. The BBoxes are NOT the same size, and neither top_lines nor bottom_lines
* are aligned at the same level.
* 3. The symbol_page boundary boxes are all with the same height, and their top_lines
* and bottum_lines are aligned at the same level!
-----------------------------------------------------------------------------------------*/
/* ------- Get MAX. base_uppmost, base_lowmost ------- */
base_uppmost=0; /* baseline to top line of [symheight * bbox_W] */
base_lowmost=0; /* baseline to top line of [symheight * bbox_W] */
/* set origin here, so that all character bitmaps are aligned to the same origin/baseline */
origin.x=0;
origin.y=0;
FT_Set_Transform( face, &matrix, &origin);
/* 1. Tranvser all chars to get MAX height for the boundary box, base line all aligned. */
symheight=0;
symwidth_sum=0;
for( n=0; n<128; n++) { /* 0-NULL, 32-SPACE, 127-DEL */
error = FT_Load_Char( face, n, FT_LOAD_RENDER );
if ( error ) {
printf("%s: FT_Load_Char() error!\n",__func__);
exit(1);
}
/* 1.1 base_uppmost: dist from base_line to top */
if(base_uppmost < slot->bitmap_top)
base_uppmost=slot->bitmap_top;
/* 1.2 base_lowmost: dist from base_line to bottom */
/* !!!! unsigned int - signed int */
if( base_lowmost < (int)(slot->bitmap.rows) - (int)(slot->bitmap_top) ) {
base_lowmost=(int)slot->bitmap.rows-(int)slot->bitmap_top;
}
/* 1.3 get symwidth, and sum it up */
/* rule out all unpritable characters, except TAB */
if( n<32 || n==127 ) {
/* symwidth set to be 0, no mem space for it. */
continue;
}
bbox_W=slot->advance.x>>6;
if(bbox_W < slot->bitmap.width) /* adjust bbox_W */
bbox_W =slot->bitmap.width;
symwidth_sum += bbox_W;
}
/* 2. get symheight at last */
symheight=base_uppmost+base_lowmost+1;
printf(" Input Hp=%d; symheight=%d; base_uppmost=%d; base_lowmost=%d (all in pixels)\n",
Hp, symheight, base_uppmost, base_lowmost);
symfont_page->symheight=symheight;
/* 3. Set Maxnum for symbol_page*/
symfont_page->maxnum=128-1; /* 0-127 */
/* 4. allocate EGI_SYMPAGE .data, .alpha, .symwidth, .symoffset */
sympix_total=symheight*symwidth_sum; /* total pixel number in the symbol page */
/* release all data before allocate it */
symbol_release_page(symfont_page);
symfont_page->data=calloc(1, sympix_total*sizeof(uint16_t));
if(symfont_page->data==NULL) { ret=-4; goto FT_FAILS; }
symfont_page->alpha=calloc(1, sympix_total*sizeof(unsigned char));
if(symfont_page->alpha==NULL) { ret=-4; goto FT_FAILS; }
symfont_page->symwidth=calloc(1, (symfont_page->maxnum+1)*sizeof(int) );
if(symfont_page->symwidth==NULL) { ret=-4; goto FT_FAILS; }
symfont_page->symoffset=calloc(1, (symfont_page->maxnum+1)*sizeof(int));
if(symfont_page->symoffset==NULL) { ret=-4; goto FT_FAILS; }
/* 5. Tranverseall ASCIIs to get symfont_page.alpha, ensure that all base_lines are aligned. */
/* NOTE:
* 1. TODO: nonprintable ASCII characters will be presented by FT as a box with a cross inside.
* It is not necessary to be allocated in mem????
* 2. For font sympage, only .aplha is available, .data is useless!!!
* 3. Symbol font boundary box [ symheight*bbox_W ] is bigger than each slot->bitmap box !
*/
symfont_page->symoffset[0]=0; /* default for first char */
for( n=0; n<128; n++) { /* 0-NULL, 32-SPACE, 127-DEL */
/* 5.0 rule out all unpritable characters */
if( n<32 || n==127 ) {
symfont_page->symwidth[n]=0;
/* Not necessary to change symfont_page->symoffset[x], as its symwidth is 0. */
continue;
}
/* 5.1 load N to ASCII symbol bitmap */
error = FT_Load_Char( face, n, FT_LOAD_RENDER );
if ( error ) {
printf("%s: Fail to FT_Load_Char() n=%d as ASCII symbol!\n", __func__, n);
exit(1);
}
/* 5.2 get symwidth for each symbol, =MAX(advance.x>>6, bitmap.width) */
bbox_W=slot->advance.x>>6;
if(bbox_W < slot->bitmap.width) /* adjust bbox_W */
bbox_W =slot->bitmap.width;
symfont_page->symwidth[n]=bbox_W;
/* 5.3 <<<<<<<<< get symfont_page.alpha >>>>>>>>>
As sympage boundary box [ symheight*bbox_W ] is big enough to hold each
slot->bitmap image, we divide symheight into 3 parts, with the mid part
equals bitmap.rows, and get alpha value accordingly.
ASSUME: bitmap start from wi=0 !!!
variables range:: { hi: 0-symheight, wi: 0-bbox_W }
*/
/* 1. From base_uppmost to bitmap_top:
* All blank lines, and alpha values are 0 as default.
*
* <------ So This Part Can be Ignored ------>
*/
#if 0 /* <------ This Part Can be Ignored ------> */
for( hi=0; hi < base_uppmost-slot->bitmap_top; hi++) {
for( wi=0; wi < bbox_W; wi++ ) {
pos_symdata=symfont_page->symoffset[n] + hi*bbox_W + wi;
/* all default value */
symfont_page->alpha[pos_symdata] = 0;
}
}
#endif
/* 2. From bitmap_top to bitmap bottom,
* Actual data here.
*/
for( hi=base_uppmost-slot->bitmap_top; hi < (base_uppmost-slot->bitmap_top) + slot->bitmap.rows; \
hi++ )
{
/* ROW: within bitmap.width, !!! ASSUME bitmap start from wi=0 !!! */
for( wi=0; wi < slot->bitmap.width; wi++ ) {
pos_symdata=symfont_page->symoffset[n] + hi*bbox_W +wi;
pos_bitmap=(hi-(base_uppmost-slot->bitmap_top))*slot->bitmap.width+wi;
symfont_page->alpha[pos_symdata]= slot->bitmap.buffer[pos_bitmap];
#if 0 /* ------ TEST ONLY, print alpha here ------ */
if(n=='j') {
if(symfont_page->alpha[pos_symdata]>0)
printf("*");
else
printf(" ");
if( wi==slot->bitmap.width-1 )
printf("\n");
}
#endif /* ------------- END TEST -------------- */
}
/* ROW: blank area in BBOX */
for( wi=slot->bitmap.width; wi<bbox_W; wi++ ) {
pos_symdata=symfont_page->symoffset[n] + hi*bbox_W +wi;
symfont_page->alpha[pos_symdata]=0;
}
}
/* 3. From bitmap bottom to symheight( bottom),
* All blank lines, and alpha values are 0 as default.
*
* <------ So This Part Can be Ignored too! ------>
*/
#if 0 /* <------ This Part Can be Ignored ------> */
for( hi=(base_uppmost-slot->bitmap_top) + slot->bitmap.rows; hi < symheight; hi++ )
{
for( wi=0; wi < bbox_W; wi++ ) {
pos_symdata=symfont_page->symoffset[n] + hi*bbox_W + wi;
/* set only alpha value */
symfont_page->alpha[pos_symdata] = 0;
}
}
#endif
/* calculate symoffset for next one */
if(n<127)
symfont_page->symoffset[n+1] = symfont_page->symoffset[n] + bbox_W * symheight;
} /* end transversing all ASCII chars */
FT_FAILS:
FT_Done_Face ( face );
FT_Done_FreeType( library );
return ret;
}
/*--------------------------------------------------------------------------
To get actual Max. symHeight/bitmapHeight of a string, with dedicated FT_Face
and font size.
1. Try NOT to use this function, it's slow. and use CAPITAL LETTERs instead
of small letters.
2. The purpose of this function is to use the result symHeight to align string
in middle of some space.
Note: It's usually for ASCII chars. For Chinese char, they are usually the same
height.
Return:
>0 OK
<0 Fails
---------------------------------------------------------------------------*/
int FTsymbol_get_symheight(FT_Face face, const unsigned char *pstr, int fw, int fh )
{
int symheight=0;
int size;
FT_Error error;
FT_GlyphSlot slot = face->glyph;
wchar_t wcode;
/* Set character size in pixels */
error = FT_Set_Pixel_Sizes(face, fw, fh); /* width,height */
if(error) {
printf("%s: Fail to set pixel size!\n",__func__);
return -1;
}
/* Default transformation matrix */
while( *pstr != L'\0' ) { /* wchar_t string end token */
/* convert one character to unicode, return size of utf-8 code */
size=char_uft8_to_unicode((unsigned char *)pstr, &wcode);
if(size<1) /* Maybe nonprintable/control char, Supposed they are at very end of the string! Break then. */
break;
else
pstr +=size;
error = FT_Load_Char( face, wcode, FT_LOAD_RENDER );
if ( error ) {
printf("%s: FT_Load_Char() error!\n",__func__);
return -1;
}
if(symheight < slot->bitmap.rows) {
symheight=slot->bitmap.rows;
// printf("%s: symheight=%d, deltY=%d \n", (char *)&wcode, symheight, -slot->bitmap_top + fh);
}
}
return symheight;
}
/*--------------------------------------------------------------------------
Return FT_Face->size->metrics.height, which is the Max. possible face height
for given font size.
Usually it's much bigger than symheight and bitmap height. and may be used
for line distance.
@fh,fw: Expected font size.
Return:
>0 OK
<0 Fails
---------------------------------------------------------------------------*/
inline int FTsymbol_get_FTface_Height(FT_Face face, int fw, int fh)
{
FT_Error error;
/* set character size in pixels */
error = FT_Set_Pixel_Sizes(face, fw, fh);
/* OR set character size in 26.6 fractional points, and resolution in dpi
error = FT_Set_Char_Size( face, 32*32, 0, 100,0 ); */
if(error) {
printf("%s: FT_Set_Pixel_Sizes() fails!\n",__func__);
return -1;
}
return face->size->metrics.height>>6;
}
/*----------------------------------------------------------------------------------------------------
Get total advance value of the input string. the FT_Face shall be preseted with expected parameters,
transforms etc. The purpose is to get the space needed for writFBing the string in a fast way.
@face: FT_Face
@load_flags: A flag indicating what to load for this glyph
!!! WARNING !!! Must be the same as in FT_Load_Char( face, wcode, flags ) as for writeFB
the same string.
@fw,fh: Expected font width and height, Must be the same as in FT_Set_Pixel_Sizes() as for writeFB
the same string.
@pstr: pointer to a string with UTF-8 encoding.
NOTE: