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 / GXPATH2.C < prev    next >
C/C++ Source or Header  |  1992-08-14  |  6KB  |  225 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. /* gxpath2.c */
  21. /* Path tracing procedures for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gzpath.h"
  28.  
  29. /* Read the current point of a path. */
  30. int
  31. gx_path_current_point(const gx_path *ppath, gs_fixed_point *ppt)
  32. {    if ( !ppath->position_valid )
  33.       return_error(gs_error_nocurrentpoint);
  34.     /* Copying the coordinates individually */
  35.     /* is much faster on a PC, and almost as fast on other machines.... */
  36.     ppt->x = ppath->position.x, ppt->y = ppath->position.y;
  37.     return 0;
  38. }
  39.  
  40. /* Read the bounding box of a path. */
  41. int
  42. gx_path_bbox(gx_path *ppath, gs_fixed_rect *pbox)
  43. {    if ( ppath->first_subpath == 0 )
  44.        {    /* The path is empty, use the current point if any. */
  45.         gx_path_current_point(ppath, &pbox->p);
  46.         return gx_path_current_point(ppath, &pbox->q);
  47.        }
  48.     /* The stored bounding box may not be up to date. */
  49.     /* Correct it now if necessary. */
  50.     if ( ppath->box_last == ppath->current_subpath->last )
  51.        {    /* Box is up to date */
  52.         *pbox = ppath->bbox;
  53.        }
  54.     else
  55.        {    gs_fixed_rect box;
  56.         register segment *pseg = ppath->box_last;
  57.         if ( pseg == 0 )    /* box is uninitialized */
  58.            {    pseg = (segment *)ppath->first_subpath;
  59.             box.p.x = box.q.x = pseg->pt.x;
  60.             box.p.y = box.q.y = pseg->pt.y;
  61.            }
  62.         else
  63.            {    box = ppath->bbox;
  64.             pseg = pseg->next;
  65.            }
  66. /* Macro for adjusting the bounding box when adding a point */
  67. #define adjust_bbox(pt)\
  68.   if ( (pt).x < box.p.x ) box.p.x = (pt).x;\
  69.   else if ( (pt).x > box.q.x ) box.q.x = (pt).x;\
  70.   if ( (pt).y < box.p.y ) box.p.y = (pt).y;\
  71.   else if ( (pt).y > box.q.y ) box.q.y = (pt).y
  72.         while ( pseg )
  73.            {    switch ( pseg->type )
  74.                {
  75.             case s_curve:
  76. #define pcurve ((curve_segment *)pseg)
  77.                 adjust_bbox(pcurve->p1);
  78.                 adjust_bbox(pcurve->p2);
  79. #undef pcurve
  80.                 /* falls through */
  81.             default:
  82.                 adjust_bbox(pseg->pt);
  83.                }
  84.             pseg = pseg->next;
  85.            }
  86. #undef adjust_bbox
  87.         ppath->bbox = box;
  88.         ppath->box_last = ppath->current_subpath->last;
  89.         *pbox = box;
  90.        }
  91.     return 0;
  92. }
  93.  
  94. /* Test if a path has any curves. */
  95. int
  96. gx_path_has_curves(const gx_path *ppath)
  97. {    return ppath->curve_count != 0;
  98. }
  99.  
  100. /* Test if a path has any segments. */
  101. int
  102. gx_path_is_void(const gx_path *ppath)
  103. {    return ppath->first_subpath == 0;
  104. }
  105.  
  106. /* Test if a path is a rectangle. */
  107. /* If so, return its bounding box. */
  108. /* Note that this must recognize open as well as closed rectangles. */
  109. int
  110. gx_path_is_rectangle(const gx_path *ppath, gs_fixed_rect *pbox)
  111. {    const subpath *pseg0;
  112.     const segment *pseg1, *pseg2, *pseg3, *pseg4;
  113.     if (    ppath->subpath_count == 1 &&
  114.         (pseg1 = (pseg0 = ppath->first_subpath)->next) != 0 &&
  115.         (pseg2 = pseg1->next) != 0 &&
  116.         (pseg3 = pseg2->next) != 0 &&
  117.         ((pseg4 = pseg3->next) == 0 || pseg4->type == s_line_close) &&
  118.         ppath->curve_count == 0
  119.        )
  120.        {    fixed x0 = pseg0->pt.x, y0 = pseg0->pt.y;
  121.         fixed x2 = pseg2->pt.x, y2 = pseg2->pt.y;
  122.         if (    (x0 == pseg1->pt.x && pseg1->pt.y == y2 &&
  123.              x2 == pseg3->pt.x && pseg3->pt.y == y0) ||
  124.             (x0 == pseg3->pt.x && pseg3->pt.y == y2 &&
  125.              x2 == pseg1->pt.x && pseg1->pt.y == y0)
  126.            )
  127.            {    /* Path is a rectangle.  Return bounding box. */
  128.             if ( x0 < x2 )
  129.                 pbox->p.x = x0, pbox->q.x = x2;
  130.             else
  131.                 pbox->p.x = x2, pbox->q.x = x0;
  132.             if ( y0 < y2 )
  133.                 pbox->p.y = y0, pbox->q.y = y2;
  134.             else
  135.                 pbox->p.y = y2, pbox->q.y = y0;
  136.             return 1;
  137.            }
  138.        }
  139.     return 0;
  140. }
  141.  
  142. /* Translate an already-constructed path (in device space). */
  143. /* Don't bother to translate the cbox. */
  144. int
  145. gx_path_translate(gx_path *ppath, fixed dx, fixed dy)
  146. {    segment *pseg;
  147. #define translate_xy(pt)\
  148.   pt.x += dx, pt.y += dy
  149.     translate_xy(ppath->bbox.p);
  150.     translate_xy(ppath->bbox.q);
  151.     translate_xy(ppath->position);
  152.     pseg = (segment *)(ppath->first_subpath);
  153.     while ( pseg )
  154.        {    switch ( pseg->type )
  155.            {
  156.         case s_curve:
  157.            {    curve_segment *pc = (curve_segment *)pseg;
  158.             translate_xy(pc->p1);
  159.             translate_xy(pc->p2);
  160.            }
  161.         default:
  162.             translate_xy(pseg->pt);
  163.            }
  164.         pseg = pseg->next;
  165.        }
  166.     return 0;
  167. }
  168.  
  169. /* Reverse a path. */
  170. /* We know ppath != ppath_old. */
  171. int
  172. gx_path_copy_reversed(const gx_path *ppath_old, gx_path *ppath, int init)
  173. {    const subpath *psub = ppath_old->first_subpath;
  174. #ifdef DEBUG
  175. if ( gs_debug['p'] )
  176.     gx_dump_path(ppath_old, "before reversepath");
  177. #endif
  178.     if ( init )
  179.         gx_path_init(ppath, &ppath_old->memory_procs);
  180. nsp:    while ( psub )
  181.        {    const segment *pseg = psub->last;
  182.         const segment *prev;
  183.         int code = gx_path_add_point(ppath, pseg->pt.x, pseg->pt.y);
  184.         if ( code < 0 )
  185.            {    gx_path_release(ppath);
  186.             return code;
  187.            }
  188.         for ( ; ; pseg = prev )
  189.            {    prev = pseg->prev;
  190.             switch ( pseg->type )
  191.                {
  192.             case s_start:
  193.                 /* Finished subpath */
  194.                 if ( psub->closed )
  195.                     code = gx_path_close_subpath(ppath);
  196.                 psub = (const subpath *)psub->last->next;
  197.                 goto nsp;
  198.             case s_curve:
  199.                {    const curve_segment *pc = (const curve_segment *)pseg;
  200.                 code = gx_path_add_curve(ppath,
  201.                     pc->p2.x, pc->p2.y,
  202.                     pc->p1.x, pc->p1.y,
  203.                     prev->pt.x, prev->pt.y);
  204.                 break;
  205.                }
  206.             case s_line:
  207.             case s_line_close:
  208.                 code = gx_path_add_line(ppath, prev->pt.x, prev->pt.y);
  209.                 break;
  210.                }
  211.             if ( code )
  212.                {    gx_path_release(ppath);
  213.                 return code;
  214.                }
  215.            }
  216.         /* not reached */
  217.     }
  218.     ppath->position = ppath_old->position;        /* restore current point */
  219. #ifdef DEBUG
  220. if ( gs_debug['p'] )
  221.     gx_dump_path(ppath, "after reversepath");
  222. #endif
  223.     return 0;
  224. }
  225.