summaryrefslogtreecommitdiff
path: root/src/lib/evas/common/evas_font_load.c
blob: 59b8146a63980fbb8ef688b511b25e0b3129bfd8 (plain)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include "evas_common_private.h"
#include "evas_private.h"

#ifdef USE_HARFBUZZ
# include <hb.h>
#endif

#include "evas_font_private.h" /* for Frame-Queuing support */
#include <freetype/tttables.h> /* Freetype2 OS/2 font table. */

#ifdef EVAS_CSERVE2
# include "../cserve2/evas_cs2_private.h"
#endif

extern FT_Library         evas_ft_lib;

static int                font_cache_usage = 0;
static int                font_cache = 0;
static int                font_dpi = 75;

static Eina_Hash   *fonts_src = NULL;
static Eina_Hash   *fonts = NULL;
static Eina_List   *fonts_lru = NULL;
static Eina_Inlist *fonts_use_lru = NULL;
static int          fonts_use_usage = 0;

static void _evas_common_font_int_clear(RGBA_Font_Int *fi);

static int
_evas_font_cache_int_cmp(const RGBA_Font_Int *k1, int k1_length EINA_UNUSED,
			 const RGBA_Font_Int *k2, int k2_length EINA_UNUSED)
{
   /* RGBA_Font_Source->name is a stringshare */
   if (k1->src->name == k2->src->name)
     {
        if (k1->size == k2->size)
           return k1->wanted_rend - k2->wanted_rend;
        else
           return k1->size - k2->size;
     }
   return strcmp(k1->src->name, k2->src->name);
}

static int
_evas_font_cache_int_hash(const RGBA_Font_Int *key, int key_length EINA_UNUSED)
{
   int hash;
   unsigned int wanted_rend = key->wanted_rend;
   hash = eina_hash_djb2(key->src->name, eina_stringshare_strlen(key->src->name) + 1);
   hash ^= eina_hash_int32(&key->size, sizeof (int));
   hash ^= eina_hash_int32(&wanted_rend, sizeof (int));
   return hash;
}

static void
_evas_common_font_source_free(RGBA_Font_Source *fs)
{
   FTLOCK();
   FT_Done_Face(fs->ft.face);
   FTUNLOCK();
   if (fs->name) eina_stringshare_del(fs->name);
   if (fs->file) eina_stringshare_del(fs->file);
   free(fs);
}

static void
_evas_common_font_int_free(RGBA_Font_Int *fi)
{
   FTLOCK();
   FT_Done_Size(fi->ft.size);
   FTUNLOCK();

   evas_common_font_int_modify_cache_by(fi, -1);
   _evas_common_font_int_clear(fi);
   eina_hash_free(fi->kerning);

   LKD(fi->ft_mutex);
#ifdef USE_HARFBUZZ
   hb_font_destroy(fi->ft.hb_font);
#endif
   evas_common_font_source_free(fi->src);
   if (fi->references <= 0) fonts_lru = eina_list_remove(fonts_lru, fi);
   if (fi->fash) fi->fash->freeme(fi->fash);
   if (fi->inuse)
    {
      fonts_use_lru = eina_inlist_remove(fonts_use_lru, EINA_INLIST_GET(fi));
      fi->inuse = 0;
      fonts_use_usage -= fi->usage;
      fi->usage = 0;
    }
#ifdef EVAS_CSERVE2
   evas_cserve2_font_free(fi->cs2_handler);
#endif
   free(fi);
}

void
evas_common_font_load_init(void)
{
   fonts_src = eina_hash_string_small_new(EINA_FREE_CB(_evas_common_font_source_free));
   fonts = eina_hash_new(NULL,
			 EINA_KEY_CMP(_evas_font_cache_int_cmp),
			 EINA_KEY_HASH(_evas_font_cache_int_hash),
			 EINA_FREE_CB(_evas_common_font_int_free),
			 5);
}

void
evas_common_font_load_shutdown(void)
{
   eina_hash_free(fonts);
   fonts = NULL;
   eina_hash_free(fonts_src);
   fonts_src = NULL;
}

EAPI void
evas_common_font_dpi_set(int dpi)
{
   font_dpi = dpi;
}

