home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFIG / TRANSFIG.2 / TRANSFIG / transfig / fig2dev / read1_3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-14  |  10.7 KB  |  473 lines

  1. /*
  2.  * TransFig: Facility for Translating Fig code
  3.  * Copyright (c) 1985 Supoj Sutantavibul
  4.  * Copyright (c) 1991 Micah Beck
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation. The authors make no representations about the suitability 
  11.  * of this software for any purpose.  It is provided "as is" without express 
  12.  * or implied warranty.
  13.  *
  14.  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  */
  23.  
  24. /*******************************************************************/
  25. /***************       Read version 1.3 format       ***************/
  26. /*******************************************************************/
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <errno.h>
  31. #include "alloc.h"
  32. #include "object.h"
  33.  
  34. /*******    Fig 1.3 subtype of objects    *******/
  35. #define            DRAW_ELLIPSE_BY_RAD     1
  36. #define            DRAW_ELLIPSE_BY_DIA     2
  37. #define            DRAW_CIRCLE_BY_RAD     3
  38. #define            DRAW_CIRCLE_BY_DIA     4
  39. #define            DRAW_CIRCULAR_ARC    5
  40. #define            DRAW_POLYLINE        6
  41. #define            DRAW_BOX        7
  42. #define            DRAW_POLYGON        8
  43. #define            DRAW_TEXT        9
  44. #define            DRAW_SPLINE        10
  45. #define            DRAW_CLOSEDSPLINE    11
  46. #define            DRAW_COMPOUND        13
  47.  
  48. extern F_arrow        *forward_arrow(), *backward_arrow();
  49. extern int        figure_modified;
  50. extern int        errno;
  51. extern char        *sys_errlist[];
  52. extern int        sys_nerr, errno;
  53.  
  54. static F_ellipse    *read_ellipseobject();
  55. static F_line        *read_lineobject();
  56. static F_text        *read_textobject();
  57. static F_spline        *read_splineobject();
  58. static F_arc        *read_arcobject();
  59. static F_compound    *read_compoundobject();
  60.  
  61. extern int        line_no;
  62. extern int        num_object;
  63.  
  64. int
  65. read_1_3_objects(fp, obj)
  66. FILE        *fp;
  67. F_compound    *obj;
  68. {
  69.     F_ellipse    *e, *le = NULL;
  70.     F_line        *l, *ll = NULL;
  71.     F_text        *t, *lt = NULL;
  72.     F_spline    *s, *ls = NULL;
  73.     F_arc        *a, *la = NULL;
  74.     F_compound    *c, *lc = NULL;
  75.     int         n;
  76.     int         object, pixperinch, canvaswid, canvasht, coord_sys;
  77.  
  78.     n = fscanf(fp,"%d%d%d%d\n", &pixperinch, &coord_sys, &canvaswid, &canvasht);
  79.     if (n != 4) {
  80.         put_msg("Incorrect format in the first line in input file");
  81.         return(-1);
  82.         }
  83.     obj->nwcorner.x = pixperinch;
  84.     obj->nwcorner.y = coord_sys;
  85.     while (fscanf(fp, "%d", &object) == 1) {
  86.         switch (object) {
  87.         case O_POLYLINE :
  88.             if ((l = read_lineobject(fp)) == NULL) return(-1);
  89.             if (ll)
  90.             ll = (ll->next = l);
  91.             else
  92.             ll = obj->lines = l;
  93.             num_object++;
  94.             break;
  95.         case O_SPLINE :
  96.             if ((s = read_splineobject(fp)) == NULL) return(-1);
  97.             if (ls)
  98.             ls = (ls->next = s);
  99.             else
  100.             ls = obj->splines = s;
  101.             num_object++;
  102.             break;
  103.         case O_ELLIPSE :
  104.             if ((e = read_ellipseobject(fp)) == NULL) return(-1);
  105.             if (le)
  106.             le = (le->next = e);
  107.             else
  108.             le = obj->ellipses = e;
  109.             num_object++;
  110.             break;
  111.         case O_ARC :
  112.             if ((a = read_arcobject(fp)) == NULL) return(-1);
  113.             if (la)
  114.             la = (la->next = a);
  115.             else
  116.             la = obj->arcs = a;
  117.             num_object++;
  118.             break;
  119.         case O_TEXT :
  120.             if ((t = read_textobject(fp)) == NULL) return(-1);
  121.             if (lt)
  122.             lt = (lt->next = t);
  123.             else
  124.             lt = obj->texts = t;
  125.             num_object++;
  126.             break;
  127.         case O_COMPOUND :
  128.             if ((c = read_compoundobject(fp)) == NULL) return(-1);
  129.             if (lc)
  130.             lc = (lc->next = c);
  131.             else
  132.             lc = obj->compounds = c;
  133.             num_object++;
  134.             break;
  135.         default:
  136.             put_msg("Incorrect object code %d", object);
  137.             return(-1);
  138.         } /*  switch */
  139.         } /*  while */
  140.     if (feof(fp))
  141.         return(0);
  142.     else
  143.         return(errno);
  144.     }
  145.  
  146. static F_arc *
  147. read_arcobject(fp)
  148. FILE    *fp;
  149. {
  150.     F_arc    *a;
  151.     int    f, b, h, w, n;
  152.  
  153.     Arc_malloc(a);
  154.     a->type = T_3_POINTS_ARC;
  155.           a->color = BLACK_COLOR;
  156.     a->depth = 0;
  157.     a->pen = 0;
  158.     a->for_arrow = NULL;
  159.     a->back_arrow = NULL;
  160.     a->next = NULL;
  161.     n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %lf %lf %d %d %d %d %d %d\n",
  162.         &a->type, &a->style, &a->thickness, 
  163.         &a->style_val, &a->direction, &f, &b,
  164.         &h, &w, &a->center.x, &a->center.y, 
  165.         &a->point[0].x, &a->point[0].y, 
  166.         &a->point[1].x, &a->point[1].y, 
  167.         &a->point[2].x, &a->point[2].y);
  168.     if (n != 17) {
  169.         put_msg("incomplete arc data");
  170.         free((char*)a);
  171.         return(NULL);
  172.         }
  173.     if (f) {
  174.         a->for_arrow = forward_arrow();
  175.         a->for_arrow->wid = w;
  176.         a->for_arrow->ht = h;
  177.         }
  178.     if (b) {
  179.         a->back_arrow = backward_arrow();
  180.         a->back_arrow->wid = w;
  181.         a->back_arrow->ht = h;
  182.         }
  183.     return(a);
  184.     }
  185.  
  186. static F_compound *
  187. read_compoundobject(fp)
  188. FILE    *fp;
  189. {
  190.     F_arc        *a, *la = NULL;
  191.     F_ellipse    *e, *le = NULL;
  192.     F_line        *l, *ll = NULL;
  193.     F_spline    *s, *ls = NULL;
  194.     F_text        *t, *lt = NULL;
  195.     F_compound    *com, *c, *lc = NULL;
  196.     int         n, object;
  197.  
  198.     Compound_malloc(com);
  199.     com->arcs = NULL;
  200.     com->ellipses = NULL;
  201.     com->lines = NULL;
  202.     com->splines = NULL;
  203.     com->texts = NULL;
  204.     com->compounds = NULL;
  205.     com->next = NULL;
  206.     n = fscanf(fp, " %d %d %d %d\n", &com->nwcorner.x, &com->nwcorner.y,
  207.         &com->secorner.x, &com->secorner.y);
  208.     if (n != 4) {
  209.         put_msg("Incorrect compound object format");
  210.         return(NULL);
  211.         }
  212.     while (fscanf(fp, "%d", &object) == 1) {
  213.         switch (object) {
  214.         case O_POLYLINE :
  215.             if ((l = read_lineobject(fp)) == NULL) { 
  216.             free_line(&l);
  217.             return(NULL);
  218.             }
  219.             if (ll)
  220.             ll = (ll->next = l);
  221.             else
  222.             ll = com->lines = l;
  223.             break;
  224.         case O_SPLINE :
  225.             if ((s = read_splineobject(fp)) == NULL) { 
  226.             free_spline(&s);
  227.             return(NULL);
  228.             }
  229.             if (ls)
  230.             ls = (ls->next = s);
  231.             else
  232.             ls = com->splines = s;
  233.             break;
  234.         case O_ELLIPSE :
  235.             if ((e = read_ellipseobject(fp)) == NULL) { 
  236.             free_ellipse(&e);
  237.             return(NULL);
  238.             }
  239.             if (le)
  240.             le = (le->next = e);
  241.             else
  242.             le = com->ellipses = e;
  243.             break;
  244.         case O_ARC :
  245.             if ((a = read_arcobject(fp)) == NULL) { 
  246.             free_arc(&a);
  247.             return(NULL);
  248.             }
  249.             if (la)
  250.             la = (la->next = a);
  251.             else
  252.             la = com->arcs = a;
  253.             break;
  254.         case O_TEXT :
  255.             if ((t = read_textobject(fp)) == NULL) { 
  256.             free_text(&t);
  257.             return(NULL);
  258.             }
  259.             if (lt)
  260.             lt = (lt->next = t);
  261.             else
  262.             lt = com->texts = t;
  263.             break;
  264.         case O_COMPOUND :
  265.             if ((c = read_compoundobject(fp)) == NULL) { 
  266.             free_compound(&c);
  267.             return(NULL);
  268.             }
  269.             if (lc)
  270.             lc = (lc->next = c);
  271.             else
  272.             lc = com->compounds = c;
  273.             break;
  274.         case O_END_COMPOUND :
  275.             return(com);
  276.         } /*  switch */
  277.         }
  278.     if (feof(fp))
  279.         return(com);
  280.     else {
  281.         put_msg("Format error: %s", sys_errlist[errno]);
  282.         return(NULL);
  283.         }
  284.     }
  285.  
  286. static F_ellipse *
  287. read_ellipseobject(fp)
  288. FILE    *fp;
  289. {
  290.     F_ellipse    *e;
  291.     int        n, t;
  292.  
  293.     Ellipse_malloc(e);
  294.           e->color = BLACK_COLOR;
  295.     e->angle = 0.0;
  296.     e->depth = 0;
  297.     e->pen = 0;
  298.     e->area_fill = 0;
  299.     e->next = NULL;
  300.     n = fscanf(fp," %d %d %d %lf %d %d %d %d %d %d %d %d %d\n", 
  301.         &t, &e->style,
  302.         &e->thickness, &e->style_val, &e->direction, 
  303.         &e->center.x, &e->center.y, 
  304.         &e->radiuses.x, &e->radiuses.y, 
  305.         &e->start.x, &e->start.y, 
  306.         &e->end.x, &e->end.y);
  307.     if (n != 13) {
  308.         put_msg("incomplete ellipse data");
  309.         free((char*)e);
  310.         return(NULL);
  311.         }
  312.     if (t == DRAW_ELLIPSE_BY_RAD)
  313.         e->type = T_ELLIPSE_BY_RAD;
  314.     else if (t == DRAW_ELLIPSE_BY_DIA)
  315.         e->type = T_ELLIPSE_BY_DIA;
  316.     else if (t == DRAW_CIRCLE_BY_RAD)
  317.         e->type = T_CIRCLE_BY_RAD;
  318.     else
  319.         e->type = T_CIRCLE_BY_DIA;
  320.     return(e);
  321.     }
  322.  
  323. static F_line *
  324. read_lineobject(fp)
  325. FILE            *fp;
  326. {
  327.     F_line    *l;
  328.     F_point    *p, *q;
  329.     int    f, b, h, w, n, t, x, y;
  330.  
  331.     Line_malloc(l);
  332.           l->color = BLACK_COLOR;
  333.     l->depth = 0;
  334.     l->pen = 0;
  335.     l->area_fill = 0;
  336.     l->for_arrow = NULL;
  337.     l->back_arrow = NULL;
  338.     l->next = NULL;
  339.     l->points = Point_malloc(p);
  340.     n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %d", &t, 
  341.         &l->style, &l->thickness, &l->style_val,
  342.         &f, &b, &h, &w, &p->x, &p->y);
  343.     if (n != 10) {
  344.         put_msg("incomplete line data");
  345.         free((char*)l);
  346.         return(NULL);
  347.         }
  348.     if (t == DRAW_POLYLINE)
  349.         l->type = T_POLYLINE;
  350.     else if (t == DRAW_POLYGON)
  351.         l->type = T_POLYGON;
  352.     else
  353.         l->type = T_BOX;
  354.     if (f) {
  355.         l->for_arrow = forward_arrow();
  356.         l->for_arrow->wid = w;
  357.         l->for_arrow->ht = h;
  358.         }
  359.     if (b) {
  360.         l->back_arrow = backward_arrow();
  361.         l->back_arrow->wid = w;
  362.         l->back_arrow->ht = h;
  363.         }
  364.     for (;;) {
  365.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  366.         put_msg("incomplete line object");
  367.         free_linestorage(l);
  368.         return(NULL);
  369.         }
  370.         if (x == 9999) break;
  371.         Point_malloc(q);
  372.         q->x = x;
  373.         q->y = y;
  374.         q->next = NULL;
  375.         p->next = q;
  376.         p = q;
  377.         }
  378.     return(l);
  379.     }
  380.  
  381. static F_spline *
  382. read_splineobject(fp)
  383. FILE    *fp;
  384. {
  385.     F_spline    *s;
  386.     F_point        *p, *q;
  387.     int        f, b, h, w, n, t, x, y;
  388.  
  389.     Spline_malloc(s);
  390.           s->color = BLACK_COLOR;
  391.     s->depth = 0;
  392.     s->pen = 0;
  393.     s->area_fill = 0;
  394.     s->for_arrow = NULL;
  395.     s->back_arrow = NULL;
  396.     s->controls = NULL;
  397.     s->next = NULL;
  398.     s->points = Point_malloc(p);
  399.     n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %d", 
  400.             &t, &s->style, &s->thickness, &s->style_val,
  401.             &f, &b,
  402.             &h, &w, &p->x, &p->y);
  403.     if (n != 10) {
  404.         put_msg("incomplete spline data");
  405.         free((char*)s);
  406.         return(NULL);
  407.         }
  408.     if (t == DRAW_CLOSEDSPLINE)
  409.         s->type = T_CLOSED_NORMAL;
  410.     else
  411.         s->type = T_OPEN_NORMAL;
  412.     if (f) {
  413.         s->for_arrow = forward_arrow();
  414.         s->for_arrow->wid = w;
  415.         s->for_arrow->ht = h;
  416.         }
  417.     if (b) {
  418.         s->back_arrow = backward_arrow();
  419.         s->back_arrow->wid = w;
  420.         s->back_arrow->ht = h;
  421.         }
  422.     for (;;) {
  423.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  424.         put_msg("incomplete spline object");
  425.         free_splinestorage(s);
  426.         return(NULL);
  427.         };
  428.         if (x == 9999) break;
  429.         Point_malloc(q);
  430.         q->x = x;
  431.         q->y = y;
  432.         q->next = NULL;
  433.         p->next = q;
  434.         p = q;
  435.         }
  436.     return(s);
  437.     }
  438.  
  439. static F_text *
  440. read_textobject(fp)
  441. FILE    *fp;
  442. {
  443.     F_text    *t;
  444.     int    n;
  445.     char    buf[128];
  446.  
  447.     Text_malloc(t);
  448.     t->type = T_LEFT_JUSTIFIED;
  449.     t->flags = 0;
  450.           t->color = BLACK_COLOR;
  451.     t->depth = 0;
  452.     t->pen = 0;
  453.     t->angle = 0.0;
  454.     t->next = NULL;
  455.     n = fscanf(fp," %d %d %d %d %d %d %d %[^\n]", &t->font, 
  456.         &t->size, &t->flags, &t->height, &t->length, 
  457.         &t->base_x, &t->base_y, buf);
  458.     if (n != 8) {
  459.         put_msg("incomplete text data");
  460.         free((char*)t);
  461.         return(NULL);
  462.         }
  463.     t->cstring = (char *) calloc((unsigned)(strlen(buf)+1), sizeof(char));
  464.     if (t->cstring == NULL) {
  465.         put_msg(Err_mem);
  466.         free((char*) t);
  467.         return(NULL);
  468.         }
  469.     (void)strcpy(t->cstring, buf);
  470.     if (t->size == 0) t->size = 18;
  471.     return(t);
  472.     }
  473.