home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / zmatrix < prev    next >
Encoding:
Text File  |  1991-10-25  |  6.2 KB  |  243 lines

  1. /* Copyright (C) 1989, 1991 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. /* zmatrix.c */
  21. /* Matrix operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gsmatrix.h"
  26. #include "state.h"
  27. #include "gscoord.h"
  28. #include "store.h"
  29.  
  30. /* Forward references */
  31. private int common_transform(P3(os_ptr,
  32.   int (*)(P4(gs_state *, floatp, floatp, gs_point *)),
  33.   int (*)(P4(floatp, floatp, gs_matrix *, gs_point *))));
  34.  
  35. /* Initialize the type and attributes of the identity matrix */
  36. private void
  37. zmatrix_init()
  38. {    extern gs_matrix gs_identity_matrix;
  39.     ref *mp = (ref *)&gs_identity_matrix;
  40.     int i;
  41.     for ( i = 0; i < 6; i++, mp++ )
  42.         r_set_type_attrs(mp, t_real, l_new);
  43. }
  44.  
  45. /* currentmatrix */
  46. int
  47. zcurrentmatrix(register os_ptr op)
  48. {    int code = write_matrix(op);
  49.     if ( code < 0 ) return code;
  50.     gs_currentmatrix(igs, (gs_matrix *)(op->value.refs));
  51.     return 0;
  52. }
  53.  
  54. /* setmatrix */
  55. int
  56. zsetmatrix(register os_ptr op)
  57. {    gs_matrix mat;
  58.     int code = read_matrix(op, &mat);
  59.     if ( code < 0 ) return code;
  60.     if ( (code = gs_setmatrix(igs, &mat)) < 0 ) return code;
  61.     pop(1);
  62.     return 0;
  63. }
  64.  
  65. /* translate */
  66. int
  67. ztranslate(register os_ptr op)
  68. {    int code = write_matrix(op);
  69.     float trans[2];
  70.     if ( code < 0 )            /* no matrix operand */
  71.        {    if ( (code = num_params(op, 2, trans)) < 0 ) return code;
  72.         code = gs_translate(igs, trans[0], trans[1]);
  73.        }
  74.     else                /* matrix operand */
  75.        {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  76.         if ( (code = num_params(op - 1, 2, trans)) < 0 ) return code;
  77.         code = gs_make_translation(trans[0], trans[1], pmat);
  78.         op[-2] = *op;
  79.        }
  80.     if ( code >= 0 ) pop(2);
  81.     return code;
  82. }
  83.  
  84. /* scale */
  85. int
  86. zscale(register os_ptr op)
  87. {    float scale[2];
  88.     int code = write_matrix(op);
  89.     if ( code < 0 )            /* no matrix operand */
  90.        {    if ( (code = num_params(op, 2, scale)) < 0 ) return code;
  91.         code = gs_scale(igs, scale[0], scale[1]);
  92.        }
  93.     else                /* matrix operand */
  94.        {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  95.         if ( (code = num_params(op - 1, 2, scale)) < 0 ) return code;
  96.         code = gs_make_scaling(scale[0], scale[1], pmat);
  97.         op[-2] = *op;
  98.        }
  99.     if ( code >= 0 ) pop(2);
  100.     return code;
  101. }
  102.  
  103. /* rotate */
  104. int
  105. zrotate(register os_ptr op)
  106. {    int code = write_matrix(op);
  107.     float ang;
  108.     if ( code < 0 )            /* no matrix operand */
  109.        {    if ( (code = num_params(op, 1, &ang)) < 0 ) return code;
  110.         code = gs_rotate(igs, ang);
  111.        }
  112.     else                /* matrix operand */
  113.        {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  114.         if ( (code = num_params(op - 1, 1, &ang)) < 0 ) return code;
  115.         code = gs_make_rotation(ang, pmat);
  116.         op[-1] = *op;
  117.        }
  118.     if ( code >= 0 ) pop(1);
  119.     return code;
  120. }
  121.  
  122. /* concat */
  123. int
  124. zconcat(register os_ptr op)
  125. {    gs_matrix mat;
  126.     int code = read_matrix(op, &mat);
  127.     if ( code < 0 ) return code;
  128.     code = gs_concat(igs, &mat);
  129.     if ( code < 0 ) return code;
  130.     pop(1);
  131.     return 0;
  132. }
  133.  
  134. /* concatmatrix */
  135. int
  136. zconcatmatrix(register os_ptr op)
  137. {    gs_matrix m1, m2;
  138.     int code;
  139.     if (    (code = read_matrix(op - 2, &m1)) < 0 ||
  140.         (code = read_matrix(op - 1, &m2)) < 0 ||
  141.         (code = write_matrix(op)) < 0 ||
  142.         (code = gs_matrix_multiply(&m1, &m2, (gs_matrix *)(op->value.refs))) < 0
  143.        ) return code;
  144.     op[-2] = *op;
  145.     pop(2);
  146.     return code;
  147. }
  148.  
  149. /* transform */
  150. int
  151. ztransform(register os_ptr op)
  152. {    return common_transform(op, gs_transform, gs_point_transform);
  153. }
  154.  
  155. /* dtransform */
  156. int
  157. zdtransform(register os_ptr op)
  158. {    return common_transform(op, gs_dtransform, gs_distance_transform);
  159. }
  160.  
  161. /* itransform */
  162. int
  163. zitransform(register os_ptr op)
  164. {    return common_transform(op, gs_itransform, gs_point_transform_inverse);
  165. }
  166.  
  167. /* idtransform */
  168. int
  169. zidtransform(register os_ptr op)
  170. {    return common_transform(op, gs_idtransform, gs_distance_transform_inverse);
  171. }
  172.  
  173. /* Common logic for [i][d]transform */
  174. private int
  175. common_transform(register os_ptr op,
  176.   int (*ptproc)(P4(gs_state *, floatp, floatp, gs_point *)),
  177.   int (*matproc)(P4(floatp, floatp, gs_matrix *, gs_point *)))
  178. {    float opxy[2];
  179.     gs_point pt;
  180.     int code;
  181.     /* Optimize for the non-matrix case */
  182.     switch ( r_type(op) )
  183.        {
  184.     case t_real: opxy[1] = op->value.realval; break;
  185.     case t_integer: opxy[1] = op->value.intval; break;
  186.     case t_array:                /* might be a matrix */
  187.        {    gs_matrix mat;
  188.         gs_matrix *pmat = &mat;
  189.         if (    (code = read_matrix(op, pmat)) < 0 ||
  190.             (code = num_params(op - 1, 2, opxy)) < 0 ||
  191.             (code = (*matproc)(opxy[0], opxy[1], pmat, &pt)) < 0
  192.            ) return code;
  193.         op--;
  194.         pop(1);
  195.         goto out;
  196.        }
  197.     default: return e_typecheck;
  198.        }
  199.     switch ( r_type(op - 1) )
  200.        {
  201.     case t_real: opxy[0] = (op - 1)->value.realval; break;
  202.     case t_integer: opxy[0] = (op - 1)->value.intval; break;
  203.     default: return e_typecheck;
  204.        }
  205.     if ( (code = (*ptproc)(igs, opxy[0], opxy[1], &pt)) < 0 )
  206.         return code;
  207. out:    make_real(op - 1, pt.x);
  208.     make_real(op, pt.y);
  209.     return 0;
  210. }
  211.  
  212. /* invertmatrix */
  213. int
  214. zinvertmatrix(register os_ptr op)
  215. {    gs_matrix m;
  216.     int code;
  217.     if (    (code = read_matrix(op - 1, &m)) < 0 ||
  218.         (code = write_matrix(op)) < 0 ||
  219.         (code = gs_matrix_invert(&m, (gs_matrix *)op->value.refs)) < 0
  220.        ) return code;
  221.     op[-1] = *op;
  222.     pop(1);
  223.     return code;
  224. }
  225.  
  226. /* ------ Initialization procedure ------ */
  227.  
  228. op_def zmatrix_op_defs[] = {
  229.     {"1concat", zconcat},
  230.     {"2dtransform", zdtransform},
  231.     {"3concatmatrix", zconcatmatrix},
  232.     {"1currentmatrix", zcurrentmatrix},
  233.     {"2idtransform", zidtransform},
  234.     {"2invertmatrix", zinvertmatrix},
  235.     {"2itransform", zitransform},
  236.     {"1rotate", zrotate},
  237.     {"2scale", zscale},
  238.     {"1setmatrix", zsetmatrix},
  239.     {"2transform", ztransform},
  240.     {"2translate", ztranslate},
  241.     op_def_end(zmatrix_init)
  242. };
  243.