home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Utilities / Fiend-1.4.1-src / UserPath.m < prev   
Encoding:
Text File  |  1994-10-20  |  9.8 KB  |  377 lines

  1. /*
  2.  * UserPath.m by Bruce Blumberg, NeXT Computer, Inc.
  3.  *
  4.  * You may freely copy,distribute and re-use the code in this example. NeXT
  5.  * disclaims any warranty of any kind, expressed or implied, as to its fitness
  6.  * for any particular purpose
  7.  *
  8.  */
  9.  
  10. #import "UserPath.h"
  11. #import <mach/mach_init.h>
  12. #import <appkit/graphics.h>
  13. #import <appkit/errors.h>
  14. #import <math.h>
  15. #import <libc.h>
  16.  
  17. static NXZone      *upZone = NULL;
  18.  
  19. NXZone *userPathZone()
  20. /* Creates a unique zone for use by all user paths */
  21. {
  22.     if (!upZone) {
  23.         upZone = NXCreateZone(vm_page_size, vm_page_size, 1);
  24.     }
  25.  
  26.     return upZone;
  27. }
  28.  
  29. UserPath *newUserPath()
  30. /* Creates a new User Path in the zone returned by userPathZone */
  31. {
  32.     UserPath    *up;
  33.  
  34.     up = (UserPath *)NXZoneMalloc(userPathZone(), sizeof(UserPath));
  35.     up->max = 32;
  36.     up->points = (float *)NXZoneMalloc(userPathZone(),
  37.                            sizeof(float) * up->max);
  38.     up->ops = (char *)NXZoneMalloc(userPathZone(),
  39.                        (2 + (up->max / 2)) * sizeof(char));
  40.     up->ping = NO;
  41.  
  42.     return up;
  43. }
  44.  
  45. void freeUserPath(UserPath *up)
  46. /* Frees User Path and its associated buffers */
  47. {
  48.     free(up->points);
  49.     free(up->ops);
  50.     free(up);
  51.  
  52.     return;
  53. }
  54.  
  55. void growUserPath(UserPath *up)
  56. /*
  57.  * grows the  associated buffers as necessary. buffer size doubles on each
  58.  * call. You never need to call grow directly as it is called as needed by the
  59.  * methods and functions which add elements into the buffer
  60.  */
  61. {
  62.  /* double the size of the internal buffers */
  63.     up->max *= 2;
  64.     up->points = (float *)NXZoneRealloc(userPathZone(), up->points,
  65.                     sizeof(float) * up->max);
  66.     up->ops = (char *)NXZoneRealloc(userPathZone(), up->ops,
  67.                         (2 + (up->max / 2)) * sizeof(char));
  68.  
  69.     return;
  70. }
  71.  
  72. void beginUserPath(UserPath *up, BOOL cache)
  73. /*
  74.  * Call this to start generating a user path. The cache argument specifies if
  75.  * you want the user path cached at the server (i.e. dps_ucache). In either
  76.  * case, the UserPath object will automatically calculate the bounding box for
  77.  * the path and add the dps_setbbox operator.
  78.  */
  79. {
  80.     up->numberOfPoints = up->numberOfOps = 0;
  81.     up->cp.x = up->cp.y = 0;
  82.     up->bbox[0] = up->bbox[1] = 1.0e6;
  83.     up->bbox[2] = up->bbox[3] = -1.0e6;
  84.     if (cache) {
  85.         up->ops[up->numberOfOps++] = dps_ucache;
  86.     }
  87.     up->ops[up->numberOfOps++] = dps_setbbox;
  88.     up->opForUserPath = 0;
  89.  
  90.     return;
  91. }
  92.  
  93. void endUserPath(UserPath *up, int op)
  94. /*
  95.  * Call this to stop filling the path. Note this does not send the userpath to
  96.  * the server -- use sendUserPath. The op argument should be one of the
  97.  * following:
  98.  *    dps_uappend, dps_ufill ,dps_ueofill, dps_ustroke, dps_ustrokepath,
  99.  *    dps_inufill, dps_inueofill, dps_inustroke, dps_def, dps_put.
  100.  * These are defined in <dpsclient/dpsNext.h.
  101.  */
  102. {
  103.     up->opForUserPath = op;
  104.     return;
  105. }
  106.  
  107.  
  108. void UPdebug(UserPath *up, BOOL shouldPing)
  109. /*
  110.  * Sets ping to YES so that after each time a user path is sent down to the
  111.  * window server, an NXPing() is sent after. The purpose is to catch PostScript
  112.  * errors that may be generated by the user path. sendUserPath brackets the
  113.  * download and the NXPing() in an NX_DURING... NX_HANDLER construct. Normally
  114.  * ping is NO.
  115.  */
  116. {
  117.     up->ping = shouldPing;
  118.  
  119.     return;
  120. }
  121.  
  122. int sendUserPath(UserPath *up)
  123. /*
  124.  * Call this to send the path down to the server. If ping==YES (set via
  125.  * debug:), the function will send an NXPing() after the Path. In any event,
  126.  * code is bracketed by a NX_DURING ... NX_HANDLER construct which will try to
  127.  * catch postscript errors.  If ping==NO (the default) it is unlikely to catch
  128.  * errors, with ping==YES it will. Whether you can recover or not is another
  129.  * matter. sendUserPath returns 0 on success and -1 on failure. If no previous
  130.  * endUserPath: has been sent, will return -2 and will not send the path to the
  131.  * server.
  132.  */
  133. {
  134.     NXHandler           exception;
  135.  
  136.     exception.code = 0;
  137.     if (up->opForUserPath != 0) {
  138.         NX_DURING
  139.             DPSDoUserPath(up->points, up->numberOfPoints, dps_float, up->ops,
  140.                           up->numberOfOps, up->bbox, up->opForUserPath);
  141.             if (up->ping)
  142.                 NXPing();
  143.         NX_HANDLER
  144.             exception = NXLocalHandler;
  145.         NX_ENDHANDLER
  146.  
  147.         if (exception.code) {
  148.             NXReportError(&exception);
  149.             if (exception.code == dps_err_ps) {
  150.                 return -1;
  151.             }
  152.         } else {
  153.             return 0;
  154.         }
  155.     }
  156.  
  157.     return -1;
  158. }
  159.  
  160. void checkBoundingBox(UserPath *up, float x, float y)
  161. /* Checks if bounding box needs to be enlarged based on x and y */
  162. {
  163.     if (x < up->bbox[0]) {
  164.         up->bbox[0] = x;
  165.     }
  166.     if (y < up->bbox[1]) {
  167.         up->bbox[1] = y;
  168.     }
  169.     if (x > up->bbox[2]) {
  170.         up->bbox[2] = x;
  171.     }
  172.     if (y > up->bbox[3]) {
  173.         up->bbox[3] = y;
  174.     }
  175.  
  176.     return;
  177. }
  178.  
  179. void addPts(UserPath *up, float x, float y)
  180. /* adds x and y to user path. Updates bounding box as necessary */
  181. {
  182.     if (!((up->numberOfPoints + 2) < up->max)) {
  183.         growUserPath(up);
  184.     }
  185.  
  186.     up->points[up->numberOfPoints++] = x;
  187.     up->points[up->numberOfPoints++] = y;
  188.     checkBoundingBox(up, x, y);
  189.  
  190.     return;
  191. }
  192.  
  193. void addOp(UserPath *up, int op)
  194. /*
  195.  * adds operator to user path.  Operator should be one of the following:
  196.  *     dps_moveto, dps_rmoveto, dps_lineto, dps_rlineto, dps_curveto,
  197.  *    dps_rcurveto, dps_arc, dps_arcn, dps_arct, dps_closepath.
  198.  */
  199. {
  200.     up->ops[up->numberOfOps++] = op;
  201.     return;
  202. }
  203.  
  204. void add(UserPath *up, int op, float x, float y)
  205. /*
  206.  * adds operator and x and y to user path. Operator should be one of the
  207.  * operators above
  208.  */
  209. {
  210.     if (!((up->numberOfPoints + 2) < up->max)) {
  211.         growUserPath(up);
  212.     }
  213.  
  214.     up->ops[up->numberOfOps++] = op;
  215.     up->points[up->numberOfPoints++] = x;
  216.     up->points[up->numberOfPoints++] = y;
  217.     checkBoundingBox(up, x, y);
  218.  
  219.     return;
  220. }
  221.  
  222. void UPmoveto(UserPath *up, float x, float y)
  223. /* adds <x y moveto> to user path and updates bounding box */
  224. {
  225.     add(up, dps_moveto, x, y);
  226.     up->cp.x = x;
  227.     up->cp.y = y;
  228.  
  229.     return;
  230. }
  231.  
  232. void UPrmoveto(UserPath *up, float x, float y)
  233. /* adds <x y rmoveto> to user path and updates bounding box */
  234. {
  235.     if (!((up->numberOfPoints + 2) < up->max)) {
  236.         growUserPath(up);
  237.     }
  238.     up->ops[up->numberOfOps++] = dps_rmoveto;
  239.     up->points[up->numberOfPoints++] = x;
  240.     up->points[up->numberOfPoints++] = y;
  241.     up->cp.x += x;
  242.     up->cp.y += y;
  243.     checkBoundingBox(up, up->cp.x, up->cp.y);
  244.  
  245.     return;
  246. }
  247.  
  248. void UPlineto(UserPath *up, float x, float y)
  249. /* adds <x y lineto> to user path and updates bounding box */
  250. {
  251.     add(up, dps_lineto, x, y);
  252.     up->cp.x = x;
  253.     up->cp.y = y;
  254.  
  255.     return;
  256. }
  257.  
  258. void UPrlineto(UserPath *up, float x, float y)
  259. /* adds <x y rlineto> to user path and updates bounding box */
  260. {
  261.     if (!((up->numberOfPoints + 2) < up->max)) {
  262.         growUserPath(up);
  263.     }
  264.     up->ops[up->numberOfOps++] = dps_rlineto;
  265.     up->points[up->numberOfPoints++] = x;
  266.     up->points[up->numberOfPoints++] = y;
  267.     up->cp.x += x;
  268.     up->cp.y += y;
  269.     checkBoundingBox(up, up->cp.x, up->cp.y);
  270.  
  271.     return;
  272. }
  273.  
  274. void UPcurveto(UserPath *up, float x1, float y1, float x2, float y2, float x3,
  275.            float y3)
  276. /* adds <x1 y1 x2 y2 curveto> to user path and updates bounding box */
  277. {
  278.     addPts(up, x1, y1);
  279.     addPts(up, x2, y2);
  280.     add(up, dps_curveto, x3, y3);
  281.     up->cp.x = x3;
  282.     up->cp.y = y3;
  283.  
  284.     return;
  285. }
  286.  
  287. void UPrcurveto(UserPath *up, float dx1, float dy1, float dx2, float dy2,
  288.         float dx3, float dy3)
  289. /* adds <x1 y1 x2 y2 rcurveto> to user path and updates bounding box */
  290. {
  291.     if (!((up->numberOfPoints + 6) < up->max)) {
  292.         growUserPath(up);
  293.     }
  294.     up->ops[up->numberOfOps++] = dps_rcurveto;
  295.     up->points[up->numberOfPoints++] = dx1;
  296.     up->points[up->numberOfPoints++] = dy1;
  297.     up->points[up->numberOfPoints++] = dx2;
  298.     up->points[up->numberOfPoints++] = dy2;
  299.     up->points[up->numberOfPoints++] = dx3;
  300.     up->points[up->numberOfPoints++] = dy3;
  301.     checkBoundingBox(up, up->cp.x + dx1, up->cp.y + dy1);
  302.     checkBoundingBox(up, up->cp.x + dx2, up->cp.y + dy2);
  303.     checkBoundingBox(up, up->cp.x + dx3, up->cp.y + dy3);
  304.     up->cp.x = dx3;
  305.     up->cp.y = dy3;
  306.  
  307.     return;
  308. }
  309.  
  310. void UParc(UserPath *up, float x, float y, float r, float ang1, float ang2)
  311. /* adds <x y r ang1 ang2 arc> to user path and updates bounding box */
  312. {
  313.     if (!((up->numberOfPoints + 5) < up->max)) {
  314.         growUserPath(up);
  315.     }
  316.     up->ops[up->numberOfOps++] = dps_arc;
  317.     up->points[up->numberOfPoints++] = x;
  318.     up->points[up->numberOfPoints++] = y;
  319.     up->points[up->numberOfPoints++] = r;
  320.     up->points[up->numberOfPoints++] = ang1;
  321.     up->points[up->numberOfPoints++] = ang2;
  322.     checkBoundingBox(up, x + r, y + r);
  323.     checkBoundingBox(up, x - r, y - r);
  324.     up->cp.x = x + cos(ang2 / 57.3) * r;
  325.     up->cp.y = y + sin(ang2 / 57.3) * r;
  326.  
  327.     return;
  328. }
  329.  
  330. void UParcn(UserPath *up, float x, float y, float r, float ang1, float ang2)
  331. /* adds <x y r ang1 ang2 arcn> to user path and updates bounding box */
  332. {
  333.     if (!((up->numberOfPoints + 5) < up->max)) {
  334.         growUserPath(up);
  335.     }
  336.     up->ops[up->numberOfOps++] = dps_arcn;
  337.     up->points[up->numberOfPoints++] = x;
  338.     up->points[up->numberOfPoints++] = y;
  339.     up->points[up->numberOfPoints++] = r;
  340.     up->points[up->numberOfPoints++] = ang1;
  341.     up->points[up->numberOfPoints++] = ang2;
  342.     checkBoundingBox(up, x + r, y + r);
  343.     checkBoundingBox(up, x - r, y - r);
  344.     up->cp.x = x + cos(ang2 / 57.3) * r;
  345.     up->cp.y = y + sin(ang2 / 57.3) * r;
  346.  
  347.     return;
  348. }
  349.  
  350. void UParct(UserPath *up, float x1, float y1, float x2, float y2, float r)
  351. /* adds <x1 y1 x2 y2 r arct> to user path and updates bounding box */
  352. {
  353.     if (!((up->numberOfPoints + 5) < up->max)) {
  354.         growUserPath(up);
  355.     }
  356.     up->ops[up->numberOfOps++] = dps_arcn;
  357.     up->points[up->numberOfPoints++] = x1;
  358.     up->points[up->numberOfPoints++] = y1;
  359.     up->points[up->numberOfPoints++] = x2;
  360.     up->points[up->numberOfPoints++] = y2;
  361.     up->points[up->numberOfPoints++] = r;
  362.     checkBoundingBox(up, x1, y1);
  363.     checkBoundingBox(up, x2, y2);
  364.     up->cp.x = x2;
  365.     up->cp.y = y2;
  366.  
  367.     return;
  368. }
  369.  
  370. void closePath(UserPath *up)
  371. /* adds <closepath> to user path and updates bounding box */
  372. {
  373.     up->ops[up->numberOfOps++] = dps_closepath;
  374.  
  375.     return;
  376. }
  377.