2 /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
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 1, 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 LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define DEFAULT_LINEWIDTH 40
24 static int linewidth
= DEFAULT_LINEWIDTH
;
26 static int draw_flag
= 1;
28 /* These values were chosen because:
30 (MULTIPLIER*SIZESCALE)/(RES*UNITWIDTH) == 1/(2^20 * 72.27)
32 and 57816 is an exact multiple of both 72.27*SIZESCALE and 72.
34 The width in the groff font file is the product of MULTIPLIER and the
35 width in the tfm file. */
38 #define RES_7227 (RES/7227)
39 #define UNITWIDTH 131072
45 class dvi_font
: public font
{
46 dvi_font(const char *);
51 void handle_unknown_font_command(int argc
, const char **argv
);
52 static dvi_font
*load_dvi_font(const char *);
55 dvi_font
*dvi_font::load_dvi_font(const char *s
)
57 dvi_font
*f
= new dvi_font(s
);
65 dvi_font::dvi_font(const char *nm
)
66 : font(nm
), checksum(0), design_size(0)
74 void dvi_font::handle_unknown_font_command(int argc
, const char **argv
)
77 if (strcmp(argv
[0], "checksum") == 0) {
79 fatal("`checksum' command requires exactly 1 argument");
80 checksum
= int(strtol(argv
[1], &ptr
, 10));
81 if (checksum
== 0 && ptr
== argv
[1]) {
82 fatal("bad checksum");
85 else if (strcmp(argv
[0], "designsize") == 0) {
87 fatal("`designsize' command requires exactly 1 argument");
88 design_size
= int(strtol(argv
[1], &ptr
, 10));
89 if (design_size
== 0 && ptr
== argv
[1]) {
90 fatal("bad design size");
100 output_font() : f(0) { }
103 class dvi_printer
: public printer
{
114 output_font output_font_table
[FONTS_MAX
];
123 void define_font(int);
125 void possibly_begin_line();
153 void moveto(int, int);
154 void out_string(const char *);
155 void out_signed(unsigned char, int);
156 void out_unsigned(unsigned char, int);
157 void do_special(const char *);
161 font
*make_font(const char *);
162 void begin_page(int);
164 void set_char(int, font
*, const environment
*, int w
);
165 void special(char *arg
, const environment
*env
);
167 void draw(int code
, int *p
, int np
, const environment
*env
);
171 class draw_dvi_printer
: public dvi_printer
{
174 void set_line_thickness(const environment
*);
179 void draw(int code
, int *p
, int np
, const environment
*env
);
183 dvi_printer::dvi_printer()
184 : byte_count(0), last_bop(-1), page_count(0), cur_font(0), fp(stdout
),
185 max_h(0), max_v(0), pushed(0), line_thickness(-1)
187 if (font::res
!= RES
)
188 fatal("resolution must be %1", RES
);
189 if (font::unitwidth
!= UNITWIDTH
)
190 fatal("unitwidth must be %1", UNITWIDTH
);
192 fatal("hor must be equal to 1");
194 fatal("vert must be equal to 1");
195 if (font::sizescale
!= SIZESCALE
)
196 fatal("sizescale must be equal to %1", SIZESCALE
);
197 max_drift
= font::res
/1000; // this is fairly arbitrary
201 dvi_printer::~dvi_printer()
207 draw_dvi_printer::draw_dvi_printer()
208 : output_pen_size(-1), fill(FILL_MAX
)
212 draw_dvi_printer::~draw_dvi_printer()
217 void dvi_printer::out1(int n
)
223 void dvi_printer::out2(int n
)
226 putc((n
>> 8) & 0xff, fp
);
230 void dvi_printer::out3(int n
)
233 putc((n
>> 16) & 0xff, fp
);
234 putc((n
>> 8) & 0xff, fp
);
238 void dvi_printer::out4(int n
)
241 putc((n
>> 24) & 0xff, fp
);
242 putc((n
>> 16) & 0xff, fp
);
243 putc((n
>> 8) & 0xff, fp
);
247 void dvi_printer::out_string(const char *s
)
255 void dvi_printer::end_of_line()
265 void dvi_printer::possibly_begin_line()
268 have_pushed
= pushed
= 1;
275 int scale(int x
, int z
)
280 alpha
= 16*z
; beta
= 16;
281 while (z
>= 040000000L) {
288 sw
= (((((d
* z
) / 0400) + (c
* z
)) / 0400) + (b
* z
)) / beta
;
297 void dvi_printer::set_char(int index
, font
*f
, const environment
*env
, int w
)
299 unsigned char code
= f
->get_code(index
);
300 if (env
->size
!= cur_point_size
|| f
!= cur_font
) {
302 cur_point_size
= env
->size
;
303 for (int i
= 0;; i
++) {
304 if (i
>= FONTS_MAX
) {
305 fatal("too many output fonts required");
307 if (output_font_table
[i
].f
== 0) {
308 output_font_table
[i
].f
= (dvi_font
*)cur_font
;
309 output_font_table
[i
].point_size
= cur_point_size
;
312 if (output_font_table
[i
].f
== cur_font
313 && output_font_table
[i
].point_size
== cur_point_size
)
318 int distance
= env
->hpos
- cur_h
;
319 if (env
->hpos
!= end_h
&& distance
!= 0) {
320 out_signed(right1
, distance
);
323 else if (distance
> max_drift
) {
324 out_signed(right1
, distance
- max_drift
);
325 cur_h
= env
->hpos
- max_drift
;
327 else if (distance
< -max_drift
) {
328 out_signed(right1
, distance
+ max_drift
);
329 cur_h
= env
->hpos
+ max_drift
;
331 if (env
->vpos
!= cur_v
) {
332 out_signed(down1
, env
->vpos
- cur_v
);
335 possibly_begin_line();
336 end_h
= env
->hpos
+ w
;
337 cur_h
+= scale(f
->get_width(index
, UNITWIDTH
)/MULTIPLIER
,
338 cur_point_size
*RES_7227
);
346 out_unsigned(set1
, code
);
349 void dvi_printer::define_font(int i
)
351 out_unsigned(fnt_def1
, i
);
352 dvi_font
*f
= output_font_table
[i
].f
;
354 out4(output_font_table
[i
].point_size
*RES_7227
);
355 out4(int((double(f
->design_size
)/(1<<20))*RES_7227
*100 + .5));
356 const char *nm
= f
->get_internal_name();
361 void dvi_printer::set_font(int i
)
363 if (i
>= 0 && i
<= 63)
366 out_unsigned(fnt1
, i
);
369 void dvi_printer::out_signed(unsigned char base
, int param
)
371 if (-128 <= param
&& param
< 128) {
375 else if (-32768 <= param
&& param
< 32768) {
379 else if (-(1 << 23) <= param
&& param
< (1 << 23)) {
389 void dvi_printer::out_unsigned(unsigned char base
, int param
)
396 else if (param
< 65536) {
400 else if (param
< (1 << 24)) {
415 void dvi_printer::preamble()
425 void dvi_printer::postamble()
427 int tem
= byte_count
;
435 out2(have_pushed
); // stack depth
438 for (i
= 0; i
< FONTS_MAX
&& output_font_table
[i
].f
!= 0; i
++)
443 for (i
= 0; i
< 4 || byte_count
% 4 != 0; i
++)
447 void dvi_printer::begin_page(int i
)
450 int tem
= byte_count
;
453 for (int j
= 1; j
< 10; j
++)
457 // By convention position (0,0) in a dvi file is placed at (1in, 1in).
463 void dvi_printer::end_page()
471 void draw_dvi_printer::end_page()
473 dvi_printer::end_page();
474 output_pen_size
= -1;
477 void dvi_printer::do_special(const char *s
)
482 possibly_begin_line();
483 out_unsigned(xxx1
, len
);
488 void dvi_printer::special(char *arg
, const environment
*env
)
490 moveto(env
->hpos
, env
->vpos
);
494 void dvi_printer::moveto(int h
, int v
)
497 out_signed(right1
, h
- cur_h
);
503 out_signed(down1
, v
- cur_v
);
511 void dvi_printer::draw(int code
, int *p
, int np
, const environment
*env
)
515 int height
= 0, width
;
517 if (line_thickness
< 0)
518 thickness
= env
->size
*RES_7227
*linewidth
/1000;
519 else if (line_thickness
> 0)
520 thickness
= line_thickness
;
524 error("2 arguments required for line");
526 else if (p
[0] == 0) {
529 x
= env
->hpos
- thickness
/2;
530 y
= env
->vpos
+ p
[1] + thickness
/2;
531 height
= p
[1] + thickness
;
535 x
= env
->hpos
- thickness
/2;
536 y
= env
->vpos
+ thickness
/2;
537 height
= thickness
- p
[1];
541 else if (p
[1] == 0) {
543 x
= env
->hpos
- thickness
/2;
544 y
= env
->vpos
+ thickness
/2;
546 width
= p
[0] + thickness
;
549 x
= env
->hpos
- p
[0] - thickness
/2;
550 y
= env
->vpos
+ thickness
/2;
552 width
= thickness
- p
[0];
562 else if (code
== 't') {
567 // troff gratuitously adds an extra 0
568 if (np
!= 1 && np
!= 2)
569 error("0 or 1 argument required for thickness");
571 line_thickness
= p
[0];
574 else if (code
== 'R') {
576 error("2 arguments required for rule");
577 else if (p
[0] != 0 || p
[1] != 0) {
598 // XXX Will this overflow?
600 inline int milliinches(int n
)
602 return (n
*1000 + font::res
/2)/font::res
;
605 void draw_dvi_printer::set_line_thickness(const environment
*env
)
608 = milliinches(line_thickness
< 0
609 // Will this overflow?
610 ? env
->size
*RES_7227
*linewidth
/1000
612 if (desired_pen_size
!= output_pen_size
) {
614 sprintf(buf
, "pn %d", desired_pen_size
);
616 output_pen_size
= desired_pen_size
;
620 void draw_dvi_printer::fill_next()
623 sprintf(buf
, "sh %.3f", double(fill
)/FILL_MAX
);
627 void draw_dvi_printer::draw(int code
, int *p
, int np
, const environment
*env
)
636 // troff adds an extra argument to C
637 if (np
!= 1 && !(code
== 'C' && np
== 2)) {
638 error("1 argument required for circle");
641 moveto(env
->hpos
+p
[0]/2, env
->vpos
);
645 set_line_thickness(env
);
646 int rad
= milliinches(p
[0]/2);
647 sprintf(buf
, "%s 0 0 %d %d 0 6.28319",
648 (fill_flag
? "ia" : "ar"),
655 error("2 arguments required for line");
658 moveto(env
->hpos
, env
->vpos
);
659 set_line_thickness(env
);
660 do_special("pa 0 0");
661 sprintf(buf
, "pa %d %d", milliinches(p
[0]), milliinches(p
[1]));
670 error("2 arguments required for ellipse");
673 moveto(env
->hpos
+p
[0]/2, env
->vpos
);
676 sprintf(buf
, "%s 0 0 %d %d 0 6.28319",
677 (fill_flag
? "ia" : "ar"),
679 milliinches(p
[1]/2));
688 error("even number of arguments required for polygon");
692 error("no arguments for polygon");
695 moveto(env
->hpos
, env
->vpos
);
699 set_line_thickness(env
);
700 do_special("pa 0 0");
702 for (int i
= 0; i
< np
; i
+= 2) {
705 sprintf(buf
, "pa %d %d", milliinches(h
), milliinches(v
));
708 do_special("pa 0 0");
709 do_special(fill_flag
? "ip" : "fp");
715 error("even number of arguments required for spline");
719 error("no arguments for spline");
722 moveto(env
->hpos
, env
->vpos
);
723 set_line_thickness(env
);
724 do_special("pa 0 0");
726 for (int i
= 0; i
< np
; i
+= 2) {
729 sprintf(buf
, "pa %d %d", milliinches(h
), milliinches(v
));
738 error("4 arguments required for arc");
741 set_line_thickness(env
);
744 int eh
= p
[0] + p
[2];
745 int ev
= p
[1] + p
[3];
746 double n
= (double(ch
)*eh
) + (double(cv
)*ev
);
748 moveto(env
->hpos
, env
->vpos
);
749 do_special("pa 0 0");
750 sprintf(buf
, "pa %d %d", milliinches(eh
), milliinches(ev
));
755 double k
= (double(eh
)*eh
+ double(ev
)*ev
)/(2.0*n
);
758 double start_angle
= atan2(-v
, -h
);
759 double end_angle
= atan2(ev
- v
, eh
- h
);
760 int rad
= milliinches(int(sqrt(h
*h
+ v
*v
) + .5));
761 moveto(env
->hpos
+ int(h
), env
->vpos
+ int(v
));
762 sprintf(buf
, "ar 0 0 %d %d %f %f",
777 // troff gratuitously adds an extra 0
778 if (np
!= 1 && np
!= 2) {
779 error("0 or 1 argument required for thickness");
782 line_thickness
= p
[0];
788 if (np
!= 1 && np
!= 2) {
789 error("1 argument required for fill");
793 if (fill
< 0 || fill
> FILL_MAX
)
800 error("2 arguments required for rule");
826 error("unrecognised drawing command `%1'", char(code
));
831 font
*dvi_printer::make_font(const char *nm
)
833 return dvi_font::load_dvi_font(nm
);
836 printer
*make_printer()
839 return new draw_dvi_printer
;
841 return new dvi_printer
;
846 int main(int argc
, char **argv
)
848 program_name
= argv
[0];
849 static char stderr_buf
[BUFSIZ
];
850 setbuf(stderr
, stderr_buf
);
852 while ((c
= getopt(argc
, argv
, "F:vw:d")) != EOF
)
856 extern const char *version_string
;
857 fprintf(stderr
, "grodvi version %s\n", version_string
);
862 if (sscanf(optarg
, "%d", &linewidth
) != 1
863 || linewidth
< 0 || linewidth
> 1000) {
864 error("bad line width");
865 linewidth
= DEFAULT_LINEWIDTH
;
872 font::command_line_font_dir(optarg
);
883 for (int i
= optind
; i
< argc
; i
++)
892 fprintf(stderr
, "usage: %s [-dv] [-F dir] [-w n] [files ...]\n",