]> git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/gxfcache.h
Import cups.org releases
[thirdparty/cups.git] / pstoraster / gxfcache.h
1 /* Copyright (C) 1992, 1995, 1997 Aladdin Enterprises. All rights reserved.
2
3 This file is part of GNU Ghostscript.
4
5 GNU Ghostscript is distributed in the hope that it will be useful, but
6 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
7 to anyone for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing. Refer
9 to the GNU General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GNU
12 Ghostscript, but only under the conditions described in the GNU General
13 Public License. A copy of this license is supposed to have been given
14 to you along with GNU Ghostscript so you can know your rights and
15 responsibilities. It should be in a file named COPYING. Among other
16 things, the copyright notice and this notice must be preserved on all
17 copies.
18
19 Aladdin Enterprises supports the work of the GNU Project, but is not
20 affiliated with the Free Software Foundation or the GNU Project. GNU
21 Ghostscript, as distributed by Aladdin Enterprises, does not require any
22 GNU software to build or run it.
23 */
24
25 /*$Id$ */
26 /* Requires gsfont.h */
27
28 #ifndef gxfcache_INCLUDED
29 # define gxfcache_INCLUDED
30
31 #include "gsuid.h"
32 #include "gsxfont.h"
33 #include "gxbcache.h"
34 #include "gxftype.h"
35
36 /* ------ Font/matrix pair cache entry ------ */
37
38 #ifndef cached_fm_pair_DEFINED
39 # define cached_fm_pair_DEFINED
40 typedef struct cached_fm_pair_s cached_fm_pair;
41
42 #endif
43
44 /*
45 * Define the entry for a cached (font,matrix) pair. If the UID
46 * is valid, the font pointer may be 0, since we keep entries even for
47 * fonts unloaded by a restore if they have valid UIDs; in this case,
48 * we also need the FontType as part of the key.
49 * Note that because of the dependency on StrokeWidth, we can't cache
50 * fonts with non-zero PaintType.
51 * We can't use the address of the pair for the hash value,
52 * since the GC may move pairs in storage, so we create a hash
53 * when we allocate the pair initially.
54 */
55 struct cached_fm_pair_s {
56 gs_font *font; /* base font */
57 gs_uid UID; /* font UniqueID or XUID */
58 font_type FontType; /* (part of key if UID is valid) */
59 uint hash; /* hash for this pair */
60 float mxx, mxy, myx, myy; /* transformation */
61 int num_chars; /* # of cached chars with this */
62 /* f/m pair */
63 bool xfont_tried; /* true if we looked up an xfont */
64 gx_xfont *xfont; /* the xfont (if any) */
65 gs_memory_t *memory; /* the allocator for the xfont */
66 uint index; /* index of this pair in mdata */
67 };
68
69 #define private_st_cached_fm_pair() /* in gxccman.c */\
70 gs_private_st_ptrs3(st_cached_fm_pair, cached_fm_pair,\
71 "cached_fm_pair", fm_pair_enum_ptrs, fm_pair_reloc_ptrs,\
72 font, UID.xvalues, xfont)
73 #define private_st_cached_fm_pair_elt() /* in gxccman.c */\
74 gs_private_st_element(st_cached_fm_pair_element, cached_fm_pair,\
75 "cached_fm_pair[]", fm_pair_element_enum_ptrs, fm_pair_element_reloc_ptrs,\
76 st_cached_fm_pair)
77 /* If font == 0 and UID is invalid, this is a free entry. */
78 #define fm_pair_is_free(pair)\
79 ((pair)->font == 0 && !uid_is_valid(&(pair)->UID))
80 #define fm_pair_set_free(pair)\
81 ((pair)->font = 0, uid_set_invalid(&(pair)->UID))
82 #define fm_pair_init(pair)\
83 (fm_pair_set_free(pair), (pair)->xfont_tried = false, (pair)->xfont = 0)
84
85 /* The font/matrix pair cache itself. */
86 typedef struct fm_pair_cache_s {
87 uint msize, mmax; /* # of cached font/matrix pairs */
88 cached_fm_pair *mdata;
89 uint mnext; /* rover for allocating font/matrix pairs */
90 } fm_pair_cache;
91
92 /* ------ Character cache entry ------- */
93
94 /* Define the allocation chunk type. */
95 typedef gx_bits_cache_chunk char_cache_chunk;
96
97 /*
98 * This is a subclass of the entry in a general bitmap cache.
99 * The character cache contains both used and free blocks.
100 * All blocks have a common header; free blocks have ONLY the header.
101 */
102 typedef gx_cached_bits_head cached_char_head;
103
104 #define cc_head_is_free(cch) cb_head_is_free(cch)
105 #define cc_head_set_free(cch) cb_head_set_free(cch)
106 /*
107 * Define the cache entry for an individual character.
108 * The bits, if any, immediately follow the structure;
109 * characters with only xfont definitions may not have bits.
110 * An entry is 'real' if it is not free and if pair != 0.
111 * We maintain the invariant that at least one of the following must be true
112 * for all real entries:
113 * - cc_has_bits(cc);
114 * - cc->xglyph != gx_no_xglyph && cc_pair(cc)->xfont != 0.
115 */
116 #ifndef cached_char_DEFINED
117 # define cached_char_DEFINED
118 typedef struct cached_char_s cached_char;
119
120 #endif
121 struct cached_char_s {
122
123 /* The code, font/matrix pair, wmode, and depth */
124 /* are the 'key' in the cache. */
125 /* gx_cached_bits_common includes depth. */
126
127 gx_cached_bits_common; /* (must be first) */
128 #define cc_depth(cc) ((cc)->cb_depth)
129 #define cc_set_depth(cc, d) ((cc)->cb_depth = (d))
130 cached_fm_pair *pair;
131 #define cc_pair(cc) ((cc)->pair)
132 #define cc_set_pair_only(cc, p) ((cc)->pair = (p))
133 gs_glyph code; /* glyph code */
134 byte wmode; /* writing mode (0 or 1) */
135
136 /* The following are neither 'key' nor 'value'. */
137
138 char_cache_chunk *chunk; /* chunk where this char */
139 /* is allocated */
140 uint loc; /* relative location in chunk */
141 uint pair_index; /* index of pair in mdata */
142
143 /* The rest of the structure is the 'value'. */
144 /* gx_cached_bits_common has width, height, raster, */
145 /* shift (not used here), id. */
146
147 #define cc_raster(cc) ((cc)->raster)
148 #define cc_set_raster(cc, r) ((cc)->raster = (r))
149 gx_xglyph xglyph; /* the xglyph for the xfont, if any */
150 gs_fixed_point wxy; /* width in device coords */
151 gs_fixed_point offset; /* (-llx, -lly) in device coords */
152 };
153
154 #define cc_is_free(cc) cc_head_is_free(&(cc)->head)
155 #define cc_set_free(cc) cc_head_set_free(&(cc)->head)
156 #define cc_set_pair(cc, p)\
157 ((cc)->pair_index = ((cc)->pair = (p))->index)
158 #define cc_has_bits(cc) ((cc)->id != gx_no_bitmap_id)
159 /*
160 * Memory management for cached_chars is a little unusual.
161 * cached_chars are never instantiated on their own; a pointer to
162 * a cached_char points into the middle of a cache chunk.
163 * Consequently, such pointers can't be traced or relocated
164 * in the usual way. What we do instead is allocate the cache
165 * outside garbage-collectable space; we do all the tracing and relocating
166 * of pointers *from* the cache (currently only the head.pair pointer)
167 * when we trace or relocate the font "directory" that owns the cache.
168 *
169 * Since cached_chars are (currently) never instantiated on their own,
170 * they only have a descriptor so that cached_char_ptr can trace them.
171 */
172 #define private_st_cached_char() /* in gxccman.c */\
173 gs_private_st_composite(st_cached_char, cached_char, "cached_char",\
174 cached_char_enum_ptrs, cached_char_reloc_ptrs)
175 #define private_st_cached_char_ptr() /* in gxccman.c */\
176 gs_private_st_composite(st_cached_char_ptr, cached_char *,\
177 "cached_char *", cc_ptr_enum_ptrs, cc_ptr_reloc_ptrs)
178 #define private_st_cached_char_ptr_elt() /* in gxccman.c */\
179 gs_private_st_element(st_cached_char_ptr_element, cached_char *,\
180 "cached_char *[]", cc_ptr_element_enum_ptrs, cc_ptr_element_reloc_ptrs,\
181 st_cached_char_ptr)
182
183 /*
184 * Define the alignment and size of the cache structures.
185 */
186 #define align_cached_char_mod align_cached_bits_mod
187 #define sizeof_cached_char\
188 round_up(sizeof(cached_char), align_cached_char_mod)
189 #define cc_bits(cc) ((byte *)(cc) + sizeof_cached_char)
190 #define cc_const_bits(cc) ((const byte *)(cc) + sizeof_cached_char)
191
192 /* Define the hash index for a (glyph, fm_pair) key. */
193 #define chars_head_index(glyph, pair)\
194 ((uint)(glyph) * 59 + (pair)->hash * 73) /* scramble it a bit */
195
196 /* ------ Character cache ------ */
197
198 /*
199 * So that we can find all the entries in the cache without
200 * following chains of pointers, we use open hashing rather than
201 * chained hashing for the lookup table.
202 */
203 typedef struct char_cache_s {
204 /* gx_bits_cache_common provides chunks, cnext, */
205 /* bsize, csize. */
206 gx_bits_cache_common;
207 gs_memory_t *struct_memory;
208 gs_memory_t *bits_memory;
209 cached_char **table; /* hash table */
210 uint table_mask; /* (a power of 2 -1) */
211 uint bmax; /* max bsize */
212 uint cmax; /* max csize */
213 uint bspace; /* space allocated for chunks */
214 uint lower; /* min size at which cached chars */
215 /* should be stored compressed */
216 uint upper; /* max size of a single cached char */
217 gs_glyph_mark_proc_t mark_glyph;
218 void *mark_glyph_data; /* closure data */
219 } char_cache;
220
221 /* ------ Font/character cache ------ */
222
223 /* A font "directory" (font/character cache manager). */
224 struct gs_font_dir_s {
225
226 /* Original (unscaled) fonts */
227
228 gs_font *orig_fonts;
229
230 /* Scaled font cache */
231
232 gs_font *scaled_fonts; /* list of recently scaled fonts */
233 uint ssize, smax;
234
235 /* Font/matrix pair cache */
236
237 fm_pair_cache fmcache;
238
239 /* Character cache */
240
241 char_cache ccache;
242 /* Scanning cache for GC */
243 uint enum_index; /* index (N) */
244 uint enum_offset; /* ccache.table[offset] is N'th non-zero entry */
245 };
246
247 #define private_st_font_dir() /* in gsfont.c */\
248 gs_private_st_composite(st_font_dir, gs_font_dir, "gs_font_dir",\
249 font_dir_enum_ptrs, font_dir_reloc_ptrs)
250
251 /* Enumerate the pointers in a font directory, except for orig_fonts. */
252 #define font_dir_do_ptrs(m)\
253 /*m(-,orig_fonts)*/ m(0,scaled_fonts) m(1,fmcache.mdata)\
254 m(2,ccache.table) m(3,ccache.mark_glyph_data)
255 #define st_font_dir_max_ptrs 4
256
257 /* Character cache procedures (in gxccache.c and gxccman.c) */
258 int gx_char_cache_alloc(P7(gs_memory_t * struct_mem, gs_memory_t * bits_mem,
259 gs_font_dir * pdir, uint bmax, uint mmax,
260 uint cmax, uint upper));
261 void gx_char_cache_init(P1(gs_font_dir *));
262 void gx_purge_selected_cached_chars(P3(gs_font_dir *, bool(*)(P2(cached_char *, void *)), void *));
263 cached_fm_pair *
264 gx_lookup_fm_pair(P2(gs_font *, const gs_state *));
265 cached_fm_pair *
266 gx_add_fm_pair(P4(gs_font_dir *, gs_font *, const gs_uid *, const gs_state *));
267 void gx_lookup_xfont(P3(const gs_state *, cached_fm_pair *, int));
268 void gs_purge_fm_pair(P3(gs_font_dir *, cached_fm_pair *, int));
269
270 #endif /* gxfcache_INCLUDED */