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 / ZCHAR.C < prev    next >
C/C++ Source or Header  |  1992-05-28  |  14KB  |  497 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. /* zchar.c */
  21. /* Character operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"            /* for gstype1.h */
  26. #include "gxmatrix.h"            /* for font.h */
  27. #include "gschar.h"
  28. #include "gxdevice.h"            /* for gxfont.h */
  29. #include "gxfont.h"
  30. #include "gxtype1.h"            /* should be gstype1.h, but */
  31.                     /* we need sizeof(gs_type1_state) */
  32.                     /* so we can use stack allocation */
  33. #include "gzpath.h"            /* for type1addpath: see below */
  34. #include "gzstate.h"
  35. #include "alloc.h"
  36. #include "dict.h"
  37. #include "dstack.h"            /* for systemdict */
  38. #include "font.h"
  39. #include "estack.h"
  40. #include "state.h"
  41. #include "store.h"
  42.  
  43. /* All the character rendering operators use the execution stack */
  44. /* for loop control -- see estack.h for details. */
  45. /* The information pushed by these operators is as follows: */
  46. /*    the enumerator (t_string, but points to a gs_show_enum); */
  47. /*    a slot for the procedure for kshow, unused otherwise; */
  48. /*    the procedure to be called at the end of the enumeration */
  49. /*        (t_operator, but called directly, not by the interpreter); */
  50. /*    the usual e-stack mark (t_null). */
  51. #define snumpush 4
  52. #define senum (gs_show_enum *)(esp->value.bytes)
  53. #define sslot esp[-1]
  54. #define seproc esp[-2]
  55.  
  56. /* Imports */
  57. extern ref name_StandardEncoding;
  58.  
  59. /* Forward references */
  60. private int setup_show(P3(ref *, op_proc_p, int));
  61. private int show_continue(P1(os_ptr));
  62. private int i_show_continue;
  63. private int finish_show(P1(os_ptr));
  64. private int i_finish_show;
  65. private int finish_stringwidth(P1(os_ptr));
  66. private int i_finish_stringwidth;
  67. private gs_show_enum *find_show(P0());
  68. private void free_show(P0());
  69.  
  70. /* show */
  71. int
  72. zshow(register os_ptr op)
  73. {    int code = setup_show(op, finish_show, i_finish_show);
  74.     if ( code < 0 ) return code;
  75.     if ( (code = gs_show_n_init(senum, igs, (char *)op->value.bytes, r_size(op))) < 0 )
  76.        {    free_show();
  77.         return code;
  78.        }
  79.     pop(1);  op--;
  80.     return show_continue(op);
  81. }
  82.  
  83. /* ashow */
  84. int
  85. zashow(register os_ptr op)
  86. {    int code;
  87.     float axy[2];
  88.     if (    (code = num_params(op - 1, 2, axy)) < 0 ||
  89.         (code = setup_show(op, finish_show, i_finish_show)) < 0
  90.        )
  91.         return code;
  92.     if ( (code = gs_ashow_n_init(senum, igs, axy[0], axy[1], (char *)op->value.bytes, r_size(op))) < 0 )
  93.        {    free_show();
  94.         return code;
  95.        }
  96.     pop(3);  op -= 3;
  97.     return show_continue(op);
  98. }
  99.  
  100. /* widthshow */
  101. int
  102. zwidthshow(register os_ptr op)
  103. {    int code;
  104.     float cxy[2];
  105.     check_type(op[-1], t_integer);
  106.     if ( (ulong)(op[-1].value.intval) > 255 ) return e_rangecheck;
  107.     if (    (code = num_params(op - 2, 2, cxy)) < 0 ||
  108.         (code = setup_show(op, finish_show, i_finish_show)) < 0
  109.        )
  110.         return code;
  111.     if ( (code = gs_widthshow_n_init(senum, igs, cxy[0], cxy[1],
  112.                      (char)op[-1].value.intval,
  113.                      (char *)op->value.bytes,
  114.                      r_size(op))) < 0 )
  115.        {    free_show();
  116.         return code;
  117.        }
  118.     pop(4);  op -= 4;
  119.     return show_continue(op);
  120. }
  121.  
  122. /* awidthshow */
  123. int
  124. zawidthshow(register os_ptr op)
  125. {    int code;
  126.     float cxy[2], axy[2];
  127.     check_type(op[-3], t_integer);
  128.     if ( (ulong)(op[-3].value.intval) > 255 ) return e_rangecheck;
  129.     if (    (code = num_params(op - 4, 2, cxy)) < 0 ||
  130.         (code = num_params(op - 1, 2, axy)) < 0 ||
  131.         (code = setup_show(op, finish_show, i_finish_show)) < 0
  132.        )
  133.         return code;
  134.     if ( (code = gs_awidthshow_n_init(senum, igs, cxy[0], cxy[1],
  135.                       (char)op[-3].value.intval,
  136.                       axy[0], axy[1],
  137.                       (char *)op->value.bytes,
  138.                       r_size(op))) < 0 )
  139.        {    free_show();
  140.         return code;
  141.        }
  142.     pop(6);  op -= 6;
  143.     return show_continue(op);
  144. }
  145.  
  146. /* kshow */
  147. int
  148. zkshow(register os_ptr op)
  149. {    int code;
  150.     check_proc(op[-1]);
  151.     if ( (code = setup_show(op, finish_show, i_finish_show)) < 0 ) return code;
  152.     if ( (code = gs_kshow_n_init(senum, igs, (char *)op->value.bytes, r_size(op))) < 0 )
  153.        {    free_show();
  154.         return code;
  155.        }
  156.     sslot = op[-1];        /* save kerning proc */
  157.     pop(2);  op -= 2;
  158.     return show_continue(op);
  159. }
  160.  
  161. /* Common finish procedure for all show operations. */
  162. /* Doesn't have to do anything. */
  163. private int
  164. finish_show(os_ptr op)
  165. {    return 0;
  166. }
  167.  
  168. /* stringwidth */
  169. int
  170. zstringwidth(register os_ptr op)
  171. {    int code = setup_show(op, finish_stringwidth, i_finish_stringwidth);
  172.     if ( code < 0 ) return code;
  173.     if ( (code = gs_stringwidth_n_init(senum, igs, (char *)op->value.bytes, r_size(op))) < 0 )
  174.        {    free_show();
  175.         return code;
  176.        }
  177.     pop(1);  op--;
  178.     return show_continue(op);
  179. }
  180. /* Finishing procedure for stringwidth. */
  181. /* Pushes the accumulated width. */
  182. private int
  183. finish_stringwidth(register os_ptr op)
  184. {    gs_point width;
  185.     gs_show_width(senum, &width);
  186.     push(2);
  187.     make_real(op - 1, width.x);
  188.     make_real(op, width.y);
  189.     return 0;
  190. }
  191.  
  192. /* charpath */
  193. int
  194. zcharpath(register os_ptr op)
  195. {    int code;
  196.     check_type(*op, t_boolean);
  197.     code = setup_show(op - 1, finish_show, i_finish_show);
  198.     if ( code < 0 ) return code;
  199.     if ( (code = gs_charpath_n_init(senum, igs, (char *)op[-1].value.bytes, r_size(op - 1), op->value.index)) < 0 )
  200.        {    free_show();
  201.         return code;
  202.        }
  203.     pop(2);  op -= 2;
  204.     return show_continue(op);
  205. }
  206.  
  207. /* setcachedevice */
  208. int
  209. zsetcachedevice(register os_ptr op)
  210. {    float wbox[6];
  211.     int npop = 6;
  212.     gs_show_enum *penum = find_show();
  213.     int code = num_params(op, 6, wbox);
  214.     if ( penum == 0 ) return e_undefined;
  215.     if ( code < 0 )
  216.        {    /* P*stScr*pt implementations apparently allow the */
  217.         /* bounding box to be specified as a 4-element array. */
  218.         /* Check for this here. */
  219.         check_array(*op);
  220.         if ( r_size(op) != 4 ||
  221.              num_params(op - 1, 2, wbox) < 0 ||
  222.              num_params(op->value.refs + 3, 4, wbox + 2) < 0
  223.            )
  224.             return code;
  225.         npop = 3;
  226.        }
  227.     if ( (code = gs_setcachedevice(penum, igs, wbox[0], wbox[1], wbox[2], wbox[3], wbox[4], wbox[5])) < 0 )
  228.         return code;
  229.     pop(npop);
  230.     return 0;
  231. }
  232.  
  233. /* setcharwidth */
  234. int
  235. zsetcharwidth(register os_ptr op)
  236. {    float width[2];
  237.     gs_show_enum *penum = find_show();
  238.     int code = num_params(op, 2, width);
  239.     if ( penum == 0 ) return e_undefined;
  240.     if (    code < 0 || 
  241.         (code = gs_setcharwidth(penum, igs, width[0], width[1])) < 0
  242.        )
  243.         return code;
  244.     pop(2);
  245.     return 0;
  246. }
  247.  
  248. /* setmetrics */
  249. int
  250. zsetmetrics(register os_ptr op)
  251. {    float params[4];
  252.     gs_point sb, w;
  253.     gs_show_enum *penum = find_show();
  254.     int code, size;
  255.     if ( penum == 0 ) return e_undefined;
  256.     switch ( r_type(op) )
  257.        {
  258.     case t_array:
  259.         switch ( (size = r_size(op)) )
  260.            {
  261.         case 2: case 4: break;
  262.         default: return e_invalidfont;
  263.            }
  264.         code = num_params(op->value.refs + size - 1, size, params);
  265.         if ( code < 0 ) return code;
  266.         sb.x = params[0];
  267.         if ( size == 4 )
  268.             sb.y = params[1], w.x = params[2], w.y = params[3];
  269.         else
  270.             sb.y = 0, w.x = params[1], w.y = 0;
  271.         code = gs_setmetrics(penum, igs, &sb, &w);
  272.         break;
  273.     default:
  274.         code = real_param(op, params);
  275.         if ( code < 0 ) return code;
  276.         w.x = params[0];
  277.         w.y = 0;
  278.         code = gs_setmetrics(penum, igs, 0, &w);
  279.        }
  280.     if ( code >= 0 ) pop(1);
  281.     return code;
  282. }
  283.  
  284. /* type1addpath */
  285. typedef struct {
  286.     gs_font *pfont;
  287.     fixed *osptr;            /* fake interpreter operand stack */
  288.     fixed ostack[2];
  289. } z1_data;
  290. int
  291. ztype1addpath(register os_ptr op)
  292. {    int code, value;
  293.     gs_show_enum *penum = find_show();
  294.     gs_font *pfont = gs_currentfont(igs);
  295.     font_data *pfdata = (font_data *)pfont->client_data;
  296.     gs_type1_state is;        /* stack allocate to avoid sandbars */
  297.     gs_type1_state *pis = &is;
  298.     fixed discard;
  299.     gs_fixed_point spt, ept;
  300.     int flex_path_was_open;
  301.     gs_type1_data tdata;
  302.     z1_data zdata;
  303.     const byte *charstring = 0;
  304.     ref enc_entry;
  305.     if ( penum == 0 ) return e_undefined;
  306.     check_type(*op, t_string);
  307.     tdata = pfont->data.base.type1_data;
  308.     zdata.pfont = pfont;
  309.     zdata.osptr = zdata.ostack;
  310.     tdata.proc_data = (char *)&zdata;
  311.     if ( r_size(op) <= tdata.lenIV )
  312.        {    /* String is empty, or too short.  Just ignore it. */
  313.         pop(1);
  314.         return 0;
  315.        }
  316.     code = gs_type1_init(pis, penum,
  317.                  gs_show_in_charpath(penum), tdata.PaintType,
  318.                  &tdata);
  319.     if ( code < 0 ) return code;
  320.     charstring = op->value.const_bytes;
  321. more:    code = gs_type1_interpret(pis, charstring, &value);
  322.     charstring = 0;
  323.     switch ( code )
  324.        {
  325.     case type1_result_seac:
  326.        {    ref *pstdenc, *pcstr;
  327.         if ( dict_find(&systemdict,
  328.                    &name_StandardEncoding, &pstdenc) <= 0 )
  329.             return e_undefined;
  330.         code = array_get(pstdenc, (long)value, &enc_entry);
  331.         if ( code < 0 ) return code;
  332.         if ( dict_find(&pfdata->CharStrings,
  333.                    &enc_entry, &pcstr) <= 0 )
  334.             return e_undefined;
  335.         if ( !r_has_type(pcstr, t_string) )
  336.             return e_invalidfont;
  337.         charstring = pcstr->value.const_bytes;
  338.        }    goto more;
  339.     case type1_result_callothersubr:
  340.        {    /* We aren't prepared to call the interpreter here, */
  341.         /* so we fake the Flex feature. */
  342.         gx_path *ppath = igs->path;
  343.         gs_type1_pop(pis, &discard);    /* pop # of args */
  344.         switch ( value )
  345.            {
  346.         case 0:
  347.             /* We have to do something really sleazy here, */
  348.             /* namely, make it look as though the rmovetos */
  349.             /* never really happened, because we don't want */
  350.             /* to interrupt the current subpath. */
  351.             gx_path_current_point(ppath, &ept);
  352.             gx_path_add_point(ppath, spt.x, spt.y);
  353.             ppath->subpath_open = flex_path_was_open;
  354.                     /* ^--- sleaze */
  355.             gx_path_add_line(ppath, ept.x, ept.y);
  356.             /* Transfer endpoint coordinates to 'ostack' */
  357.             gs_type1_pop(pis, &zdata.ostack[0]);
  358.             gs_type1_pop(pis, &zdata.ostack[1]);
  359.             gs_type1_pop(pis, &discard);
  360.             zdata.osptr = &zdata.ostack[2];
  361.             goto more;
  362.         case 1:
  363.             gx_path_current_point(ppath, &spt);
  364.             flex_path_was_open = ppath->subpath_open;
  365.                     /* ^--- more sleaze */
  366.             goto more;
  367.         case 2:
  368.             goto more;
  369.         case 3:
  370.             gs_type1_pop(pis, &zdata.ostack[0]); /* pop subr# */
  371.             zdata.osptr = &zdata.ostack[1];
  372.             reset_stem_hints(pis);
  373.             goto more;
  374.            }
  375.         /* Unrecognized othersubr */
  376.         code = e_rangecheck;
  377.        }    break;
  378.        }
  379.     if ( code >= 0 ) pop(1);
  380.     return code;
  381. }
  382.  
  383. /* ------ Auxiliary procedures for type 1 fonts ------ */
  384.  
  385. int
  386. z1_subr_proc(gs_type1_data *pdata, int index, const byte **pstr)
  387. {    gs_font *pfont = ((z1_data *)(pdata->proc_data))->pfont;
  388.     font_data *pfdata = (font_data *)(pfont->client_data);
  389.     ref *psubr;
  390.     if ( index < 0 || index >= r_size(&pfdata->Subrs) )
  391.         return e_rangecheck;
  392.     psubr = pfdata->Subrs.value.refs + index;
  393.     check_type(*psubr, t_string);
  394.     *pstr = psubr->value.bytes;
  395.     return 0;
  396. }
  397.  
  398. int
  399. z1_pop_proc(gs_type1_data *pdata, fixed *pf)
  400. {    *pf = *--(((z1_data *)(pdata->proc_data))->osptr);
  401.     return 0;
  402. }
  403.  
  404. /* ------ Initialization procedure ------ */
  405.  
  406. op_def zchar_op_defs[] = {
  407.     {"3ashow", zashow},
  408.     {"6awidthshow", zawidthshow},
  409.     {"2charpath", zcharpath},
  410.     {"2kshow", zkshow},
  411.     {"3setcachedevice", zsetcachedevice},
  412.     {"2setcharwidth", zsetcharwidth},
  413.     {"1.setmetrics", zsetmetrics},
  414.     {"1show", zshow},
  415.     {"1stringwidth", zstringwidth},
  416.     {"1type1addpath", ztype1addpath},
  417.     {"4widthshow", zwidthshow},
  418.         /* Internal operators */
  419.     {"0%finish_show", finish_show, &i_finish_show},
  420.     {"0%finish_stringwidth", finish_stringwidth, &i_finish_stringwidth},
  421.     {"0%show_continue", show_continue, &i_show_continue},
  422.     op_def_end(0)
  423. };
  424.  
  425. /* ------ Internal routines ------ */
  426.  
  427. /* Set up for a show operator. */
  428. /* The top stack element must be the string to be scanned. */
  429. /* The caller has already done all other argument checking. */
  430. private int
  431. setup_show(ref *op, op_proc_p endproc /* end procedure */, int proc_index)
  432. {    gs_show_enum *penum;
  433.     check_read_type(*op, t_string);
  434.     check_estack(snumpush + 2);
  435.     if ( (penum = (gs_show_enum *)alloc(1, gs_show_enum_sizeof, "setup_show")) == 0 )
  436.         return e_VMerror;
  437.     mark_estack(es_show);
  438.     push_op_estack(endproc, proc_index);
  439.     ++esp;
  440.     make_tv(esp, t_null, index, 0);        /* reserve slot */
  441.     ++esp;
  442.     make_tasv(esp, t_string, 0, gs_show_enum_sizeof, bytes, (byte *)penum);
  443.     return o_push_estack;
  444. }
  445.  
  446. /* Continuation operator for character rendering. */
  447. private int
  448. show_continue(register os_ptr op)
  449. {    gs_show_enum *penum = senum;
  450.     int code = gs_show_next(penum);
  451.     switch ( code )
  452.        {
  453.     case 0:                /* all done */
  454.         code = (*real_opproc(&seproc))(op);
  455.         free_show();
  456.         return (code >= 0 ? o_pop_estack : code);
  457.     case gs_show_kern:
  458.        {    ref *pslot = &sslot;
  459.         push(2);
  460.         make_int(op - 1, gs_kshow_previous_char(penum));
  461.         make_int(op, gs_kshow_next_char(penum));
  462.         push_op_estack(show_continue, i_show_continue);        /* continue after kerning */
  463.         *++esp = *pslot;    /* kerning procedure */
  464.        }
  465.         return o_push_estack;
  466.     case gs_show_render:
  467.        {    font_data *pfont = (font_data *)gs_currentfont(igs)->client_data;
  468.         push(2);
  469.         op[-1] = pfont->dict;    /* push the font */
  470.         make_int(op, gs_show_current_char(penum));
  471.         push_op_estack(show_continue, i_show_continue);
  472.         *++esp = pfont->BuildChar;
  473.        }
  474.         return o_push_estack;
  475.     default:            /* error */
  476.         free_show();
  477.         return code;
  478.        }
  479. }
  480.  
  481. /* Find the current show enumerator on the e-stack. */
  482. private gs_show_enum *
  483. find_show()
  484. {    es_ptr ep = esp;
  485.     while ( !(r_has_type(ep, t_null) && ep->value.index == es_show) )
  486.        {    if ( --ep < esbot ) return 0;    /* no mark */
  487.        }
  488.     return (gs_show_enum *)ep[snumpush - 1].value.bytes;
  489. }
  490.  
  491. /* Discard the show record (after an error, or at the end). */
  492. private void
  493. free_show()
  494. {    alloc_free((char *)senum, 1, gs_show_enum_sizeof, "free_show");
  495.     esp -= snumpush;
  496. }
  497.