2 /* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 void print_object_list(object *);
28 line_type::line_type()
29 : type(solid), thickness(1.0)
33 output::output() : args(0), desired_height(0.0), desired_width(0.0)
42 void output::set_desired_width_height(double wid, double ht)
48 void output::set_args(const char *s)
51 if (s == 0 || *s == '\0')
57 int output::supports_filled_polygons()
62 void output::begin_block(const position &, const position &)
66 void output::end_block()
70 double output::compute_scale(double sc, const position &ll, const position &ur)
72 distance dim = ur - ll;
73 if (desired_width != 0.0 || desired_height != 0.0) {
75 if (desired_width != 0.0) {
77 error("width specified for picture with zero width");
79 sc = dim.x/desired_width;
81 if (desired_height != 0.0) {
83 error("height specified for picture with zero height");
85 double tem = dim.y/desired_height;
90 return sc == 0.0 ? 1.0 : sc;
95 distance sdim = dim/sc;
96 double max_width = 0.0;
97 lookup_variable("maxpswid", &max_width);
98 double max_height = 0.0;
99 lookup_variable("maxpsht", &max_height);
100 if ((max_width > 0.0 && sdim.x > max_width)
101 || (max_height > 0.0 && sdim.y > max_height)) {
102 double xscale = dim.x/max_width;
103 double yscale = dim.y/max_height;
104 return xscale > yscale ? xscale : yscale;
111 position::position(const place &pl)
114 // Use two statements to work around bug in SGI C++.
115 object *tem = pl.obj;
116 *this = tem->origin();
124 position::position() : x(0.0), y(0.0)
128 position::position(double a, double b) : x(a), y(b)
133 int operator==(const position &a, const position &b)
135 return a.x == b.x && a.y == b.y;
138 int operator!=(const position &a, const position &b)
140 return a.x != b.x || a.y != b.y;
143 position &position::operator+=(const position &a)
150 position &position::operator-=(const position &a)
157 position &position::operator*=(double a)
164 position &position::operator/=(double a)
171 position operator-(const position &a)
173 return position(-a.x, -a.y);
176 position operator+(const position &a, const position &b)
178 return position(a.x + b.x, a.y + b.y);
181 position operator-(const position &a, const position &b)
183 return position(a.x - b.x, a.y - b.y);
186 position operator/(const position &a, double n)
188 return position(a.x/n, a.y/n);
191 position operator*(const position &a, double n)
193 return position(a.x*n, a.y*n);
198 double operator*(const position &a, const position &b)
200 return a.x*b.x + a.y*b.y;
203 double hypot(const position &a)
205 return hypot(a.x, a.y);
208 struct arrow_head_type {
214 void draw_arrow(const position &pos, const distance &dir,
215 const arrow_head_type &aht, const line_type <,
216 char *outline_color_for_fill)
218 double hyp = hypot(dir);
220 error("cannot draw arrow on object with zero length");
223 position base = -dir;
224 base *= aht.height/hyp;
225 position n(dir.y, -dir.x);
226 n *= aht.width/(hyp*2.0);
228 slt.type = line_type::solid;
229 if (aht.solid && out->supports_filled_polygons()) {
232 v[1] = pos + base + n;
233 v[2] = pos + base - n;
234 // fill with outline color
235 out->set_color(outline_color_for_fill, outline_color_for_fill);
236 out->polygon(v, 3, slt, 1);
241 v[1] = pos + base + n;
242 out->line(pos + base - n, v, 2, slt);
246 object::object() : prev(0), next(0)
254 void object::move_by(const position &)
262 void object::print_text()
271 struct bounding_box {
277 void encompass(const position &);
280 bounding_box::bounding_box()
285 void bounding_box::encompass(const position &pos)
304 void object::update_bounding_box(bounding_box *)
308 position object::origin()
310 return position(0.0,0.0);
313 position object::north()
318 position object::south()
323 position object::east()
328 position object::west()
333 position object::north_east()
338 position object::north_west()
343 position object::south_east()
348 position object::south_west()
353 position object::start()
358 position object::end()
363 position object::center()
368 double object::width()
373 double object::radius()
378 double object::height()
383 place *object::find_label(const char *)
388 segment::segment(const position &a, int n, segment *p)
389 : is_absolute(n), pos(a), next(p)
393 text_item::text_item(char *t, const char *fn, int ln)
394 : next(0), text(t), filename(fn), lineno(ln)
396 adj.h = CENTER_ADJUST;
400 text_item::~text_item()
405 object_spec::object_spec(object_type t) : type(t)
410 segment_width = segment_height = 0.0;
411 segment_is_absolute = 0;
414 dir = RIGHT_DIRECTION;
417 object_spec::~object_spec()
420 while (segment_list != 0) {
421 segment *tem = segment_list;
422 segment_list = segment_list->next;
425 object *p = oblist.head;
432 text_item *tem = text;
439 class command_object : public object {
441 const char *filename;
444 command_object(char *, const char *, int);
446 object_type type() { return OTHER_OBJECT; }
450 command_object::command_object(char *p, const char *fn, int ln)
451 : s(p), filename(fn), lineno(ln)
455 command_object::~command_object()
460 void command_object::print()
462 out->command(s, filename, lineno);
465 object *make_command_object(char *s, const char *fn, int ln)
467 return new command_object(s, fn, ln);
470 class mark_object : public object {
476 object *make_mark_object()
478 return new mark_object();
481 mark_object::mark_object()
485 object_type mark_object::type()
490 object_list::object_list() : head(0), tail(0)
494 void object_list::append(object *obj)
497 obj->next = obj->prev = 0;
508 void object_list::wrap_up_block(object_list *ol)
511 for (p = tail; p && p->type() != MARK_OBJECT; p = p->prev)
529 text_piece::text_piece()
530 : text(0), filename(0), lineno(-1)
532 adj.h = CENTER_ADJUST;
536 text_piece::~text_piece()
541 class graphic_object : public object {
552 object_type type() = 0;
554 void add_text(text_item *, int);
555 void set_dotted(double);
556 void set_dashed(double);
557 void set_thickness(double);
558 void set_invisible();
559 void set_outline_color(char *);
560 char *get_outline_color();
561 virtual void set_fill(double);
562 virtual void set_fill_color(char *);
565 graphic_object::graphic_object()
566 : ntext(0), text(0), aligned(0), outline_color(0), color_fill(0)
570 void graphic_object::set_dotted(double wid)
572 lt.type = line_type::dotted;
576 void graphic_object::set_dashed(double wid)
578 lt.type = line_type::dashed;
582 void graphic_object::set_thickness(double th)
587 void graphic_object::set_fill(double)
591 void graphic_object::set_fill_color(char *c)
596 void graphic_object::set_outline_color(char *c)
601 char *graphic_object::get_outline_color()
603 return outline_color;
606 void graphic_object::set_invisible()
608 lt.type = line_type::invisible;
611 void graphic_object::add_text(text_item *t, int a)
616 for (p = t; p; p = p->next)
621 text = new text_piece[len];
622 for (p = t, len = 0; p; p = p->next, len++) {
623 text[len].text = p->text;
625 text[len].adj = p->adj;
626 text[len].filename = p->filename;
627 text[len].lineno = p->lineno;
633 void graphic_object::print_text()
637 position d(end() - start());
638 if (d.x != 0.0 || d.y != 0.0)
639 angle = atan2(d.y, d.x);
642 out->set_color(color_fill, get_outline_color());
643 out->text(center(), text, ntext, angle);
648 graphic_object::~graphic_object()
651 ad_delete(ntext) text;
654 class rectangle_object : public graphic_object {
659 rectangle_object(const position &);
660 double width() { return dim.x; }
661 double height() { return dim.y; }
662 position origin() { return cent; }
663 position center() { return cent; }
664 position north() { return position(cent.x, cent.y + dim.y/2.0); }
665 position south() { return position(cent.x, cent.y - dim.y/2.0); }
666 position east() { return position(cent.x + dim.x/2.0, cent.y); }
667 position west() { return position(cent.x - dim.x/2.0, cent.y); }
668 position north_east() { return position(cent.x + dim.x/2.0, cent.y + dim.y/2.0); }
669 position north_west() { return position(cent.x - dim.x/2.0, cent.y + dim.y/2.0); }
670 position south_east() { return position(cent.x + dim.x/2.0, cent.y - dim.y/2.0); }
671 position south_west() { return position(cent.x - dim.x/2.0, cent.y - dim.y/2.0); }
672 object_type type() = 0;
673 void update_bounding_box(bounding_box *);
674 void move_by(const position &);
677 rectangle_object::rectangle_object(const position &d)
682 void rectangle_object::update_bounding_box(bounding_box *p)
684 p->encompass(cent - dim/2.0);
685 p->encompass(cent + dim/2.0);
688 void rectangle_object::move_by(const position &a)
693 class closed_object : public rectangle_object {
695 closed_object(const position &);
696 object_type type() = 0;
697 void set_fill(double);
698 void set_fill_color(char *fill);
700 double fill; // < 0 if not filled
701 char *color_fill; // = 0 if not colored
704 closed_object::closed_object(const position &pos)
705 : rectangle_object(pos), fill(-1.0), color_fill(0)
709 void closed_object::set_fill(double f)
715 void closed_object::set_fill_color(char *fill)
720 class box_object : public closed_object {
724 box_object(const position &, double);
725 object_type type() { return BOX_OBJECT; }
727 position north_east();
728 position north_west();
729 position south_east();
730 position south_west();
733 box_object::box_object(const position &pos, double r)
734 : closed_object(pos), xrad(dim.x > 0 ? r : -r), yrad(dim.y > 0 ? r : -r)
738 const double CHOP_FACTOR = 1.0 - 1.0/M_SQRT2;
740 position box_object::north_east()
742 return position(cent.x + dim.x/2.0 - CHOP_FACTOR*xrad,
743 cent.y + dim.y/2.0 - CHOP_FACTOR*yrad);
746 position box_object::north_west()
748 return position(cent.x - dim.x/2.0 + CHOP_FACTOR*xrad,
749 cent.y + dim.y/2.0 - CHOP_FACTOR*yrad);
752 position box_object::south_east()
754 return position(cent.x + dim.x/2.0 - CHOP_FACTOR*xrad,
755 cent.y - dim.y/2.0 + CHOP_FACTOR*yrad);
758 position box_object::south_west()
760 return position(cent.x - dim.x/2.0 + CHOP_FACTOR*xrad,
761 cent.y - dim.y/2.0 + CHOP_FACTOR*yrad);
764 void box_object::print()
766 if (lt.type == line_type::invisible && fill < 0.0 && color_fill == 0)
768 out->set_color(color_fill, graphic_object::get_outline_color());
770 distance dim2 = dim/2.0;
772 vec[0] = cent + position(dim2.x, -dim2.y);
773 vec[1] = cent + position(dim2.x, dim2.y);
774 vec[2] = cent + position(-dim2.x, dim2.y);
775 vec[3] = cent + position(-dim2.x, -dim2.y);
776 out->polygon(vec, 4, lt, fill);
779 distance abs_dim(fabs(dim.x), fabs(dim.y));
780 out->rounded_box(cent, abs_dim, fabs(xrad), lt, fill);
785 graphic_object *object_spec::make_box(position *curpos, direction *dirp)
787 static double last_box_height;
788 static double last_box_width;
789 static double last_box_radius;
790 static int have_last_box = 0;
791 if (!(flags & HAS_HEIGHT)) {
792 if ((flags & IS_SAME) && have_last_box)
793 height = last_box_height;
795 lookup_variable("boxht", &height);
797 if (!(flags & HAS_WIDTH)) {
798 if ((flags & IS_SAME) && have_last_box)
799 width = last_box_width;
801 lookup_variable("boxwid", &width);
803 if (!(flags & HAS_RADIUS)) {
804 if ((flags & IS_SAME) && have_last_box)
805 radius = last_box_radius;
807 lookup_variable("boxrad", &radius);
809 last_box_width = width;
810 last_box_height = height;
811 last_box_radius = radius;
813 radius = fabs(radius);
814 if (radius*2.0 > fabs(width))
815 radius = fabs(width/2.0);
816 if (radius*2.0 > fabs(height))
817 radius = fabs(height/2.0);
818 box_object *p = new box_object(position(width, height), radius);
819 if (!position_rectangle(p, curpos, dirp)) {
826 // return non-zero for success
828 int object_spec::position_rectangle(rectangle_object *p,
829 position *curpos, direction *dirp)
832 dir = *dirp; // ignore any direction in attribute list
836 motion.y = p->height()/2.0;
839 motion.y = -p->height()/2.0;
842 motion.x = -p->width()/2.0;
844 case RIGHT_DIRECTION:
845 motion.x = p->width()/2.0;
850 if (flags & HAS_AT) {
852 if (flags & HAS_WITH) {
856 if (!with->follow(here, &offset))
871 class block_object : public rectangle_object {
875 block_object(const position &, const object_list &ol, PTABLE(place) *t);
877 place *find_label(const char *);
879 void move_by(const position &);
883 block_object::block_object(const position &d, const object_list &ol,
885 : rectangle_object(d), oblist(ol), tbl(t)
889 block_object::~block_object()
892 object *p = oblist.head;
900 void block_object::print()
902 out->begin_block(south_west(), north_east());
903 print_object_list(oblist.head);
907 static void adjust_objectless_places(PTABLE(place) *tbl, const position &a)
909 // Adjust all the labels that aren't attached to objects.
910 PTABLE_ITERATOR(place) iter(tbl);
913 while (iter.next(&key, &pl))
914 if (key && csupper(key[0]) && pl->obj == 0) {
920 void block_object::move_by(const position &a)
923 for (object *p = oblist.head; p; p = p->next)
925 adjust_objectless_places(tbl, a);
929 place *block_object::find_label(const char *name)
931 return tbl->lookup(name);
934 object_type block_object::type()
939 graphic_object *object_spec::make_block(position *curpos, direction *dirp)
942 for (object *p = oblist.head; p; p = p->next)
943 p->update_bounding_box(&bb);
946 position m = -(bb.ll + bb.ur)/2.0;
947 for (object *p = oblist.head; p; p = p->next)
949 adjust_objectless_places(tbl, m);
952 if (flags & HAS_WIDTH)
954 if (flags & HAS_HEIGHT)
956 block_object *block = new block_object(dim, oblist, tbl);
957 if (!position_rectangle(block, curpos, dirp)) {
962 oblist.head = oblist.tail = 0;
966 class text_object : public rectangle_object {
968 text_object(const position &);
969 object_type type() { return TEXT_OBJECT; }
972 text_object::text_object(const position &d)
973 : rectangle_object(d)
977 graphic_object *object_spec::make_text(position *curpos, direction *dirp)
979 if (!(flags & HAS_HEIGHT)) {
980 lookup_variable("textht", &height);
982 for (text_item *t = text; t; t = t->next)
986 if (!(flags & HAS_WIDTH))
987 lookup_variable("textwid", &width);
988 text_object *p = new text_object(position(width, height));
989 if (!position_rectangle(p, curpos, dirp)) {
997 class ellipse_object : public closed_object {
999 ellipse_object(const position &);
1000 position north_east() { return position(cent.x + dim.x/(M_SQRT2*2.0),
1001 cent.y + dim.y/(M_SQRT2*2.0)); }
1002 position north_west() { return position(cent.x - dim.x/(M_SQRT2*2.0),
1003 cent.y + dim.y/(M_SQRT2*2.0)); }
1004 position south_east() { return position(cent.x + dim.x/(M_SQRT2*2.0),
1005 cent.y - dim.y/(M_SQRT2*2.0)); }
1006 position south_west() { return position(cent.x - dim.x/(M_SQRT2*2.0),
1007 cent.y - dim.y/(M_SQRT2*2.0)); }
1008 double radius() { return dim.x/2.0; }
1009 object_type type() { return ELLIPSE_OBJECT; }
1013 ellipse_object::ellipse_object(const position &d)
1018 void ellipse_object::print()
1020 if (lt.type == line_type::invisible && fill < 0.0 && color_fill == 0)
1022 out->set_color(color_fill, graphic_object::get_outline_color());
1023 out->ellipse(cent, dim, lt, fill);
1027 graphic_object *object_spec::make_ellipse(position *curpos, direction *dirp)
1029 static double last_ellipse_height;
1030 static double last_ellipse_width;
1031 static int have_last_ellipse = 0;
1032 if (!(flags & HAS_HEIGHT)) {
1033 if ((flags & IS_SAME) && have_last_ellipse)
1034 height = last_ellipse_height;
1036 lookup_variable("ellipseht", &height);
1038 if (!(flags & HAS_WIDTH)) {
1039 if ((flags & IS_SAME) && have_last_ellipse)
1040 width = last_ellipse_width;
1042 lookup_variable("ellipsewid", &width);
1044 last_ellipse_width = width;
1045 last_ellipse_height = height;
1046 have_last_ellipse = 1;
1047 ellipse_object *p = new ellipse_object(position(width, height));
1048 if (!position_rectangle(p, curpos, dirp)) {
1055 class circle_object : public ellipse_object {
1057 circle_object(double);
1058 object_type type() { return CIRCLE_OBJECT; }
1062 circle_object::circle_object(double diam)
1063 : ellipse_object(position(diam, diam))
1067 void circle_object::print()
1069 if (lt.type == line_type::invisible && fill < 0.0 && color_fill == 0)
1071 out->set_color(color_fill, graphic_object::get_outline_color());
1072 out->circle(cent, dim.x/2.0, lt, fill);
1076 graphic_object *object_spec::make_circle(position *curpos, direction *dirp)
1078 static double last_circle_radius;
1079 static int have_last_circle = 0;
1080 if (!(flags & HAS_RADIUS)) {
1081 if ((flags & IS_SAME) && have_last_circle)
1082 radius = last_circle_radius;
1084 lookup_variable("circlerad", &radius);
1086 last_circle_radius = radius;
1087 have_last_circle = 1;
1088 circle_object *p = new circle_object(radius*2.0);
1089 if (!position_rectangle(p, curpos, dirp)) {
1096 class move_object : public graphic_object {
1100 move_object(const position &s, const position &e);
1101 position origin() { return en; }
1102 object_type type() { return MOVE_OBJECT; }
1103 void update_bounding_box(bounding_box *);
1104 void move_by(const position &);
1107 move_object::move_object(const position &s, const position &e)
1112 void move_object::update_bounding_box(bounding_box *p)
1118 void move_object::move_by(const position &a)
1124 graphic_object *object_spec::make_move(position *curpos, direction *dirp)
1126 static position last_move;
1127 static int have_last_move = 0;
1129 // No need to look at at since `at' attribute sets `from' attribute.
1130 position startpos = (flags & HAS_FROM) ? from : *curpos;
1131 if (!(flags & HAS_SEGMENT)) {
1132 if ((flags & IS_SAME) && have_last_move)
1133 segment_pos = last_move;
1137 segment_pos.y = segment_height;
1139 case DOWN_DIRECTION:
1140 segment_pos.y = -segment_height;
1142 case LEFT_DIRECTION:
1143 segment_pos.x = -segment_width;
1145 case RIGHT_DIRECTION:
1146 segment_pos.x = segment_width;
1153 segment_list = new segment(segment_pos, segment_is_absolute, segment_list);
1154 // Reverse the segment_list so that it's in forward order.
1155 segment *old = segment_list;
1158 segment *tem = old->next;
1159 old->next = segment_list;
1163 // Compute the end position.
1164 position endpos = startpos;
1165 for (segment *s = segment_list; s; s = s->next)
1171 last_move = endpos - startpos;
1172 move_object *p = new move_object(startpos, endpos);
1177 class linear_object : public graphic_object {
1179 char arrow_at_start;
1181 arrow_head_type aht;
1185 linear_object(const position &s, const position &e);
1186 position start() { return strt; }
1187 position end() { return en; }
1188 void move_by(const position &);
1189 void update_bounding_box(bounding_box *) = 0;
1190 object_type type() = 0;
1191 void add_arrows(int at_start, int at_end, const arrow_head_type &);
1194 class line_object : public linear_object {
1199 line_object(const position &s, const position &e, position *, int);
1201 position origin() { return strt; }
1202 position center() { return (strt + en)/2.0; }
1203 position north() { return (en.y - strt.y) > 0 ? en : strt; }
1204 position south() { return (en.y - strt.y) < 0 ? en : strt; }
1205 position east() { return (en.x - strt.x) > 0 ? en : strt; }
1206 position west() { return (en.x - strt.x) < 0 ? en : strt; }
1207 object_type type() { return LINE_OBJECT; }
1208 void update_bounding_box(bounding_box *);
1210 void move_by(const position &);
1213 class arrow_object : public line_object {
1215 arrow_object(const position &, const position &, position *, int);
1216 object_type type() { return ARROW_OBJECT; }
1219 class spline_object : public line_object {
1221 spline_object(const position &, const position &, position *, int);
1222 object_type type() { return SPLINE_OBJECT; }
1224 void update_bounding_box(bounding_box *);
1227 linear_object::linear_object(const position &s, const position &e)
1228 : arrow_at_start(0), arrow_at_end(0), strt(s), en(e)
1232 void linear_object::move_by(const position &a)
1238 void linear_object::add_arrows(int at_start, int at_end,
1239 const arrow_head_type &a)
1241 arrow_at_start = at_start;
1242 arrow_at_end = at_end;
1246 line_object::line_object(const position &s, const position &e,
1248 : linear_object(s, e), v(p), n(i)
1252 void line_object::print()
1254 if (lt.type == line_type::invisible)
1256 out->set_color(0, graphic_object::get_outline_color());
1257 out->line(strt, v, n, lt);
1259 draw_arrow(strt, strt-v[0], aht, lt, graphic_object::get_outline_color());
1261 draw_arrow(en, v[n-1] - (n > 1 ? v[n - 2] : strt), aht, lt,
1262 graphic_object::get_outline_color());
1266 void line_object::update_bounding_box(bounding_box *p)
1269 for (int i = 0; i < n; i++)
1273 void line_object::move_by(const position &pos)
1275 linear_object::move_by(pos);
1276 for (int i = 0; i < n; i++)
1280 void spline_object::update_bounding_box(bounding_box *p)
1292 [ the points for the Bezier cubic ]
1300 (1-t)^3*p1 + 3*t*(t - 1)^2*p2 + 3*t^2*(1-t)*p3 + t^3*p4
1301 [ the equation for the Bezier cubic ]
1303 = .125*q1 + .75*q2 + .125*q3
1306 for (int i = 1; i < n; i++)
1307 p->encompass((i == 1 ? strt : v[i-2])*.125 + v[i-1]*.75 + v[i]*.125);
1310 arrow_object::arrow_object(const position &s, const position &e,
1312 : line_object(s, e, p, i)
1316 spline_object::spline_object(const position &s, const position &e,
1318 : line_object(s, e, p, i)
1322 void spline_object::print()
1324 if (lt.type == line_type::invisible)
1326 out->set_color(0, graphic_object::get_outline_color());
1327 out->spline(strt, v, n, lt);
1329 draw_arrow(strt, strt-v[0], aht, lt, graphic_object::get_outline_color());
1331 draw_arrow(en, v[n-1] - (n > 1 ? v[n - 2] : strt), aht, lt,
1332 graphic_object::get_outline_color());
1336 line_object::~line_object()
1341 linear_object *object_spec::make_line(position *curpos, direction *dirp)
1343 static position last_line;
1344 static int have_last_line = 0;
1346 // No need to look at at since `at' attribute sets `from' attribute.
1347 position startpos = (flags & HAS_FROM) ? from : *curpos;
1348 if (!(flags & HAS_SEGMENT)) {
1349 if ((flags & IS_SAME) && (type == LINE_OBJECT || type == ARROW_OBJECT)
1351 segment_pos = last_line;
1355 segment_pos.y = segment_height;
1357 case DOWN_DIRECTION:
1358 segment_pos.y = -segment_height;
1360 case LEFT_DIRECTION:
1361 segment_pos.x = -segment_width;
1363 case RIGHT_DIRECTION:
1364 segment_pos.x = segment_width;
1370 segment_list = new segment(segment_pos, segment_is_absolute, segment_list);
1371 // reverse the segment_list so that it's in forward order
1372 segment *old = segment_list;
1375 segment *tem = old->next;
1376 old->next = segment_list;
1380 // Absolutise all movements
1381 position endpos = startpos;
1384 for (s = segment_list; s; s = s->next, nsegments++)
1390 s->is_absolute = 1; // to avoid confusion
1394 position *v = new position[nsegments];
1396 for (s = segment_list; s; s = s->next, i++)
1398 if (flags & IS_DEFAULT_CHOPPED) {
1399 lookup_variable("circlerad", &start_chop);
1400 end_chop = start_chop;
1401 flags |= IS_CHOPPED;
1403 if (flags & IS_CHOPPED) {
1404 position start_chop_vec, end_chop_vec;
1405 if (start_chop != 0.0) {
1406 start_chop_vec = v[0] - startpos;
1407 start_chop_vec *= start_chop / hypot(start_chop_vec);
1409 if (end_chop != 0.0) {
1410 end_chop_vec = (v[nsegments - 1]
1411 - (nsegments > 1 ? v[nsegments - 2] : startpos));
1412 end_chop_vec *= end_chop / hypot(end_chop_vec);
1414 startpos += start_chop_vec;
1415 v[nsegments - 1] -= end_chop_vec;
1416 endpos -= end_chop_vec;
1420 p = new spline_object(startpos, endpos, v, nsegments);
1423 p = new arrow_object(startpos, endpos, v, nsegments);
1426 p = new line_object(startpos, endpos, v, nsegments);
1432 last_line = endpos - startpos;
1437 class arc_object : public linear_object {
1442 arc_object(int, const position &, const position &, const position &);
1443 position origin() { return cent; }
1444 position center() { return cent; }
1445 double radius() { return rad; }
1450 position north_east();
1451 position north_west();
1452 position south_east();
1453 position south_west();
1454 void update_bounding_box(bounding_box *);
1455 object_type type() { return ARC_OBJECT; }
1457 void move_by(const position &pos);
1460 arc_object::arc_object(int cw, const position &s, const position &e,
1462 : linear_object(s, e), clockwise(cw), cent(c)
1467 void arc_object::move_by(const position &pos)
1469 linear_object::move_by(pos);
1473 // we get arc corners from the corresponding circle
1475 position arc_object::north()
1477 position result(cent);
1482 position arc_object::south()
1484 position result(cent);
1489 position arc_object::east()
1491 position result(cent);
1496 position arc_object::west()
1498 position result(cent);
1503 position arc_object::north_east()
1505 position result(cent);
1506 result.x += rad/M_SQRT2;
1507 result.y += rad/M_SQRT2;
1511 position arc_object::north_west()
1513 position result(cent);
1514 result.x -= rad/M_SQRT2;
1515 result.y += rad/M_SQRT2;
1519 position arc_object::south_east()
1521 position result(cent);
1522 result.x += rad/M_SQRT2;
1523 result.y -= rad/M_SQRT2;
1527 position arc_object::south_west()
1529 position result(cent);
1530 result.x -= rad/M_SQRT2;
1531 result.y -= rad/M_SQRT2;
1536 void arc_object::print()
1538 if (lt.type == line_type::invisible)
1540 out->set_color(0, graphic_object::get_outline_color());
1542 out->arc(en, cent, strt, lt);
1544 out->arc(strt, cent, en, lt);
1545 if (arrow_at_start) {
1546 position c = cent - strt;
1548 (clockwise ? position(c.y, -c.x) : position(-c.y, c.x)),
1549 aht, lt, graphic_object::get_outline_color());
1552 position e = en - cent;
1554 (clockwise ? position(e.y, -e.x) : position(-e.y, e.x)),
1555 aht, lt, graphic_object::get_outline_color());
1560 inline double max(double a, double b)
1562 return a > b ? a : b;
1565 void arc_object::update_bounding_box(bounding_box *p)
1569 position start_offset = strt - cent;
1570 if (start_offset.x == 0.0 && start_offset.y == 0.0)
1572 position end_offset = en - cent;
1573 if (end_offset.x == 0.0 && end_offset.y == 0.0)
1575 double start_quad = atan2(start_offset.y, start_offset.x)/(M_PI/2.0);
1576 double end_quad = atan2(end_offset.y, end_offset.x)/(M_PI/2.0);
1578 double temp = start_quad;
1579 start_quad = end_quad;
1582 if (start_quad < 0.0)
1584 while (end_quad <= start_quad)
1586 double radius = max(hypot(start_offset), hypot(end_offset));
1587 for (int q = int(start_quad) + 1; q < end_quad; q++) {
1603 p->encompass(cent + offset);
1607 // We ignore the with attribute. The at attribute always refers to the center.
1609 linear_object *object_spec::make_arc(position *curpos, direction *dirp)
1612 int cw = (flags & IS_CLOCKWISE) != 0;
1613 // compute the start
1615 if (flags & HAS_FROM)
1619 if (!(flags & HAS_RADIUS))
1620 lookup_variable("arcrad", &radius);
1626 position m(radius, radius);
1627 // Adjust the signs.
1629 if (dir == DOWN_DIRECTION || dir == LEFT_DIRECTION)
1631 if (dir == DOWN_DIRECTION || dir == RIGHT_DIRECTION)
1633 *dirp = direction((dir + 3) % 4);
1636 if (dir == UP_DIRECTION || dir == LEFT_DIRECTION)
1638 if (dir == DOWN_DIRECTION || dir == LEFT_DIRECTION)
1640 *dirp = direction((dir + 1) % 4);
1642 endpos = startpos + m;
1644 // compute the center
1648 else if (startpos == endpos)
1649 centerpos = startpos;
1651 position h = (endpos - startpos)/2.0;
1652 double d = hypot(h);
1655 // make the radius big enough
1658 double alpha = acos(d/radius);
1659 double theta = atan2(h.y, h.x);
1664 centerpos = position(cos(theta), sin(theta))*radius + startpos;
1666 arc_object *p = new arc_object(cw, startpos, endpos, centerpos);
1671 graphic_object *object_spec::make_linear(position *curpos, direction *dirp)
1674 if (type == ARC_OBJECT)
1675 obj = make_arc(curpos, dirp);
1677 obj = make_line(curpos, dirp);
1678 if (type == ARROW_OBJECT
1679 && (flags & (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD)) == 0)
1680 flags |= HAS_RIGHT_ARROW_HEAD;
1681 if (obj && (flags & (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD))) {
1683 int at_start = (flags & HAS_LEFT_ARROW_HEAD) != 0;
1684 int at_end = (flags & HAS_RIGHT_ARROW_HEAD) != 0;
1685 if (flags & HAS_HEIGHT)
1688 lookup_variable("arrowht", &a.height);
1689 if (flags & HAS_WIDTH)
1692 lookup_variable("arrowwid", &a.width);
1694 lookup_variable("arrowhead", &solid);
1695 a.solid = solid != 0.0;
1696 obj->add_arrows(at_start, at_end, a);
1701 object *object_spec::make_object(position *curpos, direction *dirp)
1703 graphic_object *obj = 0;
1706 obj = make_block(curpos, dirp);
1709 obj = make_box(curpos, dirp);
1712 obj = make_text(curpos, dirp);
1714 case ELLIPSE_OBJECT:
1715 obj = make_ellipse(curpos, dirp);
1718 obj = make_circle(curpos, dirp);
1721 obj = make_move(curpos, dirp);
1727 obj = make_linear(curpos, dirp);
1736 if (flags & IS_INVISIBLE)
1737 obj->set_invisible();
1739 obj->add_text(text, (flags & IS_ALIGNED) != 0);
1740 if (flags & IS_DOTTED)
1741 obj->set_dotted(dash_width);
1742 else if (flags & IS_DASHED)
1743 obj->set_dashed(dash_width);
1745 if (flags & HAS_THICKNESS)
1748 lookup_variable("linethick", &th);
1749 obj->set_thickness(th);
1750 if (flags & IS_OUTLINED)
1751 obj->set_outline_color(outlined);
1752 if (flags & (IS_DEFAULT_FILLED|IS_FILLED)) {
1753 if (flags & IS_SHADED)
1754 obj->set_fill_color(shaded);
1756 if (flags & IS_DEFAULT_FILLED)
1757 lookup_variable("fillval", &fill);
1759 error("bad fill value %1", fill);
1761 obj->set_fill(fill);
1768 struct string_list {
1771 string_list(char *);
1775 string_list::string_list(char *s)
1780 string_list::~string_list()
1785 /* A path is used to hold the argument to the `with' attribute. For
1786 example, `.nw' or `.A.s' or `.A'. The major operation on a path is to
1787 take a place and follow the path through the place to place within the
1788 place. Note that `.A.B.C.sw' will work.
1790 For compatibility with DWB pic, `with' accepts positions also (this
1791 is incorrectly documented in CSTR 116). */
1793 path::path(corner c)
1794 : crn(c), label_list(0), ypath(0), is_position(0)
1798 path::path(position p)
1799 : crn(0), label_list(0), ypath(0), is_position(1)
1805 path::path(char *l, corner c)
1806 : crn(c), ypath(0), is_position(0)
1808 label_list = new string_list(l);
1813 while (label_list) {
1814 string_list *tem = label_list;
1815 label_list = label_list->next;
1821 void path::append(corner c)
1827 void path::append(char *s)
1830 for (p = &label_list; *p; p = &(*p)->next)
1832 *p = new string_list(s);
1835 void path::set_ypath(path *p)
1840 // return non-zero for success
1842 int path::follow(const place &pl, place *result) const
1850 const place *p = &pl;
1851 for (string_list *lb = label_list; lb; lb = lb->next)
1852 if (p->obj == 0 || (p = p->obj->find_label(lb->str)) == 0) {
1853 lex_error("object does not contain a place `%1'", lb->str);
1856 if (crn == 0 || p->obj == 0)
1859 position ps = ((p->obj)->*(crn))();
1866 if (!ypath->follow(pl, &tem))
1869 if (result->obj != tem.obj)
1875 void print_object_list(object *p)
1877 for (; p; p = p->next) {
1883 void print_picture(object *obj)
1886 for (object *p = obj; p; p = p->next)
1887 p->update_bounding_box(&bb);
1889 lookup_variable("scale", &scale);
1890 out->start_picture(scale, bb.ll, bb.ur);
1891 print_object_list(obj);
1892 out->finish_picture();