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 / ICCFONT.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  8KB  |  275 lines

  1. /* Copyright (C) 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. /* iccfont.c */
  21. /* Initialization support for compiled fonts */
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "ccfont.h"
  26. #include "dict.h"
  27. #include "dstack.h"
  28. #include "errors.h"
  29. #include "iutil.h"
  30. #include "name.h"
  31. #include "oper.h"
  32. #include "save.h"            /* for alloc_refs */
  33. #include "store.h"
  34.  
  35. extern ref name_StandardEncoding;
  36.  
  37. /* Forward references */
  38. private int huge
  39.     cfont_ref_dict_create(P4(ref *, const cfont_dict_keys *,
  40.                  cfont_string_array, const ref *)),
  41.     cfont_string_dict_create(P4(ref *, const cfont_dict_keys *,
  42.                     cfont_string_array, cfont_string_array)),
  43.     cfont_num_dict_create(P4(ref *, const cfont_dict_keys *,
  44.                  cfont_string_array, const float *)),
  45.     cfont_name_array_create(P3(ref *, cfont_string_array, int)),
  46.     cfont_string_array_create(P4(ref *, cfont_string_array, int, uint)),
  47.     cfont_name_create(P2(ref *, const char *));
  48.  
  49. /* Procedure vector passed to font initialization procedures. */
  50. private const cfont_procs ccfont_procs = {
  51.     cfont_ref_dict_create,
  52.     cfont_string_dict_create,
  53.     cfont_num_dict_create,
  54.     cfont_name_array_create,
  55.     cfont_string_array_create,
  56.     cfont_name_create
  57. };
  58.  
  59. /* ------ Initialization tables ------ */
  60.  
  61. /*
  62.  * The file gconfigf.h contains a line
  63.  *    font_("0.font_xxx", gsf_xxx, zf_xxx)
  64.  * for each compiled font.
  65.  */
  66.  
  67. /* Generate an operator procedure for each font. */
  68. #ifdef __PROTOTYPES__
  69. #  define zfproto(proc) proc(os_ptr op)
  70. #else
  71. #  define zfproto(proc) proc(op) os_ptr op;
  72. #endif
  73. #define font_(fname, fproc, zfproc)\
  74. extern int huge fproc(P2(const cfont_procs *, ref *));\
  75. private int zfproto(zfproc)\
  76. {    int code = fproc(&ccfont_procs, (ref *)(op + 1));\
  77.     if ( code < 0 ) return code;\
  78.     push(1);\
  79.     return 0;\
  80. }
  81. #include "gconfigf.h"
  82.  
  83. /* Generate the operator initialization table. */
  84. #undef font_
  85. #define font_(fname, fproc, zfproc)\
  86.     {fname, zfproc},
  87. op_def ccfonts_op_defs[] = {
  88. #include "gconfigf.h"
  89.     op_def_end(0)
  90. };
  91.  
  92. /* ------ Procedural code ------ */
  93.  
  94. typedef struct {
  95.     const char *str_array;
  96.     const byte *str;
  97.     uint len;
  98. } str_enum;
  99. #define init_str_enum(sep, ksa)\
  100.   (sep)->str_array = ksa
  101. typedef struct {
  102.     cfont_dict_keys keys;
  103.     str_enum strings;
  104. } key_enum;
  105. #define init_key_enum(kep, kp, ksa)\
  106.   (kep)->keys = *kp, init_str_enum(&(kep)->strings, ksa)
  107.  
  108. /* Check for reaching the end of the keys. */
  109. #define more_keys(kep) ((kep)->keys.num_enc_keys | (kep)->keys.num_str_keys)
  110.  
  111. /* Get the next string from a string array. */
  112. /* Return true if it was a string, false if it was a null. */
  113. private int near
  114. cfont_next_string(str_enum _ss *pse)
  115. {    const char *str = pse->str_array;
  116.     uint len = (((const byte *)str)[0] << 8) + ((const byte *)str)[1];
  117.     int was_string = 1;
  118.     if ( len == 0xffff )
  119.     {    len = 0;
  120.         was_string = 0;
  121.     }
  122.     pse->str = (const byte *)str + 2;
  123.     pse->len = len;
  124.     pse->str_array = str + 2 + len;
  125.     return was_string;
  126. }
  127.  
  128. /* Put the next entry into a dictionary. */
  129. /* We know that more_keys(kp) is true. */
  130. private int near
  131. cfont_put_next(ref *pdict, key_enum _ss *kep, const ref *pvalue,
  132.   ref * _ss *pencodings)
  133. {    ref kname;
  134.     int code;
  135. #define kp (&kep->keys)
  136.     if ( *pencodings == 0 )
  137.     {    /* Create the dictionary and look up the encodings. */
  138.         code = dict_create(kp->num_enc_keys + kp->num_str_keys + kp->extra_slots, pdict);
  139.         if ( code < 0 ) return code;
  140.         make_tasv(&kname, t_string, 0, 17, bytes,
  141.               (byte *)"ISOLatin1Encoding");
  142.         if ( dict_find(&systemdict, &name_StandardEncoding, &pencodings[0]) <= 0 ||
  143.              dict_find(&systemdict, &kname, &pencodings[1]) <= 0
  144.            )
  145.             return e_undefined;
  146.     }
  147.     if ( kp->num_enc_keys )
  148.     {    const charindex *skp = kp->enc_keys++;
  149.         code = array_get(pencodings[skp->encx], (long)(skp->charx), &kname);
  150.         kp->num_enc_keys--;
  151.     }
  152.     else        /* must have kp->num_str_keys != 0 */
  153.     {    cfont_next_string(&kep->strings);
  154.         code = name_ref(kep->strings.str, kep->strings.len, &kname, 0);
  155.         kp->num_str_keys--;
  156.     }
  157.     return dict_put(pdict, &kname, pvalue);
  158. #undef kp
  159. }
  160.  
  161. /* ------ Routines called from compiled font initialization ------ */
  162.  
  163. /* Create a dictionary with general ref values. */
  164. private int huge
  165. cfont_ref_dict_create(ref *pdict, const cfont_dict_keys *kp,
  166.   cfont_string_array ksa, const ref *values)
  167. {    key_enum kenum;
  168.     const ref *vp = values;
  169.     ref *pencodings[2];
  170.     init_key_enum(&kenum, kp, ksa);
  171.     pencodings[0] = 0;
  172.     while ( more_keys(&kenum) )
  173.     {    const ref *pvalue = vp++;
  174.         int code = cfont_put_next(pdict, &kenum, pvalue, pencodings);
  175.         if ( code < 0 ) return code;
  176.     }
  177.     return 0;
  178. }
  179.  
  180. /* Create a dictionary with string/null values. */
  181. private int huge
  182. cfont_string_dict_create(ref *pdict, const cfont_dict_keys *kp,
  183.   cfont_string_array ksa, cfont_string_array kva)
  184. {    key_enum kenum;
  185.     str_enum senum;
  186.     uint attrs = kp->value_attrs;
  187.     ref *pencodings[2];
  188.     init_key_enum(&kenum, kp, ksa);
  189.     init_str_enum(&senum, kva);
  190.     pencodings[0] = 0;
  191.     while ( more_keys(&kenum) )
  192.     {    ref vstring;
  193.         int code;
  194.         if ( cfont_next_string(&senum) )
  195.         {    make_tasv(&vstring, t_string, attrs, senum.len,
  196.                   const_bytes, (const byte *)senum.str);
  197.         }
  198.         else
  199.             make_null(&vstring);
  200.         code = cfont_put_next(pdict, &kenum, &vstring, pencodings);
  201.         if ( code < 0 ) return code;
  202.     }
  203.     return 0;
  204. }
  205.  
  206. /* Create a dictionary with number values. */
  207. private int huge
  208. cfont_num_dict_create(ref *pdict, const cfont_dict_keys *kp,
  209.   cfont_string_array ksa, const float *values)
  210. {    key_enum kenum;
  211.     const float *vp = values;
  212.     ref *pencodings[2];
  213.     ref vnum;
  214.     init_key_enum(&kenum, kp, ksa);
  215.     pencodings[0] = 0;
  216.     while ( more_keys(&kenum) )
  217.     {    float val = *vp++;
  218.         int code;
  219.         if ( val == (int)val )
  220.             make_int(&vnum, (int)val);
  221.         else
  222.             make_real(&vnum, val);
  223.         code = cfont_put_next(pdict, &kenum, &vnum, pencodings);
  224.         if ( code < 0 ) return code;
  225.     }
  226.     return 0;
  227. }
  228.  
  229. /* Create an array with name values. */
  230. private int huge
  231. cfont_name_array_create(ref *parray, cfont_string_array ksa, int size)
  232. {    ref *aptr = alloc_refs(size, "cfont_name_array_create");
  233.     int i;
  234.     str_enum senum;
  235.     if ( aptr == 0 ) return e_VMerror;
  236.     init_str_enum(&senum, ksa);
  237.     make_tasv(parray, t_array, a_readonly, size, refs, aptr);
  238.     for ( i = 0; i < size; i++, aptr++ )
  239.     {    ref nref;
  240.         int code;
  241.         cfont_next_string(&senum);
  242.         code = name_ref((const byte *)senum.str, senum.len, &nref, 0);
  243.         if ( code < 0 ) return code;
  244.         ref_assign_new(aptr, &nref);
  245.     }
  246.     return 0;
  247. }
  248.  
  249. /* Create an array with string/null values. */
  250. private int huge
  251. cfont_string_array_create(ref *parray, cfont_string_array ksa,
  252.   int size, uint attrs)
  253. {    ref *aptr = alloc_refs(size, "cfont_string_array_create");
  254.     int i;
  255.     str_enum senum;
  256.     if ( aptr == 0 ) return e_VMerror;
  257.     init_str_enum(&senum, ksa);
  258.     make_tasv(parray, t_array, a_readonly, size, refs, aptr);
  259.     for ( i = 0; i < size; i++, aptr++ )
  260.     {    if ( cfont_next_string(&senum) )
  261.         {    make_tasv_new(aptr, t_string, attrs, senum.len,
  262.                       const_bytes, (const byte *)senum.str);
  263.         }
  264.         else
  265.             make_null_new(aptr);
  266.     }
  267.     return 0;
  268. }
  269.  
  270. /* Create a name. */
  271. private int huge
  272. cfont_name_create(ref *pnref, const char *str)
  273. {    return name_ref((const byte *)str, strlen(str), pnref, 0);
  274. }
  275.