EAPI RGBA_Font_Source *
evas_common_font_source_memory_load(const char *name, const void *data, int data_size)
{
   int error; 
   RGBA_Font_Source *fs;

   assert(name != NULL);
   fs = calloc(1, sizeof(RGBA_Font_Source) + data_size);
   if (!fs) return NULL;
   fs->data = ((unsigned char *)fs) + sizeof(RGBA_Font_Source);
   fs->data_size = data_size;
   fs->current_size = 0;
   memcpy(fs->data, data, data_size);
   FTLOCK();
   error = FT_New_Memory_Face(evas_ft_lib, fs->data, fs->data_size, 0, &(fs->ft.face));
   FTUNLOCK();
   if (error)
     {
	free(fs);
	return NULL;
     }
   fs->name = eina_stringshare_add(name);
   fs->file = NULL;
   FTLOCK();
   error = FT_Select_Charmap(fs->ft.face, ft_encoding_unicode);

   if (error)
     {
        FT_Done_Face(fs->ft.face);
        FTUNLOCK();
        fs->ft.face = NULL;
        free(fs);
        return NULL;
     }

   FTUNLOCK();
   fs->ft.orig_upem = fs->ft.face->units_per_EM;
   fs->references = 1;
   eina_hash_direct_add(fonts_src, fs->name, fs);
   return fs;
}

EAPI RGBA_Font_Source *
evas_common_font_source_load(const char *name)
{
   RGBA_Font_Source *fs;

   assert(name != NULL);
   fs = calloc(1, sizeof(RGBA_Font_Source));
   if (!fs) return NULL;
   fs->data = NULL;
   fs->data_size = 0;
   fs->current_size = 0;
   fs->ft.face = NULL;
   fs->name = eina_stringshare_add(name);
   fs->file = eina_stringshare_ref(fs->name);
   fs->ft.orig_upem = 0;
   fs->references = 1;
   eina_hash_direct_add(fonts_src, fs->name, fs);
   return fs;
}

void
evas_common_font_source_unload(RGBA_Font_Source *fs)
{
   FTLOCK();
   FT_Done_Face(fs->ft.face);
   fs->ft.face = NULL;
   FTUNLOCK();
}

void
evas_common_font_source_reload(RGBA_Font_Source *fs)
{
  if (fs->ft.face) return;
  if (fs->data)
    {
      int error;
      
      FTLOCK();
      error = FT_New_Memory_Face(evas_ft_lib, fs->data, fs->data_size, 0, &(fs->ft.face));
      FTUNLOCK();
      if (error) return;
      FTLOCK();
      error = FT_Select_Charmap(fs->ft.face, ft_encoding_unicode);
      if (error)
        {
          FT_Done_Face(fs->ft.face);
          fs->ft.face = NULL;
        }
      FTUNLOCK();
    }
  else
    evas_common_font_source_load_complete(fs);
}

EAPI int
evas_common_font_source_load_complete(RGBA_Font_Source *fs)
{
   int error;

   FTLOCK();
   error = FT_New_Face(evas_ft_lib, fs->file, 0, &(fs->ft.face));
   if (error)
     {
        FTUNLOCK();
	fs->ft.face = NULL;
	return error;
     }
   error = FT_Select_Charmap(fs->ft.face, ft_encoding_unicode);
   if (error)
     {
	FT_Done_Face(fs->ft.face);
        FTUNLOCK();
	fs->ft.face = NULL;
	return error;
     }
   FTUNLOCK();
   fs->ft.orig_upem = fs->ft.face->units_per_EM;
   return error;
}

EAPI RGBA_Font_Source *
evas_common_font_source_find(const char *name)
{
   RGBA_Font_Source *fs;

   if (!name) return NULL;
   fs = eina_hash_find(fonts_src, name);
   if (fs)
     {
	fs->references++;
	return fs;
     }
   return NULL;
}

EAPI void
evas_common_font_source_free(RGBA_Font_Source *fs)
{
   fs->references--;
   fs->current_size = 0;
   if (fs->references > 0) return;
   eina_hash_del(fonts_src, fs->name, fs);
}

EAPI void
evas_common_font_size_use(RGBA_Font *fn)
{
   RGBA_Font_Int *fi;
   Eina_List *l;

   EINA_LIST_FOREACH(fn->fonts, l, fi)
     {
	if (fi->src->current_size != fi->size)
	  {
             evas_common_font_source_reload(fi->src);
             FTLOCK();
	     FT_Activate_Size(fi->ft.size);
             FTUNLOCK();
	     fi->src->current_size = fi->size;
	  }
     }
}

