home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part09 / f_readold.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  10.5 KB  |  482 lines

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