home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GXCACHE.C < prev    next >
C/C++ Source or Header  |  1992-09-16  |  12KB  |  389 lines

  1. /* Copyright (C) 1989, 1992 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 "gpcheck.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"
  28. #include "gspaint.h"
  29. #include "gzstate.h"
  30. #include "gzdevice.h"            /* requires gsstate.h */
  31. #include "gzcolor.h"
  32. #include "gzpath.h"
  33. #include "gxcpath.h"
  34. #include "gxdevmem.h"
  35. #include "gxchar.h"
  36. #include "gxcache.h"
  37. #include "gxfont.h"
  38. #include "gxfdir.h"
  39.  
  40. extern ulong gs_next_ids(P1(uint));
  41.  
  42. /* Define the size of the cache structures. */
  43. /* We round the size of a cached_char so that */
  44. /* an immediately following bitmap will be properly aligned. */
  45. const uint cached_char_sizeof =
  46.   sizeof(cached_char) + (-sizeof(cached_char) & 3);
  47. #define cc_bits(cc) ((byte *)(cc) + cached_char_sizeof)
  48. const uint cached_fm_pair_sizeof = sizeof(cached_fm_pair);
  49.  
  50. /* Define the hash chain for a (code, fm_pair) key. */
  51. #define chars_head(dir, code, pair)\
  52.   &(dir)->chars[((uint)(code) + ((uint)(pair) << 4)) & (dir)->chars_mask]
  53.  
  54. /* Forward references */
  55. private void shorten_cached_char(P3(gs_font_dir *, cached_char *, uint));
  56. private void purge_fm_pair(P2(gs_font_dir *, cached_fm_pair *));
  57.  
  58. /* Initialize the character cache. */
  59. void
  60. gx_char_cache_init(register gs_font_dir *dir)
  61. {    cached_char_head *cdata = (cached_char_head *)dir->cdata;
  62.     int i;
  63.     cached_fm_pair *pair;
  64.     dir->bsize = 0;
  65.     dir->msize = 0;
  66.     dir->csize = 0;
  67.     dir->mnext = 0;
  68.     dir->cnext = 0;
  69.     cdata->pair = 0;
  70.     cdata->size = dir->cdata_size;
  71.     memset((char *)dir->chars, 0,
  72.            (dir->chars_mask + 1) * sizeof(cached_char *));
  73.     for ( i = dir->mmax, pair = dir->mdata; --i >= 0; pair++ )
  74.       fm_pair_set_free(pair);
  75. }
  76.  
  77. /* Allocate storage for caching a rendered character, */
  78. /* and set up the memory device. */
  79. /* Return the cached_char if OK, 0 if too big. */
  80. cached_char *
  81. gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
  82.   ushort iwidth, ushort iheight)
  83. {    ulong isize, icdsize;
  84.     uint iraster;
  85.     uint cdsize;
  86.     cached_char_head *cch;
  87. #define hcc ((cached_char *)cch)
  88.     cached_char *cc;
  89.     uint fsize = 0;
  90.     byte *bits;
  91.     dev->width = iwidth;
  92.     dev->height = iheight;
  93.     iraster = gdev_mem_raster(dev);
  94.     if ( iraster != 0 && iheight > dir->upper / iraster )
  95.         return 0;        /* too big */
  96.     isize = gdev_mem_bitmap_size(dev);
  97.     icdsize = isize + cached_char_sizeof;
  98.     if ( icdsize > dir->cdata_size )
  99.         return 0;        /* too big */
  100.     cdsize = (uint)icdsize;
  101.     /* Look for and/or free enough space. */
  102.     cch = (cached_char_head *)(dir->cdata + dir->cnext);
  103.     cc = hcc;
  104.     for ( ; ; )
  105.       { if ( (byte *)cc + fsize == dir->cdata + dir->cdata_size )
  106.           cch = (cached_char_head *)dir->cdata, cc = hcc, fsize = 0;
  107.         if ( !cc_head_is_free(cch) )
  108.           { /* Free the character */
  109.         cached_char **pcc = chars_head(dir, hcc->code, cch->pair);
  110.         while ( *pcc != hcc )
  111.           pcc = &(*pcc)->next;
  112.         *pcc = hcc->next; /* remove from chain */
  113.         gx_free_cached_char(dir, hcc);
  114.           }
  115.         fsize += cch->size;
  116.         if_debug2('K', "[K]merging free 0x%lx(%u)\n",
  117.               (ulong)cch, cch->size);
  118.         cc->head.size = fsize;
  119.         if ( fsize == cdsize ||
  120.          fsize >= cdsize + sizeof(cached_char_head)
  121.            )
  122.           break;        /* enough room here */
  123.         cch = (cached_char_head *)((byte *)cc + fsize);
  124.       }
  125. #undef hcc
  126.     if ( fsize > cdsize )
  127.       { shorten_cached_char(dir, cc, fsize - cdsize);
  128.         if_debug2('K', "[K]shortening 0x%lx by %u (initial)\n",
  129.               (ulong)cc, fsize - cdsize);
  130.       }
  131.     if_debug4('k', "[k]adding 0x%lx:%u(%u,%u)\n",
  132.           (ulong)cc, cdsize, iwidth, iheight);
  133.     bits = cc_bits(cc);
  134.     memset((char *)bits, 0, (uint)isize);
  135.     cc->width = iwidth;
  136.     cc->height = iheight;
  137.     cc->raster = iraster;
  138.     cc->head.pair = 0;    /* not linked in yet */
  139.     dev->base = bits;
  140.     (*dev->procs->open_device)((gx_device *)dev);    /* initialize */
  141.     dir->csize++;
  142.     dir->bsize += cdsize;
  143.     dir->cnext = (byte *)cc + cdsize - dir->cdata;
  144.     return cc;
  145. }
  146.  
  147. /* Remove a character from the cache. */
  148. void
  149. gx_free_cached_char(gs_font_dir *dir, cached_char *cc)
  150. {    dir->cnext = (byte *)cc - dir->cdata;
  151.     dir->csize--;
  152.     dir->bsize -= cc->head.size;
  153.     if ( cc->head.pair != 0 )
  154.        {    /* might be allocated but not added to table yet */
  155.         cc->head.pair->num_chars--;
  156.        }
  157.     if_debug2('k', "[k]freeing 0x%lx, pair=0x%lx\n",
  158.           (ulong)cc, (ulong)cc->head.pair);
  159.     cc_set_free(cc);
  160. }
  161.  
  162. /* Look up, and if necessary add, a font/matrix pair in the cache */
  163. cached_fm_pair *
  164. gx_lookup_fm_pair(register const gs_state *pgs)
  165. {    float    mxx = pgs->char_tm.xx, mxy = pgs->char_tm.xy,
  166.         myx = pgs->char_tm.yx, myy = pgs->char_tm.yy;
  167.     gs_font *font = pgs->font;
  168.     register gs_font_dir *dir = font->dir;
  169.     register cached_fm_pair *pair = dir->mdata + dir->mnext;
  170.     int count = dir->mmax;
  171.     long uid = -1;
  172.     cached_fm_pair *mend;
  173.     if ( font->FontType != ft_composite )
  174.        {    uid = font->data.base.UniqueID;
  175.         if ( uid != -1 ) font = 0;
  176.        }
  177.     while ( count-- )
  178.        {    if ( pair == dir->mdata ) pair += dir->mmax;
  179.         pair--;
  180.         if (    pair->font == font && pair->UniqueID == uid &&
  181.             pair->mxx == mxx && pair->mxy == mxy &&
  182.             pair->myx == myx && pair->myy == myy
  183.            )
  184.           return pair;
  185.        }
  186.     /* Add the pair to the cache */
  187.     mend = dir->mdata + dir->mmax;
  188.     if ( dir->msize == dir->mmax ) /* cache is full */
  189.       { /* Prefer an entry with num_chars == 0, if any. */
  190.         for ( count = dir->mmax; --count >= 0 && pair->num_chars != 0; )
  191.           if ( ++pair == mend ) pair = dir->mdata;
  192.         purge_fm_pair(dir, pair);
  193.       }
  194.     else
  195.       { /* Look for an empty entry.  (We know there is one.) */
  196.         while ( !fm_pair_is_free(pair) )
  197.           if ( ++pair == mend ) pair = dir->mdata;
  198.       }
  199.     dir->msize++;
  200.     dir->mnext = pair + 1 - dir->mdata;
  201.     if ( dir->mnext == dir->mmax ) dir->mnext = 0;
  202.     pair->font = font;
  203.     pair->UniqueID = uid;
  204.     pair->mxx = mxx, pair->mxy = mxy;
  205.     pair->myx = myx, pair->myy = myy;
  206.     pair->num_chars = 0;
  207.     return pair;
  208. }
  209.  
  210. /* Add a character to the cache */
  211. void
  212. gx_add_cached_char(gs_font_dir *dir, gx_device_memory *dev,
  213.   cached_char *cc, cached_fm_pair *pair)
  214. {    uint raster = cc->raster;
  215.     uint bsize = raster * cc->height;
  216.     byte *bits = cc_bits(cc);
  217.     cc->id = gs_next_ids(1);
  218.     /* Make sure the bits are in the right order */
  219.     /* to use as a source. */
  220.     gdev_mem_ensure_byte_order(dev);
  221.     /* Add the new character at the tail of its chain. */
  222.     {    register cached_char **head =
  223.           chars_head(dir, cc->code, pair);
  224.         while ( *head != 0 ) head = &(*head)->next;
  225.         *head = cc;
  226.         cc->next = 0;
  227.         cc->head.pair = pair;
  228.         pair->num_chars++;
  229.     }
  230.     /* Compress the character in place. */
  231.     /* For now, just discard leading and trailing blank rows. */
  232.     {    /* Discard trailing blank rows. */
  233.         register byte *p = bits + bsize;
  234.         register uint n = bsize;
  235.         while ( n && !p[-1] ) --n, --p;
  236.         bsize = (n + raster - 1) / raster * raster;
  237.     }
  238.     if ( bsize )
  239.     {    /* Discard leading blank rows. */
  240.         int offset;
  241.         register byte *p = bits;
  242.         while ( !*p ) ++p;
  243.         offset = (p - bits) / raster;
  244.         if ( offset )
  245.         {    uint diff = offset * raster;
  246.             bsize -= diff;
  247.             memcpy((char *)bits, (char *)bits + diff, bsize);
  248.             cc->offset.y -= int2fixed(offset);
  249.         }
  250.     }
  251.     cc->height = bsize / raster;
  252.     /* Discard the memory device overhead that follows the bits. */
  253.     {    uint diff = gdev_mem_bitmap_size(dev) - bsize;
  254.         if ( diff >= sizeof(cached_char_head) )
  255.         {    shorten_cached_char(dir, cc, diff);
  256.             dir->bsize -= diff;
  257.             if_debug2('K', "[K]shortening 0x%lx by %u (mdev overhead)\n",
  258.                   (ulong)cc, diff);
  259.         }
  260.     }
  261. }
  262.  
  263. /* Look up a character in the cache. */
  264. /* Return the cached_char or 0. */
  265. cached_char *
  266. gx_lookup_cached_char(const gs_state *pgs, const cached_fm_pair *pair, char_code ccode)
  267. {    gs_font_dir *dir = pgs->font->dir;
  268.     register cached_char *cc = *chars_head(dir, ccode, pair);
  269.     while ( cc != 0 )
  270.       { if ( cc->code == ccode && cc->head.pair == pair )
  271.           return cc;
  272.         cc = cc->next;
  273.       }
  274.     return 0;
  275. }
  276.  
  277. /* Copy a cached character to the screen. */
  278. /* Assume the caller has already done gx_color_load, */
  279. /* and the color is not a halftone. */
  280. /* Return 0 if OK, 1 if we couldn't do the operation but no error */
  281. /* occurred, or a negative error code. */
  282. int
  283. gx_image_cached_char(register gs_show_enum *penum, register const cached_char *cc)
  284. {    register gs_state *pgs = penum->pgs;
  285.     int x, y, w, h;
  286.     int code;
  287.     gs_fixed_point pt;
  288.     gx_device *dev = pgs->device->info;
  289.     gx_device_clip cdev;
  290.     code = gx_path_current_point_inline(pgs->path, &pt);
  291.     if ( code < 0 ) return code;
  292.     /* Abort if the device color isn't pure. */
  293.     if ( !penum->color_loaded )
  294.        {    if ( !color_is_pure(pgs->dev_color) )
  295.             return 1;    /* can't use cache */
  296.         penum->color_loaded = 1;
  297.        }
  298.     /* If the character doesn't lie entirely within the */
  299.     /* quick-check clipping rectangle, we have to */
  300.     /* set up an intermediate clipping device. */
  301.     pt.x -= cc->offset.x;
  302.     x = fixed2int_var_rounded(pt.x) + penum->ftx;
  303.     pt.y -= cc->offset.y;
  304.     y = fixed2int_var_rounded(pt.y) + penum->fty;
  305.     w = cc->width;
  306.     h = cc->height;
  307. #ifdef DEBUG
  308.     if ( gs_debug['K'] )
  309.     {    debug_dump_bytes(cc_bits(cc), cc_bits(cc) + cc->raster * h,
  310.                  "[K]bits");
  311.         dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong)cc,
  312.              fixed2float(-cc->offset.x),
  313.              fixed2float(-cc->offset.y));
  314.         dprintf6("   at (%g,%g)+(%d,%d)->(%d,%d)\n",
  315.              fixed2float(pt.x), fixed2float(pt.y),
  316.              penum->ftx, penum->fty, x, y);
  317.     }
  318. #endif
  319.     if (    x < penum->cxmin || x + w > penum->cxmax ||
  320.         y < penum->cymin || y + h > penum->cymax
  321.        )
  322.        {    cdev = gs_clip_device;
  323.         cdev.target = dev;
  324.         cdev.list = pgs->clip_path->list;
  325.         dev = (gx_device *)&cdev;
  326.         (*dev->procs->open_device)(dev);
  327.         if_debug0('K', "[K](clipping)\n");
  328.        }
  329.     /* Copy the bits. */
  330.     code = (*dev->procs->copy_mono)
  331.         (dev, cc_bits(cc), 0, cc->raster, cc->id,
  332.          x, y, w, h,
  333.          gx_no_color_index, pgs->dev_color->color1);
  334.     gp_check_interrupts();
  335.     return ( code < 0 ? code : 0 );
  336. }
  337.  
  338. /* Purge from the caches all references to a given font. */
  339. void
  340. gs_purge_font_from_char_caches(gs_font_dir *dir, const gs_font *font)
  341. {    cached_fm_pair *pair = dir->mdata;
  342.     int count = dir->mmax;
  343.     if_debug1('k', "[k]purging font 0x%lx\n",
  344.           (ulong)font);
  345.     while ( count-- )
  346.       { if ( pair->font == font ) purge_fm_pair(dir, pair);
  347.         pair++;
  348.       }
  349. }
  350.  
  351. /* ------ Internal routines ------ */
  352.  
  353. /* Shorten a cached character. */
  354. /* diff >= sizeof(cached_char_head). */
  355. private void
  356. shorten_cached_char(gs_font_dir *dir, cached_char *cc, uint diff)
  357. {    cached_char_head *next;
  358.     if ( (byte *)cc + cc->head.size == dir->cdata + dir->cnext )
  359.       dir->cnext -= diff;
  360.     cc->head.size -= diff;
  361.     next = (cached_char_head *)((byte *)cc + cc->head.size);
  362.     if_debug2('K', "[K]shortening creates free block 0x%lx(%u)\n",
  363.           (ulong)next, diff);
  364.     cc_head_set_free(next);
  365.     next->size = diff;
  366. }
  367.  
  368. /* Purge from the caches all references to a given font/matrix pair. */
  369. private void
  370. purge_fm_pair(gs_font_dir *dir, cached_fm_pair *pair)
  371. {    int chi;
  372.     if_debug1('k', "[k]purging pair 0x%lx\n",
  373.           (ulong)pair);
  374.     for ( chi = dir->chars_mask; pair->num_chars != 0; )
  375.       { cached_char **pcc = dir->chars + chi--;
  376.         while ( *pcc != 0 )
  377.           { cached_char *cc = *pcc;
  378.         if ( cc->head.pair == pair )
  379.           { gx_free_cached_char(dir, cc);
  380.             *pcc = cc->next;
  381.           }
  382.         else
  383.           pcc = &cc->next;
  384.           }
  385.       }
  386.     fm_pair_set_free(pair);
  387.     dir->msize--;
  388. }
  389.