static int
_evas_common_font_double_int_cmp(const int *key1, EINA_UNUSED int key1_length,
				 const int *key2, EINA_UNUSED int key2_length)
{
   if (key1[0] - key2[0] == 0) return key1[1] - key2[1];
   return key1[0] - key2[0];
}

static int
_evas_common_font_double_int_hash(const unsigned int key[2], int key_length)
{
   return 
    eina_hash_int32(&key[0], key_length) ^ 
    eina_hash_int32(&key[1], key_length);
}

static void
_evas_common_font_int_cache_init(RGBA_Font_Int *fi)
{
   /* Add some font kerning cache. */
  fi->kerning = eina_hash_new(NULL,
			       EINA_KEY_CMP(_evas_common_font_double_int_cmp),
			       EINA_KEY_HASH(_evas_common_font_double_int_hash),
			       free, 3);
   LKI(fi->ft_mutex);
}

EAPI RGBA_Font_Int *
evas_common_font_int_memory_load(const char *source, const char *name, int size, const void *data, int data_size, Font_Rend_Flags wanted_rend)
{
   RGBA_Font_Int *fi;
   char *fake_name;

   fake_name = evas_file_path_join(source, name);
   fi = evas_common_font_int_find(fake_name, size, wanted_rend);
   if (fi)
     {
        free(fake_name);
        return fi;
     }
   fi = calloc(1, sizeof(RGBA_Font_Int));
   if (!fi)
     {
        free(fake_name);
        return NULL;
     }
   fi->src = evas_common_font_source_find(fake_name);
   if (!fi->src)
    fi->src = evas_common_font_source_memory_load(fake_name, data, data_size);
   if (!fi->src)
     {
	free(fi);
        free(fake_name);
	return NULL;
     }
   fi->size = size;
   _evas_common_font_int_cache_init(fi);
   fi = evas_common_font_int_load_init(fi);
   evas_common_font_int_load_complete(fi);
#ifdef EVAS_CSERVE2
   if (evas_cserve2_use_get())
     {
        fi->cs2_handler = evas_cserve2_font_load(source, name, size, font_dpi,
                                                 wanted_rend);
        if (fi->cs2_handler)
          {
             if (evas_cserve2_font_load_wait((Font_Entry *)fi->cs2_handler) != 0)
               {
                  evas_cserve2_font_free(fi->cs2_handler);
                  fi->cs2_handler = NULL;
               }
          }
     }
#endif
   free(fake_name);
   return fi;
}

EAPI RGBA_Font_Int *
evas_common_font_int_load(const char *name, int size,
                          Font_Rend_Flags wanted_rend)
{
   RGBA_Font_Int *fi;

   fi = evas_common_font_int_find(name, size, wanted_rend);
   if (fi) return fi;
   fi = calloc(1, sizeof(RGBA_Font_Int));
   if (!fi) return NULL;
   fi->src = evas_common_font_source_find(name);
   if (!fi->src && evas_file_path_is_file(name))
     fi->src = evas_common_font_source_load(name);

   if (!fi->src)
     {
	free(fi);
	return NULL;
     }
   fi->size = size;
   fi->wanted_rend = wanted_rend;
   _evas_common_font_int_cache_init(fi);
   fi = evas_common_font_int_load_init(fi);
#ifdef EVAS_CSERVE2
   if (evas_cserve2_use_get())
     {
        fi->cs2_handler = evas_cserve2_font_load(NULL, name, size, font_dpi,
                                                 wanted_rend);
        if (fi->cs2_handler)
          {
             if (evas_cserve2_font_load_wait((Font_Entry *)fi->cs2_handler) != 0)
               {
                  evas_cserve2_font_free(fi->cs2_handler);
                  fi->cs2_handler = NULL;
               }
          }
     }
#endif
//   evas_common_font_int_load_complete(fi);
   return fi;
}

EAPI RGBA_Font_Int *
evas_common_font_int_load_init(RGBA_Font_Int *fi)
{
   fi->ft.size = NULL;
   fi->references = 1;
   eina_hash_direct_add(fonts, fi, fi);
   return fi;
}

