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 / ZFONT2.C < prev    next >
C/C++ Source or Header  |  1992-09-07  |  7KB  |  212 lines

  1. /* Copyright (C) 1989, 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. /* zfont2.c */
  21. /* Font creation utilities 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 "bfont.h"
  32. #include "dict.h"
  33. #include "dparam.h"
  34. #include "name.h"
  35. #include "packed.h"
  36. #include "store.h"
  37.  
  38. /* Global font-related objects */
  39. /* Names of system-known keys in font dictionaries: */
  40. static ref name_FontType;
  41. static ref name_WMode;
  42. static ref name_FontBBox;
  43. static ref name_Encoding;
  44. ref name_UniqueID;            /* used by zfont1 */
  45. static ref name_XUID;
  46. static ref name_BuildChar;
  47.  
  48. /* The global font directory */
  49. extern gs_font_dir *ifont_dir;
  50.  
  51. /* Initialize the font building operators */
  52. private void
  53. zfont2_init()
  54. {    static const names_def fnd2[] = {
  55.  
  56.     /* Create the names of the standard elements of */
  57.     /* a font dictionary. */
  58.        { "FontType", &name_FontType },
  59.        { "WMode", &name_WMode },
  60.        { "FontBBox", &name_FontBBox },
  61.        { "Encoding", &name_Encoding },
  62.        { "UniqueID", &name_UniqueID },
  63.        { "XUID", &name_XUID },
  64.        { "BuildChar", &name_BuildChar },
  65.  
  66.     /* Mark the end of the initalized name list. */
  67.        names_def_end
  68.     };
  69.  
  70.     init_names(fnd2);
  71. }
  72.  
  73. /* .buildfont3 */
  74. /* Build a type 3 (user-defined) font. */
  75. int
  76. zbuildfont3(os_ptr op)
  77. {    int code;
  78.     ref *pbuildchar;
  79.     gs_font *pfont;
  80.     check_type(*op, t_dictionary);
  81.     code = dict_find(op, &name_BuildChar, &pbuildchar);
  82.     if ( code <= 0 ) return e_invalidfont;
  83.     check_proc(*pbuildchar);
  84.     return build_gs_simple_font(op, &pfont, ft_user_defined, (const ref *)pbuildchar);
  85. }
  86.  
  87. /* ------ Initialization procedure ------ */
  88.  
  89. op_def zfont2_op_defs[] = {
  90.     {"1.buildfont3", zbuildfont3},
  91.     op_def_end(zfont2_init)
  92. };
  93.  
  94. /* ------ Subroutines ------ */
  95.  
  96. /* Do the common work for building a font of any non-composite FontType. */
  97. /* The caller guarantees that *op is a dictionary. */
  98. int
  99. build_gs_simple_font(os_ptr op, gs_font **ppfont, font_type ftype, const ref *pbuildchar)
  100. {    ref *pbbox;
  101.     float bbox[4];
  102.     long unique_id;
  103.     ref *puniqueid;
  104.     int code;
  105.     gs_font *pfont;
  106.     if ( dict_find(op, &name_FontBBox, &pbbox) <= 0 )
  107.       return e_invalidfont;
  108.     switch ( r_type(pbbox) )
  109.        {
  110.     default: return e_invalidfont;
  111.     case t_array: case t_mixedarray: case t_shortarray: ;
  112.         if ( r_size(pbbox) != 4 ) return e_invalidfont;
  113.        }
  114.        {    const ref_packed *pbe = pbbox->value.packed;
  115.         ref rbe[4];
  116.         int i;
  117.         for ( i = 0; i < 4; i++ )
  118.            {    packed_get(pbe, rbe + i);
  119.             pbe = packed_next(pbe);
  120.            }
  121.         if ( num_params(rbe + 3, 4, bbox) < 0 )
  122.             return e_invalidfont;
  123.        }
  124.     /* If no UniqueID entry, set the UniqueID member to -1, */
  125.     /* because UniqueID need not be present in all fonts, */
  126.     /* and if it is, the legal range is 0 to 2^24-1. */
  127.     if ( dict_find(op, &name_UniqueID, &puniqueid) <= 0 )
  128.         unique_id = -1;
  129.     else
  130.        {    if ( !r_has_type(puniqueid, t_integer) ||
  131.              puniqueid->value.intval < 0 ||
  132.              puniqueid->value.intval > ((1L << 24) - 1)
  133.            )
  134.             return e_invalidfont;
  135.         unique_id = puniqueid->value.intval;
  136.         /* Apparently fonts created by Fontographer often have */
  137.         /* a UniqueID of 0, contrary to Adobe's specifications. */
  138.         /* Treat 0 as equivalent to -1 (no UniqueID). */
  139.         if ( unique_id == 0 ) unique_id = -1;
  140.        }
  141.     code = build_gs_font(op, ppfont, ftype, pbuildchar);
  142.     if ( code != 0 ) return code;    /* invalid or scaled font */
  143.     pfont = *ppfont;
  144.     pfont->data.base.FontBBox.p.x = bbox[0];
  145.     pfont->data.base.FontBBox.p.y = bbox[1];
  146.     pfont->data.base.FontBBox.q.x = bbox[2];
  147.     pfont->data.base.FontBBox.q.y = bbox[3];
  148.     pfont->data.base.UniqueID = unique_id;
  149.     return 0;
  150. }
  151.  
  152. /* Do the common work for building a font of any FontType. */
  153. /* The caller guarantees that *op is a dictionary. */
  154. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  155. /* or a negative error code. */
  156. int
  157. build_gs_font(os_ptr op, gs_font **ppfont, font_type ftype, const ref *pbuildchar)
  158. {    ref *pftype;
  159.     ref *pmatrix;
  160.     gs_matrix mat;
  161.     ref *pencoding;
  162.     int wmode;
  163.     int code;
  164.     gs_font *pfont;
  165.     ref *pfid;
  166.     ref *aop = dict_access_ref(op);
  167.     if ( dict_find(op, &name_FontType, &pftype) <= 0 ||
  168.         !r_has_type(pftype, t_integer) ||
  169.         pftype->value.intval != (int)ftype ||
  170.         dict_find(op, &name_FontMatrix, &pmatrix) <= 0 ||
  171.         dict_find(op, &name_Encoding, &pencoding) <= 0 ||
  172.         read_matrix(pmatrix, &mat) < 0
  173.        )
  174.       return e_invalidfont;
  175.     switch ( r_type(pencoding) )
  176.        {
  177.     default: return e_invalidfont;
  178.     case t_array: case t_mixedarray: case t_shortarray: ;
  179.        }
  180.     code = dict_int_param(op, &name_WMode, 0, 1, 0, &wmode);
  181.     if ( code < 0 ) return code;
  182.     code = dict_find(op, &name_FID, &pfid);
  183.     if ( r_has_attr(aop, a_write) )
  184.        {    /* Assume this is a new font */
  185.         font_data *pdata;
  186.         if ( code > 0 ) return e_invalidfont;    /* has FID already */
  187.         if ( (pfont = (gs_font *)alloc(1, sizeof(gs_font), "buildfont(font)")) == 0 ||
  188.              (pdata = (font_data *)alloc(1, sizeof(font_data), "buildfont(data)")) == 0
  189.            )
  190.           return e_VMerror;
  191.         if ( (code = add_FID(op, pfont)) < 0 ) return code;
  192.         ref_assign(&pdata->dict, op);
  193.         ref_assign(&pdata->BuildChar, pbuildchar);
  194.         ref_assign(&pdata->Encoding, pencoding);
  195.         pfont->base = pfont;
  196.         pfont->dir = ifont_dir;
  197.         pfont->client_data = (char *)pdata;
  198.         pfont->FontType = ftype;
  199.         pfont->FontMatrix = mat;
  200.         pfont->WMode = wmode;
  201.         pfont->build_char_proc = gs_no_build_char_proc;
  202.        }
  203.     else
  204.        {    /* Assume this was made by makefont or scalefont */
  205.         if ( code <= 0 || !r_has_type(pfid, t_fontID) )
  206.             return e_invalidfont;
  207.         pfont = pfid->value.pfont;
  208.        }
  209.     *ppfont = pfont;
  210.     return 0;
  211. }
  212.