home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / gsfont < prev    next >
Encoding:
Text File  |  1991-10-26  |  7.6 KB  |  260 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. /* 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 *, 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. /* Allocate a font directory */
  41. gs_font_dir *
  42. gs_font_dir_alloc(proc_alloc_t palloc, proc_free_t pfree)
  43. {    return gs_font_dir_alloc_limits(palloc, pfree,
  44.         10,            /* smax - # of scaled fonts */
  45.         25000,            /* bmax - space for cached chars */
  46.         20,            /* mmax - # of cached font/matrix pairs */
  47.         500,            /* cmax - # of cached chars */
  48.         100);            /* blimit/upper - max size of a single cached char */
  49. }
  50. gs_font_dir *
  51. gs_font_dir_alloc_limits(proc_alloc_t palloc, proc_free_t pfree,
  52.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  53. {    register gs_font_dir *pdir = (gs_font_dir *)(*palloc)(1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  54.     uint cdcount = bmax / cached_char_sizeof + 1;
  55.     uint cdsize = cdcount * cached_char_sizeof;
  56.     struct cached_fm_pair_s *mdata;
  57.     struct cached_char_s *cdata;
  58.     if ( pdir == 0 ) return 0;
  59.     mdata = (struct cached_fm_pair_s *)(*palloc)(mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  60.     cdata = (struct cached_char_s *)(*palloc)(cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  61.     if ( mdata == 0 || cdata == 0 )
  62.        {    if ( cdata != 0 ) (*pfree)((char *)cdata, cdcount, cached_char_sizeof, "font_dir_alloc(cdata)");
  63.         if ( mdata != 0 ) (*pfree)((char *)mdata, mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  64.         (*pfree)((char *)pdir, 1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  65.         return 0;
  66.        }
  67.     memset((char *)pdir, 0, sizeof(gs_font_dir));    /* easiest to clear everything first */
  68.     pdir->alloc = palloc;
  69.     pdir->free = pfree;
  70.     pdir->smax = smax;
  71.     pdir->bmax = bmax;
  72.     pdir->mmax = mmax;
  73.     pdir->cmax = cmax;
  74.     pdir->lower = upper / 10;
  75.     pdir->upper = upper;
  76.     pdir->mdata = mdata;
  77.     pdir->cdata = cdata;
  78.     pdir->cdata_size = pdir->cbot = pdir->ctop = cdsize;
  79.     return pdir;
  80. }
  81.  
  82. /* Macro for linking an element at the head of a chain */
  83. #define link_first(first, elt)\
  84.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  85.   elt->prev = 0;\
  86.   first = elt
  87.  
  88. /* scalefont */
  89. int
  90. gs_scalefont(gs_font_dir *pdir, gs_font *pfont, floatp scale,
  91.   gs_font **ppfont, gs_font **pdfont)
  92. {    gs_matrix mat;
  93.     gs_make_scaling(scale, scale, &mat);
  94.     return gs_makefont(pdir, pfont, &mat, ppfont, pdfont);
  95. }
  96.  
  97. /* makefont */
  98. int
  99. gs_makefont(gs_font_dir *pdir, gs_font *pfont, gs_matrix *pmat,
  100.   gs_font **ppfont, gs_font **pdfont)
  101. {    int code;
  102.     gs_font *prev = 0, *pf_out = pdir->scaled_fonts;
  103.     gs_matrix newmat;
  104.     *pdfont = 0;
  105.     gs_make_identity(&newmat);    /* fill in tags */
  106.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  107.       return code;
  108.     /* Check for the font already being in the scaled font cache. */
  109.     /* Only attempt to share fonts if the current font has */
  110.     /* a real UniqueID (i.e., not -1). */
  111. #ifdef DEBUG
  112. if ( gs_debug['m'] )
  113.    {    dprintf2("[m]UniqueID=%ld, FontType=%d,\n",
  114.       pfont->data.base.UniqueID, pfont->FontType);
  115.     dprintf6("[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  116.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  117.       pmat->tx, pmat->ty);
  118.    }
  119. #endif
  120.     if ( pfont->data.base.UniqueID != -1 )
  121.       for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  122.         if (    pf_out->data.base.UniqueID == pfont->data.base.UniqueID &&
  123.             pf_out->FontType == pfont->FontType &&
  124.             pf_out->FontMatrix.xx == newmat.xx &&
  125.             pf_out->FontMatrix.xy == newmat.xy &&
  126.             pf_out->FontMatrix.yx == newmat.yx &&
  127.             pf_out->FontMatrix.yy == newmat.yy &&
  128.             pf_out->FontMatrix.tx == newmat.tx &&
  129.             pf_out->FontMatrix.ty == newmat.ty
  130.            )
  131.            {    *ppfont = pf_out;
  132. #ifdef DEBUG
  133. if ( gs_debug['m'] )
  134.             dprintf1("[m]found font=%lx\n", (ulong)pf_out);
  135. #endif
  136.             return 0;
  137.            }
  138.     pf_out = (gs_font *)(*pdir->alloc)(1, sizeof(gs_font), "gs_makefont");
  139.     if ( !pf_out ) return_error(gs_error_VMerror);
  140.     *pf_out = *pfont;
  141.     pf_out->FontMatrix = newmat;
  142.     if ( pdir->ssize == pdir->smax )
  143.       { /* Must discard a cached scaled font. */
  144.         /* Scan for the oldest font if we didn't already. */
  145.         if ( !prev )
  146.           for ( prev = pdir->scaled_fonts;
  147.             prev->next != 0;
  148.             prev = prev->next
  149.           ) ;
  150. #ifdef DEBUG
  151. if ( gs_debug['m'] )
  152.         dprintf1("[m]discarding font %lx\n", (ulong)prev);
  153. #endif
  154.         *pdfont = prev;
  155.         prev->prev->next = 0;
  156.       }
  157.     else
  158.       pdir->ssize++;
  159.     link_first(pdir->scaled_fonts, pf_out);
  160.     pf_out->base = pfont->base;
  161.     pf_out->dir = pdir;
  162.     *ppfont = pf_out;
  163. #ifdef DEBUG
  164. if ( gs_debug['m'] )
  165.     dprintf1("[m]new font=%lx\n", (ulong)pf_out);
  166. #endif
  167.     return 1;
  168. }
  169.  
  170. /* setfont */
  171. int
  172. gs_setfont(gs_state *pgs, gs_font *pfont)
  173. {    pgs->font = pfont;
  174.     pgs->char_tm_valid = 0;
  175.     return 0;
  176. }
  177.  
  178. /* currentfont */
  179. gs_font *
  180. gs_currentfont(gs_state *pgs)
  181. {    return pgs->font;
  182. }
  183.  
  184. /* cachestatus */
  185. void
  186. gs_cachestatus(register gs_font_dir *pdir, register uint pstat[7])
  187. {    pstat[0] = pdir->bsize;
  188.     pstat[1] = pdir->bmax;
  189.     pstat[2] = pdir->msize;
  190.     pstat[3] = pdir->mmax;
  191.     pstat[4] = pdir->csize;
  192.     pstat[5] = pdir->cmax;
  193.     pstat[6] = pdir->upper;
  194. }
  195.  
  196. /* setcachelimit */
  197. int
  198. gs_setcachelimit(gs_font_dir *pdir, uint size)
  199. {    pdir->upper = size;
  200.     return 0;
  201. }
  202.  
  203. /* setcacheparams */
  204. int
  205. gs_setcachelower(gs_font_dir *pdir, uint size)
  206. {    pdir->lower = size;
  207.     return 0;
  208. }
  209. int
  210. gs_setcacheupper(gs_font_dir *pdir, uint size)
  211. {    pdir->upper = size;
  212.     return 0;
  213. }
  214.  
  215. /* currentcacheparams */
  216. uint
  217. gs_currentcachelower(gs_font_dir *pdir)
  218. {    return pdir->lower;
  219. }
  220. uint
  221. gs_currentcacheupper(gs_font_dir *pdir)
  222. {    return pdir->upper;
  223. }
  224.  
  225. /* Dummy (ineffective) BuildChar procedure */
  226. int
  227. gs_no_build_char_proc(struct gs_show_enum_s *penum, gs_state *pgs,
  228.   gs_font *pfont, int chr, char *data)
  229. {    return 1;            /* failure, but not error */
  230. }
  231.  
  232. /* Purge a font from the font- and character-related caches. */
  233. /* This is only used by restore (and, someday, the GC). */
  234. void
  235. gs_purge_font_from_caches(gs_font_dir *pdir, gs_font *pfont)
  236. {
  237.     /* Purge the font from the scaled font cache. */
  238.     gs_font *pf = pdir->scaled_fonts;
  239.     while ( pf != 0 )
  240.       { if ( pf == pfont )
  241.           { pf = pf->next;
  242.         if ( pfont->prev ) pfont->prev->next = pf;
  243.         else pdir->scaled_fonts = pf;
  244.         if ( pf ) pf->prev = pfont->prev;
  245.         pdir->ssize--;
  246.           }
  247.         else if ( pf->base == pfont )
  248.           { gs_purge_font_from_caches(pdir, pf->base);
  249.         pf = pdir->scaled_fonts; /* start over */
  250.           }
  251.         else
  252.           pf = pf->next;
  253.        }
  254.  
  255.     /* Purge the font from the font/matrix pair cache, */
  256.     /* including all cached characters rendered with that font. */
  257.     gs_purge_font_from_char_caches(pdir, pfont);
  258.  
  259. }
  260.