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 / ZFONT0.C < prev    next >
C/C++ Source or Header  |  1992-05-23  |  7KB  |  215 lines

  1. /* Copyright (C) 1991, 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. /* zfont0.c */
  21. /* Composite font creation operator 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 "alloc.h"
  39. #include "bfont.h"
  40. #include "dict.h"
  41. #include "name.h"
  42. #include "state.h"
  43. #include "store.h"
  44.  
  45. /* Forward references */
  46. private int ensure_char_entry(P4(os_ptr, const ref *, byte *, int));
  47.  
  48. /* Names of system-known keys in type 0 font dictionaries: */
  49. static ref name_EscChar;
  50. static ref name_FDepVector;
  51. static ref name_FMapType;
  52. static ref name_PrefEnc;
  53. static ref name_ShiftIn;
  54. static ref name_ShiftOut;
  55. static ref name_SubsVector;
  56. static ref name_Type0BuildChar;
  57.  
  58. /* Initialize the composite font operators */
  59. private void
  60. zfont0_init()
  61. {    static const names_def fnd0[] = {
  62.        { "EscChar", &name_EscChar },
  63.        { "FDepVector", &name_FDepVector },
  64.        { "FMapType", &name_FMapType },
  65.        { "PrefEnc", &name_PrefEnc },
  66.        { "ShiftIn", &name_ShiftIn },
  67.        { "ShiftOut", &name_ShiftOut },
  68.        { "SubsVector", &name_SubsVector },
  69.        { "Type0BuildChar", &name_Type0BuildChar },
  70.        names_def_end
  71.     };
  72.     init_names(fnd0);
  73.  
  74.     /* Make the standard BuildChar procedure executable. */
  75.     r_set_attrs(&name_Type0BuildChar, a_executable);
  76. }
  77.  
  78. /* .buildfont0 */
  79. /* Build a type 0 (composite) font. */
  80. int
  81. zbuildfont0(os_ptr op)
  82. {    ref *pfmaptype;
  83.     gs_type0_data data;
  84.     ref *pfdepvector;
  85.     ref *pprefenc;
  86.     ref *psubsvector;
  87.     gs_font *pfont;
  88.     font_data *pdata;
  89.     int i;
  90.     int code;
  91.     check_type(*op, t_dictionary);
  92.     if ( dict_find(op, &name_FMapType, &pfmaptype) <= 0 ||
  93.          !r_has_type(pfmaptype, t_integer) ||
  94.          pfmaptype->value.intval < (int)fmap_type_min ||
  95.          pfmaptype->value.intval > (int)fmap_type_max ||
  96.          dict_find(op, &name_FDepVector, &pfdepvector) <= 0 ||
  97.          !r_has_type(pfdepvector, t_array)
  98.        )
  99.         return e_invalidfont;
  100.     data.FMapType = (fmap_type)pfmaptype->value.intval;
  101.     /* Check that every element of the FDepVector is a font. */
  102.     data.fdep_size = r_size(pfdepvector);
  103.     for ( i = 0; i < data.fdep_size; i++ )
  104.        {    ref *pdep = pfdepvector->value.refs + i;
  105.         ref *pfid;
  106.         gs_font *psub;
  107.         if ( !r_has_type(pdep, t_dictionary) ||
  108.              dict_find(pdep, &name_FID, &pfid) <= 0 ||
  109.              !r_has_type(pfid, t_fontID)
  110.            )
  111.             return e_invalidfont;
  112.         /*
  113.          * Check the inheritance rules.  Allowed configurations
  114.          * (paths from root font) are defined by the regular
  115.          * expression:
  116.          *    (shift | double_escape escape* | escape*)
  117.          *      non_modal* non_composite
  118.          */
  119.         psub = pfid->value.pfont;
  120.         if ( psub->FontType == ft_composite )
  121.            {    fmap_type fmt = psub->data.type0_data.FMapType;
  122.             if ( fmt == fmap_double_escape ||
  123.                  fmt == fmap_shift ||
  124.                  fmt == fmap_escape &&
  125.                   !(data.FMapType == fmap_escape ||
  126.                 data.FMapType == fmap_double_escape)
  127.                )
  128.                 return e_invalidfont;
  129.            }
  130.        }
  131.     switch ( data.FMapType )
  132.        {
  133.     case fmap_escape: case fmap_double_escape:    /* need EscChar */
  134.         code = ensure_char_entry(op, &name_EscChar, &data.EscChar, 255);
  135.         break;
  136.     case fmap_shift:            /* need ShiftIn & ShiftOut */
  137.         code = ensure_char_entry(op, &name_ShiftIn, &data.ShiftIn, 15);
  138.         if ( code == 0 )
  139.           code = ensure_char_entry(op, &name_ShiftOut, &data.ShiftOut, 14);
  140.         break;
  141.     case fmap_SubsVector:            /* need SubsVector */
  142.         if ( dict_find(op, &name_SubsVector, &psubsvector) <= 0 ||
  143.              !r_has_type(psubsvector, t_string) ||
  144.              r_size(psubsvector) == 0 ||
  145.              (data.subs_width = (int)*psubsvector->value.bytes + 1) > 4 ||
  146.              (r_size(psubsvector) - 1) % data.subs_width != 0
  147.            )
  148.             return e_invalidfont;
  149.     default:
  150.         code = 0;
  151.        }
  152.     if ( code < 0 ) return code;
  153.     code = build_gs_font(op, &pfont, ft_composite, &name_Type0BuildChar);
  154.     if ( code != 0 ) return code;
  155.     if ( dict_find(op, &name_PrefEnc, &pprefenc) <= 0 )
  156.        {    ref nul;
  157.         make_null_new(&nul);
  158.         if ( (code = dict_put(op, &name_PrefEnc, &nul)) < 0 )
  159.             return code;
  160.        }
  161.     /* Fill in the font data */
  162.     pdata = (font_data *)(pfont->client_data);
  163.     data.subs_size = (r_size(psubsvector) - 1) / data.subs_width;
  164.     data.SubsVector = psubsvector->value.bytes + 1;
  165.     data.encoding_size = r_size(&pdata->Encoding);
  166.     data.Encoding =
  167.       (uint *)alloc(data.encoding_size, sizeof(uint),
  168.                "buildfont0(Encoding)");
  169.     /* Fill in the encoding vector, checking to make sure that */
  170.     /* each element is an integer between 0 and fdep_size-1. */
  171.     for ( i = 0; i < data.encoding_size; i++ )
  172.        {    ref *penc = pdata->Encoding.value.refs + i;
  173.         if ( !r_has_type(penc, t_integer) ||
  174.              (ulong)penc->value.intval >= data.fdep_size
  175.            )
  176.             return e_invalidfont;
  177.         data.Encoding[i] = (uint)penc->value.intval;
  178.        }
  179.     data.FDepVector =
  180.       (gs_font **)alloc(data.fdep_size, sizeof(gs_font *),
  181.                "buildfont0(FDepVector)");
  182.     for ( i = 0; i < data.fdep_size; i++ )
  183.        {    ref *pfid;
  184.         /* The lookup can't fail, because of the pre-check above. */
  185.         dict_find(pfdepvector->value.refs + i, &name_FID, &pfid);
  186.         data.FDepVector[i] = pfid->value.pfont;
  187.        }
  188.     pfont->data.type0_data = data;
  189.     return 0;
  190. }
  191. /* Private routine to find or add an integer entry in a font dictionary. */
  192. private int
  193. ensure_char_entry(os_ptr op, const ref *pname, byte *pvalue, int default_value)
  194. {    ref *pentry;
  195.     if ( dict_find(op, pname, &pentry) <= 0 )
  196.        {    ref ent;
  197.         make_int(&ent, default_value);
  198.         return dict_put(op, pname, &ent);
  199.        }
  200.     else
  201.       if ( !r_has_type(pentry, t_integer) ||
  202.            (ulong)(pentry->value.intval) > 255
  203.          )
  204.         return e_invalidfont;
  205.     *pvalue = (byte)pentry->value.intval;
  206.     return 0;
  207. }
  208.  
  209. /* ------ Initialization procedure ------ */
  210.  
  211. op_def zfont0_op_defs[] = {
  212.     {"1.buildfont0", zbuildfont0},
  213.     op_def_end(zfont0_init)
  214. };
  215.