home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXFCACHE.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  226 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gxfcache.h */
  20. /* Definitions for Ghostscript font and character caches */
  21. /* Requires gsfont.h */
  22. #include "gsuid.h"
  23. #include "gsxfont.h"
  24.  
  25. /* ------ Font/matrix pair cache ------ */
  26.  
  27. #ifndef cached_fm_pair_DEFINED
  28. #  define cached_fm_pair_DEFINED
  29. typedef struct cached_fm_pair_s cached_fm_pair;
  30. #endif
  31.  
  32. /* An entry for a (font,matrix) pair in the character cache. */
  33. /* If UID is valid, font may be 0, since we keep entries for fonts */
  34. /* unloaded by a restore if they have valid UIDs. */
  35. struct cached_fm_pair_s {
  36.     gs_font *font;            /* base font */
  37.     gs_uid UID;            /* font UniqueID or XUID */
  38.     float mxx, mxy, myx, myy;    /* transformation */
  39.     int num_chars;            /* # of cached chars with this */
  40.                     /* f/m pair */
  41.     bool xfont_tried;        /* true if we looked up an xfont */
  42.     gx_xfont *xfont;        /* the xfont (if any) */
  43.     gs_memory_t *memory;        /* the allocator for the xfont */
  44.     uint index;            /* index of this pair in mdata */
  45. };
  46. #define private_st_cached_fm_pair() /* in gxccman.c */\
  47.   gs_private_st_ptrs3(st_cached_fm_pair, cached_fm_pair,\
  48.     "cached_fm_pair", fm_pair_enum_ptrs, fm_pair_reloc_ptrs,\
  49.     font, UID.xvalues, xfont)
  50. #define private_st_cached_fm_pair_elt()    /* in gxccman.c */\
  51.   gs_private_st_element(st_cached_fm_pair_element, cached_fm_pair,\
  52.     "cached_fm_pair[]", fm_pair_element_enum_ptrs, fm_pair_element_reloc_ptrs,\
  53.     st_cached_fm_pair)
  54. /* If font == 0 and UID is invalid, this is a free entry. */
  55. #define fm_pair_is_free(pair)\
  56.   ((pair)->font == 0 && !uid_is_valid(&(pair)->UID))
  57. #define fm_pair_set_free(pair)\
  58.   ((pair)->font = 0, uid_set_invalid(&(pair)->UID))
  59. #define fm_pair_init(pair)\
  60.   (fm_pair_set_free(pair), (pair)->xfont_tried = false, (pair)->xfont = 0)
  61.  
  62. /* The font/matrix pair cache itself. */
  63. typedef struct fm_pair_cache_s {
  64.     uint msize, mmax;        /* # of cached font/matrix pairs */
  65.     cached_fm_pair *mdata;
  66.     uint mnext;            /* rover for allocating font/matrix pairs */
  67. } fm_pair_cache;
  68.  
  69. /* ------ Character cache ------- */
  70.  
  71. /* The character cache contains both used and free blocks. */
  72. /* All blocks have a common header; free blocks have ONLY the header. */
  73. typedef struct cached_char_head_s {
  74.     uint size;            /* total block size in bytes */
  75.     cached_fm_pair *pair;        /* font/matrix pair, 0 if free */
  76. } cached_char_head;
  77. #define cc_head_is_free(cch) ((cch)->pair == 0)
  78. #define cc_head_set_free(cch) ((cch)->pair = 0)
  79. /*
  80.  * A cached bitmap for an individual character.
  81.  * The bits, if any, immediately follow the structure;
  82.  * characters with only xfont definitions may not have bits.
  83.  * We maintain the invariant that if cc->head.pair != 0 (the character
  84.  * is visible in the cache), at least one of the following must be true:
  85.  *    - cc_has_bits(cc);
  86.  *    - cc->xglyph != gx_no_xglyph && cc->head.pair->xfont != 0.
  87.  */
  88. struct char_cache_chunk_s;
  89. typedef struct char_cache_chunk_s char_cache_chunk;
  90. #ifndef cached_char_DEFINED
  91. #  define cached_char_DEFINED
  92. typedef struct cached_char_s cached_char;
  93. #endif
  94. struct cached_char_s {
  95.     /* The code, font/matrix pair, wmode, and depth */
  96.     /* are the 'key' in the cache. */
  97.     cached_char_head head;        /* (must be first, */
  98.                     /* references font/matrix pair) */
  99.     gs_glyph code;            /* glyph code */
  100.     byte wmode;            /* writing mode (0 or 1) */
  101.     byte depth;            /* # of gray-scale bits per pixel */
  102.                     /* (1-8; currently always 1) */
  103.     /* The following are neither 'key' nor 'value'. */
  104.     cached_char *next;        /* next in replacement ring */
  105.     char_cache_chunk *chunk;    /* chunk where this char */
  106.                     /* is allocated */
  107.     uint loc;            /* relative location in chunk */
  108.     /* The rest of the structure is the 'value'. */
  109.     gx_xglyph xglyph;        /* the xglyph for the xfont, if any */
  110.     ushort width, height;        /* dimensions of bitmap */
  111.     ushort raster;
  112.     gx_bitmap_id id;        /* if null, no bits follow */
  113.     gs_fixed_point wxy;        /* width in device coords */
  114.     gs_fixed_point offset;        /* (-llx, -lly) in device coords */
  115. };
  116. /* Note that cached_chars are (currently) never instantiated on their own. */
  117. /* They only have a descriptor so that cached_char_ptr can trace them. */
  118. #define private_st_cached_char() /* in gxccman.c */\
  119.   gs_private_st_composite(st_cached_char, cached_char, "cached_char",\
  120.     cached_char_enum_ptrs, cached_char_reloc_ptrs)
  121. #define private_st_cached_char_ptr() /* in gxccman.c */\
  122.   gs_private_st_composite(st_cached_char_ptr, cached_char *,\
  123.     "cached_char *", cc_ptr_enum_ptrs, cc_ptr_reloc_ptrs)
  124. #define private_st_cached_char_ptr_elt() /* in gxccman.c */\
  125.   gs_private_st_element(st_cached_char_ptr_element, cached_char *,\
  126.     "cached_char *[]", cc_ptr_element_enum_ptrs, cc_ptr_element_reloc_ptrs,\
  127.     st_cached_char_ptr)
  128. #define cc_is_free(cc) cc_head_is_free(&(cc)->head)
  129. #define cc_set_free(cc) cc_head_set_free(&(cc)->head)
  130. #define cc_has_bits(cc) ((cc)->id != gx_no_bitmap_id)
  131.  
  132. /* Define the size of the cache structures. */
  133. /* We round the size of a cached_char so that */
  134. /* an immediately following bitmap will be properly aligned. */
  135. #define align_cached_char_mod\
  136.   (max(align_bitmap_mod, max(arch_align_ptr_mod, arch_align_long_mod)))
  137. #define sizeof_cached_char\
  138.   round_up(sizeof(cached_char), align_cached_char_mod)
  139. #define cc_bits(cc) ((byte *)(cc) + sizeof_cached_char)
  140.  
  141. /* Define the hash chain for a (glyph, fm_pair) key. */
  142. /* The OSF/1 compiler doesn't like casting a pointer to a shorter int. */
  143. #if arch_sizeof_ptr == arch_sizeof_int
  144. typedef uint ptr_uint_t;
  145. #else
  146. typedef ulong ptr_uint_t;
  147. #endif
  148. #define chars_head(dir, glyph, pair)\
  149.   &(dir)->ccache.chars[((uint)(glyph) + ((ptr_uint_t)(pair) << 4)) & (dir)->ccache.chars_mask]
  150.  
  151. /*
  152.  * We allocate the character cache in chunks, so as not to tie up memory
  153.  * prematurely if it isn't needed (or something else needs it more).
  154.  * Thus there is a structure for managing an entire cache, and another
  155.  * structure for managing each chunk.
  156.  *
  157.  * We assume that the character cache is in non-garbage-collectable
  158.  * storage; we fix up its pointers (to the f/m pairs) when we fix up
  159.  * the font directory that manages it.  This is something of a hack....
  160.  */
  161.  
  162. struct char_cache_chunk_s {
  163.     char_cache_chunk *next;
  164.     byte *data;        /* struct cached_char_head_s * */
  165.     uint size;
  166. };
  167.  
  168. /* Define the glyph marking procedure for the GC. */
  169. typedef bool (*cc_mark_glyph_proc_t)(P1(gs_glyph));
  170.  
  171. #ifndef cached_char_DEFINED
  172. #  define cached_char_DEFINED
  173. typedef struct cached_char_s cached_char;
  174. #endif
  175. typedef struct char_cache_s {
  176.     gs_memory_t *memory;
  177.     uint bsize, bmax;        /* # of bytes for cached chars */
  178.     uint bspace;            /* space allocated for chunks */
  179.     uint csize, cmax;        /* # of cached chars */
  180.     uint lower;            /* min size at which cached chars */
  181.                     /* should be stored compressed */
  182.     uint upper;            /* max size of a single cached char */
  183.     cached_char **chars;        /* chain heads */
  184.     uint chars_mask;        /* (a power of 2 -1) */
  185.     char_cache_chunk *chunks;    /* current chunk in circular list */
  186.     uint cnext;            /* rover for allocating characters */
  187.                     /* in current chunk */
  188.     char_cache_chunk *initial_chunk;    /* dummy initial chunk */
  189.     cc_mark_glyph_proc_t mark_glyph;
  190. } char_cache;
  191.  
  192. /* ------ Font/character cache ------ */
  193.  
  194. /* A font "directory" (font/character cache manager). */
  195. struct gs_font_dir_s {
  196.         /* Original (unscaled) fonts */
  197.     gs_font *orig_fonts;
  198.         /* Scaled font cache */
  199.     gs_font *scaled_fonts;        /* list of recently scaled fonts */
  200.     uint ssize, smax;
  201.         /* Font/matrix pair cache */
  202.     fm_pair_cache fmcache;
  203.         /* Character cache */
  204.     char_cache ccache;
  205. };
  206. #define private_st_font_dir()    /* in gsfont.c */\
  207.   gs_private_st_composite(st_font_dir, gs_font_dir, "gs_font_dir",\
  208.     font_dir_enum_ptrs, font_dir_reloc_ptrs)
  209.  
  210. /* Enumerate the pointers in a font directory. */
  211. #define font_dir_do_ptrs(m)\
  212.   m(0,orig_fonts) m(1,scaled_fonts) m(2,fmcache.mdata)\
  213.   m(3,ccache.chars)
  214. #define st_font_dir_max_ptrs 4
  215.  
  216. /* Character cache procedures (in gxccache.c and gxccman.c) */
  217. int gx_char_cache_alloc(P6(gs_memory_t *, gs_font_dir *, uint, uint, uint, uint));
  218. void gx_char_cache_init(P1(gs_font_dir *));
  219. void gx_purge_selected_cached_chars(P3(gs_font_dir *, bool (*)(P2(cached_char *, void *)), void *));
  220. cached_fm_pair *
  221.     gx_lookup_fm_pair(P2(gs_font *, const gs_state *));
  222. cached_fm_pair *
  223.     gx_add_fm_pair(P4(gs_font_dir *, gs_font *, const gs_uid *, const gs_state *));
  224. void gx_lookup_xfont(P3(const gs_state *, cached_fm_pair *, int));
  225. void gs_purge_fm_pair(P3(gs_font_dir *, cached_fm_pair *, int));
  226.