home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / cshar_ps.sit < prev    next >
Encoding:
Text File  |  1988-06-20  |  13.7 KB  |  601 lines

  1. 18-Jun-88 14:38:44-MDT,14616;000000000000
  2. Return-Path: <u-lchoqu%sunset@cs.utah.edu>
  3. Received: from cs.utah.edu by SIMTEL20.ARPA with TCP; Sat, 18 Jun 88 14:38:17 MDT
  4. Received: by cs.utah.edu (5.54/utah-2.0-cs)
  5.     id AA22539; Sat, 18 Jun 88 14:38:18 MDT
  6. Received: by sunset.utah.edu (5.54/utah-2.0-leaf)
  7.     id AA24721; Sat, 18 Jun 88 14:38:14 MDT
  8. Date: Sat, 18 Jun 88 14:38:14 MDT
  9. From: u-lchoqu%sunset@cs.utah.edu (Lee Choquette)
  10. Message-Id: <8806182038.AA24721@sunset.utah.edu>
  11. To: rthum@simtel20.arpa
  12. Subject: PseudoPS.c.shar
  13.  
  14. #! /bin/sh
  15. #
  16. # This is a shell archive.  Save this into a file, edit it
  17. # and delete all lines above this comment.  Then give this
  18. # file to sh by executing the command "sh file".  The files
  19. # will be extracted into the current directory owned by
  20. # you with default permissions.
  21. #
  22. # The files contained herein are:
  23. #
  24. #    6 PseudoPS.c
  25. #    1 PseudoPS.h
  26. #    5 ParsePS.c
  27. #    1 stack.c
  28. #
  29. echo 'Extracting PseudoPS.c'
  30. if test -f PseudoPS.c; then echo 'shar: will not overwrite PseudoPS.c'; else
  31. sed 's/^X//' << '________This_Is_The_END________' > PseudoPS.c
  32. X/*
  33. X *  PseudoPS -- a small PostScript interpreter (1987)
  34. X *
  35. X *  Written by        Craig E Rasmussen
  36. X *                    Center for Atmospheric and Space Science
  37. X *                    Utah State University
  38. X *                    Logan, Utah 84322-4405
  39. X *                    (801) 750-2967
  40. X *
  41. X *    email        -    cer@star.stanford.edu
  42. X *                -    theory::craig on the SPAN network
  43. X *
  44. X *  with the use of SimpleTools.c (c) Erik Kilk 1986
  45. X * 
  46. X *  This program may be freely distributed and modified as long as this
  47. X *  header remains in place.
  48. X *
  49. X *  Link with LightSpeedC modules
  50. X *      -    MacTraps
  51. X *      -    math
  52. X *      -    stdio
  53. X *      -    strings
  54. X *      -    unix
  55. X */
  56. X#include <Quickdraw.h>
  57. X#include "simple.h"        /* SimpleTools header file    */
  58. X#include "PseudoPS.h"
  59. X
  60. XFILE *fpPS, *fpErr;
  61. Xchar PStext[STRINGLENGTH];
  62. Xint PSopen = FALSE;
  63. X
  64. Xinwindow (x, y)            /* executed when click in our window */
  65. Xint x, y;
  66. X{
  67. X  Point m;
  68. X    while ( StillDown() ) {    /* while the Button is pressed */
  69. X        GetMouse (&m);        /* waste time */
  70. X    }
  71. X
  72. X}
  73. X
  74. Xnop()
  75. X{
  76. X}
  77. X
  78. Xno_edit ()            /* turn off edit menu (on activation) */
  79. X{
  80. X  menu ("Edit", "", itemdisable);
  81. X}
  82. X
  83. Xyes_edit ()            /* turn on edit menu (on deactivation) */
  84. X{
  85. X  menu ("Edit", "", itemenable);
  86. X}
  87. X
  88. XaboutPs()            /* About message */
  89. X{
  90. X    char messageStr[255];
  91. X    strcpy(messageStr,"PseudoPS -- by Craig E Rasmussen (1987)\r");
  92. X    strcat(messageStr,"A very small PostScript interpreter.\r");
  93. X    strcat(messageStr,"Please freely distribute and improve.\r");
  94. X    if (message(messageStr)) {
  95. X        strcpy(messageStr,"Programmed with the aid of SimpleTools\r");
  96. X        strcat(messageStr,"(c) Erik Kilk 1986");
  97. X        message(messageStr);
  98. X    }
  99. X}
  100. X
  101. XOpenPSfile()            /* to be executed File-open menu is selected */
  102. X{
  103. X    char PSfile[255], *PtoCstr();
  104. X    int i, n, c;
  105. X    
  106. X    if (PSopen == TRUE) {
  107. X        SysBeep(10);
  108. X        message("a file is already open");
  109. X        return;
  110. X    }
  111. X    if (getfile("TEXT", PSfile)) {
  112. X        if ((fpPS = fopen(PtoCstr(PSfile), "r")) != NULL) {
  113. X            PSopen = TRUE;
  114. X            window (PSfile, WXTOP, WYTOP, WXBOT, WYBOT
  115. X                , no_edit, yes_edit, nop, inwindow);
  116. X            menu ("File", "Open.../O", itemdisable);
  117. X            SetTransforms(WXTOP, WYTOP, WXBOT, WYBOT);
  118. X        }
  119. X        else SysBeep(10);
  120. X    }
  121. X    else SysBeep(10);
  122. X}
  123. X
  124. X
  125. Xsetup ()            /* Setup the menus and windows */
  126. X{
  127. X    menu (applestring, "About PsuedoPS...", aboutPs);
  128. X    menu (applestring, "About PsuedoPS...", itemenable);
  129. X    menu ("File", "Open.../O", OpenPSfile);
  130. X    simplequits ();
  131. X    
  132. X    if ((fpErr = fopen("PS.errors", "w")) == NULL) {
  133. X        SysBeep(0);
  134. X        exit();
  135. X    }
  136. X}
  137. X
  138. Xmain ()
  139. X{
  140. X    
  141. X    simpletools ("About PsuedoPS...");        /* Initialize SimpleTools    */
  142. X    setup ();
  143. X    
  144. X    while (1) {
  145. X        simpleevents ();                    /* Handle all events        */
  146. X        PostScript();
  147. X    }
  148. X}
  149. X
  150. X
  151. XPostScript()
  152. X{
  153. X    char s[STRINGLENGTH];
  154. X    int status;
  155. X    float atof();
  156. X
  157. X    if (!PSopen) return;    
  158. X    if ((status = getPS(s, STRINGLENGTH))) {
  159. X        if (status == -1)
  160. X            fprintf(fpErr, "PostScript buffer overflow\n");
  161. X        else if (status == -2) {
  162. X            fprintf(fpErr, "unterminated PostScript string\n");
  163. X            strcpy(PStext, s);
  164. X        }
  165. X        else if (status == 2) strcpy(PStext, s);
  166. X        else if (IsInteger(s)) push( (float)atoi(s) );
  167. X        else if (IsFloat(s)) push(atof(s));
  168. X        else ParsePS(s);
  169. X    }
  170. X}
  171. X
  172. X
  173. X/*
  174. X *  Copy PostScript token to s if present, return(1); else return(0) if
  175. X *  no token present or return(-1) if buffer overflow.
  176. X */
  177. XgetPS(s, max)
  178. Xchar *s;
  179. Xint max;
  180. X{
  181. X    char *sPtr;
  182. X    int count = 0, c;
  183. X
  184. X    sPtr = s;
  185. X    while (WhiteSpace(c = getc(fpPS)));        /* remove leading blanks */
  186. X
  187. X    if (c == '(') return(getPSstr(s, max));    /* get PostScript string */
  188. X    
  189. X    while (!WhiteSpace(c)) {                /* copy character to s */
  190. X        if (c != EOF) {
  191. X            if (++count < max) *sPtr++ = c;    /* copy c if space available */
  192. X            else {                            /* buffer overflow */
  193. X                *sPtr = '\0';
  194. X                return(-1);
  195. X            }
  196. X        }
  197. X        else {                                /* end of file reached */
  198. X            fclose(fpPS);
  199. X            PSopen = FALSE;
  200. X            menu ("File", "Open.../O", itemenable);
  201. X            if (count > 0) {                /* token present */
  202. X                *sPtr = '\0';
  203. X                return(1);
  204. X            }
  205. X            else return(0);                    /* no token present */
  206. X        }
  207. X        c = getc(fpPS);
  208. X    }
  209. X    *sPtr = '\0';
  210. X    return(1);                                /* token present */
  211. X}
  212. X
  213. X
  214. X/*
  215. X *  Copy PostScript string to s if present, return(2); else return(0) if
  216. X *  no string not properly terminated or return(-1) if buffer overflow.
  217. X *  Character '(' already found in input stream.
  218. X */
  219. XgetPSstr(s, max)
  220. Xchar *s;
  221. Xint max;
  222. X{
  223. X    char *sPtr;
  224. X    int count = 0, excess = 0, c;
  225. X
  226. X    sPtr = s;
  227. X    c = getc(fpPS);
  228. X    
  229. X    while (c != ')'  ||  excess > 0) {        /* copy character to s */
  230. X        if (c != EOF) {
  231. X            if (++count < max) {            /* copy c if space available */
  232. X                *sPtr++ = c;
  233. X                if (c == '(') ++excess;        /* increase unbalanced ( count */
  234. X                else if (c == ')') --excess;
  235. X            }
  236. X            else {                            /* buffer overflow */
  237. X                *sPtr = '\0';
  238. X                return(-1);
  239. X            }
  240. X        }
  241. X        else {                                /* end of file reached */
  242. X            fclose(fpPS);
  243. X            PSopen = FALSE;
  244. X            menu ("File", "Open.../O", itemenable);
  245. X            if (count > 0) {                /* string present */
  246. X                *sPtr = '\0';                /* but not terminated by ')' */
  247. X                return(-2);
  248. X            }
  249. X            else return(0);                    /* no string present */
  250. X        }
  251. X        c = getc(fpPS);
  252. X    }
  253. X    *sPtr = '\0';
  254. X    return(2);                                /* string present */
  255. X}
  256. X
  257. X
  258. XIsFloat(s)
  259. Xchar *s;
  260. X{
  261. X    char c;
  262. X
  263. X    while (WhiteSpace(*s)) s++;
  264. X    if (*s == '-' || *s == '+') s++;    
  265. X    while (c = *s++)
  266. X        if (c < '0' || c > '9') {
  267. X            if (c == '.') break;
  268. X            else return (0);
  269. X        }
  270. X    if (c != '.') return(0);                    /* no decimal point */
  271. X    while (c = *s++) if (c < '0'  ||  c > '9') return (0); /* decimal part */
  272. X    return (1);
  273. X} 
  274. X
  275. X
  276. XIsInteger(s)
  277. Xchar *s;
  278. X{
  279. X    char c;
  280. X
  281. X    while (WhiteSpace(*s)) s++;
  282. X    if (*s == '-' || *s == '+') s++;    
  283. X    while (c = *s++) if (c < '0'  ||  c > '9') return (0);
  284. X    return (1);
  285. X} 
  286. X
  287. XWhiteSpace(c)
  288. X{
  289. X    switch (c) {
  290. X        case ' ':
  291. X        case '\n':
  292. X        case '\t':
  293. X        case '\v':
  294. X        case '\f':
  295. X        case '\r':
  296. X            return (1);
  297. X        default:
  298. X            return (0);
  299. X    }
  300. ________This_Is_The_END________
  301. if test `wc -l < PseudoPS.c` -ne 271; then
  302.     echo 'shar: PseudoPS.c was damaged during transit'
  303.   echo '      (should have been 271 bytes)'
  304. fi
  305. fi        ; : end of overwriting check
  306. echo 'Extracting PseudoPS.h'
  307. if test -f PseudoPS.h; then echo 'shar: will not overwrite PseudoPS.h'; else
  308. sed 's/^X//' << '________This_Is_The_END________' > PseudoPS.h
  309. X#define STRINGLENGTH 255
  310. X#define WXTOP 3            /* 20  */
  311. X#define WYTOP 40        /* 50  */
  312. X#define WXBOT 508        /* 490 */
  313. X#define WYBOT 338        /* 325 */
  314. X
  315. Xextern FILE *fpErr;
  316. ________This_Is_The_END________
  317. if test `wc -l < PseudoPS.h` -ne 7; then
  318.     echo 'shar: PseudoPS.h was damaged during transit'
  319.   echo '      (should have been 7 bytes)'
  320. fi
  321. fi        ; : end of overwriting check
  322. echo 'Extracting ParsePS.c'
  323. if test -f ParsePS.c; then echo 'shar: will not overwrite ParsePS.c'; else
  324. sed 's/^X//' << '________This_Is_The_END________' > ParsePS.c
  325. X/*
  326. X *  PseudoPS -- a small PostScript interpreter (1987)
  327. X *
  328. X *  Written by        Craig E Rasmussen
  329. X *                    Center for Atmospheric and Space Science
  330. X *                    Utah State University
  331. X *                    Logan, Utah 84322-4405
  332. X *                    (801) 750-2967
  333. X *
  334. X *    email        -    cer@star.stanford.edu
  335. X *                -    theory::craig on the SPAN network
  336. X */
  337. X
  338. X#include <Quickdraw.h>
  339. X#include <stdio.h>
  340. X#include "PseudoPS.h"
  341. X
  342. X#define DTR .01745329252
  343. X
  344. Xfloat xShift, yShift, xScale, yScale, rotate, cosTH, sinTH;
  345. Xint HandleAllocated = FALSE, RegionOpen = FALSE;
  346. XRgnHandle region;
  347. X
  348. X
  349. XParsePS(s)
  350. Xchar *s;
  351. X{
  352. X    Rect rect;
  353. X    float x1, x2, x3, x4, x5, left, top, right, bottom;
  354. X    int pop();
  355. X    char outstring[80];
  356. X
  357. X    switch (*s) {
  358. X        case 'a':
  359. X            if (strcmp(s,"arc") == 0) {
  360. X                if (pop(&x5) != 0) StackError();
  361. X                if (pop(&x4) != 0) StackError();
  362. X                if (pop(&x3) != 0) StackError();
  363. X                if (pop(&x2) != 0) StackError();
  364. X                if (pop(&x1) != 0) StackError();
  365. X                left   = x1 - x3;
  366. X                top    = x2 + x3;
  367. X                right  = x1 + x3;
  368. X                bottom = x2 - x3;
  369. X                transform(&left, &top);
  370. X                transform(&right, &bottom);
  371. X                SetRect(&rect,(int)left,(int)top,(int)right,(int)bottom);
  372. X                FrameArc(&rect, (int)(90.-x4), (int)(x4-x5));
  373. X            }
  374. X            else PSerror(s);
  375. X            break;
  376. X        case 'c':
  377. X            if (strcmp(s,"closepath") == 0) {
  378. X                if (!HandleAllocated) PSerror("closepath (no RegionHandle)");
  379. X                else if (!RegionOpen) PSerror("closepath (no current path)");
  380. X                else {
  381. X                    CloseRgn(region);
  382. X                    RegionOpen = FALSE;
  383. X                }
  384. X            }
  385. X            else PSerror(s);
  386. X            break;
  387. X        case 'f':
  388. X            if (strcmp(s,"fill") == 0) {
  389. X                if (!HandleAllocated) PSerror("fill (no RegionHandle)");
  390. X                else {
  391. X                    PaintRgn(region);
  392. X                    DisposeRgn(region);
  393. X                    HandleAllocated = FALSE;
  394. X                }
  395. X            }
  396. X            else PSerror(s);
  397. X            break;
  398. X        case 'l':
  399. X            if (strcmp(s,"lineto") == 0) {
  400. X                if (pop(&x2) != 0) StackError();
  401. X                if (pop(&x1) != 0) StackError();
  402. X                transform(&x1, &x2);
  403. X                LineTo((int)x1, (int)x2);
  404. X            }
  405. X            else PSerror(s);
  406. X            break;
  407. X        case 'm':
  408. X            if (strcmp(s,"moveto") == 0) {
  409. X                if (pop(&x2) != 0) StackError();
  410. X                if (pop(&x1) != 0) StackError();
  411. X                transform(&x1, &x2);
  412. X                MoveTo((int)x1, (int)x2);
  413. X            }
  414. X            else PSerror(s);
  415. X            break;
  416. X        case 'n':
  417. X            if (strcmp(s,"newpath") == 0) {
  418. X                if (HandleAllocated) {
  419. X                    if (RegionOpen) {
  420. X                        PSerror("newpath (region already open)");
  421. X                        CloseRgn(region);
  422. X                    }
  423. X                    PSerror("newpath (RegionHandle already created)");
  424. X                    DisposeRgn(region);
  425. X                }
  426. X                region = NewRgn();
  427. X                OpenRgn();
  428. X                HandleAllocated = TRUE;
  429. X                RegionOpen = TRUE;
  430. X            }
  431. X            else PSerror(s);
  432. X            break;
  433. X        case 'r':
  434. X            if (strcmp(s,"rlineto") == 0) {
  435. X                if (pop(&x2) != 0) StackError();
  436. X                if (pop(&x1) != 0) StackError();
  437. X                scale(&x1, &x2);
  438. X                Line((int)x1, (int)x2);
  439. X            }
  440. X            else if (strcmp(s,"rmoveto") == 0) {
  441. X                if (pop(&x2) != 0) StackError();
  442. X                if (pop(&x1) != 0) StackError();
  443. X                scale(&x1, &x2);
  444. X                Move((int)x1, (int)x2);
  445. X            }
  446. X            else if (strcmp(s,"rotate") == 0) {
  447. X                if (pop(&x1) != 0) StackError();
  448. X                rotate = x1;
  449. X                cosTH = 0.0;  /*cos(rotate*DTR);*/
  450. X                sinTH = 1.0;  /*sin(rotate*DTR);*/
  451. X            }
  452. X            else PSerror(s);
  453. X            break;
  454. X        case 's':
  455. X            if (strcmp(s,"scale") == 0) {
  456. X                if (pop(&x2) != 0) StackError();
  457. X                if (pop(&x1) != 0) StackError();
  458. X                xScale *= x1;
  459. X                yScale *= x2;
  460. X            }
  461. X            else if (strcmp(s,"scalefont") == 0) {
  462. X                if (pop(&x1) != 0) StackError();
  463. X                TextSize((int)x1);
  464. X            }
  465. X            else if (strcmp(s,"setgray") == 0) {
  466. X                if (pop(&x1) != 0) StackError();
  467. X                if (x1 > .875) PenPat(white);
  468. X                else if (x1 > .625) PenPat(ltGray);
  469. X                else if (x1 > .375) PenPat(gray);
  470. X                else if (x1 > .125) PenPat(dkGray);
  471. X                else PenPat(black);
  472. X            }
  473. X            else if (strcmp(s,"setlinewidth") == 0) {
  474. X                if (pop(&x1) != 0) StackError();
  475. X                PenSize((int)x1, (int)x1);
  476. X            }
  477. X            else if (strcmp(s,"show") == 0) {
  478. X                CtoPstr(PStext);
  479. X                DrawString(PStext);
  480. X            }
  481. X            else if (strcmp(s,"showpage") == 0) break;        /* nop */
  482. X            else if (strcmp(s,"stroke") == 0) {
  483. X                if (!HandleAllocated) PSerror("stroke (no RegionHandle)");
  484. X                else {
  485. X                    FrameRgn(region);
  486. X                    DisposeRgn(region);
  487. X                    HandleAllocated = FALSE;
  488. X                }
  489. X            }
  490. X            else PSerror(s);
  491. X            break;
  492. X        case 't':
  493. X            if (strcmp(s,"translate") == 0) {
  494. X                if (pop(&x2) != 0) StackError();
  495. X                if (pop(&x1) != 0) StackError();
  496. X                xShift += x1;
  497. X                yShift += x2;
  498. X            }
  499. X            else PSerror(s);
  500. X            break;
  501. X        default:
  502. X            PSerror(s);
  503. X    }
  504. X}
  505. X
  506. XPSerror(s)
  507. Xchar *s;
  508. X{
  509. X/*    SerialPutS("\n%offending command -> ");
  510. X    SerialPutS(s);
  511. X    SerialPutChar('\n');  */
  512. X    fprintf(fpErr, "%%offending command -> %s\n", s);
  513. X}
  514. X
  515. X
  516. XStackError()
  517. X{
  518. X/*    SerialPutS("\n%stack error\n");  */
  519. X    fprintf(fpErr, "\n%%stack error\n");
  520. X}
  521. X
  522. X
  523. Xtransform(x, y)
  524. Xfloat *x, *y;
  525. X{
  526. X    *x += xShift;
  527. X    *y += yShift;    
  528. X    scale(x, y);
  529. X}
  530. X
  531. X
  532. Xscale1D(r)
  533. Xfloat *r;
  534. X{
  535. X    *r *= sqrt(xScale*xScale + yScale*yScale);
  536. X}
  537. X
  538. X
  539. XTransformAngle(t)
  540. Xfloat *t;
  541. X{
  542. X    *t += rotate;
  543. X}
  544. X
  545. X
  546. Xscale(x, y)
  547. Xfloat *x, *y;
  548. X{
  549. X    *x *=  xScale;
  550. X    *y *= -yScale;                    /* change increasing y to upwards */
  551. X}
  552. X
  553. XSetTransforms(wXtop, wYtop, wXbot, wYbot)
  554. Xint wXtop, wYtop, wXbot, wYbot;
  555. X{
  556. X    xShift = 0.0; yShift = 0.0; xScale = 1.0; yScale = 1.0;
  557. X    rotate = 0.0; cosTH = 1.0; sinTH = 0.0;
  558. X    SetOrigin(0, wYtop - wYbot);
  559. X    PenPat(black);
  560. X    PenSize(1,1);
  561. X}
  562. X
  563. ________This_Is_The_END________
  564. if test `wc -l < ParsePS.c` -ne 238; then
  565.     echo 'shar: ParsePS.c was damaged during transit'
  566.   echo '      (should have been 238 bytes)'
  567. fi
  568. fi        ; : end of overwriting check
  569. echo 'Extracting stack.c'
  570. if test -f stack.c; then echo 'shar: will not overwrite stack.c'; else
  571. sed 's/^X//' << '________This_Is_The_END________' > stack.c
  572. X#define    STKSIZE    10
  573. X
  574. Xfloat stack[STKSIZE], *StkPtr = stack;
  575. X
  576. Xpush(x)
  577. Xfloat x;
  578. X{
  579. X    if (StkPtr >= stack + STKSIZE) return(-1);    /* stack overflow */
  580. X    *(++StkPtr) = x;
  581. X    return(0);
  582. X}
  583. X
  584. Xpop(x)
  585. Xfloat *x;
  586. X{        
  587. X    if (StkPtr < stack) return(-1);        /* stack underflow */
  588. X    *x = *StkPtr--;
  589. X    return(0);
  590. X}
  591. ________This_Is_The_END________
  592. if test `wc -l < stack.c` -ne 19; then
  593.     echo 'shar: stack.c was damaged during transit'
  594.   echo '      (should have been 19 bytes)'
  595. fi
  596. fi        ; : end of overwriting check
  597. exit 0
  598.