2 /* Copyright (C) 1989, 1990, 1991, 1992, 2003 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 2, or (at your option) any later
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 along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
27 class tex_output
: public common_output
{
31 void start_picture(double, const position
&ll
, const position
&ur
);
32 void finish_picture();
33 void text(const position
&, text_piece
*, int, double);
34 void line(const position
&, const position
*, int n
,
36 void polygon(const position
*, int n
,
37 const line_type
&, double);
38 void spline(const position
&, const position
*, int n
,
40 void arc(const position
&, const position
&, const position
&,
42 void circle(const position
&, double rad
, const line_type
&, double);
43 void ellipse(const position
&, const distance
&, const line_type
&, double);
44 void command(const char *, const char *, int);
45 void set_color(char *, char *);
47 char *get_last_filled();
48 char *get_outline_color();
49 int supports_filled_polygons();
57 void point(const position
&);
58 void dot(const position
&, const line_type
&);
59 void solid_arc(const position
¢
, double rad
, double start_angle
,
60 double end_angle
, const line_type
<
);
61 position
transform(const position
&);
63 virtual void set_pen_size(double ps
);
66 // convert inches to milliinches
68 inline int milliinches(double x
)
70 return int(x
*1000.0 + .5);
73 inline position
tex_output::transform(const position
&pos
)
75 return position((pos
.x
- upper_left
.x
)/scale
,
76 (upper_left
.y
- pos
.y
)/scale
);
79 output
*make_tex_output()
81 return new tex_output
;
84 tex_output::tex_output()
88 tex_output::~tex_output()
92 const int DEFAULT_PEN_SIZE
= 8;
94 void tex_output::set_pen_size(double ps
)
100 printf(" \\special{pn %d}%%\n",
101 ps
< 0.0 ? DEFAULT_PEN_SIZE
: int(ps
*(1000.0/72.0) + .5));
105 void tex_output::start_picture(double sc
, const position
&ll
,
110 scale
= compute_scale(sc
, ll
, ur
);
111 height
= (ur
.y
- ll
.y
)/scale
;
112 width
= (ur
.x
- ll
.x
)/scale
;
113 /* The point of \vskip 0pt is to ensure that the vtop gets
114 a height of 0 rather than the height of the hbox; this
115 might be non-zero if text from text attributes lies outside pic's
116 idea of the bounding box of the picture. */
117 /* \newbox and \newdimen are defined with \outer in plain.tex and can't
118 be used directly in an \if clause. */
119 printf("\\expandafter\\ifx\\csname %s\\endcsname\\relax\n"
120 " \\csname newbox\\expandafter\\endcsname\\csname %s\\endcsname\n"
122 "\\ifx\\graphtemp\\undefined\n"
123 " \\csname newdimen\\endcsname\\graphtemp\n"
125 "\\expandafter\\setbox\\csname %s\\endcsname\n"
126 " =\\vtop{\\vskip 0pt\\hbox{%%\n",
127 graphname
, graphname
, graphname
);
131 void tex_output::finish_picture()
133 printf(" \\hbox{\\vrule depth%.3fin width0pt height 0pt}%%\n"
140 void tex_output::text(const position
¢er
, text_piece
*v
, int n
, double)
142 position c
= transform(center
);
143 for (int i
= 0; i
< n
; i
++)
144 if (v
[i
].text
!= 0 && *v
[i
].text
!= '\0') {
146 if (v
[i
].adj
.v
== ABOVE_ADJUST
)
148 else if (v
[i
].adj
.v
== BELOW_ADJUST
)
151 printf(" \\graphtemp=.5ex\n"
152 " \\advance\\graphtemp by %.3fin\n", c
.y
);
155 printf(" \\graphtemp=\\baselineskip\n"
156 " \\multiply\\graphtemp by %d\n"
157 " \\divide\\graphtemp by 2\n"
158 " \\advance\\graphtemp by .5ex\n"
159 " \\advance\\graphtemp by %.3fin\n",
162 printf(" \\rlap{\\kern %.3fin\\lower\\graphtemp", c
.x
);
163 fputs("\\hbox to 0pt{", stdout
);
164 if (v
[i
].adj
.h
!= LEFT_ADJUST
)
165 fputs("\\hss ", stdout
);
166 fputs(v
[i
].text
, stdout
);
167 if (v
[i
].adj
.h
!= RIGHT_ADJUST
)
168 fputs("\\hss", stdout
);
169 fputs("}}%\n", stdout
);
173 void tex_output::point(const position
&pos
)
175 position p
= transform(pos
);
176 printf(" \\special{pa %d %d}%%\n", milliinches(p
.x
), milliinches(p
.y
));
179 void tex_output::line(const position
&start
, const position
*v
, int n
,
182 set_pen_size(lt
.thickness
);
184 for (int i
= 0; i
< n
; i
++)
186 fputs(" \\special{", stdout
);
188 case line_type::invisible
:
191 case line_type::solid
:
194 case line_type::dotted
:
195 printf("dt %.3f", lt
.dash_width
/scale
);
197 case line_type::dashed
:
198 printf("da %.3f", lt
.dash_width
/scale
);
201 fputs("}%\n", stdout
);
204 void tex_output::polygon(const position
*v
, int n
,
205 const line_type
<
, double fill
)
210 printf(" \\special{sh %.3f}%%\n", fill
);
212 line(v
[n
-1], v
, n
, lt
);
215 void tex_output::spline(const position
&start
, const position
*v
, int n
,
218 if (lt
.type
== line_type::invisible
)
220 set_pen_size(lt
.thickness
);
222 for (int i
= 0; i
< n
; i
++)
224 fputs(" \\special{sp", stdout
);
226 case line_type::solid
:
228 case line_type::dotted
:
229 printf(" %.3f", -lt
.dash_width
/scale
);
231 case line_type::dashed
:
232 printf(" %.3f", lt
.dash_width
/scale
);
234 case line_type::invisible
:
237 fputs("}%\n", stdout
);
240 void tex_output::solid_arc(const position
¢
, double rad
,
241 double start_angle
, double end_angle
,
244 set_pen_size(lt
.thickness
);
245 position c
= transform(cent
);
246 printf(" \\special{ar %d %d %d %d %f %f}%%\n",
249 milliinches(rad
/scale
),
250 milliinches(rad
/scale
),
252 (-end_angle
> -start_angle
) ? (double)M_PI
* 2 - start_angle
256 void tex_output::arc(const position
&start
, const position
¢
,
257 const position
&end
, const line_type
<
)
260 case line_type::invisible
:
262 case line_type::dashed
:
263 dashed_arc(start
, cent
, end
, lt
);
265 case line_type::dotted
:
266 dotted_arc(start
, cent
, end
, lt
);
268 case line_type::solid
:
271 if (!compute_arc_center(start
, cent
, end
, &c
)) {
272 line(start
, &end
, 1, lt
);
277 atan2(start
.y
- c
.y
, start
.x
- c
.x
),
278 atan2(end
.y
- c
.y
, end
.x
- c
.x
),
285 void tex_output::circle(const position
¢
, double rad
,
286 const line_type
<
, double fill
)
288 if (fill
>= 0.0 && lt
.type
!= line_type::solid
) {
292 ilt
.type
= line_type::invisible
;
293 ellipse(cent
, position(rad
*2.0, rad
*2.0), ilt
, fill
);
296 case line_type::dashed
:
297 dashed_circle(cent
, rad
, lt
);
299 case line_type::invisible
:
301 case line_type::solid
:
302 ellipse(cent
, position(rad
*2.0,rad
*2.0), lt
, fill
);
304 case line_type::dotted
:
305 dotted_circle(cent
, rad
, lt
);
312 void tex_output::ellipse(const position
¢
, const distance
&dim
,
313 const line_type
<
, double fill
)
315 if (lt
.type
== line_type::invisible
) {
320 set_pen_size(lt
.thickness
);
324 printf(" \\special{sh %.3f}%%\n", fill
);
326 position c
= transform(cent
);
328 case line_type::solid
:
329 case line_type::invisible
:
330 printf(" \\special{%s %d %d %d %d 0 6.28319}%%\n",
331 (lt
.type
== line_type::invisible
? "ia" : "ar"),
334 milliinches(dim
.x
/(2.0*scale
)),
335 milliinches(dim
.y
/(2.0*scale
)));
337 case line_type::dashed
:
338 dashed_ellipse(cent
, dim
/ scale
, lt
);
340 case line_type::dotted
:
341 dotted_ellipse(cent
, dim
/ scale
, lt
);
348 void tex_output::command(const char *s
, const char *, int)
351 putchar('%'); // avoid unwanted spaces
355 int tex_output::supports_filled_polygons()
360 void tex_output::dot(const position
&pos
, const line_type
<
)
362 if (zero_length_line_flag
) {
364 slt
.type
= line_type::solid
;
365 line(pos
, &pos
, 1, slt
);
368 int dot_rad
= int(lt
.thickness
*(1000.0/(72.0*2)) + .5);
371 position p
= transform(pos
);
372 printf(" \\special{sh 1}%%\n"
373 " \\special{ia %d %d %d %d 0 6.28319}%%\n",
374 milliinches(p
.x
), milliinches(p
.y
), dot_rad
, dot_rad
);
378 void tex_output::set_color(char *, char *)
380 /* not implemented yet */
383 void tex_output::reset_color()
385 /* not implemented yet */
388 char *tex_output::get_last_filled()
390 /* not implemented yet */
394 char *tex_output::get_outline_color()
396 /* not implemented yet */
400 class tpic_output
: public tex_output
{
403 void command(const char *, const char *, int);
405 void set_pen_size(double ps
);
406 int default_pen_size
;
407 int prev_default_pen_size
;
410 tpic_output::tpic_output()
411 : default_pen_size(DEFAULT_PEN_SIZE
), prev_default_pen_size(DEFAULT_PEN_SIZE
)
415 void tpic_output::command(const char *s
, const char *filename
, int lineno
)
418 if (s
[1] == 'p' && s
[2] == 's' && (s
[3] == '\0' || !csalpha(s
[3]))) {
419 const char *p
= s
+ 3;
423 int temp
= default_pen_size
;
424 default_pen_size
= prev_default_pen_size
;
425 prev_default_pen_size
= temp
;
429 int temp
= (int)strtol(p
, &ptr
, 10);
430 if (temp
== 0 && ptr
== p
)
431 error_with_file_and_line(filename
, lineno
,
432 "argument to `.ps' not an integer");
434 error_with_file_and_line(filename
, lineno
,
435 "negative pen size");
437 prev_default_pen_size
= default_pen_size
;
438 default_pen_size
= temp
;
443 printf("\\%s%%\n", s
+ 1);
446 void tpic_output::set_pen_size(double ps
)
449 printf(" \\special{pn %d}%%\n", default_pen_size
);
451 tex_output::set_pen_size(ps
);
454 output
*make_tpic_output()
456 return new tpic_output
;