home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZFONT.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  330 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. /* zfont.c */
  20. /* Generic font operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"        /* for registering root */
  25. #include "gzstate.h"        /* must precede gxdevice */
  26. #include "gxdevice.h"        /* must precede gxfont */
  27. #include "gschar.h"
  28. #include "gxfont.h"
  29. #include "gxfcache.h"
  30. #include "bfont.h"
  31. #include "ialloc.h"
  32. #include "idict.h"
  33. #include "igstate.h"
  34. #include "iname.h"        /* for name_mark_index */
  35. #include "isave.h"
  36. #include "store.h"
  37. #include "ivmspace.h"
  38.  
  39. /* Imported operators */
  40. extern int zcleartomark(P1(os_ptr));
  41.  
  42. /* Forward references */
  43. private int font_param(P2(os_ptr, gs_font **));
  44. private int make_font(P2(os_ptr, const gs_matrix *));
  45. private void make_uint_array(P3(os_ptr, const uint *, int));
  46.  
  47. /* The (global) font directory */
  48. gs_font_dir *ifont_dir = 0;        /* needed for buildfont */
  49. private gs_gc_root_t font_dir_root;
  50.  
  51. /* Initialize the font operators */
  52. private bool
  53. cc_mark_glyph_name(gs_glyph glyph)
  54. {    return name_mark_index((uint)glyph);
  55. }
  56. private void
  57. zfont_init(void)
  58. {    ifont_dir = gs_font_dir_alloc(imemory);
  59.     ifont_dir->ccache.mark_glyph = cc_mark_glyph_name;
  60.     gs_register_struct_root(imemory, &font_dir_root,
  61.                 (void **)&ifont_dir, "ifont_dir");
  62. }
  63.  
  64. /* <font> <scale> scalefont <new_font> */
  65. int
  66. zscalefont(register os_ptr op)
  67. {    int code;
  68.     float scale;
  69.     gs_matrix mat;
  70.     if ( (code = num_params(op, 1, &scale)) < 0 ) return code;
  71.     if ( (code = gs_make_scaling(scale, scale, &mat)) < 0 ) return code;
  72.     return make_font(op, &mat);
  73. }
  74.  
  75. /* <font> <matrix> makefont <new_font> */
  76. int
  77. zmakefont(register os_ptr op)
  78. {    int code;
  79.     gs_matrix mat;
  80.     if ( (code = read_matrix(op, &mat)) < 0 ) return code;
  81.     return make_font(op, &mat);
  82. }
  83.  
  84. /* <font> setfont - */
  85. int
  86. zsetfont(register os_ptr op)
  87. {    gs_font *pfont;
  88.     int code = font_param(op, &pfont);
  89.     if ( code < 0 || (code = gs_setfont(igs, pfont)) < 0 )
  90.         return code;
  91.     pop(1);
  92.     return code;
  93. }
  94.  
  95. /* - currentfont <font> */
  96. int
  97. zcurrentfont(register os_ptr op)
  98. {    push(1);
  99.     *op = *pfont_dict(gs_currentfont(igs));
  100.     return 0;
  101. }
  102.  
  103. /* - cachestatus <mark> <bsize> <bmax> <msize> <mmax> <csize> <cmax> <blimit> */
  104. int
  105. zcachestatus(register os_ptr op)
  106. {    uint status[7];
  107.     gs_cachestatus(ifont_dir, status);
  108.     push(7);
  109.     make_uint_array(op - 6, status, 7);
  110.     return 0;
  111. }
  112.  
  113. /* <blimit> setcachelimit - */
  114. int
  115. zsetcachelimit(register os_ptr op)
  116. {    check_int_leu(*op, max_uint);
  117.     gs_setcachelimit(ifont_dir, (uint)op->value.intval);
  118.     pop(1);
  119.     return 0;
  120. }
  121.  
  122. /* <mark> <size> <lower> <upper> setcacheparams - */
  123. int
  124. zsetcacheparams(register os_ptr op)
  125. {    uint params[2];
  126.     int i, code;
  127.     os_ptr pp = op;
  128.     for ( i = 0; i < 2 && !r_has_type(pp, t_mark) ; i++, pp-- )
  129.        {    check_int_leu(*pp, max_uint);
  130.         params[i] = pp->value.intval;
  131.        }
  132.     switch ( i )
  133.        {
  134.     case 2:
  135.         if ( (code = gs_setcachelower(ifont_dir, params[1])) < 0 )
  136.             return code;
  137.     case 1:
  138.         if ( (code = gs_setcacheupper(ifont_dir, params[0])) < 0 )
  139.             return code;
  140.     case 0: ;
  141.        }
  142.     return zcleartomark(op);
  143. }
  144.  
  145. /* - currentcacheparams <mark> <size> <lower> <upper> */
  146. int
  147. zcurrentcacheparams(register os_ptr op)
  148. {    uint params[2];
  149.     params[0] = gs_currentcachelower(ifont_dir);
  150.     params[1] = gs_currentcacheupper(ifont_dir);
  151.     push(3);
  152.     make_mark(op - 2);
  153.     make_uint_array(op - 1, params, 2);
  154.     return 0;
  155. }
  156.  
  157. /* ------ Initialization procedure ------ */
  158.  
  159. op_def zfont_op_defs[] = {
  160.     {"0currentfont", zcurrentfont},
  161.     {"2makefont", zmakefont},
  162.     {"2scalefont", zscalefont},
  163.     {"1setfont", zsetfont},
  164.     {"0cachestatus", zcachestatus},
  165.     {"1setcachelimit", zsetcachelimit},
  166.     {"1setcacheparams", zsetcacheparams},
  167.     {"0currentcacheparams", zcurrentcacheparams},
  168.     op_def_end(zfont_init)
  169. };
  170.  
  171. /* ------ Subroutines ------ */
  172.  
  173. /* Validate a font parameter. */
  174. private int
  175. font_param(os_ptr fp, gs_font **pfont)
  176. {    /* Check that fp is a read-only dictionary, */
  177.     /* and that it has a FID entry. */
  178.     ref *pid;
  179.     check_type(*fp, t_dictionary);
  180.     if ( dict_find_string(fp, "FID", &pid) <= 0 )
  181.         return_error(e_invalidfont);
  182.     *pfont = r_ptr(pid, gs_font);
  183.     if ( *pfont == 0 )
  184.         return_error(e_invalidfont); /* unregistered font */
  185.     return 0;
  186. }
  187.  
  188. /* Add the FID entry to a font dictionary. */
  189. int
  190. add_FID(ref *fp    /* t_dictionary */,  gs_font *pfont)
  191. {    ref fid;
  192.     make_tav_new(&fid, t_fontID, a_readonly | ilocal_attr,
  193.              pstruct, (void *)pfont);
  194.     return dict_put_string(fp, "FID", &fid);
  195. }
  196.  
  197. /* Make a transformed font (common code for makefont/scalefont). */
  198. private int
  199. make_font(os_ptr op, const gs_matrix *pmat)
  200. {    os_ptr fp = op - 1;
  201.     gs_font *oldfont, *newfont;
  202.     bool local = ialloc_is_local(idmemory);
  203.     int code;
  204.     ialloc_set_local(idmemory, r_is_local(fp));
  205.     code = font_param(fp, &oldfont);
  206.     if (code >= 0) code = gs_makefont(ifont_dir, oldfont, pmat, &newfont);
  207.     ialloc_set_local(idmemory, local);
  208.     if ( code < 0 )
  209.         return code;
  210.     *fp = *pfont_dict(newfont);
  211.     pop(1);
  212.     return 0;
  213. }
  214. /* Create the transformed font dictionary. */
  215. /* This is the make_font completion procedure for all non-composite fonts */
  216. /* created at the interpreter level (see build_gs_simple_font in zfont2.c.) */
  217. int
  218. zdefault_make_font(gs_font_dir *pdir, const gs_font *oldfont,
  219.   const gs_matrix *pmat, gs_font **ppfont)
  220. {    gs_font *newfont = *ppfont;
  221.     ref *fp = pfont_dict(oldfont);
  222.     font_data *pdata;
  223.     ref newdict, newmat, scalemat;
  224.     uint dlen = dict_maxlength(fp);
  225.     uint mlen = dict_length(fp) + 3;    /* FontID, OrigFont, ScaleMatrix */
  226.     int code;
  227.     if ( dlen < mlen )
  228.       dlen = mlen;
  229.     if ( (pdata = ialloc_struct(font_data, &st_font_data,
  230.                     "make_font(font_data)")) == 0
  231.        )
  232.       return_error(e_VMerror);
  233.     if ( (code = dict_create(dlen, &newdict)) < 0 ||
  234.          (code = dict_copy(fp, &newdict)) < 0 ||
  235.          (code = ialloc_ref_array(&newmat, a_all, 12, "make_font(matrices)")) < 0
  236.        )
  237.       return code;
  238.     refset_null(newmat.value.refs, 12);
  239.     /* Create the scaling matrix. */
  240.     make_array_new(&scalemat, a_all, 6, newmat.value.refs + 6);
  241.     write_matrix(&scalemat, pmat);
  242.     r_clear_attrs(&scalemat, a_write);
  243.     r_set_size(&newmat, 6);
  244.     write_matrix(&newmat, &newfont->FontMatrix);
  245.     r_clear_attrs(&newmat, a_write);
  246.     if ( (code = dict_put_string(&newdict, "FontMatrix", &newmat)) < 0 ||
  247.          (code = dict_put_string(&newdict, "OrigFont", fp)) < 0 ||
  248.          (code = dict_put_string(&newdict, "ScaleMatrix", &scalemat)) < 0 ||
  249.          (code = add_FID(&newdict, newfont)) < 0
  250.        )
  251.       return code;
  252.     newfont->client_data = pdata;
  253.     *pdata = *pfont_data(oldfont);
  254.     pdata->dict = newdict;
  255.     r_clear_attrs(dict_access_ref(&newdict), a_write);
  256.     return 0;
  257. }
  258.  
  259. /* Convert an array of (unsigned) integers to stack form. */
  260. private void
  261. make_uint_array(register os_ptr op, const uint *intp, int count)
  262. {    int i;
  263.     for ( i = 0; i < count; i++, op++, intp++ )
  264.         make_int(op, *intp);
  265. }
  266.  
  267. /* Remove scaled font and character cache entries that would be */
  268. /* invalidated by a restore. */
  269. private bool
  270. purge_if_name_removed(cached_char *cc, void *vsave)
  271. {    return alloc_name_index_is_since_save(cc->code, vsave);
  272. }
  273. void
  274. font_restore(const alloc_save_t *save)
  275. {    gs_font_dir *pdir = ifont_dir;
  276.     if ( pdir == 0 )        /* not initialized yet */
  277.         return;
  278.  
  279.     /* Purge original (unscaled) fonts. */
  280.  
  281.     {    gs_font *pfont;
  282. otop:        for ( pfont = pdir->orig_fonts; pfont != 0;
  283.               pfont = pfont->next
  284.             )
  285.         { if ( alloc_is_since_save((char *)pfont, save) )
  286.            { gs_purge_font(pfont); goto otop; }
  287.         }
  288.     }
  289.  
  290.     /* Purge cached scaled fonts. */
  291.  
  292.     {    gs_font *pfont;
  293. top:        for ( pfont = pdir->scaled_fonts; pfont != 0;
  294.               pfont = pfont->next
  295.             )
  296.         { if ( alloc_is_since_save((char *)pfont, save) )
  297.            { gs_purge_font(pfont); goto top; }
  298.         }
  299.     }
  300.  
  301.     /* Purge xfonts and uncached scaled fonts. */
  302.  
  303.     {    cached_fm_pair *pair;
  304.         uint n;
  305.         for ( pair = pdir->fmcache.mdata, n = pdir->fmcache.mmax;
  306.               n > 0; pair++, n--
  307.             )
  308.           if ( !fm_pair_is_free(pair) )
  309.         {    if ( (pair->font != 0 &&
  310.                   alloc_is_since_save((char *)pair->font, save)) ||
  311.                  (uid_is_XUID(&pair->UID) &&
  312.                   alloc_is_since_save((char *)pair->UID.xvalues,
  313.                           save))
  314.                )
  315.                 gs_purge_fm_pair(pdir, pair, 0);
  316.             else
  317.             if ( pair->xfont != 0 &&
  318.                  alloc_is_since_save((char *)pair->xfont, save)
  319.                )
  320.                 gs_purge_fm_pair(pdir, pair, 1);
  321.         }
  322.     }
  323.  
  324.     /* Purge characters with names about to be removed. */
  325.  
  326.     gx_purge_selected_cached_chars(pdir, purge_if_name_removed,
  327.                        (void *)save);
  328.  
  329. }
  330.