2 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
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 3 of the License, or
10 (at your option) any later version.
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
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 class tex_output : public common_output {
30 void start_picture(double, const position &ll, const position &ur);
31 void finish_picture();
32 void text(const position &, text_piece *, int, double);
33 void line(const position &, const position *, int n,
35 void polygon(const position *, int n,
36 const line_type &, double);
37 void spline(const position &, const position *, int n,
39 void arc(const position &, const position &, const position &,
41 void circle(const position &, double rad, const line_type &, double);
42 void ellipse(const position &, const distance &, const line_type &, double);
43 void command(const char *, const char *, int);
44 void set_color(char *, char *);
46 char *get_last_filled();
47 char *get_outline_color();
48 int supports_filled_polygons();
56 void point(const position &);
57 void dot(const position &, const line_type &);
58 void solid_arc(const position ¢, double rad, double start_angle,
59 double end_angle, const line_type <);
60 position transform(const position &);
62 virtual void set_pen_size(double ps);
65 // convert inches to milliinches
67 inline int milliinches(double x)
69 return int(x*1000.0 + .5);
72 inline position tex_output::transform(const position &pos)
74 return position((pos.x - upper_left.x)/scale,
75 (upper_left.y - pos.y)/scale);
78 output *make_tex_output()
80 return new tex_output;
83 tex_output::tex_output()
87 tex_output::~tex_output()
91 const int DEFAULT_PEN_SIZE = 8;
93 void tex_output::set_pen_size(double ps)
99 printf(" \\special{pn %d}%%\n",
100 ps < 0.0 ? DEFAULT_PEN_SIZE : int(ps*(1000.0/72.0) + .5));
104 void tex_output::start_picture(double sc, const position &ll,
109 scale = compute_scale(sc, ll, ur);
110 height = (ur.y - ll.y)/scale;
111 width = (ur.x - ll.x)/scale;
112 /* The point of \vskip 0pt is to ensure that the vtop gets
113 a height of 0 rather than the height of the hbox; this
114 might be non-zero if text from text attributes lies outside pic's
115 idea of the bounding box of the picture. */
116 /* \newbox and \newdimen are defined with \outer in plain.tex and can't
117 be used directly in an \if clause. */
118 printf("\\expandafter\\ifx\\csname %s\\endcsname\\relax\n"
119 " \\csname newbox\\expandafter\\endcsname\\csname %s\\endcsname\n"
121 "\\ifx\\graphtemp\\undefined\n"
122 " \\csname newdimen\\endcsname\\graphtemp\n"
124 "\\expandafter\\setbox\\csname %s\\endcsname\n"
125 " =\\vtop{\\vskip 0pt\\hbox{%%\n",
126 graphname, graphname, graphname);
130 void tex_output::finish_picture()
132 printf(" \\hbox{\\vrule depth%.3fin width0pt height 0pt}%%\n"
139 void tex_output::text(const position ¢er, text_piece *v, int n, double)
141 position c = transform(center);
142 for (int i = 0; i < n; i++)
143 if (v[i].text != 0 && *v[i].text != '\0') {
145 if (v[i].adj.v == ABOVE_ADJUST)
147 else if (v[i].adj.v == BELOW_ADJUST)
150 printf(" \\graphtemp=.5ex\n"
151 " \\advance\\graphtemp by %.3fin\n", c.y);
154 printf(" \\graphtemp=\\baselineskip\n"
155 " \\multiply\\graphtemp by %d\n"
156 " \\divide\\graphtemp by 2\n"
157 " \\advance\\graphtemp by .5ex\n"
158 " \\advance\\graphtemp by %.3fin\n",
161 printf(" \\rlap{\\kern %.3fin\\lower\\graphtemp", c.x);
162 fputs("\\hbox to 0pt{", stdout);
163 if (v[i].adj.h != LEFT_ADJUST)
164 fputs("\\hss ", stdout);
165 fputs(v[i].text, stdout);
166 if (v[i].adj.h != RIGHT_ADJUST)
167 fputs("\\hss", stdout);
168 fputs("}}%\n", stdout);
172 void tex_output::point(const position &pos)
174 position p = transform(pos);
175 printf(" \\special{pa %d %d}%%\n", milliinches(p.x), milliinches(p.y));
178 void tex_output::line(const position &start, const position *v, int n,
181 set_pen_size(lt.thickness);
183 for (int i = 0; i < n; i++)
185 fputs(" \\special{", stdout);
187 case line_type::invisible:
190 case line_type::solid:
193 case line_type::dotted:
194 printf("dt %.3f", lt.dash_width/scale);
196 case line_type::dashed:
197 printf("da %.3f", lt.dash_width/scale);
200 fputs("}%\n", stdout);
203 void tex_output::polygon(const position *v, int n,
204 const line_type <, double fill)
209 printf(" \\special{sh %.3f}%%\n", fill);
211 line(v[n-1], v, n, lt);
214 void tex_output::spline(const position &start, const position *v, int n,
217 if (lt.type == line_type::invisible)
219 set_pen_size(lt.thickness);
221 for (int i = 0; i < n; i++)
223 fputs(" \\special{sp", stdout);
225 case line_type::solid:
227 case line_type::dotted:
228 printf(" %.3f", -lt.dash_width/scale);
230 case line_type::dashed:
231 printf(" %.3f", lt.dash_width/scale);
233 case line_type::invisible:
236 fputs("}%\n", stdout);
239 void tex_output::solid_arc(const position ¢, double rad,
240 double start_angle, double end_angle,
243 set_pen_size(lt.thickness);
244 position c = transform(cent);
245 printf(" \\special{ar %d %d %d %d %f %f}%%\n",
248 milliinches(rad/scale),
249 milliinches(rad/scale),
251 (-end_angle > -start_angle) ? (double)M_PI * 2 - start_angle
255 void tex_output::arc(const position &start, const position ¢,
256 const position &end, const line_type <)
259 case line_type::invisible:
261 case line_type::dashed:
262 dashed_arc(start, cent, end, lt);
264 case line_type::dotted:
265 dotted_arc(start, cent, end, lt);
267 case line_type::solid:
270 if (!compute_arc_center(start, cent, end, &c)) {
271 line(start, &end, 1, lt);
276 atan2(start.y - c.y, start.x - c.x),
277 atan2(end.y - c.y, end.x - c.x),
284 void tex_output::circle(const position ¢, double rad,
285 const line_type <, double fill)
287 if (fill >= 0.0 && lt.type != line_type::solid) {
291 ilt.type = line_type::invisible;
292 ellipse(cent, position(rad*2.0, rad*2.0), ilt, fill);
295 case line_type::dashed:
296 dashed_circle(cent, rad, lt);
298 case line_type::invisible:
300 case line_type::solid:
301 ellipse(cent, position(rad*2.0,rad*2.0), lt, fill);
303 case line_type::dotted:
304 dotted_circle(cent, rad, lt);
311 void tex_output::ellipse(const position ¢, const distance &dim,
312 const line_type <, double fill)
314 if (lt.type == line_type::invisible) {
319 set_pen_size(lt.thickness);
323 printf(" \\special{sh %.3f}%%\n", fill);
325 position c = transform(cent);
327 case line_type::solid:
328 case line_type::invisible:
329 printf(" \\special{%s %d %d %d %d 0 6.28319}%%\n",
330 (lt.type == line_type::invisible ? "ia" : "ar"),
333 milliinches(dim.x/(2.0*scale)),
334 milliinches(dim.y/(2.0*scale)));
336 case line_type::dashed:
337 dashed_ellipse(cent, dim / scale, lt);
339 case line_type::dotted:
340 dotted_ellipse(cent, dim / scale, lt);
347 void tex_output::command(const char *s, const char *, int)
350 putchar('%'); // avoid unwanted spaces
354 int tex_output::supports_filled_polygons()
359 void tex_output::dot(const position &pos, const line_type <)
361 if (zero_length_line_flag) {
363 slt.type = line_type::solid;
364 line(pos, &pos, 1, slt);
367 int dot_rad = int(lt.thickness*(1000.0/(72.0*2)) + .5);
370 position p = transform(pos);
371 printf(" \\special{sh 1}%%\n"
372 " \\special{ia %d %d %d %d 0 6.28319}%%\n",
373 milliinches(p.x), milliinches(p.y), dot_rad, dot_rad);
377 void tex_output::set_color(char *, char *)
379 /* not implemented yet */
382 void tex_output::reset_color()
384 /* not implemented yet */
387 char *tex_output::get_last_filled()
389 /* not implemented yet */
393 char *tex_output::get_outline_color()
395 /* not implemented yet */
399 class tpic_output : public tex_output {
402 void command(const char *, const char *, int);
404 void set_pen_size(double ps);
405 int default_pen_size;
406 int prev_default_pen_size;
409 tpic_output::tpic_output()
410 : default_pen_size(DEFAULT_PEN_SIZE), prev_default_pen_size(DEFAULT_PEN_SIZE)
414 void tpic_output::command(const char *s, const char *filename, int lineno)
417 if (s[1] == 'p' && s[2] == 's' && (s[3] == '\0' || !csalpha(s[3]))) {
418 const char *p = s + 3;
422 int temp = default_pen_size;
423 default_pen_size = prev_default_pen_size;
424 prev_default_pen_size = temp;
428 int temp = (int)strtol(p, &ptr, 10);
429 if (temp == 0 && ptr == p)
430 error_with_file_and_line(filename, lineno,
431 "argument to `.ps' not an integer");
433 error_with_file_and_line(filename, lineno,
434 "negative pen size");
436 prev_default_pen_size = default_pen_size;
437 default_pen_size = temp;
442 printf("\\%s%%\n", s + 1);
445 void tpic_output::set_pen_size(double ps)
448 printf(" \\special{pn %d}%%\n", default_pen_size);
450 tex_output::set_pen_size(ps);
453 output *make_tpic_output()
455 return new tpic_output;