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

  1. /* Copyright (C) 1989, 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. /* gsfont.c */
  20. /* Font operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.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 "gxfcache.h"
  32.  
  33. /* Imported procedures */
  34. void    gs_purge_font_from_char_caches(P2(gs_font_dir *, const gs_font *));
  35.  
  36. /* Define the sizes of the various aspects of the font/character cache. */
  37. /*** Big memory machines ***/
  38. #define smax_LARGE 50        /* smax - # of scaled fonts */
  39. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  40. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  41. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  42. #define blimit_LARGE 2500    /* blimit/upper - max size of a single cached char */
  43. /*** Small memory machines ***/
  44. #define smax_SMALL 20        /* smax - # of scaled fonts */
  45. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  46. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  47. #define cmax_SMALL 500        /* cmax - # of cached chars */
  48. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  49.  
  50. private_st_font_dir();
  51. public_st_gs_font();
  52. public_st_gs_font_base();
  53. private_st_gs_font_ptr();
  54. public_st_gs_font_ptr_element();
  55.  
  56. /* Font directory GC procedures */
  57. private ENUM_PTRS_BEGIN(font_dir_enum_ptrs) return 0;
  58. #define e1(i,elt) ENUM_PTR(i,gs_font_dir,elt);
  59.     font_dir_do_ptrs(e1)
  60. #undef e1
  61.     case st_font_dir_max_ptrs:
  62.     {    /* Mark the cached character glyphs. */
  63.         /* See gxfcache.h for why we do this here. */
  64. #define dir ((gs_font_dir *)vptr)
  65.         cc_mark_glyph_proc_t mark_glyph = dir->ccache.mark_glyph;
  66.         int chi;
  67.         cached_char *cc;
  68.         for ( chi = dir->ccache.chars_mask; --chi >= 0; )
  69.           for ( cc = dir->ccache.chars[chi]; cc != 0; cc = cc->next )
  70.             (*mark_glyph)(cc->code);
  71.     }    return 0;
  72. #undef dir
  73. ENUM_PTRS_END
  74. private RELOC_PTRS_BEGIN(font_dir_reloc_ptrs) ;
  75.     /* Relocate the pointers from cached characters to f/m pairs. */
  76.     /* See gxfcache.h for why we do this here. */
  77. #define dir ((gs_font_dir *)vptr)
  78.     {    int chi;
  79.         for ( chi = dir->ccache.chars_mask; --chi >= 0; )
  80.         {    cached_char *cc = dir->ccache.chars[chi];
  81.             while ( cc != 0 )
  82.             {    cached_fm_pair *pair = cc->head.pair;
  83.                 cc->head.pair = (cached_fm_pair *)
  84.                   gs_reloc_struct_ptr(pair - pair->index, gcst) +
  85.                   pair->index;
  86.                 cc = cc->next;
  87.             }
  88.         }
  89.     }
  90. #undef dir
  91.     /* We have to relocate the cached characters before we */
  92.     /* relocate dir->ccache.chars! */
  93. #define r1(i,elt) RELOC_PTR(gs_font_dir,elt);
  94.     font_dir_do_ptrs(r1)
  95. #undef r1
  96. RELOC_PTRS_END
  97.  
  98. /* GC procedures for fonts */
  99. private ENUM_PTRS_BEGIN(font_enum_ptrs) return 0;
  100.     ENUM_PTR(0, gs_font, next);
  101.     ENUM_PTR(1, gs_font, prev);
  102.     ENUM_PTR(2, gs_font, dir);
  103.     ENUM_PTR(3, gs_font, base);
  104.     ENUM_PTR(4, gs_font, client_data);
  105. ENUM_PTRS_END
  106. private RELOC_PTRS_BEGIN(font_reloc_ptrs) ;
  107.     RELOC_PTR(gs_font, next);
  108.     RELOC_PTR(gs_font, prev);
  109.     RELOC_PTR(gs_font, dir);
  110.     RELOC_PTR(gs_font, base);
  111.     RELOC_PTR(gs_font, client_data);
  112. RELOC_PTRS_END
  113.  
  114. /* Allocate a font directory */
  115. private bool
  116. cc_no_mark_glyph(gs_glyph glyph)
  117. {    return false;
  118. }
  119. gs_font_dir *
  120. gs_font_dir_alloc(gs_memory_t *mem)
  121. {    gs_font_dir *pdir;
  122. #if !arch_ints_are_short
  123.     /* Try allocating a very large cache. */
  124.     /* If this fails, allocate a small one. */
  125.     pdir = gs_font_dir_alloc_limits(mem,
  126.                     smax_LARGE, bmax_LARGE, mmax_LARGE,
  127.                     cmax_LARGE, blimit_LARGE);
  128.     if ( pdir == 0 )
  129. #endif
  130.       pdir = gs_font_dir_alloc_limits(mem,
  131.                       smax_SMALL, bmax_SMALL, mmax_SMALL,
  132.                       cmax_SMALL, blimit_SMALL);
  133.     if ( pdir == 0 )
  134.       return 0;
  135.     pdir->ccache.mark_glyph = cc_no_mark_glyph;
  136.     return pdir;
  137. }
  138. gs_font_dir *
  139. gs_font_dir_alloc_limits(gs_memory_t *mem,
  140.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  141. {    register gs_font_dir *pdir =
  142.       gs_alloc_struct(mem, gs_font_dir, &st_font_dir,
  143.               "font_dir_alloc(dir)");
  144.     int code;
  145.     if ( pdir == 0 )
  146.       return 0;
  147.     code = gx_char_cache_alloc(mem, pdir, bmax, mmax, cmax, upper);
  148.     if ( code < 0 )
  149.     {    gs_free_object(mem, pdir, "font_dir_alloc(dir)");
  150.         return 0;
  151.     }
  152.     pdir->orig_fonts = 0;
  153.     pdir->scaled_fonts = 0;
  154.     pdir->ssize = 0;
  155.     pdir->smax = smax;
  156.     return pdir;
  157. }
  158.  
  159. /* Macro for linking an element at the head of a chain */
  160. #define link_first(first, elt)\
  161.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  162.   elt->prev = 0;\
  163.   first = elt
  164.  
  165. /* definefont */
  166. /* Use this only for original (unscaled) fonts! */
  167. /* Note that it expects pfont->procs.define_font to be set already. */
  168. int
  169. gs_definefont(gs_font_dir *pdir, gs_font *pfont)
  170. {    int code;
  171.     pfont->dir = pdir;
  172.     pfont->base = pfont;
  173.     code = (*pfont->procs.define_font)(pdir, pfont);
  174.     if ( code < 0 )
  175.         return code;
  176.     link_first(pdir->orig_fonts, pfont);
  177.     if_debug1('m', "[m]defining font 0x%lx\n", (ulong)pfont);
  178.     return 0;
  179. }
  180. /* Default (vacuous) definefont handler. */
  181. int
  182. gs_no_define_font(gs_font_dir *pdir, gs_font *pfont)
  183. {    return 0;
  184. }
  185.  
  186. /* scalefont */
  187. int
  188. gs_scalefont(gs_font_dir *pdir, const gs_font *pfont, floatp scale,
  189.   gs_font **ppfont)
  190. {    gs_matrix mat;
  191.     gs_make_scaling(scale, scale, &mat);
  192.     return gs_makefont(pdir, pfont, &mat, ppfont);
  193. }
  194.  
  195. /* makefont */
  196. int
  197. gs_makefont(gs_font_dir *pdir, const gs_font *pfont,
  198.   const gs_matrix *pmat, gs_font **ppfont)
  199. {
  200. #define pbfont ((gs_font_base *)pfont)
  201.     int code;
  202.     gs_font *prev = 0;
  203.     gs_font *pf_out = pdir->scaled_fonts;
  204.     gs_memory_t *mem = pfont->memory;
  205.     gs_matrix newmat;
  206.     bool can_cache;
  207.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  208.       return code;
  209.     /* Check for the font already being in the scaled font cache. */
  210.     /* Only attempt to share fonts if the current font has */
  211.     /* a valid UniqueID or XUID. */
  212. #ifdef DEBUG
  213. if ( gs_debug_c('m') )
  214.    {    if ( pfont->FontType == ft_composite )
  215.       dprintf("[m]composite");
  216.     else if ( uid_is_UniqueID(&pbfont->UID) )
  217.       dprintf1("[m]UniqueID=%ld", pbfont->UID.id);
  218.     else if ( uid_is_XUID(&pbfont->UID) )
  219.       dprintf1("[m]XUID(%u)", (uint)(-pbfont->UID.id));
  220.     else
  221.       dprintf("[m]no UID");
  222.     dprintf7(", FontType=%d,\n[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  223.       pfont->FontType,
  224.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  225.       pmat->tx, pmat->ty);
  226.    }
  227. #endif
  228.     /* The UID of a composite font is of no value in caching.... */
  229.     if ( pfont->FontType != ft_composite &&
  230.          uid_is_valid(&pbfont->UID)
  231.        )
  232.     { for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  233.         if ( pf_out->FontType == pfont->FontType &&
  234.          pf_out->base == pfont->base &&
  235.          uid_equal(&((gs_font_base *)pf_out)->UID, &pbfont->UID) &&
  236.          pf_out->FontMatrix.xx == newmat.xx &&
  237.          pf_out->FontMatrix.xy == newmat.xy &&
  238.          pf_out->FontMatrix.yx == newmat.yx &&
  239.          pf_out->FontMatrix.yy == newmat.yy &&
  240.          pf_out->FontMatrix.tx == newmat.tx &&
  241.          pf_out->FontMatrix.ty == newmat.ty
  242.            )
  243.         {    *ppfont = pf_out;
  244.             if_debug1('m', "[m]found font=0x%lx\n", (ulong)pf_out);
  245.             return 0;
  246.         }
  247.       can_cache = true;
  248.     }
  249.     else
  250.       can_cache = false;
  251.     pf_out = gs_alloc_struct(mem, gs_font, gs_object_type(mem, pfont),
  252.                  "gs_makefont");
  253.     if ( !pf_out )
  254.         return_error(gs_error_VMerror);
  255.     memcpy(pf_out, pfont, gs_object_size(mem, pfont));
  256.     pf_out->FontMatrix = newmat;
  257.     pf_out->client_data = 0;
  258.     pf_out->dir = pdir;
  259.     pf_out->base = pfont->base;
  260.     *ppfont = pf_out;
  261.     code = (*pf_out->procs.make_font)(pdir, pfont, pmat, ppfont);
  262.     if ( code < 0 )
  263.         return code;
  264.     if ( can_cache )
  265.     {    if ( pdir->ssize == pdir->smax )
  266.         {    /* Must discard a cached scaled font. */
  267.             /* prev points to the last (oldest) font. */
  268.             if_debug1('m', "[m]discarding font 0x%lx\n",
  269.                   (ulong)prev);
  270.             prev->prev->next = 0;
  271.             if ( prev->FontType != ft_composite )
  272.             {    uid_free(&((gs_font_base *)prev)->UID,
  273.                      prev->memory,
  274.                      "gs_makefont(discarding)");
  275.                 uid_set_invalid(&((gs_font_base *)prev)->UID);
  276.             }
  277.         }
  278.         else
  279.             pdir->ssize++;
  280.         link_first(pdir->scaled_fonts, pf_out);
  281.     }
  282.     if_debug1('m', "[m]new font=0x%lx\n", (ulong)*ppfont);
  283.     return 1;
  284. #undef pbfont
  285. }
  286. /* Default (vacuous) makefont handler. */
  287. int
  288. gs_no_make_font(gs_font_dir *pdir, const gs_font *pfont,
  289.   const gs_matrix *pmat, gs_font **ppfont)
  290. {    return 0;
  291. }
  292.  
  293. /* setfont */
  294. int
  295. gs_setfont(gs_state *pgs, gs_font *pfont)
  296. {    pgs->font = pgs->root_font = pfont;
  297.     pgs->char_tm_valid = 0;
  298.     return 0;
  299. }
  300.  
  301. /* currentfont */
  302. gs_font *
  303. gs_currentfont(const gs_state *pgs)
  304. {    return pgs->font;
  305. }
  306.  
  307. /* rootfont */
  308. gs_font *
  309. gs_rootfont(const gs_state *pgs)
  310. {    return pgs->root_font;
  311. }
  312.  
  313. /* cachestatus */
  314. void
  315. gs_cachestatus(register const gs_font_dir *pdir, register uint pstat[7])
  316. {    pstat[0] = pdir->ccache.bsize;
  317.     pstat[1] = pdir->ccache.bmax;
  318.     pstat[2] = pdir->fmcache.msize;
  319.     pstat[3] = pdir->fmcache.mmax;
  320.     pstat[4] = pdir->ccache.csize;
  321.     pstat[5] = pdir->ccache.cmax;
  322.     pstat[6] = pdir->ccache.upper;
  323. }
  324.  
  325. /* setcachelimit */
  326. int
  327. gs_setcachelimit(gs_font_dir *pdir, uint size)
  328. {    pdir->ccache.upper = size;
  329.     return 0;
  330. }
  331.  
  332. /* setcacheparams */
  333. int
  334. gs_setcachelower(gs_font_dir *pdir, uint size)
  335. {    pdir->ccache.lower = size;
  336.     return 0;
  337. }
  338. int
  339. gs_setcacheupper(gs_font_dir *pdir, uint size)
  340. {    pdir->ccache.upper = size;
  341.     return 0;
  342. }
  343.  
  344. /* currentcacheparams */
  345. uint
  346. gs_currentcachelower(const gs_font_dir *pdir)
  347. {    return pdir->ccache.lower;
  348. }
  349. uint
  350. gs_currentcacheupper(const gs_font_dir *pdir)
  351. {    return pdir->ccache.upper;
  352. }
  353.  
  354. /* Dummy (ineffective) BuildChar/BuildGlyph procedure */
  355. int
  356. gs_no_build_char(gs_show_enum *penum, gs_state *pgs,
  357.   gs_font *pfont, gs_char chr, gs_glyph glyph)
  358. {    return 1;            /* failure, but not error */
  359. }
  360.  
  361. /* Dummy character encoding procedure */
  362. gs_glyph
  363. gs_no_encode_char(gs_show_enum *penum,
  364.   gs_font *pfont, gs_char *pchr)
  365. {    return gs_no_glyph;
  366. }
  367.  
  368. /* Purge a font from all font- and character-related tables. */
  369. /* This is only used by restore (and, someday, the GC). */
  370. void
  371. gs_purge_font(const gs_font *pfont)
  372. {    gs_font_dir *pdir = pfont->dir;
  373.     gs_font *pf;
  374.  
  375.     /* Remove the font from its list (orig_fonts or scaled_fonts). */
  376.     gs_font *prev = pfont->prev;
  377.     gs_font *next = pfont->next;
  378.     if ( next != 0 )
  379.         next->prev = prev;
  380.     if ( prev != 0 )
  381.         prev->next = next;
  382.     else if ( pdir->orig_fonts == pfont )
  383.         pdir->orig_fonts = next;
  384.     else if ( pdir->scaled_fonts == pfont )
  385.         pdir->scaled_fonts = next;
  386.     else
  387.     {    /* Shouldn't happen! */
  388.         lprintf1("purged font 0x%lx not found\n", (ulong)pfont);
  389.     }
  390.     if ( pfont->base != pfont )
  391.     {    /* I.e., this is a cached scaled font. */
  392.         pdir->ssize--;
  393.     }
  394.  
  395.     /* Purge the font from the scaled font cache. */
  396.     for ( pf = pdir->scaled_fonts; pf != 0; )
  397.     {    if ( pf->base == pfont )
  398.         {    gs_purge_font(pf);
  399.             pf = pdir->scaled_fonts; /* start over */
  400.         }
  401.         else
  402.             pf = pf->next;
  403.     }
  404.  
  405.     /* Purge the font from the font/matrix pair cache, */
  406.     /* including all cached characters rendered with that font. */
  407.     gs_purge_font_from_char_caches(pdir, pfont);
  408.  
  409. }
  410.