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

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