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

  1. /* Copyright (C) 1992, 1993, 1994 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. /* idparam.c */
  20. /* Utilities for getting parameters out of dictionaries. */
  21. #include "memory_.h"
  22. #include "string_.h"        /* for strlen */
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "gsmatrix.h"        /* for dict_matrix_param */
  26. #include "gsuid.h"
  27. #include "idict.h"
  28. #include "idparam.h"        /* interface definition */
  29. #include "ilevel.h"
  30. #include "imemory.h"        /* for iutil.h */
  31. #include "iname.h"
  32. #include "iutil.h"
  33. #include "oper.h"        /* for check_proc */
  34. #include "store.h"        /* for making empty proc */
  35.  
  36. /* Get a Boolean parameter from a dictionary. */
  37. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  38. int
  39. dict_bool_param(const ref *pdict, const char _ds *kstr,
  40.   bool defaultval, bool *pvalue)
  41. {    ref *pdval;
  42.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  43.     {    *pvalue = defaultval;
  44.         return 1;
  45.     }
  46.     if ( !r_has_type(pdval, t_boolean) )
  47.         return_error(e_typecheck);
  48.     *pvalue = pdval->value.boolval;
  49.     return 0;
  50. }        
  51.  
  52. /* Get an integer parameter from a dictionary. */
  53. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  54. /* Note that the default value may be out of range, in which case */
  55. /* a missing value will return e_rangecheck rather than 1. */
  56. int
  57. dict_int_param(const ref *pdict, const char _ds *kstr, int minval, int maxval,
  58.   int defaultval, int *pvalue)
  59. {    ref *pdval;
  60.     int code;
  61.     long ival;
  62.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  63.     {    ival = defaultval;
  64.         code = 1;
  65.     }
  66.     else
  67.     {    switch ( r_type(pdval) )
  68.         {
  69.         case t_integer:
  70.             ival = pdval->value.intval;
  71.             break;
  72.         case t_real:
  73.             /* Allow an integral real, because Fontographer */
  74.             /* (which violates the Adobe specs in other ways */
  75.             /* as well) sometimes generates output that */
  76.             /* needs this. */
  77.             if ( pdval->value.realval < minval || pdval->value.realval > maxval )
  78.                 return_error(e_rangecheck);
  79.             ival = (long)pdval->value.realval;
  80.             if ( ival != pdval->value.realval )
  81.                 return_error(e_rangecheck);
  82.             break;
  83.         default:
  84.             return_error(e_typecheck);
  85.         }
  86.         code = 0;
  87.     }
  88.     if ( ival < minval || ival > maxval )
  89.         return_error(e_rangecheck);
  90.     *pvalue = (int)ival;
  91.     return code;
  92. }        
  93.  
  94. /* Get an unsigned integer parameter from a dictionary. */
  95. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  96. /* Note that the default value may be out of range, in which case */
  97. /* a missing value will return e_rangecheck rather than 1. */
  98. int
  99. dict_uint_param(const ref *pdict, const char _ds *kstr,
  100.   uint minval, uint maxval, uint defaultval, uint *pvalue)
  101. {    ref *pdval;
  102.     int code;
  103.     uint ival;
  104.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  105.     {    ival = defaultval;
  106.         code = 1;
  107.     }
  108.     else
  109.     {    check_type(*pdval, t_integer);
  110.         if ( pdval->value.intval != (uint)pdval->value.intval )
  111.             return_error(e_rangecheck);
  112.         ival = (uint)pdval->value.intval;
  113.         code = 0;
  114.     }
  115.     if ( ival < minval || ival > maxval )
  116.         return_error(e_rangecheck);
  117.     *pvalue = ival;
  118.     return code;
  119. }        
  120.  
  121. /* Get a float parameter from a dictionary. */
  122. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  123. int
  124. dict_float_param(const ref *pdict, const char _ds *kstr,
  125.   floatp defaultval, float *pvalue)
  126. {    ref *pdval;
  127.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  128.     {    *pvalue = defaultval;
  129.         return 1;
  130.     }
  131.     switch ( r_type(pdval) )
  132.     {
  133.     case t_integer: *pvalue = pdval->value.intval; return 0;
  134.     case t_real: *pvalue = pdval->value.realval; return 0;
  135.     }
  136.     return_error(e_typecheck);
  137. }        
  138.  
  139. /* Get an integer array from a dictionary. */
  140. /* Return the element count if OK, 0 if missing, <0 if invalid. */
  141. int
  142. dict_int_array_param(const ref *pdict, const char _ds *kstr,
  143.   uint maxlen, int *ivec)
  144. {    ref *pdval;
  145.     const ref *pa;
  146.     int *pi = ivec;
  147.     uint size;
  148.     int i;
  149.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  150.         return 0;
  151.     if ( !r_has_type(pdval, t_array) )
  152.         return_error(e_typecheck);
  153.     size = r_size(pdval);
  154.     if ( size > maxlen )
  155.         return_error(e_limitcheck);
  156.     pa = pdval->value.const_refs;
  157.     for ( i = 0; i < size; i++, pa++, pi++ )
  158.        {    /* See dict_int_param above for why we allow reals here. */
  159.         switch ( r_type(pa) )
  160.         {
  161.         case t_integer:
  162.             if ( pa->value.intval != (int)pa->value.intval )
  163.                 return_error(e_rangecheck);
  164.             *pi = (int)pa->value.intval;
  165.             break;
  166.         case t_real:
  167.             if ( pa->value.realval < min_int ||
  168.                  pa->value.realval > max_int ||
  169.                  pa->value.realval != (int)pa->value.realval
  170.                )
  171.                 return_error(e_rangecheck);
  172.             *pi = (int)pa->value.realval;
  173.             break;
  174.         default:
  175.             return_error(e_typecheck);
  176.         }
  177.        }
  178.     return size;
  179. }        
  180.  
  181. /* Get a float array from a dictionary. */
  182. /* Return the element count if OK, <0 if invalid. */
  183. /* If the parameter is missing, then if defaultvec is NULL, return 0; */
  184. /* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
  185. /* and return maxlen. */
  186. int
  187. dict_float_array_param(const ref *pdict, const char _ds *kstr,
  188.   uint maxlen, float *fvec, float *defaultvec)
  189. {    ref *pdval;
  190.     uint size;
  191.     int code;
  192.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  193.     {    if ( defaultvec == NULL ) return 0;
  194.         memcpy(fvec, defaultvec, maxlen * sizeof(float));
  195.         return maxlen;
  196.     }
  197.     if ( !r_has_type(pdval, t_array) )
  198.         return_error(e_typecheck);
  199.     size = r_size(pdval);
  200.     if ( size > maxlen )
  201.         return_error(e_limitcheck);
  202.     code = num_params(pdval->value.refs + size - 1, size, fvec);
  203.     return (code >= 0 ? size : code);
  204. }        
  205.  
  206. /* Get a procedure from a dictionary. */
  207. /* If the key is missing, set the procedure to an empty one and return 1. */
  208. int
  209. dict_proc_param(const ref *pdict, const char _ds *kstr, ref *pproc)
  210. {    ref *pdval;
  211.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  212.     {    make_array(pproc, a_readonly + a_executable, 0, NULL);
  213.         return 1;
  214.     }
  215.     check_proc(*pdval);
  216.     *pproc = *pdval;
  217.     return 0;
  218. }
  219.  
  220. /* Get a matrix from a dictionary. */
  221. int
  222. dict_matrix_param(const ref *pdict, const char _ds *kstr, gs_matrix *pmat)
  223. {    ref *pdval;
  224.     if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
  225.         return_error(e_typecheck);
  226.     return read_matrix(pdval, pmat);
  227. }
  228.  
  229. /* Get a UniqueID or XUID from a dictionary. */
  230. /* Return 0 if UniqueID, 1 if XUID, <0 if error. */
  231. /* If there is no uid, return default. */
  232. int
  233. dict_uid_param(const ref *pdict, gs_uid *puid, int defaultval,
  234.   gs_memory_t *mem)
  235. {    ref *puniqueid;
  236.     if ( pdict == 0 )
  237.     {    uid_set_invalid(puid);
  238.         return defaultval;
  239.     }
  240.     /* In a Level 2 environment, check for XUID first. */
  241.     if ( level2_enabled &&
  242.          dict_find_string(pdict, "XUID", &puniqueid) > 0
  243.        )
  244.     {    long *xvalues;
  245.         uint size, i;
  246.         if ( !r_has_type(puniqueid, t_array) )
  247.             return_error(e_typecheck);
  248.         size = r_size(puniqueid);
  249.         if ( size == 0 )
  250.             return_error(e_rangecheck);
  251.         xvalues = (long *)gs_alloc_byte_array(mem, size, sizeof(long),
  252.                               "get XUID");
  253.         if ( xvalues == 0 )
  254.             return_error(e_VMerror);
  255.         /* Get the values from the XUID array. */
  256.         for ( i = 0; i < size; i++ )
  257.         {    const ref *pvalue = puniqueid->value.const_refs + i;
  258.             if ( !r_has_type(pvalue, t_integer) )
  259.             {    gs_free_object(mem, xvalues, "get XUID");
  260.                 return_error(e_typecheck);
  261.             }
  262.             xvalues[i] = pvalue->value.intval;
  263.         }
  264.         uid_set_XUID(puid, xvalues, size);
  265.         return 1;
  266.     }
  267.     /* If no UniqueID entry, set the UID to invalid, */
  268.     /* because UniqueID need not be present in all fonts, */
  269.     /* and if it is, the legal range is 0 to 2^24-1. */
  270.     if ( dict_find_string(pdict, "UniqueID", &puniqueid) <= 0 )
  271.     {    uid_set_invalid(puid);
  272.         return defaultval;
  273.     }
  274.     else
  275.        {    if ( !r_has_type(puniqueid, t_integer) ||
  276.              puniqueid->value.intval < 0 ||
  277.              puniqueid->value.intval > 0xffffffL
  278.            )
  279.             return_error(e_rangecheck);
  280.         /* Apparently fonts created by Fontographer often have */
  281.         /* a UniqueID of 0, contrary to Adobe's specifications. */
  282.         /* Treat 0 as equivalent to -1 (no UniqueID). */
  283.         if ( puniqueid->value.intval == 0 )
  284.         {    uid_set_invalid(puid);
  285.             return defaultval;
  286.         }
  287.         else
  288.             uid_set_UniqueID(puid, puniqueid->value.intval);
  289.        }
  290.     return 0;
  291. }
  292.  
  293. /* Check that a UID in a dictionary is equal to an existing, valid UID. */
  294. bool
  295. dict_check_uid_param(const ref *pdict, const gs_uid *puid)
  296. {    ref *puniqueid;
  297.     if ( uid_is_XUID(puid) )
  298.       {    uint size = uid_XUID_size(puid);
  299.         uint i;
  300.         if ( dict_find_string(pdict, "XUID", &puniqueid) <= 0 )
  301.           return false;
  302.         if ( !r_has_type(puniqueid, t_array) ||
  303.              r_size(puniqueid) != size
  304.            )
  305.           return false;
  306.         for ( i = 0; i < size; i++ )
  307.           {    const ref *pvalue = puniqueid->value.const_refs + i;
  308.             if ( !r_has_type(pvalue, t_integer) )
  309.               return false;
  310.             if ( pvalue->value.intval != uid_XUID_values(puid)[i] )
  311.               return false;
  312.           }
  313.         return true;
  314.       }
  315.     else
  316.       {    if ( dict_find_string(pdict, "UniqueID", &puniqueid) <= 0 )
  317.           return false;
  318.         return (r_has_type(puniqueid, t_integer) &&
  319.             puniqueid->value.intval == puid->id);
  320.       }
  321. }
  322.  
  323.