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

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