4 /* Copyright (C) 1989, 1990, 1991, 1992, 2003 Free Software Foundation, Inc.
5 Written by James Clark (jjc@jclark.com)
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING. If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
29 class tex_output
: public common_output
{
33 void start_picture(double, const position
&ll
, const position
&ur
);
34 void finish_picture();
35 void text(const position
&, text_piece
*, int, double);
36 void line(const position
&, const position
*, int n
,
38 void polygon(const position
*, int n
,
39 const line_type
&, double);
40 void spline(const position
&, const position
*, int n
,
42 void arc(const position
&, const position
&, const position
&,
44 void circle(const position
&, double rad
, const line_type
&, double);
45 void ellipse(const position
&, const distance
&, const line_type
&, double);
46 void command(const char *, const char *, int);
47 void set_color(char *, char *);
49 char *get_last_filled();
50 char *get_outline_color();
51 int supports_filled_polygons();
59 void point(const position
&);
60 void dot(const position
&, const line_type
&);
61 void solid_arc(const position
¢
, double rad
, double start_angle
,
62 double end_angle
, const line_type
<
);
63 position
transform(const position
&);
65 virtual void set_pen_size(double ps
);
68 // convert inches to milliinches
70 inline int milliinches(double x
)
72 return int(x
*1000.0 + .5);
75 inline position
tex_output::transform(const position
&pos
)
77 return position((pos
.x
- upper_left
.x
)/scale
,
78 (upper_left
.y
- pos
.y
)/scale
);
81 output
*make_tex_output()
83 return new tex_output
;
86 tex_output::tex_output()
90 tex_output::~tex_output()
94 const int DEFAULT_PEN_SIZE
= 8;
96 void tex_output::set_pen_size(double ps
)
100 if (ps
!= pen_size
) {
102 printf(" \\special{pn %d}%%\n",
103 ps
< 0.0 ? DEFAULT_PEN_SIZE
: int(ps
*(1000.0/72.0) + .5));
107 void tex_output::start_picture(double sc
, const position
&ll
,
112 scale
= compute_scale(sc
, ll
, ur
);
113 height
= (ur
.y
- ll
.y
)/scale
;
114 width
= (ur
.x
- ll
.x
)/scale
;
115 /* The point of \vskip 0pt is to ensure that the vtop gets
116 a height of 0 rather than the height of the hbox; this
117 might be non-zero if text from text attributes lies outside pic's
118 idea of the bounding box of the picture. */
119 /* \newbox and \newdimen are defined with \outer in plain.tex and can't
120 be used directly in an \if clause. */
121 printf("\\expandafter\\ifx\\csname %s\\endcsname\\relax\n"
122 " \\csname newbox\\expandafter\\endcsname\\csname %s\\endcsname\n"
124 "\\ifx\\graphtemp\\undefined\n"
125 " \\csname newdimen\\endcsname\\graphtemp\n"
127 "\\expandafter\\setbox\\csname %s\\endcsname\n"
128 " =\\vtop{\\vskip 0pt\\hbox{%%\n",
129 graphname
, graphname
, graphname
);
133 void tex_output::finish_picture()
135 printf(" \\hbox{\\vrule depth%.3fin width0pt height 0pt}%%\n"
142 void tex_output::text(const position
¢er
, text_piece
*v
, int n
, double)
144 position c
= transform(center
);
145 for (int i
= 0; i
< n
; i
++)
146 if (v
[i
].text
!= 0 && *v
[i
].text
!= '\0') {
148 if (v
[i
].adj
.v
== ABOVE_ADJUST
)
150 else if (v
[i
].adj
.v
== BELOW_ADJUST
)
153 printf(" \\graphtemp=.5ex\n"
154 " \\advance\\graphtemp by %.3fin\n", c
.y
);
157 printf(" \\graphtemp=\\baselineskip\n"
158 " \\multiply\\graphtemp by %d\n"
159 " \\divide\\graphtemp by 2\n"
160 " \\advance\\graphtemp by .5ex\n"
161 " \\advance\\graphtemp by %.3fin\n",
164 printf(" \\rlap{\\kern %.3fin\\lower\\graphtemp", c
.x
);
165 fputs("\\hbox to 0pt{", stdout
);
166 if (v
[i
].adj
.h
!= LEFT_ADJUST
)
167 fputs("\\hss ", stdout
);
168 fputs(v
[i
].text
, stdout
);
169 if (v
[i
].adj
.h
!= RIGHT_ADJUST
)
170 fputs("\\hss", stdout
);
171 fputs("}}%\n", stdout
);
175 void tex_output::point(const position
&pos
)
177 position p
= transform(pos
);
178 printf(" \\special{pa %d %d}%%\n", milliinches(p
.x
), milliinches(p
.y
));
181 void tex_output::line(const position
&start
, const position
*v
, int n
,
184 set_pen_size(lt
.thickness
);
186 for (int i
= 0; i
< n
; i
++)
188 fputs(" \\special{", stdout
);
190 case line_type::invisible
:
193 case line_type::solid
:
196 case line_type::dotted
:
197 printf("dt %.3f", lt
.dash_width
/scale
);
199 case line_type::dashed
:
200 printf("da %.3f", lt
.dash_width
/scale
);
203 fputs("}%\n", stdout
);
206 void tex_output::polygon(const position
*v
, int n
,
207 const line_type
<
, double fill
)
212 printf(" \\special{sh %.3f}%%\n", fill
);
214 line(v
[n
-1], v
, n
, lt
);
217 void tex_output::spline(const position
&start
, const position
*v
, int n
,
220 if (lt
.type
== line_type::invisible
)
222 set_pen_size(lt
.thickness
);
224 for (int i
= 0; i
< n
; i
++)
226 fputs(" \\special{sp", stdout
);
228 case line_type::solid
:
230 case line_type::dotted
:
231 printf(" %.3f", -lt
.dash_width
/scale
);
233 case line_type::dashed
:
234 printf(" %.3f", lt
.dash_width
/scale
);
236 case line_type::invisible
:
239 fputs("}%\n", stdout
);
242 void tex_output::solid_arc(const position
¢
, double rad
,
243 double start_angle
, double end_angle
,
246 set_pen_size(lt
.thickness
);
247 position c
= transform(cent
);
248 printf(" \\special{ar %d %d %d %d %f %f}%%\n",
251 milliinches(rad
/scale
),
252 milliinches(rad
/scale
),
254 (-end_angle
> -start_angle
) ? (double)M_PI
* 2 - start_angle
258 void tex_output::arc(const position
&start
, const position
¢
,
259 const position
&end
, const line_type
<
)
262 case line_type::invisible
:
264 case line_type::dashed
:
265 dashed_arc(start
, cent
, end
, lt
);
267 case line_type::dotted
:
268 dotted_arc(start
, cent
, end
, lt
);
270 case line_type::solid
:
273 if (!compute_arc_center(start
, cent
, end
, &c
)) {
274 line(start
, &end
, 1, lt
);
279 atan2(start
.y
- c
.y
, start
.x
- c
.x
),
280 atan2(end
.y
- c
.y
, end
.x
- c
.x
),
287 void tex_output::circle(const position
¢
, double rad
,
288 const line_type
<
, double fill
)
290 if (fill
>= 0.0 && lt
.type
!= line_type::solid
) {
294 ilt
.type
= line_type::invisible
;
295 ellipse(cent
, position(rad
*2.0, rad
*2.0), ilt
, fill
);
298 case line_type::dashed
:
299 dashed_circle(cent
, rad
, lt
);
301 case line_type::invisible
:
303 case line_type::solid
:
304 ellipse(cent
, position(rad
*2.0,rad
*2.0), lt
, fill
);
306 case line_type::dotted
:
307 dotted_circle(cent
, rad
, lt
);
314 void tex_output::ellipse(const position
¢
, const distance
&dim
,
315 const line_type
<
, double fill
)
317 if (lt
.type
== line_type::invisible
) {
322 set_pen_size(lt
.thickness
);
326 printf(" \\special{sh %.3f}%%\n", fill
);
328 position c
= transform(cent
);
330 case line_type::solid
:
331 case line_type::invisible
:
332 printf(" \\special{%s %d %d %d %d 0 6.28319}%%\n",
333 (lt
.type
== line_type::invisible
? "ia" : "ar"),
336 milliinches(dim
.x
/(2.0*scale
)),
337 milliinches(dim
.y
/(2.0*scale
)));
339 case line_type::dashed
:
340 dashed_ellipse(cent
, dim
/ scale
, lt
);
342 case line_type::dotted
:
343 dotted_ellipse(cent
, dim
/ scale
, lt
);
350 void tex_output::command(const char *s
, const char *, int)
353 putchar('%'); // avoid unwanted spaces
357 int tex_output::supports_filled_polygons()
362 void tex_output::dot(const position
&pos
, const line_type
<
)
364 if (zero_length_line_flag
) {
366 slt
.type
= line_type::solid
;
367 line(pos
, &pos
, 1, slt
);
370 int dot_rad
= int(lt
.thickness
*(1000.0/(72.0*2)) + .5);
373 position p
= transform(pos
);
374 printf(" \\special{sh 1}%%\n"
375 " \\special{ia %d %d %d %d 0 6.28319}%%\n",
376 milliinches(p
.x
), milliinches(p
.y
), dot_rad
, dot_rad
);
380 void tex_output::set_color(char *, char *)
382 /* not implemented yet */
385 void tex_output::reset_color()
387 /* not implemented yet */
390 char *tex_output::get_last_filled()
392 /* not implemented yet */
396 char *tex_output::get_outline_color()
398 /* not implemented yet */
402 class tpic_output
: public tex_output
{
405 void command(const char *, const char *, int);
407 void set_pen_size(double ps
);
408 int default_pen_size
;
409 int prev_default_pen_size
;
412 tpic_output::tpic_output()
413 : default_pen_size(DEFAULT_PEN_SIZE
), prev_default_pen_size(DEFAULT_PEN_SIZE
)
417 void tpic_output::command(const char *s
, const char *filename
, int lineno
)
420 if (s
[1] == 'p' && s
[2] == 's' && (s
[3] == '\0' || !csalpha(s
[3]))) {
421 const char *p
= s
+ 3;
425 int temp
= default_pen_size
;
426 default_pen_size
= prev_default_pen_size
;
427 prev_default_pen_size
= temp
;
431 int temp
= (int)strtol(p
, &ptr
, 10);
432 if (temp
== 0 && ptr
== p
)
433 error_with_file_and_line(filename
, lineno
,
434 "argument to `.ps' not an integer");
436 error_with_file_and_line(filename
, lineno
,
437 "negative pen size");
439 prev_default_pen_size
= default_pen_size
;
440 default_pen_size
= temp
;
445 printf("\\%s%%\n", s
+ 1);
448 void tpic_output::set_pen_size(double ps
)
451 printf(" \\special{pn %d}%%\n", default_pen_size
);
453 tex_output::set_pen_size(ps
);
456 output
*make_tpic_output()
458 return new tpic_output
;