NAME
cachechars, agefont, loadchar, Subfont, Fontchar, Font – font utilities |
SYNOPSIS
#include <u.h> #include <libc.h> #include <draw.h>
int cachechars(Font *f, char **s, Rune **r, ushort *c, int max,
|
DESCRIPTION
A Font may contain too many characters to hold in memory simultaneously.
The graphics library and draw device (see draw(3)) cooperate to
solve this problem by maintaining a cache of recently used character
images. The details of this cooperation need not be known by most
programs: initdraw and its associated
font variable, openfont, stringwidth, string, and freefont are
sufficient for most purposes. The routines described below are
used internally by the graphics library to maintain the font cache.
A Subfont is a set of images for a contiguous range of characters,
stored as a single image with the characters placed side–by–side
on a common baseline. It is described by the following data structures.
A Font consists of an overall height and ascent and a collection
of subfonts together with the ranges of runes (see utf(6)) they
represent. Fonts are described by the following structures.
The characters are taken from the subfont starting at character number offset (usually zero) in the subfont, permitting selection of parts of subfonts. Thus the image for rune r is found in position r–min+offset of the subfont. For each font, the library, with support from the graphics server, maintains a cache of subfonts and a cache of recently used character images. The subf and cache fields are used by the library to maintain these caches. The width of a font is the maximum of the horizontal extents of the characters in the cache. String draws a string by loading the cache and emitting a sequence of cache indices to draw. Cachechars guarantees the images for the characters pointed to by *s or *r (one of these must be nil in each call) are in the cache of f. It calls loadchar to put missing characters into the cache. Cachechars translates the character string into a set of cache indices which it loads into the array c, up to a maximum of n indices or the length of the string. Cachechars returns in c the number of cache indices emitted, updates *s to point to the next character to be processed, and sets *widp to the total width of the characters processed. Cachechars may return before the end of the string if it cannot proceed without destroying active data in the caches. If it needs to load a new subfont, it will fill *sfname with the name of the subfont it needs and return –1. It can return zero if it is unable to make progress because it cannot resize the caches. Loadchar loads a character image into the character cache. Then it tells the graphics server to copy the character into position h in the character cache. If the current font width is smaller than the horizontal extent of the character being loaded, loadfont clears the cache and resets it to accept characters with the bigger width, unless noclr is set, in which case it just returns –1. If the character does not exist in the font at all, loadfont returns 0; if it is unable to load the character without destroying cached information, it returns –1, updating *sfname as described above. It returns 1 to indicate success. The age fields record when subfonts and characters have been used. The font age is increased every time the font is used (agefont does this). A character or subfont age is set to the font age at each use. Thus, characters or subfonts with small ages are the best candidates for replacement when the cache is full. |
SOURCE
/sys/src/libdraw |
SEE ALSO
graphics(2), allocimage(2), draw(2), subfont(2), image(6), font(6) |
DIAGNOSTICS
All of the functions use the graphics error function (see graphics(2)). |