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

  1. /* Copyright (C) 1990, 1992, 1993 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. /* zupath.c */
  20. /* Operators related to user paths */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "idict.h"
  25. #include "dstack.h"
  26. #include "igstate.h"
  27. #include "iutil.h"
  28. #include "store.h"
  29. #include "stream.h"
  30. #include "ibnum.h"
  31. #include "gsmatrix.h"
  32. #include "gsstate.h"
  33. #include "gscoord.h"
  34. #include "gspaint.h"
  35. #include "gxfixed.h"
  36. #include "gxdevice.h"
  37. #include "gxpath.h"
  38.  
  39. /* Forward references */
  40. private int upath_append(P1(os_ptr));
  41. private int upath_stroke(P1(os_ptr));
  42.  
  43. /* ------ Insideness testing ------ */
  44.  
  45. /* Forward references */
  46. private int in_test(P2(os_ptr, int (*)(P1(gs_state *))));
  47. private int in_path(P1(os_ptr));
  48. private int in_path_result(P3(os_ptr, int, int));
  49. private int in_utest(P2(os_ptr, int (*)(P1(gs_state *))));
  50. private int in_upath(P1(os_ptr));
  51. private int in_upath_result(P3(os_ptr, int, int));
  52.  
  53. /* We use invalidexit, which the painting procedures cannot generate, */
  54. /* as an "error" to indicate that the hit detection device found a hit. */
  55. #define e_hit e_invalidexit
  56.  
  57. /* <x> <y> ineofill <bool> */
  58. /* <userpath> ineofill <bool> */
  59. int
  60. zineofill(os_ptr op)
  61. {    return in_test(op, gs_eofill);
  62. }
  63.  
  64. /* <x> <y> infill <bool> */
  65. /* <userpath> infill <bool> */
  66. int
  67. zinfill(os_ptr op)
  68. {    return in_test(op, gs_fill);
  69. }
  70.  
  71. /* <x> <y> instroke <bool> */
  72. /* <userpath> instroke <bool> */
  73. int
  74. zinstroke(os_ptr op)
  75. {    return in_test(op, gs_stroke);
  76. }
  77.  
  78. /* <x> <y> <userpath> inueofill <bool> */
  79. /* <userpath1> <userpath2> inueofill <bool> */
  80. int
  81. zinueofill(os_ptr op)
  82. {    return in_utest(op, gs_eofill);
  83. }
  84.  
  85. /* <x> <y> <userpath> inufill <bool> */
  86. /* <userpath1> <userpath2> inufill <bool> */
  87. int
  88. zinufill(os_ptr op)
  89. {    return in_utest(op, gs_fill);
  90. }
  91.  
  92. /* <x> <y> <userpath> inustroke <bool> */
  93. /* <userpath1> <userpath2> inustroke <bool> */
  94. int
  95. zinustroke(os_ptr op)
  96. {    /* This is different because of the optional matrix operand. */
  97.     int code = gs_gsave(igs);
  98.     int spop, npop;
  99.     if ( code < 0 ) return code;
  100.     if ( (spop = upath_stroke(op)) < 0 ||
  101.          (npop = in_path(op - spop)) < 0
  102.        )
  103.        {    gs_grestore(igs);
  104.         return code;
  105.        }
  106.     code = gs_stroke(igs);
  107.     return in_upath_result(op, npop + spop, code);
  108. }
  109.  
  110. /* ------ Internal routines ------ */
  111.  
  112. /* Define a minimal device for insideness testing. */
  113. /* It returns e_hit whenever it is asked to actually paint any pixels. */
  114. private dev_proc_fill_rectangle(hit_fill_rectangle);
  115. private gx_device hit_device =
  116. {    sizeof(gx_device),
  117.     0,            /* &...dev.std_procs */
  118.     "hit detector",
  119.     0, 0, 1, 1, no_margins, dci_black_and_white, dev_init_misc,
  120.     {    NULL,            /* open_device */
  121.         NULL,            /* get_initial_matrix */
  122.         NULL,            /* sync_output */
  123.         NULL,            /* output_page */
  124.         NULL,            /* close_device */
  125.         gx_default_map_rgb_color,
  126.         gx_default_map_color_rgb,
  127.         hit_fill_rectangle,
  128.         NULL,            /* tile_rectangle */
  129.         NULL,            /* copy_mono */
  130.         NULL,            /* copy_color */
  131.         gx_default_draw_line
  132.     }
  133. };
  134. /* Test for a hit when filling a rectangle. */
  135. private int
  136. hit_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  137.   gx_color_index color)
  138. {    return (w > 0 && h > 0 ? e_hit : 0);
  139. }
  140.  
  141. /* Do the work of the non-user-path insideness operators. */
  142. private int
  143. in_test(os_ptr op, int (*paintproc)(P1(gs_state *)))
  144. {    int npop = in_path(op);
  145.     int code;
  146.     if ( npop < 0 ) return npop;
  147.     code = (*paintproc)(igs);
  148.     return in_path_result(op, npop, code);
  149. }
  150.  
  151. /* Set up a clipping path and device for insideness testing. */
  152. private int
  153. in_path(os_ptr op)
  154. {    int code = gs_gsave(igs);
  155.     int npop;
  156.     float uxy[2];
  157.     if ( code < 0 ) return code;
  158.     code = num_params(op, 2, uxy);
  159.     if ( code >= 0 )
  160.        {    /* Aperture is a single pixel. */
  161.         gs_point dxy;
  162.         gs_fixed_rect fr;
  163.         gs_transform(igs, uxy[0], uxy[1], &dxy);
  164.         fr.p.x = fixed_floor(float2fixed(dxy.x));
  165.         fr.p.y = fixed_floor(float2fixed(dxy.y));
  166.         fr.q.x = fr.p.x + fixed_1;
  167.         fr.q.y = fr.p.y + fixed_1;
  168.         code = gx_clip_to_rectangle(igs, &fr);
  169.         npop = 2;
  170.        }
  171.     else
  172.        {    /* Aperture is a user path. */
  173.         if ( (code = upath_append(op)) >= 0 )
  174.             code = gx_clip_to_path(igs);
  175.         npop = 1;
  176.        }
  177.     if ( code < 0 )
  178.        {    gs_grestore(igs);
  179.         return code;
  180.        }
  181.     /* Install the hit detection device. */
  182.     gs_setgray(igs, 0.0);
  183.     hit_device.procs = &hit_device.std_procs;
  184.     gx_set_device_only(igs, &hit_device);
  185.     return npop;
  186. }
  187.  
  188. /* Finish an insideness test. */
  189. private int
  190. in_path_result(os_ptr op, int npop, int code)
  191. {    int result;
  192.     gs_grestore(igs);
  193.     switch ( code )
  194.        {
  195.     case e_hit:            /* found a hit */
  196.         result = 1;
  197.         break;
  198.     case 0:                /* completed painting without a hit */
  199.         result = 0;
  200.         break;
  201.     default:            /* error */
  202.         return code;
  203.        }
  204.     npop--;
  205.     pop(npop); op -= npop;
  206.     make_bool(op, result);
  207.     return 0;
  208.         
  209. }
  210.  
  211. /* Do the work of the user-path insideness operators. */
  212. private int
  213. in_utest(os_ptr op, int (*paintproc)(P1(gs_state *)))
  214. {    int npop = in_upath(op);
  215.     int code;
  216.     if ( npop < 0 ) return npop;
  217.     code = (*paintproc)(igs);
  218.     return in_upath_result(op, npop, code);
  219. }
  220.  
  221. /* Set up a clipping path and device for insideness testing */
  222. /* with a user path. */
  223. private int
  224. in_upath(os_ptr op)
  225. {    int code = gs_gsave(igs);
  226.     int npop;
  227.     if ( code < 0 ) return code;
  228.     if ( (code = upath_append(op)) < 0 ||
  229.          (npop = in_path(op - 1)) < 0
  230.        )
  231.        {    gs_grestore(igs);
  232.         return code;
  233.        }
  234.     return npop + 1;
  235. }
  236.  
  237. /* Finish an insideness test with a user path. */
  238. private int
  239. in_upath_result(os_ptr op, int npop, int code)
  240. {    gs_grestore(igs);        /* undo the extra gsave */
  241.     return in_path_result(op, npop, code);
  242. }
  243.  
  244. /* ------ User paths ------ */
  245.  
  246. /* User path operator codes */
  247. typedef enum {
  248.   upath_setbbox = 0,
  249.   upath_moveto = 1,
  250.   upath_rmoveto = 2,
  251.   upath_lineto = 3,
  252.   upath_rlineto = 4,
  253.   upath_curveto = 5,
  254.   upath_rcurveto = 6,
  255.   upath_arc = 7,
  256.   upath_arcn = 8,
  257.   upath_arct = 9,
  258.   upath_closepath = 10,
  259.   upath_ucache = 11
  260. } upath_op;
  261. #define upath_op_max 11
  262. #define upath_repeat 32
  263. static byte up_nargs[upath_op_max + 1] =
  264.    { 4, 2, 2, 2, 2, 6, 6, 5, 5, 5, 0, 0 };
  265. #define zp(proc) extern int proc(P1(os_ptr))
  266.     zp(zsetbbox); zp(zmoveto); zp(zrmoveto);
  267.     zp(zlineto); zp(zrlineto); zp(zcurveto); zp(zrcurveto);
  268.     zp(zarc); zp(zarcn); zp(zarct); zp(zclosepath); zp(zucache);
  269. #undef zp
  270. static op_proc_p up_ops[upath_op_max + 1] =
  271.    {    zsetbbox, zmoveto, zrmoveto, zlineto, zrlineto,
  272.     zcurveto, zrcurveto, zarc, zarcn, zarct,
  273.     zclosepath, zucache
  274.    };
  275.  
  276. /* - ucache - */
  277. int
  278. zucache(os_ptr op)
  279. {    /* A no-op for now. */
  280.     return 0;
  281. }
  282.  
  283. /* <userpath> uappend - */
  284. int
  285. zuappend(register os_ptr op)
  286. {    int code = gs_gsave(igs);
  287.     if ( code < 0 ) return code;
  288.     if ( (code = upath_append(op)) >= 0 )
  289.         code = gs_upmergepath(igs);
  290.     gs_grestore(igs);
  291.     if ( code < 0 ) return code;
  292.     pop(1);
  293.     return 0;
  294. }
  295.  
  296. /* <userpath> ueofill - */
  297. int
  298. zueofill(register os_ptr op)
  299. {    int code = gs_gsave(igs);
  300.     if ( code < 0 ) return code;
  301.     if ( (code = upath_append(op)) >= 0 )
  302.         code = gs_eofill(igs);
  303.     gs_grestore(igs);
  304.     if ( code < 0 ) return code;
  305.     pop(1);
  306.     return 0;
  307. }
  308.  
  309. /* <userpath> ufill - */
  310. int
  311. zufill(register os_ptr op)
  312. {    int code = gs_gsave(igs);
  313.     if ( code < 0 ) return code;
  314.     if ( (code = upath_append(op)) >= 0 )
  315.         code = gs_fill(igs);
  316.     gs_grestore(igs);
  317.     if ( code < 0 ) return code;
  318.     pop(1);
  319.     return 0;
  320. }
  321.  
  322. /* <userpath> ustroke - */
  323. /* <userpath> <matrix> ustroke - */
  324. int
  325. zustroke(register os_ptr op)
  326. {    int code = gs_gsave(igs);
  327.     int npop;
  328.     if ( code < 0 ) return code;
  329.     if ( (code = npop = upath_stroke(op)) >= 0 )
  330.         code = gs_stroke(igs);
  331.     gs_grestore(igs);
  332.     if ( code < 0 ) return code;
  333.     pop(npop);
  334.     return 0;
  335. }
  336.  
  337. /* <userpath> ustrokepath - */
  338. /* <userpath> <matrix> ustrokepath - */
  339. int
  340. zustrokepath(register os_ptr op)
  341. {    int code = gs_gsave(igs);
  342.     int npop;
  343.     if ( code < 0 ) return code;
  344.     if ( (code = npop = upath_stroke(op)) < 0 ||
  345.          (code = gs_strokepath(igs)) < 0 ||
  346.          (code = gs_upmergepath(igs)) < 0
  347.        )
  348.         DO_NOTHING;
  349.     gs_grestore(igs);
  350.     if ( code < 0 ) return code;
  351.     pop(npop);
  352.     return 0;
  353. }
  354.  
  355. /* --- Internal routines --- */
  356.  
  357. /* Append a user path to the current path. */
  358. private int
  359. upath_append(os_ptr op)
  360. {    check_read(*op);
  361.     gs_newpath(igs);
  362.     /****** ROUND tx AND ty ******/
  363.     if ( r_has_type(op, t_array) && r_size(op) == 2 &&
  364.          r_has_type(op->value.refs + 1, t_string)
  365.        )
  366.     {    /* 1st element is operators, 2nd is operands */
  367.         stream st;
  368.         int code, format;
  369.         int repcount = 1;
  370.         const byte *opp;
  371.         uint ocount;
  372.         code = sread_num_array(&st, op->value.refs);
  373.         if ( code < 0 ) return code;
  374.         format = code;
  375.         opp = op->value.refs[1].value.bytes;
  376.         ocount = r_size(&op->value.refs[1]);
  377.         while ( ocount-- )
  378.            {    byte opx = *opp++;
  379.             if ( opx > 32 )
  380.                 repcount = opx - 32;
  381.             else if ( opx > upath_op_max )
  382.                 return_error(e_typecheck);
  383.             else        /* operator */
  384.                {    do
  385.                    {    byte opargs = up_nargs[opx];
  386.                     while ( opargs-- )
  387.                        {    push(1);
  388.                         code = sget_encoded_number(&st, format, op);
  389.                         switch ( code )
  390.                            {
  391.                         case t_integer:
  392.                             r_set_type_attrs(op, t_integer, 0);
  393.                             break;
  394.                         case t_real:
  395.                             r_set_type_attrs(op, t_real, 0);
  396.                             break;
  397.                         default:
  398.                             return_error(e_typecheck);
  399.                            }
  400.                        }
  401.                     code = (*up_ops[opx])(op);
  402.                     if ( code < 0 ) return code;
  403.                     op = osp;    /* resync */
  404.                    }
  405.                 while ( --repcount );
  406.                 repcount = 1;
  407.                }
  408.            }
  409.     }
  410.     else
  411.     {    /* Ordinary executable array */
  412.         const ref *arp = op;
  413.         ref *defp;
  414.         ref rup;
  415.         uint ocount = r_size(op);
  416.         long index = 0;
  417.         int argcount = 0;
  418.         int (*oproc)(P1(os_ptr));
  419.         int opx, code;
  420.         for ( ; index < ocount; index++ )
  421.          switch ( array_get(arp, index, &rup), r_type(&rup) )
  422.            {
  423.         case t_integer:
  424.         case t_real:
  425.             argcount++;
  426.             push(1);
  427.             *op = rup;
  428.             break;
  429.         case t_name:
  430.             if ( !r_has_attr(&rup, a_executable) )
  431.                 return_error(e_typecheck);
  432.             if ( dict_find(systemdict, &rup, &defp) <= 0 )
  433.                 return_error(e_undefined);
  434.             if ( r_btype(defp) != t_operator )
  435.                 return_error(e_typecheck);
  436.             goto xop;
  437.         case t_operator:
  438.             defp = &rup;
  439. xop:            if ( !r_has_attr(defp, a_executable) )
  440.                 return_error(e_typecheck);
  441.             oproc = real_opproc(defp);
  442.             for ( opx = 0; opx <= upath_op_max; opx++ )
  443.                 if ( oproc == up_ops[opx] ) break;
  444.             if ( opx > upath_op_max || argcount != up_nargs[opx] )
  445.                 return_error(e_typecheck);
  446.             code = (*oproc)(op);
  447.             if ( code < 0 ) return code;
  448.             op = osp;    /* resync ostack pointer */
  449.             argcount = 0;
  450.             break;
  451.         default:
  452.             return_error(e_typecheck);
  453.            }
  454.         if ( argcount )
  455.             return_error(e_typecheck);    /* leftover args */
  456.     }
  457.     return 0;
  458. }
  459.  
  460. /* Append a user path to the current path, and then apply */
  461. /* a transformation if one is supplied. */
  462. private int
  463. upath_stroke(register os_ptr op)
  464. {    int code, npop;
  465.     gs_matrix mat;
  466.     if ( (code = read_matrix(op, &mat)) >= 0 )
  467.        {    if ( (code = upath_append(op - 1)) >= 0 )
  468.             code = gs_concat(igs, &mat);
  469.         npop = 2;
  470.        }
  471.     else
  472.        {    code = upath_append(op);
  473.         npop = 1;
  474.        }
  475.     return (code < 0 ? code : npop);
  476. }
  477.  
  478. /* ------ Initialization procedure ------ */
  479.  
  480. op_def zupath_l2_op_defs[] = {
  481.         op_def_begin_level2(),
  482.         /* Insideness testing */
  483.     {"1ineofill", zineofill},
  484.     {"1infill", zinfill},
  485.     {"1instroke", zinstroke},
  486.     {"2inueofill", zinueofill},
  487.     {"2inufill", zinufill},
  488.     {"2inustroke", zinustroke},
  489.         /* User paths */
  490.     {"1uappend", zuappend},
  491.     {"1ueofill", zueofill},
  492.     {"1ufill", zufill},
  493.     {"1ustroke", zustroke},
  494.     {"1ustrokepath", zustrokepath},
  495.     {"0ucache", zucache},
  496.     op_def_end(0)
  497. };
  498.