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 / ZFONT.C < prev    next >
C/C++ Source or Header  |  1992-05-01  |  7KB  |  276 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. /* zfont.c */
  21. /* Font operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. /*
  26.  * The following lines used to say:
  27.  *    #include "gsmatrix.h"
  28.  *    #include "gxdevice.h"        /. for gxfont.h ./
  29.  * Tony Li says the longer list is necessary to keep the GNU compiler
  30.  * happy, but this is pretty hard to understand....
  31.  */
  32. #include    "gxfixed.h"
  33. #include    "gxmatrix.h"
  34. #include    "gzstate.h"        /* must precede gxdevice */
  35. #include    "gxdevice.h"        /* must precede gxfont */
  36. #include    "gschar.h"
  37. #include "gxfont.h"
  38. #include "gxfdir.h"
  39. #include "alloc.h"
  40. #include "bfont.h"
  41. #include "dict.h"
  42. #include "name.h"
  43. #include "packed.h"
  44. #include "save.h"
  45. #include "state.h"
  46. #include "store.h"
  47.  
  48. /* Imported operators */
  49. extern int zcleartomark(P1(os_ptr));
  50.  
  51. /* Forward references */
  52. private int font_param(P2(os_ptr, gs_font **));
  53. private int make_font(P2(os_ptr, const gs_matrix *));
  54. private void make_uint_array(P3(os_ptr, const uint *, int));
  55.  
  56. /* The (global) font directory */
  57. gs_font_dir *ifont_dir;            /* needed for buildfont */
  58.  
  59. /* Global font-related objects */
  60. ref name_StandardEncoding;        /* only needed for seac */
  61. /* Names of system-known keys in font dictionaries: */
  62. ref name_FontMatrix;            /* needed for buildfont */
  63. ref name_FID;
  64.  
  65. /* Initialize the font operators */
  66. private void
  67. zfont_init()
  68. {    static const names_def fnd[] = {
  69.  
  70.     /* Create the names of the global known objects. */
  71.        { "StandardEncoding", &name_StandardEncoding },
  72.  
  73.     /* Create the names of the standard elements of */
  74.     /* a font dictionary. */
  75.        { "FontMatrix", &name_FontMatrix },
  76.        { "FID", &name_FID },
  77.  
  78.     /* Mark the end of the initalized name list. */
  79.        names_def_end
  80.     };
  81.  
  82.     ifont_dir = gs_font_dir_alloc(alloc, alloc_free);
  83.     init_names(fnd);
  84. }
  85.  
  86. /* scalefont */
  87. int
  88. zscalefont(register os_ptr op)
  89. {    int code;
  90.     float scale;
  91.     gs_matrix mat;
  92.     if ( (code = num_params(op, 1, &scale)) < 0 ) return code;
  93.     if ( (code = gs_make_scaling(scale, scale, &mat)) < 0 ) return code;
  94.     return make_font(op, &mat);
  95. }
  96.  
  97. /* makefont */
  98. int
  99. zmakefont(register os_ptr op)
  100. {    int code;
  101.     gs_matrix mat;
  102.     if ( (code = read_matrix(op, &mat)) < 0 ) return code;
  103.     return make_font(op, &mat);
  104. }
  105.  
  106. /* setfont */
  107. int
  108. zsetfont(register os_ptr op)
  109. {    gs_font *pfont;
  110.     int code = font_param(op, &pfont);
  111.     if ( code < 0 || (code = gs_setfont(igs, pfont)) < 0 )
  112.       return code;
  113.     istate.font = *op;
  114.     pop(1);
  115.     return code;
  116. }
  117.  
  118. /* currentfont */
  119. int
  120. zcurrentfont(register os_ptr op)
  121. {    push(1);
  122.     *op = istate.font;
  123.     return 0;
  124. }
  125.  
  126. /* cachestatus */
  127. int
  128. zcachestatus(register os_ptr op)
  129. {    uint status[7];
  130.     gs_cachestatus(ifont_dir, status);
  131.     push(7);
  132.     make_uint_array(op - 6, status, 7);
  133.     return 0;
  134. }
  135.  
  136. /* setcachelimit */
  137. int
  138. zsetcachelimit(register os_ptr op)
  139. {    long limit;
  140.     check_type(*op, t_integer);
  141.     limit = op->value.intval;
  142.     if ( (ulong)limit > max_uint )    /* also covers limit < 0 */
  143.         return e_rangecheck;
  144.     gs_setcachelimit(ifont_dir, (uint)limit);
  145.     pop(1);
  146.     return 0;
  147. }
  148.  
  149. /* setcacheparams */
  150. int
  151. zsetcacheparams(register os_ptr op)
  152. {    uint params[2];
  153.     int i, code;
  154.     for ( i = 0; i < 2 && !r_has_type(op - i, t_mark) ; i++ )
  155.        {    long parm;
  156.         check_type(op[-i], t_integer);
  157.         parm = op[-i].value.intval;
  158.         if ( (ulong)parm > max_uint )    /* covers parm < 0 */
  159.             return e_rangecheck;
  160.         params[i] = parm;
  161.        }
  162.     switch ( i )
  163.        {
  164.     case 2:
  165.         if ( (code = gs_setcachelower(ifont_dir, params[1])) < 0 )
  166.             return code;
  167.     case 1:
  168.         if ( (code = gs_setcacheupper(ifont_dir, params[0])) < 0 )
  169.             return code;
  170.     case 0: ;
  171.        }
  172.     return zcleartomark(op);
  173. }
  174.  
  175. /* currentcacheparams */
  176. int
  177. zcurrentcacheparams(register os_ptr op)
  178. {    uint params[2];
  179.     params[0] = gs_currentcachelower(ifont_dir);
  180.     params[1] = gs_currentcacheupper(ifont_dir);
  181.     push(3);
  182.     make_tv(op - 2, t_mark, intval, 0);
  183.     make_uint_array(op - 1, params, 2);
  184.     return 0;
  185. }
  186.  
  187. /* ------ Initialization procedure ------ */
  188.  
  189. op_def zfont_op_defs[] = {
  190.     {"0currentfont", zcurrentfont},
  191.     {"2makefont", zmakefont},
  192.     {"2scalefont", zscalefont},
  193.     {"1setfont", zsetfont},
  194.     {"0cachestatus", zcachestatus},
  195.     {"1setcachelimit", zsetcachelimit},
  196.     {"1setcacheparams", zsetcacheparams},
  197.     {"0currentcacheparams", zcurrentcacheparams},
  198.     op_def_end(zfont_init)
  199. };
  200.  
  201. /* ------ Subroutines ------ */
  202.  
  203. /* Validate a font parameter. */
  204. private int
  205. font_param(os_ptr fp, gs_font **pfont)
  206. {    /* Check that fp is a read-only dictionary, */
  207.     /* and that it has a FID entry. */
  208.     ref *pid;
  209.     int code;
  210.     check_type(*fp, t_dictionary);
  211.     if ( (code = dict_find(fp, &name_FID, &pid)) < 0 ) return code;
  212.     *pfont = pid->value.pfont;
  213.     if ( *pfont == 0 ) return e_invalidfont; /* unregistered font */
  214.     return 0;
  215. }
  216.  
  217. /* Add the FID entry to a font dictionary. */
  218. int
  219. add_FID(ref *fp /* t_dictionary */,  gs_font *pfont)
  220. {    ref fid;
  221.     make_tv_new(&fid, t_fontID, pfont, pfont);
  222.     return dict_put(fp, &name_FID, &fid);
  223. }
  224.  
  225. /* Make a transformed font (common code for makefont/scalefont). */
  226. private int
  227. make_font(os_ptr op, const gs_matrix *pmat)
  228. {    os_ptr fp = op - 1;
  229.     gs_font *oldfont, *newfont, *ffont;
  230.     ref newdict;
  231.     ref newmat;
  232.     ref *mbody;
  233.     int code;
  234.     if ( (code = font_param(fp, &oldfont)) < 0 ||
  235.          (code = gs_makefont(ifont_dir, oldfont, pmat,
  236.                  &newfont, &ffont)) < 0 ||
  237.          (code = dict_create(dict_maxlength(fp), &newdict)) < 0 ||
  238.          (code = dict_copy(fp, &newdict)) < 0 ||
  239.          (code = ((mbody = alloc_refs(6, "make_font")) == 0 ? e_VMerror : 0)) < 0 ||
  240.          (make_tasv_new(&newmat, t_array, a_all, 6, refs, mbody),
  241.           (code = dict_put(&newdict, &name_FontMatrix, &newmat))) < 0 ||
  242.          (code = add_FID(&newdict, newfont)) < 0
  243.        )
  244.       return code;
  245.     *(gs_matrix *)mbody = newfont->FontMatrix;
  246.     if ( ffont )
  247.       { /****** SHOULD DECREMENT REFCT ******/
  248.       }
  249.     r_clear_attrs(dict_access_ref(&newdict), a_write);
  250.     *fp = newdict;
  251.     pop(1);
  252.     return 0;
  253. }
  254.  
  255. /* Convert an array of (unsigned) integers to stack form. */
  256. private void
  257. make_uint_array(register os_ptr op, const uint *intp, int count)
  258. {    int i;
  259.     for ( i = 0; i < count; i++, op++, intp++ )
  260.         make_int(op, *intp);
  261. }
  262.  
  263. /* Remove scaled font and character cache entries that would be */
  264. /* invalidated by a restore. */
  265. void
  266. font_restore(const alloc_save *save)
  267. {    gs_font_dir *pdir = ifont_dir;
  268.     gs_font *pfont;
  269. top:    for ( pfont = pdir->scaled_fonts; pfont != 0; pfont = pfont->next )
  270.       { if ( alloc_is_since_save((char *)pfont, save) )
  271.           { gs_purge_font_from_caches(pdir, pfont); goto top; }
  272.         else if ( alloc_is_since_save((char *)pfont->base, save) )
  273.           { gs_purge_font_from_caches(pdir, pfont->base); goto top; }
  274.       }
  275. }
  276.