EAPI RGBA_Font_Int *
evas_common_font_int_load_complete(RGBA_Font_Int *fi)
{
   int val, dv;
   int ret;
   int error;

   FTLOCK();
   error = FT_New_Size(fi->src->ft.face, &(fi->ft.size));
   if (!error)
     {
	FT_Activate_Size(fi->ft.size);
     }
   fi->real_size = fi->size * 64;
   error = FT_Set_Char_Size(fi->src->ft.face, 0, fi->real_size, font_dpi, font_dpi);
   if (error)
     error = FT_Set_Pixel_Sizes(fi->src->ft.face, 0, fi->real_size);
   FTUNLOCK();
   if (error)
     {
	int i, maxd = 0x7fffffff;
	int chosen_size = 0;
	int chosen_size2 = 0;

	for (i = 0; i < fi->src->ft.face->num_fixed_sizes; i++)
	  {
	     int s, cd;
             
	     s = fi->src->ft.face->available_sizes[i].size;
	     cd = chosen_size - fi->real_size;
	     if (cd < 0) cd = -cd;
             if (cd < maxd)
               {
                  maxd = cd;
		  chosen_size = s;
		  chosen_size2 = fi->src->ft.face->available_sizes[i].y_ppem;
                  if (maxd == 0) break;
	       }
	  }
	fi->real_size = chosen_size;
        FTLOCK();
	error = FT_Set_Pixel_Sizes(fi->src->ft.face, 0, fi->real_size);
        FTUNLOCK();
	if (error)
	  {
             error = FT_Set_Char_Size(fi->src->ft.face, 0, fi->real_size, font_dpi, font_dpi);
             if (error)
               {
                  /* hack around broken fonts */
                  fi->real_size = (chosen_size2 / 64) * 60;
                  error = FT_Set_Char_Size(fi->src->ft.face, 0, fi->real_size, font_dpi, font_dpi);
                  if (error)
                    {
                       /* couldn't choose the size anyway... what now? */
                    }
               }
	  }
     }
   fi->src->current_size = 0;
   fi->max_h = 0;
   val = (int)fi->src->ft.face->bbox.yMax;
   if (fi->src->ft.face->units_per_EM != 0)
     {
        dv = (fi->src->ft.orig_upem * 2048) / fi->src->ft.face->units_per_EM;
        ret = FONT_METRIC_CONV(val, dv, fi->src->ft.face->size->metrics.y_scale);
     }
   else
     {
        if ((fi->src->ft.face->bbox.yMax == 0) &&
            (fi->src->ft.face->bbox.yMin == 0))
          ret = FONT_METRIC_ROUNDUP((int)fi->ft.size->metrics.ascender);
        else
          ret = val;
     }
   fi->max_h += ret;
   val = -(int)fi->src->ft.face->bbox.yMin;
   if (fi->src->ft.face->units_per_EM != 0)
     {
        dv = (fi->src->ft.orig_upem * 2048) / fi->src->ft.face->units_per_EM;
        ret = FONT_METRIC_CONV(val, dv, fi->src->ft.face->size->metrics.y_scale);
     }
   else
     {
        if ((fi->src->ft.face->bbox.yMax == 0) &&
            (fi->src->ft.face->bbox.yMin == 0))
          ret = FONT_METRIC_ROUNDUP(-(int)fi->ft.size->metrics.descender);
        else
          ret = val;
     }
   fi->max_h += ret;

   /* If the loaded font doesn't match with wanted_rend value requested by
    * textobject and textblock, Set the runtime_rend value as FONT_REND_SLANT
    * or FONT_REND_WEIGHT for software rendering. */
   fi->runtime_rend = FONT_REND_REGULAR;
   if ((fi->wanted_rend & FONT_REND_SLANT) &&
       !(fi->src->ft.face->style_flags & FT_STYLE_FLAG_ITALIC))
      fi->runtime_rend |= FONT_REND_SLANT;

   if ((fi->wanted_rend & FONT_REND_WEIGHT) &&
       !(fi->src->ft.face->style_flags & FT_STYLE_FLAG_BOLD))
     {
        TT_OS2 *tt_os2 = FT_Get_Sfnt_Table(fi->src->ft.face, ft_sfnt_os2);
        if (!tt_os2 || (tt_os2->usWeightClass < 600))
          {
             fi->runtime_rend |= FONT_REND_WEIGHT;
          }
     }

   return fi;
}

