home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / pic / object.cc < prev    next >
C/C++ Source or Header  |  1993-07-14  |  38KB  |  1,816 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.com)
  4.  
  5. This file is part of groff.
  6.  
  7. groff is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 2, or (at your option) any later
  10. version.
  11.  
  12. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with groff; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include "pic.h"
  22. #include "ptable.h"
  23. #include "object.h"
  24.  
  25. void print_object_list(object *);
  26.  
  27. line_type::line_type()
  28. : type(solid), thickness(1.0)
  29. {
  30. }
  31.  
  32. output::output() : desired_height(0.0), desired_width(0.0), args(0)
  33. {
  34. }
  35.  
  36. output::~output()
  37. {
  38.   a_delete args;
  39. }
  40.  
  41. void output::set_desired_width_height(double wid, double ht)
  42. {
  43.   desired_width = wid;
  44.   desired_height = ht;
  45. }
  46.  
  47. void output::set_args(const char *s)
  48. {
  49.   a_delete args;
  50.   if (s == 0 || *s == '\0')
  51.     args = 0;
  52.   else
  53.     args = strsave(s);
  54. }
  55.  
  56. void output::command(const char *, const char *, int)
  57. {
  58. }
  59.  
  60. void output::set_location(const char *, int)
  61. {
  62. }
  63.  
  64. int output::supports_filled_polygons()
  65. {
  66.   return 0;
  67. }
  68.  
  69. void output::begin_block(const position &, const position &)
  70. {
  71. }
  72.  
  73. void output::end_block()
  74. {
  75. }
  76.  
  77. double output::compute_scale(double sc, const position &ll, const position &ur)
  78. {
  79.   distance dim = ur - ll;
  80.   if (desired_width != 0.0 || desired_height != 0.0) {
  81.     sc = 0.0;
  82.     if (desired_width != 0.0) {
  83.       if (dim.x == 0.0)
  84.     error("width specified for picture with zero width");
  85.       else
  86.     sc = dim.x/desired_width;
  87.     }
  88.     if (desired_height != 0.0) {
  89.       if (dim.y == 0.0)
  90.     error("height specified for picture with zero height");
  91.       else {
  92.     double tem = dim.y/desired_height;
  93.     if (tem > sc)
  94.       sc = tem;
  95.       }
  96.     }
  97.     return sc == 0.0 ? 1.0 : sc;
  98.   }
  99.   else {
  100.     if (sc <= 0.0)
  101.       sc = 1.0;
  102.     distance sdim = dim/sc;
  103.     double max_width = 0.0;
  104.     lookup_variable("maxpswid", &max_width);
  105.     double max_height = 0.0;
  106.     lookup_variable("maxpsht", &max_height);
  107.     if ((max_width > 0.0 && sdim.x > max_width)
  108.     || (max_height > 0.0 && sdim.y > max_height)) {
  109.       double xscale = dim.x/max_width;
  110.       double yscale = dim.y/max_height;
  111.       return xscale > yscale ? xscale : yscale;
  112.     }
  113.     else
  114.       return sc;
  115.   }
  116. }
  117.  
  118. position::position(const place &pl)
  119. {
  120.   if (pl.obj != 0) {
  121.     // Use two statements to work around bug in SGI C++.
  122.     object *tem = pl.obj;
  123.     *this = tem->origin();
  124.   }
  125.   else {
  126.     x = pl.x;
  127.     y = pl.y;
  128.   }
  129. }
  130.  
  131. position::position() : x(0.0), y(0.0)
  132. {
  133. }
  134.  
  135. position::position(double a, double b) : x(a), y(b)
  136. {
  137. }
  138.  
  139.  
  140. int operator==(const position &a, const position &b)
  141. {
  142.   return a.x == b.x && a.y == b.y;
  143. }
  144.  
  145. int operator!=(const position &a, const position &b)
  146. {
  147.   return a.x != b.x || a.y != b.y;
  148. }
  149.  
  150. position &position::operator+=(const position &a)
  151. {
  152.   x += a.x;
  153.   y += a.y;
  154.   return *this;
  155. }
  156.  
  157. position &position::operator-=(const position &a)
  158. {
  159.   x -= a.x;
  160.   y -= a.y;
  161.   return *this;
  162. }
  163.  
  164. position &position::operator*=(double a)
  165. {
  166.   x *= a;
  167.   y *= a;
  168.   return *this;
  169. }
  170.  
  171. position &position::operator/=(double a)
  172. {
  173.   x /= a;
  174.   y /= a;
  175.   return *this;
  176. }
  177.  
  178. position operator-(const position &a)
  179. {
  180.   return position(-a.x, -a.y);
  181. }
  182.  
  183. position operator+(const position &a, const position &b)
  184. {
  185.   return position(a.x + b.x, a.y + b.y);
  186. }
  187.  
  188. position operator-(const position &a, const position &b)
  189. {
  190.   return position(a.x - b.x, a.y - b.y);
  191. }
  192.  
  193. position operator/(const position &a, double n)
  194. {
  195.   return position(a.x/n, a.y/n);
  196. }
  197.  
  198. position operator*(const position &a, double n)
  199. {
  200.   return position(a.x*n, a.y*n);
  201. }
  202.  
  203. // dot product
  204.  
  205. double operator*(const position &a, const position &b)
  206. {
  207.   return a.x*b.x + a.y*b.y;
  208. }
  209.  
  210. double hypot(const position &a)
  211. {
  212.   return hypot(a.x, a.y);
  213. }
  214.  
  215. struct arrow_head_type {
  216.   double height;
  217.   double width;
  218.   int solid;
  219. };
  220.  
  221. void draw_arrow(const position &pos, const distance &dir,
  222.         const arrow_head_type &aht, const line_type <)
  223. {
  224.   double hyp = hypot(dir);
  225.   if (hyp == 0.0) {
  226.     error("cannot draw arrow on object with zero length");
  227.     return;
  228.   }
  229.   position base = -dir;
  230.   base *= aht.height/hyp;
  231.   position n(dir.y, -dir.x);
  232.   n *= aht.width/(hyp*2.0);
  233.   line_type slt = lt;
  234.   slt.type = line_type::solid;
  235.   if (aht.solid && out->supports_filled_polygons()) {
  236.     position v[3];
  237.     v[0] = pos;
  238.     v[1] = pos + base + n;
  239.     v[2] = pos + base - n;
  240.     // A value > 1 means fill with the current color.
  241.     out->polygon(v, 3, slt, 2.0);
  242.   }
  243.   else {
  244.     position v[2];
  245.     v[0] = pos;
  246.     v[1] = pos + base + n;
  247.     out->line(pos + base - n, v, 2, slt);
  248.   }
  249. }
  250.  
  251. object::object() : prev(0), next(0)
  252. {
  253. }
  254.  
  255. object::~object()
  256. {
  257. }
  258.  
  259. void object::move_by(const position &)
  260. {
  261. }
  262.  
  263. void object::print()
  264. {
  265. }
  266.  
  267. void object::print_text()
  268. {
  269. }
  270.  
  271. int object::blank()
  272. {
  273.   return 0;
  274. }
  275.  
  276. struct bounding_box {
  277.   int blank;
  278.   position ll;
  279.   position ur;
  280.  
  281.   bounding_box();
  282.   void encompass(const position &);
  283. };
  284.  
  285. bounding_box::bounding_box()
  286. : blank(1)
  287. {
  288. }
  289.  
  290. void bounding_box::encompass(const position &pos)
  291. {
  292.   if (blank) {
  293.     ll = pos;
  294.     ur = pos;
  295.     blank = 0;
  296.   }
  297.   else {
  298.     if (pos.x < ll.x)
  299.       ll.x = pos.x;
  300.     if (pos.y < ll.y)
  301.       ll.y = pos.y;
  302.     if (pos.x > ur.x)
  303.       ur.x = pos.x;
  304.     if (pos.y > ur.y)
  305.       ur.y = pos.y;
  306.   }
  307. }
  308.  
  309. void object::update_bounding_box(bounding_box *)
  310. {
  311. }
  312.  
  313. position object::origin()
  314. {
  315.   return position(0.0,0.0);
  316. }
  317.  
  318. position object::north()
  319. {
  320.   return origin();
  321. }
  322.  
  323. position object::south()
  324. {
  325.   return origin();
  326. }
  327.  
  328. position object::east()
  329. {
  330.   return origin();
  331. }
  332.  
  333. position object::west()
  334. {
  335.   return origin();
  336. }
  337.  
  338. position object::north_east()
  339. {
  340.   return origin();
  341. }
  342.  
  343. position object::north_west()
  344. {
  345.   return origin();
  346. }
  347.  
  348. position object::south_east()
  349. {
  350.   return origin();
  351. }
  352.  
  353. position object::south_west()
  354. {
  355.   return origin();
  356. }
  357.  
  358. position object::start()
  359. {
  360.   return origin();
  361. }
  362.  
  363. position object::end()
  364. {
  365.   return origin();
  366. }
  367.  
  368. position object::center()
  369. {
  370.   return origin();
  371. }
  372.  
  373. double object::width()
  374. {
  375.   return 0.0;
  376. }
  377.  
  378. double object::radius()
  379. {
  380.   return 0.0;
  381. }
  382.  
  383. double object::height()
  384. {
  385.   return 0.0;
  386. }
  387.  
  388. place *object::find_label(const char *)
  389. {
  390.   return 0;
  391. }
  392.  
  393. segment::segment(const position &a, int n, segment *p)
  394. : pos(a), is_absolute(n), next(p)
  395. {
  396. }
  397.  
  398. text_item::text_item(char *t, const char *fn, int ln)
  399. : filename(fn), lineno(ln), text(t), next(0)
  400. {
  401.   adj.h = CENTER_ADJUST;
  402.   adj.v = NONE_ADJUST;
  403. }
  404.  
  405. text_item::~text_item()
  406. {
  407.   a_delete text;
  408. }
  409.  
  410. object_spec::object_spec(object_type t) : type(t)
  411. {
  412.   flags = 0;
  413.   tbl = 0;
  414.   segment_list = 0;
  415.   segment_width = segment_height = 0.0;
  416.   segment_is_absolute = 0;
  417.   text = 0;
  418.   with = 0;
  419.   dir = RIGHT_DIRECTION;
  420. }
  421.  
  422. object_spec::~object_spec()
  423. {
  424.   delete tbl;
  425.   while (segment_list != 0) {
  426.     segment *tem = segment_list;
  427.     segment_list = segment_list->next;
  428.     delete tem;
  429.   }
  430.   object *p = oblist.head;
  431.   while (p != 0) {
  432.     object *tem = p;
  433.     p = p->next;
  434.     delete tem;
  435.   }
  436.   while (text != 0) {
  437.     text_item *tem = text;
  438.     text = text->next;
  439.     delete tem;
  440.   }
  441.   delete with;
  442. }
  443.  
  444. class command_object : public object {
  445.   char *s;
  446.   const char *filename;
  447.   int lineno;
  448. public:
  449.   command_object(char *, const char *, int);
  450.   ~command_object();
  451.   object_type type() { return OTHER_OBJECT; }
  452.   void print();
  453. };
  454.  
  455. command_object::command_object(char *p, const char *fn, int ln)
  456. : s(p), filename(fn), lineno(ln)
  457. {
  458. }
  459.  
  460. command_object::~command_object()
  461. {
  462.   a_delete s;
  463. }
  464.  
  465. void command_object::print()
  466. {
  467.   out->command(s, filename, lineno);
  468. }
  469.  
  470. object *make_command_object(char *s, const char *fn, int ln)
  471. {
  472.   return new command_object(s, fn, ln);
  473. }
  474.  
  475. class mark_object : public object {
  476. public:
  477.   mark_object();
  478.   object_type type();
  479. };
  480.  
  481. object *make_mark_object()
  482. {
  483.   return new mark_object();
  484. }
  485.  
  486. mark_object::mark_object()
  487. {
  488. }
  489.  
  490. object_type mark_object::type()
  491. {
  492.   return MARK_OBJECT;
  493. }
  494.  
  495. object_list::object_list() : head(0), tail(0)
  496. {
  497. }
  498.  
  499. void object_list::append(object *obj)
  500. {
  501.   if (tail == 0) {
  502.     obj->next = obj->prev = 0;
  503.     head = tail = obj;
  504.   }
  505.   else {
  506.     obj->prev = tail;
  507.     obj->next = 0;
  508.     tail->next = obj;
  509.     tail = obj;
  510.   }
  511. }
  512.  
  513. void object_list::wrap_up_block(object_list *ol)
  514. {
  515.   for (object *p = tail; p && p->type() != MARK_OBJECT; p = p->prev)
  516.     ;
  517.   assert(p != 0);
  518.   ol->head = p->next;
  519.   if (ol->head) {
  520.     ol->tail = tail;
  521.     ol->head->prev = 0;
  522.   }
  523.   else
  524.     ol->tail = 0;
  525.   tail = p->prev;
  526.   if (tail)
  527.     tail->next = 0;
  528.   else
  529.     head = 0;
  530.   delete p;
  531. }
  532.  
  533. text_piece::text_piece()
  534. : text(0), filename(0), lineno(-1)
  535. {
  536.   adj.h = CENTER_ADJUST;
  537.   adj.v = NONE_ADJUST;
  538. }
  539.  
  540. text_piece::~text_piece()
  541. {
  542.   a_delete text;
  543. }
  544.  
  545. class graphic_object : public object {
  546.   int ntext;
  547.   text_piece *text;
  548.   int aligned;
  549. protected:
  550.   line_type lt;
  551. public:
  552.   graphic_object();
  553.   ~graphic_object();
  554.   object_type type() = 0;
  555.   void print_text();
  556.   void add_text(text_item *, int);
  557.   void set_dotted(double);
  558.   void set_dashed(double);
  559.   void set_thickness(double);
  560.   void set_invisible();
  561.   virtual void set_fill(double);
  562. };
  563.  
  564. graphic_object::graphic_object() : ntext(0), text(0), aligned(0)
  565. {
  566. }
  567.  
  568. void graphic_object::set_dotted(double wid)
  569. {
  570.   lt.type = line_type::dotted;
  571.   lt.dash_width = wid;
  572. }
  573.  
  574. void graphic_object::set_dashed(double wid)
  575. {
  576.   lt.type = line_type::dashed;
  577.   lt.dash_width = wid;
  578. }
  579.  
  580. void graphic_object::set_thickness(double th)
  581. {
  582.   lt.thickness = th;
  583. }
  584.  
  585. void graphic_object::set_fill(double)
  586. {
  587. }
  588.  
  589. void graphic_object::set_invisible()
  590. {
  591.   lt.type = line_type::invisible;
  592. }
  593.  
  594. void graphic_object::add_text(text_item *t, int a)
  595. {
  596.   aligned = a;
  597.   int len = 0;
  598.   for (text_item *p = t; p; p = p->next)
  599.     len++;
  600.   if (len == 0)
  601.     text = 0;
  602.   else {
  603.     text = new text_piece[len];
  604.     for (p = t, len = 0; p; p = p->next, len++) {
  605.       text[len].text = p->text;
  606.       p->text = 0;
  607.       text[len].adj = p->adj;
  608.       text[len].filename = p->filename;
  609.       text[len].lineno = p->lineno;
  610.     }
  611.   }
  612.   ntext = len;
  613. }
  614.  
  615. void graphic_object::print_text()
  616. {
  617.   double angle = 0.0;
  618.   if (aligned) {
  619.     position d(end() - start());
  620.     if (d.x != 0.0 || d.y != 0.0)
  621.       angle = atan2(d.y, d.x);
  622.   }
  623.   if (text != 0)
  624.     out->text(center(), text, ntext, angle);
  625. }
  626.  
  627. graphic_object::~graphic_object()
  628. {
  629.   if (text)
  630.     ad_delete(ntext) text;
  631. }
  632.  
  633. class rectangle_object : public graphic_object {
  634. protected:
  635.   position cent;
  636.   position dim;
  637. public:
  638.   rectangle_object(const position &);
  639.   double width() { return dim.x; }
  640.   double height() { return dim.y; }
  641.   position origin() { return cent; }
  642.   position center() { return cent; }
  643.   position north() { return position(cent.x, cent.y + dim.y/2.0); }
  644.   position south() { return position(cent.x, cent.y - dim.y/2.0); }
  645.   position east() { return position(cent.x + dim.x/2.0, cent.y); }
  646.   position west() { return position(cent.x - dim.x/2.0, cent.y); }
  647.   position north_east() { return position(cent.x + dim.x/2.0, cent.y + dim.y/2.0); }
  648.   position north_west() { return position(cent.x - dim.x/2.0, cent.y + dim.y/2.0); }
  649.   position south_east() { return position(cent.x + dim.x/2.0, cent.y - dim.y/2.0); }
  650.   position south_west() { return position(cent.x - dim.x/2.0, cent.y - dim.y/2.0); }
  651.   object_type type() = 0;
  652.   void update_bounding_box(bounding_box *);
  653.   void move_by(const position &);
  654. };
  655.  
  656. rectangle_object::rectangle_object(const position &d)
  657. : dim(d)
  658. {
  659. }
  660.  
  661. void rectangle_object::update_bounding_box(bounding_box *p)
  662. {
  663.   p->encompass(cent - dim/2.0);
  664.   p->encompass(cent + dim/2.0);
  665. }
  666.  
  667. void rectangle_object::move_by(const position &a)
  668. {
  669.   cent += a;
  670. }
  671.  
  672. class closed_object : public rectangle_object {
  673. public:
  674.   closed_object(const position &);
  675.   object_type type() = 0;
  676.   void set_fill(double);
  677. protected:
  678.   double fill;            // < 0 if not filled
  679. };
  680.  
  681. closed_object::closed_object(const position &pos)
  682. : rectangle_object(pos), fill(-1.0)
  683. {
  684. }
  685.  
  686. void closed_object::set_fill(double f)
  687. {
  688.   assert(f >= 0.0);
  689.   fill = f;
  690. }
  691.  
  692.  
  693. class box_object : public closed_object {
  694.   double xrad;
  695.   double yrad;
  696. public:
  697.   box_object(const position &, double);
  698.   object_type type() { return BOX_OBJECT; }
  699.   void print();
  700.   position north_east();
  701.   position north_west();
  702.   position south_east();
  703.   position south_west();
  704. };
  705.  
  706. box_object::box_object(const position &pos, double r)
  707. : closed_object(pos), xrad(dim.x > 0 ? r : -r), yrad(dim.y > 0 ? r : -r)
  708. {
  709. }
  710.  
  711. const double CHOP_FACTOR = 1.0 - 1.0/M_SQRT2;
  712.  
  713. position box_object::north_east()
  714. {
  715.   return position(cent.x + dim.x/2.0 - CHOP_FACTOR*xrad,
  716.           cent.y + dim.y/2.0 - CHOP_FACTOR*yrad);
  717. }
  718.  
  719. position box_object::north_west()
  720. {
  721.   return position(cent.x - dim.x/2.0 + CHOP_FACTOR*xrad,
  722.           cent.y + dim.y/2.0 - CHOP_FACTOR*yrad);
  723. }
  724.  
  725. position box_object::south_east()
  726. {
  727.   return position(cent.x + dim.x/2.0 - CHOP_FACTOR*xrad,
  728.           cent.y - dim.y/2.0 + CHOP_FACTOR*yrad);
  729. }
  730.  
  731. position box_object::south_west()
  732. {
  733.   return position(cent.x - dim.x/2.0 + CHOP_FACTOR*xrad,
  734.           cent.y - dim.y/2.0 + CHOP_FACTOR*yrad);
  735. }
  736.  
  737. void box_object::print()
  738. {
  739.   if (lt.type == line_type::invisible && fill < 0.0)
  740.     return;
  741.   if (xrad == 0.0) {
  742.     distance dim2 = dim/2.0;
  743.     position vec[4];
  744.     vec[0] = cent + position(dim2.x, -dim2.y);
  745.     vec[1] = cent + position(dim2.x, dim2.y);
  746.     vec[2] = cent + position(-dim2.x, dim2.y);
  747.     vec[3] = cent + position(-dim2.x, -dim2.y);
  748.     out->polygon(vec, 4, lt, fill);
  749.   }
  750.   else {
  751.     distance abs_dim(fabs(dim.x), fabs(dim.y));
  752.     out->rounded_box(cent, abs_dim, fabs(xrad), lt, fill);
  753.   }
  754. }
  755.  
  756. graphic_object *object_spec::make_box(position *curpos, direction *dirp)
  757. {
  758.   static double last_box_height;
  759.   static double last_box_width;
  760.   static double last_box_radius;
  761.   static int have_last_box = 0;
  762.   if (!(flags & HAS_HEIGHT)) {
  763.     if ((flags & IS_SAME) && have_last_box)
  764.       height = last_box_height;
  765.     else
  766.       lookup_variable("boxht", &height);
  767.   }
  768.   if (!(flags & HAS_WIDTH)) {
  769.     if ((flags & IS_SAME) && have_last_box)
  770.       width = last_box_width;
  771.     else
  772.       lookup_variable("boxwid", &width);
  773.   }
  774.   if (!(flags & HAS_RADIUS)) {
  775.     if ((flags & IS_SAME) && have_last_box)
  776.       radius = last_box_radius;
  777.     else
  778.       lookup_variable("boxrad", &radius);
  779.   }
  780.   last_box_width = width;
  781.   last_box_height = height;
  782.   last_box_radius = radius;
  783.   have_last_box = 1;
  784.   radius = fabs(radius);
  785.   if (radius*2.0 > fabs(width))
  786.     radius = fabs(width/2.0);
  787.   if (radius*2.0 > fabs(height))
  788.     radius = fabs(height/2.0);
  789.   box_object *p = new box_object(position(width, height), radius);
  790.   if (!position_rectangle(p, curpos, dirp)) {
  791.     delete p;
  792.     p = 0;
  793.   }
  794.   return p;
  795. }
  796.  
  797. // return non-zero for success
  798.  
  799. int object_spec::position_rectangle(rectangle_object *p,
  800.                     position *curpos, direction *dirp)
  801. {
  802.   position pos;
  803.   dir = *dirp;            // ignore any direction in attribute list
  804.   position motion;
  805.   switch (dir) {
  806.   case UP_DIRECTION:
  807.     motion.y = p->height()/2.0;
  808.     break;
  809.   case DOWN_DIRECTION:
  810.     motion.y = -p->height()/2.0;
  811.     break;
  812.   case LEFT_DIRECTION:
  813.     motion.x = -p->width()/2.0;
  814.     break;
  815.   case RIGHT_DIRECTION:
  816.     motion.x = p->width()/2.0;
  817.     break;
  818.   default:
  819.     assert(0);
  820.   }
  821.   if (flags & HAS_AT) {
  822.     pos = at;
  823.     if (flags & HAS_WITH) {
  824.       place offset;
  825.       place here;
  826.       here.obj = p;
  827.       if (!with->follow(here, &offset))
  828.     return 0;
  829.       pos -= offset;
  830.     }
  831.   }
  832.   else {
  833.     pos = *curpos;
  834.     pos += motion;
  835.   }
  836.   p->move_by(pos);
  837.   pos += motion;
  838.   *curpos = pos;
  839.   return 1;
  840. }
  841.  
  842. class block_object : public rectangle_object {
  843.   object_list oblist;
  844.   PTABLE(place) *tbl;
  845. public:
  846.   block_object(const position &, const object_list &ol, PTABLE(place) *t);
  847.   ~block_object();
  848.   place *find_label(const char *);
  849.   object_type type();
  850.   void move_by(const position &);
  851.   void print();
  852. };
  853.  
  854. block_object::block_object(const position &d, const object_list &ol,
  855.                PTABLE(place) *t)
  856. : oblist(ol), tbl(t), rectangle_object(d)
  857. {
  858. }
  859.  
  860. block_object::~block_object()
  861. {
  862.   delete tbl;
  863.   object *p = oblist.head;
  864.   while (p != 0) {
  865.     object *tem = p;
  866.     p = p->next;
  867.     delete tem;
  868.   }
  869. }
  870.  
  871. void block_object::print()
  872. {
  873.   out->begin_block(south_west(), north_east());
  874.   print_object_list(oblist.head);
  875.   out->end_block();
  876. }
  877.  
  878. static void adjust_objectless_places(PTABLE(place) *tbl, const position &a)
  879. {
  880.   // Adjust all the labels that aren't attached to objects.
  881.   PTABLE_ITERATOR(place) iter(tbl);
  882.   const char *key;
  883.   place *pl;
  884.   while (iter.next(&key, &pl))
  885.     if (key && csupper(key[0]) && pl->obj == 0) {
  886.       pl->x += a.x;
  887.       pl->y += a.y;
  888.     }
  889. }
  890.  
  891. void block_object::move_by(const position &a)
  892. {
  893.   cent += a;
  894.   for (object *p = oblist.head; p; p = p->next)
  895.     p->move_by(a);
  896.   adjust_objectless_places(tbl, a);
  897. }
  898.  
  899.  
  900. place *block_object::find_label(const char *name)
  901. {
  902.   return tbl->lookup(name);
  903. }
  904.  
  905. object_type block_object::type()
  906. {
  907.   return BLOCK_OBJECT;
  908. }
  909.  
  910. graphic_object *object_spec::make_block(position *curpos, direction *dirp)
  911. {
  912.   bounding_box bb;
  913.   for (object *p = oblist.head; p; p = p->next)
  914.     p->update_bounding_box(&bb);
  915.   position dim;
  916.   if (!bb.blank) {
  917.     position m = -(bb.ll + bb.ur)/2.0;
  918.     for (object *p = oblist.head; p; p = p->next)
  919.       p->move_by(m);
  920.     adjust_objectless_places(tbl, m);
  921.     dim = bb.ur - bb.ll;
  922.   }
  923.   if (flags & HAS_WIDTH)
  924.     dim.x = width;
  925.   if (flags & HAS_HEIGHT)
  926.     dim.y = height;
  927.   block_object *block = new block_object(dim, oblist, tbl);
  928.   if (!position_rectangle(block, curpos, dirp)) {
  929.     delete block;
  930.     block = 0;
  931.   }
  932.   tbl = 0;
  933.   oblist.head = oblist.tail = 0;
  934.   return block;
  935. }
  936.  
  937. class text_object : public rectangle_object {
  938. public:
  939.   text_object(const position &);
  940.   object_type type() { return TEXT_OBJECT; }
  941. };
  942.  
  943. text_object::text_object(const position &d)
  944. : rectangle_object(d)
  945. {
  946. }
  947.  
  948. graphic_object *object_spec::make_text(position *curpos, direction *dirp)
  949. {
  950.   if (!(flags & HAS_HEIGHT)) {
  951.     lookup_variable("textht", &height);
  952.     int nitems = 0;
  953.     for (text_item *t = text; t; t = t->next)
  954.       nitems++;
  955.     height *= nitems;
  956.   }
  957.   if (!(flags & HAS_WIDTH))
  958.     lookup_variable("textwid", &width);
  959.   text_object *p = new text_object(position(width, height));
  960.   if (!position_rectangle(p, curpos, dirp)) {
  961.     delete p;
  962.     p = 0;
  963.   }
  964.   return p;
  965. }
  966.  
  967.  
  968. class ellipse_object : public closed_object {
  969. public:
  970.   ellipse_object(const position &);
  971.   position north_east() { return position(cent.x + dim.x/(M_SQRT2*2.0),
  972.                       cent.y + dim.y/(M_SQRT2*2.0)); }
  973.   position north_west() { return position(cent.x - dim.x/(M_SQRT2*2.0),
  974.                       cent.y + dim.y/(M_SQRT2*2.0)); }
  975.   position south_east() { return position(cent.x + dim.x/(M_SQRT2*2.0),
  976.                       cent.y - dim.y/(M_SQRT2*2.0)); }
  977.   position south_west() { return position(cent.x - dim.x/(M_SQRT2*2.0),
  978.                       cent.y - dim.y/(M_SQRT2*2.0)); }
  979.   double radius() { return dim.x/2.0; }
  980.   object_type type() { return ELLIPSE_OBJECT; }
  981.   void print();
  982. };
  983.  
  984. ellipse_object::ellipse_object(const position &d)
  985. : closed_object(d)
  986. {
  987. }
  988.  
  989. void ellipse_object::print()
  990. {
  991.   if (lt.type == line_type::invisible && fill < 0.0)
  992.     return;
  993.   out->ellipse(cent, dim, lt, fill);
  994. }
  995.  
  996. graphic_object *object_spec::make_ellipse(position *curpos, direction *dirp)
  997. {
  998.   static double last_ellipse_height;
  999.   static double last_ellipse_width;
  1000.   static int have_last_ellipse = 0;
  1001.   if (!(flags & HAS_HEIGHT)) {
  1002.     if ((flags & IS_SAME) && have_last_ellipse)
  1003.       height = last_ellipse_height;
  1004.     else
  1005.       lookup_variable("ellipseht", &height);
  1006.   }
  1007.   if (!(flags & HAS_WIDTH)) {
  1008.     if ((flags & IS_SAME) && have_last_ellipse)
  1009.       width = last_ellipse_width;
  1010.     else
  1011.       lookup_variable("ellipsewid", &width);
  1012.   }
  1013.   last_ellipse_width = width;
  1014.   last_ellipse_height = height;
  1015.   have_last_ellipse = 1;
  1016.   ellipse_object *p = new ellipse_object(position(width, height));
  1017.   if (!position_rectangle(p, curpos, dirp)) {
  1018.     delete p;
  1019.     return 0;
  1020.   }
  1021.   return p;
  1022. }
  1023.  
  1024. class circle_object : public ellipse_object {
  1025. public:
  1026.   circle_object(double);
  1027.   object_type type() { return CIRCLE_OBJECT; }
  1028.   void print();
  1029. };
  1030.  
  1031. circle_object::circle_object(double diam)
  1032. : ellipse_object(position(diam, diam))
  1033. {
  1034. }
  1035.  
  1036. void circle_object::print()
  1037. {
  1038.   if (lt.type == line_type::invisible && fill < 0.0)
  1039.     return;
  1040.   out->circle(cent, dim.x/2.0, lt, fill);
  1041. }
  1042.  
  1043. graphic_object *object_spec::make_circle(position *curpos, direction *dirp)
  1044. {
  1045.   static double last_circle_radius;
  1046.   static int have_last_circle = 0;
  1047.   if (!(flags & HAS_RADIUS)) {
  1048.     if ((flags & IS_SAME) && have_last_circle)
  1049.       radius = last_circle_radius;
  1050.     else
  1051.       lookup_variable("circlerad", &radius);
  1052.   }
  1053.   last_circle_radius = radius;
  1054.   have_last_circle = 1;
  1055.   circle_object *p = new circle_object(radius*2.0);
  1056.   if (!position_rectangle(p, curpos, dirp)) {
  1057.     delete p;
  1058.     return 0;
  1059.   }
  1060.   return p;
  1061. }
  1062.  
  1063. class move_object : public graphic_object {
  1064.   position strt;
  1065.   position en;
  1066. public:
  1067.   move_object(const position &s, const position &e);
  1068.   position origin() { return en; }
  1069.   object_type type() { return MOVE_OBJECT; }
  1070.   void update_bounding_box(bounding_box *);
  1071.   void move_by(const position &);
  1072. };
  1073.  
  1074. move_object::move_object(const position &s, const position &e)
  1075. : strt(s), en(e)
  1076. {
  1077. }
  1078.  
  1079. void move_object::update_bounding_box(bounding_box *p)
  1080. {
  1081.   p->encompass(strt);
  1082.   p->encompass(en);
  1083. }
  1084.  
  1085. void move_object::move_by(const position &a)
  1086. {
  1087.   strt += a;
  1088.   en += a;
  1089. }
  1090.  
  1091. graphic_object *object_spec::make_move(position *curpos, direction *dirp)
  1092. {
  1093.   static position last_move;
  1094.   static int have_last_move = 0;
  1095.   *dirp = dir;
  1096.   // No need to look at at since `at' attribute sets `from' attribute.
  1097.   position startpos = (flags & HAS_FROM) ? from : *curpos;
  1098.   if (!(flags & HAS_SEGMENT)) {
  1099.     if ((flags && IS_SAME) && have_last_move)
  1100.       segment_pos = last_move;
  1101.     else {
  1102.       switch (dir) {
  1103.       case UP_DIRECTION:
  1104.     segment_pos.y = segment_height;
  1105.     break;
  1106.       case DOWN_DIRECTION:
  1107.     segment_pos.y = -segment_height;
  1108.     break;
  1109.       case LEFT_DIRECTION:
  1110.     segment_pos.x = -segment_width;
  1111.     break;
  1112.       case RIGHT_DIRECTION:
  1113.     segment_pos.x = segment_width;
  1114.     break;
  1115.       default:
  1116.     assert(0);
  1117.       }
  1118.     }
  1119.   }
  1120.   segment_list = new segment(segment_pos, segment_is_absolute, segment_list);
  1121.   // Reverse the segment_list so that it's in forward order.
  1122.   segment *old = segment_list;
  1123.   segment_list = 0;
  1124.   while (old != 0) {
  1125.     segment *tem = old->next;
  1126.     old->next = segment_list;
  1127.     segment_list = old;
  1128.     old = tem;
  1129.   }
  1130.   // Compute the end position.
  1131.   position endpos = startpos;
  1132.   for (segment *s = segment_list; s; s = s->next)
  1133.     if (s->is_absolute)
  1134.       endpos = s->pos;
  1135.     else 
  1136.       endpos += s->pos;
  1137.   have_last_move = 1;
  1138.   last_move = endpos - startpos;
  1139.   move_object *p = new move_object(startpos, endpos);
  1140.   *curpos = endpos;
  1141.   return p;
  1142. }
  1143.  
  1144. class linear_object : public graphic_object {
  1145. protected:
  1146.   char arrow_at_start;
  1147.   char arrow_at_end;
  1148.   arrow_head_type aht;
  1149.   position strt;
  1150.   position en;
  1151. public:
  1152.   linear_object(const position &s, const position &e);
  1153.   position start() { return strt; }
  1154.   position end() { return en; }
  1155.   void move_by(const position &);
  1156.   void update_bounding_box(bounding_box *) = 0;
  1157.   object_type type() = 0;
  1158.   void add_arrows(int at_start, int at_end, const arrow_head_type &);
  1159. };
  1160.  
  1161. class line_object : public linear_object {
  1162. protected:
  1163.   position *v;
  1164.   int n;
  1165. public:
  1166.   line_object(const position &s, const position &e, position *, int);
  1167.   ~line_object();
  1168.   position origin() { return strt; }
  1169.   position center() { return (strt + en)/2.0; }
  1170.   position north() { return (en.y - strt.y) > 0 ? en : strt; }
  1171.   position south() { return (en.y - strt.y) < 0 ? en : strt; }
  1172.   position east() { return (en.x - strt.x) > 0 ? en : strt; }
  1173.   position west() { return (en.x - strt.x) < 0 ? en : strt; }
  1174.   object_type type() { return LINE_OBJECT; }
  1175.   void update_bounding_box(bounding_box *);
  1176.   void print();
  1177.   void move_by(const position &);
  1178. };
  1179.  
  1180. class arrow_object : public line_object {
  1181. public:
  1182.   arrow_object(const position &, const position &, position *, int);
  1183.   object_type type() { return ARROW_OBJECT; }
  1184. };
  1185.  
  1186. class spline_object : public line_object {
  1187. public:
  1188.   spline_object(const position &, const position &, position *, int);
  1189.   object_type type() { return SPLINE_OBJECT; }
  1190.   void print();
  1191.   void update_bounding_box(bounding_box *);
  1192. };
  1193.  
  1194. linear_object::linear_object(const position &s, const position &e)
  1195. : strt(s), en(e), arrow_at_start(0), arrow_at_end(0)
  1196. {
  1197. }
  1198.  
  1199. void linear_object::move_by(const position &a)
  1200. {
  1201.   strt += a;
  1202.   en += a;
  1203. }
  1204.  
  1205. void linear_object::add_arrows(int at_start, int at_end,
  1206.                    const arrow_head_type &a)
  1207. {
  1208.   arrow_at_start = at_start;
  1209.   arrow_at_end = at_end;
  1210.   aht = a;
  1211. }
  1212.  
  1213. line_object::line_object(const position &s, const position &e,
  1214.              position *p, int i)
  1215. : v(p), n(i), linear_object(s, e)
  1216. {
  1217. }
  1218.  
  1219. void line_object::print()
  1220. {
  1221.   if (lt.type == line_type::invisible)
  1222.     return;
  1223.   out->line(strt, v, n, lt);
  1224.   if (arrow_at_start)
  1225.     draw_arrow(strt, strt-v[0], aht, lt);
  1226.   if (arrow_at_end)
  1227.     draw_arrow(en, v[n-1] - (n > 1 ? v[n - 2] : strt), aht, lt);
  1228. }
  1229.  
  1230. void line_object::update_bounding_box(bounding_box *p)
  1231. {
  1232.   p->encompass(strt);
  1233.   for (int i = 0; i < n; i++)
  1234.     p->encompass(v[i]);
  1235. }
  1236.  
  1237. void line_object::move_by(const position &pos)
  1238. {
  1239.   linear_object::move_by(pos);
  1240.   for (int i = 0; i < n; i++)
  1241.     v[i] += pos;
  1242. }
  1243.   
  1244. void spline_object::update_bounding_box(bounding_box *p)
  1245. {
  1246.   p->encompass(strt);
  1247.   p->encompass(en);
  1248.   /*
  1249.  
  1250.   If
  1251.  
  1252.   p1 = q1/2 + q2/2
  1253.   p2 = q1/6 + q2*5/6
  1254.   p3 = q2*5/6 + q3/6
  1255.   p4 = q2/2 + q3/2
  1256.   [ the points for the Bezier cubic ]
  1257.  
  1258.   and
  1259.  
  1260.   t = .5
  1261.  
  1262.   then
  1263.  
  1264.   (1-t)^3*p1 + 3*t*(t - 1)^2*p2 + 3*t^2*(1-t)*p3 + t^3*p4
  1265.   [ the equation for the Bezier cubic ]
  1266.  
  1267.   = .125*q1 + .75*q2 + .125*q3
  1268.  
  1269.   */
  1270.   for (int i = 1; i < n; i++)
  1271.     p->encompass((i == 1 ? strt : v[i-2])*.125 + v[i-1]*.75 + v[i]*.125);
  1272. }
  1273.  
  1274. arrow_object::arrow_object(const position &s, const position &e,
  1275.                position *p, int i)
  1276. : line_object(s, e, p, i)
  1277. {
  1278. }
  1279.  
  1280. spline_object::spline_object(const position &s, const position &e,
  1281.                  position *p, int i)
  1282. : line_object(s, e, p, i)
  1283. {
  1284. }
  1285.  
  1286. void spline_object::print()
  1287. {
  1288.   if (lt.type == line_type::invisible)
  1289.     return;
  1290.   out->spline(strt, v, n, lt);
  1291.   if (arrow_at_start)
  1292.     draw_arrow(strt, strt-v[0], aht, lt);
  1293.   if (arrow_at_end)
  1294.     draw_arrow(en, v[n-1] - (n > 1 ? v[n - 2] : strt), aht, lt);
  1295. }
  1296.  
  1297. line_object::~line_object()
  1298. {
  1299.   a_delete v;
  1300. }
  1301.  
  1302. linear_object *object_spec::make_line(position *curpos, direction *dirp)
  1303. {
  1304.   static position last_line;
  1305.   static int have_last_line = 0;
  1306.   *dirp = dir;
  1307.   // No need to look at at since `at' attribute sets `from' attribute.
  1308.   position startpos = (flags & HAS_FROM) ? from : *curpos;
  1309.   if (!(flags & HAS_SEGMENT)) {
  1310.     if ((flags & IS_SAME) && (type == LINE_OBJECT || type == ARROW_OBJECT)
  1311.     && have_last_line)
  1312.       segment_pos = last_line;
  1313.     else 
  1314.       switch (dir) {
  1315.       case UP_DIRECTION:
  1316.     segment_pos.y = segment_height;
  1317.     break;
  1318.       case DOWN_DIRECTION:
  1319.     segment_pos.y = -segment_height;
  1320.     break;
  1321.       case LEFT_DIRECTION:
  1322.     segment_pos.x = -segment_width;
  1323.     break;
  1324.       case RIGHT_DIRECTION:
  1325.     segment_pos.x = segment_width;
  1326.     break;
  1327.       default:
  1328.     assert(0);
  1329.       }
  1330.   }
  1331.   segment_list = new segment(segment_pos, segment_is_absolute, segment_list);
  1332.   // reverse the segment_list so that it's in forward order
  1333.   segment *old = segment_list;
  1334.   segment_list = 0;
  1335.   while (old != 0) {
  1336.     segment *tem = old->next;
  1337.     old->next = segment_list;
  1338.     segment_list = old;
  1339.     old = tem;
  1340.   }
  1341.   // Absolutise all movements
  1342.   position endpos = startpos;
  1343.   int nsegments = 0;
  1344.   for (segment *s = segment_list; s; s = s->next, nsegments++)
  1345.     if (s->is_absolute)
  1346.       endpos = s->pos;
  1347.     else {
  1348.       endpos += s->pos;
  1349.       s->pos = endpos;
  1350.       s->is_absolute = 1;    // to avoid confusion
  1351.     }
  1352.   // handle chop
  1353.   line_object *p = 0;
  1354.   position *v = new position[nsegments];
  1355.   int i = 0;
  1356.   for (s = segment_list; s; s = s->next, i++)
  1357.     v[i] = s->pos;
  1358.   if (flags & IS_DEFAULT_CHOPPED) {
  1359.     lookup_variable("circlerad", &start_chop);
  1360.     end_chop = start_chop;
  1361.     flags |= IS_CHOPPED;
  1362.   }
  1363.   if (flags & IS_CHOPPED) {
  1364.     position start_chop_vec, end_chop_vec;
  1365.     if (start_chop != 0.0) {
  1366.       start_chop_vec = v[0] - startpos;
  1367.       start_chop_vec *= start_chop / hypot(start_chop_vec);
  1368.     }
  1369.     if (end_chop != 0.0) {
  1370.       end_chop_vec = (v[nsegments - 1]
  1371.               - (nsegments > 1 ? v[nsegments - 2] : startpos));
  1372.       end_chop_vec *= end_chop / hypot(end_chop_vec);
  1373.     }
  1374.     startpos += start_chop_vec;
  1375.     v[nsegments - 1] -= end_chop_vec;
  1376.     endpos -= end_chop_vec;
  1377.   }
  1378.   switch (type) {
  1379.   case SPLINE_OBJECT:
  1380.     p = new spline_object(startpos, endpos, v, nsegments);
  1381.     break;
  1382.   case ARROW_OBJECT:
  1383.     p = new arrow_object(startpos, endpos, v, nsegments);
  1384.     break;
  1385.   case LINE_OBJECT:
  1386.     p = new line_object(startpos, endpos, v, nsegments);
  1387.     break;
  1388.   default:
  1389.     assert(0);
  1390.   }
  1391.   have_last_line = 1;
  1392.   last_line = endpos - startpos;
  1393.   *curpos = endpos;
  1394.   return p;
  1395. }
  1396.  
  1397. class arc_object : public linear_object {
  1398.   int clockwise;
  1399.   position cent;
  1400.   double rad;
  1401. public:
  1402.   arc_object(int, const position &, const position &, const position &);
  1403.   position origin() { return cent; }
  1404.   position center() { return cent; }
  1405.   double radius() { return rad; }
  1406.   position north();
  1407.   position south();
  1408.   position east();
  1409.   position west();
  1410.   position north_east();
  1411.   position north_west();
  1412.   position south_east();
  1413.   position south_west();
  1414.   void update_bounding_box(bounding_box *);
  1415.   object_type type() { return ARC_OBJECT; }
  1416.   void print();
  1417.   void move_by(const position &pos);
  1418. };
  1419.  
  1420. arc_object::arc_object(int cw, const position &s, const position &e,
  1421.                const position &c)
  1422. : linear_object(s, e), clockwise(cw), cent(c)
  1423. {
  1424.   rad = hypot(c - s);
  1425. }
  1426.  
  1427. void arc_object::move_by(const position &pos)
  1428. {
  1429.   linear_object::move_by(pos);
  1430.   cent += pos;
  1431. }
  1432.  
  1433. // we get arc corners from the corresponding circle
  1434.  
  1435. position arc_object::north()
  1436. {
  1437.   position result(cent);
  1438.   result.y += rad;
  1439.   return result;
  1440. }
  1441.  
  1442. position arc_object::south()
  1443. {
  1444.   position result(cent);
  1445.   result.y -= rad;
  1446.   return result;
  1447. }
  1448.  
  1449. position arc_object::east()
  1450. {
  1451.   position result(cent);
  1452.   result.x += rad;
  1453.   return result;
  1454. }
  1455.  
  1456. position arc_object::west()
  1457. {
  1458.   position result(cent);
  1459.   result.x -= rad;
  1460.   return result;
  1461. }
  1462.  
  1463. position arc_object::north_east()
  1464. {
  1465.   position result(cent);
  1466.   result.x += rad/M_SQRT2;
  1467.   result.y += rad/M_SQRT2;
  1468.   return result;
  1469. }
  1470.  
  1471. position arc_object::north_west()
  1472. {
  1473.   position result(cent);
  1474.   result.x -= rad/M_SQRT2;
  1475.   result.y += rad/M_SQRT2;
  1476.   return result;
  1477. }
  1478.  
  1479. position arc_object::south_east()
  1480. {
  1481.   position result(cent);
  1482.   result.x += rad/M_SQRT2;
  1483.   result.y -= rad/M_SQRT2;
  1484.   return result;
  1485. }
  1486.  
  1487. position arc_object::south_west()
  1488. {
  1489.   position result(cent);
  1490.   result.x -= rad/M_SQRT2;
  1491.   result.y -= rad/M_SQRT2;
  1492.   return result;
  1493. }
  1494.  
  1495.  
  1496. void arc_object::print()
  1497. {
  1498.   if (lt.type == line_type::invisible)
  1499.     return;
  1500.   if (clockwise)
  1501.     out->arc(en, cent, strt, lt);
  1502.   else
  1503.     out->arc(strt, cent, en, lt);
  1504.   if (arrow_at_start) {
  1505.     position c = cent - strt;
  1506.     draw_arrow(strt,
  1507.            (clockwise ? position(c.y, -c.x) : position(-c.y, c.x)),
  1508.            aht, lt);
  1509.   }
  1510.   if (arrow_at_end) {
  1511.     position e = en - cent;
  1512.     draw_arrow(en,
  1513.            (clockwise ? position(e.y, -e.x) : position(-e.y, e.x)),
  1514.            aht, lt);
  1515.   }
  1516. }
  1517.  
  1518. inline double max(double a, double b)
  1519. {
  1520.   return a > b ? a : b;
  1521. }
  1522.  
  1523. void arc_object::update_bounding_box(bounding_box *p)
  1524. {
  1525.   p->encompass(strt);
  1526.   p->encompass(en);
  1527.   position start_offset = strt - cent;
  1528.   if (start_offset.x == 0.0 && start_offset.y == 0.0)
  1529.     return;
  1530.   position end_offset = en  - cent;
  1531.   if (end_offset.x == 0.0 && end_offset.y == 0.0)
  1532.     return;
  1533.   double start_quad = atan2(start_offset.y, start_offset.x)/(M_PI/2.0);
  1534.   double end_quad = atan2(end_offset.y, end_offset.x)/(M_PI/2.0);
  1535.   if (clockwise) {
  1536.     double temp = start_quad;
  1537.     start_quad = end_quad;
  1538.     end_quad = temp;
  1539.   }
  1540.   if (start_quad < 0.0)
  1541.     start_quad += 4.0;
  1542.   while (end_quad <= start_quad)
  1543.     end_quad += 4.0;
  1544.   double radius = max(hypot(start_offset), hypot(end_offset));
  1545.   for (int q = int(start_quad) + 1; q < end_quad; q++) {
  1546.     position offset;
  1547.     switch (q % 4) {
  1548.     case 0:
  1549.       offset.x = radius;
  1550.       break;
  1551.     case 1:
  1552.       offset.y = radius;
  1553.       break;
  1554.     case 2:
  1555.       offset.x = -radius;
  1556.       break;
  1557.     case 3:
  1558.       offset.y = -radius;
  1559.       break;
  1560.     }
  1561.     p->encompass(cent + offset);
  1562.   }
  1563. }
  1564.  
  1565. // We ignore the with attribute. The at attribute always refers to the center.
  1566.  
  1567. linear_object *object_spec::make_arc(position *curpos, direction *dirp)
  1568. {
  1569.   *dirp = dir;
  1570.   int cw = (flags & IS_CLOCKWISE) != 0;
  1571.   // compute the start
  1572.   position startpos;
  1573.   if (flags & HAS_FROM)
  1574.     startpos = from;
  1575.   else
  1576.     startpos = *curpos;
  1577.   if (!(flags & HAS_RADIUS))
  1578.     lookup_variable("arcrad", &radius);
  1579.   // compute the end
  1580.   position endpos;
  1581.   if (flags & HAS_TO)
  1582.     endpos = to;
  1583.   else {
  1584.     position m(radius, radius);
  1585.     // Adjust the signs.
  1586.     if (cw) {
  1587.       if (dir == DOWN_DIRECTION || dir == LEFT_DIRECTION)
  1588.     m.x = -m.x;
  1589.       if (dir == DOWN_DIRECTION || dir == RIGHT_DIRECTION)
  1590.     m.y = -m.y;
  1591.       *dirp = direction((dir + 3) % 4);
  1592.     }
  1593.     else {
  1594.       if (dir == UP_DIRECTION || dir == LEFT_DIRECTION)
  1595.     m.x = -m.x;
  1596.       if (dir == DOWN_DIRECTION || dir == LEFT_DIRECTION)
  1597.     m.y = -m.y;
  1598.       *dirp = direction((dir + 1) % 4);
  1599.     }
  1600.     endpos = startpos + m;
  1601.   }
  1602.   // compute the center
  1603.   position centerpos;
  1604.   if (flags & HAS_AT)
  1605.     centerpos = at;
  1606.   else if (startpos == endpos)
  1607.     centerpos = startpos;
  1608.   else {
  1609.     position h = (endpos - startpos)/2.0;
  1610.     double d = hypot(h);
  1611.     if (radius <= 0)
  1612.       radius = .25;
  1613.     // make the radius big enough
  1614.     while (radius < d)
  1615.       radius *= 2.0;
  1616.     double alpha = acos(d/radius);
  1617.     double theta = atan2(h.y, h.x);
  1618.     if (cw)
  1619.       theta -= alpha;
  1620.     else
  1621.       theta += alpha;
  1622.     centerpos = position(cos(theta), sin(theta))*radius + startpos;
  1623.   }
  1624.   arc_object *p = new arc_object(cw, startpos, endpos, centerpos);
  1625.   *curpos = endpos;
  1626.   return p;
  1627. }
  1628.  
  1629. graphic_object *object_spec::make_linear(position *curpos, direction *dirp)
  1630. {
  1631.   linear_object *obj;
  1632.   if (type == ARC_OBJECT)
  1633.     obj = make_arc(curpos, dirp);
  1634.   else
  1635.     obj = make_line(curpos, dirp);
  1636.   if (type == ARROW_OBJECT
  1637.       && (flags & (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD)) == 0)
  1638.     flags |= HAS_RIGHT_ARROW_HEAD;
  1639.   if (obj && (flags & (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD))) {
  1640.     arrow_head_type a;
  1641.     int at_start = (flags & HAS_LEFT_ARROW_HEAD) != 0;
  1642.     int at_end = (flags & HAS_RIGHT_ARROW_HEAD) != 0;
  1643.     if (flags & HAS_HEIGHT)
  1644.       a.height = height;
  1645.     else
  1646.       lookup_variable("arrowht", &a.height);
  1647.     if (flags & HAS_WIDTH)
  1648.       a.width = width;
  1649.     else
  1650.       lookup_variable("arrowwid", &a.width);
  1651.     double solid;
  1652.     lookup_variable("arrowhead", &solid);
  1653.     a.solid = solid != 0.0;
  1654.     obj->add_arrows(at_start, at_end, a);
  1655.   }
  1656.   return obj;
  1657. }
  1658.  
  1659. object *object_spec::make_object(position *curpos, direction *dirp)
  1660. {
  1661.   graphic_object *obj = 0;
  1662.   switch (type) {
  1663.   case BLOCK_OBJECT:
  1664.     obj = make_block(curpos, dirp);
  1665.     break;
  1666.   case BOX_OBJECT:
  1667.     obj = make_box(curpos, dirp);
  1668.     break;
  1669.   case TEXT_OBJECT:
  1670.     obj = make_text(curpos, dirp);
  1671.     break;
  1672.   case ELLIPSE_OBJECT:
  1673.     obj = make_ellipse(curpos, dirp);
  1674.     break;
  1675.   case CIRCLE_OBJECT:
  1676.     obj = make_circle(curpos, dirp);
  1677.     break;
  1678.   case MOVE_OBJECT:
  1679.     obj = make_move(curpos, dirp);
  1680.     break;
  1681.   case ARC_OBJECT:
  1682.   case LINE_OBJECT:
  1683.   case SPLINE_OBJECT:
  1684.   case ARROW_OBJECT:
  1685.     obj = make_linear(curpos, dirp);
  1686.     break;
  1687.   case MARK_OBJECT:
  1688.   case OTHER_OBJECT:
  1689.   default:
  1690.     assert(0);
  1691.     break;
  1692.   }
  1693.   if (obj) {
  1694.     if (flags & IS_INVISIBLE)
  1695.       obj->set_invisible();
  1696.     if (text != 0)
  1697.       obj->add_text(text, (flags & IS_ALIGNED) != 0);
  1698.     if (flags & IS_DOTTED)
  1699.       obj->set_dotted(dash_width);
  1700.     else if (flags & IS_DASHED)
  1701.       obj->set_dashed(dash_width);
  1702.     double th;
  1703.     if (flags & HAS_THICKNESS)
  1704.       th = thickness;
  1705.     else
  1706.       lookup_variable("linethick", &th);
  1707.     obj->set_thickness(th);
  1708.     if (flags & (IS_DEFAULT_FILLED|IS_FILLED)) {
  1709.       if (flags & IS_DEFAULT_FILLED)
  1710.     lookup_variable("fillval", &fill);
  1711.       if (fill < 0.0)
  1712.     error("bad fill value %1", fill);
  1713.       else
  1714.     obj->set_fill(fill);
  1715.     }
  1716.   }
  1717.   return obj;
  1718. }
  1719.  
  1720. struct string_list {
  1721.   string_list *next;
  1722.   char *str;
  1723.   string_list(char *);
  1724.   ~string_list();
  1725. };
  1726.  
  1727. string_list::string_list(char *s)
  1728. : next(0), str(s)
  1729. {
  1730. }
  1731.  
  1732. string_list::~string_list()
  1733. {
  1734.   a_delete str;
  1735. }
  1736.   
  1737. /* A path is used to hold the argument to the with attribute. For example,
  1738. `.nw' or `.A.s' or `.A'. The major operation on a path is to take a 
  1739. place and follow the path through the place to place within the place.
  1740. Note that `.A.B.C.sw' will work. */
  1741.  
  1742. path::path(corner c)
  1743. : label_list(0), crn(c)
  1744. {
  1745. }
  1746.  
  1747. path::path(char *l, corner c)
  1748. : crn(c)
  1749. {
  1750.   label_list = new string_list(l);
  1751. }
  1752.  
  1753. path::~path()
  1754. {
  1755.   while (label_list) {
  1756.     string_list *tem = label_list;
  1757.     label_list = label_list->next;
  1758.     delete tem;
  1759.   }
  1760. }
  1761.  
  1762. void path::append(corner c)
  1763. {
  1764.   assert(crn == 0);
  1765.   crn = c;
  1766. }
  1767.  
  1768. void path::append(char *s)
  1769. {
  1770.   for (string_list **p = &label_list; *p; p = &(*p)->next)
  1771.     ;
  1772.   *p = new string_list(s);
  1773. }
  1774.  
  1775. // return non-zero for success
  1776.  
  1777. int path::follow(const place &pl, place *result) const
  1778. {
  1779.   const place *p = &pl;
  1780.   for (string_list *lb = label_list; lb; lb = lb->next)
  1781.     if (p->obj == 0 || (p = p->obj->find_label(lb->str)) == 0) {
  1782.       lex_error("object does not contain a place `%1'", lb->str);
  1783.       return 0;
  1784.     }
  1785.   if (crn == 0 || p->obj == 0)
  1786.     *result = *p;
  1787.   else {
  1788.     position pos = ((p->obj)->*(crn))();
  1789.     result->x = pos.x;
  1790.     result->y = pos.y;
  1791.     result->obj = 0;
  1792.   }
  1793.   return 1;
  1794. }
  1795.  
  1796. void print_object_list(object *p)
  1797. {
  1798.   for (; p; p = p->next) {
  1799.     p->print();
  1800.     p->print_text();
  1801.   }
  1802. }
  1803.  
  1804. void print_picture(object *obj)
  1805. {
  1806.   bounding_box bb;
  1807.   for (object *p = obj; p; p = p->next)
  1808.     p->update_bounding_box(&bb);
  1809.   double scale;
  1810.   lookup_variable("scale", &scale);
  1811.   out->start_picture(scale, bb.ll, bb.ur);
  1812.   print_object_list(obj);
  1813.   out->finish_picture();
  1814. }
  1815.  
  1816.