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

  1. /* Copyright (C) 1989, 1990 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. /* gscoord.c */
  21. /* Coordinate system operators for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzdevice.h"            /* requires gsstate */
  29. #include "gscoord.h"            /* requires gsmatrix, gsstate */
  30.  
  31. /* Forward declarations */
  32. #ifdef DEBUG
  33. private void trace_ctm(P1(gs_state *));
  34. private void trace_matrix(P1(gs_matrix *));
  35. #endif
  36.  
  37. /* Macro for ensuring ctm_inverse is valid */
  38. #ifdef DEBUG
  39. #define print_inverse(pgs)\
  40. if ( gs_debug['x'] )\
  41.     dprintf("[x]Inverting:\n"), trace_ctm(pgs), trace_matrix(&pgs->ctm_inverse)
  42. #else
  43. #define print_inverse(pgs) 0
  44. #endif
  45. #define ensure_inverse_valid(pgs)\
  46.     if ( !pgs->inverse_valid )\
  47.        {    int code = gs_matrix_invert(&ctm_only(pgs), &pgs->ctm_inverse);\
  48.         print_inverse(pgs);\
  49.         if ( code < 0 ) return code;\
  50.         pgs->inverse_valid = 1;\
  51.        }
  52.  
  53. /* Macro for updating fixed version of ctm */
  54. #define update_ctm(pgs)\
  55.     update_matrix_fixed(pgs->ctm),\
  56.     pgs->inverse_valid = 0,\
  57.     pgs->char_tm_valid = 0
  58.  
  59. /* ------ Coordinate system definition ------ */
  60.  
  61. int
  62. gs_initmatrix(gs_state *pgs)
  63. {    gx_device *dev = pgs->device->info;
  64.     (*dev->procs->get_initial_matrix)(dev, &ctm_only(pgs));
  65.     update_ctm(pgs);
  66. #ifdef DEBUG
  67. if ( gs_debug['x'] )
  68.     dprintf("[x]initmatrix:\n"), trace_ctm(pgs);
  69. #endif
  70.     return 0;
  71. }
  72.  
  73. int
  74. gs_defaultmatrix(gs_state *pgs, gs_matrix *pmat)
  75. {    gx_device *dev = pgs->device->info;
  76.     (*dev->procs->get_initial_matrix)(dev, pmat);
  77.     return 0;
  78. }
  79.  
  80. int
  81. gs_currentmatrix(gs_state *pgs, gs_matrix *pmat)
  82. {    *pmat = ctm_only(pgs);
  83.     return 0;
  84. }
  85.  
  86. int
  87. gs_setmatrix(gs_state *pgs, gs_matrix *pmat)
  88. {    ctm_only(pgs) = *pmat;
  89.     update_ctm(pgs);
  90. #ifdef DEBUG
  91. if ( gs_debug['x'] )
  92.     dprintf("[x]setmatrix:\n"), trace_ctm(pgs);
  93. #endif
  94.     return 0;
  95. }
  96.  
  97. int
  98. gs_translate(gs_state *pgs, floatp dx, floatp dy)
  99. {    gs_point pt;
  100.     int code;
  101.     if ( (code = gs_distance_transform(dx, dy, &ctm_only(pgs), &pt)) < 0 )
  102.         return code;
  103.     pgs->ctm.tx += pt.x;
  104.     pgs->ctm.ty += pt.y;
  105.     update_ctm(pgs);
  106. #ifdef DEBUG
  107. if ( gs_debug['x'] )
  108.     dprintf4("[x]translate: %f %f -> %f %f\n",
  109.          dx, dy, pt.x, pt.y),
  110.     trace_ctm(pgs);
  111. #endif
  112.     return 0;
  113. }
  114.  
  115. int
  116. gs_scale(gs_state *pgs, floatp sx, floatp sy)
  117. {    pgs->ctm.xx *= sx;
  118.     pgs->ctm.xy *= sx;
  119.     pgs->ctm.yx *= sy;
  120.     pgs->ctm.yy *= sy;
  121.     pgs->inverse_valid = 0, pgs->char_tm_valid = 0;
  122. #ifdef DEBUG
  123. if ( gs_debug['x'] )
  124.     dprintf2("[x]scale: %f %f\n", sx, sy), trace_ctm(pgs);
  125. #endif
  126.     return 0;
  127. }
  128.  
  129. int
  130. gs_rotate(gs_state *pgs, floatp ang)
  131. {    int code = gs_matrix_rotate(&ctm_only(pgs), ang, &ctm_only(pgs));
  132.     pgs->inverse_valid = 0, pgs->char_tm_valid = 0;
  133. #ifdef DEBUG
  134. if ( gs_debug['x'] )
  135.     dprintf1("[x]rotate: %f\n", ang), trace_ctm(pgs);
  136. #endif
  137.     return code;
  138. }
  139.  
  140. int
  141. gs_concat(gs_state *pgs, gs_matrix *pmat)
  142. {    int code = gs_matrix_multiply(pmat, &ctm_only(pgs), &ctm_only(pgs));
  143.     update_ctm(pgs);
  144. #ifdef DEBUG
  145. if ( gs_debug['x'] )
  146.     dprintf("[x]concat:\n"), trace_matrix(pmat), trace_ctm(pgs);
  147. #endif
  148.     return code;
  149. }
  150.  
  151. /* ------ Coordinate transformation ------ */
  152.  
  153. int
  154. gs_transform(gs_state *pgs, floatp x, floatp y, gs_point *pt)
  155. {    return gs_point_transform(x, y, &ctm_only(pgs), pt);
  156. }
  157.  
  158. int
  159. gs_dtransform(gs_state *pgs, floatp dx, floatp dy, gs_point *pt)
  160. {    return gs_distance_transform(dx, dy, &ctm_only(pgs), pt);
  161. }
  162.  
  163. int
  164. gs_itransform(gs_state *pgs, floatp x, floatp y, gs_point *pt)
  165. {    ensure_inverse_valid(pgs);
  166.     return gs_point_transform((float)x, (float)y, &pgs->ctm_inverse, pt);
  167. }
  168.  
  169. int
  170. gs_idtransform(gs_state *pgs, floatp dx, floatp dy, gs_point *pt)
  171. {    ensure_inverse_valid(pgs);
  172.     return gs_distance_transform((float)dx, (float)dy, &pgs->ctm_inverse, pt);
  173. }
  174.  
  175. /* ------ For internal use only ------ */
  176.  
  177. /* Set the translation to a fixed value, */
  178. /* and mark char_tm as valid. */
  179. /* Used by gschar.c to prepare for a BuildChar procedure. */
  180. int
  181. gs_translate_to_fixed(register gs_state *pgs, fixed px, fixed py)
  182. {    pgs->ctm.tx = fixed2float(pgs->ctm.tx_fixed = px);
  183.     pgs->ctm.ty = fixed2float(pgs->ctm.ty_fixed = py);
  184.     pgs->inverse_valid = 0;
  185.     pgs->char_tm_valid = 1;
  186.     return 0;
  187. }
  188.  
  189. /* ------ Debugging printout ------ */
  190.  
  191. #ifdef DEBUG
  192.  
  193. /* Print a matrix */
  194. private void
  195. trace_ctm(gs_state *pgs)
  196. {    gs_matrix_fixed *pmat = &pgs->ctm;
  197.     trace_matrix((gs_matrix *)pmat);
  198.     dprintf2("\t\tt_fixed: [%6g %6g]\n",
  199.          fixed2float(pmat->tx_fixed), fixed2float(pmat->ty_fixed));
  200. }
  201. private void
  202. trace_matrix(register gs_matrix *pmat)
  203. {    dprintf6("\t[%6g %6g %6g %6g %6g %6g]\n",
  204.          pmat->xx, pmat->xy, pmat->yx, pmat->yy, pmat->tx, pmat->ty);
  205. }
  206.  
  207. #endif
  208.