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 / ZGSTATE.C < prev    next >
C/C++ Source or Header  |  1992-07-26  |  8KB  |  308 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. /* zgstate.c */
  21. /* Graphics state operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "gsmatrix.h"
  27. #include "gsstate.h"
  28. #include "state.h"
  29. #include "store.h"
  30.  
  31. /* Forward references */
  32. private int near num_param(P2(os_ptr, int (*)(P2(gs_state *, floatp))));
  33. private int near line_param(P2(os_ptr, int *));
  34.  
  35. /* ------ Operations on the entire graphics state ------ */
  36.  
  37. /* The current graphics state */
  38. gs_state *igs;
  39. int_gstate istate;
  40.  
  41. /* "Client" procedures */
  42. private char/*void*/ *gs_istate_alloc(P1(const gs_memory_procs *mp));
  43. private int gs_istate_copy(P2(char/*void*/ *to, const char/*void*/ *from));
  44. private void gs_istate_free(P2(char/*void*/ *old, const gs_memory_procs *mp));
  45. private const gs_state_client_procs istate_procs = {
  46.     gs_istate_alloc,
  47.     gs_istate_copy,
  48.     gs_istate_free
  49. };
  50.  
  51. /* Initialize the graphics stack. */
  52. void
  53. gs_init()
  54. {    igs = gs_state_alloc(alloc, alloc_free);
  55.     make_null(&istate.screen_proc);
  56.     make_null(&istate.transfer_procs.red);
  57.     make_null(&istate.transfer_procs.green);
  58.     make_null(&istate.transfer_procs.blue);
  59.     make_null(&istate.transfer_procs.gray);
  60.     make_null(&istate.colorspace);
  61.     make_null(&istate.pattern);
  62.     make_null(&istate.colorrendering);
  63.     make_null(&istate.halftone);
  64.     gs_state_set_client(igs, (char/*void*/ *)&istate, &istate_procs);
  65.     /* gsave and grestore only work properly */
  66.     /* if there are always at least 2 entries on the stack. */
  67.     /* We count on the PostScript initialization code to do a gsave. */
  68. }
  69.  
  70. /* gsave */
  71. int
  72. zgsave(register os_ptr op)
  73. {    return gs_gsave(igs);
  74. }
  75.  
  76. /* grestore */
  77. int
  78. zgrestore(register os_ptr op)
  79. {    return gs_grestore(igs);
  80. }
  81.  
  82. /* grestoreall */
  83. int
  84. zgrestoreall(register os_ptr op)
  85. {    return gs_grestoreall(igs);
  86. }
  87.  
  88. /* initgraphics */
  89. int
  90. zinitgraphics(register os_ptr op)
  91. {    return gs_initgraphics(igs);
  92. }
  93.  
  94. /* ------ Operations on graphics state elements ------ */
  95.  
  96. /* setlinewidth */
  97. int
  98. zsetlinewidth(register os_ptr op)
  99. {    return num_param(op, gs_setlinewidth);
  100. }
  101.  
  102. /* currentlinewidth */
  103. int
  104. zcurrentlinewidth(register os_ptr op)
  105. {    push(1);
  106.     make_real(op, gs_currentlinewidth(igs));
  107.     return 0;
  108. }
  109.  
  110. /* setlinecap */
  111. int
  112. zsetlinecap(register os_ptr op)
  113. {    int param;
  114.     int code = line_param(op, ¶m);
  115.     if ( !code ) code = gs_setlinecap(igs, (gs_line_cap)param);
  116.     return code;
  117. }
  118.  
  119. /* currentlinecap */
  120. int
  121. zcurrentlinecap(register os_ptr op)
  122. {    push(1);
  123.     make_int(op, (int)gs_currentlinecap(igs));
  124.     return 0;
  125. }
  126.  
  127. /* setlinejoin */
  128. int
  129. zsetlinejoin(register os_ptr op)
  130. {    int param;
  131.     int code = line_param(op, ¶m);
  132.     if ( !code ) code = gs_setlinejoin(igs, (gs_line_join)param);
  133.     return code;
  134. }
  135.  
  136. /* currentlinejoin */
  137. int
  138. zcurrentlinejoin(register os_ptr op)
  139. {    push(1);
  140.     make_int(op, (int)gs_currentlinejoin(igs));
  141.     return 0;
  142. }
  143.  
  144. /* setmiterlimit */
  145. int
  146. zsetmiterlimit(register os_ptr op)
  147. {    return num_param(op, gs_setmiterlimit);
  148. }
  149.  
  150. /* currentmiterlimit */
  151. int
  152. zcurrentmiterlimit(register os_ptr op)
  153. {    push(1);
  154.     make_real(op, gs_currentmiterlimit(igs));
  155.     return 0;
  156. }
  157.  
  158. /* setdash */
  159. int
  160. zsetdash(register os_ptr op)
  161. {    float offset;
  162.     uint n, i;
  163.     const ref *dfrom;
  164.     float *pattern, *dto;
  165.     int code = real_param(op, &offset);
  166.     if ( code ) return code;
  167.     check_array(op[-1]);
  168.     check_read(op[-1]);
  169.     /* Unpack the dash pattern and check it */
  170.     dfrom = op[-1].value.const_refs;
  171.     i = n = r_size(op - 1);
  172.     pattern = dto = (float *)alloc(n, sizeof(float), "setdash");
  173.     while ( i-- )
  174.        {    switch ( r_type(dfrom) )
  175.            {
  176.         case t_integer:
  177.             *dto++ = dfrom->value.intval;
  178.             break;
  179.         case t_real:
  180.             *dto++ = dfrom->value.realval;
  181.             break;
  182.         default:
  183.             alloc_free((char *)dto, n, sizeof(float), "setdash");
  184.             return e_typecheck;
  185.            }
  186.         dfrom++;
  187.        }
  188.     code = gs_setdash(igs, pattern, n, offset);
  189.     if ( !code ) pop(2);
  190.     return code;
  191. }
  192.  
  193. /* currentdash */
  194. int
  195. zcurrentdash(register os_ptr op)
  196. {    int n = gs_currentdash_length(igs);
  197.     int i = n;
  198.     ref *pattern = alloc_refs(n, "currentdash");
  199.     ref *dto = pattern;
  200.     float *dfrom = (float *)((char *)pattern + n * (sizeof(ref) - sizeof(float)));
  201.     gs_currentdash_pattern(igs, dfrom);
  202.     while ( i-- )
  203.        {    make_real(dto, *dfrom);
  204.         dto++, dfrom++;
  205.        }
  206.     push(2);
  207.     make_tasv(op - 1, t_array, a_all, n, refs, pattern);
  208.     make_real(op, gs_currentdash_offset(igs));
  209.     return 0;
  210. }
  211.  
  212. /* setflat */
  213. int
  214. zsetflat(register os_ptr op)
  215. {    return num_param(op, gs_setflat);
  216. }
  217.  
  218. /* currentflat */
  219. int
  220. zcurrentflat(register os_ptr op)
  221. {    push(1);
  222.     make_real(op, gs_currentflat(igs));
  223.     return 0;
  224. }
  225.  
  226. /* setstrokeadjust */
  227. int
  228. zsetstrokeadjust(register os_ptr op)
  229. {    check_type(*op, t_boolean);
  230.     gs_setstrokeadjust(igs, op->value.index);
  231.     pop(1);
  232.     return 0;
  233. }
  234.  
  235. /* currentstrokeadjust */
  236. int
  237. zcurrentstrokeadjust(register os_ptr op)
  238. {    push(1);
  239.     make_bool(op, gs_currentstrokeadjust(igs));
  240.     return 0;
  241. }
  242.  
  243. /* ------ Initialization procedure ------ */
  244.  
  245. op_def zgstate_op_defs[] = {
  246.     {"0currentdash", zcurrentdash},
  247.     {"0currentflat", zcurrentflat},
  248.     {"0currentlinecap", zcurrentlinecap},
  249.     {"0currentlinejoin", zcurrentlinejoin},
  250.     {"0currentlinewidth", zcurrentlinewidth},
  251.     {"0currentmiterlimit", zcurrentmiterlimit},
  252.     {"0currentstrokeadjust", zcurrentstrokeadjust},
  253.     {"0grestore", zgrestore},
  254.     {"0grestoreall", zgrestoreall},
  255.     {"0gsave", zgsave},
  256.     {"0initgraphics", zinitgraphics},
  257.     {"2setdash", zsetdash},
  258.     {"1setflat", zsetflat},
  259.     {"1setlinecap", zsetlinecap},
  260.     {"1setlinejoin", zsetlinejoin},
  261.     {"1setlinewidth", zsetlinewidth},
  262.     {"1setmiterlimit", zsetmiterlimit},
  263.     {"1setstrokeadjust", zsetstrokeadjust},
  264.     op_def_end(0)
  265. };
  266.  
  267. /* ------ Internal routines ------ */
  268.  
  269. /* Allocate the interpreter's part of a graphics state. */
  270. private char/*void*/ *
  271. gs_istate_alloc(const gs_memory_procs *mp)
  272. {    return (char/*void*/ *)alloc_refs(sizeof(int_gstate) / sizeof(ref), "int_gsave");
  273. }
  274.  
  275. /* Copy the interpreter's part of a graphics state. */
  276. private int
  277. gs_istate_copy(char/*void*/ *to, const char/*void*/ *from)
  278. {    *(int_gstate *)to = *(int_gstate *)from;
  279.     return 0;
  280. }
  281.  
  282. /* Free the interpreter's part of a graphics state. */
  283. private void
  284. gs_istate_free(char/*void*/ *old, const gs_memory_procs *mp)
  285. {    alloc_free_refs((ref *)old, sizeof(int_gstate) / sizeof(ref), "int_grestore");
  286. }
  287.  
  288. /* Get a numeric parameter */
  289. private int near
  290. num_param(os_ptr op, int (*pproc)(P2(gs_state *, floatp)))
  291. {    float param;
  292.     int code = real_param(op, ¶m);
  293.     if ( !code ) code = (*pproc)(igs, param);
  294.     if ( !code ) pop(1);
  295.     return code;
  296. }
  297.  
  298. /* Get an integer parameter 0-2. */
  299. private int near
  300. line_param(register os_ptr op, int *pparam)
  301. {    check_type(*op, t_integer);
  302.     if ( op->value.intval < 0 || op->value.intval > 2 )
  303.         return e_rangecheck;
  304.     *pparam = (int)op->value.intval;
  305.     pop(1);
  306.     return 0;
  307. }
  308.