home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / zfont2 < prev    next >
Encoding:
Text File  |  1991-10-25  |  10.2 KB  |  344 lines

  1. /* Copyright (C) 1989, 1990, 1991 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. /* zfont2.c */
  21. /* Font creation operator for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"
  26. #include "gsmatrix.h"
  27. #include "gxdevice.h"
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "alloc.h"
  31. #include "dict.h"
  32. #include "font.h"
  33. #include "name.h"
  34. #include "packed.h"
  35. #include "store.h"
  36.  
  37. /* Imported procedures */
  38. int add_FID(P2(ref *, gs_font *));    /* in zfont */
  39. int buildfont0(P3(os_ptr, ref **, gs_font **));    /* in zfont0 */
  40.  
  41. /* Forward references */
  42. private int buildfont1(P3(os_ptr, ref **, gs_font **));
  43. private int buildfont3(P3(os_ptr, ref **, gs_font **));
  44. private int build_gs_simple_font(P2(os_ptr, gs_font **));
  45. int build_gs_font(P2(os_ptr, gs_font **));    /* buildfont0 needs this */
  46.  
  47. /* Type 1 auxiliary procedures (defined in zchar.c) */
  48. extern int z1_subr_proc(P3(gs_type1_data *, int, byte **));
  49. extern int z1_pop_proc(P2(gs_type1_data *, fixed *));
  50.  
  51. /* Global font-related objects */
  52. /* Names of system-known keys in font dictionaries: */
  53. extern ref name_FID;            /* in zfont.c */
  54. extern ref name_FontMatrix;        /* in zfont.c */
  55. private ref name_FontType;
  56. private ref name_FontBBox;
  57. private ref name_Encoding;
  58. private ref name_PaintType;
  59. private ref name_UniqueID;
  60. private ref name_BuildChar;
  61. private ref name_Type1BuildChar;
  62. private ref name_Private;
  63. private ref name_CharStrings;        /* only needed for seac */
  64. /* Names of system-known keys in type 1 font Private dictionary: */
  65. private ref name_Subrs;
  66. private ref name_lenIV;
  67.  
  68. /* The global font directory */
  69. extern gs_font_dir *ifont_dir;
  70.  
  71. /* Default value of lenIV */
  72. #define default_lenIV 4
  73.  
  74. /* Initialize the font building operators */
  75. private void
  76. zfont2_init()
  77. {    static names_def fnd2[] = {
  78.  
  79.     /* Create the names of the standard elements of */
  80.     /* a font dictionary. */
  81.        { "FontType", &name_FontType },
  82.        { "FontBBox", &name_FontBBox },
  83.        { "Encoding", &name_Encoding },
  84.        { "PaintType", &name_PaintType },
  85.        { "UniqueID", &name_UniqueID },
  86.        { "BuildChar", &name_BuildChar },
  87.        { "Type1BuildChar", &name_Type1BuildChar },
  88.        { "Private", &name_Private },
  89.        { "CharStrings", &name_CharStrings },
  90.  
  91.     /* Create the names of the known entries in */
  92.     /* a type 1 font Private dictionary. */
  93.        { "Subrs", &name_Subrs },
  94.        { "lenIV", &name_lenIV },
  95.  
  96.     /* Mark the end of the initalized name list. */
  97.        names_def_end
  98.     };
  99.  
  100.     init_names(fnd2);
  101.  
  102.     /* Make the standard BuildChar procedures executable. */
  103.     r_set_attrs(&name_Type1BuildChar, a_executable);
  104. }
  105.  
  106. /* .buildfont */
  107. int
  108. zbuildfont(register os_ptr op)
  109. {    int code;
  110.     ref *ptype;
  111.     ref *pbuildchar;
  112.     gs_font *pfont;
  113.     /* Validate and unpack the font. */
  114.     check_type(*op, t_dictionary);
  115.     if ( dict_find(op, &name_FontType, &ptype) <= 0 ||
  116.          !r_has_type(ptype, t_integer) ||
  117.          (ulong)(ptype->value.intval) > 3
  118.        )
  119.       return e_invalidfont;
  120.     /* Different FontTypes demand different checking. */
  121.     switch ( (int)(ptype->value.intval) )
  122.        {
  123.     case (int)ft_composite:
  124.         code = buildfont0(op, &pbuildchar, &pfont);
  125.         break;
  126.     case (int)ft_encrypted:
  127.         code = buildfont1(op, &pbuildchar, &pfont);
  128.         break;
  129.     case (int)ft_user_defined:
  130.         code = buildfont3(op, &pbuildchar, &pfont);
  131.         break;
  132.     default:
  133.         return e_invalidfont;
  134.        }
  135.     switch ( code )
  136.        {
  137.     case 0:                /* new font */
  138.         pfont->FontType = (font_type)ptype->value.intval;
  139.         ref_assign(&((font_data *)(pfont->client_data))->BuildChar,
  140.                pbuildchar);
  141.     case 1:                /* made by makefont/scalefont */
  142.         break;
  143.     default:
  144.         return code;
  145.        }
  146.     return 0;
  147. }
  148. /* Build a type 1 (Adobe encrypted) font. */
  149. private int
  150. buildfont1(os_ptr op, ref **ppbuildchar, gs_font **ppfont)
  151. {    ref *ppainttype;
  152.     int paint_type;
  153.     ref *psubrs;
  154.     ref *pcharstrings;
  155.     ref *pprivate;
  156.     ref *plenIV;
  157.     int lenIV;
  158.     ref *puniqueid;
  159.     static ref no_subrs;
  160.     gs_font *pfont;
  161.     font_data *pdata;
  162.     int code;
  163.     /* If no PaintType entry, set the paint_type member to 0. */
  164.     if ( dict_find(op, &name_PaintType, &ppainttype) <= 0 )
  165.         paint_type = 0;
  166.     else
  167.        {    if ( !(r_has_type(ppainttype, t_integer) &&
  168.                ppainttype->value.intval >= 0 &&
  169.                ppainttype->value.intval <= 2)
  170.            )
  171.             return e_invalidfont;
  172.         paint_type = (int)ppainttype->value.intval;
  173.        }
  174.     if ( dict_find(op, &name_CharStrings, &pcharstrings) <= 0 ||
  175.         !r_has_type(pcharstrings, t_dictionary) ||
  176.         dict_find(op, &name_Private, &pprivate) <= 0 ||
  177.         !r_has_type(pprivate, t_dictionary)
  178.        )
  179.         return e_invalidfont;
  180.     if ( dict_find(pprivate, &name_Subrs, &psubrs) > 0 )
  181.        {    check_array_else(*psubrs, e_invalidfont);
  182.        }
  183.     else
  184.        {    make_tasv(&no_subrs, t_string, 0, 0, bytes, (byte *)0),
  185.         psubrs = &no_subrs;
  186.        }
  187.     if ( dict_find(pprivate, &name_lenIV, &plenIV) > 0 )
  188.        {    if ( !r_has_type(plenIV, t_integer) ||
  189.             (ulong)(plenIV->value.intval) > 255 /* arbitrary */
  190.            )
  191.             return e_invalidfont;
  192.         lenIV = plenIV->value.intval;
  193.        }
  194.     else
  195.         lenIV = default_lenIV;
  196.     *ppbuildchar = &name_Type1BuildChar;
  197.     /* Do the work common to all non-composite font types. */
  198.     code = build_gs_simple_font(op, ppfont);
  199.     if ( code != 0 ) return code;
  200.     /* This is a new font, fill it in. */
  201.     pfont = *ppfont;
  202.     pdata = (font_data *)pfont->client_data;
  203.     ref_assign(&pdata->CharStrings, pcharstrings);
  204.     ref_assign(&pdata->Subrs, psubrs);
  205.     pfont->data.base.type1_data.subr_proc = z1_subr_proc;
  206.     pfont->data.base.type1_data.pop_proc = z1_pop_proc;
  207.     pfont->data.base.type1_data.proc_data = (char *)pdata;
  208.     pfont->data.base.type1_data.lenIV = lenIV;
  209.     /* Check that the UniqueIDs match.  This is part of the */
  210.     /* Adobe protection scheme, but we may as well emulate it. */
  211.     if ( pfont->data.base.UniqueID >= 0 )
  212.        {    if ( dict_find(pprivate, &name_UniqueID, &puniqueid) <= 0 ||
  213.              !r_has_type(puniqueid, t_integer) ||
  214.              puniqueid->value.intval != pfont->data.base.UniqueID
  215.            )
  216.             pfont->data.base.UniqueID = -1;
  217.        }
  218.     pfont->data.base.type1_data.PaintType = paint_type;
  219.     return 0;
  220. }
  221. /* Build a type 3 (user-defined) font. */
  222. private int
  223. buildfont3(os_ptr op, ref **ppbuildchar, gs_font **ppfont)
  224. {    int code;
  225.     code = dict_find(op, &name_BuildChar, ppbuildchar);
  226.     if ( code <= 0 ) return e_invalidfont;
  227.     check_proc(**ppbuildchar);
  228.     code = build_gs_simple_font(op, ppfont);
  229.     if ( code != 0 ) return code;
  230.     return 0;
  231. }
  232.  
  233. /* ------ Initialization procedure ------ */
  234.  
  235. op_def zfont2_op_defs[] = {
  236.     {"1.buildfont", zbuildfont},
  237.     op_def_end(zfont2_init)
  238. };
  239.  
  240. /* ------ Subroutines ------ */
  241.  
  242. /* Do the common work for building a font of any non-composite FontType. */
  243. /* The caller guarantees that *op is a dictionary. */
  244. private int
  245. build_gs_simple_font(os_ptr op, gs_font **ppfont)
  246. {    ref *pbbox;
  247.     float bbox[4];
  248.     long unique_id;
  249.     ref *puniqueid;
  250.     int code;
  251.     gs_font *pfont;
  252.     if ( dict_find(op, &name_FontBBox, &pbbox) <= 0 )
  253.       return e_invalidfont;
  254.     switch ( r_type(pbbox) )
  255.        {
  256.     default: return e_invalidfont;
  257.     case t_array: case t_mixedarray: case t_shortarray: ;
  258.         if ( r_size(pbbox) != 4 ) return e_invalidfont;
  259.        }
  260.        {    ushort *pbe = pbbox->value.packed;
  261.         ref rbe[4];
  262.         int i;
  263.         for ( i = 0; i < 4; i++ )
  264.            {    packed_get(pbe, rbe + i);
  265.             pbe = packed_next(pbe);
  266.            }
  267.         if ( num_params(rbe + 3, 4, bbox) < 0 )
  268.             return e_invalidfont;
  269.        }
  270.     /* If no UniqueID entry, set the UniqueID member to -1, */
  271.     /* because UniqueID need not be present in all fonts, */
  272.     /* and if it is, the legal range is 0 to 2^24-1. */
  273.     if ( dict_find(op, &name_UniqueID, &puniqueid) <= 0 )
  274.         unique_id = -1;
  275.     else
  276.        {    if ( !r_has_type(puniqueid, t_integer) ||
  277.              puniqueid->value.intval < 0 ||
  278.              puniqueid->value.intval > ((1L << 24) - 1)
  279.            )
  280.             return e_invalidfont;
  281.         unique_id = puniqueid->value.intval;
  282.        }
  283.     code = build_gs_font(op, ppfont);
  284.     if ( code != 0 ) return code;    /* invalid or scaled font */
  285.     pfont = *ppfont;
  286.     pfont->data.base.FontBBox.p.x = bbox[0];
  287.     pfont->data.base.FontBBox.p.y = bbox[1];
  288.     pfont->data.base.FontBBox.q.x = bbox[2];
  289.     pfont->data.base.FontBBox.q.y = bbox[3];
  290.     pfont->data.base.UniqueID = unique_id;
  291.     return 0;
  292. }
  293.  
  294. /* Do the common work for building a font of any FontType. */
  295. /* The caller guarantees that *op is a dictionary. */
  296. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  297. /* or a negative error code. */
  298. int
  299. build_gs_font(os_ptr op, gs_font **ppfont)
  300. {    ref *pmatrix;
  301.     gs_matrix mat;
  302.     ref *pencoding;
  303.     int code;
  304.     gs_font *pfont;
  305.     ref *pfid;
  306.     ref *aop = dict_access_ref(op);
  307.     if ( dict_find(op, &name_FontMatrix, &pmatrix) <= 0 ||
  308.         dict_find(op, &name_Encoding, &pencoding) <= 0 ||
  309.         read_matrix(pmatrix, &mat) < 0
  310.        )
  311.       return e_invalidfont;
  312.     switch ( r_type(pencoding) )
  313.        {
  314.     default: return e_invalidfont;
  315.     case t_array: case t_mixedarray: case t_shortarray: ;
  316.        }
  317.     code = dict_find(op, &name_FID, &pfid);
  318.     if ( r_has_attrs(aop, a_write) )
  319.        {    /* Assume this is a new font */
  320.         font_data *pdata;
  321.         if ( code > 0 ) return e_invalidfont;    /* has FID already */
  322.         if ( (pfont = (gs_font *)alloc(1, sizeof(gs_font), "buildfont(font)")) == 0 ||
  323.              (pdata = (font_data *)alloc(1, sizeof(font_data), "buildfont(data)")) == 0
  324.            )
  325.           return e_VMerror;
  326.         if ( (code = add_FID(op, pfont)) < 0 ) return code;
  327.         ref_assign(&pdata->dict, op);
  328.         ref_assign(&pdata->Encoding, pencoding);
  329.         pfont->base = pfont;
  330.         pfont->dir = ifont_dir;
  331.         pfont->client_data = (char *)pdata;
  332.         pfont->FontMatrix = mat;
  333.         pfont->build_char_proc = gs_no_build_char_proc;
  334.        }
  335.     else
  336.        {    /* Assume this was made by makefont or scalefont */
  337.         if ( code <= 0 || !r_has_type(pfid, t_fontID) )
  338.             return e_invalidfont;
  339.         pfont = pfid->value.pfont;
  340.        }
  341.     *ppfont = pfont;
  342.     return 0;
  343. }
  344.