home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / gs-2.6.1.4-src.lha / src / amiga / gs-2.6.1.4 / zfont2.c < prev    next >
C/C++ Source or Header  |  1994-01-27  |  13KB  |  427 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfont2.c */
  20. /* Font creation utilities for Ghostscript */
  21. #include "memory_.h"
  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 "ilevel.h"
  35. #include "iname.h"
  36. #include "packed.h"
  37. #include "save.h"        /* for alloc_array */
  38. #include "store.h"
  39.  
  40. /* Global font-related objects */
  41. /* Names of system-known keys in font dictionaries: */
  42. static ref name_FontType;
  43. static ref name_FontName;
  44. static ref name_WMode;
  45. static ref name_Encoding;
  46. static ref name_FontBBox;
  47. static ref name_UniqueID;
  48. static ref name_XUID;
  49. static ref name_BuildChar;
  50. static ref name_BuildGlyph;
  51. /* Bitmap fonts */
  52. static ref name_BitmapWidths;
  53. static ref name_ExactSize;
  54. static ref name_InBetweenSize;
  55. static ref name_TransformedChar;
  56.  
  57. /* Registered encodings.  See font.h for documentation. */
  58. ref registered_Encodings[registered_Encodings_countof];
  59.  
  60. /* Initialize the font building operators */
  61. private void
  62. zfont2_init(void)
  63. {    static const names_def fnd2[] = {
  64.  
  65.     /* Create the names of the standard elements of */
  66.     /* a font dictionary. */
  67.        { "FontType", &name_FontType },
  68.        { "FontName", &name_FontName },
  69.        { "WMode", &name_WMode },
  70.        { "Encoding", &name_Encoding },
  71.        { "FontBBox", &name_FontBBox },
  72.        { "UniqueID", &name_UniqueID },
  73.        { "XUID", &name_XUID },
  74.        { "BuildChar", &name_BuildChar },
  75.        { "BuildGlyph", &name_BuildGlyph },
  76.        { "BitmapWidths", &name_BitmapWidths },
  77.        { "ExactSize", &name_ExactSize },
  78.        { "InBetweenSize", &name_InBetweenSize },
  79.        { "TransformedChar", &name_TransformedChar },
  80.  
  81.     /* Mark the end of the initialized name list. */
  82.        names_def_end
  83.     };
  84.  
  85.     init_names(fnd2);
  86.  
  87.     /* Initialize the registered Encodings. */
  88.     {    int i;
  89.         for ( i = 0; i < registered_Encodings_countof; i++ )
  90.             make_array(®istered_Encodings[i], 0, 0, NULL);
  91.     }
  92. }
  93.  
  94. /* <string|name> <font_dict> .buildfont3 <string|name> <font> */
  95. /* Build a type 3 (user-defined) font. */
  96. int
  97. zbuildfont3(os_ptr op)
  98. {    int ccode, gcode, code;
  99.     build_proc_refs build;
  100.     ref pnull;
  101.     gs_font *pfont;
  102.     check_type(*op, t_dictionary);
  103.     ccode = dict_find(op, &name_BuildChar, &build.pBuildChar);
  104.     gcode = dict_find(op, &name_BuildGlyph, &build.pBuildGlyph);
  105.     make_null(&pnull);
  106.     if ( ccode <= 0 )
  107.     {    if ( gcode <= 0 )
  108.             return_error(e_invalidfont);
  109.         build.pBuildChar = &pnull;
  110.     }
  111.     else
  112.     {    check_proc(*build.pBuildChar);
  113.     }
  114.     if ( gcode <= 0 )
  115.         build.pBuildGlyph = &pnull;
  116.     else
  117.     {    check_proc(*build.pBuildGlyph);
  118.     }
  119.     code = build_gs_simple_font(op, &pfont, ft_user_defined, &build);
  120.     if ( code < 0 )
  121.         return code;
  122.     return define_gs_font(pfont);
  123. }
  124.  
  125. /* <int> <packedarray> .registerencoding - */
  126. private int
  127. zregisterencoding(register os_ptr op)
  128. {    long i;
  129.     check_type(op[-1], t_integer);
  130.     check_read_type(*op, t_shortarray);
  131.     i = op[-1].value.intval;
  132.     if ( i >= 0 && i < registered_Encodings_countof )
  133.     {    ref_assign_old(®istered_Encodings[i], op,
  134.                    ".registerencoding");
  135.     }
  136.     pop(2);
  137.     return 0;
  138. }
  139.  
  140. /* Encode a character. */
  141. /* (This is very inefficient right now; we can speed it up later.) */
  142. private gs_glyph
  143. zfont_encode_char(gs_show_enum *penum, gs_font *pfont, gs_char *pchr)
  144. {    const ref *pencoding =
  145.         &((font_data *)(pfont->client_data))->Encoding;
  146.     ulong index = *pchr;    /* work around VAX widening bug */
  147.     ref cname;
  148.     int code = array_get(pencoding, (long)index, &cname);
  149.     if ( code < 0 || !r_has_type(&cname, t_name) )
  150.         return gs_no_glyph;
  151.     return (gs_glyph)name_index(&cname);
  152. }
  153.  
  154. /* Get the name of a glyph. */
  155. /* The following typedef is needed to work around a bug in */
  156. /* some AIX C compiler. */
  157. typedef const char *const_chars;
  158. private const_chars
  159. zfont_glyph_name(gs_glyph index, uint *plen)
  160. {    ref nref, sref;
  161.     name_index_ref(index, &nref);
  162.     name_string_ref(&nref, &sref);
  163.     *plen = r_size(&sref);
  164.     return (const char *)sref.value.const_bytes;
  165. }
  166.  
  167. /* ------ Initialization procedure ------ */
  168.  
  169. op_def zfont2_op_defs[] = {
  170.     {"2.buildfont3", zbuildfont3},
  171.     {"2.registerencoding", zregisterencoding},
  172.     op_def_end(zfont2_init)
  173. };
  174.  
  175. /* ------ Subroutines ------ */
  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 **ppfont, font_type ftype,
  181.   const build_proc_refs *pbuild)
  182. {    ref *pbbox;
  183.     float bbox[4];
  184.     gs_uid uid;
  185.     int code;
  186.     gs_font *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(op, &name_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 = get_gs_font_uid(op, &uid);
  209.     if ( code < 0 ) return code;
  210.     code = build_gs_font(op, ppfont, ftype, pbuild);
  211.     if ( code != 0 ) return code;    /* invalid or scaled font */
  212.     pfont = *ppfont;
  213.     pfont->data.base.FontBBox.p.x = bbox[0];
  214.     pfont->data.base.FontBBox.p.y = bbox[1];
  215.     pfont->data.base.FontBBox.q.x = bbox[2];
  216.     pfont->data.base.FontBBox.q.y = bbox[3];
  217.     pfont->data.base.UID = uid;
  218.     {    const ref *pfe =
  219.             &((font_data *)(pfont->client_data))->Encoding;
  220.         int index;
  221.         for ( index = registered_Encodings_countof; --index >= 0; )
  222.           if ( obj_eq(pfe, ®istered_Encodings[index]) )
  223.             break;
  224.         pfont->data.base.encoding_index = index;
  225.         if ( index < 0 )
  226.         {    /* Look for an encoding that's "close". */
  227.             int near_index = -1;
  228.             uint esize = r_size(pfe);
  229.             uint best = esize / 3;    /* must match at least this many */
  230.             for ( index = registered_Encodings_countof; --index >= 0; )
  231.             {    const ref *pre = ®istered_Encodings[index];
  232.                 uint match = esize;
  233.                 int i;
  234.                 if ( r_size(pre) != esize )
  235.                     continue;
  236.                 for ( i = esize; --i >= 0; )
  237.                 {    ref fe, re;
  238.                     array_get(pfe, (long)i, &fe);
  239.                     array_get(pre, (long)i, &re);
  240.                     if ( !obj_eq(&fe, &re) )
  241.                         match--;
  242.                 }
  243.                 if ( match > best )
  244.                     best = match,
  245.                     near_index = index;
  246.             }
  247.             index = near_index;
  248.         }
  249.         pfont->data.base.nearest_encoding_index = index;
  250.     }
  251.     return 0;
  252. }
  253.  
  254. /* Get the UniqueID or XUID from a font dictionary. */
  255. int
  256. get_gs_font_uid(os_ptr op, gs_uid *puid)
  257. {    ref *puniqueid;
  258.     /* In a Level 2 environment, check for XUID first. */
  259.     if ( level2_enabled && dict_find(op, &name_XUID, &puniqueid) > 0 )
  260.     {    long *xvalues;
  261.         uint size, i;
  262.         check_array_else(*puniqueid, e_invalidfont);
  263.         size = r_size(puniqueid);
  264.         if ( size == 0 )
  265.             return_error(e_invalidfont);
  266.         xvalues = (long *)alloc(size, sizeof(long), "get XUID");
  267.         if ( xvalues == 0 )
  268.             return_error(e_VMerror);
  269.         /* Get the values from the XUID array. */
  270.         for ( i = 0; i < size; i++ )
  271.         {    const ref *pvalue = puniqueid->value.const_refs + i;
  272.             if ( !r_has_type(pvalue, t_integer) )
  273.             {    alloc_free((char *)xvalues, size, sizeof(long), "get XUID");
  274.                 return_error(e_invalidfont);
  275.             }
  276.             xvalu