blob: b0c7abfac707614d3b2a96f49a25febb017082e6 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH CACHECHARS 3
2.SH NAME
3cachechars, agefont, loadchar, Subfont, Fontchar, Font \- font utilities
4.SH SYNOPSIS
5.B #include <u.h>
6.br
rsc058b0112005-01-03 06:40:20 +00007.B #include <libc.h>
rsccfa37a72004-04-10 18:53:55 +00008.br
9.B #include <draw.h>
10.PP
11.ta \w'\fLCacheinfo 'u
12.PP
13.B
14int cachechars(Font *f, char **s, Rune **r, ushort *c, int max,
15.PP
16.B
17 int *widp, char **sfname)
18.PP
19.B
20int loadchar(Font *f, Rune r, Cacheinfo *c, int h,
21.PP
22.B
23 int noclr, char **sfname)
24.PP
25.B
26void agefont(Font *f)
27.SH DESCRIPTION
28A
29.I Font
30may contain too many characters to hold in memory
31simultaneously.
32The graphics library and draw device (see
33.IR draw (3))
34cooperate to solve this problem by maintaining a cache of recently used
35character images.
36The details of this cooperation need not be known by most programs:
37.I initdraw
38and its associated
39.I font
40variable,
41.IR openfont ,
42.IR stringwidth ,
43.IR string ,
44and
45.I freefont
46are sufficient for most purposes.
47The routines described below are used internally by the graphics library
48to maintain the font cache.
49.PP
50A
51.B Subfont
52is a set of images for a contiguous range of characters, stored as a single
53image
54with the characters
55placed side-by-side on a common baseline.
56It is described by the following data structures.
57.IP
58.EX
59.ta 6n +\w'Fontchar 'u +\w'bottom; 'u
60typedef
61struct Fontchar {
62 int x; /* left edge of bits */
63 uchar top; /* first non-zero scan-line */
64 uchar bottom; /* last non-zero scan-line */
65 char left; /* offset of baseline */
66 uchar width; /* width of baseline */
67} Fontchar;
68
69typedef
70struct Subfont {
71 char *name;
72 short n; /* number of chars in subfont */
73 uchar height; /* height of image */
74 char ascent; /* top of image to baseline */
75 Fontchar *info; /* n+1 Fontchars */
76 Image *bits; /* of font */
77} Subfont;
78.EE
79.PP
80The image fills the rectangle
81\fL(0, 0, \fIw\fP, height)\fR,
82where
83.I w
84is the sum of the horizontal extents (of non-zero pixels)
85for all characters.
86The pixels to be displayed for character
87.I c
88are in the rectangle
89\fL(\fIi\fP->x, \fIi\fP->top, (\fIi\fP+1)->x, \%\fIi\fP->bottom)\fR
90where
91.I i
92is
93.B
94&subfont->info[\fIc\fP]\fR.
95When a character is displayed at
96.B Point
97.B p
98in an image,
99the character rectangle is placed at
100.BI (p.x+ i ->left,
101.B p.y)
102and the next character of the string is displayed at
103.BI (p.x+ i ->width,
104.BR p.y) .
105The baseline of the characters is
106.L ascent
107rows down from the top of the subfont image.
108The
109.L info
110array has
111.B n+1
112elements, one each for characters 0 to
113.BR n-1
114plus an additional entry so the size of the last character
115can be calculated.
116Thus the width,
117.IR w ,
118of the
119.B Image
120associated with a
121.B Subfont
122.B s
123is
124.BR s->info[s->n].x .
125.PP
126A
127.B Font
128consists of an overall height and ascent
129and a collection of subfonts together with the ranges of runes (see
rsc058b0112005-01-03 06:40:20 +0000130.IR utf (7))
rsccfa37a72004-04-10 18:53:55 +0000131they represent.
132Fonts are described by the following structures.
133.IP
134.EX
135.ta 6n +\w'Cacheinfo 'u +\w'height; 'u
136typedef
137struct Cachefont {
138 Rune min; /* value of 0th char in subfont */
139 Rune max; /* value+1 of last char in subfont */
140 int offset; /* posn in subfont of char at min */
141 char *name; /* stored in font */
142 char *subfontname; /* to access subfont */
143} Cachefont;
144
145typedef
146struct Cacheinfo {
147 ushort x; /* left edge of bits */
148 uchar width; /* width of baseline */
149 schar left; /* offset of baseline */
150 Rune value; /* of char at this slot in cache */
151 ushort age;
152} Cacheinfo;
153
154typedef
155struct Cachesubf {
156 ulong age; /* for replacement */
157 Cachefont *cf; /* font info that owns us */
158 Subfont *f; /* attached subfont */
159} Cachesubf;
160
161typedef
162struct Font {
163 char *name;
164 Display *display;
165 short height; /* max ht of image;interline space*/
166 short ascent; /* top of image to baseline */
167 short width; /* widest so far; used in caching */
168 short nsub; /* number of subfonts */
169 ulong age; /* increasing counter; for LRU */
170 int ncache; /* size of cache */
171 int nsubf; /* size of subfont list */
172 Cacheinfo *cache;
173 Cachesubf *subf;
174 Cachefont **sub; /* as read from file */
175 Image *cacheimage;
176} Font;
177.EE
178.PP
179The
180.LR height
181and
182.LR ascent
183fields of Font are described in
rscbf8a59f2004-04-11 03:42:27 +0000184.IR graphics (3).
rsccfa37a72004-04-10 18:53:55 +0000185.L Sub
186contains
187.L nsub
188pointers to
189.BR Cachefonts .
190A
191.B Cachefont
192connects runes
193.L min
194through
195.LR max ,
196inclusive, to the subfont
197with file name
198.LR name ;
199it corresponds to a line of the file describing the font.
200.PP
201The characters
202are taken from the subfont starting at character number
203.L offset
204(usually zero) in the subfont, permitting selection of parts of subfonts.
205Thus
206the image for rune
207.I r
208is found in position
209.IB r -min+offset
210of the subfont.
211.PP
212For each font, the library, with support from the
213graphics server,
214maintains a cache of
215subfonts and a cache of recently used
216character images.
217The
218.B subf
219and
220.B cache
221fields are used by the library to maintain these caches.
222The
223.L width
224of a font is the maximum of the horizontal extents of the characters
225in the cache.
226.I String
227draws a string by loading the cache and emitting a sequence of
228cache indices to draw.
229.I Cachechars
230guarantees the images for the characters pointed to by
231.I *s
232or
233.I *r
234(one of these must be nil in each call)
235are in the cache of
236.IR f .
237It calls
238.I loadchar
239to put missing characters into the cache.
240.I Cachechars
241translates the character string into a set of cache indices
242which it loads into the array
243.IR c ,
244up to a maximum of
245.I n
246indices or the length of the string.
247.I Cachechars
248returns in
249.I c
250the number of cache indices emitted,
251updates
252.I *s
253to point to the next character to be processed, and sets
254.I *widp
255to the total width of the characters processed.
256.I Cachechars
257may return before the end of the string if it cannot
258proceed without destroying active data in the caches.
259If it needs to load a new subfont, it will fill
260.B *sfname
261with the name of the subfont it needs and return \-1.
262It can return zero if it is unable to make progress because
263it cannot resize the caches.
264.PP
265.I Loadchar
266loads a character image into the character cache.
267Then it tells the graphics server to copy the character
268into position
269.I h
270in the character cache.
271If the current font
272.L width
273is smaller than the horizontal extent of the character being loaded,
274.I loadfont
275clears the cache and resets it to
276accept characters with the bigger width, unless
277.I noclr
278is set, in which case it just returns \-1.
279If the character does not exist in the font at all,
280.I loadfont
281returns 0; if it is unable to load the character
282without destroying cached information, it returns \-1,
283updating
284.B *sfname
285as described above.
286It returns 1 to indicate success.
287.PP
288The
289.L age
290fields record when
291subfonts and characters have been used.
292The font
293.L age
294is increased every time the font is used
295.RI ( agefont
296does this).
297A character or subfont
298.L age
299is set to the font age at each use.
300Thus, characters or subfonts with small ages are the best candidates
301for replacement when the cache is full.
302.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000303.B \*9/src/libdraw
rsccfa37a72004-04-10 18:53:55 +0000304.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000305.IR graphics (3),
306.IR allocimage (3),
307.IR draw (3),
308.IR subfont (3),
rsc058b0112005-01-03 06:40:20 +0000309.IR image (7),
310.IR font (7)
rsccfa37a72004-04-10 18:53:55 +0000311.SH DIAGNOSTICS
312All of the functions use the graphics error function (see
rscbf8a59f2004-04-11 03:42:27 +0000313.IR graphics (3)).