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

  1. /* Copyright (C) 1991, 1992, 1994 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. /* gsdps1.c */
  20. /* Display PostScript graphics additions for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"            /* for gscoord.h */
  25. #include "gscoord.h"
  26. #include "gspaint.h"
  27. #include "gxfixed.h"
  28. #include "gxmatrix.h"
  29. #include "gspath2.h"            /* defines interface */
  30. #include "gzpath.h"
  31. #include "gzstate.h"
  32.  
  33. /* ------ Graphics state ------ */
  34.  
  35. /* Set the bounding box for the current path. */
  36. int
  37. gs_setbbox(gs_state *pgs, floatp llx, floatp lly, floatp urx, floatp ury)
  38. {    gs_rect ubox, dbox;
  39.     gs_fixed_rect obox, bbox;
  40.     gx_path *ppath = pgs->path;
  41.     int code;
  42.     if ( llx > urx || lly > ury )
  43.         return_error(gs_error_rangecheck);
  44.     /* Transform box to device coordinates. */
  45.     ubox.p.x = llx;
  46.     ubox.p.y = lly;
  47.     ubox.q.x = urx;
  48.     ubox.q.y = ury;
  49.     if ( (code = gs_bbox_transform(&ubox, &ctm_only(pgs), &dbox)) < 0 )
  50.         return code;
  51.     /* Round the corners in opposite directions. */
  52.     bbox.p.x = (fixed)floor(dbox.p.x * _fixed_scale);
  53.     bbox.p.y = (fixed)floor(dbox.p.y * _fixed_scale);
  54.     bbox.q.x = (fixed)ceil(dbox.q.x * _fixed_scale);
  55.     bbox.q.y = (fixed)ceil(dbox.q.y * _fixed_scale);
  56.     if ( gx_path_bbox(ppath, &obox) >= 0 )
  57.     {    /* Take the union of the bboxes. */
  58.         ppath->bbox.p.x = min(obox.p.x, bbox.p.x);
  59.         ppath->bbox.p.y = min(obox.p.y, bbox.p.y);
  60.         ppath->bbox.q.x = max(obox.q.x, bbox.q.x);
  61.         ppath->bbox.q.y = max(obox.q.y, bbox.q.y);
  62.     }
  63.     else        /* empty path */
  64.     {    /* Just set the bbox. */
  65.         ppath->bbox.p.x = bbox.p.x;
  66.         ppath->bbox.p.y = bbox.p.y;
  67.         ppath->bbox.q.x = bbox.q.x;
  68.         ppath->bbox.q.y = bbox.q.y;
  69.         ppath->bbox_set = 1;
  70.     }
  71.     return 0;
  72. }
  73.  
  74. /* ------ Rectangles ------ */
  75.  
  76. /* Append a list of rectangles to a path. */
  77. int
  78. gs_rectappend(gs_state *pgs, const gs_rect *pr, uint count)
  79. {    for ( ; count != 0; count--, pr++ )
  80.        {    floatp px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
  81.         int code;
  82.         /* Ensure counter-clockwise drawing. */
  83.         if ( (qx >= px) != (qy >= py) )
  84.             qx = px, px = pr->q.x;    /* swap x values */
  85.         if ( (code = gs_moveto(pgs, px, py)) < 0 ||
  86.              (code = gs_lineto(pgs, qx, py)) < 0 ||
  87.              (code = gs_lineto(pgs, qx, qy)) < 0 ||
  88.              (code = gs_lineto(pgs, px, qy)) < 0 ||
  89.              (code = gs_closepath(pgs)) < 0
  90.            )
  91.             return code;
  92.        }
  93.     return 0;
  94. }
  95.  
  96. /* Clip to a list of rectangles. */
  97. int
  98. gs_rectclip(gs_state *pgs, const gs_rect *pr, uint count)
  99. {    int code;
  100.     gx_path old_path;
  101.     old_path = *pgs->path;
  102.     gx_path_reset(pgs->path);
  103.     if ( (code = gs_rectappend(pgs, pr, count)) < 0 ||
  104.          (code = gs_clip(pgs)) < 0
  105.        )
  106.       {    gx_path_release(pgs->path);
  107.         *pgs->path = old_path;
  108.         return code;
  109.       }
  110.     gs_newpath(pgs);
  111.     gx_path_release(&old_path);
  112.     return 0;
  113. }
  114.  
  115. /* Fill a list of rectangles. */
  116. /* (We could do this a lot more efficiently.) */
  117. int
  118. gs_rectfill(gs_state *pgs, const gs_rect *pr, uint count)
  119. {    int code;
  120.     if ( (code = gs_gsave(pgs)) < 0 ) return code;
  121.     if ( (code = gs_newpath(pgs)) < 0 ||
  122.          (code = gs_rectappend(pgs, pr, count)) < 0 ||
  123.          (code = gs_fill(pgs)) < 0
  124.        )
  125.       DO_NOTHING;
  126.     gs_grestore(pgs);
  127.     return code;
  128. }
  129.  
  130. /* Stroke a list of rectangles. */
  131. /* (We could do this a lot more efficiently.) */
  132. int
  133. gs_rectstroke(gs_state *pgs, const gs_rect *pr, uint count,
  134.   const gs_matrix *pmat)
  135. {    int code;
  136.     if ( (code = gs_gsave(pgs)) < 0 ) return code;
  137.     if ( (code = gs_newpath(pgs)) < 0 ||
  138.          (code = gs_rectappend(pgs, pr, count)) < 0 ||
  139.          (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
  140.          (code = gs_stroke(pgs)) < 0
  141.        )
  142.       DO_NOTHING;
  143.     gs_grestore(pgs);
  144.     return code;
  145. }
  146.