This section describes how to use the font cache to handle binary fonts in TouchGFX.
Read first about binary fonts.
Recall that using binary fonts require the whole font to be loaded to memory. This can in some cases be undesireable if the font is large, e.g. with large Chinese fonts.
Font caching allows an application to load from external memory only the letters required to show a string. This means that the whole font does not need to reside in the addressable flash or RAM, but can be stored on a larger file system.
In the drawing below, the compiled-in font, Font2, has been replaced
by the font cache. When TouchGFX is drawing a text, that uses Font2,
TouchGFX will find the pointer to the
CachedFont object in the
font table. This special font will lookup the letters in the FontCache
The CachedFont is setup with a pointer to the linked-in font (Font2 above). When TouchGFX asks the CachedFont for a specific letter, the cachedFont will first look in the normal Font it is replacing (Font2). This font may be an empty font, but can also be a "normal" font containing a selection of some often used letters. If the font does not contain the required letter, the CachedFont will look into the FontCache to see if the letter has been loaded from the file system.
This principle limits the amount of letters that must be cached, as we do not need to cache letters already found in the normal font.
Before the application can install a CachedFont it must also create a FontCache, a memory buffer, and a file system reader object:
The FontCache must be linked to the buffer and the reader:
Now the application can setup the font cache, initialize the CachedFont and pass it to TouchGFX.
The font cache requires a TextId to initialize a CachedFont object. The TextId is used to lookup the font that the CachedFont must point to. This secures that you are replacing the font used by the text that you have on your display:
The code above can be put anywhere in the application. If the cached font is only used in a specific view, this view can be a good place to insert the code.
The font cache is still empty. Before we can show any letters they must be read from the font cache. This is done by passing an array of unicodes (a string) to the font cache. In this example we just pass the text from T_SINGLEUSEID1.
The font cache will load the letters found in the
str array through the
The read unicodes will be linked to the font that is used by the
The application is responsible for configuring the reader object to load from the correct file.
For languages that convert sequences of unicodes to other unicodes before displaying (e.g. Arabic and Devanagari) the above method is not good. It caches the original unicodes and not the unicodes that are displayed after conversion. This method will convert the given unicodes and cache the required unicodes (after conversion):
The font cache can calculate the current amount used memory:
Some fonts use a GSUB table while rendering. These are only a few fonts for eastern languages, e.g. Devanagari fonts. The GSUB tables allow the font system to reorder characters and substitute sequences of characters for other "combination" characters.
The Font Cache can load this GSUB table from the file system. If it is not loaded, the font is not displayed correctly as the GSUB table is then not available to the text rendering system.
The GSUB table is loaded by supplying an extra argument when initializing the cached font:
The FileDataReader class used in the above example code is not included in TouchGFX as it is dependant on the operating system you are using.
Here is an example for normal "stdio" compatible file systems.
The FileDataReader class implements the FontDataReader interface from FontCache.hpp: