home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / u_create.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  10.7 KB  |  518 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. extern int    cur_linewidth;
  19.  
  20. /* LOCAL */
  21.  
  22. static float    forward_arrow_wid = 4;
  23. static float    forward_arrow_ht = 8;
  24. static int    forward_arrow_type = 0;
  25. static int    forward_arrow_style = 0;
  26. static float    forward_arrow_thickness = 1;
  27.  
  28. static float    backward_arrow_wid = 4;
  29. static float    backward_arrow_ht = 8;
  30. static int    backward_arrow_type = 0;
  31. static int    backward_arrow_style = 0;
  32. static float    backward_arrow_thickness = 1;
  33.  
  34. static F_arrow *create_arrow();
  35. static char    Err_mem[] = "Running out of memory.";
  36.  
  37. /****************** ARROWS ****************/
  38.  
  39. static F_arrow *
  40. create_arrow()
  41. {
  42.     F_arrow       *a;
  43.  
  44.     if ((a = (F_arrow *) malloc(ARROW_SIZE)) == NULL)
  45.     put_msg(Err_mem);
  46.     return (a);
  47. }
  48.  
  49. F_arrow           *
  50. forward_arrow()
  51. {
  52.     F_arrow       *a;
  53.  
  54.     if ((a = create_arrow()) == NULL)
  55.     return (NULL);
  56.  
  57.     a->type = forward_arrow_type;
  58.     a->style = forward_arrow_style;
  59.     a->thickness = forward_arrow_thickness = (float) cur_linewidth;
  60.     a->wid = forward_arrow_thickness * forward_arrow_wid;
  61.     a->ht = forward_arrow_thickness * forward_arrow_ht;
  62.     return (a);
  63. }
  64.  
  65. F_arrow           *
  66. backward_arrow()
  67. {
  68.     F_arrow       *a;
  69.  
  70.     if ((a = create_arrow()) == NULL)
  71.     return (NULL);
  72.  
  73.     a->type = backward_arrow_type;
  74.     a->style = backward_arrow_style;
  75.     a->thickness = backward_arrow_thickness = (float) cur_linewidth;
  76.     a->wid = backward_arrow_thickness * backward_arrow_wid;
  77.     a->ht = backward_arrow_thickness * backward_arrow_ht;
  78.     return (a);
  79. }
  80.  
  81. F_arrow           *
  82. new_arrow(type, style, thickness, wid, ht)
  83.     int            type, style;
  84.     float        thickness, wid, ht;
  85. {
  86.     F_arrow       *a;
  87.  
  88.     if ((a = create_arrow()) == NULL)
  89.     return (NULL);
  90.  
  91.     a->type = type;
  92.     a->style = style;
  93.     a->thickness = thickness;
  94.     a->wid = wid;
  95.     a->ht = ht;
  96.     return (a);
  97. }
  98.  
  99. /************************ SMART LINKS *************************/
  100.  
  101. F_linkinfo     *
  102. new_link(l, ep, pp)
  103.     F_line       *l;
  104.     F_point       *ep, *pp;
  105. {
  106.     F_linkinfo       *k;
  107.  
  108.     if ((k = (F_linkinfo *) malloc(LINKINFO_SIZE)) == NULL) {
  109.     put_msg(Err_mem);
  110.     return (NULL);
  111.     }
  112.     k->line = l;
  113.     k->endpt = ep;
  114.     k->prevpt = pp;
  115.     k->next = NULL;
  116.     return (k);
  117. }
  118.  
  119. /************************ POINTS *************************/
  120.  
  121. F_point           *
  122. create_point()
  123. {
  124.     F_point       *p;
  125.  
  126.     if ((p = (F_point *) malloc(POINT_SIZE)) == NULL)
  127.     put_msg(Err_mem);
  128.     return (p);
  129. }
  130.  
  131. F_control      *
  132. create_cpoint()
  133. {
  134.     F_control       *cp;
  135.  
  136.     if ((cp = (F_control *) malloc(CONTROL_SIZE)) == NULL)
  137.     put_msg(Err_mem);
  138.     return (cp);
  139. }
  140.  
  141. F_point           *
  142. copy_points(orig_pt)
  143.     F_point       *orig_pt;
  144. {
  145.     F_point       *new_pt, *prev_pt, *first_pt;
  146.  
  147.     if ((new_pt = create_point()) == NULL)
  148.     return (NULL);
  149.  
  150.     first_pt = new_pt;
  151.     *new_pt = *orig_pt;
  152.     new_pt->next = NULL;
  153.     prev_pt = new_pt;
  154.     for (orig_pt = orig_pt->next; orig_pt != NULL; orig_pt = orig_pt->next) {
  155.     if ((new_pt = create_point()) == NULL) {
  156.         free_points(first_pt);
  157.         return (NULL);
  158.     }
  159.     prev_pt->next = new_pt;
  160.     *new_pt = *orig_pt;
  161.     new_pt->next = NULL;
  162.     prev_pt = new_pt;
  163.     }
  164.     return (first_pt);
  165. }
  166.  
  167. /************************ ARCS *************************/
  168.  
  169. F_arc           *
  170. create_arc()
  171. {
  172.     F_arc       *a;
  173.  
  174.     if ((a = (F_arc *) malloc(ARCOBJ_SIZE)) == NULL)
  175.     put_msg(Err_mem);
  176.     a->tagged = 0;
  177.     return (a);
  178. }
  179.  
  180. F_arc           *
  181. copy_arc(a)
  182.     F_arc       *a;
  183. {
  184.     F_arc       *arc;
  185.     F_arrow       *arrow;
  186.  
  187.     if ((arc = create_arc()) == NULL)
  188.     return (NULL);
  189.  
  190.     *arc = *a;
  191.     arc->next = NULL;
  192.     if (a->for_arrow) {
  193.     if ((arrow = create_arrow()) == NULL) {
  194.         free((char *) arc);
  195.         return (NULL);
  196.     }
  197.     arc->for_arrow = arrow;
  198.     *arrow = *a->for_arrow;
  199.     }
  200.     if (a->back_arrow) {
  201.     if ((arrow = create_arrow()) == NULL) {
  202.         free((char *) arc);
  203.         return (NULL);
  204.     }
  205.     arc->back_arrow = arrow;
  206.     *arrow = *a->back_arrow;
  207.     }
  208.     return (arc);
  209. }
  210.  
  211. /************************ ELLIPSES *************************/
  212.  
  213. F_ellipse      *
  214. create_ellipse()
  215. {
  216.     F_ellipse       *e;
  217.  
  218.     if ((e = (F_ellipse *) malloc(ELLOBJ_SIZE)) == NULL)
  219.     put_msg(Err_mem);
  220.     e->tagged = 0;
  221.     return (e);
  222. }
  223.  
  224. F_ellipse      *
  225. copy_ellipse(e)
  226.     F_ellipse       *e;
  227. {
  228.     F_ellipse       *ellipse;
  229.  
  230.     if ((ellipse = create_ellipse()) == NULL)
  231.     return (NULL);
  232.  
  233.     *ellipse = *e;
  234.     ellipse->next = NULL;
  235.     return (ellipse);
  236. }
  237.  
  238. /************************ LINES *************************/
  239.  
  240. F_line           *
  241. create_line()
  242. {
  243.     F_line       *l;
  244.  
  245.     if ((l = (F_line *) malloc(LINOBJ_SIZE)) == NULL)
  246.     put_msg(Err_mem);
  247.     l->tagged = 0;
  248.     l->eps = NULL;
  249.     l->next = NULL;
  250.     l->for_arrow = NULL;
  251.     l->back_arrow = NULL;
  252.     l->points = NULL;
  253.     l->radius = DEFAULT;
  254.     return (l);
  255. }
  256.  
  257. F_eps           *
  258. create_eps()
  259. {
  260.     F_eps       *e;
  261.  
  262.     if ((e = (F_eps *) malloc(EPS_SIZE)) == NULL)
  263.     put_msg(Err_mem);
  264.     return (e);
  265. }
  266.  
  267. F_line           *
  268. copy_line(l)
  269.     F_line       *l;
  270. {
  271.     F_line       *line;
  272.     F_arrow       *arrow;
  273.     int            nbytes;
  274.  
  275.     if ((line = create_line()) == NULL)
  276.     return (NULL);
  277.  
  278.     *line = *l;
  279.     line->next = NULL;
  280.     if (l->for_arrow) {
  281.     if ((arrow = create_arrow()) == NULL) {
  282.         free((char *) line);
  283.         return (NULL);
  284.     }
  285.     line->for_arrow = arrow;
  286.     *arrow = *l->for_arrow;
  287.     }
  288.     if (l->back_arrow) {
  289.     if ((arrow = create_arrow()) == NULL) {
  290.         free((char *) line);
  291.         return (NULL);
  292.     }
  293.     line->back_arrow = arrow;
  294.     *arrow = *l->back_arrow;
  295.     }
  296.     line->points = copy_points(l->points);
  297.     if (NULL == line->points) {
  298.     put_msg(Err_mem);
  299.     free_linestorage(line);
  300.     return (NULL);
  301.     }
  302.     if (l->eps != NULL) {
  303.     if ((line->eps = create_eps()) == NULL) {
  304.         free((char *) line);
  305.         return (NULL);
  306.     }
  307.     bcopy(l->eps, line->eps, EPS_SIZE);
  308.     if (l->eps->bitmap != NULL) {
  309.         nbytes = (line->eps->bit_size.x + 7) / 8 * line->eps->bit_size.y;
  310.         line->eps->bitmap = (unsigned char *) malloc(nbytes);
  311.         if (line->eps->bitmap == NULL)
  312.         fprintf(stderr, "xfig: out of memory in function copy_line");
  313.         bcopy(l->eps->bitmap, line->eps->bitmap, nbytes);
  314.         line->eps->pix_width = 0;
  315.         line->eps->pix_height = 0;
  316.         line->eps->pixmap = 0;
  317.     }
  318.     }
  319.     return (line);
  320. }
  321.  
  322. /************************ SPLINES *************************/
  323.  
  324. F_spline       *
  325. create_spline()
  326. {
  327.     F_spline       *s;
  328.  
  329.     if ((s = (F_spline *) malloc(SPLOBJ_SIZE)) == NULL)
  330.     put_msg(Err_mem);
  331.     s->tagged = 0;
  332.     return (s);
  333. }
  334.  
  335. F_spline       *
  336. copy_spline(s)
  337.     F_spline       *s;
  338. {
  339.     F_spline       *spline;
  340.     F_control       *new_cp, *orig_cp, *last_cp;
  341.     F_arrow       *arrow;
  342.  
  343.     if ((spline = create_spline()) == NULL)
  344.     return (NULL);
  345.  
  346.     *spline = *s;
  347.     spline->next = NULL;
  348.     if (s->for_arrow) {
  349.     if ((arrow = create_arrow()) == NULL) {
  350.         free((char *) spline);
  351.         return (NULL);
  352.     }
  353.     spline->for_arrow = arrow;
  354.     *arrow = *s->for_arrow;
  355.     }
  356.     if (s->back_arrow) {
  357.     if ((arrow = create_arrow()) == NULL) {
  358.         free((char *) spline);
  359.         return (NULL);
  360.     }
  361.     spline->back_arrow = arrow;
  362.     *arrow = *s->back_arrow;
  363.     }
  364.     spline->points = copy_points(s->points);
  365.     if (NULL == spline->points) {
  366.     put_msg(Err_mem);
  367.     free_splinestorage(spline);
  368.     return (NULL);
  369.     }
  370.     spline->controls = NULL;
  371.     if (s->controls == NULL)
  372.     return (spline);
  373.  
  374.     if ((new_cp = create_cpoint()) == NULL) {
  375.     free_splinestorage(spline);
  376.     return (NULL);
  377.     }
  378.     new_cp->next = NULL;
  379.     last_cp = spline->controls = new_cp;
  380.     orig_cp = s->controls;
  381.     *new_cp = *orig_cp;
  382.     for (orig_cp = orig_cp->next; orig_cp != NULL; orig_cp = orig_cp->next) {
  383.     if ((new_cp = create_cpoint()) == NULL) {
  384.         free_splinestorage(spline);
  385.         return (NULL);
  386.     }
  387.     last_cp->next = new_cp;
  388.     new_cp->next = NULL;
  389.     *new_cp = *orig_cp;
  390.     last_cp = new_cp;
  391.     }
  392.     return (spline);
  393. }
  394.  
  395. /************************ TEXTS *************************/
  396.  
  397. F_text           *
  398. create_text()
  399. {
  400.     F_text       *t;
  401.  
  402.     if ((t = (F_text *) malloc(TEXOBJ_SIZE)) == NULL)
  403.     put_msg(Err_mem);
  404.     t->tagged = 0;
  405.     return (t);
  406. }
  407.  
  408. char           *
  409. new_string(len)
  410.     int            len;
  411. {
  412.     char       *c;
  413.  
  414.     if ((c = (char *) calloc((unsigned) len, sizeof(char))) == NULL)
  415.     put_msg(Err_mem);
  416.     return (c);
  417. }
  418.  
  419. F_text           *
  420. copy_text(t)
  421.     F_text       *t;
  422. {
  423.     F_text       *text;
  424.  
  425.     if ((text = create_text()) == NULL)
  426.     return (NULL);
  427.  
  428.     *text = *t;
  429.     if ((text->cstring = new_string(strlen(t->cstring) + 1)) == NULL) {
  430.     free((char *) text);
  431.     return (NULL);
  432.     }
  433.     strcpy(text->cstring, t->cstring);
  434.     text->next = NULL;
  435.     return (text);
  436. }
  437.  
  438. /************************ COMPOUNDS *************************/
  439.  
  440. F_compound     *
  441. create_compound()
  442. {
  443.     F_compound       *c;
  444.  
  445.     if ((c = (F_compound *) malloc(COMOBJ_SIZE)) == NULL)
  446.     put_msg(Err_mem);
  447.     c->tagged = 0;
  448.     return (c);
  449. }
  450.  
  451. F_compound     *
  452. copy_compound(c)
  453.     F_compound       *c;
  454. {
  455.     F_ellipse       *e, *ee;
  456.     F_arc       *a, *aa;
  457.     F_line       *l, *ll;
  458.     F_spline       *s, *ss;
  459.     F_text       *t, *tt;
  460.     F_compound       *cc, *ccc, *compound;
  461.  
  462.     if ((compound = create_compound()) == NULL)
  463.     return (NULL);
  464.  
  465.     compound->nwcorner = c->nwcorner;
  466.     compound->secorner = c->secorner;
  467.     compound->arcs = NULL;
  468.     compound->ellipses = NULL;
  469.     compound->lines = NULL;
  470.     compound->splines = NULL;
  471.     compound->texts = NULL;
  472.     compound->compounds = NULL;
  473.     compound->next = NULL;
  474.     for (e = c->ellipses; e != NULL; e = e->next) {
  475.     if (NULL == (ee = copy_ellipse(e))) {
  476.         put_msg(Err_mem);
  477.         return (NULL);
  478.     }
  479.     list_add_ellipse(&compound->ellipses, ee);
  480.     }
  481.     for (a = c->arcs; a != NULL; a = a->next) {
  482.     if (NULL == (aa = copy_arc(a))) {
  483.         put_msg(Err_mem);
  484.         return (NULL);
  485.     }
  486.     list_add_arc(&compound->arcs, aa);
  487.     }
  488.     for (l = c->lines; l != NULL; l = l->next) {
  489.     if (NULL == (ll = copy_line(l))) {
  490.         put_msg(Err_mem);
  491.         return (NULL);
  492.     }
  493.     list_add_line(&compound->lines, ll);
  494.     }
  495.     for (s = c->splines; s != NULL; s = s->next) {
  496.     if (NULL == (ss = copy_spline(s))) {
  497.         put_msg(Err_mem);
  498.         return (NULL);
  499.     }
  500.     list_add_spline(&compound->splines, ss);
  501.     }
  502.     for (t = c->texts; t != NULL; t = t->next) {
  503.     if (NULL == (tt = copy_text(t))) {
  504.         put_msg(Err_mem);
  505.         return (NULL);
  506.     }
  507.     list_add_text(&compound->texts, tt);
  508.     }
  509.     for (cc = c->compounds; cc != NULL; cc = cc->next) {
  510.     if (NULL == (ccc = copy_compound(cc))) {
  511.         put_msg(Err_mem);
  512.         return (NULL);
  513.     }
  514.     list_add_compound(&compound->compounds, ccc);
  515.     }
  516.     return (compound);
  517. }
  518.