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

  1. /* Copyright (C) 1991, 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. /* zfont0.c */
  20. /* Composite font creation operator */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsstruct.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 "gxfont0.h"
  39. #include "bfont.h"
  40. #include "ialloc.h"
  41. #include "idict.h"
  42. #include "igstate.h"
  43. #include "store.h"
  44.  
  45. /* Composite font procedures */
  46. extern font_proc_init_fstack(gs_type0_init_fstack);
  47. extern font_proc_next_char(gs_type0_next_char);
  48. extern font_proc_define_font(gs_type0_define_font);
  49. extern font_proc_make_font(gs_type0_make_font);
  50.  
  51. /* Forward references */
  52. private font_proc_define_font(ztype0_define_font);
  53. private font_proc_make_font(ztype0_make_font);
  54. private int ensure_char_entry(P4(os_ptr, const char _ds *, byte *, int));
  55.  
  56. /* <string|name> <font_dict> .buildfont0 <string|name> <font> */
  57. /* Build a type 0 (composite) font. */
  58. int
  59. zbuildfont0(os_ptr op)
  60. {    ref *pfmaptype;
  61.     gs_type0_data data;
  62.     ref *pfdepvector;
  63.     ref *pprefenc;
  64.     ref *psubsvector;
  65.     gs_font_type0 *pfont;
  66.     font_data *pdata;
  67.     int i;
  68.     int code;
  69.     check_type(*op, t_dictionary);
  70.     if ( dict_find_string(op, "FMapType", &pfmaptype) <= 0 ||
  71.          !r_has_type(pfmaptype, t_integer) ||
  72.          pfmaptype->value.intval < (int)fmap_type_min ||
  73.          pfmaptype->value.intval > (int)fmap_type_max ||
  74.          dict_find_string(op, "FDepVector", &pfdepvector) <= 0 ||
  75.          !r_is_array(pfdepvector)
  76.        )
  77.         return_error(e_invalidfont);
  78.     data.FMapType = (fmap_type)pfmaptype->value.intval;
  79.     /* Check that every element of the FDepVector is a font. */
  80.     data.fdep_size = r_size(pfdepvector);
  81.     for ( i = 0; i < data.fdep_size; i++ )
  82.     {    ref fdep;
  83.         ref *pfid;
  84.         gs_font *psub;
  85.         array_get(pfdepvector, i, &fdep);
  86.         if ( !r_has_type(&fdep, t_dictionary) ||
  87.              dict_find_string(&fdep, "FID", &pfid) <= 0 ||
  88.              !r_has_type(pfid, t_fontID)
  89.            )
  90.             return_error(e_invalidfont);
  91.         /*
  92.          * Check the inheritance rules.  Allowed configurations
  93.          * (paths from root font) are defined by the regular
  94.          * expression:
  95.          *    (shift | double_escape escape* | escape*)
  96.          *      non_modal* non_composite
  97.          */
  98.         psub = r_ptr(pfid, gs_font);
  99.         if ( psub->FontType == ft_composite )
  100.         {
  101. #define psub0 ((gs_font_type0 *)psub)
  102.             fmap_type fmt = psub0->data.FMapType;
  103.             if ( fmt == fmap_double_escape ||
  104.                  fmt == fmap_shift ||
  105.                  (fmt == fmap_escape &&
  106.                   !(data.FMapType == fmap_escape ||
  107.                 data.FMapType == fmap_double_escape))
  108.                )
  109.                 return_error(e_invalidfont);
  110. #undef psub0
  111.         }
  112.     }
  113.     switch ( data.FMapType )
  114.     {
  115.     case fmap_escape: case fmap_double_escape:    /* need EscChar */
  116.         code = ensure_char_entry(op, "EscChar", &data.EscChar, 255);
  117.         break;
  118.     case fmap_shift:            /* need ShiftIn & ShiftOut */
  119.         code = ensure_char_entry(op, "ShiftIn", &data.ShiftIn, 15);
  120.         if ( code >= 0 )
  121.           code = ensure_char_entry(op, "ShiftOut", &data.ShiftOut, 14);
  122.         break;
  123.     case fmap_SubsVector:            /* need SubsVector */
  124.     {    uint svsize;
  125.         if ( dict_find_string(op, "SubsVector", &psubsvector) <= 0 ||
  126.              !r_has_type(psubsvector, t_string) ||
  127.              (svsize = r_size(psubsvector)) == 0 ||
  128.              (data.subs_width = (int)*psubsvector->value.bytes + 1) > 4 ||
  129.              (svsize - 1) % data.subs_width != 0
  130.            )
  131.             return_error(e_invalidfont);
  132.         data.subs_size = (svsize - 1) / data.subs_width;
  133.         data.SubsVector.data = psubsvector->value.bytes + 1;
  134.         data.SubsVector.size = svsize - 1;
  135.     }
  136.     default:
  137.         code = 0;
  138.     }
  139.     if ( code < 0 ) return code;
  140.     {    build_proc_refs build;
  141.         code = build_proc_name_refs(&build,
  142.             "Type0BuildChar", "Type0BuildGlyph");
  143.         if ( code < 0 ) return code;
  144.         code = build_gs_font(op, (gs_font **)&pfont,
  145.                      ft_composite, &st_gs_font_type0, &build);
  146.     }
  147.     if ( code != 0 ) return code;
  148.     pfont->procs.init_fstack = gs_type0_init_fstack;
  149.     pfont->procs.next_char = gs_type0_next_char;
  150.     pfont->procs.define_font = ztype0_define_font;
  151.     pfont->procs.make_font = ztype0_make_font;
  152.     if ( dict_find_string(op, "PrefEnc", &pprefenc) <= 0 )
  153.     {    ref nul;
  154.         make_null_new(&nul);
  155.         if ( (code = dict_put_string(op, "PrefEnc", &nul)) < 0 )
  156.             return code;
  157.     }
  158.     /* Fill in the font data */
  159.     pdata = pfont_data(pfont);
  160.     data.encoding_size = r_size(&pdata->Encoding);
  161.     data.Encoding =
  162.       (uint *)ialloc_byte_array(data.encoding_size, sizeof(uint),
  163.                     "buildfont0(Encoding)");
  164.     if ( data.Encoding == 0 )
  165.         return_error(e_VMerror);
  166.     /* Fill in the encoding vector, checking to make sure that */
  167.     /* each element is an integer between 0 and fdep_size-1. */
  168.     for ( i = 0; i < data.encoding_size; i++ )
  169.     {    ref enc;
  170.         array_get(&pdata->Encoding, i, &enc);
  171.         check_int_leu(enc, data.fdep_size);
  172.         data.Encoding[i] = (uint)enc.value.intval;
  173.     }
  174.     data.FDepVector =
  175.       ialloc_struct_array(data.fdep_size, gs_font *,
  176.                   &st_gs_font_ptr_element,
  177.                   "buildfont0(FDepVector)");
  178.     if ( data.FDepVector == 0 )
  179.         return_error(e_VMerror);
  180.     for ( i = 0; i < data.fdep_size; i++ )
  181.     {    ref fdep;
  182.         ref *pfid;
  183.         array_get(pfdepvector, i, &fdep);
  184.         /* The lookup can't fail, because of the pre-check above. */
  185.         dict_find_string(&fdep, "FID", &pfid);
  186.         data.FDepVector[i] = r_ptr(pfid, gs_font);
  187.     }
  188.     pfont->data = data;
  189.     return define_gs_font((gs_font *)pfont);
  190. }
  191. /* If a newly defined or scaled composite font had to scale */
  192. /* any composite sub-fonts, adjust the parent font's FDepVector. */
  193. /* This is called only if gs_type0_define/make_font */
  194. /* actually changed the FDepVector. */
  195. private int
  196. ztype0_adjust_FDepVector(gs_font_type0 *pfont)
  197. {    gs_font **pdep = pfont->data.FDepVector;
  198.     ref newdep;
  199.     uint fdep_size = pfont->data.fdep_size;
  200.     ref *prdep;
  201.     uint i;
  202.     int code = ialloc_ref_array(&newdep, a_readonly, fdep_size,
  203.                     "ztype0_adjust_matrix");
  204.     if ( code < 0 )
  205.         return code;
  206.     for ( prdep = newdep.value.refs, i = 0; i < fdep_size; i++, prdep++ )
  207.     {    const ref *pdict = pfont_dict(pdep[i]);
  208.         ref_assign_new(prdep, pdict);
  209.     }
  210.     return dict_put_string(pfont_dict(pfont), "FDepVector", &newdep);
  211. }
  212. private int
  213. ztype0_define_font(gs_font_dir *pdir, gs_font *pfont)
  214. {
  215. #define pfont0 ((gs_font_type0 *)pfont)
  216.     gs_font **pdep = pfont0->data.FDepVector;
  217.     int code = gs_type0_define_font(pdir, pfont);
  218.     if ( code < 0 || pfont0->data.FDepVector == pdep )
  219.         return code;
  220.     return ztype0_adjust_FDepVector(pfont0);
  221. #undef pfont0
  222. }
  223. private int
  224. ztype0_make_font(gs_font_dir *pdir, const gs_font *pfont,
  225.   const gs_matrix *pmat, gs_font **ppfont)
  226. {
  227. #define ppfont0 ((gs_font_type0 **)ppfont)
  228.     gs_font **pdep = (*ppfont0)->data.FDepVector;
  229.     int code;
  230.     code = zdefault_make_font(pdir, pfont, pmat, ppfont);
  231.     if ( code < 0 )
  232.         return code;
  233.     code = gs_type0_make_font(pdir, pfont, pmat, ppfont);
  234.     if ( code < 0 )
  235.         return code;
  236.     if ( (*ppfont0)->data.FDepVector == pdep )
  237.         return 0;
  238.     return ztype0_adjust_FDepVector(*ppfont0);
  239. #undef ppfont0
  240. }
  241.  
  242. /* ------ Internal routines ------ */
  243.  
  244. /* Find or add a character entry in a font dictionary. */
  245. private int
  246. ensure_char_entry(os_ptr op, const char _ds *kstr,
  247.   byte *pvalue, int default_value)
  248. {    ref *pentry;
  249.     if ( dict_find_string(op, kstr, &pentry) <= 0 )
  250.       {    ref ent;
  251.         make_int(&ent, default_value);
  252.         *pvalue = (byte)default_value;
  253.         return dict_put_string(op, kstr, &ent);
  254.       }
  255.     else
  256.       {    check_int_leu(*pentry, 255);
  257.         *pvalue = (byte)pentry->value.intval;
  258.         return 0;
  259.       }
  260. }
  261.  
  262. /* ------ Initialization procedure ------ */
  263.  
  264. op_def zfont0_op_defs[] = {
  265.     {"2.buildfont0", zbuildfont0},
  266.     op_def_end(0)
  267. };
  268.