EAPI RGBA_Font *
evas_common_font_memory_load(const char *source, const char *name, int size, const void *data, int data_size, Font_Rend_Flags wanted_rend)
{
   RGBA_Font *fn;
   RGBA_Font_Int *fi;

   fi = evas_common_font_int_memory_load(source, name, size, data, data_size,
                                         wanted_rend);
   if (!fi) return NULL;
   fn = calloc(1, sizeof(RGBA_Font));
   if (!fn)
     {
        evas_common_font_int_unref(fi);
	return NULL;
     }
   fn->fonts = eina_list_append(fn->fonts, fi);
   fn->hinting = FONT_BYTECODE_HINT;
   fi->hinting = fn->hinting;
   fn->references = 1;
   LKI(fn->lock);
   if (fi->inuse) evas_common_font_int_promote(fi);
   else
    {
      fi->inuse = 1;
      fonts_use_lru = eina_inlist_prepend(fonts_use_lru, EINA_INLIST_GET(fi));
    }
   return fn;
}


//ZZZ: font struct looks like:
// fn->(fi, fi, fi, ...)
//   fi->fs

EAPI RGBA_Font *
evas_common_font_load(const char *name, int size, Font_Rend_Flags wanted_rend)
{
   RGBA_Font *fn;
   RGBA_Font_Int *fi;

   fi = evas_common_font_int_load(name, size, wanted_rend);
   if (!fi) return NULL;
   /* First font, complete load */
   if (!fi->ft.size)
     {
	if (!fi->src->ft.face)
	  {
	     if (evas_common_font_source_load_complete(fi->src))
	       {
                  evas_common_font_int_unref(fi);
		  return NULL;
	       }
	  }
	evas_common_font_int_load_complete(fi);
     }
   fn = calloc(1, sizeof(RGBA_Font));
   if (!fn)
     {
        evas_common_font_int_unref(fi);
	return NULL;
     }

   fn->fonts = eina_list_append(fn->fonts, fi);
   fn->hinting = FONT_BYTECODE_HINT;
   fi->hinting = fn->hinting;
   fn->references = 1;
   LKI(fn->lock);
   if (fi->inuse) evas_common_font_int_promote(fi);
   else
    {
      fi->inuse = 1;
      fonts_use_lru = eina_inlist_prepend(fonts_use_lru, EINA_INLIST_GET(fi));
    }
   return fn;
}

EAPI RGBA_Font *
evas_common_font_add(RGBA_Font *fn, const char *name, int size, Font_Rend_Flags wanted_rend)
{
   RGBA_Font_Int *fi;

   if (!fn) return NULL;
   fi = evas_common_font_int_load(name, size, wanted_rend);
   if (fi)
     {
	fn->fonts = eina_list_append(fn->fonts, fi);
	fi->hinting = fn->hinting;
        if (fi->inuse) evas_common_font_int_promote(fi);
        else
         {
           fi->inuse = 1;
           fonts_use_lru = eina_inlist_prepend(fonts_use_lru, EINA_INLIST_GET(fi));
         }
	return fn;
     }
   return NULL;
}

EAPI RGBA_Font *
evas_common_font_memory_add(RGBA_Font *fn, const char *source, const char *name, int size, const void *data, int data_size, Font_Rend_Flags wanted_rend)
{
   RGBA_Font_Int *fi;

   if (!fn)
      return NULL;
   fi = evas_common_font_int_memory_load(source, name, size, data, data_size, wanted_rend);
   if (fi)
     {
	fn->fonts = eina_list_append(fn->fonts, fi);
	fi->hinting = fn->hinting;
        if (fi->inuse) evas_common_font_int_promote(fi);
        else
         {
           fi->inuse = 1;
           fonts_use_lru = eina_inlist_prepend(fonts_use_lru, EINA_INLIST_GET(fi));
         }
	return fn;
     }
   return NULL;
}

EAPI void
evas_common_font_int_unref(RGBA_Font_Int *fi)
{
   fi->references--;
   if (fi->references == 0)
     {
        fonts_lru = eina_list_append(fonts_lru, fi);
        evas_common_font_int_modify_cache_by(fi, 1);
        evas_common_font_flush();
     }
}

