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

  1. /* Copyright (C) 1989, 1992, 1993, 1994 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. /* zfont2.c */
  20. /* Font creation utilities */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "gxfixed.h"
  27. #include "gsmatrix.h"
  28. #include "gxdevice.h"
  29. #include "gschar.h"
  30. #include "gxfont.h"
  31. #include "bfont.h"
  32. #include "ialloc.h"
  33. #include "idict.h"
  34. #include "idparam.h"
  35. #include "ilevel.h"
  36. #include "iname.h"
  37. #include "ipacked.h"
  38. #include "istruct.h"
  39. #include "store.h"
  40.  
  41. /* Registered encodings.  See ifont.h for documentation. */
  42. ref registered_Encodings;
  43. private ref *registered_Encodings_p = ®istered_Encodings;
  44. private gs_gc_root_t registered_Encodings_root;
  45.  
  46. /* Structure descriptor */
  47. public_st_font_data();
  48.  
  49. /* Initialize the font building operators */
  50. private void
  51. zfont2_init(void)
  52. {    /* Initialize the registered Encodings. */
  53.     {    int i;
  54.         ialloc_ref_array(®istered_Encodings, a_readonly,
  55.                  registered_Encodings_countof,
  56.                  "registered_Encodings");
  57.         for ( i = 0; i < registered_Encodings_countof; i++ )
  58.           make_array(®istered_Encoding(i), 0, 0, NULL);
  59.     }
  60.     gs_register_ref_root(imemory, ®istered_Encodings_root,
  61.                  (void **)®istered_Encodings_p,
  62.                  "registered_Encodings");
  63. }
  64.  
  65. /* <string|name> <font_dict> .buildfont3 <string|name> <font> */
  66. /* Build a type 3 (user-defined) font. */
  67. int
  68. zbuildfont3(os_ptr op)
  69. {    int ccode, gcode, code;
  70.     ref *pBuildChar;
  71.     ref *pBuildGlyph;
  72.     build_proc_refs build;
  73.     gs_font_base *pfont;
  74.     check_type(*op, t_dictionary);
  75.     ccode = dict_find_string(op, "BuildChar", &pBuildChar);
  76.     gcode = dict_find_string(op, "BuildGlyph", &pBuildGlyph);
  77.     if ( ccode <= 0 )
  78.     {    if ( gcode <= 0 )
  79.             return_error(e_invalidfont);
  80.         make_null(&build.BuildChar);
  81.     }
  82.     else
  83.     {    build.BuildChar = *pBuildChar;
  84.         check_proc(build.BuildChar);
  85.     }
  86.     if ( gcode <= 0 )
  87.         make_null(&build.BuildGlyph);
  88.     else
  89.     {    build.BuildGlyph = *pBuildGlyph;
  90.         check_proc(build.BuildGlyph);
  91.     }
  92.     code = build_gs_simple_font(op, &pfont, ft_user_defined,
  93.                     &st_gs_font_base, &build);
  94.     if ( code < 0 )
  95.         return code;
  96.     return define_gs_font((gs_font *)pfont);
  97. }
  98.  
  99. /* <int> <array|shortarray> .registerencoding - */
  100. private int
  101. zregisterencoding(register os_ptr op)
  102. {    long i;
  103.     check_type(op[-1], t_integer);
  104.     switch ( r_type(op) )
  105.       {
  106.       case t_array:
  107.       case t_shortarray:
  108.         break;
  109.       default:
  110.         return_error(e_typecheck);
  111.       }
  112.     check_read(*op);
  113.     for ( i = r_size(op); i > 0; )
  114.       {    ref cname;
  115.         array_get(op, --i, &cname);
  116.         check_type(cname, t_name);
  117.       }
  118.     i = op[-1].value.intval;
  119.     if ( i >= 0 && i < registered_Encodings_countof )
  120.     {    ref *penc = ®istered_Encoding(i);
  121.         ref_assign_old(NULL, penc, op, ".registerencoding");
  122.     }
  123.     pop(2);
  124.     return 0;
  125. }
  126.  
  127. /* Encode a character. */
  128. /* (This is very inefficient right now; we can speed it up later.) */
  129. private gs_glyph
  130. zfont_encode_char(gs_show_enum *penum, gs_font *pfont, gs_char *pchr)
  131. {    const ref *pencoding = &pfont_data(pfont)->Encoding;
  132.     ulong index = *pchr;    /* work around VAX widening bug */
  133.     ref cname;
  134.     int code = array_get(pencoding, (long)index, &cname);
  135.     if ( code < 0 || !r_has_type(&cname, t_name) )
  136.         return gs_no_glyph;
  137.     return (gs_glyph)name_index(&cname);
  138. }
  139.  
  140. /* Get the name of a glyph. */
  141. /* The following typedef is needed to work around a bug in */
  142. /* some AIX C compiler. */
  143. typedef const char *const_chars;
  144. private const_chars
  145. zfont_glyph_name(gs_glyph index, uint *plen)
  146. {    ref nref, sref;
  147.     name_index_ref(index, &nref);
  148.     name_string_ref(&nref, &sref);
  149.     *plen = r_size(&sref);
  150.     return (const char *)sref.value.const_bytes;
  151. }
  152.  
  153. /* ------ Initialization procedure ------ */
  154.  
  155. op_def zfont2_op_defs[] = {
  156.     {"2.buildfont3", zbuildfont3},
  157.     {"2.registerencoding", zregisterencoding},
  158.     op_def_end(zfont2_init)
  159. };
  160.  
  161. /* ------ Subroutines ------ */
  162.  
  163. /* Convert strings to executable names for build_proc_refs. */
  164. int
  165. build_proc_name_refs(build_proc_refs *pbuild,
  166.   const char _ds *bcstr, const char _ds *bgstr)
  167. {    int code;
  168.     if ( (code = name_ref((const byte *)bcstr, strlen(bcstr), &pbuild->BuildChar, 0)) < 0 ||
  169.          (code = name_ref((const byte *)bgstr, strlen(bgstr), &pbuild->BuildGlyph, 0)) < 0
  170.        )
  171.         return code;
  172.     r_set_attrs(&pbuild->BuildChar, a_executable);
  173.     r_set_attrs(&pbuild->BuildGlyph, a_executable);
  174.     return 0;
  175. }
  176.  
  177. /* Do the common work for building a font of any non-composite FontType. */
  178. /* The caller guarantees that *op is a dictionary. */
  179. int
  180. build_gs_simple_font(os_ptr op, gs_font_base **ppfont, font_type ftype,
  181.   gs_memory_type_ptr_t pstype, const build_proc_refs *pbuild)
  182. {    ref *pbbox;
  183.     float bbox[4];
  184.     gs_uid uid;
  185.     int code;
  186.     gs_font_base *pfont;
  187.     /* Pre-clear the bbox in case it's invalid. */
  188.     /* The Red Books say that FontBBox is required, */
  189.     /* but the Adobe interpreters don't require it, */
  190.     /* and a few user-written fonts don't supply it, */
  191.     /* or supply one of the wrong size (!). */
  192.     bbox[0] = bbox[1] = bbox[2] = bbox[3] = 0.0;
  193.     if ( dict_find_string(op, "FontBBox", &pbbox) > 0 )
  194.     {    if ( !r_is_array(pbbox) )
  195.             return_error(e_invalidfont);
  196.         if ( r_size(pbbox) == 4 )
  197.         {    const ref_packed *pbe = pbbox->value.packed;
  198.             ref rbe[4];
  199.             int i;
  200.             for ( i = 0; i < 4; i++ )
  201.             {    packed_get(pbe, rbe + i);
  202.                 pbe = packed_next(pbe);
  203.             }
  204.             if ( num_params(rbe + 3, 4, bbox) < 0 )
  205.                 return_error(e_invalidfont);
  206.         }
  207.     }
  208.     code = dict_uid_param(op, &uid, 0, imemory);
  209.     if ( code < 0 ) return code;
  210.     code = build_gs_font(op, (gs_font **)ppfont, ftype, pstype, pbuild);
  211.     if ( code != 0 ) return code;    /* invalid or scaled font */
  212.     pfont = *ppfont;
  213.     pfont->procs.init_fstack = gs_default_init_fstack;
  214.     pfont->procs.next_char = gs_default_next_char;
  215.     pfont->procs.define_font = gs_no_define_font;
  216.     pfont->procs.make_font = zdefault_make_font;
  217.     pfont->FontBBox.p.x = bbox[0];
  218.     pfont->FontBBox.p.y = bbox[1];
  219.     pfont->FontBBox.q.x = bbox[2];
  220.     pfont->FontBBox.q.y = bbox[3];
  221.     pfont->UID = uid;
  222.     {    const ref *pfe = &pfont_data(pfont)->Encoding;
  223.         int index;
  224.         for ( index = registered_Encodings_countof; --index >= 0; )
  225.           if ( obj_eq(pfe, ®istered_Encoding(index)) )
  226.             break;
  227.         pfont->encoding_index = index;
  228.         if ( index < 0 )
  229.         {    /* Look for an encoding that's "close". */
  230.             int near_index = -1;
  231.             uint esize = r_size(pfe);
  232.             uint best = esize / 3;    /* must match at least this many */
  233.             for ( index = registered_Encodings_countof; --index >= 0; )
  234.             {    const ref *pre = ®istered_Encoding(index);
  235.                 bool r_packed = r_has_type(pre, t_shortarray);
  236.                 bool f_packed = !r_has_type(pfe, t_array);
  237.                 uint match = esize;
  238.                 int i;
  239.                 ref fchar;
  240.                 const ref *pfchar = &fchar;
  241.                 if ( r_size(pre) != esize )
  242.                     continue;
  243.                 for ( i = esize; --i >= 0; )
  244.                 {    uint rnidx =
  245.                       (r_packed ?
  246.                        name_index(pre->value.refs + i) :
  247.                        packed_name_index(pre->value.packed + i));
  248.                     if ( f_packed )
  249.                       array_get(pfe, (long)i, &fchar);
  250.                     else
  251.                       pfchar = pfe->value.const_refs + i;
  252.                     if ( !r_has_type(pfchar, t_name) ||
  253.                          name_index(pfchar) != rnidx
  254.                        )
  255.                       if ( --match <= best )
  256.                         break;
  257.                 }
  258.                 if ( match > best )
  259.                     best = match,
  260.                     near_index = index;
  261.             }
  262.             index = near_index;
  263.         }
  264.         pfont->nearest_encoding_index = index;
  265.     }
  266.     return 0;
  267. }
  268.  
  269. /* Do the common work for building a font of any FontType. */
  270. /* The caller guarantees that *op is a dictionary. */
  271. /* op[-1] must be the key under which the font is being registered */
  272. /* in FontDirectory, normally a name or string. */
  273. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  274. /* or a negative error code. */
  275. private void get_font_name(P2(ref *, const ref *));
  276. private void copy_font_name(P2(gs_font_name *, const ref *));
  277. int
  278. build_gs_font(os_ptr op, gs_font **ppfont, font_type ftype,
  279.   gs_memory_type_ptr_t pstype, const build_proc_refs *pbuild)
  280. {    ref kname, fname;        /* t_string */
  281.     ref *pftype;
  282.     ref *pfontname;
  283.     ref *pmatrix;
  284.     gs_matrix mat;
  285.     ref *pencoding;
  286.     bool bitmapwidths;
  287.     int exactsize, inbetweensize, transformedchar;
  288.     int wmode;
  289.     int code;
  290.     gs_font *pfont;
  291.     ref *pfid;
  292.     ref *aop = dict_access_ref(op);
  293.     get_font_name(&kname, op - 1);
  294.     if ( dict_find_string(op, "FontType", &pftype) <= 0 ||
  295.         !r_has_type(pftype, t_integer) ||
  296.         pftype->value.intval != (int)ftype ||
  297.         dict_find_string(op, "FontMatrix", &pmatrix) <= 0 ||
  298.         read_matrix(pmatrix, &mat) < 0 ||
  299.         dict_find_string(op, "Encoding", &pencoding) <= 0 ||
  300.         !r_is_array(pencoding)
  301.        )
  302.         return_error(e_invalidfont);
  303.     if ( dict_find_string(op, "FontName", &pfontname) > 0 )
  304.         get_font_name(&fname, pfontname);
  305.     else
  306.         make_string(&fname, a_readonly, 0, NULL);
  307.     if ( (code = dict_int_param(op, "WMode", 0, 1, 0, &wmode)) < 0 ||
  308.          (code = dict_bool_param(op, "BitmapWidths", false, &bitmapwidths)) < 0 ||
  309.          (code = dict_int_param(op, "ExactSize", 0, 2, fbit_use_bitmaps, &exactsize)) < 0 ||
  310.          (code = dict_int_param(op, "InBetweenSize", 0, 2, fbit_use_outlines, &inbetweensize)) < 0 ||
  311.          (code = dict_int_param(op, "TransformedChar", 0, 2, fbit_use_outlines, &transformedchar)) < 0
  312.        )
  313.         return code;
  314.     code = dict_find_string(op, "FID", &pfid);
  315.     if ( code > 0 )
  316.     {    if ( !r_has_type(pfid, t_fontID) )
  317.           return_error(e_invalidfont);
  318.         /*
  319.          * If this font has a FID entry already, it might be
  320.          * a scaled font made by makefont or scalefont,
  321.          * or it might just be an existing font being registered
  322.          * under a second name.  The latter is only allowed
  323.          * in Level 2 environments.
  324.          */
  325.         pfont = r_ptr(pfid, gs_font);
  326.         if ( pfont->base == pfont )    /* original font */
  327.         {    if ( !level2_enabled )
  328.               return_error(e_invalidfont);
  329.             *ppfont = pfont;
  330.             return 0;
  331.         }
  332.         /* This was made by makefont or scalefont. */
  333.         /* Just insert the new name. */
  334.         code = 1;
  335.         goto set_name;
  336.     }
  337.     /* This is a new font. */
  338.     if ( !r_has_attr(aop, a_write) )
  339.         return_error(e_invalidaccess);
  340.     {    font_data *pdata;
  341.         pfont = ialloc_struct(gs_font, pstype,
  342.                       "buildfont(font)");
  343.         pdata = ialloc_struct(font_data, &st_font_data,
  344.                       "buildfont(data)");
  345.         if ( pfont == 0 || pdata == 0 )
  346.             code = gs_note_error(e_VMerror);
  347.         else
  348.             code = add_FID(op, pfont);
  349.         if ( code < 0 )
  350.           {    ifree_object(pdata, "buildfont(data)");
  351.             ifree_object(pfont, "buildfont(font)");
  352.             return code;
  353.           }
  354.         refset_null((ref *)pdata, sizeof(font_data) / sizeof(ref));
  355.         ref_assign_new(&pdata->dict, op);
  356.         ref_assign_new(&pdata->BuildChar, &pbuild->BuildChar);
  357.         ref_assign_new(&pdata->BuildGlyph, &pbuild->BuildGlyph);
  358.         ref_assign_new(&pdata->Encoding, pencoding);
  359.         pfont->base = pfont;
  360.         pfont->memory = imemory;
  361.         pfont->dir = 0;
  362.         pfont->client_data = pdata;
  363.         pfont->FontType = ftype;
  364.         pfont->FontMatrix = mat;
  365.         pfont->BitmapWidths = bitmapwidths;
  366.         pfont->ExactSize = exactsize;
  367.         pfont->InBetweenSize = inbetweensize;
  368.         pfont->TransformedChar = transformedchar;
  369.         pfont->WMode = wmode;
  370.         pfont->procs.build_char = gs_no_build_char;
  371.         pfont->procs.encode_char = zfont_encode_char;
  372.         pfont->procs.glyph_name = zfont_glyph_name;
  373.     }
  374.     code = 0;
  375. set_name:
  376.     copy_font_name(&pfont->key_name, &kname);
  377.     copy_font_name(&pfont->font_name, &fname);
  378.     *ppfont = pfont;
  379.     return code;
  380. }
  381.  
  382. /* Get the string corresponding to a font name. */
  383. /* If the font name isn't a name or a string, return an empty string. */
  384. private void
  385. get_font_name(ref *pfname, const ref *op)
  386. {    switch ( r_type(op) )
  387.     {
  388.     case t_string:
  389.         *pfname = *op;
  390.         break;
  391.     case t_name:
  392.         name_string_ref(op, pfname);
  393.         break;
  394.     default:
  395.         /* This is weird, but legal.... */
  396.         make_string(pfname, a_readonly, 0, NULL);
  397.     }
  398. }
  399.  
  400. /* Copy a font name into the gs_font structure. */
  401. private void
  402. copy_font_name(gs_font_name *pfstr, const ref *pfname)
  403. {    uint size = r_size(pfname);
  404.     if ( size > gs_font_name_max )
  405.         size = gs_font_name_max;
  406.     memcpy(&pfstr->chars[0], pfname->value.const_bytes, size);
  407.     /* Following is only for debugging printout. */
  408.     pfstr->chars[size] = 0;
  409.     pfstr->size = size;
  410. }
  411.  
  412. /* Finish building a font, by calling gs_definefont if needed. */
  413. int
  414. define_gs_font(gs_font *pfont)
  415. {    return (pfont->base == pfont && pfont->dir == 0 ?    /* i.e., unregistered original font */
  416.         gs_definefont(ifont_dir, pfont) :
  417.         0);
  418. }
  419.