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

  1. /* Copyright (C) 1989, 1992 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. /* gsline.c */
  20. /* Line parameter operators for Ghostscript library */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"            /* ditto */
  26. #include "gxmatrix.h"            /* for gzstate */
  27. #include "gzstate.h"
  28. #include "gzline.h"
  29.  
  30. /* setlinewidth */
  31. int
  32. gs_setlinewidth(gs_state *pgs, floatp width)
  33. {    pgs->line_params->width = width / 2;
  34.     return 0;
  35. }
  36.  
  37. /* currentlinewidth */
  38. float
  39. gs_currentlinewidth(const gs_state *pgs)
  40. {    return (float)(pgs->line_params->width * 2);
  41. }
  42.  
  43. /* setlinecap */
  44. int
  45. gs_setlinecap(gs_state *pgs, gs_line_cap cap)
  46. {    pgs->line_params->cap = cap;
  47.     return 0;
  48. }
  49.  
  50. /* currentlinecap */
  51. gs_line_cap
  52. gs_currentlinecap(const gs_state *pgs)
  53. {    return pgs->line_params->cap;
  54. }
  55.  
  56. /* setlinejoin */
  57. int
  58. gs_setlinejoin(gs_state *pgs, gs_line_join join)
  59. {    pgs->line_params->join = join;
  60.     return 0;
  61. }
  62.  
  63. /* currentlinejoin */
  64. gs_line_join
  65. gs_currentlinejoin(const gs_state *pgs)
  66. {    return pgs->line_params->join;
  67. }
  68.  
  69. /* setmiterlimit */
  70. int
  71. gs_setmiterlimit(gs_state *pgs, floatp limit)
  72. {    if ( limit < 1.0 ) return_error(gs_error_rangecheck);
  73.     pgs->line_params->miter_limit = limit;
  74.     /* The supplied miter limit is an upper bound on */
  75.     /* 1/sin(phi/2).  We convert this to a lower bound on */
  76.     /* tan(phi).  Note that if phi > pi/2, this is negative. */
  77.     /* We use the half-angle and angle-sum formulas here */
  78.     /* to avoid the trig functions.... */
  79.        {    double limit_sq = limit * limit;
  80.     /* We need a special check for phi/2 close to pi/4. */
  81.     /* Some C compilers can't handle the following as a */
  82.     /* conditional expression.... */
  83.         if ( limit_sq < 2.0001 && limit_sq > 1.9999 )
  84.             pgs->line_params->miter_check = 1.0e6;
  85.         else
  86.             pgs->line_params->miter_check =
  87.                 sqrt(limit_sq - 1) * 2 / (limit_sq - 2);
  88.        }
  89.     return 0;
  90. }
  91.  
  92. /* currentmiterlimit */
  93. float
  94. gs_currentmiterlimit(const gs_state *pgs)
  95. {    return pgs->line_params->miter_limit;
  96. }
  97.  
  98. /* setdash */
  99. int
  100. gs_setdash(gs_state *pgs, const float *pattern, uint length, floatp offset)
  101. {    uint n = length;
  102.     const float *dfrom = pattern;
  103.     bool ink = true;
  104.     int index = 0;
  105.     float pattern_length = 0.0;
  106.     float dist_left;
  107.     gx_dash_params *dash = &pgs->line_params->dash;
  108.     float *ppat;
  109.     /* Check the dash pattern */
  110.     while ( n-- )
  111.     {    float elt = *dfrom++;
  112.         if ( elt < 0 )
  113.             return_error(gs_error_rangecheck);
  114.         pattern_length += elt;
  115.     }
  116.     if ( length == 0 )        /* empty pattern */
  117.     {    dist_left = 0.0;
  118.         ppat = 0;
  119.     }
  120.     else
  121.     {    if ( pattern_length == 0 )
  122.             return_error(gs_error_rangecheck);
  123.         /* Compute the initial index, ink_on, and distance left */
  124.         /* in the pattern, according to the offset. */
  125. #define f_mod(a, b) ((a) - floor((a) / (b)) * (b))
  126.         if ( length & 1 )
  127.         {    /* Odd and even repetitions of the pattern */
  128.             /* have opposite ink values! */
  129.             float length2 = pattern_length * 2;
  130.             dist_left = f_mod(offset, length2);
  131.             if ( dist_left >= pattern_length )
  132.                 dist_left -= pattern_length,
  133.                 ink = !ink;
  134.         }
  135.         else
  136.             dist_left = f_mod(offset, pattern_length);
  137.         while ( (dist_left -= pattern[index]) >= 0 )
  138.             ink = !ink, index++;
  139.         ppat = (float *)gs_alloc_bytes(pgs->memory,
  140.                            length * sizeof(float),
  141.                            "dash pattern");
  142.         if ( ppat == 0 ) return_error(gs_error_VMerror);
  143.         memcpy(ppat, pattern, length * sizeof(float));
  144.     }
  145.     dash->pattern = ppat;
  146.     dash->pattern_size = length;
  147.     dash->offset = offset;
  148.     dash->init_ink_on = ink;
  149.     dash->init_index = index;
  150.     dash->init_dist_left = -dist_left;
  151.     return 0;
  152. }
  153.  
  154. /* currentdash */
  155. uint
  156. gs_currentdash_length(const gs_state *pgs)
  157. {    return pgs->line_params->dash.pattern_size;
  158. }
  159. const float *
  160. gs_currentdash_pattern(const gs_state *pgs)
  161. {    return pgs->line_params->dash.pattern;
  162. }
  163. float
  164. gs_currentdash_offset(const gs_state *pgs)
  165. {    return pgs->line_params->dash.offset;
  166. }
  167.  
  168. /* Internal accessor for line parameters */
  169. const gx_line_params *
  170. gs_currentlineparams(const gs_state *pgs)
  171. {    return pgs->line_params;
  172. }
  173.