The FreeType Caching Subsystem - first lines of code beware, this code is not tested, and probably doesn't compile correctly.. more information will follow..
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
diff --git a/src/cache/ftcimage.c b/src/cache/ftcimage.c
new file mode 100644
index 0000000..3c735e4
--- /dev/null
+++ b/src/cache/ftcimage.c
@@ -0,0 +1,147 @@
+#include <cache/ftcimage.h>
+
+ static
+ void ftc_done_glyph_image( FTC_Image_Queue queue,
+ FTC_ImageNode node )
+ {
+ FT_UNUSED(queue);
+ FT_Done_Glyph( FTC_IMAGENODE_GET_GLYPH(node) );
+ }
+
+ static
+ FT_ULong ftc_size_bitmap_image( FTC_Image_Queue queue,
+ FTC_ImageNode node )
+ {
+ FT_Long pitch;
+ FT_BitmapGlyph glyph;
+
+ FT_UNUSED(queue);
+ glyph = (FT_BitmapGlyph)FTC_IMAGENODE_GET_GLYPH(node);
+ pitch = glyph->bitmap.pitch;
+ if (pitch < 0)
+ pitch = -pitch;
+
+ return (FT_ULong)(pitch * glyph->bitmap->rows + sizeof(*glyph));
+ }
+
+ static
+ FT_ULong ftc_size_outline_image( FTC_Image_Queue queue,
+ FTC_ImageNode node )
+ {
+ FT_Long pitch;
+ FT_OutlineGlyph glyph;
+ FT_Outline* outline;
+
+ FT_UNUSED(queue);
+ glyph = (FT_OutlineGlyph)FTC_IMAGENODE_GET_GLYPH(node);
+ outline = &glyph->outline;
+
+ return (FT_ULong)(
+ outline->n_points * (sizeof(FT_Vector)+sizeof(FT_Byte)) +
+ outline->n_contours * sizeof(FT_Short) +
+ sizeof(*glyph) );
+ }
+
+ /*******************************************************************/
+ /*******************************************************************/
+ /***** *****/
+ /***** MONOCHROME BITMAP CALLBACKS *****/
+ /***** *****/
+ /*******************************************************************/
+ /*******************************************************************/
+
+ static
+ FT_Error ftc_init_mono_image( FTC_Image_Queue queue,
+ FTC_ImageNode node )
+ {
+ FT_Face face;
+ FT_Size size;
+ FT_Error error;
+
+
+ error = FTC_Manager_Lookup_Size( queue->manager,
+ &queue->size_rec,
+ &face, &size );
+ if (!error)
+ {
+ FT_UInt glyph_index = FTC_IMAGENODE_GINDEX(node);
+
+
+ error = FT_Load_Glyph( face, glyph_index,
+ FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
+ if (!error)
+ {
+ if ( face->glyph->format != ft_image_format_bitmap ||
+ face->glyph->bitmap.pixel_mode != ft_pixel_mode_mono )
+ {
+ /* there is no monochrome glyph for this font !! */
+ error = FT_Err_Invalid_Glyph_Index;
+ }
+ else
+ {
+ /* ok, copy it */
+ FT_Glyph glyph;
+
+
+ error = FT_Get_Glyph( face->glyph, &glyph );
+ if (!error)
+ FTC_IMAGENODE_SET_GLYPH(node,glyph);
+ }
+ }
+ }
+ return error;
+ }
+
+
+ static
+ FT_Error ftc_init_gray_image( FTC_Image_Queue queue,
+ FTC_ImageNode node )
+ {
+ FT_Face face;
+ FT_Size size;
+ FT_Error error;
+
+
+ error = FTC_Manager_Lookup_Size( queue->manager,
+ &queue->size_rec,
+ &face, &size );
+ if (!error)
+ {
+ FT_UInt glyph_index = FTC_IMAGENODE_GINDEX(node);
+
+
+ error = FT_Load_Glyph( face, glyph_index,
+ FT_LOAD_RENDER );
+ if (!error)
+ {
+ if ( face->glyph->format != ft_image_format_bitmap ||
+ face->glyph->bitmap.pixel_mode != ft_pixel_mode_grays )
+ {
+ /* there is no monochrome glyph for this font !! */
+ error = FT_Err_Invalid_Glyph_Index;
+ }
+ else
+ {
+ /* ok, copy it */
+ FT_Glyph glyph;
+
+
+ error = FT_Get_Glyph( face->glyph, &glyph );
+ if (!error)
+ FTC_IMAGENODE_SET_GLYPH(node,glyph);
+ }
+ }
+ }
+ return error;
+ }
+
+
+ /*******************************************************************/
+ /*******************************************************************/
+ /***** *****/
+ /***** MONOCHROME BITMAP CALLBACKS *****/
+ /***** *****/
+ /*******************************************************************/
+ /*******************************************************************/
+
+
diff --git a/src/cache/ftcimage.h b/src/cache/ftcimage.h
new file mode 100644
index 0000000..ae00ce7
--- /dev/null
+++ b/src/cache/ftcimage.h
@@ -0,0 +1,104 @@
+#ifndef FTCIMAGE_H
+#define FTCIMAGE_H
+
+#include <cache/ftcmanag.h>
+#include <freetype/ftglyph.h>
+
+ typedef struct FTC_Image_QueueRec_* FTC_Image_Queue;
+ typedef struct FTC_Image_CacheRec_* FTC_Image_Cache;
+ typedef struct FTC_ImageNodeRec_* FTC_ImageNode;
+
+ /* types of glyph images */
+ typedef enum FTC_Image_Type_
+ {
+ ftc_image_mono = 0, /* monochrome bitmap */
+ ftc_image_grays, /* anti-aliased bitmap */
+ ftc_image_outline, /* scaled outline */
+ ftc_image_master_outline, /* original outline */
+
+ } FTC_Image_Type;
+
+
+ /* a descriptor used to describe all glyphs in a given queue */
+ typedef struct FTC_Image_Desc_
+ {
+ FTC_FaceID face_id;
+ FT_UInt pix_width;
+ FT_UInt pix_height;
+ FT_UInt image_type;
+
+ } FTC_Image_Desc;
+
+/* macros used to pack a glyph index and a queue index in a single ptr */
+#define FTC_PTR_TO_GINDEX(p) ((FT_UInt)((FT_ULong)(p) >> 16))
+#define FTC_PTR_TO_QINDEX(p) ((FT_UInt)((FT_ULong)(p) & 0xFFFF))
+#define FTC_INDICES_TO_PTR(g,q) ((FT_Pointer)(((FT_ULong)(g) << 16) | \
+ ((FT_ULong)(q) & 0xFFFF)))
+
+ typedef struct FTC_ImageNodeRec_
+ {
+ FT_ListNodeRec root1; /* root1.data contains a FT_Glyph handle */
+ FT_ListNodeRec root2; /* root2.data contains a glyph index + queue index */
+
+ } FTC_ImageNodeRec;
+
+/* macros to read/set the glyph & queue index in a FTC_ImageNode */
+#define FTC_IMAGENODE_GET_GINDEX(n) FTC_PTR_TO_GINDEX((n)->root2.data)
+#define FTC_IMAGENODE_GET_QINDEX(n) FTC_PTR_TO_QINDEX((n)->root2.data)
+#define FTC_IMAGENODE_SET_INDICES(g,q) \
+ do { (n)->root2.data = FTC_INDICES_TO_PTR(g,q); } while (0)
+
+
+ typedef struct FTC_Image_CacheRec_
+ {
+ FTC_Manager manager;
+ FT_Memory memory;
+
+ FT_ULong max_bytes; /* maximum size of cache in bytes */
+ FT_ULong num_bytes; /* current size of cache in bytes */
+
+ FT_Lru queues_lru; /* static queues lru list */
+
+ } FTC_Image_Cache;
+
+
+ /* a table of functions used to generate/manager glyph images */
+ typedef struct FTC_Image_Class_
+ {
+ FT_Error (*init_image)( FTC_Image_Queue queue,
+ FTC_Image_Node node );
+
+ void (*done_image)( FTC_Image_Queue queue,
+ FTC_Image_Node node );
+
+ FT_ULong (*size_image)( FTC_Image_Queue queue,
+ FTC_Image_Node node );
+
+ } FTC_Image_Class;
+
+
+ typedef struct FTC_Image_QueueRec_
+ {
+ FTC_Image_Cache cache;
+ FTC_Manager manager;
+ FT_Memory memory;
+ FTC_Image_Class* clazz;
+ FTC_Image_Desc descriptor;
+ FT_UInt hash_size;
+ FT_List buckets;
+
+ } FTC_Image_SubCacheRec;
+
+
+ FT_EXPORT_DEF(FT_Error) FTC_Image_Cache_New( FTC_Manager manager,
+ FT_ULong max_bytes,
+ FTC_Image_Cache *acache );
+
+ FT_EXPORT_DEF(void) FTC_Image_Cache_Done( FTC_Image_Cache cache );
+
+ FT_EXPORT_DEF(FT_Error) FTC_Image_Cache_Lookup( FTC_Image_Cache cache,
+ FTC_Image_Desc* desc,
+ FT_UInt gindex,
+ FT_Glyph *aglyph );
+
+#endif /* FTCIMAGE_H */
diff --git a/src/cache/ftcmanag.c b/src/cache/ftcmanag.c
new file mode 100644
index 0000000..98453e1
--- /dev/null
+++ b/src/cache/ftcmanag.c
@@ -0,0 +1,280 @@
+#include <cache/ftcmanag.h>
+#include <freetype/internal/ftobjs.h>
+
+#define FTC_LRU_GET_MANAGER(lru) ((FTC_Manager_Lru)lru)->manager
+
+ /*******************************************************************/
+ /*******************************************************************/
+ /***** *****/
+ /***** FACE & SIZE LRU CALLBACKS *****/
+ /***** *****/
+ /*******************************************************************/
+ /*******************************************************************/
+
+ static
+ FT_Error ftc_manager_init_face( FT_Lru lru,
+ FT_LruNode node )
+ {
+ FTC_Manager manager = FTC_LRU_GET_MANAGER(lru);
+ FT_Error error;
+
+ error = manager->request_face( (FTC_FaceID)node->key,
+ manager->request_data,
+ (FT_Face*)&node->root.data );
+ if (!error)
+ {
+ /* destroy initial size object, it will be re-created later */
+ FT_Face face = (FT_Face)node->root.data;
+ FT_Done_Size( face->size );
+ }
+ return error;
+ }
+
+
+ /* helper function for ftc_manager_done_face */
+ static
+ FT_Bool ftc_manager_size_selector( FT_Lru lru,
+ FT_LruNode node,
+ FT_Pointer data )
+ {
+ FT_UNUSED(lru);
+ return ((FT_Size)node->root.data)->face == (FT_Face)data;
+ }
+
+ static
+ void ftc_manager_done_face( FT_Lru lru,
+ FT_LruNode node )
+ {
+ FTC_Manager manager = FTC_LRU_GET_MANAGER(lru);
+ FT_Face face = (FT_Face)node->root.data;
+
+ /* we must begin by removing all sizes for the target face */
+ /* from the manager's list.. */
+ FT_Lru_Remove_Selection( manager->sizes_lru,
+ ftc_manager_size_selector,
+ face );
+
+ /* all right, we can discard the face now */
+ FT_Done_Face( face );
+ node->root.data = 0;
+ }
+
+
+ typedef struct FTC_SizeRequest_
+ {
+ FT_Face face;
+ FT_UShort width;
+ FT_UShort height;
+
+ } FTC_SizeRequest;
+
+
+ static
+ FT_Error ftc_manager_init_size( FT_Lru lru,
+ FT_LruNode node )
+ {
+ FTC_SizeRequest* size_req = (FTC_SizeRequest*)node->key;
+ FT_Size size;
+ FT_Error error;
+
+ FT_UNUSED(lru);
+
+ node->root.data = 0;
+ error = FT_New_Size( size_req->face, &size );
+ if (!error)
+ {
+ error = FT_Set_Pixel_Sizes( size_req->face,
+ size_req->width,
+ size_req->height );
+ if (error)
+ FT_Done_Size(size);
+ else
+ node->root.data = size;
+ }
+ return error;
+ }
+
+
+ static
+ void ftc_manager_done_size( FT_Lru lru,
+ FT_LruNode node )
+ {
+ FT_UNUSED(lru);
+ FT_Done_Size( (FT_Size)node->root.data );
+ }
+
+
+
+ static
+ FT_Error ftc_manager_flush_size( FT_Lru lru,
+ FT_LruNode node,
+ FT_LruKey key )
+ {
+ FTC_SizeRequest* req = (FTC_SizeRequest*)key;
+ FT_Size size = (FT_Size)node->root.data;
+ FT_Error error;
+
+ if ( size->face == req->face )
+ {
+ size->face->size = size; /* set current size */
+ error = FT_Set_Pixel_Sizes( req->face, req->width, req->height );
+ if (error)
+ FT_Done_Size( size );
+ }
+ else
+ {
+ FT_Done_Size(size);
+ node->key = key;
+ error = ftc_manager_init_size( lru, node );
+ }
+ return error;
+ }
+
+
+ static
+ FT_Bool ftc_manager_compare_size( FT_LruNode node,
+ FT_LruKey key )
+ {
+ FTC_SizeRequest* req = (FTC_SizeRequest*)key;
+ FT_Size size = (FT_Size)node->root.data;
+
+ FT_UNUSED(node);
+ return ( size->face == req->face &&
+ size->metrics.x_ppem == req->width &&
+ size->metrics.y_ppem == req->height );
+ }
+
+
+
+
+ static
+ const FT_Lru_Class ftc_face_lru_class =
+ {
+ sizeof( FTC_Manager_LruRec ),
+ ftc_manager_init_face,
+ ftc_manager_done_face,
+ 0,
+ 0
+ };
+
+
+ static
+ const FT_Lru_Class ftc_size_lru_class =
+ {
+ sizeof( FTC_Manager_LruRec ),
+ ftc_manager_init_size,
+ ftc_manager_done_size,
+ ftc_manager_flush_size,
+ ftc_manager_compare_size
+ };
+
+
+
+ FT_EXPORT_FUNC(FT_Error) FTC_Manager_New ( FT_Library library,
+ FTC_Face_Requester requester,
+ FT_Pointer req_data,
+ FTC_Manager *amanager )
+ {
+ FT_Error error;
+ FT_Memory memory = library->memory;
+ FTC_Manager manager = 0;
+
+
+ if ( ALLOC( manager, sizeof(*manager) ) )
+ goto Exit;
+
+ error = FT_Lru_New( &ftc_face_lru_class,
+ FTC_MAX_FACES,
+ memory,
+ 1, /* pre_alloc = TRUE */
+ (FT_Lru*)&manager->faces_lru );
+ if (error)
+ goto Exit;
+
+ error = FT_Lru_New( &ftc_size_lru_class,
+ FTC_MAX_SIZES,
+ memory,
+ 1, /* pre_alloc = TRUE */
+ (FT_Lru*)&manager->sizes_lru );
+ if (error)
+ goto Exit;
+
+ ((FTC_Manager_Lru)manager->faces_lru)->manager = manager;
+ ((FTC_Manager_Lru)manager->sizes_lru)->manager = manager;
+
+ manager->library = library;
+ manager->request_face = requester;
+ manager->request_data = req_data;
+ *amanager = manager;
+
+ Exit:
+ if (error && manager)
+ {
+ FT_Lru_Done( manager->sizes_lru );
+ FT_Lru_Done( manager->faces_lru );
+ FREE( manager );
+ }
+
+ return error;
+ }
+
+ FT_EXPORT_DEF(void) FTC_Manager_Done ( FTC_Manager manager )
+ {
+ FT_Memory memory = manager->library->memory;
+
+ FT_Lru_Done( manager->sizes_lru );
+ FT_Lru_Done( manager->faces_lru );
+ FREE( manager );
+ }
+
+
+ FT_EXPORT_DEF(void) FTC_Manager_Reset( FTC_Manager manager )
+ {
+ FT_Lru_Reset( manager->sizes_lru );
+ FT_Lru_Reset( manager->faces_lru );
+ }
+
+
+ FT_EXPORT_DEF(FT_Error) FTC_Manager_Lookup_Face( FTC_Manager manager,
+ FTC_FaceID face_id,
+ FT_Face *aface )
+ {
+ return FT_Lru_Lookup( manager->faces_lru,
+ (FT_LruKey)face_id,
+ (FT_Pointer*)aface );
+ }
+
+
+
+ FT_EXPORT_DEF(FT_Error) FTC_Manager_Lookup_Size( FTC_Manager manager,
+ FTC_SizeID size_id,
+ FT_Face *aface,
+ FT_Size *asize )
+ {
+ FTC_SizeRequest req;
+ FT_Error error;
+ FT_Face face;
+
+ *aface = 0;
+ *asize = 0;
+ error = FTC_Manager_Lookup_Face( manager, size_id->face_id, &face );
+ if (!error)
+ {
+ req.face = face;
+ req.width = size_id->pix_width;
+ req.height = size_id->pix_height;
+
+ error = FT_Lru_Lookup( manager->sizes_lru,
+ (FT_LruKey)&req,
+ (FT_Pointer*)asize );
+ if (!error)
+ {
+ /* select the size as the current one for this face */
+ face->size = *asize;
+ *aface = face;
+ }
+ }
+ return error;
+ }
+
+
diff --git a/src/cache/ftcmanag.h b/src/cache/ftcmanag.h
new file mode 100644
index 0000000..e000e09
--- /dev/null
+++ b/src/cache/ftcmanag.h
@@ -0,0 +1,65 @@
+#ifndef FTCMANAG_H
+#define FTCMANAG_H
+
+#include <cache/ftlru.h>
+
+#define FTC_MAX_FACES 4
+#define FTC_MAX_SIZES 8
+
+ typedef FT_Pointer FTC_FaceID;
+
+ typedef struct FTC_SizeRec_
+ {
+ FTC_FaceID face_id;
+ FT_UShort pix_width;
+ FT_UShort pix_height;
+
+ } FTC_SizeRec, *FTC_SizeID;
+
+
+ typedef FT_Error (*FTC_Face_Requester)( FTC_FaceID face_id,
+ FT_Pointer data,
+ FT_Face *aface );
+
+
+ typedef struct FTC_ManagerRec_* FTC_Manager;
+
+ typedef struct FTC_Manager_LruRec_
+ {
+ FT_LruRec root;
+ FTC_Manager manager;
+
+ } FTC_Manager_LruRec, *FTC_Manager_Lru;
+
+
+ typedef struct FTC_ManagerRec_
+ {
+ FT_Library library;
+ FT_Lru faces_lru;
+ FT_Lru sizes_lru;
+
+ FT_Pointer request_data;
+ FTC_Face_Requester request_face;
+
+ } FTC_ManagerRec;
+
+
+ FT_EXPORT_DEF(FT_Error) FTC_Manager_New ( FT_Library library,
+ FTC_Face_Requester requester,
+ FT_Pointer req_data,
+ FTC_Manager *amanager );
+
+ FT_EXPORT_DEF(void) FTC_Manager_Done ( FTC_Manager manager );
+
+ FT_EXPORT_DEF(void) FTC_Manager_Reset( FTC_Manager manager );
+
+ FT_EXPORT_DEF(FT_Error) FTC_Manager_Lookup_Face( FTC_Manager manager,
+ FTC_FaceID face_id,
+ FT_Face *aface );
+
+ FT_EXPORT_DEF(FT_Error) FTC_Manager_Lookup_Size( FTC_Manager manager,
+ FTC_SizeID size_id,
+ FT_Face *aface,
+ FT_Size *asize );
+
+#endif /* FTCMANAG_H */
diff --git a/src/cache/ftlru.c b/src/cache/ftlru.c
new file mode 100644
index 0000000..d9a619a
--- /dev/null
+++ b/src/cache/ftlru.c
@@ -0,0 +1,263 @@
+#include <cache/ftlru.h>
+#include <freetype/internal/ftobjs.h>
+#include <freetype/internal/ftlist.h>
+
+ static
+ void lru_build_free_list( FT_LruNode nodes,
+ FT_UInt count,
+ FT_List free_list )
+ {
+ FT_LruNode node = nodes;
+ FT_LruNode limit = node + count;
+
+ free_list->head = free_list->tail = 0;
+ for ( ; node < limit; node++ )
+ FT_List_Add( free_list, (FT_ListNode)node );
+ }
+
+
+ FT_EXPORT_FUNC(FT_Error) FT_Lru_New ( const FT_Lru_Class* clazz,
+ FT_UInt max_elements,
+ FT_Memory memory,
+ FT_Bool pre_alloc,
+ FT_Lru *alru )
+ {
+ FT_Error error;
+ FT_Lru lru;
+
+ *alru = 0;
+ if ( !ALLOC( lru, sizeof(*lru) ) )
+ {
+ if (pre_alloc)
+ {
+ /* allocate static array of lru list nodes */
+ if ( ALLOC_ARRAY( lru->nodes, max_elements, FT_LruNodeRec ) )
+ {
+ FREE( lru );
+ goto Exit;
+ }
+
+ /* build the 'free_nodes' list from the array */
+ lru_build_free_list( lru->nodes, max_elements, &lru->free_nodes );
+ }
+
+ /* initialize common fields */
+ lru->clazz = (FT_Lru_Class*)clazz;
+ lru->max_elements = max_elements;
+ lru->memory = memory;
+ *alru = lru;
+ }
+ Exit:
+ return error;
+ }
+
+
+
+ FT_EXPORT_DEF(void) FT_Lru_Reset ( FT_Lru lru )
+ {
+ FT_ListNode node = lru->elements.head;
+ FT_Lru_Class* clazz = lru->clazz;
+ FT_Memory memory = lru->memory;
+
+ while (node)
+ {
+ FT_ListNode next = node->next;
+
+ clazz->done_element( lru, (FT_LruNode)node );
+ if (!lru->nodes)
+ FREE(node);
+
+ node = next;
+ }
+
+ /* rebuild free list if necessary */
+ if (lru->nodes)
+ lru_build_free_list( lru->nodes, lru->max_elements, &lru->free_nodes );
+ lru->elements.head = lru->elements.tail = 0;
+ lru->num_elements = 0;
+ }
+
+
+
+ FT_EXPORT_DEF(void) FT_Lru_Done ( FT_Lru lru )
+ {
+ FT_Memory memory = lru->memory;
+
+ FT_Lru_Reset(lru);
+ FREE(lru);
+ }
+
+
+
+ FT_EXPORT_DEF(FT_Error) FT_Lru_Lookup_Node( FT_Lru lru,
+ FT_LruKey key,
+ FT_LruNode* anode )
+ {
+ FT_Error error = 0;
+ FT_ListNode node = lru->elements.head;
+ FT_Lru_Class* clazz = lru->clazz;
+ FT_LruNode found = 0;
+ FT_Memory memory = lru->memory;
+
+ if (clazz->compare_element)
+ {
+ for ( ; node; node = node->next )
+ if (clazz->compare_element( (FT_LruNode)node, key ))
+ {
+ found = (FT_LruNode)node;
+ break;
+ }
+ }
+ else
+ {
+ for ( ; node; node = node->next )
+ if (((FT_LruNode)node)->key == key)
+ {
+ found = (FT_LruNode)node;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ /* we didn't find the relevant element. We will now try */
+ /* to create a new one.. */
+ if ( lru->num_elements >= lru->max_elements )
+ {
+ /* this lru list is full, we will now flush */
+ /* the oldest node */
+ FT_LruNode lru_node;
+
+
+ node = lru->elements.tail;
+ lru_node = (FT_LruNode)node;
+
+ if (clazz->flush_element)
+ error = clazz->flush_element( lru, lru_node, key );
+ else
+ {
+ clazz->done_element( lru, lru_node );
+ lru_node->key = key;
+ node->data = 0;
+ error = clazz->init_element( lru, lru_node );
+ }
+
+ if (!error)
+ {
+ /* now, move element to top of list */
+ FT_List_Up( &lru->elements, node );
+ }
+ else
+ {
+ /* in case of error, the node must be discarded */
+ FT_List_Remove( &lru->elements, node );
+ lru->num_elements--;
+
+ if (lru->nodes)
+ FT_List_Insert( &lru->free_nodes, node );
+ else
+ FREE( lru_node );
+ }
+ }
+ else
+ {
+ FT_LruNode lru_node;
+
+ /* create a new lru list node, then the element for it */
+ if (lru->nodes)
+ {
+ node = lru->free_nodes.head;
+ lru_node = (FT_LruNode)node;
+ lru_node->key = key;
+
+ error = clazz->init_element( lru, lru_node );
+ if (error)
+ goto Exit;
+
+ FT_List_Remove( &lru->free_nodes, node );
+ }
+ else
+ {
+ if ( ALLOC( lru_node, sizeof(*lru_node) ) )
+ goto Exit;
+
+ lru_node->key = key;
+ error = clazz->init_element( lru, lru_node );
+ if (error)
+ {
+ FREE( lru_node );
+ goto Exit;
+ }
+ }
+
+ found = lru_node;
+ node = (FT_ListNode)lru_node;
+ FT_List_Insert( &lru->elements, node );
+ lru->num_elements++;
+ }
+ }
+
+ Exit:
+ *anode = found;
+ return error;
+ }
+
+
+ FT_EXPORT_DEF(FT_Error) FT_Lru_Lookup( FT_Lru lru,
+ FT_LruKey key,
+ FT_Pointer *aobject )
+ {
+ FT_Error error;
+ FT_LruNode node;
+
+ *aobject = 0;
+ error = FT_Lru_Lookup_Node( lru, key, &node );
+ if (!error)
+ *aobject = node->root.data;
+
+ return error;
+ }
+
+
+ FT_EXPORT_FUNC(void) FT_Lru_Remove_Node( FT_Lru lru,
+ FT_LruNode node )
+ {
+ if (lru->num_elements > 0)
+ {
+ FT_List_Remove( &lru->elements, (FT_ListNode)node );
+ lru->clazz->done_element( lru, node );
+
+ if (lru->nodes)
+ FT_List_Insert( &lru->free_nodes, (FT_ListNode)node );
+ else
+ {
+ FT_Memory memory = lru->memory;
+ FREE(node);
+ }
+ lru->num_elements--;
+ }
+ }
+
+
+ FT_EXPORT_FUNC(void) FT_Lru_Remove_Selection( FT_Lru lru,
+ FT_Lru_Selector selector,
+ FT_Pointer data )
+ {
+ if (lru->num_elements > 0)
+ {
+ FT_ListNode node = lru->elements.head;
+ FT_ListNode next;
+
+ while (node)
+ {
+ next = node->next;
+ if ( selector( lru, (FT_LruNode)node, data ) )
+ {
+ /* remove this element from the list, and destroy it */
+ FT_Lru_Remove_Node( lru, (FT_LruNode)node );
+ }
+ node = next;
+ }
+ }
+ }
+
diff --git a/src/cache/ftlru.h b/src/cache/ftlru.h
new file mode 100644
index 0000000..c61f89b
--- /dev/null
+++ b/src/cache/ftlru.h
@@ -0,0 +1,83 @@
+#ifndef FTLRU_H
+#define FTLRU_H
+
+#include <freetype/freetype.h>
+
+
+ typedef FT_Pointer FT_LruKey;
+
+
+ typedef struct FT_LruNodeRec_
+ {
+ FT_ListNodeRec root;
+ FT_LruKey key;
+
+ } FT_LruNodeRec, *FT_LruNode;
+
+
+ typedef struct FT_LruRec_* FT_Lru;
+
+ typedef struct FT_Lru_Class_
+ {
+ FT_UInt lru_size; /* object size in bytes */
+
+ FT_Error (*init_element)( FT_Lru lru,
+ FT_LruNode node );
+
+ void (*done_element)( FT_Lru lru,
+ FT_LruNode node );
+
+ FT_Error (*flush_element)( FT_Lru lru,
+ FT_LruNode node,
+ FT_LruKey new_key );
+
+ FT_Bool (*compare_element)( FT_LruNode node,
+ FT_LruKey key );
+ } FT_Lru_Class;
+
+
+ typedef FT_Bool (*FT_Lru_Selector)( FT_Lru lru,
+ FT_LruNode node,
+ FT_Pointer data );
+
+ typedef struct FT_LruRec_
+ {
+ FT_Lru_Class* clazz;
+ FT_UInt max_elements;
+ FT_UInt num_elements;
+ FT_ListRec elements;
+ FT_Memory memory;
+
+ /* the following fields are only meaningful for static lru containers */
+ FT_ListRec free_nodes;
+ FT_LruNode nodes;
+
+ } FT_LruRec;
+
+
+ FT_EXPORT_DEF(FT_Error) FT_Lru_New ( const FT_Lru_Class* clazz,
+ FT_UInt max_elements,
+ FT_Memory memory,
+ FT_Bool pre_alloc,
+ FT_Lru *alru );
+
+ FT_EXPORT_DEF(void) FT_Lru_Reset ( FT_Lru lru );
+
+ FT_EXPORT_DEF(void) FT_Lru_Done ( FT_Lru lru );
+
+ FT_EXPORT_DEF(FT_Error) FT_Lru_Lookup_Node( FT_Lru lru,
+ FT_LruKey key,
+ FT_LruNode* anode );
+
+ FT_EXPORT_DEF(FT_Error) FT_Lru_Lookup( FT_Lru lru,
+ FT_LruKey key,
+ FT_Pointer *aobject );
+
+ FT_EXPORT_DEF(void) FT_Lru_Remove_Node( FT_Lru lru,
+ FT_LruNode node );
+
+ FT_EXPORT_DEF(void) FT_Lru_Remove_Selection( FT_Lru lru,
+ FT_Lru_Selector selector,
+ FT_Pointer data );
+
+#endif /* FTLRU_H */