EAPI void
evas_common_font_free(RGBA_Font *fn)
{
   Eina_List *l;
   RGBA_Font_Int *fi;

   if (!fn) return;
   fn->references--;
   if (fn->references > 0) return;
   EINA_LIST_FOREACH(fn->fonts, l, fi)
     evas_common_font_int_unref(fi);
   evas_common_font_flush();
   eina_list_free(fn->fonts);
   if (fn->fash) fn->fash->freeme(fn->fash);
   LKD(fn->lock);
   free(fn);
}

EAPI void
evas_common_font_hinting_set(RGBA_Font *fn, Font_Hint_Flags hinting)
{
   Eina_List *l;
   RGBA_Font_Int *fi;

   if (!fn) return;
   fn->hinting = hinting;
   EINA_LIST_FOREACH(fn->fonts, l, fi)
     {
        if (fi->hinting != fn->hinting)
          _evas_common_font_int_clear(fi);
        fi->hinting = fn->hinting;
     }
}

EAPI Eina_Bool
evas_common_hinting_available(Font_Hint_Flags hinting)
{
   switch (hinting)
     {
      case FONT_NO_HINT:
      case FONT_AUTO_HINT:
	 /* these two hinting modes are always available */
	 return EINA_TRUE;
      case FONT_BYTECODE_HINT:
	 /* Only use the bytecode interpreter if support for the _patented_
	  * algorithms is available because the free bytecode
	  * interpreter's results are too crappy.
	  *
	  * On freetyp 2.2+, we can ask the library about support for
	  * the patented interpreter. On older versions, we need to use
	  * macros to check for it.
	  */
#if FREETYPE_MINOR >= 2
	 return FT_Get_TrueType_Engine_Type(evas_ft_lib) >=
		FT_TRUETYPE_ENGINE_TYPE_PATENTED;
#else
	 /* we may not rely on TT_CONFIG_OPTION_BYTECODE_INTERPRETER
	  * here to find out whether it's supported.
	  *
	  * so, assume it is. o_O
	  */
	 return EINA_TRUE;
#endif
     }
   /* shouldn't get here - need to add another case statement */
   return EINA_FALSE;
}

EAPI RGBA_Font *
evas_common_font_memory_hinting_load(const char *source, const char *name, int size, const void *data, int data_size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend)
{
   RGBA_Font *fn;

   fn = evas_common_font_memory_load(source, name, size, data, data_size, wanted_rend);
   if (fn) evas_common_font_hinting_set(fn, hinting);
   return fn;
}

EAPI RGBA_Font *
evas_common_font_hinting_load(const char *name, int size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend)
{
   RGBA_Font *fn;

   fn = evas_common_font_load(name, size, wanted_rend);
   if (fn) evas_common_font_hinting_set(fn, hinting);
   return fn;
}

EAPI RGBA_Font *
evas_common_font_hinting_add(RGBA_Font *fn, const char *name, int size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend)
{
   fn = evas_common_font_add(fn, name, size, wanted_rend);
   if (fn) evas_common_font_hinting_set(fn, hinting);
   return fn;
}

EAPI RGBA_Font *
evas_common_font_memory_hinting_add(RGBA_Font *fn, const char *source, const char *name, int size, const void *data, int data_size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend)
{
   fn = evas_common_font_memory_add(fn, source, name, size, data, data_size,
                                    wanted_rend);
   if (fn) evas_common_font_hinting_set(fn, hinting);
   return fn;
}

static void
_evas_common_font_int_clear(RGBA_Font_Int *fi)
{
   LKL(fi->ft_mutex);
   if (!fi->fash)
     {
        LKU(fi->ft_mutex);
        return;
     }
   evas_common_font_int_modify_cache_by(fi, -1);
   if (fi->references <= 1)
     {
        if (fi->fash)
          {
             fi->fash->freeme(fi->fash);
             fi->fash = NULL;
          }
     }
   if (fi->inuse) fonts_use_usage -= fi->usage;
   fi->usage = 0;
   fi->generation++;
   LKU(fi->ft_mutex);
}

static Eina_Bool
_evas_common_font_all_clear_cb(const Eina_Hash *hash EINA_UNUSED, const void *key EINA_UNUSED, void *data, void *fdata EINA_UNUSED)
{
   RGBA_Font_Int *fi = data;
   _evas_common_font_int_clear(fi);
   return 1;
}

