home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xditv.patch / part01 next >
Text File  |  1990-08-27  |  6KB  |  251 lines

  1. Path: uunet!zephyr.ens.tek.com!uw-beaver!mit-eddie!snorkelwacker!usc!cs.utexas.edu!sun-barr!newstop!sun!yetti.cs.yorku.ca
  2. From: amana@yetti.cs.yorku.ca (John Amanatides)
  3. Newsgroups: comp.sources.x
  4. Subject: v08i101: pic support for xditview, Part01/01
  5. Message-ID: <141434@sun.Eng.Sun.COM>
  6. Date: 28 Aug 90 07:54:09 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 240
  9. Approved: argv@sun.com
  10.  
  11. Submitted-by: amana@yetti.cs.yorku.ca (John Amanatides)
  12. Posting-number: Volume 8, Issue 101
  13. Archive-name: xditv.patch/part01
  14.  
  15. [ Note: this is not an "official" patch to the program since it was
  16. never submitted to this newsgroup. --dan ]
  17.  
  18. The X11R4 release of xditview does not have support for pic commands.
  19. Below is the replacement for draw.c with the added support.
  20. Note: you have to include the math library (-lm) now.
  21.  
  22. -----------------------------------------------------------------------
  23. /*
  24.  * draw.c
  25.  *
  26.  * accept dvi function calls and translate to X
  27.  *
  28.  * added pic support, John Amanatides, August 1990
  29.  */
  30.  
  31. #include <X11/Xlib.h>
  32. #include <X11/Xos.h>
  33. #include <X11/IntrinsicP.h>
  34. #include <X11/StringDefs.h>
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include "DviP.h"
  38.  
  39. extern double sqrt(), atan2();
  40. extern long strtol();
  41.  
  42. HorizontalMove(dw, delta)
  43.     DviWidget    dw;
  44.     int        delta;
  45. {
  46.     dw->dvi.state->x += delta;
  47. }
  48.  
  49. HorizontalGoto(dw, NewPosition)
  50.     DviWidget    dw;
  51.     int        NewPosition;
  52. {
  53.     dw->dvi.state->x = NewPosition;
  54. }
  55.  
  56. VerticalMove(dw, delta)
  57.     DviWidget    dw;
  58.     int        delta;
  59. {
  60.     dw->dvi.state->y += delta;
  61. }
  62.  
  63. VerticalGoto(dw, NewPosition)
  64.     DviWidget    dw;
  65.     int        NewPosition;
  66. {
  67.     dw->dvi.state->y = NewPosition;
  68. }
  69.  
  70. FlushCharCache (dw)
  71.     DviWidget    dw;
  72. {
  73.     if (dw->dvi.cache.char_index != 0)
  74.         XDrawText (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
  75.             dw->dvi.cache.start_x, dw->dvi.cache.start_y,
  76.              dw->dvi.cache.cache, dw->dvi.cache.index + 1);
  77.     dw->dvi.cache.index = 0;
  78.     dw->dvi.cache.max = DVI_TEXT_CACHE_SIZE;
  79.     if (dw->dvi.noPolyText)
  80.         dw->dvi.cache.max = 1;
  81.     dw->dvi.cache.char_index = 0;
  82.     dw->dvi.cache.cache[0].nchars = 0;
  83.     dw->dvi.cache.start_x = dw->dvi.cache.x = dw->dvi.state->x;
  84.     dw->dvi.cache.start_y = dw->dvi.cache.y = dw->dvi.state->y;
  85. }
  86.  
  87. ClearPage (dw)
  88.     DviWidget    dw;
  89. {
  90.     XClearWindow (XtDisplay (dw), XtWindow (dw));
  91. }
  92.  
  93. static void setGC (dw)
  94.     DviWidget    dw;
  95. {
  96.     if (dw->dvi.state->line_style != dw->dvi.line_style ||
  97.         dw->dvi.state->line_width != dw->dvi.line_width)
  98.     {
  99.         XSetLineAttributes (XtDisplay (dw), dw->dvi.normal_GC,
  100.                     dw->dvi.state->line_width,
  101.                     LineSolid,
  102.                     CapButt,
  103.                     JoinMiter
  104.                     );
  105.         dw->dvi.line_style = dw->dvi.state->line_style;
  106.         dw->dvi.line_width = dw->dvi.state->line_width;
  107.     }
  108. }
  109.  
  110. DrawLine (dw, x, y)
  111.     DviWidget    dw;
  112.     int        x, y;
  113. {
  114.     x += dw->dvi.state->x;
  115.     y += dw->dvi.state->y;
  116.     XDrawLine (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
  117.         dw->dvi.state->x, dw->dvi.state->y, x, y);
  118.     dw->dvi.state->x = x;
  119.     dw->dvi.state->y = y;
  120. }
  121.  
  122. DrawCircle (dw, diameter)
  123.     DviWidget    dw;
  124.     int        diameter;
  125. {
  126.     XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
  127.         dw->dvi.state->x, dw->dvi.state->y - diameter/2,
  128.         diameter, diameter, 0, 360*64);
  129. }
  130.  
  131. DrawEllipse (dw, a, b)
  132.     DviWidget    dw;
  133.     int        a, b;
  134. {
  135.     XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
  136.         dw->dvi.state->x, dw->dvi.state->y - b/2,
  137.         a, b, 0, 360*64);
  138. }
  139.  
  140. #define RAD2DEG    (57.29577951308232)
  141.  
  142. DrawArc (dw, x0, y0, x1, y1)
  143.     DviWidget    dw;
  144.     int        x0, y0, x1, y1;
  145. {
  146.     int radius, angle0, angle1;
  147.  
  148.     radius= sqrt((double) (x0*x0 + y0*y0));
  149.     angle0= -64.0*RAD2DEG*atan2((double) -y0, (double) -x0);
  150.     if(angle0 < 0)
  151.         angle0 += 360*64;
  152.     angle1= -64.0*RAD2DEG*atan2((double) y1, (double) x1);
  153.     if(angle1 < 0)
  154.         angle1 += 360*64;
  155.     angle1 -= angle0;
  156.     if(angle1 < 0)
  157.         angle1 += 360*64;
  158.     XDrawArc (XtDisplay (dw), XtWindow (dw), dw->dvi.normal_GC,
  159.         dw->dvi.state->x + x0 - radius, dw->dvi.state->y + y0 - radius,
  160.         2*radius, 2*radius, angle0, angle1);
  161.     dw->dvi.state->x += x0 + x1;
  162.     dw->dvi.state->y += y0 + y1;
  163. }
  164.  
  165. #define MAX_KNOTS    256
  166.  
  167. DrawSpline (dw, s, len)
  168.     DviWidget    dw;
  169.     char        *s;
  170.     int        len;
  171. {
  172.     XPoint knots[MAX_KNOTS];
  173.     XPoint *points, *ComputeSpline();
  174.     int numKnots, numPoints;
  175.  
  176.     /* read knots, duplicate first and last, convert relative to absolute */
  177.     knots[0].x= dw->dvi.state->x;
  178.     knots[0].y= dw->dvi.state->y;
  179.     knots[1]= knots[0];
  180.     numKnots= 2;
  181.     while(getpoint(&s, &knots[numKnots]) && numKnots < MAX_KNOTS-1) {
  182.         knots[numKnots].x += knots[numKnots-1].x;
  183.         knots[numKnots].y += knots[numKnots-1].y;
  184.         numKnots++;
  185.     }
  186.     knots[numKnots]= knots[numKnots-1];
  187.     numKnots++;
  188.  
  189.     points= ComputeSpline(knots, numKnots, &numPoints);
  190.     XDrawLines (XtDisplay (dw),  XtWindow (dw), dw->dvi.normal_GC,
  191.             points, numPoints, CoordModeOrigin);
  192.     dw->dvi.state->x= points[numPoints-1].x;
  193.     dw->dvi.state->y= points[numPoints-1].y;
  194. }
  195.  
  196. static
  197. int getpoint(buffer, point)
  198. char    **buffer;
  199. XPoint    *point;
  200. {
  201.     char *end;
  202.  
  203.     if(*buffer == 0 || **buffer == '\0')
  204.         return 0;
  205.     point->x= (short) strtol(*buffer, &end, 10);
  206.     if(end == *buffer)
  207.         return 0;
  208.     *buffer= end;
  209.     point->y= (short) strtol(*buffer, &end, 10);
  210.     *buffer= end;
  211.     return 1;
  212. }
  213.  
  214. #define SUBDIV    4
  215.  
  216. static
  217. XPoint *ComputeSpline(knots, numKnots, numPoints)
  218. register XPoint    knots[];
  219. int    numKnots, *numPoints;
  220. {
  221.     static XPoint spline[SUBDIV*MAX_KNOTS];
  222.     register int i, j, n;
  223.     double w, t1, t2, t3;
  224.  
  225.     n= 0;
  226.     for(i=0;i < numKnots-2; i++)
  227.         for(j=0; j < SUBDIV; j++) {
  228.             w= (1.0/SUBDIV) * j;
  229.             t1= 0.5*w*w;
  230.             w -= 0.5;
  231.             t2= 0.75-w*w;
  232.             w -= 0.5;
  233.             t3= 0.5*w*w;
  234.             spline[n].x= t1*knots[i+2].x + t2*knots[i+1].x +
  235.                             t3*knots[i].x + 0.5;
  236.             spline[n].y= t1*knots[i+2].y + t2*knots[i+1].y +
  237.                             t3*knots[i].y + 0.5;
  238.             if(n == 0 || spline[n].x != spline[n-1].x ||
  239.                     spline[n].y != spline[n-1].y)
  240.                 n++;
  241.         }
  242.     spline[n++]= knots[numKnots-1];
  243.     *numPoints= n;
  244.     return spline;
  245. }
  246.  
  247. dan
  248. ----------------------------------------------------
  249. O'Reilly && Associates   argv@sun.com / argv@ora.com
  250. Opinions expressed reflect those of the author only.
  251.