home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / transfig / part04 < prev    next >
Text File  |  1990-10-09  |  42KB  |  1,565 lines

  1. Subject:  v23i017:  Tools for creating TeX documents with portable graphics, Part04/06
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: ae3f8421 b1d964da adbd526c 6f318e10
  5.  
  6. Submitted-by: Micah Beck <beck@cs.cornell.edu>
  7. Posting-number: Volume 23, Issue 17
  8. Archive-name: transfig/part04
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 4 (of 6)."
  17. # Contents:  transfig/fig2dev/dev/genlatex.c
  18. #   transfig/fig2dev/dev/genps.c
  19. # Wrapped by beck@rocky on Thu May 17 15:56:13 1990
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'transfig/fig2dev/dev/genlatex.c' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'transfig/fig2dev/dev/genlatex.c'\"
  23. else
  24. echo shar: Extracting \"'transfig/fig2dev/dev/genlatex.c'\" \(20332 characters\)
  25. sed "s/^X//" >'transfig/fig2dev/dev/genlatex.c' <<'END_OF_FILE'
  26. X/* 
  27. X *    genlatex.c : LaTeX driver for fig2dev
  28. X *
  29. X *    Author: Frank Schmuck, Cornell University 6/88
  30. X *     Converted from fig2latex 5/89 by Micah Beck
  31. X *
  32. X*/
  33. X#ifdef hpux
  34. X#include <sys/types.h>
  35. X#endif
  36. X#include <sys/file.h>
  37. X#include <stdio.h>
  38. X#include <math.h>
  39. X#include "object.h"
  40. X#include "fig2dev.h"
  41. X#include "texfonts.h"
  42. X
  43. Xextern char *strchr();
  44. Xextern void printf(), fprintf();
  45. Xextern double rad2deg, sin(), cos(), acos(), fabs(), atan();
  46. X
  47. X#ifdef hpux
  48. X#define rint(a) floor((a)+0.5)     /* close enough? */
  49. X#endif
  50. X
  51. X#ifdef gould
  52. X#define rint(a) floor((a)+0.5)     /* close enough? */
  53. X#endif
  54. X
  55. X/* 
  56. X *  Installation dependent constants:
  57. X *
  58. X *  THINDOT    latex command for generating a dot if line width = \thinlines
  59. X *  THICKDOT    latex command for generating a dot if line width = \thicklines
  60. X *  MIN_LEN    shortest slanted line that latex can produce; shorter lines will
  61. X *        we translated into a sequence of dots generated by \multiput.
  62. X *  THICK_LDOT    latex command for generating the dot for making short slanted
  63. X *        lines if line width = \thinlines
  64. X *  THIN_LDOT    ...  if line width = \thicklines
  65. X */
  66. X#define THICKDOT    "\\tenrm ."
  67. X#define THINDOT        "\\sevrm ."
  68. Xdouble    THIN_XOFF =    (0.1/72.0);
  69. Xdouble    THIN_YOFF =    (0.7/72.0);
  70. Xdouble    THICK_XOFF =    (0.4/72.0);
  71. Xdouble    THICK_YOFF =    (0.6/72.0);
  72. X#define THICK_LDOT    "\\sevrm ."
  73. X#define THIN_LDOT    "\\fivrm ."
  74. Xdouble    THIN_LXOFF =    (0.1/72.0);
  75. Xdouble    THIN_LYOFF =    (0.7/72.0);
  76. Xdouble    THICK_LXOFF =    (0.4/72.0);
  77. Xdouble    THICK_LYOFF =    (0.6/72.0);
  78. X#define MIN_LEN        (13.0/72.0)    /* 13  points */
  79. X
  80. X/*
  81. X *  other constants and macros
  82. X */
  83. X#define TOP        840
  84. X#define THINLINES    1
  85. X#define THICKLINES    2
  86. X
  87. X/*
  88. X * Cornell DVI-to-IP has trouble with circles of radius greater than 8pt.
  89. X */
  90. X#ifdef CORNELL
  91. X#define MAXCIRCLEDIA    16
  92. X#else
  93. X#define MAXCIRCLEDIA    80
  94. X#endif
  95. X#define MAXCIRCLERAD    ((MAXCIRCLEDIA-0.5)/(2*72.27))
  96. X
  97. X#define    SWAP(x,y)    {tmp=x; x=y; y=tmp;}
  98. X#define TRANS(x,y)        (*translate_coordinates)(&x,&y)
  99. X#define TRANS2(x1,y1,x2,y2)    (*translate_coordinates)(&x1,&y1); \
  100. X                (*translate_coordinates)(&x2,&y2)
  101. X#define TRANSD(x,y)        (*translate_coordinates_d)(&x,&y)
  102. X#define    MIN(x,y)    (((x) <= (y))? (x): (y))
  103. X#define    MAX(x,y)    (((x) >= (y))? (x): (y))
  104. X#define    ABS(x)        (((x) >= 0)? (x): -(x))
  105. X#define round(x)    ((int) ((x) + ((x >= 0)? 0.5: -0.5)))
  106. X#define round4(x)    ((round(10000.0*(x))/10000.0))
  107. X
  108. Xchar        thindot [] = THINDOT;
  109. Xchar        thickdot[] = THICKDOT;
  110. Xchar        thin_ldot [] = THIN_LDOT;
  111. Xchar        thick_ldot[] = THICK_LDOT;
  112. X
  113. Xint        coord_system;
  114. Xint        verbose = 0;
  115. Xdouble        dash_mag = 1.0;
  116. Xint        thick_width = 1;
  117. Xdouble        tolerance = 2.0;
  118. Xdouble        arc_tolerance = 1.0;
  119. Xint        (*translate_coordinates)() = NULL;
  120. Xint        (*translate_coordinates_d)() = NULL;
  121. Xdouble        unitlength;
  122. Xint        cur_thickness = -1;
  123. Xdouble        ldot_diameter = 1.0/72.0;
  124. Xchar        *dot_cmd = thindot;
  125. Xchar        *ldot_cmd = thin_ldot;
  126. Xdouble        dot_xoffset;
  127. Xdouble        dot_yoffset;
  128. Xdouble        ldot_xoffset;
  129. Xdouble        ldot_yoffset;
  130. X
  131. Xstatic translate1(xp, yp)
  132. Xint    *xp, *yp;
  133. X{
  134. X    *xp = *xp + 1;
  135. X    *yp = *yp + 1;
  136. X    }
  137. X
  138. Xstatic translate2(xp, yp)
  139. Xint    *xp, *yp;
  140. X{
  141. X    *xp = *xp + 1;
  142. X    *yp = TOP - *yp -1;
  143. X    }
  144. X
  145. Xstatic translate1_d(xp, yp)
  146. Xdouble    *xp, *yp;
  147. X{
  148. X    *xp = *xp + 1.0;
  149. X    *yp = *yp + 1.0;
  150. X    }
  151. X
  152. Xstatic translate2_d(xp, yp)
  153. Xdouble    *xp, *yp;
  154. X{
  155. X    *xp = *xp + 1.0;
  156. X    *yp = (double)TOP - *yp -1.0;
  157. X    }
  158. X
  159. Xvoid genlatex_option(opt, optarg)
  160. Xchar opt, *optarg;
  161. X{
  162. X    switch (opt) {
  163. X    case 'f':        /* set default text font */
  164. X    {   int i;
  165. X
  166. X        for ( i = 1; i <= MAXFONT + 1; i++ )
  167. X        if ( !strcmp(optarg, fontnames[i]) ) break;
  168. X
  169. X        if ( i > MAXFONT + 1 )
  170. X        fprintf(stderr,
  171. X            "warning: non-standard font name %s\n", optarg);
  172. X    }
  173. X        
  174. X        fontnames[0] = fontnames[1] = optarg;
  175. X        break;
  176. X
  177. X    case 'l':        /* set thin/thick line threshold */
  178. X        thick_width = atoi(optarg);
  179. X        break;
  180. X
  181. X    case 'd':
  182. X        dash_mag = atof(optarg);    /* set dash magnification */
  183. X        break;
  184. X
  185. X    case 'v':
  186. X        verbose = 1;        /* verbose mode */
  187. X        break;
  188. X
  189. X    case 's':
  190. X        if (font_size <= 0 || font_size > MAXFONTSIZE) {
  191. X        fprintf(stderr,
  192. X            "warning: font size %d out of bounds\n", font_size);
  193. X        }
  194. X        break;
  195. X
  196. X    case 'm':
  197. X    case 'L':
  198. X        break;
  199. X
  200. X    default:
  201. X        put_msg(Err_badarg, opt, "latex");
  202. X        exit(1);
  203. X        break;
  204. X    }
  205. X}
  206. X
  207. Xvoid genlatex_start(objects)
  208. XF_compound    *objects;
  209. X{
  210. X    int tmp;
  211. X
  212. X    fontsizes[0] = fontsizes[1] = TEXFONTSIZE(font_size);
  213. X
  214. X    coord_system = objects->nwcorner.y;
  215. X     unitlength = mag/objects->nwcorner.x;
  216. X
  217. X    switch (coord_system) {
  218. X        case 1:
  219. X        translate_coordinates = translate1;
  220. X        translate_coordinates_d = translate1_d;
  221. X        break;
  222. X        case 2:
  223. X        translate_coordinates = translate2;
  224. X        translate_coordinates_d = translate2_d;
  225. X        break;
  226. X        default:
  227. X        fprintf(stderr, "Wrong coordinate system; cannot continue\n");
  228. X        return;
  229. X        }
  230. X
  231. X    TRANS2(llx, lly, urx, ury);
  232. X    if (llx > urx) SWAP(llx, urx)
  233. X    if (lly > ury) SWAP(lly, ury)
  234. X
  235. X    /* LaTeX start */
  236. X    fprintf(tfp, "\\setlength{\\unitlength}{%.4fin}%%\n",
  237. X                        round4(unitlength));
  238. X    fprintf(tfp, "\\begin{picture}(%d,%d)(%d,%d)\n",
  239. X                      urx-llx, ury-lly, llx, lly);
  240. X}
  241. X
  242. Xvoid genlatex_end()
  243. X{
  244. X    /* LaTeX ending */
  245. X    fprintf(tfp, "\\end{picture}\n");
  246. X}
  247. X
  248. Xstatic set_linewidth(w)
  249. Xint    w;
  250. X{
  251. X    int        latex_w;
  252. X
  253. X    if (w == 0) return;
  254. X    /* latex only knows thin lines or thick lines */
  255. X    latex_w = (w >= thick_width)? THICKLINES: THINLINES;
  256. X    if (latex_w != cur_thickness) {
  257. X        cur_thickness = latex_w;
  258. X        if (cur_thickness == THICKLINES) {
  259. X        fprintf(tfp, "\\thicklines\n");
  260. X        dot_cmd = thickdot;
  261. X        dot_xoffset = round4(THICK_XOFF/unitlength);
  262. X        dot_yoffset = round4(THICK_YOFF/unitlength);
  263. X        ldot_cmd = thick_ldot;
  264. X        ldot_xoffset = round4(THICK_LXOFF/unitlength);
  265. X        ldot_yoffset = round4(THICK_LYOFF/unitlength);
  266. X        }
  267. X        else {
  268. X        fprintf(tfp, "\\thinlines\n");
  269. X        dot_cmd = thin_ldot;
  270. X        dot_xoffset = round4(THIN_XOFF/unitlength);
  271. X        dot_yoffset = round4(THIN_YOFF/unitlength);
  272. X        ldot_cmd = thin_ldot;
  273. X        ldot_xoffset = round4(THIN_LXOFF/unitlength);
  274. X        ldot_yoffset = round4(THIN_LYOFF/unitlength);
  275. X        }
  276. X        }
  277. X    }
  278. X
  279. Xvoid genlatex_line(l)
  280. XF_line    *l;
  281. X{
  282. X    F_point        *p, *q;
  283. X    int        x, y, llx, lly, urx, ury, arrow;
  284. X
  285. X    if (verbose) fprintf(tfp, "%%\n%% Fig POLYLINE object\n%%\n");
  286. X
  287. X    set_linewidth(l->thickness);
  288. X
  289. X    p = l->points;
  290. X    q = p->next;
  291. X
  292. X    if (q == NULL) { /* A single point line */
  293. X        x = p->x; y = p->y;
  294. X        TRANS(x, y);
  295. X        fprintf(tfp, "\\put(%3d,%3d){\\makebox(%.4f,%.4f){%s}}\n",
  296. X          x, y, dot_xoffset, dot_yoffset, dot_cmd);
  297. X        return;
  298. X        }
  299. X
  300. X    if (l->type == T_BOX) { /* A box */
  301. X        x = p->x; y = p->y;
  302. X        TRANS(x, y);
  303. X        llx = urx = x;
  304. X        lly = ury = y;
  305. X        while (q != NULL) {
  306. X        x = q->x; y = q->y;
  307. X        TRANS(x, y);
  308. X        if (x < llx) llx = x;
  309. X        if (y < lly) lly = y;
  310. X        if (x > urx) urx = x;
  311. X        if (y > ury) ury = y;
  312. X        q = q->next;
  313. X        }
  314. X        put_box (llx, lly, urx, ury, l->style, l->style_val);
  315. X        return;
  316. X        }
  317. X
  318. X    while (q != NULL) {
  319. X        arrow = 0;
  320. X        if (l->for_arrow  &&  q->next == NULL)
  321. X        arrow = 1;
  322. X        if (l->back_arrow  &&  p == l->points)
  323. X        arrow = (arrow)? 2: -1;
  324. X        single_line(p->x, p->y, q->x, q->y, arrow, l->style, l->style_val);
  325. X        p = q;
  326. X        q = q->next;
  327. X        }
  328. X
  329. X    if (l->area_fill && (int)l->area_fill != DEFAULT)
  330. X        fprintf(stderr, "Line area fill not implemented\n");
  331. X    }
  332. X
  333. Xstatic single_line (x1, y1, x2, y2, arrow, style, val)
  334. Xint    x1, y1, x2, y2, arrow, style;
  335. Xdouble    val;
  336. X{
  337. X    int    dx, dy, sx, sy;
  338. X    double l, m, deviation;
  339. X
  340. X    TRANS2(x1, y1, x2, y2);
  341. X    dx = x2-x1;
  342. X    dy = y2-y1;
  343. X    /*** compute direction vector ***/
  344. X    get_slope(dx, dy, &sx, &sy, arrow);
  345. X    /*** compute line length in x-direction ***/
  346. X    if (sx == 0) {
  347. X        l = (double)abs(dy);
  348. X    } else {
  349. X        m = (double)abs(sy) / (double)abs(sx);
  350. X        l = ((double)abs(dx) + m*(double)abs(dy)) / (1.0 + m*m);
  351. X        deviation = fabs(l-abs(dx)) + fabs(m*l-abs(dy));
  352. X        if (deviation > tolerance)
  353. X        fprintf(stderr,
  354. X          "Not a LaTeX slope (%d, %d), deviation %.1f pixels\n",
  355. X          dx, dy, deviation);
  356. X    }
  357. X    l = round4(l);
  358. X    /*** output letex command ***/
  359. X    switch (style) {
  360. X        case SOLID_LINE:
  361. X        put_solidline(x1, y1, sx, sy, l, arrow);
  362. X        break;
  363. X        case DASH_LINE:
  364. X        put_dashline(x1, y1, sx, sy, l, arrow, val);
  365. X        break;
  366. X        case DOTTED_LINE:
  367. X        put_dotline(x1, y1, sx, sy, l, arrow, val);
  368. X        break;
  369. X        }
  370. X    }
  371. X
  372. X
  373. X/*
  374. X * draw box
  375. X */
  376. Xstatic put_box (llx, lly, urx, ury, style, val)
  377. Xint    llx, lly, urx, ury, style;
  378. Xdouble    val;
  379. X{
  380. X    int    dlen;
  381. X
  382. X    switch (style) {
  383. X        case SOLID_LINE:
  384. X        fprintf(tfp, "\\put(%3d,%3d){\\framebox(%d,%d){}}\n",
  385. X          llx, lly, urx-llx, ury-lly);
  386. X        break;
  387. X        case DASH_LINE:
  388. X        dlen = round(val*dash_mag);
  389. X        fprintf(tfp, "\\put(%3d,%3d){\\dashbox{%d}(%d,%d){}}\n",
  390. X          llx, lly, dlen, urx-llx, ury-lly);
  391. X        break;
  392. X        case DOTTED_LINE:
  393. X        put_dotline (llx, lly, 1, 0, (double)(urx-llx), 0, val);
  394. X        put_dotline (llx, ury, 1, 0, (double)(urx-llx), 0, val);
  395. X        put_dotline (llx, lly, 0, 1, (double)(ury-lly), 0, val);
  396. X        put_dotline (urx, lly, 0, 1, (double)(ury-lly), 0, val);
  397. X        break;
  398. X        }
  399. X    return;
  400. X    }
  401. X
  402. X/*
  403. X * draw a solid line given latex slope
  404. X */
  405. Xstatic put_solidline (x, y, sx, sy, l, arrow)
  406. Xint    x, y, sx, sy, arrow;
  407. Xdouble    l;
  408. X{
  409. X    double    cosine;        /* cosine of line angle */
  410. X    double    dx, dy;
  411. X    int    x2, y2, n;
  412. X
  413. X    if (sx) {
  414. X        cosine = (double)abs(sx) / hypot((double)sx, (double)sy);
  415. X        x2 = (sx >= 0)? x + round(l): x - round(l);
  416. X        y2 = y + round( ((sx>=0)? l: -l) * (double)sy / (double)sx);
  417. X        }
  418. X    else {
  419. X        cosine = 1.0;
  420. X        x2 = x;
  421. X        y2 = (sy >= 0)? y + round(l): y - round(l);
  422. X        }
  423. X    if (sx == 0  ||  sy == 0  ||  (l/cosine)*unitlength >= MIN_LEN) {
  424. X        switch (arrow) {
  425. X        case 0:  /* simple line */
  426. X        fprintf(tfp, "\\put(%3d,%3d){\\line(%2d,%2d)", x, y, sx,sy);
  427. X        break;
  428. X        case 1:  /* forward arrow */
  429. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d)", x, y, sx,sy);
  430. X        break;
  431. X        case -1: /* backward arrow */
  432. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d)", x2, y2, -sx,-sy);
  433. X        break;
  434. X        case 2:  /* double arrow */
  435. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){  0}}\n", x,y,-sx,-sy);
  436. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d)", x, y, sx, sy);
  437. X        break;
  438. X        }
  439. X        if (l == floor(l))
  440. X        fprintf(tfp, "{%3.0f}}\n", l);
  441. X        else
  442. X        fprintf(tfp, "{%7.3f}}\n", l);
  443. X        }
  444. X    else {
  445. X        n = 2 * (l/cosine) / (ldot_diameter/unitlength);
  446. X        fprintf(stderr, "Line too short; will do %d dots\n", n);
  447. X        dx = l / (double)n;
  448. X        if (sx < 0) dx = -dx;
  449. X        dy = dx * (double)sy / (double)sx;
  450. X        fprintf(tfp, 
  451. X          "\\multiput(%3d,%3d)(%.5f,%.5f){%d}{\\makebox(%.4f,%.4f){%s}}\n",
  452. X          x, y, dx, dy, n+1, ldot_xoffset, ldot_yoffset, ldot_cmd);
  453. X        if (arrow == 1  ||  arrow == 2)  /* forward arrow */
  454. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x2,y2, sx,sy);
  455. X        if (arrow == -1  ||  arrow == 2) /* backward arrow */
  456. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x,y, -sx,-sy);
  457. X        }
  458. X    }
  459. X
  460. X/*
  461. X * draw a dashed line given latex slope
  462. X */
  463. Xstatic put_dashline (x, y, sx, sy, l, arrow, val)
  464. Xint    x, y, sx, sy, arrow;
  465. Xdouble    l;
  466. Xdouble    val;
  467. X{
  468. X    double    cosine;        /* cosine of line angle */
  469. X    double    nd;        /* number of dashes and gaps fitting on line */
  470. X    int    n;        /* nd rounded to the nearest odd integer */
  471. X    double    dl;        /* actual x-length of each dash */
  472. X    double    dg;        /* actual x-length of each gap */
  473. X    double    dx, dy;        /* step between dashes */
  474. X    int    x2, y2;
  475. X
  476. X
  477. X    if (sx) {
  478. X        cosine = (double)abs(sx) / hypot((double)sx, (double)sy);
  479. X        x2 = (sx >= 0)? x + round(l): x - round(l);
  480. X        y2 = y + round( ((sx>=0)? l: -l) * (double)sy / (double)sx );
  481. X        }
  482. X    else {
  483. X        cosine = 1.0;
  484. X        x2 = x;
  485. X        y2 = (sy >= 0)? y + round(l): y - round(l);
  486. X        }
  487. X    /*** compute number of dashes, length of dashes and gaps ***/
  488. X    nd = l / (val*dash_mag*cosine);
  489. X    n = (int) (rint((nd + 1.0)/2.0)*2 - 1);
  490. X    dl = l / (double)n;
  491. X    if (sx  &&  sy  &&  (dl/cosine)*unitlength < MIN_LEN) {
  492. X        fprintf(stderr, "Dash too small; using larger dash\n");
  493. X        dl = MIN_LEN/unitlength * cosine;
  494. X        nd = l / dl;
  495. X        n = (int) (rint((nd + 1.0)/2.0)*2 - 1);
  496. X        }
  497. X    if (2*dl >= l  ||  (sx  &&  sy  &&  (l/cosine)*unitlength < MIN_LEN)) {
  498. X        fprintf(stderr, "Dashed line too short; drawing solid line\n");
  499. X        put_solidline (x, y, sx, sy, l, arrow);
  500. X        return;
  501. X        }
  502. X    dg = (l - (n/2+1)*dl) / (double)(n/2);
  503. X    if (sx) {
  504. X        dx = dl+dg;
  505. X        if (sx < 0) dx = -dx;
  506. X        dy = dx * (double)sy / (double)sx;
  507. X        }
  508. X    else {
  509. X        dx = 0.0;
  510. X        dy = dl+dg;
  511. X        if (sy < 0) dy = -dy;
  512. X        }
  513. X    /*** draw dashed line ***/
  514. X    fprintf(tfp, "\\multiput(%3d,%3d)(%.5f,%.5f){%d}{\\line(%2d,%2d){%7.3f}}\n",
  515. X        x, y, dx, dy, n/2+1, sx, sy, dl);
  516. X    /*** draw arrow heads ***/
  517. X    if (arrow == 1  ||  arrow == 2)
  518. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x2, y2, sx, sy);
  519. X    if (arrow == -1  ||  arrow == 2)
  520. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x, y, -sx, -sy);
  521. X    }
  522. X
  523. X/*
  524. X * draw a dotted line given latex slope
  525. X */
  526. Xstatic put_dotline (x, y, sx, sy, l, arrow, val)
  527. Xint    x, y, sx, sy, arrow;
  528. Xdouble    l;
  529. Xdouble    val;
  530. X{
  531. X    double    cosine;        /* cosine of line angle */
  532. X    double    nd;        /* number of dots fitting on line */
  533. X    int    n;        /* nd rounded to the nearest integer */
  534. X    double    dx, dy;        /* step between dashes */
  535. X    int    x2, y2;
  536. X
  537. X
  538. X    cosine = (sx)? (double)abs(sx) / hypot((double)sx, (double)sy): 1.0;
  539. X    /*** compute step width ***/
  540. X    nd = l / (3*val*cosine);
  541. X    n = rint(nd);
  542. X    dx = l / (double)n;
  543. X    if (sx) {
  544. X        dx = l / (double)n;
  545. X        if (sx < 0) dx = -dx;
  546. X        dy = dx * (double)sy / (double)sx;
  547. X        }
  548. X    else {
  549. X        dx = 0.0;
  550. X        dy = l / (double)n;
  551. X        if (sy < 0) dy = -dy;
  552. X        }
  553. X    /*** draw arrow heads ***/
  554. X    if (arrow == 1  ||  arrow == 2) {
  555. X        /* forward arrow */
  556. X        if (sx) {
  557. X        x2 = (sx >= 0)? x + round(l): x - round(l);
  558. X        y2 = y + round( ((sx>=0)? l: -l) * (double)sy / (double)sx );
  559. X        }
  560. X        else {
  561. X        x2 = x;
  562. X        y2 = (sy >= 0)? y + round(l): y - round(l);
  563. X        }
  564. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x2, y2, sx, sy);
  565. X        n--;
  566. X        }
  567. X    if (arrow == -1  ||  arrow == 2) {
  568. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%2d,%2d){0}}\n", x, y, -sx, -sy);
  569. X        x = round(x + dx);
  570. X        y = round(y + dy);
  571. X        n--;
  572. X        }
  573. X    /*** draw dotted line ***/
  574. X    fprintf(tfp, "\\multiput(%3d,%3d)(%.5f,%.5f){%d}{\\makebox(%.4f,%.4f){%s}}\n",
  575. X        x, y, dx, dy, n+1, dot_xoffset, dot_yoffset, dot_cmd);
  576. X    }
  577. X
  578. Xvoid genlatex_spline(s)
  579. XF_spline    *s;
  580. X{
  581. X    fprintf(stderr, "Can't generate spline; omitting object\n");
  582. X    }
  583. X
  584. Xvoid genlatex_ellipse(e)
  585. XF_ellipse    *e;
  586. X{
  587. X    int  x, y, d, dx, dy;
  588. X
  589. X    if (verbose) fprintf(tfp, "%%\n%% Fig ELLIPSE\n%%\n");
  590. X
  591. X    set_linewidth(e->thickness);
  592. X    switch (e->style) {
  593. X        case SOLID_LINE:
  594. X        break;
  595. X        case DASH_LINE:
  596. X        fprintf(stderr, "Dashed circles and elipses not supported\n");
  597. X        break;
  598. X        case DOTTED_LINE:
  599. X        fprintf(stderr, "Dotted circles and elipses not supported\n");
  600. X        break;
  601. X        }
  602. X
  603. X    x = e->center.x;
  604. X    y = e->center.y;
  605. X    TRANS(x, y);
  606. X    if ((e->type == T_CIRCLE_BY_RAD || e->type == T_CIRCLE_BY_DIA)
  607. X            && e->radiuses.x*unitlength <= MAXCIRCLERAD) {
  608. X
  609. X        d = 2 * e->radiuses.x;
  610. X        if (e->area_fill == BLACK_FILL)
  611. X            fprintf(tfp, "\\put(%3d,%3d){\\circle*{%d}}\n", x, y, d);
  612. X        else {
  613. X              fprintf(tfp, "\\put(%3d,%3d){\\circle{%d}}\n", x, y, d);
  614. X        if (e->area_fill && (int)e->area_fill != DEFAULT)
  615. X            fprintf(stderr, "Circle area fill not implemented\n");
  616. X        }
  617. X
  618. X    } else {        
  619. X        dx = 2 * e->radiuses.x;
  620. X        dy = 2 * e->radiuses.y;
  621. X        fprintf(tfp, "\\put(%3d,%3d){\\oval(%d,%d)}\n", x, y, dx, dy);
  622. X        if (e->area_fill && (int)e->area_fill != DEFAULT)
  623. X        fprintf(stderr, "Ellipse area fill not implemented\n");
  624. X    }
  625. X      }
  626. X
  627. Xvoid genlatex_text(t)
  628. XF_text    *t;
  629. X{
  630. X    int       x, y;
  631. X    char    *tpos, *cp;
  632. X
  633. X    if (verbose) fprintf(tfp, "%%\n%% Fig TEXT object\n%%\n");
  634. X
  635. X    x = t->base_x;
  636. X    y = t->base_y;
  637. X    TRANS(x, y);
  638. X
  639. X    switch (t->type) {
  640. X
  641. X        case T_LEFT_JUSTIFIED:
  642. X        case DEFAULT:
  643. X            tpos = "[lb]";
  644. X        break;
  645. X
  646. X        case T_CENTER_JUSTIFIED:
  647. X            tpos = "[b]";
  648. X        break;
  649. X
  650. X        case T_RIGHT_JUSTIFIED:
  651. X            tpos = "[rb]";
  652. X        break;
  653. X
  654. X        default:
  655. X        fprintf(stderr, "Text incorrectly positioned\n");
  656. X        }
  657. X
  658. X    /* raisebox is used to position text at baseline */
  659. X    fprintf(tfp, 
  660. X      "\\put(%3d,%3d){\\makebox(0,0)%s{\\raisebox{0pt}[0pt][0pt]",
  661. X      x, y, tpos);
  662. X    fprintf(tfp, "{\\%s%s ", TEXFONTSIZE(t->size), TEXFONT(t->font));
  663. X
  664. X    if (t->font && t->font !=DEFAULT)
  665. X
  666. X        /* this loop escapes characters "$&%#_{}" */
  667. X        /* and deleted characters "~^\" */
  668. X        for(cp = t->cstring; *cp; cp++) {
  669. X                  if (strchr("$&%#_{}", *cp)) (void)fputc('\\', tfp);
  670. X                  if (strchr("~^\\", *cp))
  671. X            fprintf(stderr,
  672. X                "Bad character in text object '%c'\n" ,*cp);
  673. X            else
  674. X            (void)fputc(*cp, tfp);
  675. X              }
  676. X    else 
  677. X        fprintf(tfp, "%s", t->cstring);
  678. X
  679. X     fprintf(tfp, "}}}\n");
  680. X    }
  681. X
  682. Xvoid genlatex_arc(a)
  683. XF_arc    *a;
  684. X/*
  685. X *  Approximates an arc by a sequence of quarter ovals.
  686. X *
  687. X *  Example:
  688. X *
  689. X *    Arc with center at (0,0) and radius 10 from +45 degree to +225 degree
  690. X *    (arc from p1 = (7.07, 7.07) to p2 = (-7.07, -7.07) counterclockwise).
  691. X *    This arc is approximated by three quarter ovals, one for each quadrant
  692. X *    through which the arc goes:
  693. X *
  694. X *     1. quarter oval from p1 to the intersection of arc and y-axis,
  695. X *        i.e., from (7.07, 7.07) to (0, 10) in quadrant 0
  696. X *
  697. X *        \put(0, 7.07){\oval(14.14, 5.86)[tr]}
  698. X *
  699. X *     2. quarter oval from intersection arc/y-axis to intersection arc/x-axis
  700. X *        i.e., from (0, 10) to (-10, 0) in quadrant 1
  701. X *
  702. X *        \put(0, 0){\oval(20,20)[tl]}
  703. X *
  704. X *     3. quarter oval from p1 to the intersection of arc and y-axis,
  705. X *        i.e., from (-10, 0) to (-7.07, -7.07) in quadrant 2
  706. X *
  707. X *        \put(-7.07, 0){\oval(5.86, 14.14)[bl]}
  708. X */
  709. X{
  710. X    F_pos        p1, p2, pq[4];
  711. X    double        cx, cy;
  712. X    double        v1x, v1y, v2x, v2y;
  713. X    double        r, angle1, angle2;
  714. X    int        q1, q2;
  715. X    int        p1_arrow, p2_arrow;
  716. X    static char    *ad1[4] = { " 0,-1", " 1, 0", " 0, 1", "-1, 0" };
  717. X    static char    *ad2[4] = { "-1, 0", " 0,-1", " 1, 0", " 0, 1" };
  718. X
  719. X    set_linewidth(a->thickness);
  720. X    switch (a->style) {
  721. X        case SOLID_LINE:
  722. X        break;
  723. X        case DASH_LINE:
  724. X        fprintf(stderr, "Dashed arcs not supported\n");
  725. X        break;
  726. X        case DOTTED_LINE:
  727. X        fprintf(stderr, "Dotted arcs not supported\n");
  728. X        break;
  729. X        }
  730. X    if (a->direction == 1) {
  731. X        p1 = a->point[0];
  732. X        p2 = a->point[2];
  733. X        p1_arrow = (a->back_arrow != NULL);
  734. X        p2_arrow = (a->for_arrow != NULL);
  735. X        }
  736. X    else {
  737. X        p1 = a->point[2];
  738. X        p2 = a->point[0];
  739. X        p1_arrow = (a->for_arrow != NULL);
  740. X        p2_arrow = (a->back_arrow != NULL);
  741. X        }
  742. X    cx = a->center.x;
  743. X    cy = a->center.y;
  744. X    TRANS2(p1.x, p1.y, p2.x, p2.y);
  745. X    TRANSD(cx, cy);
  746. X    /*** compute vectors and angles from arc center to p1, p2 ***/
  747. X    v1x = (double)p1.x - cx;
  748. X    v1y = (double)p1.y - cy;
  749. X    v2x = (double)p2.x - cx;
  750. X    v2y = (double)p2.y - cy;
  751. X    angle1 = atan2(v1y, v1x) * rad2deg;
  752. X    angle2 = atan2(v2y, v2x) * rad2deg;
  753. X    if (angle1 < 0.0)
  754. X        angle1 += 360.0; 
  755. X    if (angle2 < 0.0)
  756. X        angle2 += 360.0; 
  757. X    /* compute arc radius */
  758. X    r = hypot(v1x, v1y);
  759. X    /*** compute intersection of arc with x and y axis (origin at cx, cy) */
  760. X    pq[0].x = round(cx);
  761. X    pq[0].y = round(cy + r);
  762. X    pq[1].x = round(cx - r);
  763. X    pq[1].y = round(cy);
  764. X    pq[2].x = round(cx);
  765. X    pq[2].y = round(cy - r);
  766. X    pq[3].x = round(cx + r);
  767. X    pq[3].y = round(cy);
  768. X    /*** compute in which quadrants p1 and p2 are located ***/
  769. X    q1 = (int)(angle1/90.0);
  770. X    q2 = (int)(angle2/90.0);
  771. X    if (fabs(angle1 - 90.0*q1) > arc_tolerance 
  772. X     || fabs(angle2 - 90.0*q2) > arc_tolerance)
  773. X        fprintf(stderr, "Approximating arc by ovals\n");
  774. X    /*** Draw arc ***/
  775. X    if (p1_arrow)
  776. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%s){0}}\n", p1.x, p1.y, ad1[q1]);
  777. X    while (q1 != q2) {
  778. X        put_quarter(p1, pq[q1], q1);
  779. X        p1 = pq[q1];
  780. X        q1 = (q1 + 1) % 4;
  781. X        }
  782. X    put_quarter(p1, p2, q1);
  783. X    if (p2_arrow)
  784. X        fprintf(tfp, "\\put(%3d,%3d){\\vector(%s){0}}\n", p2.x, p2.y, ad2[q2]);
  785. X
  786. X    if (a->area_fill && (int)a->area_fill != DEFAULT)
  787. X        fprintf(stderr, "Arc area fill not implemented\n");
  788. X    }
  789. X
  790. Xstatic put_quarter(p1, p2, q)
  791. XF_pos    p1, p2;
  792. Xint    q;
  793. X/*
  794. X *  Draw quarter oval from p1 to p2 in quadrant q
  795. X */
  796. X{
  797. X    char    *opt;
  798. X    int    px, py, dx, dy;
  799. X
  800. X    dx = 2*ABS(p1.x - p2.x);
  801. X    dy = 2*ABS(p1.y - p2.y);
  802. X    if (dx == 0  &&  dy == 0)
  803. X        return;
  804. X    switch (q) {
  805. X        case 0:
  806. X        px = MIN(p1.x, p2.x);
  807. X        py = MIN(p1.y, p2.y);
  808. X        opt = "tr";
  809. X        break;
  810. X        case 1:
  811. X        px = MAX(p1.x, p2.x);
  812. X        py = MIN(p1.y, p2.y);
  813. X        opt = "tl";
  814. X        break;
  815. X        case 2:
  816. X        px = MAX(p1.x, p2.x);
  817. X        py = MAX(p1.y, p2.y);
  818. X        opt = "bl";
  819. X        break;
  820. X        case 3:
  821. X        px = MIN(p1.x, p2.x);
  822. X        py = MAX(p1.y, p2.y);
  823. X        opt = "br";
  824. X        break;
  825. X        }
  826. X    fprintf(tfp, "\\put(%3d,%3d){\\oval(%3d,%3d)[%s]}\n", px, py, dx, dy, opt);
  827. X    }
  828. X
  829. Xstruct driver dev_latex = {
  830. X         genlatex_option,
  831. X    genlatex_start,
  832. X    genlatex_arc,
  833. X    genlatex_ellipse,
  834. X    genlatex_line,
  835. X    genlatex_spline,
  836. X    genlatex_text,
  837. X    genlatex_end,
  838. X    EXCLUDE_TEXT
  839. X};
  840. END_OF_FILE
  841. if test 20332 -ne `wc -c <'transfig/fig2dev/dev/genlatex.c'`; then
  842.     echo shar: \"'transfig/fig2dev/dev/genlatex.c'\" unpacked with wrong size!
  843. fi
  844. # end of 'transfig/fig2dev/dev/genlatex.c'
  845. fi
  846. if test -f 'transfig/fig2dev/dev/genps.c' -a "${1}" != "-c" ; then 
  847.   echo shar: Will not clobber existing file \"'transfig/fig2dev/dev/genps.c'\"
  848. else
  849. echo shar: Extracting \"'transfig/fig2dev/dev/genps.c'\" \(17407 characters\)
  850. sed "s/^X//" >'transfig/fig2dev/dev/genps.c' <<'END_OF_FILE'
  851. X/* 
  852. X *    genps.c: PostScript driver for fig2dev
  853. X *
  854. X*/
  855. X#ifdef hpux
  856. X#include <sys/types.h>
  857. X#endif
  858. X#include <sys/file.h>
  859. X#include <stdio.h>
  860. X#include <math.h>
  861. X#include "pi.h"
  862. X#include <pwd.h>
  863. X#include "object.h"
  864. X#include "fig2dev.h"
  865. X
  866. Xextern            x_flag;
  867. X
  868. X#define        PAGE_WIDTH        612    /* points; 8.5" */
  869. X#define        PAGE_HEIGHT        792    /* points; 11" */
  870. X#define        TRUE            1
  871. X#define        FALSE            0
  872. X#define        POINT_PER_INCH        72
  873. X#define        ULIMIT_FONT_SIZE    300
  874. X
  875. Xint        coord_system;
  876. Xint        show_page = 0;
  877. Xint        cur_thickness;
  878. Xint        center = 0;
  879. Xint        landscape = 0;
  880. X
  881. Xstatic char        *fontnames_x[] = {
  882. X            "Times-Roman", "Times-Roman",    /* default */
  883. X            "Times-Italic",            /* italic */
  884. X            "Times-Bold",            /* bold */
  885. X            "Times-BoldItalic",
  886. X            "AvantGarde",
  887. X            "AvantGarde-BookOblique",
  888. X            "AvantGarde-Demi",
  889. X            "AvantGarde-DemiOblique",
  890. X            "Bookman-Light",
  891. X            "Bookman-LightItalic",
  892. X            "Bookman-Demi",
  893. X            "Bookman-DemiItalic",
  894. X            "Courier",    
  895. X            "Courier-Oblique",
  896. X            "Courier-Bold",
  897. X            "Courier-BoldItalic",
  898. X            "Helvetica",
  899. X            "Helvetica-Oblique",
  900. X            "Helvetica-Bold",
  901. X            "Helvetica-BoldOblique",
  902. X            "Helvetica-Narrow",
  903. X            "Helvetica-Narrow-Oblique",
  904. X            "Helvetica-Narrow-Bold",
  905. X            "Helvetica-Narrow-BoldOblique",
  906. X            "NewCenturySchlbk-Roman",
  907. X            "NewCenturySchlbk-Italic",
  908. X            "NewCenturySchlbk-Bold",
  909. X            "NewCenturySchlbk-BoldItalic",
  910. X            "Palatino-Roman",
  911. X            "Palatino-Italic",
  912. X            "Palatino-Bold",
  913. X            "Palatino-BoldItalic",
  914. X            "Symbol",
  915. X            "ZapfChancery-MediumItalic",
  916. X            "ZapfDingbats"
  917. X        };
  918. X
  919. Xstatic char        *fontnames_sv[] = {
  920. X            "Times-Roman", "Times-Roman",    /* default */
  921. X            "Times-Roman",            /* roman */
  922. X            "Times-Bold",            /* bold */
  923. X            "Times-Italic",            /* italic */
  924. X            "Helvetica",            /* sans serif */
  925. X            "Courier"            /* typewriter */
  926. X        };
  927. X#define MAXFONT (x_flag ? 35 : 5)
  928. X
  929. X#define PS_FONTNAMES    (x_flag ? fontnames_x : fontnames_sv)
  930. X
  931. X#define PSFONT(F) \
  932. X     ((F) <= MAXFONT ? PS_FONTNAMES[(F)+1] : PS_FONTNAMES[0])
  933. X
  934. X#define PSFONTSIZE(S)    (((S) > 0 ? \
  935. X                ((S) <= ULIMIT_FONT_SIZE ? \
  936. X                     (S) : \
  937. X                    ULIMIT_FONT_SIZE) : \
  938. X                font_size)/mag)
  939. X
  940. X#define GRAYVAL(F)    (x_flag ? ((int)(F) <= 21 ? \
  941. X                (21 - (int)(F))/20.0 : 0.0) : \
  942. X                ((int)(F) <= 5 ? ((int)(F) - 1)/4.0 : 0.0))
  943. X
  944. X#define        BEGIN_PROLOG    "\
  945. X/$F2psDict 32 dict def \
  946. X$F2psDict begin\
  947. X    $F2psDict /mtrx matrix put\
  948. X"
  949. X#define        ELLIPSE_PS    " \
  950. X/DrawEllipse {\
  951. X    /endangle exch def\
  952. X    /startangle exch def\
  953. X    /yrad exch def\
  954. X    /xrad exch def\
  955. X    /y exch def\
  956. X    /x exch def\
  957. X    /savematrix mtrx currentmatrix def\
  958. X    x y translate xrad yrad scale 0 0 1 startangle endangle arc\
  959. X    savematrix setmatrix\
  960. X    } def\
  961. X"
  962. X/* The original PostScript definition for adding a spline section to the
  963. X * current path uses recursive bisection.  The following definition using the
  964. X * curveto operator is more efficient since it executes at compiled rather
  965. X * than interpreted code speed.  The Bezier control points are 2/3 of the way
  966. X * from z1 (and z3) to z2.
  967. X *
  968. X * ---Rene Llames, 21 July 1988.
  969. X */
  970. X#define        SPLINE_PS    " \
  971. X/DrawSplineSection {\
  972. X    /y3 exch def\
  973. X    /x3 exch def\
  974. X    /y2 exch def\
  975. X    /x2 exch def\
  976. X    /y1 exch def\
  977. X    /x1 exch def\
  978. X    /xa x1 x2 x1 sub 0.666667 mul add def\
  979. X    /ya y1 y2 y1 sub 0.666667 mul add def\
  980. X    /xb x3 x2 x3 sub 0.666667 mul add def\
  981. X    /yb y3 y2 y3 sub 0.666667 mul add def\
  982. X    x1 y1 lineto\
  983. X    xa ya xb yb x3 y3 curveto\
  984. X    } def\
  985. X"
  986. X#define        END_PROLOG    "\
  987. X    end\
  988. X    /$F2psBegin {$F2psDict begin /$F2psEnteredState save def} def\
  989. X    /$F2psEnd {$F2psEnteredState restore end} def\
  990. X    %%EndProlog\
  991. X"
  992. X
  993. Xvoid genps_option(opt, optarg)
  994. Xchar opt;
  995. Xchar *optarg;
  996. X{
  997. X    int i;
  998. X
  999. X    switch (opt) {
  1000. X
  1001. X    case 'f':
  1002. X        for ( i = 1; i <= MAXFONT + 1; i++ )
  1003. X            if ( !strcmp(optarg, PS_FONTNAMES[i]) ) break;
  1004. X
  1005. X        if ( i > MAXFONT + 1 )
  1006. X            fprintf(stderr,
  1007. X                "warning: non-standard font name %s\n", optarg);
  1008. X
  1009. X            fontnames_sv[0] = fontnames_sv[1] = optarg;
  1010. X            fontnames_x[0] = fontnames_x[1] = optarg;
  1011. X            break;
  1012. X
  1013. X    case 'c':
  1014. X            center = 1;
  1015. X        break;
  1016. X
  1017. X    case 's':
  1018. X        if (font_size <= 0 || font_size > ULIMIT_FONT_SIZE) {
  1019. X            fprintf(stderr,
  1020. X                "warning: font size %d out of bounds\n", font_size);
  1021. X        }
  1022. X        break;
  1023. X
  1024. X    case 'P':
  1025. X        show_page = 1;
  1026. X        break;
  1027. X
  1028. X          case 'm':
  1029. X          case 'L':
  1030. X        break;
  1031. X
  1032. X          case 'l':
  1033. X        landscape = 1;
  1034. X        break;
  1035. X
  1036. X    default:
  1037. X        put_msg(Err_badarg, opt, "ps");
  1038. X        exit(1);
  1039. X        break;
  1040. X    }
  1041. X}
  1042. X
  1043. Xvoid genps_start(objects)
  1044. XF_compound    *objects;
  1045. X{
  1046. X    char        host[256];
  1047. X    struct passwd    *who;
  1048. X    long        when;
  1049. X    extern char    *ctime(), *strcpy();
  1050. X    extern long    time();
  1051. X    double        tx, scalex, scaley;
  1052. X    double        dx, dy, origx, origy;
  1053. X    int        itmp;
  1054. X
  1055. X    coord_system = objects->nwcorner.y;
  1056. X    scalex = scaley = mag * POINT_PER_INCH / (double)objects->nwcorner.x;
  1057. X    /* convert to point unit */
  1058. X    llx = (int)ceil(llx * scalex); lly = (int)ceil(lly * scaley);
  1059. X    urx = (int)ceil(urx * scalex); ury = (int)ceil(ury * scaley);
  1060. X    if (landscape)          /* swap x,y */
  1061. X        {
  1062. X        itmp = llx; llx = lly; lly = itmp;
  1063. X        itmp = urx; urx = ury; ury = itmp;
  1064. X        }
  1065. X    if (center) {
  1066. X        dx = urx - llx;
  1067. X        dy = ury - lly;
  1068. X        tx = (PAGE_WIDTH - dx) / 2.0;
  1069. X        if (landscape)
  1070. X        origx=0;
  1071. X        else
  1072. X        origx = tx - llx;
  1073. X        urx = (llx=tx) + dx;
  1074. X        ury = (PAGE_HEIGHT + dy) / 2.0;
  1075. X        if (coord_system == 2)
  1076. X        origy = ury + lly;
  1077. X        else
  1078. X        origy = ury - dy - lly;
  1079. X        lly = ury - dy;
  1080. X        }
  1081. X    else {
  1082. X        origx = 0.0;
  1083. X        origy = PAGE_HEIGHT;
  1084. X        }
  1085. X
  1086. X    if (coord_system == 2) scaley = -scaley;
  1087. X
  1088. X    fprintf(tfp, "%%!\n");    /* PostScript magic strings */
  1089. X    who = getpwuid(getuid());
  1090. X    if (-1 == gethostname(host, sizeof(host)))
  1091. X        (void)strcpy(host, "unknown-host!?!?");
  1092. X    (void) time(&when);
  1093. X    fprintf(tfp, "%%%%Title: %s\n", ((from) ? from : "stdin"));
  1094. X    fprintf(tfp, "%%%%Creator: %s\n", prog);
  1095. X    fprintf(tfp, "%%%%CreationDate: %s", ctime(&when));
  1096. X    fprintf(tfp, "%%%%For: %s@%s (%s)\n",
  1097. X            who->pw_name, host, who->pw_gecos);
  1098. X    fprintf(tfp, "%%%%Pages: %d\n", show_page);
  1099. X
  1100. X    if (show_page)
  1101. X      fprintf(tfp, "%%%%BoundingBox: %d %d %d %d\n", llx, lly, urx, ury);
  1102. X    else
  1103. X      fprintf(tfp, "%%%%BoundingBox: 0 0 %d %d\n", urx-llx, ury-lly);
  1104. X
  1105. X    fprintf(tfp, "%%%%EndComments\n");
  1106. X    fprintf(tfp, "%s\n", BEGIN_PROLOG);
  1107. X    if (ellipse_exist(objects)) fprintf(tfp, "%s\n", ELLIPSE_PS);
  1108. X    if (normal_spline_exist(objects)) fprintf(tfp, "%s\n", SPLINE_PS);
  1109. X    fprintf(tfp, "%s\n", END_PROLOG);
  1110. X    fprintf(tfp, "$F2psBegin\n");
  1111. X    fprintf(tfp, "1 setlinecap 1 setlinejoin\n");
  1112. X
  1113. X    if (!show_page) fprintf(tfp, "%d %d translate\n", -llx, lly);
  1114. X    fprintf(tfp, "%f %f translate %.3f %.3f scale\n",
  1115. X        origx, (show_page ? origy : ury-lly), scalex, scaley);
  1116. X
  1117. X    /* **** Land scape mode ****/
  1118. X    if (landscape)
  1119. X        fprintf(tfp, "%d 0 translate 90 rotate\n", PAGE_WIDTH);
  1120. X
  1121. X    /*    if ((t = PAGE_HEIGHT / SCALE / WIDTH) < 1.0)
  1122. X    *    fprintf(tfp, "%f %f scale\n", t, t);
  1123. X    *   }
  1124. X    *else if ((t = PAGE_HEIGHT / SCALE / HEIGHT) < 1.0)
  1125. X    *    fprintf(tfp, "%f %f scale\n", t, t);
  1126. X    */
  1127. X
  1128. X    }
  1129. X
  1130. Xvoid genps_end()
  1131. X{
  1132. X    if (show_page) fprintf(tfp, "showpage\n");
  1133. X    fprintf(tfp, "$F2psEnd\n");
  1134. X    }
  1135. X
  1136. Xstatic set_style(s, v)
  1137. Xint    s;
  1138. Xdouble    v;
  1139. X{
  1140. X    if (s == DASH_LINE) {
  1141. X        if (v > 0.0) fprintf(tfp, "\t[%f] 0 setdash\n", v);
  1142. X        }
  1143. X    else if (s == DOTTED_LINE) {
  1144. X        if (v > 0.0) fprintf(tfp, "\t[1 %f] 0 setdash\n", v);
  1145. X        }
  1146. X    }
  1147. X
  1148. Xstatic reset_style(s, v)
  1149. Xint    s;
  1150. Xdouble    v;
  1151. X{
  1152. X    if (s == DASH_LINE) {
  1153. X        if (v > 0.0) fprintf(tfp, "\t[] 0 setdash\n");
  1154. X        }
  1155. X    else if (s == DOTTED_LINE) {
  1156. X        if (v > 0.0) fprintf(tfp, "\t[] 0 setdash\n");
  1157. X        }
  1158. X    }
  1159. X
  1160. Xstatic set_linewidth(w)
  1161. Xint    w;
  1162. X{
  1163. X    extern int    cur_thickness;
  1164. X
  1165. X    if (w != cur_thickness) {
  1166. X        cur_thickness = w;
  1167. X        fprintf(tfp, "%.3f setlinewidth\n", 1.0 * cur_thickness);
  1168. X        }
  1169. X    }
  1170. X
  1171. Xvoid genps_line(l)
  1172. XF_line    *l;
  1173. X{
  1174. X    F_point        *p, *q;
  1175. X    /* JNT */
  1176. X    int        radius;
  1177. X    
  1178. X    set_linewidth(l->thickness);
  1179. X    radius = l->radius;                /* radius of rounded-corner boxes */
  1180. X    p = l->points;
  1181. X    q = p->next;
  1182. X    if (q == NULL) { /* A single point line */
  1183. X        fprintf(tfp, "newpath %d %d moveto %d %d lineto stroke\n",
  1184. X            p->x, p->y, p->x, p->y);
  1185. X        return;
  1186. X        }
  1187. X    if (l->back_arrow)
  1188. X        draw_arrow_head((double)q->x, (double)q->y, (double)p->x,
  1189. X            (double)p->y, l->back_arrow->ht, l->back_arrow->wid);
  1190. X    set_style(l->style, l->style_val);
  1191. X    fprintf(tfp, "%% Polyline\n");
  1192. X    /* JNT */
  1193. X            if (l->type == T_ARC_BOX)
  1194. X                {
  1195. X                register int xmin,xmax,ymin,ymax;
  1196. X
  1197. X                xmin = xmax = p->x;
  1198. X                ymin = ymax = p->y;
  1199. X                while (p->next != NULL) /* find lower left and upper right corne
  1200. Xrs */
  1201. X                        {
  1202. X                        p=p->next;
  1203. X                        if (xmin > p->x)
  1204. X                                xmin = p->x;
  1205. X                        else if (xmax < p->x)
  1206. X                                xmax = p->x;
  1207. X                        if (ymin > p->y)
  1208. X                                ymin = p->y;
  1209. X                        else if (ymax < p->y)
  1210. X                                ymax = p->y;
  1211. X            }
  1212. X                fprintf(tfp, "newpath %d %d moveto",xmin+radius, ymin);
  1213. X                fprintf(tfp, " %d %d %d %d %d arcto 4 {pop} repeat",
  1214. X                                xmin, ymin, xmin, ymax-radius, radius);
  1215. X                fprintf(tfp, " %d %d %d %d %d arcto 4 {pop} repeat", /* arc thro
  1216. Xugh bl to br */
  1217. X                                xmin, ymax, xmax-radius, ymax, radius);
  1218. X                fprintf(tfp, " %d %d %d %d %d arcto 4 {pop} repeat", /* arc thro
  1219. Xugh br to tr */
  1220. X                                xmax, ymax, xmax, ymin+radius, radius);
  1221. X                fprintf(tfp, " %d %d %d %d %d arcto 4 {pop} repeat", /* arc thro
  1222. Xugh tr to tl */
  1223. X                                xmax, ymin, xmin+radius, ymin, radius);
  1224. X        }
  1225. X        else
  1226. X                {
  1227. X    fprintf(tfp, "newpath %d %d moveto", p->x, p->y);
  1228. X    while (q->next != NULL) {
  1229. X        p = q;
  1230. X        q = q->next;
  1231. X        fprintf(tfp, " %d %d lineto", p->x, p->y);
  1232. X        }
  1233. X        }
  1234. X    if (l->type == T_POLYLINE)
  1235. X        fprintf(tfp, " %d %d lineto stroke\n", q->x, q->y);
  1236. X    else
  1237. X        fprintf(tfp, " closepath ");
  1238. X    if (l->area_fill && (int)l->area_fill != DEFAULT)
  1239. X        fprintf(tfp, "gsave %6.3f setgray fill grestore ",
  1240. X            GRAYVAL(l->area_fill));
  1241. X    fprintf(tfp, "stroke\n");
  1242. X    reset_style(l->style, l->style_val);
  1243. X    if (l->for_arrow)
  1244. X        draw_arrow_head((double)p->x, (double)p->y, (double)q->x,
  1245. X            (double)q->y, l->for_arrow->ht, l->for_arrow->wid);
  1246. X    }
  1247. X
  1248. Xvoid genps_spline(s)
  1249. XF_spline    *s;
  1250. X{
  1251. X    if (int_spline(s))
  1252. X        genps_itp_spline(s);
  1253. X    else
  1254. X        genps_ctl_spline(s);
  1255. X    }
  1256. X
  1257. Xgenps_itp_spline(s)
  1258. XF_spline    *s;
  1259. X{
  1260. X    F_point        *p, *q;
  1261. X    F_control    *a, *b;
  1262. X
  1263. X    set_linewidth(s->thickness);
  1264. X    a = s->controls;
  1265. X    p = s->points;
  1266. X    if (s->back_arrow)
  1267. X        draw_arrow_head(a->rx, a->ry, (double)p->x,
  1268. X            (double)p->y, s->back_arrow->ht, s->back_arrow->wid);
  1269. X
  1270. X    set_style(s->style, s->style_val);
  1271. X    fprintf(tfp, "%% Interpolated spline\n");
  1272. X    fprintf(tfp, "newpath %d %d moveto\n", p->x, p->y);
  1273. X    for (q = p->next; q != NULL; p = q, q = q->next) {
  1274. X        b = a->next;
  1275. X        fprintf(tfp, "\t%.3f %.3f %.3f %.3f %d %d curveto\n",
  1276. X            a->rx, a->ry, b->lx, b->ly, q->x, q->y);
  1277. X        a = b;
  1278. X        b = b->next;
  1279. X        }
  1280. X    if (closed_spline(s)) fprintf(tfp, " closepath ");
  1281. X    if (s->area_fill && (int)s->area_fill != DEFAULT)
  1282. X        fprintf(tfp, "gsave %6.3f setgray fill grestore ",
  1283. X            GRAYVAL(s->area_fill));
  1284. X    fprintf(tfp, " stroke\n");
  1285. X    reset_style(s->style, s->style_val);
  1286. X
  1287. X    if (s->for_arrow)
  1288. X        draw_arrow_head(a->lx, a->ly, (double)p->x,
  1289. X            (double)p->y, s->for_arrow->ht, s->for_arrow->wid);
  1290. X    }
  1291. X
  1292. Xgenps_ctl_spline(s)
  1293. XF_spline    *s;
  1294. X{
  1295. X    double        a, b, c, d, x1, y1, x2, y2, x3, y3;
  1296. X    F_point        *p, *q;
  1297. X
  1298. X    /*
  1299. X    if (first) {
  1300. X        first = FALSE;
  1301. X        fprintf(tfp, "%s\n", SPLINE_PS);
  1302. X        }
  1303. X    */
  1304. X
  1305. X    p = s->points;
  1306. X    x1 = p->x; y1 = p->y;
  1307. X    p = p->next;
  1308. X    c = p->x; d = p->y;
  1309. X    set_linewidth(s->thickness);
  1310. X    x3 = a = (x1 + c) / 2;
  1311. X    y3 = b = (y1 + d) / 2;
  1312. X    if (s->back_arrow) {
  1313. X        draw_arrow_head(c, d, x1, y1, s->back_arrow->ht, s->back_arrow->wid);
  1314. X        }
  1315. X    set_style(s->style, s->style_val);
  1316. X    if (! closed_spline(s)) {
  1317. X        fprintf(tfp, "%% Open spline\n");
  1318. X        fprintf(tfp, "newpath %.3f %.3f moveto %.3f %.3f lineto\n",
  1319. X            x1, y1, x3, y3);
  1320. X        }
  1321. X    else {
  1322. X        fprintf(tfp, "%% Closed spline\n");
  1323. X        fprintf(tfp, "newpath %.3f %.3f moveto\n", a, b);
  1324. X        }
  1325. X    for (q = p->next; q != NULL; p = q, q = q->next) {
  1326. X        x1 = x3; y1 = y3;
  1327. X        x2 = c;  y2 = d;
  1328. X        c = q->x; d = q->y;
  1329. X        x3 = (x2 + c) / 2;
  1330. X        y3 = (y2 + d) / 2;
  1331. X        fprintf(tfp, "\t%.3f %.3f %.3f %.3f %.3f %.3f DrawSplineSection\n",
  1332. X            x1, y1, x2, y2, x3, y3);
  1333. X        }
  1334. X    /*
  1335. X    * At this point, (x2,y2) and (c,d) are the position of the 
  1336. X    * next-to-last and last point respectively, in the point list
  1337. X    */
  1338. X    if (closed_spline(s)) {
  1339. X        fprintf(tfp, "\t%.3f %.3f %.3f %.3f %.3f %.3f DrawSplineSection closepath ",
  1340. X            x3, y3, c, d, a, b);
  1341. X        }
  1342. X    else {
  1343. X        fprintf(tfp, "\t%.3f %.3f lineto ", c, d);
  1344. X        }
  1345. X    if (s->area_fill && (int)s->area_fill != DEFAULT)
  1346. X        fprintf(tfp, "gsave %6.3f setgray fill grestore ",
  1347. X            GRAYVAL(s->area_fill));
  1348. X    fprintf(tfp, "stroke\n");
  1349. X    reset_style(s->style, s->style_val);
  1350. X    if (s->for_arrow) {
  1351. X        draw_arrow_head(x2, y2, c, d, s->for_arrow->ht,
  1352. X                s->for_arrow->wid);
  1353. X        }
  1354. X    }
  1355. X
  1356. Xvoid genps_ellipse(e)
  1357. XF_ellipse    *e;
  1358. X{
  1359. X    set_linewidth(e->thickness);
  1360. X    set_style(e->style, e->style_val);
  1361. X    if (e->angle == 0)
  1362. X    {
  1363. X        fprintf(tfp, "%% Ellipse\n");
  1364. X        fprintf(tfp, "newpath %d %d %d %d 0 360 DrawEllipse ",
  1365. X              e->center.x, e->center.y, e->radiuses.x, e->radiuses.y);
  1366. X    }
  1367. X    else
  1368. X    {
  1369. X        fprintf(tfp, "%% Rotated Ellipse\n");
  1370. X        fprintf(tfp, "gsave\n");
  1371. X        fprintf(tfp, "%d %d translate\n",e->center.x, e->center.y);
  1372. X        fprintf(tfp, "%6.3f rotate\n",e->angle*180/M_PI);
  1373. X        fprintf(tfp, "newpath 0 0 %d %d 0 360 DrawEllipse ",
  1374. X         e->radiuses.x, e->radiuses.y);
  1375. X    }
  1376. X    if (e->area_fill && (int)e->area_fill != DEFAULT)
  1377. X        fprintf(tfp, "gsave %6.3f setgray fill grestore ",
  1378. X            GRAYVAL(e->area_fill));
  1379. X    fprintf(tfp, "stroke\n");
  1380. X    if (e->angle != 0)
  1381. X        fprintf(tfp, "grestore\n");
  1382. X    reset_style(e->style, e->style_val);
  1383. X    }
  1384. X
  1385. X#define    TEXT_PS        "\
  1386. X/%s findfont %.3f scalefont setfont\n\
  1387. X"
  1388. Xvoid genps_text(t)
  1389. XF_text    *t;
  1390. X{
  1391. X    char        *cp;
  1392. X
  1393. X    fprintf(tfp, TEXT_PS, PSFONT(t->font), PSFONTSIZE(t->size));
  1394. X
  1395. X    fprintf(tfp, "%d %d moveto \n", t->base_x,  t->base_y);
  1396. X    if (coord_system == 2) fprintf(tfp, "1 -1 scale\n");
  1397. X
  1398. X    /* this loop escapes characters '(', ')', and '\' */
  1399. X    fputc('(', tfp);
  1400. X    for(cp = t->cstring; *cp; cp++) {
  1401. X          if (strchr("()\\", *cp)) fputc('\\', tfp);
  1402. X          fputc(*cp, tfp);
  1403. X          }
  1404. X    fputc(')', tfp);
  1405. X
  1406. X    if ((t->type == T_CENTER_JUSTIFIED) || (t->type == T_RIGHT_JUSTIFIED)){
  1407. X
  1408. X          fprintf(tfp, "dup stringwidth pop ");
  1409. X        if (t->type == T_CENTER_JUSTIFIED) fprintf(tfp, "2 div ");
  1410. X        fprintf(tfp, "neg 0 rmoveto ");
  1411. X              }
  1412. X
  1413. X    else if ((t->type != T_LEFT_JUSTIFIED) && (t->type != DEFAULT))
  1414. X        fprintf(stderr, "Text incorrectly positioned\n");
  1415. X
  1416. X    fprintf(tfp, " gsave %6.3f rotate show grestore ", t->angle*180/M_PI);
  1417. X    if (coord_system == 2) fprintf(tfp, "1 -1 scale\n");
  1418. X
  1419. X    }
  1420. X
  1421. Xvoid genps_arc(a)
  1422. XF_arc    *a;
  1423. X{
  1424. X    double        angle1, angle2, dx, dy, radius, x, y;
  1425. X    double        cx, cy, sx, sy, ex, ey;
  1426. X    int        direction;
  1427. X
  1428. X    cx = a->center.x; cy = a->center.y;
  1429. X    sx = a->point[0].x; sy = a->point[0].y;
  1430. X    ex = a->point[2].x; ey = a->point[2].y;
  1431. X
  1432. X    if (coord_system == 2)
  1433. X        direction = !a->direction;
  1434. X    else
  1435. X        direction = a->direction;
  1436. X    set_linewidth(a->thickness);
  1437. X    if (a->for_arrow) {
  1438. X        arc_tangent(cx, cy, ex, ey, direction, &x, &y);
  1439. X        draw_arrow_head(x, y, ex, ey, a->for_arrow->ht, a->for_arrow->wid);
  1440. X        }
  1441. X    if (a->back_arrow) {
  1442. X        arc_tangent(cx, cy, sx, sy, !direction, &x, &y);
  1443. X        draw_arrow_head(x, y, sx, sy, a->back_arrow->ht, a->back_arrow->wid);
  1444. X        }
  1445. X    dx = cx - sx;
  1446. X    dy = cy - sy;
  1447. X    radius = hypot(dx, dy);
  1448. X    angle1 = atan2(sy-cy, sx-cx) * 180 / M_PI;
  1449. X    angle2 = atan2(ey-cy, ex-cx) * 180 / M_PI;
  1450. X    /* direction = 1 -> Counterclockwise */
  1451. X    set_style(a->style, a->style_val);
  1452. X    fprintf(tfp, "newpath %.3f %.3f %.3f %.3f %.3f %s\n",
  1453. X        cx, cy, radius, angle1, angle2,
  1454. X        ((direction == 1) ? "arc" : "arcn"));
  1455. X    if (a->area_fill && (int)a->area_fill != DEFAULT)
  1456. X        fprintf(tfp, "gsave %6.3f setgray fill grestore ",
  1457. X            GRAYVAL(a->area_fill));
  1458. X    fprintf(tfp, "stroke\n");
  1459. X    reset_style(a->style, a->style_val);
  1460. X    }
  1461. X
  1462. Xstatic arc_tangent(x1, y1, x2, y2, direction, x, y)
  1463. Xdouble    x1, y1, x2, y2, *x, *y;
  1464. Xint    direction;
  1465. X{
  1466. X    if (direction) { /* counter clockwise  */
  1467. X        *x = x2 + (y2 - y1);
  1468. X        *y = y2 - (x2 - x1);
  1469. X        }
  1470. X    else {
  1471. X        *x = x2 - (y2 - y1);
  1472. X        *y = y2 + (x2 - x1);
  1473. X        }
  1474. X    }
  1475. X
  1476. X/*    draw arrow heading from (x1, y1) to (x2, y2)    */
  1477. X
  1478. Xstatic draw_arrow_head(x1, y1, x2, y2, arrowht, arrowwid)
  1479. Xdouble    x1, y1, x2, y2, arrowht, arrowwid;
  1480. X{
  1481. X    double    x, y, xb, yb, dx, dy, l, sina, cosa;
  1482. X    double    xc, yc, xd, yd;
  1483. X
  1484. X    dx = x2 - x1;  dy = y1 - y2;
  1485. X    l = hypot(dx, dy);
  1486. X    sina = dy / l;  cosa = dx / l;
  1487. X    xb = x2*cosa - y2*sina;
  1488. X    yb = x2*sina + y2*cosa;
  1489. X    x = xb - arrowht;
  1490. X    y = yb - arrowwid / 2;
  1491. X    xc = x*cosa + y*sina;
  1492. X    yc = -x*sina + y*cosa;
  1493. X    y = yb + arrowwid / 2;
  1494. X    xd = x*cosa + y*sina;
  1495. X    yd = -x*sina + y*cosa;
  1496. X    fprintf(tfp, "newpath %.3f %.3f moveto %.3f %.3f lineto %.3f %.3f lineto stroke\n",
  1497. X        xc, yc, x2, y2, xd, yd);
  1498. X    }
  1499. X
  1500. Xstatic ellipse_exist(ob)
  1501. XF_compound    *ob;
  1502. X{
  1503. X    F_compound    *c;
  1504. X
  1505. X    if (NULL != ob->ellipses) return(1);
  1506. X
  1507. X    for (c = ob->compounds; c != NULL; c = c->next) {
  1508. X        if (ellipse_exist(c)) return(1);
  1509. X        }
  1510. X
  1511. X    return(0);
  1512. X    }
  1513. X
  1514. Xstatic normal_spline_exist(ob)
  1515. XF_compound    *ob;
  1516. X{
  1517. X    F_spline    *s;
  1518. X    F_compound    *c;
  1519. X
  1520. X    for (s = ob->splines; s != NULL; s = s->next) {
  1521. X        if (normal_spline(s)) return(1);
  1522. X        }
  1523. X
  1524. X    for (c = ob->compounds; c != NULL; c = c->next) {
  1525. X        if (normal_spline_exist(c)) return(1);
  1526. X        }
  1527. X
  1528. X    return(0);
  1529. X    }
  1530. X
  1531. Xstruct driver dev_ps = {
  1532. X         genps_option,
  1533. X    genps_start,
  1534. X    genps_arc,
  1535. X    genps_ellipse,
  1536. X    genps_line,
  1537. X    genps_spline,
  1538. X    genps_text,
  1539. X    genps_end,
  1540. X    INCLUDE_TEXT
  1541. X};
  1542. END_OF_FILE
  1543. if test 17407 -ne `wc -c <'transfig/fig2dev/dev/genps.c'`; then
  1544.     echo shar: \"'transfig/fig2dev/dev/genps.c'\" unpacked with wrong size!
  1545. fi
  1546. # end of 'transfig/fig2dev/dev/genps.c'
  1547. fi
  1548. echo shar: End of archive 4 \(of 6\).
  1549. cp /dev/null ark4isdone
  1550. MISSING=""
  1551. for I in 1 2 3 4 5 6 ; do
  1552.     if test ! -f ark${I}isdone ; then
  1553.     MISSING="${MISSING} ${I}"
  1554.     fi
  1555. done
  1556. if test "${MISSING}" = "" ; then
  1557.     echo You have unpacked all 6 archives.
  1558.     rm -f ark[1-9]isdone
  1559. else
  1560.     echo You still need to unpack the following archives:
  1561.     echo "        " ${MISSING}
  1562. fi
  1563. ##  End of shell archive.
  1564. exit 0
  1565.