EAPI void
evas_common_font_all_clear(void)
{
   eina_hash_foreach(fonts, _evas_common_font_all_clear_cb, NULL);
}

void
evas_common_font_int_promote(RGBA_Font_Int *fi)
{
  return;
  if (fonts_use_lru == (Eina_Inlist *)fi) return;
  if (!fi->inuse) return;
  fonts_use_lru = eina_inlist_remove(fonts_use_lru, EINA_INLIST_GET(fi));
  fonts_use_lru = eina_inlist_prepend(fonts_use_lru, EINA_INLIST_GET(fi));
}

void
evas_common_font_int_use_increase(int size)
{
  fonts_use_usage += size;
}

void
evas_common_font_int_use_trim(void)
{
  Eina_Inlist *l;

  return;
  if (fonts_use_usage <= (font_cache << 1)) return;
  if (!fonts_use_lru) return;
  l = fonts_use_lru->last;
  while (l)
    {
      RGBA_Font_Int *fi = (RGBA_Font_Int *)l;
      if (fonts_use_usage <= (font_cache << 1)) break;
      // FIXME: del fi->kerning content
      _evas_common_font_int_clear(fi);
      evas_common_font_int_unload(fi);
      evas_common_font_int_promote(fi);
      l = l->prev;
    }
}

void
evas_common_font_int_unload(RGBA_Font_Int *fi)
{
  return;
  if (!fi->src->ft.face) return;
  _evas_common_font_int_clear(fi);
  FT_Done_Size(fi->ft.size);
  fi->ft.size = NULL;
  evas_common_font_source_unload(fi->src);
}

void
evas_common_font_int_reload(RGBA_Font_Int *fi)
{
  if (fi->src->ft.face) return;
  evas_common_font_source_load_complete(fi->src);
  return;
  evas_common_font_source_reload(fi->src);
  evas_common_font_int_load_complete(fi);
}

/* when the fi->references == 0 we increase this instead of really deleting
 * we then check if the cache_useage size is larger than allowed
 * !If the cache is NOT too large we dont delete font_int
 * !If the cache is too large we really delete font_int */
EAPI void
evas_common_font_int_modify_cache_by(RGBA_Font_Int *fi, int dir)
{
   font_cache_usage += dir * (sizeof(RGBA_Font) + fi->usage +
                              sizeof(FT_FaceRec) + 16384); /* fudge values */
}

EAPI int
evas_common_font_cache_get(void)
{
   return font_cache;
}

EAPI void
evas_common_font_cache_set(int size)
{
   font_cache = size;
   evas_common_font_flush();
   evas_common_font_int_use_trim();
}

EAPI void
evas_common_font_flush(void)
{
   if (font_cache_usage < font_cache) return;
   while (font_cache_usage > font_cache)
     {
        int pfont_cache_usage;
        
        pfont_cache_usage = font_cache_usage;
        evas_common_font_flush_last();
        if (pfont_cache_usage == font_cache_usage) break;
     }
}

/* We run this when the cache gets larger than allowed size
 * We check cache size each time a fi->references goes to 0
 * PERFORMS: Find font_int(s) with references == 0 and delete them */
EAPI void
evas_common_font_flush_last(void)
{
   RGBA_Font_Int *fi = NULL;

   if (!fonts_lru) return;
   fi = eina_list_data_get(fonts_lru);
   fonts_lru = eina_list_remove_list(fonts_lru, fonts_lru);
   eina_hash_del(fonts, fi, fi);
}

EAPI RGBA_Font_Int *
evas_common_font_int_find(const char *name, int size,
                          Font_Rend_Flags wanted_rend)
{
   RGBA_Font_Int tmp_fi;
   RGBA_Font_Source tmp_fn;
   RGBA_Font_Int *fi;

   tmp_fn.name = (char*) eina_stringshare_add(name);
   tmp_fi.src = &tmp_fn;
   tmp_fi.size = size;
   tmp_fi.wanted_rend = wanted_rend;
   fi = eina_hash_find(fonts, &tmp_fi);
   if (fi)
     {
	if (fi->references == 0)
	  {
	     evas_common_font_int_modify_cache_by(fi, -1);
	     fonts_lru = eina_list_remove(fonts_lru, fi);
	  }
	fi->references++;
     }
   eina_stringshare_del(tmp_fn.name);
   return fi;
}