1 /****************************************************************************
5 * FreeType Cache subsystem (specification).
7 * Copyright (C) 1996-2020 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
23 #include <freetype/ftglyph.h>
29 /**************************************************************************
38 * How to cache face, size, and glyph data with FreeType~2.
41 * This section describes the FreeType~2 cache sub-system, which is used
42 * to limit the number of concurrently opened @FT_Face and @FT_Size
43 * objects, as well as caching information like character maps and glyph
44 * images while limiting their maximum memory usage.
46 * Note that all types and functions begin with the `FTC_` prefix.
48 * The cache is highly portable and thus doesn't know anything about the
49 * fonts installed on your system, or how to access them. This implies
50 * the following scheme:
52 * First, available or installed font faces are uniquely identified by
53 * @FTC_FaceID values, provided to the cache by the client. Note that
54 * the cache only stores and compares these values, and doesn't try to
55 * interpret them in any way.
57 * Second, the cache calls, only when needed, a client-provided function
58 * to convert an @FTC_FaceID into a new @FT_Face object. The latter is
59 * then completely managed by the cache, including its termination
60 * through @FT_Done_Face. To monitor termination of face objects, the
61 * finalizer callback in the `generic` field of the @FT_Face object can
62 * be used, which might also be used to store the @FTC_FaceID of the
65 * Clients are free to map face IDs to anything else. The most simple
66 * usage is to associate them to a (pathname,face_index) pair that is
67 * used to call @FT_New_Face. However, more complex schemes are also
70 * Note that for the cache to work correctly, the face ID values must be
71 * **persistent**, which means that the contents they point to should not
72 * change at runtime, or that their value should not become invalid.
74 * If this is unavoidable (e.g., when a font is uninstalled at runtime),
75 * you should call @FTC_Manager_RemoveFaceID as soon as possible, to let
76 * the cache get rid of any references to the old @FTC_FaceID it may keep
77 * internally. Failure to do so will lead to incorrect behaviour or even
80 * To use the cache, start with calling @FTC_Manager_New to create a new
81 * @FTC_Manager object, which models a single cache instance. You can
82 * then look up @FT_Face and @FT_Size objects with
83 * @FTC_Manager_LookupFace and @FTC_Manager_LookupSize, respectively.
85 * If you want to use the charmap caching, call @FTC_CMapCache_New, then
86 * later use @FTC_CMapCache_Lookup to perform the equivalent of
87 * @FT_Get_Char_Index, only much faster.
89 * If you want to use the @FT_Glyph caching, call @FTC_ImageCache, then
90 * later use @FTC_ImageCache_Lookup to retrieve the corresponding
91 * @FT_Glyph objects from the cache.
93 * If you need lots of small bitmaps, it is much more memory efficient to
94 * call @FTC_SBitCache_New followed by @FTC_SBitCache_Lookup. This
95 * returns @FTC_SBitRec structures, which are used to store small bitmaps
96 * directly. (A small bitmap is one whose metrics and dimensions all fit
97 * into 8-bit integers).
99 * We hope to also provide a kerning cache in the near future.
110 * FTC_Manager_LookupFace
111 * FTC_Manager_LookupSize
112 * FTC_Manager_RemoveFaceID
119 * FTC_ImageCache_Lookup
124 * FTC_SBitCache_Lookup
128 * FTC_CMapCache_Lookup
130 *************************************************************************/
133 /*************************************************************************/
134 /*************************************************************************/
135 /*************************************************************************/
137 /***** BASIC TYPE DEFINITIONS *****/
139 /*************************************************************************/
140 /*************************************************************************/
141 /*************************************************************************/
144 /**************************************************************************
150 * An opaque pointer type that is used to identity face objects. The
151 * contents of such objects is application-dependent.
153 * These pointers are typically used to point to a user-defined structure
154 * containing a font file path, and face index.
157 * Never use `NULL` as a valid @FTC_FaceID.
159 * Face IDs are passed by the client to the cache manager that calls,
160 * when needed, the @FTC_Face_Requester to translate them into new
163 * If the content of a given face ID changes at runtime, or if the value
164 * becomes invalid (e.g., when uninstalling a font), you should
165 * immediately call @FTC_Manager_RemoveFaceID before any other cache
168 * Failure to do so will result in incorrect behaviour or even memory
171 typedef FT_Pointer FTC_FaceID;
174 /**************************************************************************
180 * A callback function provided by client applications. It is used by
181 * the cache manager to translate a given @FTC_FaceID into a new valid
182 * @FT_Face object, on demand.
186 * The face ID to resolve.
189 * A handle to a FreeType library object.
192 * Application-provided request data (see note below).
196 * A new @FT_Face handle.
199 * FreeType error code. 0~means success.
202 * The third parameter `req_data` is the same as the one passed by the
203 * client when @FTC_Manager_New is called.
205 * The face requester should not perform funny things on the returned
206 * face object, like creating a new @FT_Size for it, or setting a
207 * transformation through @FT_Set_Transform!
210 (*FTC_Face_Requester)( FTC_FaceID face_id,
218 /*************************************************************************/
219 /*************************************************************************/
220 /*************************************************************************/
222 /***** CACHE MANAGER OBJECT *****/
224 /*************************************************************************/
225 /*************************************************************************/
226 /*************************************************************************/
229 /**************************************************************************
235 * This object corresponds to one instance of the cache-subsystem. It is
236 * used to cache one or more @FT_Face objects, along with corresponding
239 * The manager intentionally limits the total number of opened @FT_Face
240 * and @FT_Size objects to control memory usage. See the `max_faces` and
241 * `max_sizes` parameters of @FTC_Manager_New.
243 * The manager is also used to cache 'nodes' of various types while
244 * limiting their total memory usage.
246 * All limitations are enforced by keeping lists of managed objects in
247 * most-recently-used order, and flushing old nodes to make room for new
250 typedef struct FTC_ManagerRec_* FTC_Manager;
253 /**************************************************************************
259 * An opaque handle to a cache node object. Each cache node is
260 * reference-counted. A node with a count of~0 might be flushed out of a
261 * full cache whenever a lookup request is performed.
263 * If you look up nodes, you have the ability to 'acquire' them, i.e., to
264 * increment their reference count. This will prevent the node from
265 * being flushed out of the cache until you explicitly 'release' it (see
268 * See also @FTC_SBitCache_Lookup and @FTC_ImageCache_Lookup.
270 typedef struct FTC_NodeRec_* FTC_Node;
273 /**************************************************************************
279 * Create a new cache manager.
283 * The parent FreeType library handle to use.
286 * Maximum number of opened @FT_Face objects managed by this cache
287 * instance. Use~0 for defaults.
290 * Maximum number of opened @FT_Size objects managed by this cache
291 * instance. Use~0 for defaults.
294 * Maximum number of bytes to use for cached data nodes. Use~0 for
295 * defaults. Note that this value does not account for managed
296 * @FT_Face and @FT_Size objects.
299 * An application-provided callback used to translate face IDs into
300 * real @FT_Face objects.
303 * A generic pointer that is passed to the requester each time it is
304 * called (see @FTC_Face_Requester).
308 * A handle to a new manager object. 0~in case of failure.
311 * FreeType error code. 0~means success.
313 FT_EXPORT( FT_Error )
314 FTC_Manager_New( FT_Library library,
318 FTC_Face_Requester requester,
320 FTC_Manager *amanager );
323 /**************************************************************************
329 * Empty a given cache manager. This simply gets rid of all the
330 * currently cached @FT_Face and @FT_Size objects within the manager.
334 * A handle to the manager.
337 FTC_Manager_Reset( FTC_Manager manager );
340 /**************************************************************************
346 * Destroy a given manager after emptying it.
350 * A handle to the target cache manager object.
353 FTC_Manager_Done( FTC_Manager manager );
356 /**************************************************************************
359 * FTC_Manager_LookupFace
362 * Retrieve the @FT_Face object that corresponds to a given face ID
363 * through a cache manager.
367 * A handle to the cache manager.
370 * The ID of the face object.
374 * A handle to the face object.
377 * FreeType error code. 0~means success.
380 * The returned @FT_Face object is always owned by the manager. You
381 * should never try to discard it yourself.
383 * The @FT_Face object doesn't necessarily have a current size object
384 * (i.e., face->size can be~0). If you need a specific 'font size', use
385 * @FTC_Manager_LookupSize instead.
387 * Never change the face's transformation matrix (i.e., never call the
388 * @FT_Set_Transform function) on a returned face! If you need to
389 * transform glyphs, do it yourself after glyph loading.
391 * When you perform a lookup, out-of-memory errors are detected _within_
392 * the lookup and force incremental flushes of the cache until enough
393 * memory is released for the lookup to succeed.
395 * If a lookup fails with `FT_Err_Out_Of_Memory` the cache has already
396 * been completely flushed, and still no memory was available for the
399 FT_EXPORT( FT_Error )
400 FTC_Manager_LookupFace( FTC_Manager manager,
405 /**************************************************************************
411 * A structure used to describe a given character size in either pixels
412 * or points to the cache manager. See @FTC_Manager_LookupSize.
416 * The source face ID.
419 * The character width.
422 * The character height.
425 * A Boolean. If 1, the `width` and `height` fields are interpreted as
426 * integer pixel character sizes. Otherwise, they are expressed as
430 * Only used when `pixel` is value~0 to indicate the horizontal
434 * Only used when `pixel` is value~0 to indicate the vertical
438 * This type is mainly used to retrieve @FT_Size objects through the
441 typedef struct FTC_ScalerRec_
453 /**************************************************************************
459 * A handle to an @FTC_ScalerRec structure.
461 typedef struct FTC_ScalerRec_* FTC_Scaler;
464 /**************************************************************************
467 * FTC_Manager_LookupSize
470 * Retrieve the @FT_Size object that corresponds to a given
471 * @FTC_ScalerRec pointer through a cache manager.
475 * A handle to the cache manager.
482 * A handle to the size object.
485 * FreeType error code. 0~means success.
488 * The returned @FT_Size object is always owned by the manager. You
489 * should never try to discard it by yourself.
491 * You can access the parent @FT_Face object simply as `size->face` if
492 * you need it. Note that this object is also owned by the manager.
495 * When you perform a lookup, out-of-memory errors are detected _within_
496 * the lookup and force incremental flushes of the cache until enough
497 * memory is released for the lookup to succeed.
499 * If a lookup fails with `FT_Err_Out_Of_Memory` the cache has already
500 * been completely flushed, and still no memory is available for the
503 FT_EXPORT( FT_Error )
504 FTC_Manager_LookupSize( FTC_Manager manager,
509 /**************************************************************************
515 * Decrement a cache node's internal reference count. When the count
516 * reaches 0, it is not destroyed but becomes eligible for subsequent
521 * The cache node handle.
524 * The cache manager handle.
527 FTC_Node_Unref( FTC_Node node,
528 FTC_Manager manager );
531 /**************************************************************************
534 * FTC_Manager_RemoveFaceID
537 * A special function used to indicate to the cache manager that a given
538 * @FTC_FaceID is no longer valid, either because its content changed, or
539 * because it was deallocated or uninstalled.
543 * The cache manager handle.
546 * The @FTC_FaceID to be removed.
549 * This function flushes all nodes from the cache corresponding to this
550 * `face_id`, with the exception of nodes with a non-null reference
553 * Such nodes are however modified internally so as to never appear in
554 * later lookups with the same `face_id` value, and to be immediately
555 * destroyed when released by all their users.
559 FTC_Manager_RemoveFaceID( FTC_Manager manager,
560 FTC_FaceID face_id );
563 /**************************************************************************
569 * An opaque handle used to model a charmap cache. This cache is to hold
570 * character codes -> glyph indices mappings.
573 typedef struct FTC_CMapCacheRec_* FTC_CMapCache;
576 /**************************************************************************
582 * Create a new charmap cache.
586 * A handle to the cache manager.
590 * A new cache handle. `NULL` in case of error.
593 * FreeType error code. 0~means success.
596 * Like all other caches, this one will be destroyed with the cache
600 FT_EXPORT( FT_Error )
601 FTC_CMapCache_New( FTC_Manager manager,
602 FTC_CMapCache *acache );
605 /**************************************************************************
608 * FTC_CMapCache_Lookup
611 * Translate a character code into a glyph index, using the charmap
616 * A charmap cache handle.
619 * The source face ID.
622 * The index of the charmap in the source face. Any negative value
623 * means to use the cache @FT_Face's default charmap.
626 * The character code (in the corresponding charmap).
629 * Glyph index. 0~means 'no glyph'.
633 FTC_CMapCache_Lookup( FTC_CMapCache cache,
636 FT_UInt32 char_code );
639 /*************************************************************************/
640 /*************************************************************************/
641 /*************************************************************************/
643 /***** IMAGE CACHE OBJECT *****/
645 /*************************************************************************/
646 /*************************************************************************/
647 /*************************************************************************/
650 /**************************************************************************
656 * A structure used to model the type of images in a glyph cache.
663 * The width in pixels.
666 * The height in pixels.
669 * The load flags, as in @FT_Load_Glyph.
672 typedef struct FTC_ImageTypeRec_
682 /**************************************************************************
688 * A handle to an @FTC_ImageTypeRec structure.
691 typedef struct FTC_ImageTypeRec_* FTC_ImageType;
697 #define FTC_IMAGE_TYPE_COMPARE( d1, d2 ) \
698 ( (d1)->face_id == (d2)->face_id && \
699 (d1)->width == (d2)->width && \
700 (d1)->flags == (d2)->flags )
703 /**************************************************************************
709 * A handle to a glyph image cache object. They are designed to hold
710 * many distinct glyph images while not exceeding a certain memory
713 typedef struct FTC_ImageCacheRec_* FTC_ImageCache;
716 /**************************************************************************
722 * Create a new glyph image cache.
726 * The parent manager for the image cache.
730 * A handle to the new glyph image cache object.
733 * FreeType error code. 0~means success.
735 FT_EXPORT( FT_Error )
736 FTC_ImageCache_New( FTC_Manager manager,
737 FTC_ImageCache *acache );
740 /**************************************************************************
743 * FTC_ImageCache_Lookup
746 * Retrieve a given glyph image from a glyph image cache.
750 * A handle to the source glyph image cache.
753 * A pointer to a glyph image type descriptor.
756 * The glyph index to retrieve.
760 * The corresponding @FT_Glyph object. 0~in case of failure.
763 * Used to return the address of the corresponding cache node after
764 * incrementing its reference count (see note below).
767 * FreeType error code. 0~means success.
770 * The returned glyph is owned and managed by the glyph image cache.
771 * Never try to transform or discard it manually! You can however create
772 * a copy with @FT_Glyph_Copy and modify the new one.
774 * If `anode` is _not_ `NULL`, it receives the address of the cache node
775 * containing the glyph image, after increasing its reference count.
776 * This ensures that the node (as well as the @FT_Glyph) will always be
777 * kept in the cache until you call @FTC_Node_Unref to 'release' it.
779 * If `anode` is `NULL`, the cache node is left unchanged, which means
780 * that the @FT_Glyph could be flushed out of the cache on the next call
781 * to one of the caching sub-system APIs. Don't assume that it is
784 FT_EXPORT( FT_Error )
785 FTC_ImageCache_Lookup( FTC_ImageCache cache,
792 /**************************************************************************
795 * FTC_ImageCache_LookupScaler
798 * A variant of @FTC_ImageCache_Lookup that uses an @FTC_ScalerRec to
799 * specify the face ID and its size.
803 * A handle to the source glyph image cache.
806 * A pointer to a scaler descriptor.
809 * The corresponding load flags.
812 * The glyph index to retrieve.
816 * The corresponding @FT_Glyph object. 0~in case of failure.
819 * Used to return the address of the corresponding cache node after
820 * incrementing its reference count (see note below).
823 * FreeType error code. 0~means success.
826 * The returned glyph is owned and managed by the glyph image cache.
827 * Never try to transform or discard it manually! You can however create
828 * a copy with @FT_Glyph_Copy and modify the new one.
830 * If `anode` is _not_ `NULL`, it receives the address of the cache node
831 * containing the glyph image, after increasing its reference count.
832 * This ensures that the node (as well as the @FT_Glyph) will always be
833 * kept in the cache until you call @FTC_Node_Unref to 'release' it.
835 * If `anode` is `NULL`, the cache node is left unchanged, which means
836 * that the @FT_Glyph could be flushed out of the cache on the next call
837 * to one of the caching sub-system APIs. Don't assume that it is
840 * Calls to @FT_Set_Char_Size and friends have no effect on cached
841 * glyphs; you should always use the FreeType cache API instead.
843 FT_EXPORT( FT_Error )
844 FTC_ImageCache_LookupScaler( FTC_ImageCache cache,
852 /**************************************************************************
858 * A handle to a small bitmap descriptor. See the @FTC_SBitRec structure
861 typedef struct FTC_SBitRec_* FTC_SBit;
864 /**************************************************************************
870 * A very compact structure used to describe a small glyph bitmap.
874 * The bitmap width in pixels.
877 * The bitmap height in pixels.
880 * The horizontal distance from the pen position to the left bitmap
881 * border (a.k.a. 'left side bearing', or 'lsb').
884 * The vertical distance from the pen position (on the baseline) to the
885 * upper bitmap border (a.k.a. 'top side bearing'). The distance is
886 * positive for upwards y~coordinates.
889 * The format of the glyph bitmap (monochrome or gray).
892 * Maximum gray level value (in the range 1 to~255).
895 * The number of bytes per bitmap line. May be positive or negative.
898 * The horizontal advance width in pixels.
901 * The vertical advance height in pixels.
904 * A pointer to the bitmap pixels.
906 typedef struct FTC_SBitRec_
924 /**************************************************************************
930 * A handle to a small bitmap cache. These are special cache objects
931 * used to store small glyph bitmaps (and anti-aliased pixmaps) in a much
932 * more efficient way than the traditional glyph image cache implemented
933 * by @FTC_ImageCache.
935 typedef struct FTC_SBitCacheRec_* FTC_SBitCache;
938 /**************************************************************************
944 * Create a new cache to store small glyph bitmaps.
948 * A handle to the source cache manager.
952 * A handle to the new sbit cache. `NULL` in case of error.
955 * FreeType error code. 0~means success.
957 FT_EXPORT( FT_Error )
958 FTC_SBitCache_New( FTC_Manager manager,
959 FTC_SBitCache *acache );
962 /**************************************************************************
965 * FTC_SBitCache_Lookup
968 * Look up a given small glyph bitmap in a given sbit cache and 'lock' it
969 * to prevent its flushing from the cache until needed.
973 * A handle to the source sbit cache.
976 * A pointer to the glyph image type descriptor.
983 * A handle to a small bitmap descriptor.
986 * Used to return the address of the corresponding cache node after
987 * incrementing its reference count (see note below).
990 * FreeType error code. 0~means success.
993 * The small bitmap descriptor and its bit buffer are owned by the cache
994 * and should never be freed by the application. They might as well
995 * disappear from memory on the next cache lookup, so don't treat them as
998 * The descriptor's `buffer` field is set to~0 to indicate a missing
1001 * If `anode` is _not_ `NULL`, it receives the address of the cache node
1002 * containing the bitmap, after increasing its reference count. This
1003 * ensures that the node (as well as the image) will always be kept in
1004 * the cache until you call @FTC_Node_Unref to 'release' it.
1006 * If `anode` is `NULL`, the cache node is left unchanged, which means
1007 * that the bitmap could be flushed out of the cache on the next call to
1008 * one of the caching sub-system APIs. Don't assume that it is
1011 FT_EXPORT( FT_Error )
1012 FTC_SBitCache_Lookup( FTC_SBitCache cache,
1019 /**************************************************************************
1022 * FTC_SBitCache_LookupScaler
1025 * A variant of @FTC_SBitCache_Lookup that uses an @FTC_ScalerRec to
1026 * specify the face ID and its size.
1030 * A handle to the source sbit cache.
1033 * A pointer to the scaler descriptor.
1036 * The corresponding load flags.
1043 * A handle to a small bitmap descriptor.
1046 * Used to return the address of the corresponding cache node after
1047 * incrementing its reference count (see note below).
1050 * FreeType error code. 0~means success.
1053 * The small bitmap descriptor and its bit buffer are owned by the cache
1054 * and should never be freed by the application. They might as well
1055 * disappear from memory on the next cache lookup, so don't treat them as
1058 * The descriptor's `buffer` field is set to~0 to indicate a missing
1061 * If `anode` is _not_ `NULL`, it receives the address of the cache node
1062 * containing the bitmap, after increasing its reference count. This
1063 * ensures that the node (as well as the image) will always be kept in
1064 * the cache until you call @FTC_Node_Unref to 'release' it.
1066 * If `anode` is `NULL`, the cache node is left unchanged, which means
1067 * that the bitmap could be flushed out of the cache on the next call to
1068 * one of the caching sub-system APIs. Don't assume that it is
1071 FT_EXPORT( FT_Error )
1072 FTC_SBitCache_LookupScaler( FTC_SBitCache cache,
1074 FT_ULong load_flags,
1084 #endif /* FTCACHE_H_ */