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 / GSFONT.C < prev    next >
C/C++ Source or Header  |  1992-07-29  |  9KB  |  281 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. /* gsfont.c */
  21. /* Font operators 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 "gzstate.h"            /* must precede gxdevice */
  28. #include "gxdevice.h"            /* must precede gxfont */
  29. #include "gschar.h"
  30. #include "gxfont.h"
  31. #include "gxfdir.h"
  32.  
  33. /* Imported procedures */
  34. void    gs_purge_font_from_char_caches(P2(gs_font_dir *, const gs_font *));
  35.  
  36. /* Size of cache structures */
  37. extern const uint cached_char_sizeof;
  38. extern const uint cached_fm_pair_sizeof;
  39.  
  40. /* Define the sizes of the various aspects of the font/character cache. */
  41. /*** Big memory machines ***/
  42. #define smax_LARGE 50        /* smax - # of scaled fonts */
  43. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  44. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  45. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  46. #define blimit_LARGE 1000    /* blimit/upper - max size of a single cached char */
  47. /*** Small memory machines ***/
  48. #define smax_SMALL 20        /* smax - # of scaled fonts */
  49. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  50. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  51. #define cmax_SMALL 500        /* cmax - # of cached chars */
  52. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  53.  
  54. /* Allocate a font directory */
  55. gs_font_dir *
  56. gs_font_dir_alloc(proc_alloc_t palloc, proc_free_t pfree)
  57. {    /* Try allocating a very large cache. */
  58.     /* If this fails, allocate a small one. */
  59.     gs_font_dir *pdir;
  60. #if !arch_ints_are_short
  61.     pdir = gs_font_dir_alloc_limits(palloc, pfree,
  62.                     smax_LARGE, bmax_LARGE, mmax_LARGE,
  63.                     cmax_LARGE, blimit_LARGE);
  64.     if ( pdir != 0 ) return pdir;
  65. #endif
  66.     return gs_font_dir_alloc_limits(palloc, pfree,
  67.                     smax_SMALL, bmax_SMALL, mmax_SMALL,
  68.                     cmax_SMALL, blimit_SMALL);
  69. }
  70. gs_font_dir *
  71. gs_font_dir_alloc_limits(proc_alloc_t palloc, proc_free_t pfree,
  72.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  73. {    register gs_font_dir *pdir = (gs_font_dir *)(*palloc)(1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  74.     uint cdcount = bmax / cached_char_sizeof + 1;
  75.     uint cdsize = cdcount * cached_char_sizeof;
  76.     uint chsize = (cmax / 5) | 31;        /* a guess */
  77.     struct cached_fm_pair_s *mdata;
  78.     byte *cdata;
  79.     struct cached_char_s **chars;
  80.     if ( pdir == 0 ) return 0;
  81.     /* Round up chsize to a power of 2. */
  82.     while ( chsize & (chsize + 1) ) chsize |= chsize >> 1;
  83.     chsize++;
  84.     mdata = (struct cached_fm_pair_s *)(*palloc)(mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  85.     cdata = (byte *)(*palloc)(cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  86.     chars = (struct cached_char_s **)(*palloc)(chsize, sizeof(struct cached_char_s *), "font_dir_alloc(chars)");
  87.     if ( mdata == 0 || cdata == 0 || chars == 0 )
  88.        {    if ( chars != 0 ) (*pfree)((char *)chars, chsize, sizeof(struct cached_char_s *), "font_dir_alloc(chars)");
  89.         if ( cdata != 0 ) (*pfree)((char *)cdata, cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  90.         if ( mdata != 0 ) (*pfree)((char *)mdata, mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  91.         (*pfree)((char *)pdir, 1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  92.         return 0;
  93.        }
  94.     memset((char *)pdir, 0, sizeof(gs_font_dir));    /* easiest to clear everything first */
  95.     pdir->alloc = palloc;
  96.     pdir->free = pfree;
  97.     pdir->smax = smax;
  98.     pdir->bmax = bmax;
  99.     pdir->mmax = mmax;
  100.     pdir->cmax = cmax;
  101.     pdir->lower = upper / 10;
  102.     pdir->upper = upper;
  103.     pdir->mdata = mdata;
  104.     pdir->cdata = cdata;
  105.     pdir->cdata_size = cdsize;
  106.     pdir->chars = chars;
  107.     pdir->chars_mask = chsize - 1;
  108.     gx_char_cache_init(pdir);
  109.     return pdir;
  110. }
  111.  
  112. /* Macro for linking an element at the head of a chain */
  113. #define link_first(first, elt)\
  114.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  115.   elt->prev = 0;\
  116.   first = elt
  117.  
  118. /* scalefont */
  119. int
  120. gs_scalefont(gs_font_dir *pdir, const gs_font *pfont, floatp scale,
  121.   gs_font **ppfont, gs_font **pdfont)
  122. {    gs_matrix mat;
  123.     gs_make_scaling(scale, scale, &mat);
  124.     return gs_makefont(pdir, pfont, &mat, ppfont, pdfont);
  125. }
  126.  
  127. /* makefont */
  128. int
  129. gs_makefont(gs_font_dir *pdir, const gs_font *pfont, const gs_matrix *pmat,
  130.   gs_font **ppfont, gs_font **pdfont)
  131. {    int code;
  132.     gs_font *prev = 0, *pf_out = pdir->scaled_fonts;
  133.     gs_matrix newmat;
  134.     *pdfont = 0;
  135.     gs_make_identity(&newmat);    /* fill in tags */
  136.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  137.       return code;
  138.     /* Check for the font already being in the scaled font cache. */
  139.     /* Only attempt to share fonts if the current font has */
  140.     /* a real UniqueID (i.e., not -1). */
  141. #ifdef DEBUG
  142. if ( gs_debug['m'] )
  143.    {    dprintf2("[m]UniqueID=%ld, FontType=%d,\n",
  144.       pfont->data.base.UniqueID, pfont->FontType);
  145.     dprintf6("[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  146.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  147.       pmat->tx, pmat->ty);
  148.    }
  149. #endif
  150.     if ( pfont->data.base.UniqueID != -1 )
  151.       for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  152.         if (    pf_out->data.base.UniqueID == pfont->data.base.UniqueID &&
  153.             pf_out->FontType == pfont->FontType &&
  154.             pf_out->FontMatrix.xx == newmat.xx &&
  155.             pf_out->FontMatrix.xy == newmat.xy &&
  156.             pf_out->FontMatrix.yx == newmat.yx &&
  157.             pf_out->FontMatrix.yy == newmat.yy &&
  158.             pf_out->FontMatrix.tx == newmat.tx &&
  159.             pf_out->FontMatrix.ty == newmat.ty
  160.            )
  161.            {    *ppfont = pf_out;
  162.             if_debug1('m', "[m]found font=%lx\n", (ulong)pf_out);
  163.             return 0;
  164.            }
  165.     pf_out = (gs_font *)(*pdir->alloc)(1, sizeof(gs_font), "gs_makefont");
  166.     if ( !pf_out ) return_error(gs_error_VMerror);
  167.     *pf_out = *pfont;
  168.     pf_out->FontMatrix = newmat;
  169.     if ( pdir->ssize == pdir->smax )
  170.       { /* Must discard a cached scaled font. */
  171.         /* Scan for the oldest font if we didn't already. */
  172.         if ( !prev )
  173.           for ( prev = pdir->scaled_fonts;
  174.             prev->next != 0;
  175.             prev = prev->next
  176.           ) ;
  177.         if_debug1('m', "[m]discarding font %lx\n", (ulong)prev);
  178.         *pdfont = prev;
  179.         prev->prev->next = 0;
  180.       }
  181.     else
  182.       pdir->ssize++;
  183.     link_first(pdir->scaled_fonts, pf_out);
  184.     pf_out->base = pfont->base;
  185.     pf_out->dir = pdir;
  186.     *ppfont = pf_out;
  187.     if_debug1('m', "[m]new font=%lx\n", (ulong)pf_out);
  188.     return 1;
  189. }
  190.  
  191. /* setfont */
  192. int
  193. gs_setfont(gs_state *pgs, gs_font *pfont)
  194. {    pgs->font = pfont;
  195.     pgs->char_tm_valid = 0;
  196.     return 0;
  197. }
  198.  
  199. /* currentfont */
  200. gs_font *
  201. gs_currentfont(const gs_state *pgs)
  202. {    return pgs->font;
  203. }
  204.  
  205. /* cachestatus */
  206. void
  207. gs_cachestatus(register const gs_font_dir *pdir, register uint pstat[7])
  208. {    pstat[0] = pdir->bsize;
  209.     pstat[1] = pdir->bmax;
  210.     pstat[2] = pdir->msize;
  211.     pstat[3] = pdir->mmax;
  212.     pstat[4] = pdir->csize;
  213.     pstat[5] = pdir->cmax;
  214.     pstat[6] = pdir->upper;
  215. }
  216.  
  217. /* setcachelimit */
  218. int
  219. gs_setcachelimit(gs_font_dir *pdir, uint size)
  220. {    pdir->upper = size;
  221.     return 0;
  222. }
  223.  
  224. /* setcacheparams */
  225. int
  226. gs_setcachelower(gs_font_dir *pdir, uint size)
  227. {    pdir->lower = size;
  228.     return 0;
  229. }
  230. int
  231. gs_setcacheupper(gs_font_dir *pdir, uint size)
  232. {    pdir->upper = size;
  233.     return 0;
  234. }
  235.  
  236. /* currentcacheparams */
  237. uint
  238. gs_currentcachelower(const gs_font_dir *pdir)
  239. {    return pdir->lower;
  240. }
  241. uint
  242. gs_currentcacheupper(const gs_font_dir *pdir)
  243. {    return pdir->upper;
  244. }
  245.  
  246. /* Dummy (ineffective) BuildChar procedure */
  247. int
  248. gs_no_build_char_proc(struct gs_show_enum_s *penum, gs_state *pgs,
  249.   gs_font *pfont, char_code chr, char *data)
  250. {    return 1;            /* failure, but not error */
  251. }
  252.  
  253. /* Purge a font from the font- and character-related caches. */
  254. /* This is only used by restore (and, someday, the GC). */
  255. void
  256. gs_purge_font_from_caches(gs_font_dir *pdir, const gs_font *pfont)
  257. {
  258.     /* Purge the font from the scaled font cache. */
  259.     gs_font *pf = pdir->scaled_fonts;
  260.     while ( pf != 0 )
  261.       { if ( pf == pfont )
  262.           { pf = pf->next;
  263.         if ( pfont->prev ) pfont->prev->next = pf;
  264.         else pdir->scaled_fonts = pf;
  265.         if ( pf ) pf->prev = pfont->prev;
  266.         pdir->ssize--;
  267.           }
  268.         else if ( pf->base == pfont )
  269.           { gs_purge_font_from_caches(pdir, pf);
  270.         pf = pdir->scaled_fonts; /* start over */
  271.           }
  272.         else
  273.           pf = pf->next;
  274.        }
  275.  
  276.     /* Purge the font from the font/matrix pair cache, */
  277.     /* including all cached characters rendered with that font. */
  278.     gs_purge_font_from_char_caches(pdir, pfont);
  279.  
  280. }
  281.