home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / gxcache < prev    next >
Encoding:
Text File  |  1991-10-26  |  8.7 KB  |  274 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxcache.c */
  21. /* Character cache routines for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gspaint.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"            /* requires gsstate.h */
  30. #include "gzcolor.h"
  31. #include "gzpath.h"
  32. #include "gxdevmem.h"
  33. #include "gxchar.h"
  34. #include "gxfont.h"
  35. #include "gxfdir.h"
  36.  
  37. /* Define the size of the cache structures. */
  38. /* We round the size of a cached_char so that */
  39. /* an immediately following bitmap will be properly aligned. */
  40. const uint cached_char_sizeof = sizeof(cached_char) + (-sizeof(cached_char) & 3);
  41. #define cc_bits(cc) ((byte *)(cc) + cached_char_sizeof)
  42. const uint cached_fm_pair_sizeof = sizeof(cached_fm_pair);
  43.  
  44. /* Forward references */
  45. private void zap_cache(P1(gs_font_dir *));
  46.  
  47. /* Allocate storage for caching a rendered character, */
  48. /* and set up the memory device. */
  49. /* Return the cached_char if OK, 0 if too big. */
  50. cached_char *
  51. gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
  52.   ushort iwidth, ushort iheight)
  53. {    ulong isize, cdsize;
  54.     cached_char *cc;
  55.     byte *bits;
  56.     dev->width = iwidth;
  57.     dev->height = iheight;
  58.     isize = gdev_mem_bitmap_size(dev);    /* sets raster */
  59.     if ( dev->raster != 0 && iheight > dir->upper / dev->raster )
  60.         return 0;        /* too big */
  61.     cdsize = isize + cached_char_sizeof;
  62.     if ( dir->csize >= dir->cmax || dir->bmax - dir->bsize < cdsize )
  63.        {    /* There isn't enough room.  Clear the entire cache. */
  64.         /* We'll do something better someday.... */
  65.         if ( dir->cmax == 0 || dir->bmax < cdsize ) return 0;
  66.         zap_cache(dir);
  67.        }
  68.     /* Allocate the cache entry and the bits. */
  69.     if ( dir->ctop - dir->cnext < cdsize )
  70.        {    /* Must remove some entries from the cache. */
  71.         /* For now, just clear the cache. */
  72.         zap_cache(dir);
  73.        }
  74.     cc = (cached_char *)((byte *)(dir->cdata) + dir->cnext);
  75. #ifdef DEBUG
  76. if ( gs_debug['k'] | gs_debug['K'] )
  77.     dprintf5("[k]adding 0x%lx(%u):%u(%u,%u)\n",
  78.              (ulong)cc, dir->cnext, (uint)cdsize, iwidth, iheight);
  79. #endif
  80.     bits = cc_bits(cc);
  81.     memset((char *)bits, 0, (uint)isize);
  82.     cc->width = iwidth;
  83.     cc->height = iheight;
  84.     cc->raster = dev->raster;
  85.     dev->base = bits;
  86.     (*dev->procs->open_device)((gx_device *)dev);    /* initialize */
  87.     dir->csize++;
  88.     dir->bsize += isize;
  89.     dir->cnext += (uint)cdsize;
  90.     return cc;
  91. }
  92.  
  93. /* Remove the just-allocated character from the cache. */
  94. /* The character hasn't been added yet. */
  95. void
  96. gx_unalloc_cached_char(gs_font_dir *dir, cached_char *cc)
  97. {    uint isize = cc->raster * cc->height;
  98.     dir->cnext -= isize + cached_char_sizeof;
  99.     dir->csize--;
  100.     dir->bsize -= isize;
  101. }
  102.  
  103. /* Look up, and if necessary add, a font/matrix pair in the cache */
  104. cached_fm_pair *
  105. gx_lookup_fm_pair(register gs_state *pgs)
  106. {    float    mxx = pgs->char_tm.xx, mxy = pgs->char_tm.xy,
  107.         myx = pgs->char_tm.yx, myy = pgs->char_tm.yy;
  108.     gs_font *font = pgs->font;
  109.     register gs_font_dir *dir = font->dir;
  110.     register cached_fm_pair *pair = dir->mdata + dir->mnext;
  111.     int count = dir->msize;
  112.     while ( count-- )
  113.        {    if ( pair == dir->mdata ) pair += dir->mmax;
  114.         pair--;
  115.         if (    pair->font == font &&
  116.             pair->mxx == mxx && pair->mxy == mxy &&
  117.             pair->myx == myx && pair->myy == myy
  118.            )
  119.           return pair;
  120.        }
  121.     /* Add the pair to the cache */
  122.     if ( dir->msize == dir->mmax )
  123.         zap_cache(dir);        /* crude, but it works */
  124.     dir->msize++;
  125.     pair = dir->mdata + dir->mnext;
  126.     if ( ++dir->mnext == dir->mmax ) dir->mnext = 0;
  127.     pair->font = font;
  128.     pair->mxx = mxx, pair->mxy = mxy;
  129.     pair->myx = myx, pair->myy = myy;
  130.     pair->num_chars = 0;
  131.     return pair;
  132. }
  133.  
  134. /* Add a character to the cache */
  135. void
  136. gx_add_cached_char(gs_font_dir *dir, gx_device_memory *dev,
  137.   cached_char *cc, cached_fm_pair *pair)
  138. {    /* Make sure the bits are in the right order */
  139.     /* to use as a source. */
  140.     gdev_mem_ensure_byte_order(dev);
  141.     /* Add the new character at the tail of its chain. */
  142.        {    register cached_char **head =
  143.           &dir->chars[cc->code & (gx_char_cache_modulus - 1)];
  144.         while ( *head != 0 ) head = &(*head)->next;
  145.         *head = cc;
  146.         cc->next = 0;
  147.         cc->pair = pair;
  148.         pair->num_chars++;
  149.        }
  150.     /* If this is the most recently allocated character, */
  151.     /* discard the memory device overhead that follows the bits. */
  152.     /****** NOTE: this won't work when we start discarding */
  153.     /* entries from the cache individually, because */
  154.     /* there is no way to compute the actual size of an entry. ******/
  155.        {    uint isize = gdev_mem_bitmap_size(dev);
  156.         uint cdsize = isize + cached_char_sizeof;
  157.         if ( (byte *)cc - (byte *)dir->cdata + cdsize == dir->cnext )
  158.            {    uint diff = isize - cc->raster * cc->height;
  159.             dir->cnext -= diff;
  160.             dir->bsize -= diff;
  161. #ifdef DEBUG
  162. if ( gs_debug['K'] )
  163.             dprintf2("[K]shortening 0x%lx by %u\n", (ulong)cc,
  164.                  diff);
  165. #endif
  166.            }
  167.        }
  168. }
  169.  
  170. /* Look up a character in the cache. */
  171. /* Return the cached_char or 0. */
  172. cached_char *
  173. gx_lookup_cached_char(gs_state *pgs, cached_fm_pair *pair, uint ccode)
  174. {    register cached_char *cc =
  175.         pgs->font->dir->chars[ccode & (gx_char_cache_modulus - 1)];
  176.     while ( cc != 0 )
  177.        {    if ( cc->code == ccode && cc->pair == pair )
  178.           return cc;
  179.         cc = cc->next;
  180.        }
  181.     return 0;
  182. }
  183.  
  184. /* Copy a cached character to the screen. */
  185. /* Assume the caller has already done gx_color_load, */
  186. /* and the color is not a halftone. */
  187. /* Return 0 if OK, 1 if we couldn't do the operation but no error */
  188. /* occurred, or a negative error code. */
  189. int
  190. gx_copy_cached_char(register gs_show_enum *penum, register cached_char *cc)
  191. {    register gs_state *pgs = penum->pgs;
  192.     int x, y, w, h;
  193.     int code;
  194.     gs_fixed_point pt;
  195.     code = gx_path_current_point_inline(pgs->path, &pt);
  196.     if ( code < 0 ) return code;
  197.     /* Abort if the device color isn't pure. */
  198.     if ( !penum->color_loaded )
  199.        {    if ( !color_is_pure(pgs->dev_color) )
  200.             return 1;    /* can't use cache */
  201.         penum->color_loaded = 1;
  202.        }
  203.     /* If the character doesn't lie entirely within the */
  204.     /* quick-check clipping rectangle, we have to use */
  205.     /* the general case of image rendering. */
  206.     pt.x -= cc->offset.x;
  207.     x = fixed2int_var_rounded(pt.x) + penum->ftx;
  208.     pt.y -= cc->offset.y;
  209.     y = fixed2int_var_rounded(pt.y) + penum->fty;
  210.     w = cc->width;
  211.     h = cc->height;
  212. #ifdef DEBUG
  213. if ( gs_debug['K'] )
  214.     dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong)cc,
  215.          fixed2float(-cc->offset.x), fixed2float(-cc->offset.y)),
  216.     dprintf4("   at (%g,%g)+(%d,%d)\n", fixed2float(pt.x),
  217.          fixed2float(pt.y), penum->ftx, penum->fty);
  218. #endif
  219.     if (    x < penum->cxmin || x + w > penum->cxmax ||
  220.         y < penum->cymin || y + h > penum->cymax
  221.        )
  222.        {    gs_matrix mat;
  223.         mat = ctm_only(pgs);
  224.         mat.tx -= fixed2float(pt.x + int2fixed(penum->ftx));
  225.         mat.ty -= fixed2float(pt.y + int2fixed(penum->fty));
  226.         code = gs_imagemask(pgs, cc->raster * 8, h, 1,
  227.                     &mat, cc_bits(cc), 0);
  228.        }
  229.     else
  230.        {    /* Just copy the bits */
  231.         gx_device *dev = pgs->device->info;
  232.         code = (*dev->procs->copy_mono)
  233.             (dev, cc_bits(cc), 0, cc->raster,
  234.              x, y, w, h,
  235.              gx_no_color_index, pgs->dev_color->color1);
  236.        }
  237.     return ( code < 0 ? code : 0 );
  238. }
  239.  
  240. /* Purge from the caches all references to a given font. */
  241. void
  242. gs_purge_font_from_char_caches(gs_font_dir *dir, gs_font *font)
  243. {    cached_fm_pair *pair = dir->mdata + dir->mnext;
  244.     int count = dir->msize;
  245.     while ( count-- )
  246.        {    if ( pair == dir->mdata ) pair += dir->mmax;
  247.         pair--;
  248.         if ( pair->font == font )
  249.            {    zap_cache(dir);    /* crude but effective */
  250.             return;
  251.            }
  252.        }
  253. }
  254.  
  255. /* ------ Internal routines ------ */
  256.  
  257. /* Zap the cache when it overflows, or when it refers to */
  258. /* a font that is being discarded by a restore. */
  259. /* This is inefficient and crude, but it works. */
  260. private void
  261. zap_cache(register gs_font_dir *dir)
  262. {    dir->bsize = 0;
  263.     dir->msize = 0;
  264.     dir->csize = 0;
  265.     dir->mnext = 0;
  266.     dir->cnext = 0;
  267.     dir->cbot = dir->ctop = dir->cdata_size;
  268.     memset((char *)dir->chars, 0, gx_char_cache_modulus * sizeof(cached_char *));
  269. #ifdef DEBUG
  270. if ( gs_debug['k'] | gs_debug['K'] )
  271.     dprintf("[k]clearing cache\n");
  272. #endif
  273. }
  274.